Make WordPress Core

Ticket #36904: 36904.diff

File 36904.diff, 12.2 KB (added by spacedmonkey, 7 years ago)
  • src/wp-includes/class-wp-comment-query.php

     
    248248         *                                                   Default true.
    249249         *     @type bool         $update_comment_post_cache Whether to prime the cache for comment posts.
    250250         *                                                   Default false.
     251         *     @type bool         $do_comment_feed_filters   Whether do comment feed filters. Default false.
     252         *     @type bool         $do_comment_feed           Whether do comment feed join. Default false.
    251253         * }
    252254         */
    253255        public function __construct( $query = '' ) {
     
    297299                        'cache_domain'              => 'core',
    298300                        'update_comment_meta_cache' => true,
    299301                        'update_comment_post_cache' => false,
     302                        'do_comment_feed_filters'   => false,
     303                        'do_comment_feed'           => false,
    300304                );
    301305
    302306                if ( ! empty( $query ) ) {
     
    385389                 */
    386390                $_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) );
    387391                unset( $_args['fields'] );
     392                unset( $_args['do_comment_feed_filters'] );
    388393
    389394                $key          = md5( serialize( $_args ) );
    390395                $last_changed = wp_cache_get_last_changed( 'comment' );
     
    473478         * @global wpdb $wpdb WordPress database abstraction object.
    474479         */
    475480        protected function get_comment_ids() {
    476                 global $wpdb;
     481                global $wp_query, $wpdb;
    477482
    478483                // Assemble clauses related to 'comment_approved'.
    479484                $approved_clauses = array();
     
    642647                        }
    643648                }
    644649
     650                if ( $this->query_vars['do_comment_feed_filters'] ) {
     651                        /**
     652                         * Filters the LIMIT clause of the comments feed query before sending.
     653                         *
     654                         * @since 2.8.0
     655                         *
     656                         * @param string $climits The JOIN clause of the query.
     657                         * @param WP_Query $wp_query The WP_Query global.
     658                         */
     659                        $limits = apply_filters( 'comment_feed_limits', $limits, $wp_query );
     660                }
     661
    645662                if ( $this->query_vars['count'] ) {
    646663                        $fields = 'COUNT(*)';
    647664                } else {
     
    818835                }
    819836
    820837                $join = '';
     838                if ( $this->query_vars['do_comment_feed'] ) {
     839                        $join_posts_table                              = true;
     840                        $this->sql_clauses['where']['do_comment_feed'] = "( post_status = 'publish' OR ( post_status = 'inherit' AND post_type = 'attachment' ) )";
     841                }
    821842
    822843                if ( $join_posts_table ) {
    823844                        $join .= "JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID";
     
    834855                        }
    835856                }
    836857
     858
     859
     860                if ( $this->query_vars['do_comment_feed_filters'] ) {
     861                        /**
     862                         * Filters the LIMIT clause of the comments feed query before sending.
     863                         *
     864                         * @since 2.8.0
     865                         *
     866                         * @param string $climits The JOIN clause of the query.
     867                         * @param WP_Query $wp_query The WP_Query global.
     868                         */
     869                        $join = apply_filters( 'comment_feed_join', $join, $wp_query );
     870                }
     871
    837872                if ( ! empty( $this->query_vars['date_query'] ) && is_array( $this->query_vars['date_query'] ) ) {
    838873                        $this->date_query                         = new WP_Date_Query( $this->query_vars['date_query'], 'comment_date' );
    839874                        $this->sql_clauses['where']['date_query'] = preg_replace( '/^\s*AND\s*/', '', $this->date_query->get_sql() );
     
    861896
    862897                $this->filtered_where_clause = $where;
    863898
     899                if ( $this->query_vars['do_comment_feed_filters'] ) {
     900                        /**
     901                         * Filters the WHERE clause of the comments feed query before sending.
     902                         *
     903                         * @since 2.2.0
     904                         *
     905                         * @param string $cwhere The WHERE clause of the query.
     906                         * @param WP_Query $wp_query The WP_Query global.
     907                         */
     908                        $where = apply_filters( 'comment_feed_where', $where, $wp_query );
     909                }
     910
    864911                if ( $where ) {
    865912                        $where = 'WHERE ' . $where;
    866913                }
    867914
     915                if ( $this->query_vars['do_comment_feed_filters'] ) {
     916                        /**
     917                         * Filters the GROUP BY clause of the comments feed query before sending.
     918                         *
     919                         * @since 2.2.0
     920                         *
     921                         * @param string $cgroupby The GROUP BY clause of the query.
     922                         * @param WP_Query $wp_query The WP_Query global.
     923                         */
     924                        $groupby = apply_filters( 'comment_feed_groupby', $groupby, $wp_query );
     925                }
     926
    868927                if ( $groupby ) {
    869928                        $groupby = 'GROUP BY ' . $groupby;
    870929                }
    871930
     931                if ( $this->query_vars['do_comment_feed_filters'] ) {
     932                        /**
     933                         * Filters the ORDER BY clause of the comments feed query before sending.
     934                         *
     935                         * @since 2.8.0
     936                         *
     937                         * @param string $corderby The ORDER BY clause of the query.
     938                         * @param WP_Query $wp_query The WP_Query global.
     939                         */
     940                        $orderby = apply_filters( 'comment_feed_orderby', $orderby, $wp_query );
     941                }
     942
    872943                if ( $orderby ) {
    873944                        $orderby = "ORDER BY $orderby";
    874945                }
  • src/wp-includes/class-wp-query.php

     
    25172517                        $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page'];
    25182518                }
    25192519
    2520                 // Comments feeds
    2521                 if ( $this->is_comment_feed && ! $this->is_singular ) {
    2522                         if ( $this->is_archive || $this->is_search ) {
    2523                                 $cjoin    = "JOIN {$wpdb->posts} ON ({$wpdb->comments}.comment_post_ID = {$wpdb->posts}.ID) $join ";
    2524                                 $cwhere   = "WHERE comment_approved = '1' $where";
    2525                                 $cgroupby = "{$wpdb->comments}.comment_id";
    2526                         } else { // Other non singular e.g. front
    2527                                 $cjoin    = "JOIN {$wpdb->posts} ON ( {$wpdb->comments}.comment_post_ID = {$wpdb->posts}.ID )";
    2528                                 $cwhere   = "WHERE ( post_status = 'publish' OR ( post_status = 'inherit' AND post_type = 'attachment' ) ) AND comment_approved = '1'";
    2529                                 $cgroupby = '';
    2530                         }
    2531 
    2532                         if ( ! $q['suppress_filters'] ) {
    2533                                 /**
    2534                                  * Filters the JOIN clause of the comments feed query before sending.
    2535                                  *
    2536                                  * @since 2.2.0
    2537                                  *
    2538                                  * @param string   $cjoin The JOIN clause of the query.
    2539                                  * @param WP_Query $this The WP_Query instance (passed by reference).
    2540                                  */
    2541                                 $cjoin = apply_filters_ref_array( 'comment_feed_join', array( $cjoin, &$this ) );
    2542 
    2543                                 /**
    2544                                  * Filters the WHERE clause of the comments feed query before sending.
    2545                                  *
    2546                                  * @since 2.2.0
    2547                                  *
    2548                                  * @param string   $cwhere The WHERE clause of the query.
    2549                                  * @param WP_Query $this   The WP_Query instance (passed by reference).
    2550                                  */
    2551                                 $cwhere = apply_filters_ref_array( 'comment_feed_where', array( $cwhere, &$this ) );
    2552 
    2553                                 /**
    2554                                  * Filters the GROUP BY clause of the comments feed query before sending.
    2555                                  *
    2556                                  * @since 2.2.0
    2557                                  *
    2558                                  * @param string   $cgroupby The GROUP BY clause of the query.
    2559                                  * @param WP_Query $this     The WP_Query instance (passed by reference).
    2560                                  */
    2561                                 $cgroupby = apply_filters_ref_array( 'comment_feed_groupby', array( $cgroupby, &$this ) );
    2562 
    2563                                 /**
    2564                                  * Filters the ORDER BY clause of the comments feed query before sending.
    2565                                  *
    2566                                  * @since 2.8.0
    2567                                  *
    2568                                  * @param string   $corderby The ORDER BY clause of the query.
    2569                                  * @param WP_Query $this     The WP_Query instance (passed by reference).
    2570                                  */
    2571                                 $corderby = apply_filters_ref_array( 'comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) );
    2572 
    2573                                 /**
    2574                                  * Filters the LIMIT clause of the comments feed query before sending.
    2575                                  *
    2576                                  * @since 2.8.0
    2577                                  *
    2578                                  * @param string   $climits The JOIN clause of the query.
    2579                                  * @param WP_Query $this    The WP_Query instance (passed by reference).
    2580                                  */
    2581                                 $climits = apply_filters_ref_array( 'comment_feed_limits', array( 'LIMIT ' . get_option( 'posts_per_rss' ), &$this ) );
    2582                         }
    2583                         $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
    2584                         $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
    2585 
    2586                         $comments = (array) $wpdb->get_results( "SELECT $distinct {$wpdb->comments}.* FROM {$wpdb->comments} $cjoin $cwhere $cgroupby $corderby $climits" );
    2587                         // Convert to WP_Comment
    2588                         $this->comments      = array_map( 'get_comment', $comments );
    2589                         $this->comment_count = count( $this->comments );
    2590 
    2591                         $post_ids = array();
    2592 
    2593                         foreach ( $this->comments as $comment ) {
    2594                                 $post_ids[] = (int) $comment->comment_post_ID;
    2595                         }
    2596 
    2597                         $post_ids = join( ',', $post_ids );
    2598                         $join     = '';
    2599                         if ( $post_ids ) {
    2600                                 $where = "AND {$wpdb->posts}.ID IN ($post_ids) ";
    2601                         } else {
    2602                                 $where = 'AND 0';
    2603                         }
    2604                 }
    2605 
    26062520                $pieces = array( 'where', 'groupby', 'join', 'orderby', 'distinct', 'fields', 'limits' );
    26072521
    26082522                /*
     
    29692883                        $this->posts = apply_filters_ref_array( 'posts_results', array( $this->posts, &$this ) );
    29702884                }
    29712885
    2972                 if ( ! empty( $this->posts ) && $this->is_comment_feed && $this->is_singular ) {
    2973                         /** This filter is documented in wp-includes/query.php */
    2974                         $cjoin = apply_filters_ref_array( 'comment_feed_join', array( '', &$this ) );
     2886                if ( ! empty( $this->posts ) && $this->is_comment_feed ) {
     2887                        $comment_args = array(
     2888                                'orderby'                   => 'comment_date_gmt',
     2889                                'order'                     => 'DESC',
     2890                                'status'                    => 'approve',
     2891                                'no_found_rows'             => true,
     2892                                'number'                    => get_option( 'posts_per_rss' ),
     2893                                'update_comment_meta_cache' => false,
     2894                                'do_comment_feed_filters'   => ! $q['suppress_filters'],
     2895                        );
    29752896
    2976                         /** This filter is documented in wp-includes/query.php */
    2977                         $cwhere = apply_filters_ref_array( 'comment_feed_where', array( "WHERE comment_post_ID = '{$this->posts[0]->ID}' AND comment_approved = '1'", &$this ) );
     2897                        if ( $this->is_singular ) {
     2898                                $comment_args['post_id'] = $this->posts[0]->ID;
     2899                        } else {
     2900                                $comment_args['do_comment_feed'] = true;
     2901                        }
    29782902
    2979                         /** This filter is documented in wp-includes/query.php */
    2980                         $cgroupby = apply_filters_ref_array( 'comment_feed_groupby', array( '', &$this ) );
    2981                         $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
    2982 
    2983                         /** This filter is documented in wp-includes/query.php */
    2984                         $corderby = apply_filters_ref_array( 'comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) );
    2985                         $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
    2986 
    2987                         /** This filter is documented in wp-includes/query.php */
    2988                         $climits = apply_filters_ref_array( 'comment_feed_limits', array( 'LIMIT ' . get_option( 'posts_per_rss' ), &$this ) );
    2989 
    2990                         $comments_request = "SELECT {$wpdb->comments}.* FROM {$wpdb->comments} $cjoin $cwhere $cgroupby $corderby $climits";
    2991                         $comments         = $wpdb->get_results( $comments_request );
    2992                         // Convert to WP_Comment
    2993                         $this->comments      = array_map( 'get_comment', $comments );
     2903                        $comment_args        = apply_filters( 'comments_feed_query_args', $comment_args );
     2904                        $comment_query       = new WP_Comment_Query( $comment_args );
     2905                        $this->comments      = $comment_query->comments;
    29942906                        $this->comment_count = count( $this->comments );
    29952907                }
    29962908
  • tests/phpunit/tests/feed/rss2.php

     
    368368                $this->assertTrue( have_comments() );
    369369
    370370                // Check to see if we have the expected XML output from the feed template.
    371                 $feed = $this->do_rss2();
     371                $feed = $this->do_rss2( 'comments' );
    372372
    373373                $xml = xml_to_array( $feed );
    374374
     
    377377
    378378                // There should only be one <rss> child element.
    379379                $this->assertEquals( 1, count( $rss ) );
     380
     381                // Comment limits obey 'posts_per_rss' count.
     382                $comments = xml_find( $xml, 'rss', 'channel', 'item' );
     383                $posts_per_rss = (int) get_option( 'posts_per_rss' );
     384                $this->assertCount( $posts_per_rss, $comments );
    380385        }
    381386
    382387        /*
     
    414419         * @ticket 30210
    415420         */
    416421        function test_valid_single_post_comment_feed_endpoint() {
     422                $comment_ids = self::factory()->comment->create_many( 3, array(
     423                        'comment_post_ID' => self::$posts[0],
     424                        'comment_approved' => '1',
     425                ) );
     426
    417427                // An example of an valid date archive feed endpoint.
    418428                $this->go_to( get_post_comments_feed_link( self::$posts[0] ) );
    419429
     
    424434                $this->assertTrue( have_posts() );
    425435
    426436                // Check to see if we have the expected XML output from the feed template.
    427                 $feed = $this->do_rss2();
     437                $feed = $this->do_rss2( 'comments' );
    428438
    429439                $xml = xml_to_array( $feed );
    430440
     
    433443
    434444                // There should only be one <rss> child element.
    435445                $this->assertEquals( 1, count( $rss ) );
     446
     447                // There should be three comments.
     448                $comments = xml_find( $xml, 'rss', 'channel', 'item' );
     449                $this->assertCount( 3, $comments );
    436450        }
    437451
    438452        /*