~canonical-sysadmins/wordpress/4.7.2

« back to all changes in this revision

Viewing changes to wp-includes/class-wp-comment-query.php

  • Committer: Barry Price
  • Date: 2016-08-17 04:50:12 UTC
  • mfrom: (1.1.18 upstream)
  • Revision ID: barry.price@canonical.com-20160817045012-qfui81zhqnqv2ba9
Merge WP4.6 from upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
62
62
        /**
63
63
         * SQL WHERE clause.
64
64
         *
65
 
         * Stored after the 'comments_clauses' filter is run on the compiled WHERE sub-clauses.
 
65
         * Stored after the {@see 'comments_clauses'} filter is run on the compiled WHERE sub-clauses.
66
66
         *
67
67
         * @since 4.4.2
68
68
         * @access protected
125
125
        public $max_num_pages = 0;
126
126
 
127
127
        /**
128
 
         * Make private/protected methods readable for backwards compatibility.
 
128
         * Make private/protected methods readable for backward compatibility.
129
129
         *
130
130
         * @since 4.0.0
131
131
         * @access public
151
151
         * @since 4.4.0 Order by `comment__in` was added. `$update_comment_meta_cache`, `$no_found_rows`,
152
152
         *              `$hierarchical`, and `$update_comment_post_cache` were added.
153
153
         * @since 4.5.0 Introduced the `$author_url` argument.
 
154
         * @since 4.6.0 Introduced the `$cache_domain` argument.
154
155
         * @access public
155
156
         *
156
157
         * @param string|array $query {
250
251
         *                                                   The parameter is ignored (forced to `false`) when
251
252
         *                                                   `$fields` is 'ids' or 'counts'. Accepts 'threaded',
252
253
         *                                                   'flat', or false. Default: false.
 
254
         *     @type string       $cache_domain              Unique cache key to be produced when this query is stored in
 
255
         *                                                   an object cache. Default is 'core'.
253
256
         *     @type bool         $update_comment_meta_cache Whether to prime the metadata cache for found comments.
254
257
         *                                                   Default true.
255
258
         *     @type bool         $update_comment_post_cache Whether to prime the cache for comment posts.
299
302
                        'meta_query' => '',
300
303
                        'date_query' => null, // See WP_Date_Query
301
304
                        'hierarchical' => false,
 
305
                        'cache_domain' => 'core',
302
306
                        'update_comment_meta_cache' => true,
303
307
                        'update_comment_post_cache' => false,
304
308
                );
311
315
        /**
312
316
         * Parse arguments passed to the comment query with default query parameters.
313
317
         *
314
 
         * @since  4.2.0 Extracted from WP_Comment_Query::query().
 
318
         * @since 4.2.0 Extracted from WP_Comment_Query::query().
315
319
         *
316
320
         * @access public
317
321
         *
323
327
                }
324
328
 
325
329
                $this->query_vars = wp_parse_args( $query, $this->query_var_defaults );
 
330
 
 
331
                /**
 
332
                 * Fires after the comment query vars have been parsed.
 
333
                 *
 
334
                 * @since 4.2.0
 
335
                 *
 
336
                 * @param WP_Comment_Query &$this The WP_Comment_Query instance (passed by reference).
 
337
                 */
326
338
                do_action_ref_array( 'parse_comment_query', array( &$this ) );
327
339
        }
328
340
 
353
365
         *
354
366
         * @global wpdb $wpdb WordPress database abstraction object.
355
367
         *
356
 
         * @return int|array The list of comments.
 
368
         * @return int|array List of comments or number of found comments if `$count` argument is true.
357
369
         */
358
370
        public function get_comments() {
359
371
                global $wpdb;
386
398
                        $last_changed = microtime();
387
399
                        wp_cache_set( 'last_changed', $last_changed, 'comment' );
388
400
                }
389
 
                $cache_key = "get_comment_ids:$key:$last_changed";
390
401
 
391
 
                $comment_ids = wp_cache_get( $cache_key, 'comment' );
