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 pagination in AMP-compatible tab of plugins list page. #6681

Merged
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
18 changes: 9 additions & 9 deletions src/Admin/AmpPlugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public function add_tab( $tabs ) {
*/
public function filter_plugins_table_api_args() {

$per_page = 100; // @todo There are currently 56 plugins, so this will show all. This is done because pagination is not working.
$per_page = 36;
$total_page = ceil( count( $this->get_plugins() ) / $per_page );
$pagenum = isset( $_REQUEST['paged'] ) ? (int) $_REQUEST['paged'] : 1; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$pagenum = ( $pagenum > $total_page ) ? $total_page : $pagenum;
Expand Down Expand Up @@ -263,18 +263,18 @@ public function filter_plugins_api( $response, /** @noinspection PhpUnusedParame
return $response;
}

$plugins = $this->get_plugins();
$total_page = ceil( count( $plugins ) / $args['per_page'] );
$page = ( ! empty( $args['page'] ) && 0 < (int) $args['page'] ) ? (int) $args['page'] : 1;
$plugin_chunks = array_chunk( $plugins, $args['per_page'] );
$plugins = ( ! empty( $plugin_chunks[ $page - 1 ] ) && is_array( $plugin_chunks[ $page - 1 ] ) ) ? $plugin_chunks[ $page - 1 ] : [];
$page = ( ! empty( $args['page'] ) && 0 < (int) $args['page'] ) ? (int) $args['page'] : 1;
$plugins = $this->get_plugins();
$plugins_count = count( $plugins );
$plugins_chunks = array_chunk( $plugins, $args['per_page'] );
$plugins_chunk = ( ! empty( $plugins_chunks[ $page - 1 ] ) && is_array( $plugins_chunks[ $page - 1 ] ) ) ? $plugins_chunks[ $page - 1 ] : [];

$response = new stdClass();
$response->plugins = $plugins;
$response->plugins = $plugins_chunk;
$response->info = [
'page' => $page,
'pages' => $total_page,
'results' => count( $plugins ),
'pages' => count( $plugins_chunks ),
'results' => $plugins_count,
];

return $response;
Expand Down
25 changes: 11 additions & 14 deletions src/Admin/AmpThemes.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,31 +223,28 @@ public function enqueue_scripts() {
public function filter_themes_api( $override, $action, $args ) {

$args = (array) $args;
if ( ! isset( $args['browse'] ) || self::AMP_COMPATIBLE !== $args['browse'] ) {
if ( ! isset( $args['browse'] ) || self::AMP_COMPATIBLE !== $args['browse'] || 'query_themes' !== $action ) {
return $override;
}

$response = new stdClass();
$response->themes = [];

$per_page = max( 36, isset( $args['per_page'] ) ? (int) $args['per_page'] : 0 );
$page = max( 1, isset( $args['page'] ) ? (int) $args['page'] : 0 );
$themes = $this->get_themes();
$theme_chunks = array_chunk( $themes, $per_page );
$themes = ( ! empty( $theme_chunks[ $page - 1 ] ) && is_array( $theme_chunks[ $page - 1 ] ) ) ? $theme_chunks[ $page - 1 ] : [];
$per_page = max( 36, isset( $args['per_page'] ) ? (int) $args['per_page'] : 0 );
$page = max( 1, isset( $args['page'] ) ? (int) $args['page'] : 0 );
$themes = $this->get_themes();
$themes_count = count( $themes );
$themes_chunks = array_chunk( $themes, $per_page );
$themes_chunk = ( ! empty( $themes_chunks[ $page - 1 ] ) && is_array( $themes_chunks[ $page - 1 ] ) ) ? $themes_chunks[ $page - 1 ] : [];

if ( 'query_themes' === $action ) {
foreach ( $themes as $i => $theme ) {
$response->themes[ $i ] = (object) $theme;
}
} else {
$response->themes = $themes;
foreach ( $themes_chunk as $theme ) {
$response->themes[] = (object) $theme;
}

$response->info = [
'page' => $page,
'pages' => count( $theme_chunks ),
'results' => count( $themes ),
'pages' => count( $themes_chunks ),
'results' => $themes_count,
];

return $response;
Expand Down