Changeset 33750 for trunk/src/wp-includes/comment-functions.php
- Timestamp:
- 08/26/2015 04:26:29 AM (9 years ago)
- File:
-
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/comment-functions.php
r33746 r33750 227 227 $query = new WP_Comment_Query; 228 228 return $query->query( $args ); 229 }230 231 /**232 * WordPress Comment Query class.233 *234 * See WP_Comment_Query::__construct() for accepted arguments.235 *236 * @since 3.1.0237 */238 class WP_Comment_Query {239 /**240 * SQL for database query.241 *242 * @since 4.0.1243 * @access public244 * @var string245 */246 public $request;247 248 /**249 * Metadata query container250 *251 * @since 3.5.0252 * @access public253 * @var object WP_Meta_Query254 */255 public $meta_query = false;256 257 /**258 * Date query container259 *260 * @since 3.7.0261 * @access public262 * @var object WP_Date_Query263 */264 public $date_query = false;265 266 /**267 * Query vars set by the user.268 *269 * @since 3.1.0270 * @access public271 * @var array272 */273 public $query_vars;274 275 /**276 * Default values for query vars.277 *278 * @since 4.2.0279 * @access public280 * @var array281 */282 public $query_var_defaults;283 284 /**285 * List of comments located by the query.286 *287 * @since 4.0.0288 * @access public289 * @var array290 */291 public $comments;292 293 /**294 * Make private/protected methods readable for backwards compatibility.295 *296 * @since 4.0.0297 * @access public298 *299 * @param callable $name Method to call.300 * @param array $arguments Arguments to pass when calling.301 * @return mixed|false Return value of the callback, false otherwise.302 */303 public function __call( $name, $arguments ) {304 if ( 'get_search_sql' === $name ) {305 return call_user_func_array( array( $this, $name ), $arguments );306 }307 return false;308 }309 310 /**311 * Constructor.312 *313 * Sets up the comment query, based on the query vars passed.314 *315 * @since 4.2.0316 * @access public317 *318 * @param string|array $query {319 * Optional. Array or query string of comment query parameters. Default empty.320 *321 * @type string $author_email Comment author email address. Default empty.322 * @type array $author__in Array of author IDs to include comments for. Default empty.323 * @type array $author__not_in Array of author IDs to exclude comments for. Default empty.324 * @type array $comment__in Array of comment IDs to include. Default empty.325 * @type array $comment__not_in Array of comment IDs to exclude. Default empty.326 * @type bool $count Whether to return a comment count (true) or array of comment327 * objects (false). Default false.328 * @type array $date_query Date query clauses to limit comments by. See WP_Date_Query.329 * Default null.330 * @type string $fields Comment fields to return. Accepts 'ids' for comment IDs only or331 * empty for all fields. Default empty.332 * @type int $ID Currently unused.333 * @type array $include_unapproved Array of IDs or email addresses of users whose unapproved comments334 * will be returned by the query regardless of `$status`. Default empty.335 * @type int $karma Karma score to retrieve matching comments for. Default empty.336 * @type string $meta_key Include comments with a matching comment meta key. Default empty.337 * @type string $meta_value Include comments with a matching comment meta value. Requires338 * `$meta_key` to be set. Default empty.339 * @type array $meta_query Meta query clauses to limit retrieved comments by.340 * See WP_Meta_Query. Default empty.341 * @type int $number Maximum number of comments to retrieve. Default null (no limit).342 * @type int $offset Number of comments to offset the query. Used to build LIMIT clause.343 * Default 0.344 * @type string|array $orderby Comment status or array of statuses. To use 'meta_value' or345 * 'meta_value_num', `$meta_key` must also be defined. To sort by346 * a specific `$meta_query` clause, use that clause's array key.347 * Accepts 'comment_agent', 'comment_approved', 'comment_author',348 * 'comment_author_email', 'comment_author_IP',349 * 'comment_author_url', 'comment_content', 'comment_date',350 * 'comment_date_gmt', 'comment_ID', 'comment_karma',351 * 'comment_parent', 'comment_post_ID', 'comment_type', 'user_id',352 * 'meta_value', 'meta_value_num', the value of $meta_key, and the353 * array keys of `$meta_query`. Also accepts false, an empty array,354 * or 'none' to disable `ORDER BY` clause.355 * Default: 'comment_date_gmt'.356 * @type string $order How to order retrieved comments. Accepts 'ASC', 'DESC'.357 * Default: 'DESC'.358 * @type int $parent Parent ID of comment to retrieve children of. Default empty.359 * @type array $post_author__in Array of author IDs to retrieve comments for. Default empty.360 * @type array $post_author__not_in Array of author IDs *not* to retrieve comments for. Default empty.361 * @type int $post_ID Currently unused.362 * @type int $post_id Limit results to those affiliated with a given post ID. Default 0.363 * @type array $post__in Array of post IDs to include affiliated comments for. Default empty.364 * @type array $post__not_in Array of post IDs to exclude affiliated comments for. Default empty.365 * @type int $post_author Comment author ID to limit results by. Default empty.366 * @type string $post_status Post status to retrieve affiliated comments for. Default empty.367 * @type string $post_type Post type to retrieve affiliated comments for. Default empty.368 * @type string $post_name Post name to retrieve affiliated comments for. Default empty.369 * @type int $post_parent Post parent ID to retrieve affiliated comments for. Default empty.370 * @type string $search Search term(s) to retrieve matching comments for. Default empty.371 * @type string $status Comment status to limit results by. Accepts 'hold'372 * (`comment_status=0`), 'approve' (`comment_status=1`), 'all', or a373 * custom comment status. Default 'all'.374 * @type string|array $type Include comments of a given type, or array of types. Accepts375 * 'comment', 'pings' (includes 'pingback' and 'trackback'), or any376 * custom type string. Default empty.377 * @type array $type__in Include comments from a given array of comment types. Default empty.378 * @type array $type__not_in Exclude comments from a given array of comment types. Default empty.379 * @type int $user_id Include comments for a specific user ID. Default empty.380 * }381 */382 public function __construct( $query = '' ) {383 $this->query_var_defaults = array(384 'author_email' => '',385 'author__in' => '',386 'author__not_in' => '',387 'include_unapproved' => '',388 'fields' => '',389 'ID' => '',390 'comment__in' => '',391 'comment__not_in' => '',392 'karma' => '',393 'number' => '',394 'offset' => '',395 'orderby' => '',396 'order' => 'DESC',397 'parent' => '',398 'post_author__in' => '',399 'post_author__not_in' => '',400 'post_ID' => '',401 'post_id' => 0,402 'post__in' => '',403 'post__not_in' => '',404 'post_author' => '',405 'post_name' => '',406 'post_parent' => '',407 'post_status' => '',408 'post_type' => '',409 'status' => 'all',410 'type' => '',411 'type__in' => '',412 'type__not_in' => '',413 'user_id' => '',414 'search' => '',415 'count' => false,416 'meta_key' => '',417 'meta_value' => '',418 'meta_query' => '',419 'date_query' => null, // See WP_Date_Query420 );421 422 if ( ! empty( $query ) ) {423 $this->query( $query );424 }425 }426 427 /**428 * Parse arguments passed to the comment query with default query parameters.429 *430 * @since 4.2.0 Extracted from WP_Comment_Query::query().431 *432 * @access public433 *434 * @param string|array $query WP_Comment_Query arguments. See WP_Comment_Query::__construct()435 */436 public function parse_query( $query = '' ) {437 if ( empty( $query ) ) {438 $query = $this->query_vars;439 }440 441 $this->query_vars = wp_parse_args( $query, $this->query_var_defaults );442 do_action_ref_array( 'parse_comment_query', array( &$this ) );443 }444 445 /**446 * Sets up the WordPress query for retrieving comments.447 *448 * @since 3.1.0449 * @since 4.1.0 Introduced 'comment__in', 'comment__not_in', 'post_author__in',450 * 'post_author__not_in', 'author__in', 'author__not_in', 'post__in',451 * 'post__not_in', 'include_unapproved', 'type__in', and 'type__not_in'452 * arguments to $query_vars.453 * @since 4.2.0 Moved parsing to WP_Comment_Query::parse_query().454 * @access public455 *456 * @param string|array $query Array or URL query string of parameters.457 * @return array|int List of comments, or number of comments when 'count' is passed as a query var.458 */459 public function query( $query ) {460 $this->query_vars = wp_parse_args( $query );461 return $this->get_comments();462 }463 464 /**465 * Get a list of comments matching the query vars.466 *467 * @since 4.2.0468 * @access public469 *470 * @global wpdb $wpdb WordPress database abstraction object.471 *472 * @return int|array The list of comments.473 */474 public function get_comments() {475 global $wpdb;476 477 $groupby = '';478 479 $this->parse_query();480 481 // Parse meta query482 $this->meta_query = new WP_Meta_Query();483 $this->meta_query->parse_query_vars( $this->query_vars );484 485 /**486 * Fires before comments are retrieved.487 *488 * @since 3.1.0489 *490 * @param WP_Comment_Query &$this Current instance of WP_Comment_Query, passed by reference.491 */492 do_action_ref_array( 'pre_get_comments', array( &$this ) );493 494 // Reparse query vars, in case they were modified in a 'pre_get_comments' callback.495 $this->meta_query->parse_query_vars( $this->query_vars );496 if ( ! empty( $this->meta_query->queries ) ) {497 $meta_query_clauses = $this->meta_query->get_sql( 'comment', $wpdb->comments, 'comment_ID', $this );498 }499 500 // $args can include anything. Only use the args defined in the query_var_defaults to compute the key.501 $key = md5( serialize( wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ) ) );502 $last_changed = wp_cache_get( 'last_changed', 'comment' );503 if ( ! $last_changed ) {504 $last_changed = microtime();505 wp_cache_set( 'last_changed', $last_changed, 'comment' );506 }507 $cache_key = "get_comments:$key:$last_changed";508 509 if ( $cache = wp_cache_get( $cache_key, 'comment' ) ) {510 $this->comments = $cache;511 return $this->comments;512 }513 514 $where = array();515 516 // Assemble clauses related to 'comment_approved'.517 $approved_clauses = array();518 519 // 'status' accepts an array or a comma-separated string.520 $status_clauses = array();521 $statuses = $this->query_vars['status'];522 if ( ! is_array( $statuses ) ) {523 $statuses = preg_split( '/[\s,]+/', $statuses );524 }525 526 // 'any' overrides other statuses.527 if ( ! in_array( 'any', $statuses ) ) {528 foreach ( $statuses as $status ) {529 switch ( $status ) {530 case 'hold' :531 $status_clauses[] = "comment_approved = '0'";532 break;533 534 case 'approve' :535 $status_clauses[] = "comment_approved = '1'";536 break;537 538 case 'all' :539 case '' :540 $status_clauses[] = "( comment_approved = '0' OR comment_approved = '1' )";541 break;542 543 default :544 $status_clauses[] = $wpdb->prepare( "comment_approved = %s", $status );545 break;546 }547 }548 549 if ( ! empty( $status_clauses ) ) {550 $approved_clauses[] = '( ' . implode( ' OR ', $status_clauses ) . ' )';551 }552 }553 554 // User IDs or emails whose unapproved comments are included, regardless of $status.555 if ( ! empty( $this->query_vars['include_unapproved'] ) ) {556 $include_unapproved = $this->query_vars['include_unapproved'];557 558 // Accepts arrays or comma-separated strings.559 if ( ! is_array( $include_unapproved ) ) {560 $include_unapproved = preg_split( '/[\s,]+/', $include_unapproved );561 }562 563 $unapproved_ids = $unapproved_emails = array();564 foreach ( $include_unapproved as $unapproved_identifier ) {565 // Numeric values are assumed to be user ids.566 if ( is_numeric( $unapproved_identifier ) ) {567 $approved_clauses[] = $wpdb->prepare( "( user_id = %d AND comment_approved = '0' )", $unapproved_identifier );568 569 // Otherwise we match against email addresses.570 } else {571 $approved_clauses[] = $wpdb->prepare( "( comment_author_email = %s AND comment_approved = '0' )", $unapproved_identifier );572 }573 }574 }575 576 // Collapse comment_approved clauses into a single OR-separated clause.577 if ( ! empty( $approved_clauses ) ) {578 if ( 1 === count( $approved_clauses ) ) {579 $where[] = $approved_clauses[0];580 } else {581 $where[] = '( ' . implode( ' OR ', $approved_clauses ) . ' )';582 }583 }584 585 $order = ( 'ASC' == strtoupper( $this->query_vars['order'] ) ) ? 'ASC' : 'DESC';586 587 // Disable ORDER BY with 'none', an empty array, or boolean false.588 if ( in_array( $this->query_vars['orderby'], array( 'none', array(), false ), true ) ) {589 $orderby = '';590 } elseif ( ! empty( $this->query_vars['orderby'] ) ) {591 $ordersby = is_array( $this->query_vars['orderby'] ) ?592 $this->query_vars['orderby'] :593 preg_split( '/[,\s]/', $this->query_vars['orderby'] );594 595 $orderby_array = array();596 $found_orderby_comment_ID = false;597 foreach ( $ordersby as $_key => $_value ) {598 if ( ! $_value ) {599 continue;600 }601 602 if ( is_int( $_key ) ) {603 $_orderby = $_value;604 $_order = $order;605 } else {606 $_orderby = $_key;607 $_order = $_value;608 }609 610 if ( ! $found_orderby_comment_ID && 'comment_ID' === $_orderby ) {611 $found_orderby_comment_ID = true;612 }613 614 $parsed = $this->parse_orderby( $_orderby );615 616 if ( ! $parsed ) {617 continue;618 }619 620 $orderby_array[] = $parsed . ' ' . $this->parse_order( $_order );621 }622 623 // If no valid clauses were found, order by comment_date_gmt.624 if ( empty( $orderby_array ) ) {625 $orderby_array[] = "$wpdb->comments.comment_date_gmt $order";626 }627 628 // To ensure determinate sorting, always include a comment_ID clause.629 if ( ! $found_orderby_comment_ID ) {630 $comment_ID_order = '';631 632 // Inherit order from comment_date or comment_date_gmt, if available.633 foreach ( $orderby_array as $orderby_clause ) {634 if ( preg_match( '/comment_date(?:_gmt)*\ (ASC|DESC)/', $orderby_clause, $match ) ) {635 $comment_ID_order = $match[1];636 break;637 }638 }639 640 // If no date-related order is available, use the date from the first available clause.641 if ( ! $comment_ID_order ) {642 foreach ( $orderby_array as $orderby_clause ) {643 if ( false !== strpos( 'ASC', $orderby_clause ) ) {644 $comment_ID_order = 'ASC';645 } else {646 $comment_ID_order = 'DESC';647 }648 649 break;650 }651 }652 653 // Default to DESC.654 if ( ! $comment_ID_order ) {655 $comment_ID_order = 'DESC';656 }657 658 $orderby_array[] = "$wpdb->comments.comment_ID $comment_ID_order";659 }660 661 $orderby = implode( ', ', $orderby_array );662 } else {663 $orderby = "$wpdb->comments.comment_date_gmt $order";664 }665 666 $number = absint( $this->query_vars['number'] );667 $offset = absint( $this->query_vars['offset'] );668 669 if ( ! empty( $number ) ) {670 if ( $offset ) {671 $limits = 'LIMIT ' . $offset . ',' . $number;672 } else {673 $limits = 'LIMIT ' . $number;674 }675 } else {676 $limits = '';677 }678 679 if ( $this->query_vars['count'] ) {680 $fields = 'COUNT(*)';681 } else {682 switch ( strtolower( $this->query_vars['fields'] ) ) {683 case 'ids':684 $fields = "$wpdb->comments.comment_ID";685 break;686 default:687 $fields = "*";688 break;689 }690 }691 692 $join = '';693 694 $post_id = absint( $this->query_vars['post_id'] );695 if ( ! empty( $post_id ) ) {696 $where[] = $wpdb->prepare( 'comment_post_ID = %d', $post_id );697 }698 699 // Parse comment IDs for an IN clause.700 if ( ! empty( $this->query_vars['comment__in'] ) ) {701 $where[] = "$wpdb->comments.comment_ID IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['comment__in'] ) ) . ' )';702 }703 704 // Parse comment IDs for a NOT IN clause.705 if ( ! empty( $this->query_vars['comment__not_in'] ) ) {706 $where[] = "$wpdb->comments.comment_ID NOT IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['comment__not_in'] ) ) . ' )';707 }708 709 // Parse comment post IDs for an IN clause.710 if ( ! empty( $this->query_vars['post__in'] ) ) {711 $where[] = 'comment_post_ID IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post__in'] ) ) . ' )';712 }713 714 // Parse comment post IDs for a NOT IN clause.715 if ( ! empty( $this->query_vars['post__not_in'] ) ) {716 $where[] = 'comment_post_ID NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post__not_in'] ) ) . ' )';717 }718 719 if ( '' !== $this->query_vars['author_email'] ) {720 $where[] = $wpdb->prepare( 'comment_author_email = %s', $this->query_vars['author_email'] );721 }722 723 if ( '' !== $this->query_vars['karma'] ) {724 $where[] = $wpdb->prepare( 'comment_karma = %d', $this->query_vars['karma'] );725 }726 727 // Filtering by comment_type: 'type', 'type__in', 'type__not_in'.728 $raw_types = array(729 'IN' => array_merge( (array) $this->query_vars['type'], (array) $this->query_vars['type__in'] ),730 'NOT IN' => (array) $this->query_vars['type__not_in'],731 );732 733 $comment_types = array();734 foreach ( $raw_types as $operator => $_raw_types ) {735 $_raw_types = array_unique( $_raw_types );736 737 foreach ( $_raw_types as $type ) {738 switch ( $type ) {739 // An empty translates to 'all', for backward compatibility740 case '':741 case 'all' :742 break;743 744 case 'comment':745 case 'comments':746 $comment_types[ $operator ][] = "''";747 break;748 749 case 'pings':750 $comment_types[ $operator ][] = "'pingback'";751 $comment_types[ $operator ][] = "'trackback'";752 break;753 754 default:755 $comment_types[ $operator ][] = $wpdb->prepare( '%s', $type );756 break;757 }758 }759 760 if ( ! empty( $comment_types[ $operator ] ) ) {761 $types_sql = implode( ', ', $comment_types[ $operator ] );762 $where[] = "comment_type $operator ($types_sql)";763 }764 }765 766 if ( '' !== $this->query_vars['parent'] ) {767 $where[] = $wpdb->prepare( 'comment_parent = %d', $this->query_vars['parent'] );768 }769 770 if ( is_array( $this->query_vars['user_id'] ) ) {771 $where[] = 'user_id IN (' . implode( ',', array_map( 'absint', $this->query_vars['user_id'] ) ) . ')';772 } elseif ( '' !== $this->query_vars['user_id'] ) {773 $where[] = $wpdb->prepare( 'user_id = %d', $this->query_vars['user_id'] );774 }775 776 if ( '' !== $this->query_vars['search'] ) {777 $search_sql = $this->get_search_sql(778 $this->query_vars['search'],779 array( 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_author_IP', 'comment_content' )780 );781 782 // Strip leading 'AND'.783 $where[] = preg_replace( '/^\s*AND\s*/', '', $search_sql );784 }785 786 // If any post-related query vars are passed, join the posts table.787 $join_posts_table = false;788 $plucked = wp_array_slice_assoc( $this->query_vars, array( 'post_author', 'post_name', 'post_parent', 'post_status', 'post_type' ) );789 $post_fields = array_filter( $plucked );790 791 if ( ! empty( $post_fields ) ) {792 $join_posts_table = true;793 foreach ( $post_fields as $field_name => $field_value ) {794 // $field_value may be an array.795 $esses = array_fill( 0, count( (array) $field_value ), '%s' );796 $where[] = $wpdb->prepare( " {$wpdb->posts}.{$field_name} IN (" . implode( ',', $esses ) . ')', $field_value );797 }798 }799 800 // Comment author IDs for an IN clause.801 if ( ! empty( $this->query_vars['author__in'] ) ) {802 $where[] = 'user_id IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['author__in'] ) ) . ' )';803 }804 805 // Comment author IDs for a NOT IN clause.806 if ( ! empty( $this->query_vars['author__not_in'] ) ) {807 $where[] = 'user_id NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['author__not_in'] ) ) . ' )';808 }809 810 // Post author IDs for an IN clause.811 if ( ! empty( $this->query_vars['post_author__in'] ) ) {812 $join_posts_table = true;813 $where[] = 'post_author IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post_author__in'] ) ) . ' )';814 }815 816 // Post author IDs for a NOT IN clause.817 if ( ! empty( $this->query_vars['post_author__not_in'] ) ) {818 $join_posts_table = true;819 $where[] = 'post_author NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post_author__not_in'] ) ) . ' )';820 }821 822 if ( $join_posts_table ) {823 $join = "JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID";824 }825 826 if ( ! empty( $meta_query_clauses ) ) {827 $join .= $meta_query_clauses['join'];828 829 // Strip leading 'AND'.830 $where[] = preg_replace( '/^\s*AND\s*/', '', $meta_query_clauses['where'] );831 832 if ( ! $this->query_vars['count'] ) {833 $groupby = "{$wpdb->comments}.comment_ID";834 }835 }836 837 $date_query = $this->query_vars['date_query'];838 if ( ! empty( $date_query ) && is_array( $date_query ) ) {839 $date_query_object = new WP_Date_Query( $date_query, 'comment_date' );840 $where[] = preg_replace( '/^\s*AND\s*/', '', $date_query_object->get_sql() );841 }842 843 $where = implode( ' AND ', $where );844 845 $pieces = array( 'fields', 'join', 'where', 'orderby', 'limits', 'groupby' );846 /**847 * Filter the comment query clauses.848 *849 * @since 3.1.0850 *851 * @param array $pieces A compacted array of comment query clauses.852 * @param WP_Comment_Query &$this Current instance of WP_Comment_Query, passed by reference.853 */854 $clauses = apply_filters_ref_array( 'comments_clauses', array( compact( $pieces ), &$this ) );855 856 $fields = isset( $clauses[ 'fields' ] ) ? $clauses[ 'fields' ] : '';857 $join = isset( $clauses[ 'join' ] ) ? $clauses[ 'join' ] : '';858 $where = isset( $clauses[ 'where' ] ) ? $clauses[ 'where' ] : '';859 $orderby = isset( $clauses[ 'orderby' ] ) ? $clauses[ 'orderby' ] : '';860 $limits = isset( $clauses[ 'limits' ] ) ? $clauses[ 'limits' ] : '';861 $groupby = isset( $clauses[ 'groupby' ] ) ? $clauses[ 'groupby' ] : '';862 863 if ( $where ) {864 $where = 'WHERE ' . $where;865 }866 867 if ( $groupby ) {868 $groupby = 'GROUP BY ' . $groupby;869 }870 871 if ( $orderby ) {872 $orderby = "ORDER BY $orderby";873 }874 875 $this->request = "SELECT $fields FROM $wpdb->comments $join $where $groupby $orderby $limits";876 877 if ( $this->query_vars['count'] ) {878 return $wpdb->get_var( $this->request );879 }880 881 if ( 'ids' == $this->query_vars['fields'] ) {882 $this->comments = $wpdb->get_col( $this->request );883 return array_map( 'intval', $this->comments );884 }885 886 $results = $wpdb->get_results( $this->request );887 /**888 * Filter the comment query results.889 *890 * @since 3.1.0891 *892 * @param array $results An array of comments.893 * @param WP_Comment_Query &$this Current instance of WP_Comment_Query, passed by reference.894 */895 $comments = apply_filters_ref_array( 'the_comments', array( $results, &$this ) );896 897 wp_cache_add( $cache_key, $comments, 'comment' );898 if ( '*' === $fields ) {899 update_comment_cache( $comments );900 }901 902 $this->comments = $comments;903 return $this->comments;904 }905 906 /**907 * Used internally to generate an SQL string for searching across multiple columns908 *909 * @since 3.1.0910 * @access protected911 *912 * @global wpdb $wpdb913 *914 * @param string $string915 * @param array $cols916 * @return string917 */918 protected function get_search_sql( $string, $cols ) {919 global $wpdb;920 921 $like = '%' . $wpdb->esc_like( $string ) . '%';922 923 $searches = array();924 foreach ( $cols as $col ) {925 $searches[] = $wpdb->prepare( "$col LIKE %s", $like );926 }927 928 return ' AND (' . implode(' OR ', $searches) . ')';929 }930 931 /**932 * Parse and sanitize 'orderby' keys passed to the comment query.933 *934 * @since 4.2.0935 * @access protected936 *937 * @global wpdb $wpdb WordPress database abstraction object.938 *939 * @param string $orderby Alias for the field to order by.940 * @return string|false Value to used in the ORDER clause. False otherwise.941 */942 protected function parse_orderby( $orderby ) {943 global $wpdb;944 945 $allowed_keys = array(946 'comment_agent',947 'comment_approved',948 'comment_author',949 'comment_author_email',950 'comment_author_IP',951 'comment_author_url',952 'comment_content',953 'comment_date',954 'comment_date_gmt',955 'comment_ID',956 'comment_karma',957 'comment_parent',958 'comment_post_ID',959 'comment_type',960 'user_id',961 );962 963 if ( ! empty( $this->query_vars['meta_key'] ) ) {964 $allowed_keys[] = $this->query_vars['meta_key'];965 $allowed_keys[] = 'meta_value';966 $allowed_keys[] = 'meta_value_num';967 }968 969 $meta_query_clauses = $this->meta_query->get_clauses();970 if ( $meta_query_clauses ) {971 $allowed_keys = array_merge( $allowed_keys, array_keys( $meta_query_clauses ) );972 }973 974 $parsed = false;975 if ( $orderby == $this->query_vars['meta_key'] || $orderby == 'meta_value' ) {976 $parsed = "$wpdb->commentmeta.meta_value";977 } elseif ( $orderby == 'meta_value_num' ) {978 $parsed = "$wpdb->commentmeta.meta_value+0";979 } elseif ( in_array( $orderby, $allowed_keys ) ) {980 981 if ( isset( $meta_query_clauses[ $orderby ] ) ) {982 $meta_clause = $meta_query_clauses[ $orderby ];983 $parsed = sprintf( "CAST(%s.meta_value AS %s)", esc_sql( $meta_clause['alias'] ), esc_sql( $meta_clause['cast'] ) );984 } else {985 $parsed = "$wpdb->comments.$orderby";986 }987 }988 989 return $parsed;990 }991 992 /**993 * Parse an 'order' query variable and cast it to ASC or DESC as necessary.994 *995 * @since 4.2.0996 * @access protected997 *998 * @param string $order The 'order' query variable.999 * @return string The sanitized 'order' query variable.1000 */1001 protected function parse_order( $order ) {1002 if ( ! is_string( $order ) || empty( $order ) ) {1003 return 'DESC';1004 }1005 1006 if ( 'ASC' === strtoupper( $order ) ) {1007 return 'ASC';1008 } else {1009 return 'DESC';1010 }1011 }1012 229 } 1013 230
Note: See TracChangeset
for help on using the changeset viewer.