Make WordPress Core

Ticket #46346: integer-page-counts.patch

File integer-page-counts.patch, 14.3 KB (added by lev0, 3 years ago)

Add and utilise a standard function for this common calculation.

  • wp-admin/includes/ajax-actions.php

     
    520520        $time    = time();
    521521        $comment = get_comment( $comment_id );
    522522        $counts  = wp_count_comments();
     523        $total_pages = wp_total_pages( $total, $per_page );
    523524
    524525        $x = new WP_Ajax_Response(
    525526                array(
     
    530531                                'postId'               => $comment ? $comment->comment_post_ID : '',
    531532                                /* translators: %s: Number of comments. */
    532533                                'total_items_i18n'     => sprintf( _n( '%s item', '%s items', $total ), number_format_i18n( $total ) ),
    533                                 'total_pages'          => ceil( $total / $per_page ),
    534                                 'total_pages_i18n'     => number_format_i18n( ceil( $total / $per_page ) ),
     534                                'total_pages'          => $total_pages,
     535                                'total_pages_i18n'     => number_format_i18n( $total_pages ),
    535536                                'total'                => $total,
    536537                                'time'                 => $time,
    537538                                'in_moderation'        => $counts->moderated,
  • wp-admin/includes/class-wp-list-table.php

     
    284284                );
    285285
    286286                if ( ! $args['total_pages'] && $args['per_page'] > 0 ) {
    287                         $args['total_pages'] = ceil( $args['total_items'] / $args['per_page'] );
     287                        $args['total_pages'] = wp_total_pages( $args['total_items'], $args['per_page'] );
    288288                }
    289289
    290290                // Redirect if page number is invalid and headers are not already sent.
  • wp-admin/includes/deprecated.php

     
    597597                                $args['role'] = urlencode($this->role);
    598598
    599599                        $this->paging_text = paginate_links( array(
    600                                 'total' => ceil($this->total_users_for_query / $this->users_per_page),
     600                                'total' => wp_total_pages( $this->total_users_for_query, $this->users_per_page ),
    601601                                'current' => $this->page,
    602602                                'base' => 'users.php?%_%',
    603603                                'format' => 'userspage=%#%',
  • wp-admin/includes/media.php

     
    28072807                                'format'    => '',
    28082808                                'prev_text' => __( '«' ),
    28092809                                'next_text' => __( '»' ),
    2810                                 'total'     => ceil( $wp_query->found_posts / 10 ),
     2810                                'total'     => wp_total_pages( $wp_query->found_posts, 10 ),
    28112811                                'current'   => $q['paged'],
    28122812                        )
    28132813                );
  • wp-admin/includes/nav-menu.php

     
    723723                return;
    724724        }
    725725
    726         $num_pages = ceil(
     726        $num_pages = wp_total_pages(
    727727                wp_count_terms(
    728728                        array_merge(
    729729                                $args,
     
    732732                                        'offset' => '',
    733733                                )
    734734                        )
    735                 ) / $per_page
     735                ), $per_page
    736736        );
    737737
    738738        $page_links = paginate_links(
  • wp-admin/widgets.php

     
    514514$sidebars_count       = count( $theme_sidebars );
    515515
    516516if ( $sidebars_count > 1 ) {
    517         $split = (int) ceil( $sidebars_count / 2 );
     517        $split = wp_total_pages( $sidebars_count, 2 );
    518518} else {
    519519        $single_sidebar_class = ' single-sidebar';
    520520}
  • wp-includes/class-wp-comment-query.php

     
    451451                }
    452452
    453453                if ( $this->found_comments && $this->query_vars['number'] ) {
    454                         $this->max_num_pages = ceil( $this->found_comments / $this->query_vars['number'] );
     454                        $this->max_num_pages = wp_total_pages( $this->found_comments, $this->query_vars['number'] );
    455455                }
    456456
    457457                // If querying for a count only, there's nothing more to do.
  • wp-includes/class-wp-network-query.php

     
    268268                }
    269269
    270270                if ( $this->found_networks && $this->query_vars['number'] ) {
    271                         $this->max_num_pages = ceil( $this->found_networks / $this->query_vars['number'] );
     271                        $this->max_num_pages = wp_total_pages( $this->found_networks, $this->query_vars['number'] );
    272272                }
    273273
    274274                // If querying for a count only, there's nothing more to do.
  • wp-includes/class-wp-query.php

     
    32813281                $this->found_posts = (int) apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) );
    32823282
    32833283                if ( ! empty( $limits ) ) {
    3284                         $this->max_num_pages = ceil( $this->found_posts / $q['posts_per_page'] );
     3284                        $this->max_num_pages = wp_total_pages( $this->found_posts, $q['posts_per_page'] );
    32853285                }
    32863286        }
    32873287
  • wp-includes/class-wp-site-query.php

     
    359359                }
    360360
    361361                if ( $this->found_sites && $this->query_vars['number'] ) {
    362                         $this->max_num_pages = ceil( $this->found_sites / $this->query_vars['number'] );
     362                        $this->max_num_pages = wp_total_pages( $this->found_sites, $this->query_vars['number'] );
    363363                }
    364364
    365365                // If querying for a count only, there's nothing more to do.
  • wp-includes/class-wp-walker.php

     
    307307                        $start  = ( (int) $page_num - 1 ) * (int) $per_page;
    308308                        $end    = $start + $per_page;
    309309                        if ( -1 == $max_depth ) {
    310                                 $this->max_pages = ceil( $total_top / $per_page );
     310                                $this->max_pages = wp_total_pages( $total_top, (int) $per_page );
    311311                        }
    312312                }
    313313
     
    351351
    352352                $total_top = count( $top_level_elements );
    353353                if ( $paging ) {
    354                         $this->max_pages = ceil( $total_top / $per_page );
     354                        $this->max_pages = wp_total_pages( $total_top, (int) $per_page );
    355355                } else {
    356356                        $end = $total_top;
    357357                }
  • wp-includes/comment-template.php

     
    14541454
    14551455                        $top_level_count = $top_level_query->query( $top_level_args );
    14561456
    1457                         $comment_args['offset'] = ( ceil( $top_level_count / $per_page ) - 1 ) * $per_page;
     1457                        $comment_args['offset'] = ( wp_total_pages( $top_level_count, $per_page ) - 1 ) * $per_page;
    14581458                }
    14591459        }
    14601460
  • wp-includes/comment.php

     
    10461046
    10471047        if ( $threaded ) {
    10481048                $walker = new Walker_Comment;
    1049                 $count  = ceil( $walker->get_number_of_root_elements( $comments ) / $per_page );
     1049                $count  = wp_total_pages( $walker->get_number_of_root_elements( $comments ), $per_page );
    10501050        } else {
    1051                 $count = ceil( count( $comments ) / $per_page );
     1051                $count = wp_total_pages( count( $comments ), $per_page );
    10521052        }
    10531053
    10541054        return $count;
     
    11871187                if ( 0 == $older_comment_count ) {
    11881188                        $page = 1;
    11891189
    1190                         // Divide comments older than this one by comments per page to get this comment's page number.
     1190                        // Use this comment's chronological position to determine its page number.
    11911191                } else {
    1192                         $page = ceil( ( $older_comment_count + 1 ) / $args['per_page'] );
     1192                        $page = wp_total_pages( $older_comment_count + 1, $args['per_page'] );
    11931193                }
    11941194        }
    11951195
  • wp-includes/functions.php

     
    78667866function wp_fuzzy_number_match( $expected, $actual, $precision = 1 ) {
    78677867        return abs( (float) $expected - (float) $actual ) <= $precision;
    78687868}
     7869
     7870/**
     7871 * Calculate the number of pages a set of results can be split into.
     7872 *
     7873 * This is similar to using `ceil()` on division but returns an integer.
     7874 *
     7875 * @since 5.8.0
     7876 *
     7877 * @param int $total_items The count of items to paginate.
     7878 * @param int $per_page    The maximum number of items per page.
     7879 * @return int The total number of pages.
     7880 */
     7881function wp_total_pages( $total_items, $per_page ) {
     7882        $remainder = $total_items % $per_page;
     7883        if ( $remainder ) {
     7884                $total_items += $per_page - $remainder;
     7885        }
     7886        return $total_items / $per_page;
     7887}
  • wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php

     
    297297                        $prepared_args['count'] = true;
    298298
    299299                        $total_comments = $query->query( $prepared_args );
    300                         $max_pages      = ceil( $total_comments / $request['per_page'] );
     300                        $max_pages      = wp_total_pages( $total_comments, $request['per_page'] );
    301301                }
    302302
    303303                $response = rest_ensure_response( $comments );
  • wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

     
    345345                        $total_posts = $count_query->found_posts;
    346346                }
    347347
    348                 $max_pages = ceil( $total_posts / (int) $posts_query->query_vars['posts_per_page'] );
     348                $max_pages = wp_total_pages( (int) $total_posts, (int) $posts_query->query_vars['posts_per_page'] );
    349349
    350350                if ( $page > $max_pages && $total_posts > 0 ) {
    351351                        return new WP_Error(
  • wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php

     
    294294                        }
    295295
    296296                        if ( $revisions_query->query_vars['posts_per_page'] > 0 ) {
    297                                 $max_pages = ceil( $total_revisions / (int) $revisions_query->query_vars['posts_per_page'] );
     297                                $max_pages = wp_total_pages( (int) $total_revisions, (int) $revisions_query->query_vars['posts_per_page'] );
    298298                        } else {
    299299                                $max_pages = $total_revisions > 0 ? 1 : 0;
    300300                        }
  • wp-includes/rest-api/endpoints/class-wp-rest-search-controller.php

     
    152152                $total     = (int) $result[ WP_REST_Search_Handler::RESULT_TOTAL ];
    153153                $page      = (int) $request['page'];
    154154                $per_page  = (int) $request['per_page'];
    155                 $max_pages = ceil( $total / $per_page );
     155                $max_pages = wp_total_pages( $total, $per_page );
    156156
    157157                if ( $page > $max_pages && $total > 0 ) {
    158158                        return new WP_Error(
  • wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php

     
    281281
    282282                // Store pagination values for headers.
    283283                $per_page = (int) $prepared_args['number'];
    284                 $page     = ceil( ( ( (int) $prepared_args['offset'] ) / $per_page ) + 1 );
     284                $page     = wp_total_pages( (int) $prepared_args['offset'], $per_page ) + 1;
    285285
    286286                $response->header( 'X-WP-Total', (int) $total_terms );
    287287
    288                 $max_pages = ceil( $total_terms / $per_page );
     288                $max_pages = wp_total_pages( (int) $total_terms, $per_page );
    289289
    290290                $response->header( 'X-WP-TotalPages', (int) $max_pages );
    291291
  • wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php

     
    329329
    330330                // Store pagination values for headers then unset for count query.
    331331                $per_page = (int) $prepared_args['number'];
    332                 $page     = ceil( ( ( (int) $prepared_args['offset'] ) / $per_page ) + 1 );
     332                $page     = wp_total_pages( (int) $prepared_args['offset'], $per_page ) + 1;
    333333
    334334                $prepared_args['fields'] = 'ID';
    335335
     
    344344
    345345                $response->header( 'X-WP-Total', (int) $total_users );
    346346
    347                 $max_pages = ceil( $total_users / $per_page );
     347                $max_pages = wp_total_pages( (int) $total_users, $per_page );
    348348
    349349                $response->header( 'X-WP-TotalPages', (int) $max_pages );
    350350
  • wp-includes/sitemaps/providers/class-wp-sitemaps-taxonomies.php

     
    158158
    159159                $term_count = wp_count_terms( $this->get_taxonomies_query_args( $taxonomy ) );
    160160
    161                 return (int) ceil( $term_count / wp_sitemaps_get_max_urls( $this->object_type ) );
     161                return wp_total_pages( $term_count, wp_sitemaps_get_max_urls( $this->object_type ) );
    162162        }
    163163
    164164        /**
  • wp-includes/sitemaps/providers/class-wp-sitemaps-users.php

     
    119119
    120120                $total_users = $query->get_total();
    121121
    122                 return (int) ceil( $total_users / wp_sitemaps_get_max_urls( $this->object_type ) );
     122                return wp_total_pages( $total_users, wp_sitemaps_get_max_urls( $this->object_type ) );
    123123        }
    124124
    125125        /**