WordPress.org

Make WordPress Core

Ticket #13939: get_page_of_comment.3.diff

File get_page_of_comment.3.diff, 4.4 KB (added by laceous, 8 years ago)

better filter

  • wp-includes/comment.php

     
    703703 * @return int|null Comment page number or null on error.
    704704 */
    705705function get_page_of_comment( $comment_ID, $args = array() ) {
    706         global $wpdb;
     706        global $wpdb, $user_ID;
    707707
    708708        if ( !$comment = get_comment( $comment_ID ) )
    709709                return;
     
    712712        $args = wp_parse_args( $args, $defaults );
    713713
    714714        if ( '' === $args['per_page'] && get_option('page_comments') )
    715                 $args['per_page'] = get_query_var('comments_per_page');
     715                $args['per_page'] = get_option('comments_per_page');
    716716        if ( empty($args['per_page']) ) {
    717717                $args['per_page'] = 0;
    718718                $args['page'] = 0;
    719719        }
    720720        if ( $args['per_page'] < 1 )
    721                 return 1;
     721                return 1; // apply_filters ?
    722722
    723723        if ( '' === $args['max_depth'] ) {
    724724                if ( get_option('thread_comments') )
     
    731731        if ( $args['max_depth'] > 1 && 0 != $comment->comment_parent )
    732732                return get_page_of_comment( $comment->comment_parent, $args );
    733733
     734        // wp_list_comments allows: all, comment, trackback, pingback, and pings
    734735        $allowedtypes = array(
    735736                'comment' => '',
    736737                'pingback' => 'pingback',
    737738                'trackback' => 'trackback',
    738739        );
    739740
    740         $comtypewhere = ( 'all' != $args['type'] && isset($allowedtypes[$args['type']]) ) ? " AND comment_type = '" . $allowedtypes[$args['type']] . "'" : '';
     741        $comtypewhere = ( 'all' != $args['type'] && 'pings' != $args['type'] && isset($allowedtypes[$args['type']]) ) ? " AND comment_type = '" . $allowedtypes[$args['type']] . "'" : '';
     742        if ( 'pings' == $args['type'] ) $comtypewhere = " AND (comment_type = 'pingback' OR comment_type = 'trackback')";
    741743
     744        if ( $args['max_depth'] > 1 )
     745                $where_comment_parent = ' AND comment_parent = 0';
     746        else
     747                $where_comment_parent = '';
     748
     749        /**
     750         * Comment author information fetched from the comment cookies.
     751         *
     752         * @uses wp_get_current_commenter()
     753         */
     754        $commenter = wp_get_current_commenter();
     755
     756        /**
     757         * The name of the current comment author escaped for use in attributes.
     758         */
     759        $comment_author = $commenter['comment_author']; // Escaped by sanitize_comment_cookies()
     760
     761        /**
     762         * The email address of the current comment author escaped for use in attributes.
     763         */
     764        $comment_author_email = $commenter['comment_author_email'];  // Escaped by sanitize_comment_cookies()
     765
    742766        // Count comments older than this one
    743         $oldercoms = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_parent = 0 AND comment_approved = '1' AND comment_date_gmt < '%s'" . $comtypewhere, $comment->comment_post_ID, $comment->comment_date_gmt ) );
     767        // Partially copied from the comments_template function
     768        if ( $user_ID )
     769                $prepared = $wpdb->prepare( "SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_post_ID = %d" . $where_comment_parent . " AND (comment_approved = '1' OR (user_id = %d AND comment_approved = '0')) AND comment_date_gmt < '%s'" . $comtypewhere, $comment->comment_post_ID, $user_ID, $comment->comment_date_gmt );
     770        else if ( empty($comment_author) )
     771                $prepared = $wpdb->prepare( "SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_post_ID = %d" . $where_comment_parent . " AND comment_approved = '1' AND comment_date_gmt < '%s'" . $comtypewhere, $comment->comment_post_ID, $comment->comment_date_gmt );
     772        else
     773                $prepared = $wpdb->prepare( "SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_post_ID = %d" . $where_comment_parent . " AND (comment_approved = '1' OR (comment_author = %s AND comment_author_email = %s AND comment_approved = '0')) AND comment_date_gmt < '%s'" . $comtypewhere, $comment->comment_post_ID, wp_specialchars_decode($comment_author,ENT_QUOTES), $comment_author_email, $comment->comment_date_gmt );
    744774
    745         // No older comments? Then it's page #1.
    746         if ( 0 == $oldercoms )
    747                 return 1;
     775        $oldercoms = $wpdb->get_var( $prepared );
    748776
    749         // Divide comments older than this one by comments per page to get this comment's page number
    750         return ceil( ( $oldercoms + 1 ) / $args['per_page'] );
     777        if ( 0 == $oldercoms ) {
     778                // No older comments? Then it's page #1.
     779                $page = 1;
     780        }
     781        else {
     782                // Divide comments older than this one by comments per page to get this comment's page number
     783                $page = ceil( ( $oldercoms + 1 ) / $args['per_page'] );
     784        }
     785
     786        return apply_filters( 'get_page_of_comment', $page, $comment, $args );
    751787}
    752788
    753789/**