Ticket #36904: 36904.diff
File 36904.diff, 12.2 KB (added by , 7 years ago) |
---|
-
src/wp-includes/class-wp-comment-query.php
248 248 * Default true. 249 249 * @type bool $update_comment_post_cache Whether to prime the cache for comment posts. 250 250 * 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. 251 253 * } 252 254 */ 253 255 public function __construct( $query = '' ) { … … 297 299 'cache_domain' => 'core', 298 300 'update_comment_meta_cache' => true, 299 301 'update_comment_post_cache' => false, 302 'do_comment_feed_filters' => false, 303 'do_comment_feed' => false, 300 304 ); 301 305 302 306 if ( ! empty( $query ) ) { … … 385 389 */ 386 390 $_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ); 387 391 unset( $_args['fields'] ); 392 unset( $_args['do_comment_feed_filters'] ); 388 393 389 394 $key = md5( serialize( $_args ) ); 390 395 $last_changed = wp_cache_get_last_changed( 'comment' ); … … 473 478 * @global wpdb $wpdb WordPress database abstraction object. 474 479 */ 475 480 protected function get_comment_ids() { 476 global $wp db;481 global $wp_query, $wpdb; 477 482 478 483 // Assemble clauses related to 'comment_approved'. 479 484 $approved_clauses = array(); … … 642 647 } 643 648 } 644 649 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 645 662 if ( $this->query_vars['count'] ) { 646 663 $fields = 'COUNT(*)'; 647 664 } else { … … 818 835 } 819 836 820 837 $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 } 821 842 822 843 if ( $join_posts_table ) { 823 844 $join .= "JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID"; … … 834 855 } 835 856 } 836 857 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 837 872 if ( ! empty( $this->query_vars['date_query'] ) && is_array( $this->query_vars['date_query'] ) ) { 838 873 $this->date_query = new WP_Date_Query( $this->query_vars['date_query'], 'comment_date' ); 839 874 $this->sql_clauses['where']['date_query'] = preg_replace( '/^\s*AND\s*/', '', $this->date_query->get_sql() ); … … 861 896 862 897 $this->filtered_where_clause = $where; 863 898 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 864 911 if ( $where ) { 865 912 $where = 'WHERE ' . $where; 866 913 } 867 914 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 868 927 if ( $groupby ) { 869 928 $groupby = 'GROUP BY ' . $groupby; 870 929 } 871 930 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 872 943 if ( $orderby ) { 873 944 $orderby = "ORDER BY $orderby"; 874 945 } -
src/wp-includes/class-wp-query.php
2517 2517 $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page']; 2518 2518 } 2519 2519 2520 // Comments feeds2521 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. front2527 $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.02537 *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.02547 *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.02557 *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.02567 *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.02577 *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_Comment2588 $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 2606 2520 $pieces = array( 'where', 'groupby', 'join', 'orderby', 'distinct', 'fields', 'limits' ); 2607 2521 2608 2522 /* … … 2969 2883 $this->posts = apply_filters_ref_array( 'posts_results', array( $this->posts, &$this ) ); 2970 2884 } 2971 2885 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 ); 2975 2896 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 } 2978 2902 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; 2994 2906 $this->comment_count = count( $this->comments ); 2995 2907 } 2996 2908 -
tests/phpunit/tests/feed/rss2.php
368 368 $this->assertTrue( have_comments() ); 369 369 370 370 // 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' ); 372 372 373 373 $xml = xml_to_array( $feed ); 374 374 … … 377 377 378 378 // There should only be one <rss> child element. 379 379 $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 ); 380 385 } 381 386 382 387 /* … … 414 419 * @ticket 30210 415 420 */ 416 421 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 417 427 // An example of an valid date archive feed endpoint. 418 428 $this->go_to( get_post_comments_feed_link( self::$posts[0] ) ); 419 429 … … 424 434 $this->assertTrue( have_posts() ); 425 435 426 436 // 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' ); 428 438 429 439 $xml = xml_to_array( $feed ); 430 440 … … 433 443 434 444 // There should only be one <rss> child element. 435 445 $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 ); 436 450 } 437 451 438 452 /*