Changeset 31467
- Timestamp:
- 02/16/2015 02:09:40 PM (10 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/comment.php
r31264 r31467 327 327 * @type int $offset Number of comments to offset the query. Used to build LIMIT clause. 328 328 * Default 0. 329 * @type string|array $orderby Comment status or array of statuses. Accepts 'comment_agent', 330 * 'comment_approved', 'comment_author', 'comment_author_email', 331 * 'comment_author_IP', 'comment_author_url', 'comment_content', 332 * 'comment_date', 'comment_date_gmt', 'comment_ID', 'comment_karma', 329 * @type string|array $orderby Comment status or array of statuses. To use 'meta_value' or 330 * 'meta_value_num', `$meta_key` must also be defined. To sort by 331 * a specific `$meta_query` clause, use that clause's array key. 332 * Accepts 'comment_agent', 'comment_approved', 'comment_author', 333 * 'comment_author_email', 'comment_author_IP', 334 * 'comment_author_url', 'comment_content', 'comment_date', 335 * 'comment_date_gmt', 'comment_ID', 'comment_karma', 333 336 * 'comment_parent', 'comment_post_ID', 'comment_type', 'user_id', 334 * 'meta_value', 'meta_value_num', or value of $meta_key. 335 * Also accepts false, empty array, or 'none' to disable `ORDER BY` 336 * clause. Default: 'comment_date_gmt'. 337 * 'meta_value', 'meta_value_num', the value of $meta_key, and the 338 * array keys of `$meta_query`. Also accepts false, an empty array, 339 * or 'none' to disable `ORDER BY` clause. 340 * Default: 'comment_date_gmt'. 337 341 * @type string $order How to order retrieved comments. Accepts 'ASC', 'DESC'. 338 342 * Default: 'DESC'. … … 412 416 $this->meta_query->parse_query_vars( $this->query_vars ); 413 417 418 if ( ! empty( $this->meta_query->queries ) ) { 419 $meta_query_clauses = $this->meta_query->get_sql( 'comment', $wpdb->comments, 'comment_ID', $this ); 420 } 421 414 422 /** 415 423 * Fires before comments are retrieved. … … 515 523 preg_split( '/[,\s]/', $this->query_vars['orderby'] ); 516 524 517 $allowed_keys = array( 518 'comment_agent', 519 'comment_approved', 520 'comment_author', 521 'comment_author_email', 522 'comment_author_IP', 523 'comment_author_url', 524 'comment_content', 525 'comment_date', 526 'comment_date_gmt', 527 'comment_ID', 528 'comment_karma', 529 'comment_parent', 530 'comment_post_ID', 531 'comment_type', 532 'user_id', 533 ); 534 if ( ! empty( $this->query_vars['meta_key'] ) ) { 535 $allowed_keys[] = $this->query_vars['meta_key']; 536 $allowed_keys[] = 'meta_value'; 537 $allowed_keys[] = 'meta_value_num'; 525 $orderby_array = array(); 526 $found_orderby_comment_ID = false; 527 foreach ( $ordersby as $_key => $_value ) { 528 if ( ! $_value ) { 529 continue; 530 } 531 532 if ( is_int( $_key ) ) { 533 $_orderby = $_value; 534 $_order = $order; 535 } else { 536 $_orderby = $_key; 537 $_order = $_value; 538 } 539 540 if ( ! $found_orderby_comment_ID && 'comment_ID' === $_orderby ) { 541 $found_orderby_comment_ID = true; 542 } 543 544 $parsed = $this->parse_orderby( $_orderby ); 545 546 if ( ! $parsed ) { 547 continue; 548 } 549 550 $orderby_array[] = $parsed . ' ' . $this->parse_order( $_order ); 538 551 } 539 $ordersby = array_intersect( $ordersby, $allowed_keys ); 540 foreach ( $ordersby as $key => $value ) { 541 if ( $value == $this->query_vars['meta_key'] || $value == 'meta_value' ) { 542 $ordersby[ $key ] = "$wpdb->commentmeta.meta_value"; 543 } elseif ( $value == 'meta_value_num' ) { 544 $ordersby[ $key ] = "$wpdb->commentmeta.meta_value+0"; 552 553 // If no valid clauses were found, order by comment_date_gmt. 554 if ( empty( $orderby_array ) ) { 555 $orderby_array[] = "$wpdb->comments.comment_date_gmt $order"; 556 } 557 558 // To ensure determinate sorting, always include a comment_ID clause. 559 if ( ! $found_orderby_comment_ID ) { 560 $comment_ID_order = ''; 561 562 // Inherit order from comment_date or comment_date_gmt, if available. 563 foreach ( $orderby_array as $orderby_clause ) { 564 if ( preg_match( '/comment_date(?:_gmt)*\ (ASC|DESC)/', $orderby_clause, $match ) ) { 565 $comment_ID_order = $match[1]; 566 break; 567 } 545 568 } 569 570 // If no date-related order is available, use the date from the first available clause. 571 if ( ! $comment_ID_order ) { 572 foreach ( $orderby_array as $orderby_clause ) { 573 if ( false !== strpos( 'ASC', $orderby_clause ) ) { 574 $comment_ID_order = 'ASC'; 575 } else { 576 $comment_ID_order = 'DESC'; 577 } 578 579 break; 580 } 581 } 582 583 // Default to DESC. 584 if ( ! $comment_ID_order ) { 585 $comment_ID_order = 'DESC'; 586 } 587 588 $orderby_array[] = "$wpdb->comments.comment_ID $comment_ID_order"; 546 589 } 547 $orderby = empty( $ordersby ) ? 'comment_date_gmt' : implode(', ', $ordersby); 590 591 $orderby = implode( ', ', $orderby_array ); 548 592 } else { 549 $orderby = 'comment_date_gmt';593 $orderby = "$wpdb->comments.comment_date_gmt $order"; 550 594 } 551 595 … … 710 754 } 711 755 712 if ( ! empty( $this->meta_query->queries ) ) { 713 $clauses = $this->meta_query->get_sql( 'comment', $wpdb->comments, 'comment_ID', $this ); 714 $join .= $clauses['join']; 756 if ( ! empty( $meta_query_clauses ) ) { 757 $join .= $meta_query_clauses['join']; 715 758 716 759 // Strip leading 'AND'. 717 $where[] = preg_replace( '/^\s*AND\s*/', '', $ clauses['where'] );760 $where[] = preg_replace( '/^\s*AND\s*/', '', $meta_query_clauses['where'] ); 718 761 719 762 if ( ! $this->query_vars['count'] ) { … … 730 773 $where = implode( ' AND ', $where ); 731 774 732 $pieces = array( 'fields', 'join', 'where', 'orderby', ' order', 'limits', 'groupby' );775 $pieces = array( 'fields', 'join', 'where', 'orderby', 'limits', 'groupby' ); 733 776 /** 734 777 * Filter the comment query clauses. … … 745 788 $where = isset( $clauses[ 'where' ] ) ? $clauses[ 'where' ] : ''; 746 789 $orderby = isset( $clauses[ 'orderby' ] ) ? $clauses[ 'orderby' ] : ''; 747 $order = isset( $clauses[ 'order' ] ) ? $clauses[ 'order' ] : '';748 790 $limits = isset( $clauses[ 'limits' ] ) ? $clauses[ 'limits' ] : ''; 749 791 $groupby = isset( $clauses[ 'groupby' ] ) ? $clauses[ 'groupby' ] : ''; … … 758 800 759 801 if ( $orderby ) { 760 $orderby = "ORDER BY $orderby $order";802 $orderby = "ORDER BY $orderby"; 761 803 } 762 804 … … 809 851 810 852 return ' AND (' . implode(' OR ', $searches) . ')'; 853 } 854 855 /** 856 * Parse and sanitize 'orderby' keys passed to the comment query. 857 * 858 * @since 4.2.0 859 * @access protected 860 * 861 * @global wpdb $wpdb WordPress database abstraction object. 862 * 863 * @param string $orderby Alias for the field to order by. 864 * @return string|bool Value to used in the ORDER clause. False otherwise. 865 */ 866 protected function parse_orderby( $orderby ) { 867 global $wpdb; 868 869 $allowed_keys = array( 870 'comment_agent', 871 'comment_approved', 872 'comment_author', 873 'comment_author_email', 874 'comment_author_IP', 875 'comment_author_url', 876 'comment_content', 877 'comment_date', 878 'comment_date_gmt', 879 'comment_ID', 880 'comment_karma', 881 'comment_parent', 882 'comment_post_ID', 883 'comment_type', 884 'user_id', 885 ); 886 887 if ( ! empty( $this->query_vars['meta_key'] ) ) { 888 $allowed_keys[] = $this->query_vars['meta_key']; 889 $allowed_keys[] = 'meta_value'; 890 $allowed_keys[] = 'meta_value_num'; 891 } 892 893 $meta_query_clauses = $this->meta_query->get_clauses(); 894 if ( $meta_query_clauses ) { 895 $allowed_keys = array_merge( $allowed_keys, array_keys( $meta_query_clauses ) ); 896 } 897 898 $parsed = false; 899 if ( $orderby == $this->query_vars['meta_key'] || $orderby == 'meta_value' ) { 900 $parsed = "$wpdb->commentmeta.meta_value"; 901 } else if ( $orderby == 'meta_value_num' ) { 902 $parsed = "$wpdb->commentmeta.meta_value+0"; 903 } else if ( in_array( $orderby, $allowed_keys ) ) { 904 905 if ( isset( $meta_query_clauses[ $orderby ] ) ) { 906 $meta_clause = $meta_query_clauses[ $orderby ]; 907 $parsed = sprintf( "CAST(%s.meta_value AS %s)", esc_sql( $meta_clause['alias'] ), esc_sql( $meta_clause['cast'] ) ); 908 } else { 909 $parsed = "$wpdb->comments.$orderby"; 910 } 911 } 912 913 return $parsed; 914 } 915 916 /** 917 * Parse an 'order' query variable and cast it to ASC or DESC as necessary. 918 * 919 * @since 4.2.0 920 * @access protected 921 * 922 * @param string $order The 'order' query variable. 923 * @return string The sanitized 'order' query variable. 924 */ 925 protected function parse_order( $order ) { 926 if ( ! is_string( $order ) || empty( $order ) ) { 927 return 'DESC'; 928 } 929 930 if ( 'ASC' === strtoupper( $order ) ) { 931 return 'ASC'; 932 } else { 933 return 'DESC'; 934 } 811 935 } 812 936 } -
trunk/tests/phpunit/tests/comment/query.php
r31015 r31467 613 613 614 614 $comments = get_comments( array( 'meta_value' => 'value3', 'orderby' => array( 'key' ) ) ); 615 $this->assertEquals( 2, count( $comments ) ); 616 $this->assertEquals( $comment_id, $comments[0]->comment_ID ); 617 $this->assertEquals( $comment_id3, $comments[1]->comment_ID ); 615 $this->assertEquals( array( $comment_id3, $comment_id ), wp_list_pluck( $comments, 'comment_ID' ) ); 618 616 619 617 $comments = get_comments( array( 'meta_value' => 'value3', 'orderby' => array( 'meta_value' ) ) ); 620 $this->assertEquals( 2, count( $comments ) ); 621 $this->assertEquals( $comment_id, $comments[0]->comment_ID ); 622 $this->assertEquals( $comment_id3, $comments[1]->comment_ID ); 618 $this->assertEquals( array( $comment_id3, $comment_id ), wp_list_pluck( $comments, 'comment_ID' ) ); 623 619 624 620 // value1 is present on two different keys for $comment_id yet we should get only one instance … … 630 626 $this->assertEquals( 1, count( $comments ) ); 631 627 } 628 629 /** 630 * @ticket 30478 631 */ 632 public function test_orderby_clause_key() { 633 $comments = $this->factory->comment->create_many( 3 ); 634 add_comment_meta( $comments[0], 'foo', 'aaa' ); 635 add_comment_meta( $comments[1], 'foo', 'zzz' ); 636 add_comment_meta( $comments[2], 'foo', 'jjj' ); 637 638 $q = new WP_Comment_Query(); 639 $found = $q->query( array( 640 'fields' => 'ids', 641 'meta_query' => array( 642 'foo_key' => array( 643 'key' => 'foo', 644 'compare' => 'EXISTS', 645 ), 646 ), 647 'orderby' => 'foo_key', 648 'order' => 'DESC', 649 ) ); 650 651 $this->assertEquals( array( $comments[1], $comments[2], $comments[0] ), $found ); 652 } 653 654 /** 655 * @ticket 30478 656 */ 657 public function test_orderby_clause_key_as_secondary_sort() { 658 $c1 = $this->factory->comment->create( array( 659 'comment_date' => '2015-01-28 03:00:00', 660 ) ); 661 $c2 = $this->factory->comment->create( array( 662 'comment_date' => '2015-01-28 05:00:00', 663 ) ); 664 $c3 = $this->factory->comment->create( array( 665 'comment_date' => '2015-01-28 03:00:00', 666 ) ); 667 668 add_comment_meta( $c1, 'foo', 'jjj' ); 669 add_comment_meta( $c2, 'foo', 'zzz' ); 670 add_comment_meta( $c3, 'foo', 'aaa' ); 671 672 $q = new WP_Comment_Query(); 673 $found = $q->query( array( 674 'fields' => 'ids', 675 'meta_query' => array( 676 'foo_key' => array( 677 'key' => 'foo', 678 'compare' => 'EXISTS', 679 ), 680 ), 681 'orderby' => array( 682 'comment_date' => 'asc', 683 'foo_key' => 'asc', 684 ), 685 ) ); 686 687 $this->assertEquals( array( $c3, $c1, $c2 ), $found ); 688 } 689 690 /** 691 * @ticket 30478 692 */ 693 public function test_orderby_more_than_one_clause_key() { 694 $comments = $this->factory->comment->create_many( 3 ); 695 696 add_comment_meta( $comments[0], 'foo', 'jjj' ); 697 add_comment_meta( $comments[1], 'foo', 'zzz' ); 698 add_comment_meta( $comments[2], 'foo', 'jjj' ); 699 add_comment_meta( $comments[0], 'bar', 'aaa' ); 700 add_comment_meta( $comments[1], 'bar', 'ccc' ); 701 add_comment_meta( $comments[2], 'bar', 'bbb' ); 702 703 $q = new WP_Comment_Query(); 704 $found = $q->query( array( 705 'fields' => 'ids', 706 'meta_query' => array( 707 'foo_key' => array( 708 'key' => 'foo', 709 'compare' => 'EXISTS', 710 ), 711 'bar_key' => array( 712 'key' => 'bar', 713 'compare' => 'EXISTS', 714 ), 715 ), 716 'orderby' => array( 717 'foo_key' => 'asc', 718 'bar_key' => 'desc', 719 ), 720 ) ); 721 722 $this->assertEquals( array( $comments[2], $comments[0], $comments[1] ), $found ); 723 } 724 632 725 633 726 /** … … 986 1079 987 1080 public function test_orderby_default() { 1081 global $wpdb; 1082 988 1083 $q = new WP_Comment_Query(); 989 1084 $q->query( array() ); 990 1085 991 $this->assertContains( 'ORDER BY comment_date_gmt', $q->request );1086 $this->assertContains( "ORDER BY $wpdb->comments.comment_date_gmt", $q->request ); 992 1087 } 993 1088 994 1089 public function test_orderby_single() { 1090 global $wpdb; 1091 995 1092 $q = new WP_Comment_Query(); 996 1093 $q->query( array( … … 998 1095 ) ); 999 1096 1000 $this->assertContains( 'ORDER BY comment_agent', $q->request );1097 $this->assertContains( "ORDER BY $wpdb->comments.comment_agent", $q->request ); 1001 1098 } 1002 1099 1003 1100 public function test_orderby_single_invalid() { 1101 global $wpdb; 1102 1004 1103 $q = new WP_Comment_Query(); 1005 1104 $q->query( array( … … 1007 1106 ) ); 1008 1107 1009 $this->assertContains( 'ORDER BY comment_date_gmt', $q->request );1108 $this->assertContains( "ORDER BY $wpdb->comments.comment_date_gmt", $q->request ); 1010 1109 } 1011 1110 1012 1111 public function test_orderby_comma_separated() { 1112 global $wpdb; 1113 1013 1114 $q = new WP_Comment_Query(); 1014 1115 $q->query( array( … … 1016 1117 ) ); 1017 1118 1018 $this->assertContains( 'ORDER BY comment_agent, comment_approved', $q->request ); 1019 } 1020 1021 public function test_orderby_array() { 1119 $this->assertContains( "ORDER BY $wpdb->comments.comment_agent DESC, $wpdb->comments.comment_approved DESC", $q->request ); 1120 } 1121 1122 public function test_orderby_flat_array() { 1123 global $wpdb; 1124 1022 1125 $q = new WP_Comment_Query(); 1023 1126 $q->query( array( … … 1025 1128 ) ); 1026 1129 1027 $this->assertContains( 'ORDER BY comment_agent, comment_approved', $q->request );1130 $this->assertContains( "ORDER BY $wpdb->comments.comment_agent DESC, $wpdb->comments.comment_approved DESC", $q->request ); 1028 1131 } 1029 1132 1030 1133 public function test_orderby_array_contains_invalid_item() { 1134 global $wpdb; 1135 1031 1136 $q = new WP_Comment_Query(); 1032 1137 $q->query( array( … … 1034 1139 ) ); 1035 1140 1036 $this->assertContains( 'ORDER BY comment_agent, comment_approved', $q->request );1141 $this->assertContains( "ORDER BY $wpdb->comments.comment_agent DESC, $wpdb->comments.comment_approved DESC", $q->request ); 1037 1142 } 1038 1143 1039 1144 public function test_orderby_array_contains_all_invalid_items() { 1145 global $wpdb; 1146 1040 1147 $q = new WP_Comment_Query(); 1041 1148 $q->query( array( … … 1043 1150 ) ); 1044 1151 1045 $this->assertContains( 'ORDER BY comment_date_gmt', $q->request );1152 $this->assertContains( "ORDER BY $wpdb->comments.comment_date_gmt", $q->request ); 1046 1153 } 1047 1154 … … 1080 1187 1081 1188 $this->assertNotContains( 'ORDER BY', $q->request ); 1189 } 1190 1191 /** 1192 * @ticket 30478 1193 */ 1194 public function test_orderby_array() { 1195 global $wpdb; 1196 1197 $q = new WP_Comment_Query(); 1198 $found = $q->query( array( 1199 'fields' => 'ids', 1200 'orderby' => array( 1201 'comment_agent' => 'DESC', 1202 'comment_date_gmt' => 'ASC', 1203 'comment_ID' => 'DESC', 1204 ), 1205 ) ); 1206 1207 $this->assertContains( "ORDER BY $wpdb->comments.comment_agent DESC, $wpdb->comments.comment_date_gmt ASC, $wpdb->comments.comment_ID DESC", $q->request ); 1208 } 1209 1210 /** 1211 * @ticket 30478 1212 */ 1213 public function test_orderby_array_should_discard_invalid_columns() { 1214 global $wpdb; 1215 1216 $q = new WP_Comment_Query(); 1217 $found = $q->query( array( 1218 'fields' => 'ids', 1219 'orderby' => array( 1220 'comment_agent' => 'DESC', 1221 'foo' => 'ASC', 1222 'comment_ID' => 'DESC', 1223 ), 1224 ) ); 1225 1226 $this->assertContains( "ORDER BY $wpdb->comments.comment_agent DESC, $wpdb->comments.comment_ID DESC", $q->request ); 1227 } 1228 1229 /** 1230 * @ticket 30478 1231 */ 1232 public function test_orderby_array_should_convert_invalid_order_to_DESC() { 1233 global $wpdb; 1234 1235 $q = new WP_Comment_Query(); 1236 $found = $q->query( array( 1237 'fields' => 'ids', 1238 'orderby' => array( 1239 'comment_agent' => 'DESC', 1240 'comment_date_gmt' => 'foo', 1241 'comment_ID' => 'DESC', 1242 ), 1243 ) ); 1244 1245 $this->assertContains( "ORDER BY $wpdb->comments.comment_agent DESC, $wpdb->comments.comment_date_gmt DESC, $wpdb->comments.comment_ID DESC", $q->request ); 1246 } 1247 1248 /** 1249 * @ticket 30478 1250 */ 1251 public function test_orderby_array_should_sort_by_comment_ID_as_fallback_and_should_inherit_order_from_comment_date_gmt() { 1252 global $wpdb; 1253 1254 $q = new WP_Comment_Query(); 1255 $found = $q->query( array( 1256 'fields' => 'ids', 1257 'orderby' => array( 1258 'comment_agent' => 'DESC', 1259 'comment_date_gmt' => 'ASC', 1260 ), 1261 ) ); 1262 1263 $this->assertContains( "ORDER BY $wpdb->comments.comment_agent DESC, $wpdb->comments.comment_date_gmt ASC, $wpdb->comments.comment_ID ASC", $q->request ); 1264 } 1265 1266 /** 1267 * @ticket 30478 1268 */ 1269 public function test_orderby_array_should_sort_by_comment_ID_as_fallback_and_should_inherit_order_from_comment_date() { 1270 global $wpdb; 1271 1272 $q = new WP_Comment_Query(); 1273 $found = $q->query( array( 1274 'fields' => 'ids', 1275 'orderby' => array( 1276 'comment_agent' => 'DESC', 1277 'comment_date' => 'ASC', 1278 ), 1279 ) ); 1280 1281 $this->assertContains( "ORDER BY $wpdb->comments.comment_agent DESC, $wpdb->comments.comment_date ASC, $wpdb->comments.comment_ID ASC", $q->request ); 1282 } 1283 1284 /** 1285 * @ticket 30478 1286 */ 1287 public function test_orderby_array_should_sort_by_comment_ID_DESC_as_fallback_when_not_sorted_by_date() { 1288 global $wpdb; 1289 1290 $q = new WP_Comment_Query(); 1291 $found = $q->query( array( 1292 'fields' => 'ids', 1293 'orderby' => array( 1294 'comment_agent' => 'ASC', 1295 ), 1296 ) ); 1297 1298 $this->assertContains( "ORDER BY $wpdb->comments.comment_agent ASC, $wpdb->comments.comment_ID DESC", $q->request ); 1299 } 1300 1301 /** 1302 * @ticket 30478 1303 */ 1304 public function test_orderby_date_modified_gmt_should_order_by_comment_ID_in_case_of_tie_ASC() { 1305 $now = current_time( 'mysql', 1 ); 1306 $comments = $this->factory->comment->create_many( 5, array( 1307 'comment_post_ID' => $this->post_id, 1308 'comment_date_gmt' => $now, 1309 ) ); 1310 1311 $q = new WP_Comment_Query(); 1312 $found = $q->query( array( 1313 'orderby' => 'comment_date_gmt', 1314 'order' => 'ASC', 1315 ) ); 1316 1317 // $comments is ASC by default. 1318 $this->assertEquals( $comments, wp_list_pluck( $found, 'comment_ID' ) ); 1319 } 1320 1321 /** 1322 * @ticket 30478 1323 */ 1324 public function test_orderby_date_modified_gmt_should_order_by_comment_ID_in_case_of_tie_DESC() { 1325 $now = current_time( 'mysql', 1 ); 1326 $comments = $this->factory->comment->create_many( 5, array( 1327 'comment_post_ID' => $this->post_id, 1328 'comment_date_gmt' => $now, 1329 ) ); 1330 1331 $q = new WP_Comment_Query(); 1332 $found = $q->query( array( 1333 'orderby' => 'comment_date_gmt', 1334 'order' => 'DESC', 1335 ) ); 1336 1337 // $comments is ASC by default. 1338 rsort( $comments ); 1339 1340 $this->assertEquals( $comments, wp_list_pluck( $found, 'comment_ID' ) ); 1082 1341 } 1083 1342
Note: See TracChangeset
for help on using the changeset viewer.