]> scripts.mit.edu Git - autoinstalls/wordpress.git/blobdiff - wp-includes/class-wp-comment-query.php
WordPress 4.6.3-scripts
[autoinstalls/wordpress.git] / wp-includes / class-wp-comment-query.php
index 9f6aaa5327dddfa3c5160a2dd99b2bf81cb44408..0920ca229f5ae496a073de538042039a33d02820 100644 (file)
@@ -62,7 +62,7 @@ class WP_Comment_Query {
        /**
         * SQL WHERE clause.
         *
-        * Stored after the 'comments_clauses' filter is run on the compiled WHERE sub-clauses.
+        * Stored after the {@see 'comments_clauses'} filter is run on the compiled WHERE sub-clauses.
         *
         * @since 4.4.2
         * @access protected
@@ -125,7 +125,7 @@ class WP_Comment_Query {
        public $max_num_pages = 0;
 
        /**
-        * Make private/protected methods readable for backwards compatibility.
+        * Make private/protected methods readable for backward compatibility.
         *
         * @since 4.0.0
         * @access public
@@ -150,12 +150,15 @@ class WP_Comment_Query {
         * @since 4.4.0 `$parent__in` and `$parent__not_in` were added.
         * @since 4.4.0 Order by `comment__in` was added. `$update_comment_meta_cache`, `$no_found_rows`,
         *              `$hierarchical`, and `$update_comment_post_cache` were added.
+        * @since 4.5.0 Introduced the `$author_url` argument.
+        * @since 4.6.0 Introduced the `$cache_domain` argument.
         * @access public
         *
         * @param string|array $query {
         *     Optional. Array or query string of comment query parameters. Default empty.
         *
         *     @type string       $author_email              Comment author email address. Default empty.
+        *     @type string       $author_url                Comment author URL. Default empty.
         *     @type array        $author__in                Array of author IDs to include comments for. Default empty.
         *     @type array        $author__not_in            Array of author IDs to exclude comments for. Default empty.
         *     @type array        $comment__in               Array of comment IDs to include. Default empty.
@@ -217,11 +220,12 @@ class WP_Comment_Query {
         *                                                   Default empty.
         *     @type array        $post__not_in              Array of post IDs to exclude affiliated comments for.
         *                                                   Default empty.
-        *     @type int          $post_author               Comment author ID to limit results by. Default empty.
-        *     @type string       $post_status               Post status to retrieve affiliated comments for.
-        *                                                   Default empty.
-        *     @type string       $post_type                 Post type to retrieve affiliated comments for.
+        *     @type int          $post_author               Post author ID to limit results by. Default empty.
+        *     @type string|array $post_status               Post status or array of post statuses to retrieve
+        *                                                   affiliated comments for. Pass 'any' to match any value.
         *                                                   Default empty.
+        *     @type string       $post_type                 Post type or array of post types to retrieve affiliated
+        *                                                   comments for. Pass 'any' to match any value. Default empty.
         *     @type string       $post_name                 Post name to retrieve affiliated comments for.
         *                                                   Default empty.
         *     @type int          $post_parent               Post parent ID to retrieve affiliated comments for.
@@ -247,6 +251,8 @@ class WP_Comment_Query {
         *                                                   The parameter is ignored (forced to `false`) when
         *                                                   `$fields` is 'ids' or 'counts'. Accepts 'threaded',
         *                                                   'flat', or false. Default: false.
+        *     @type string       $cache_domain              Unique cache key to be produced when this query is stored in
+        *                                                   an object cache. Default is 'core'.
         *     @type bool         $update_comment_meta_cache Whether to prime the metadata cache for found comments.
         *                                                   Default true.
         *     @type bool         $update_comment_post_cache Whether to prime the cache for comment posts.
@@ -256,6 +262,7 @@ class WP_Comment_Query {
        public function __construct( $query = '' ) {
                $this->query_var_defaults = array(
                        'author_email' => '',
+                       'author_url' => '',
                        'author__in' => '',
                        'author__not_in' => '',
                        'include_unapproved' => '',
@@ -270,6 +277,8 @@ class WP_Comment_Query {
                        'orderby' => '',
                        'order' => 'DESC',
                        'parent' => '',
+                       'parent__in' => '',
+                       'parent__not_in' => '',
                        'post_author__in' => '',
                        'post_author__not_in' => '',
                        'post_ID' => '',
@@ -293,6 +302,7 @@ class WP_Comment_Query {
                        'meta_query' => '',
                        'date_query' => null, // See WP_Date_Query
                        'hierarchical' => false,
+                       'cache_domain' => 'core',
                        'update_comment_meta_cache' => true,
                        'update_comment_post_cache' => false,
                );
@@ -305,7 +315,7 @@ class WP_Comment_Query {
        /**
         * Parse arguments passed to the comment query with default query parameters.
         *
-        * @since  4.2.0 Extracted from WP_Comment_Query::query().
+        * @since 4.2.0 Extracted from WP_Comment_Query::query().
         *
         * @access public
         *
@@ -317,6 +327,14 @@ class WP_Comment_Query {
                }
 
                $this->query_vars = wp_parse_args( $query, $this->query_var_defaults );
+
+               /**
+                * Fires after the comment query vars have been parsed.
+                *
+                * @since 4.2.0
+                *
+                * @param WP_Comment_Query &$this The WP_Comment_Query instance (passed by reference).
+                */
                do_action_ref_array( 'parse_comment_query', array( &$this ) );
        }
 