392
 
                if ( false === $comment_ids ) {
 
402
                $cache_key   = "get_comments:$key:$last_changed";
 
403
                $cache_value = wp_cache_get( $cache_key, 'comment' );
 
404
                if ( false === $cache_value ) {
393
405
                        $comment_ids = $this->get_comment_ids();
394
 
                        wp_cache_add( $cache_key, $comment_ids, 'comment' );
 
406
                        if ( $comment_ids ) {
 
407
                                $this->set_found_comments();
 
408
                        }
 
409
 
 
410
                        $cache_value = array(
 
411
                                'comment_ids'    => $comment_ids,
 
412
                                'found_comments' => $this->found_comments,
 
413
                        );
 
414
                        wp_cache_add( $cache_key, $cache_value, 'comment' );
 
415
                } else {
 
416
                        $comment_ids          = $cache_value['comment_ids'];
 
417
                        $this->found_comments = $cache_value['found_comments'];
 
418
                }
 
419
 
 
420
                if ( $this->found_comments && $this->query_vars['number'] ) {
 
421
                        $this->max_num_pages = ceil( $this->found_comments / $this->query_vars['number'] );
395
422
                }
396
423
 
397
424
                // If querying for a count only, there's nothing more to do.
402
429
 
403
430
                $comment_ids = array_map( 'intval', $comment_ids );
404
431
 
405
 
                $this->comment_count = count( $this->comments );
406
 
 
407
 
                if ( $comment_ids && $this->query_vars['number'] && ! $this->query_vars['no_found_rows'] ) {
408
 
                        /**
409
 
                         * Filter the query used to retrieve found comment count.
410
 
                         *
411
 
                         * @since 4.4.0
412
 
                         *
413
 
                         * @param string           $found_comments_query SQL query. Default 'SELECT FOUND_ROWS()'.
414
 
                         * @param WP_Comment_Query $comment_query        The `WP_Comment_Query` instance.
415
 
                         */
416
 
                        $found_comments_query = apply_filters( 'found_comments_query', 'SELECT FOUND_ROWS()', $this );
417
 
                        $this->found_comments = (int) $wpdb->get_var( $found_comments_query );
418
 
 
419
 
                        $this->max_num_pages = ceil( $this->found_comments / $this->query_vars['number'] );
420
 
                }
421
 
 
422
432
                if ( 'ids' == $this->query_vars['fields'] ) {
423
433
                        $this->comments = $comment_ids;
424
434
                        return $this->comments;
445
455
                }
446
456
 
447
457
                /**
448
 
                 * Filter the comment query results.
 
458
                 * Filters the comment query results.
449
459
                 *
450
460
                 * @since 3.1.0
451
461
                 *
734
744
                        }
735
745
                }
736
746
 
737
 
                if ( $this->query_vars['hierarchical'] && ! $this->query_vars['parent'] ) {
738
 
                        $this->query_vars['parent'] = 0;
 
747
                $parent = $this->query_vars['parent'];
 
748
                if ( $this->query_vars['hierarchical'] && ! $parent ) {
 
749
                        $parent = 0;
739
750
                }
740
751
 
741
 
                if ( '' !== $this->query_vars['parent'] ) {
742
 
                        $this->sql_clauses['where']['parent'] = $wpdb->prepare( 'comment_parent = %d', $this->query_vars['parent'] );
 
752
                if ( '' !== $parent ) {
 
753
                        $this->sql_clauses['where']['parent'] = $wpdb->prepare( 'comment_parent = %d', $parent );
743
754
                }
744
755
 
745
756
                if ( is_array( $this->query_vars['user_id'] ) ) {
833
844
                        }
834
845
                }
835
846
 
836
 
                $date_query = $this->query_vars['date_query'];
837
 
                if ( ! empty( $date_query ) && is_array( $date_query ) ) {
838
 
                        $date_query_object = new WP_Date_Query( $date_query, 'comment_date' );
839
 
                        $this->sql_clauses['where']['date_query'] = preg_replace( '/^\s*AND\s*/', '', $date_query_object->get_sql() );
 
847
                if ( ! empty( $this->query_vars['date_query'] ) && is_array( $this->query_vars['date_query'] ) ) {
 
848
                        $this->date_query = new WP_Date_Query( $this->query_vars['date_query'], 'comment_date' );
 
849
                        $this->sql_clauses['where']['date_query'] = preg_replace( '/^\s*AND\s*/', '', $this->date_query->get_sql() );
840
850
                }
841
851
 
842
852
                $where = implode( ' AND ', $this->sql_clauses['where'] );
843
853
 
844
854
                $pieces = array( 'fields', 'join', 'where', 'orderby', 'limits', 'groupby' );
845
855
                /**
846
 
                 * Filter the comment query clauses.
 
856
                 * Filters the comment query clauses.
847
857
                 *
848
858
                 * @since 3.1.0
849
859
                 *
895
905
        }
896
906
 
897
907
        /**
 
908
         * Populates found_comments and max_num_pages properties for the current
 
909
         * query if the limit clause was used.
 
910
         *
 
911
         * @since 4.6.0
 
912
         * @access private
 
913
         *
 
914
         * @global wpdb $wpdb WordPress database abstraction object.
 
915
         */
 
916
        private function set_found_comments() {
 
917
                global $wpdb;
 
918
 
 
919
                if ( $this->query_vars['number'] && ! $this->query_vars['no_found_rows'] ) {
 
920
                        /**
 
921
                         * Filters the query used to retrieve found comment count.
 
922
                         *
 
923
                         * @since 4.4.0
 
924
                         *
 
925
                         * @param string           $found_comments_query SQL query. Default 'SELECT FOUND_ROWS()'.
 
926
                         * @param WP_Comment_Query $comment_query        The `WP_Comment_Query` instance.
 
927
                         */
 
928
                        $found_comments_query = apply_filters( 'found_comments_query', 'SELECT FOUND_ROWS()', $this );
 
929
 
 
930
                        $this->found_comments = (int) $wpdb->get_var( $found_comments_query );
 
931
                }
 
932
        }
 
933
 
 
934
        /**
898
935
         * Fetch descendants for located comments.
899
936
         *
900
937
         * Instead of calling `get_children()` separately on each child comment, we do a single set of queries to fetch
934
971
                        }
935
972
                }
936
973
 
 
974
                $key = md5( serialize( wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ) ) );
 
975
                $last_changed = wp_cache_get( 'last_changed', 'comment' );
 
976
                if ( ! $last_changed ) {
 
977
                        $last_changed = microtime();
 
978
                        wp_cache_set( 'last_changed', $last_changed, 'comment' );
 
979
                }
 
980
 
937
981
                // Fetch an entire level of the descendant tree at a time.
938
982
                $level = 0;
939
983
                do {
940
 
                        $parent_ids = $levels[ $level ];
941
 
                        if ( ! $parent_ids ) {
942
 
                                break;
943
 
                        }
944
 
 
945
 
                        $where = 'WHERE ' . $_where . ' AND comment_parent IN (' . implode( ',', array_map( 'intval', $parent_ids ) ) . ')';
946
 
                        $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" );
 
984
                        // Parent-child relationships may be cached. Only query for those that are not.
 
985
                        $child_ids = $uncached_parent_ids = array();
 
986
                        $_parent_ids = $levels[ $level ];
 
987
                        foreach ( $_parent_ids as $parent_id ) {
 
988
                                $cache_key = "get_comment_child_ids:$parent_id:$key:$last_changed";
 
989
                                $parent_child_ids = wp_cache_get( $cache_key, 'comment' );
 
990
                                if ( false !== $parent_child_ids ) {
 
991
                                        $child_ids = array_merge( $child_ids, $parent_child_ids );
 
992
                                } else {
 
993
                                        $uncached_parent_ids[] = $parent_id;
 
994
                                }
 
995
                        }
 
996
 
 
997
                        if ( $uncached_parent_ids ) {
 
998
                                $where = 'WHERE ' . $_where . ' AND comment_parent IN (' . implode( ',', array_map( 'intval', $uncached_parent_ids ) ) . ')';
 
999
                                $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" );
 
1000
 
 
1001
                                // Cache parent-child relationships.
 
1002
                                $parent_map = array_fill_keys( $uncached_parent_ids, array() );
 
1003
                                foreach ( $level_comments as $level_comment ) {
 
1004
                                        $parent_map[ $level_comment->comment_parent ][] = $level_comment->comment_ID;
 
1005
                                        $child_ids[] = $level_comment->comment_ID;
 
1006
                                }
 
1007
 
 
1008
                                foreach ( $parent_map as $parent_id => $children ) {
 
1009
                                        $cache_key = "get_comment_child_ids:$parent_id:$key:$last_changed";
 
1010
                                        wp_cache_set( $cache_key, $children, 'comment' );
 
1011
                                }
 
1012
                        }
947
1013
 
948
1014
                        $level++;
949
 
                        $levels[ $level ] = $comment_ids;
950
 
                } while ( $comment_ids );
 
1015
                        $levels[ $level ] = $child_ids;
 
1016
                } while ( $child_ids );
951
1017
 
952
1018
                // Prime comment caches for non-top-level comments.
953
1019
                $descendant_ids = array();