Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix/164: Show pagination when Sort by Order is clicked #165

Merged
merged 5 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions simple-page-ordering.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,31 @@ public static function load_edit_screen() {
'sort_by_order_link',
)
);
add_action( 'pre_get_posts', array( __CLASS__, 'filter_query' ) );
add_action( 'wp', array( __CLASS__, 'wp' ) );
add_action( 'admin_head', array( __CLASS__, 'admin_head' ) );
}

/**
* This is to enable pagination.
*
* @param WP_Query $query The WP_Query instance (passed by reference).
*/
public static function filter_query( $query ) {
if ( ! $query->is_main_query() ) {
return;
}

// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$is_simple_page_ordering = isset( $_GET['id'] ) ? 'simple-page-ordering' === $_GET['id'] : false;

if ( ! $is_simple_page_ordering ) {
return;
}

$query->set( 'posts_per_page', -1 );
peterwilsoncc marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* when we load up our posts query, if we're actually sorting by menu order, initialize sorting scripts
*/
Expand Down Expand Up @@ -526,6 +547,7 @@ public static function sort_by_order_link( $views ) {
if ( ! is_post_type_hierarchical( get_post_type() ) ) {
$query_string = add_query_arg( 'orderby', 'menu_order title', $query_string );
$query_string = add_query_arg( 'order', 'asc', $query_string );
$query_string = add_query_arg( 'id', 'simple-page-ordering', $query_string );
}
$views['byorder'] = sprintf( '<a href="%s" class="%s">%s</a>', esc_url( $query_string ), $class, __( 'Sort by Order', 'simple-page-ordering' ) );

Expand Down
15 changes: 15 additions & 0 deletions tests/cypress/integration/admin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,19 @@ describe('Admin can login and make sure plugin is activated', () => {
cy.visit('/wp-admin/edit.php?post_type=page');
cy.get('.subsubsub .byorder').should('have.text', 'Sort by Order');
});

it('Pagination is visible when Clicking "Sort by Order"', () => {
cy.login();
cy.visit( '/wp-admin/edit.php?post_type=page' );
cy.get( '#show-settings-link' ).click();
cy.get( '#edit_page_per_page' ).clear().type( '2' );
cy.get( '#screen-options-apply' ).click();
cy.get( '.byorder' ).click();
cy.get( '.pagination-links' ).should( 'be.visible' );

// Restore default pagination.
cy.get( '#show-settings-link' ).click();
cy.get( '#edit_page_per_page' ).clear().type( '10' );
cy.get( '#screen-options-apply' ).click();
});
});
Loading