@@ -347,7 +365,7 @@ class WP_Comment_Query {
         *
         * @global wpdb $wpdb WordPress database abstraction object.
         *
-        * @return int|array The list of comments.
+        * @return int|array List of comments or number of found comments if `$count` argument is true.
         */
        public function get_comments() {
                global $wpdb;
@@ -380,12 +398,27 @@ class WP_Comment_Query {
                        $last_changed = microtime();
                        wp_cache_set( 'last_changed', $last_changed, 'comment' );
                }
-               $cache_key = "get_comment_ids:$key:$last_changed";
 
-               $comment_ids = wp_cache_get( $cache_key, 'comment' );
-               if ( false === $comment_ids ) {
+               $cache_key   = "get_comments:$key:$last_changed";
+               $cache_value = wp_cache_get( $cache_key, 'comment' );
+               if ( false === $cache_value ) {
                        $comment_ids = $this->get_comment_ids();
-                       wp_cache_add( $cache_key, $comment_ids, 'comment' );
+                       if ( $comment_ids ) {
+                               $this->set_found_comments();
+                       }
+
+                       $cache_value = array(
+                               'comment_ids'    => $comment_ids,
+                               'found_comments' => $this->found_comments,
+                       );
+                       wp_cache_add( $cache_key, $cache_value, 'comment' );
+               } else {
+                       $comment_ids          = $cache_value['comment_ids'];
+                       $this->found_comments = $cache_value['found_comments'];
+               }
+
+               if ( $this->found_comments && $this->query_vars['number'] ) {
+                       $this->max_num_pages = ceil( $this->found_comments / $this->query_vars['number'] );
                }
 
                // If querying for a count only, there's nothing more to do.
@@ -396,23 +429,6 @@ class WP_Comment_Query {
 
                $comment_ids = array_map( 'intval', $comment_ids );
 
-               $this->comment_count = count( $this->comments );
-
-               if ( $comment_ids && $this->query_vars['number'] && ! $this->query_vars['no_found_rows'] ) {
-                       /**
-                        * Filter the query used to retrieve found comment count.
-                        *
-                        * @since 4.4.0
-                        *
-                        * @param string           $found_comments_query SQL query. Default 'SELECT FOUND_ROWS()'.
-                        * @param WP_Comment_Query $comment_query        The `WP_Comment_Query` instance.
-                        */
-                       $found_comments_query = apply_filters( 'found_comments_query', 'SELECT FOUND_ROWS()', $this );
-                       $this->found_comments = (int) $wpdb->get_var( $found_comments_query );
-
-                       $this->max_num_pages = ceil( $this->found_comments / $this->query_vars['number'] );
-               }
-
                if ( 'ids' == $this->query_vars['fields'] ) {
                        $this->comments = $comment_ids;
                        return $this->comments;
@@ -439,7 +455,7 @@ class WP_Comment_Query {
                }
 
                /**
-                * Filter the comment query results.
+                * Filters the comment query results.
                 *
                 * @since 3.1.0
                 *
@@ -681,6 +697,10 @@ class WP_Comment_Query {
                        $this->sql_clauses['where']['author_email'] = $wpdb->prepare( 'comment_author_email = %s', $this->query_vars['author_email'] );
                }
 
+               if ( '' !== $this->query_vars['author_url'] ) {
+                       $this->sql_clauses['where']['author_url'] = $wpdb->prepare( 'comment_author_url = %s', $this->query_vars['author_url'] );
+               }
+
                if ( '' !== $this->query_vars['karma'] ) {
                        $this->sql_clauses['where']['karma'] = $wpdb->prepare( 'comment_karma = %d', $this->query_vars['karma'] );
                }
@@ -724,12 +744,13 @@ class WP_Comment_Query {
                        }
                }
 
-               if ( $this->query_vars['hierarchical'] && ! $this->query_vars['parent'] ) {
-                       $this->query_vars['parent'] = 0;
+               $parent = $this->query_vars['parent'];
+               if ( $this->query_vars['hierarchical'] && ! $parent ) {
+                       $parent = 0;
                }
 
-               if ( '' !== $this->query_vars['parent'] ) {
-                       $this->sql_clauses['where']['parent'] = $wpdb->prepare( 'comment_parent = %d', $this->query_vars['parent'] );
+               if ( '' !== $parent ) {
+                       $this->sql_clauses['where']['parent'] = $wpdb->prepare( 'comment_parent = %d', $parent );
                }
 
                if ( is_array( $this->query_vars['user_id'] ) ) {
@@ -738,7 +759,8 @@ class WP_Comment_Query {
                        $this->sql_clauses['where']['user_id'] = $wpdb->prepare( 'user_id = %d', $this->query_vars['user_id'] );
                }
 
-               if ( '' !== $this->query_vars['search'] ) {
+               // Falsy search strings are ignored.
+               if ( strlen( $this->query_vars['search'] ) ) {
                        $search_sql = $this->get_search_sql(
                                $this->query_vars['search'],
                                array( 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_author_IP', 'comment_content' )
@@ -750,7 +772,7 @@ class WP_Comment_Query {
 
                // If any post-related query vars are passed, join the posts table.
                $join_posts_table = false;
-               $plucked = wp_array_slice_assoc( $this->query_vars, array( 'post_author', 'post_name', 'post_parent', 'post_status', 'post_type' ) );
+               $plucked = wp_array_slice_assoc( $this->query_vars, array( 'post_author', 'post_name', 'post_parent' ) );
                $post_fields = array_filter( $plucked );
 
                if ( ! empty( $post_fields ) ) {
@@ -762,6 +784,27 @@ class WP_Comment_Query {
                        }
                }
 
+               // 'post_status' and 'post_type' are handled separately, due to the specialized behavior of 'any'.
+               foreach ( array( 'post_status', 'post_type' ) as $field_name ) {
+                       $q_values = array();
+                       if ( ! empty( $this->query_vars[ $field_name ] ) ) {
+                               $q_values = $this->query_vars[ $field_name ];
+                               if ( ! is_array( $q_values ) ) {
+                                       $q_values = explode( ',', $q_values );
+                               }
+
+                               // 'any' will cause the query var to be ignored.
+                               if ( in_array( 'any', $q_values, true ) || empty( $q_values ) ) {
+                                       continue;
+                               }
+
+                               $join_posts_table = true;
+
+                               $esses = array_fill( 0, count( $q_values ), '%s' );
+                               $this->sql_clauses['where'][ $field_name ] = $wpdb->prepare( " {$wpdb->posts}.{$field_name} IN (" . implode( ',', $esses ) . ")", $q_values );
+                       }
+               }
+
                // Comment author IDs for an IN clause.
                if ( ! empty( $this->query_vars['author__in'] ) ) {
                        $this->sql_clauses['where']['author__in'] = 'user_id IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['author__in'] ) ) . ' )';
@@ -801,17 +844,16 @@ class WP_Comment_Query {
                        }
                }
 
-               $date_query = $this->query_vars['date_query'];
-               if ( ! empty( $date_query ) && is_array( $date_query ) ) {
-                       $date_query_object = new WP_Date_Query( $date_query, 'comment_date' );
-                       $this->sql_clauses['where']['date_query'] = preg_replace( '/^\s*AND\s*/', '', $date_query_object->get_sql() );
+               if ( ! empty( $this->query_vars['date_query'] ) && is_array( $this->query_vars['date_query'] ) ) {
+                       $this->date_query = new WP_Date_Query( $this->query_vars['date_query'], 'comment_date' );
+                       $this->sql_clauses['where']['date_query'] = preg_replace( '/^\s*AND\s*/', '', $this->date_query->get_sql() );
                }
 
                $where = implode( ' AND ', $this->sql_clauses['where'] );
 
                $pieces = array( 'fields', 'join', 'where', 'orderby', 'limits', 'groupby' );
                /**
-                * Filter the comment query clauses.
+                * Filters the comment query clauses.
                 *
                 * @since 3.1.0
                 *
@@ -862,6 +904,33 @@ class WP_Comment_Query {
                }
        }
 
+       /**
+        * Populates found_comments and max_num_pages properties for the current
+        * query if the limit clause was used.
+        *
+        * @since 4.6.0
+        * @access private
+        *
+        * @global wpdb $wpdb WordPress database abstraction object.
+        */
+       private function set_found_comments() {
+               global $wpdb;
+
+               if ( $this->query_vars['number'] && ! $this->query_vars['no_found_rows'] ) {
+                       /**
+                        * Filters the query used to retrieve found comment count.
+                        *
+                        * @since 4.4.0
+                        *
+                        * @param string           $found_comments_query SQL query. Default 'SELECT FOUND_ROWS()'.
+                        * @param WP_Comment_Query $comment_query        The `WP_Comment_Query` instance.
+                        */
+                       $found_comments_query = apply_filters( 'found_comments_query', 'SELECT FOUND_ROWS()', $this );
+
+                       $this->found_comments = (int) $wpdb->get_var( $found_comments_query );
+               }
+       }
+
        /**
         * Fetch descendants for located comments.
         *
@@ -902,20 +971,49 @@ class WP_Comment_Query {
                        }
                }
 
+               $key = md5( serialize( wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ) ) );
+               $last_changed = wp_cache_get( 'last_changed', 'comment' );
+               if ( ! $last_changed ) {
+                       $last_changed = microtime();
+                       wp_cache_set( 'last_changed', $last_changed, 'comment' );
+               }
+
                // Fetch an entire level of the descendant tree at a time.
                $level = 0;
                do {
-                       $parent_ids = $levels[ $level ];
-                       if ( ! $parent_ids ) {
-                               break;
+                       // Parent-child relationships may be cached. Only query for those that are not.
+                       $child_ids = $uncached_parent_ids = array();
+                       $_parent_ids = $levels[ $level ];
+                       foreach ( $_parent_ids as $parent_id ) {
+                               $cache_key = "get_comment_child_ids:$parent_id:$key:$last_changed";
+                               $parent_child_ids = wp_cache_get( $cache_key, 'comment' );
+                               if ( false !== $parent_child_ids ) {
+                                       $child_ids = array_merge( $child_ids, $parent_child_ids );
+                               } else {
+                                       $uncached_parent_ids[] = $parent_id;
+                               }
                        }
 
-                       $where = 'WHERE ' . $_where . ' AND comment_parent IN (' . implode( ',', array_map( 'intval', $parent_ids ) ) . ')';
-                       $comment_ids = $wpdb->get_col( "{$this->sql_clauses['select']} {$this->sql_clauses['from']} {$where} {$this->sql_clauses['groupby']} ORDER BY comment_date_gmt ASC, comment_ID ASC" );
+                       if ( $uncached_parent_ids ) {
+                               $where = 'WHERE ' . $_where . ' AND comment_parent IN (' . implode( ',', array_map( 'intval', $uncached_parent_ids ) ) . ')';
+                               $level_comments = $wpdb->get_results( "SELECT $wpdb->comments.comment_ID, $wpdb->comments.comment_parent {$this->sql_clauses['from']} {$where} {$this->sql_clauses['groupby']} ORDER BY comment_date_gmt ASC, comment_ID ASC" );
+
+                               // Cache parent-child relationships.
+                               $parent_map = array_fill_keys( $uncached_parent_ids, array() );
+                               foreach ( $level_comments as $level_comment ) {
+                                       $parent_map[ $level_comment->comment_parent ][] = $level_comment->comment_ID;
+                                       $child_ids[] = $level_comment->comment_ID;
+                               }
+
+                               foreach ( $parent_map as $parent_id => $children ) {
+                                       $cache_key = "get_comment_child_ids:$parent_id:$key:$last_changed";
+                                       wp_cache_set( $cache_key, $children, 'comment' );
+                               }
+                       }
 
                        $level++;
-                       $levels[ $level ] = $comment_ids;
-               } while ( $comment_ids );
+                       $levels[ $level ] = $child_ids;
+               } while ( $child_ids );
 
                // Prime comment caches for non-top-level comments.
                $descendant_ids = array();