Ticket #36904: 36904.4.patch
File 36904.4.patch, 13.7 KB (added by , 7 years ago) |
---|
-
src/wp-includes/class-wp-comment-query.php
diff --git src/wp-includes/class-wp-comment-query.php src/wp-includes/class-wp-comment-query.php index 25b7fcf1a8..67f165ea07 100644
class WP_Comment_Query { 257 257 * Default true. 258 258 * @type bool $update_comment_post_cache Whether to prime the cache for comment posts. 259 259 * Default false. 260 * @type bool $do_comment_feed_filters Whether do comment feed filters. Default false. 260 261 * } 261 262 */ 262 263 public function __construct( $query = '' ) { … … class WP_Comment_Query { 305 306 'cache_domain' => 'core', 306 307 'update_comment_meta_cache' => true, 307 308 'update_comment_post_cache' => false, 309 'do_comment_feed_filters' => false, 308 310 ); 309 311 310 312 if ( ! empty( $query ) ) { … … class WP_Comment_Query { 481 483 * @global wpdb $wpdb WordPress database abstraction object. 482 484 */ 483 485 protected function get_comment_ids() { 484 global $wp db;486 global $wp_query, $wpdb; 485 487 486 488 // Assemble clauses related to 'comment_approved'. 487 489 $approved_clauses = array(); … … class WP_Comment_Query { 649 651 } 650 652 } 651 653 654 if ( $this->query_vars['do_comment_feed_filters'] ) { 655 /** 656 * Filters the LIMIT clause of the comments feed query before sending. 657 * 658 * @since 2.8.0 659 * 660 * @param string $climits The JOIN clause of the query. 661 * @param WP_Query $wp_query The WP_Query global. 662 */ 663 $limits = apply_filters( 'comment_feed_limits', $limits, $wp_query ); 664 } 665 652 666 if ( $this->query_vars['count'] ) { 653 667 $fields = 'COUNT(*)'; 654 668 } else { … … class WP_Comment_Query { 841 855 } 842 856 } 843 857 858 if ( $this->query_vars['do_comment_feed_filters'] ) { 859 /** 860 * Filters the LIMIT clause of the comments feed query before sending. 861 * 862 * @since 2.8.0 863 * 864 * @param string $climits The JOIN clause of the query. 865 * @param WP_Query $wp_query The WP_Query global. 866 */ 867 $join = apply_filters( 'comment_feed_join', $join, $wp_query ); 868 } 869 844 870 if ( ! empty( $this->query_vars['date_query'] ) && is_array( $this->query_vars['date_query'] ) ) { 845 871 $this->date_query = new WP_Date_Query( $this->query_vars['date_query'], 'comment_date' ); 846 872 $this->sql_clauses['where']['date_query'] = preg_replace( '/^\s*AND\s*/', '', $this->date_query->get_sql() ); … … class WP_Comment_Query { 868 894 869 895 $this->filtered_where_clause = $where; 870 896 897 if ( $this->query_vars['do_comment_feed_filters'] ) { 898 /** 899 * Filters the WHERE clause of the comments feed query before sending. 900 * 901 * @since 2.2.0 902 * 903 * @param string $cwhere The WHERE clause of the query. 904 * @param WP_Query $wp_query The WP_Query global. 905 */ 906 $where = apply_filters( 'comment_feed_where', $where, $wp_query ); 907 } 908 871 909 if ( $where ) { 872 910 $where = 'WHERE ' . $where; 873 911 } 874 912 913 if ( $this->query_vars['do_comment_feed_filters'] ) { 914 /** 915 * Filters the GROUP BY clause of the comments feed query before sending. 916 * 917 * @since 2.2.0 918 * 919 * @param string $cgroupby The GROUP BY clause of the query. 920 * @param WP_Query $wp_query The WP_Query global. 921 */ 922 $groupby = apply_filters( 'comment_feed_groupby', $groupby, $wp_query ); 923 } 924 875 925 if ( $groupby ) { 876 926 $groupby = 'GROUP BY ' . $groupby; 877 927 } 878 928 929 if ( $this->query_vars['do_comment_feed_filters'] ) { 930 /** 931 * Filters the ORDER BY clause of the comments feed query before sending. 932 * 933 * @since 2.8.0 934 * 935 * @param string $corderby The ORDER BY clause of the query. 936 * @param WP_Query $wp_query The WP_Query global. 937 */ 938 $orderby = apply_filters( 'comment_feed_orderby', $orderby, $wp_query ); 939 } 940 879 941 if ( $orderby ) { 880 942 $orderby = "ORDER BY $orderby"; 881 943 } -
src/wp-includes/class-wp-query.php
diff --git src/wp-includes/class-wp-query.php src/wp-includes/class-wp-query.php index e20d574409..ef8b5631c5 100644
class WP_Query { 2427 2427 $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page']; 2428 2428 } 2429 2429 2430 // Comments feeds2431 if ( $this->is_comment_feed && ! $this->is_singular ) {2432 if ( $this->is_archive || $this->is_search ) {2433 $cjoin = "JOIN {$wpdb->posts} ON ({$wpdb->comments}.comment_post_ID = {$wpdb->posts}.ID) $join ";2434 $cwhere = "WHERE comment_approved = '1' $where";2435 $cgroupby = "{$wpdb->comments}.comment_id";2436 } else { // Other non singular e.g. front2437 $cjoin = "JOIN {$wpdb->posts} ON ( {$wpdb->comments}.comment_post_ID = {$wpdb->posts}.ID )";2438 $cwhere = "WHERE ( post_status = 'publish' OR ( post_status = 'inherit' AND post_type = 'attachment' ) ) AND comment_approved = '1'";2439 $cgroupby = '';2440 }2441 2442 if ( !$q['suppress_filters'] ) {2443 /**2444 * Filters the JOIN clause of the comments feed query before sending.2445 *2446 * @since 2.2.02447 *2448 * @param string $cjoin The JOIN clause of the query.2449 * @param WP_Query &$this The WP_Query instance (passed by reference).2450 */2451 $cjoin = apply_filters_ref_array( 'comment_feed_join', array( $cjoin, &$this ) );2452 2453 /**2454 * Filters the WHERE clause of the comments feed query before sending.2455 *2456 * @since 2.2.02457 *2458 * @param string $cwhere The WHERE clause of the query.2459 * @param WP_Query &$this The WP_Query instance (passed by reference).2460 */2461 $cwhere = apply_filters_ref_array( 'comment_feed_where', array( $cwhere, &$this ) );2462 2463 /**2464 * Filters the GROUP BY clause of the comments feed query before sending.2465 *2466 * @since 2.2.02467 *2468 * @param string $cgroupby The GROUP BY clause of the query.2469 * @param WP_Query &$this The WP_Query instance (passed by reference).2470 */2471 $cgroupby = apply_filters_ref_array( 'comment_feed_groupby', array( $cgroupby, &$this ) );2472 2473 /**2474 * Filters the ORDER BY clause of the comments feed query before sending.2475 *2476 * @since 2.8.02477 *2478 * @param string $corderby The ORDER BY clause of the query.2479 * @param WP_Query &$this The WP_Query instance (passed by reference).2480 */2481 $corderby = apply_filters_ref_array( 'comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) );2482 2483 /**2484 * Filters the LIMIT clause of the comments feed query before sending.2485 *2486 * @since 2.8.02487 *2488 * @param string $climits The JOIN clause of the query.2489 * @param WP_Query &$this The WP_Query instance (passed by reference).2490 */2491 $climits = apply_filters_ref_array( 'comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) );2492 }2493 $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';2494 $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';2495 2496 $comments = (array) $wpdb->get_results("SELECT $distinct {$wpdb->comments}.* FROM {$wpdb->comments} $cjoin $cwhere $cgroupby $corderby $climits");2497 // Convert to WP_Comment2498 $this->comments = array_map( 'get_comment', $comments );2499 $this->comment_count = count($this->comments);2500 2501 $post_ids = array();2502 2503 foreach ( $this->comments as $comment )2504 $post_ids[] = (int) $comment->comment_post_ID;2505 2506 $post_ids = join(',', $post_ids);2507 $join = '';2508 if ( $post_ids ) {2509 $where = "AND {$wpdb->posts}.ID IN ($post_ids) ";2510 } else {2511 $where = "AND 0";2512 }2513 }2514 2515 2430 $pieces = array( 'where', 'groupby', 'join', 'orderby', 'distinct', 'fields', 'limits' ); 2516 2431 2517 2432 /* … … class WP_Query { 2875 2790 $this->posts = apply_filters_ref_array( 'posts_results', array( $this->posts, &$this ) ); 2876 2791 } 2877 2792 2878 if ( !empty($this->posts) && $this->is_comment_feed && $this->is_singular ) { 2879 /** This filter is documented in wp-includes/query.php */ 2880 $cjoin = apply_filters_ref_array( 'comment_feed_join', array( '', &$this ) ); 2881 2882 /** This filter is documented in wp-includes/query.php */ 2883 $cwhere = apply_filters_ref_array( 'comment_feed_where', array( "WHERE comment_post_ID = '{$this->posts[0]->ID}' AND comment_approved = '1'", &$this ) ); 2884 2885 /** This filter is documented in wp-includes/query.php */ 2886 $cgroupby = apply_filters_ref_array( 'comment_feed_groupby', array( '', &$this ) ); 2887 $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : ''; 2888 2889 /** This filter is documented in wp-includes/query.php */ 2890 $corderby = apply_filters_ref_array( 'comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) ); 2891 $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : ''; 2793 if ( ! empty( $this->posts ) && $this->is_comment_feed ) { 2794 $comment_args = array( 2795 'orderby' => 'comment_date_gmt', 2796 'order' => 'DESC', 2797 'status' => 'approve', 2798 'post__in' => wp_list_pluck( $this->posts, 'ID' ), 2799 'no_found_rows' => false, 2800 'number' => get_option( 'posts_per_rss' ), 2801 'update_comment_meta_cache' => false, 2802 'do_comment_feed_filters' => ! $q['suppress_filters'], 2803 ); 2892 2804 2893 /** This filter is documented in wp-includes/query.php */ 2894 $climits = apply_filters_ref_array( 'comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) ); 2805 $comment_args = apply_filters( 'comments_feed_query_args', $comment_args ); 2895 2806 2896 $comments_request = "SELECT {$wpdb->comments}.* FROM {$wpdb->comments} $cjoin $cwhere $cgroupby $corderby $climits"; 2897 $comments = $wpdb->get_results($comments_request); 2898 // Convert to WP_Comment 2899 $this->comments = array_map( 'get_comment', $comments ); 2900 $this->comment_count = count($this->comments); 2807 $comment_query = new WP_Comment_Query( $comment_args ); 2808 $this->comments = $comment_query->comments; 2809 $this->comment_count = count( $this->comments ); 2901 2810 } 2902 2811 2903 2812 // Check post status to determine if post should be displayed. -
tests/phpunit/includes/utils.php
diff --git tests/phpunit/includes/utils.php tests/phpunit/includes/utils.php index 96d9badaa1..75b3c897f5 100644
function xml_find($tree /*, $el1, $el2, $el3, .. */) { 202 202 return $out; 203 203 204 204 for ($i=0; $i<count($tree); $i++) { 205 # echo "checking '{$tree[$i][name]}'== '{$a[0]}'\n";205 # echo 'checking ' . $tree[$i]['name'] . " == '{$a[0]}'\n"; 206 206 # var_dump($tree[$i]['name'], $a[0]); 207 207 if ($tree[$i]['name'] == $a[0]) { 208 208 # echo "n == {$n}\n"; … … class wpdb_exposed_methods_for_testing extends wpdb { 408 408 */ 409 409 function benchmark_pcre_backtracking( $pattern, $subject, $strategy ) { 410 410 $saved_config = ini_get( 'pcre.backtrack_limit' ); 411 411 412 412 // Attempt to prevent PHP crashes. Adjust these lower when needed. 413 413 if ( version_compare( phpversion(), '5.4.8', '>' ) ) { 414 414 $limit = 1000000; … … function benchmark_pcre_backtracking( $pattern, $subject, $strategy ) { 420 420 for( $i = 4; $i <= $limit; $i *= 2 ) { 421 421 422 422 ini_set( 'pcre.backtrack_limit', $i ); 423 423 424 424 switch( $strategy ) { 425 425 case 'split': 426 426 preg_split( $pattern, $subject ); -
tests/phpunit/tests/feed/rss2.php
diff --git tests/phpunit/tests/feed/rss2.php tests/phpunit/tests/feed/rss2.php index f0d7919812..3650bf3ad1 100644
class Tests_Feeds_RSS2 extends WP_UnitTestCase { 67 67 68 68 /** 69 69 * This is a bit of a hack used to buffer feed content. 70 * 71 * @since 4.9.0 Introduced `$feed_type` parameter. 72 * 73 * @param string $feed_type Feed type. 'main' or 'comments'. Default 'main'. 74 * @return string 70 75 */ 71 function do_rss2() { 76 function do_rss2( $feed_type = 'main' ) { 77 switch ( $feed_type ) { 78 case 'comments' : 79 $feed_template_path = ABSPATH . 'wp-includes/feed-rss2-comments.php'; 80 break; 81 82 default : 83 $feed_template_path = ABSPATH . 'wp-includes/feed-rss2.php'; 84 break; 85 } 86 72 87 ob_start(); 73 88 // Nasty hack! In the future it would better to leverage do_feed( 'rss2' ). 74 89 global $post; 75 90 try { 76 @require( ABSPATH . 'wp-includes/feed-rss2.php');91 @require( $feed_template_path ); 77 92 $out = ob_get_clean(); 78 93 } catch (Exception $e) { 79 94 $out = ob_get_clean(); … … class Tests_Feeds_RSS2 extends WP_UnitTestCase { 360 375 $this->assertTrue( have_comments() ); 361 376 362 377 // Check to see if we have the expected XML output from the feed template. 363 $feed = $this->do_rss2( );378 $feed = $this->do_rss2( 'comments' ); 364 379 365 380 $xml = xml_to_array( $feed ); 366 381 … … class Tests_Feeds_RSS2 extends WP_UnitTestCase { 369 384 370 385 // There should only be one <rss> child element. 371 386 $this->assertEquals( 1, count( $rss ) ); 387 388 // Comment limits obey 'posts_per_rss' count. 389 $comments = xml_find( $xml, 'rss', 'channel', 'item' ); 390 $posts_per_rss = (int) get_option( 'posts_per_rss' ); 391 $this->assertCount( $posts_per_rss, $comments ); 372 392 } 373 393 374 394 /* … … class Tests_Feeds_RSS2 extends WP_UnitTestCase { 406 426 * @ticket 30210 407 427 */ 408 428 function test_valid_single_post_comment_feed_endpoint() { 429 $comment_ids = self::factory()->comment->create_many( 3, array( 430 'comment_post_ID' => self::$posts[0], 431 'comment_approved' => '1', 432 ) ); 433 409 434 // An example of an valid date archive feed endpoint. 410 435 $this->go_to( get_post_comments_feed_link( self::$posts[0] ) ); 411 436 … … class Tests_Feeds_RSS2 extends WP_UnitTestCase { 416 441 $this->assertTrue( have_posts() ); 417 442 418 443 // Check to see if we have the expected XML output from the feed template. 419 $feed = $this->do_rss2( );444 $feed = $this->do_rss2( 'comments' ); 420 445 421 446 $xml = xml_to_array( $feed ); 422 447 … … class Tests_Feeds_RSS2 extends WP_UnitTestCase { 425 450 426 451 // There should only be one <rss> child element. 427 452 $this->assertEquals( 1, count( $rss ) ); 453 454 // There should be three comments. 455 $comments = xml_find( $xml, 'rss', 'channel', 'item' ); 456 $this->assertCount( 3, $comments ); 428 457 } 429 458 430 459 /*