Make WordPress Core


Ignore:
Timestamp:
10/03/2015 07:38:49 PM (11 years ago)
Author:
boonebgorges
Message:

Introduce 'page_of_comment' filter.

This filter allows developers to modify the output of get_page_of_comment().

As a side effect of this new filter, comment page numbers will always be
returned as integers. Previously, they would sometimes be returned as floats -
eg float(2.0) instead of int(2).

Props laceous.
See #13939.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/comment-functions.php

    r34806 r34808  
    850850        global $wpdb;
    851851
     852        $page = null;
     853
    852854        if ( !$comment = get_comment( $comment_ID ) )
    853855                return;
     
    855857        $defaults = array( 'type' => 'all', 'page' => '', 'per_page' => '', 'max_depth' => '' );
    856858        $args = wp_parse_args( $args, $defaults );
     859        $original_args = $args;
    857860
    858861        // Order of precedence: 1. `$args['per_page']`, 2. 'comments_per_page' query_var, 3. 'comments_per_page' option.
     
    869872                $args['page'] = 0;
    870873        }
    871         if ( $args['per_page'] < 1 )
    872                 return 1;
    873 
    874         if ( '' === $args['max_depth'] ) {
    875                 if ( get_option('thread_comments') )
    876                         $args['max_depth'] = get_option('thread_comments_depth');
    877                 else
    878                         $args['max_depth'] = -1;
    879         }
    880 
    881         // Find this comment's top level parent if threading is enabled
    882         if ( $args['max_depth'] > 1 && 0 != $comment->comment_parent )
    883                 return get_page_of_comment( $comment->comment_parent, $args );
    884 
    885         $comment_args = array(
    886                 'type'       => $args['type'],
    887                 'post_id'    => $comment->comment_post_ID,
    888                 'fields'     => 'ids',
    889                 'count'      => true,
    890                 'status'     => 'approve',
    891                 'parent'     => 0,
    892                 'date_query' => array(
    893                         array(
    894                                 'column' => "$wpdb->comments.comment_date_gmt",
    895                                 'before' => $comment->comment_date_gmt,
    896                         )
    897                 ),
    898         );
    899 
    900         $comment_query = new WP_Comment_Query();
    901         $older_comment_count = $comment_query->query( $comment_args );
    902 
    903         // No older comments? Then it's page #1.
    904         if ( 0 == $older_comment_count )
    905                 return 1;
    906 
    907         // Divide comments older than this one by comments per page to get this comment's page number
    908         return ceil( ( $older_comment_count + 1 ) / $args['per_page'] );
     874
     875        if ( $args['per_page'] < 1 ) {
     876                $page = 1;
     877        }
     878
     879        if ( null === $page ) {
     880                if ( '' === $args['max_depth'] ) {
     881                        if ( get_option('thread_comments') )
     882                                $args['max_depth'] = get_option('thread_comments_depth');
     883                        else
     884                                $args['max_depth'] = -1;
     885                }
     886
     887                // Find this comment's top level parent if threading is enabled
     888                if ( $args['max_depth'] > 1 && 0 != $comment->comment_parent )
     889                        return get_page_of_comment( $comment->comment_parent, $args );
     890
     891                $comment_args = array(
     892                        'type'       => $args['type'],
     893                        'post_id'    => $comment->comment_post_ID,
     894                        'fields'     => 'ids',
     895                        'count'      => true,
     896                        'status'     => 'approve',
     897                        'parent'     => 0,
     898                        'date_query' => array(
     899                                array(
     900                                        'column' => "$wpdb->comments.comment_date_gmt",
     901                                        'before' => $comment->comment_date_gmt,
     902                                )
     903                        ),
     904                );
     905
     906                $comment_query = new WP_Comment_Query();
     907                $older_comment_count = $comment_query->query( $comment_args );
     908
     909                // No older comments? Then it's page #1.
     910                if ( 0 == $older_comment_count ) {
     911                        $page = 1;
     912
     913                // Divide comments older than this one by comments per page to get this comment's page number
     914                } else {
     915                        $page = ceil( ( $older_comment_count + 1 ) / $args['per_page'] );
     916                }
     917        }
     918
     919        /**
     920         * Filters the calculated page on which a comment appears.
     921         *
     922         * @since 4.4.0
     923         *
     924         * @param int   $page          Comment page.
     925         * @param array $args {
     926         *     Arguments used to calculate pagination. These include arguments auto-detected by the function,
     927         *     based on query vars, system settings, etc. For pristine arguments passed to the function,
     928         *     see `$original_args`.
     929         *
     930         *     @type string $type      Type of comments to count.
     931         *     @type int    $page      Calculated current page.
     932         *     @type int    $per_page  Calculated number of comments per page.
     933         *     @type int    $max_depth Maximum comment threading depth allowed.
     934         * }
     935         * @param array $original_args {
     936         *     Array of arguments passed to the function. Some or all of these may not be set.
     937         *
     938         *     @type string $type      Type of comments to count.
     939         *     @type int    $page      Current comment page.
     940         *     @type int    $per_page  Number of comments per page.
     941         *     @type int    $max_depth Maximum comment threading depth allowed.
     942         * }
     943         */
     944        return apply_filters( 'page_of_comment', (int) $page, $args, $original_args );
    909945}
    910946
Note: See TracChangeset for help on using the changeset viewer.