~canonical-sysadmins/wordpress/4.5.3

« back to all changes in this revision

Viewing changes to wp-includes/comment.php

  • Committer: Ryan Finnie
  • Date: 2015-08-31 16:09:47 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: ryan.finnie@canonical.com-20150831160947-1h6rfxby9z1ec62u
Merge WP4.3 from upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
161
161
 *
162
162
 * @since 2.0.0
163
163
 *
164
 
 * @global wpdb $wpdb WordPress database abstraction object.
 
164
 * @global wpdb   $wpdb WordPress database abstraction object.
 
165
 * @global object $comment
165
166
 *
166
167
 * @param object|string|int $comment Comment to retrieve.
167
168
 * @param string $output Optional. OBJECT or ARRAY_A or ARRAY_N constants.
297
298
         *
298
299
         * @param callable $name      Method to call.
299
300
         * @param array    $arguments Arguments to pass when calling.
300
 
         * @return mixed|bool Return value of the callback, false otherwise.
 
301
         * @return mixed|false Return value of the callback, false otherwise.
301
302
         */
302
303
        public function __call( $name, $arguments ) {
303
304
                if ( 'get_search_sql' === $name ) {
377
378
         *     @type array        $type__not_in        Exclude comments from a given array of comment types. Default empty.
378
379
         *     @type int          $user_id             Include comments for a specific user ID. Default empty.
379
380
         * }
380
 
         * @return WP_Comment_Query WP_Comment_Query instance.
381
381
         */
382
382
        public function __construct( $query = '' ) {
383
383
                $this->query_var_defaults = array(
454
454
         * @access public
455
455
         *
456
456
         * @param string|array $query Array or URL query string of parameters.
457
 
         * @return array List of comments.
 
457
         * @return array|int List of comments, or number of comments when 'count' is passed as a query var.
458
458
         */
459
459
        public function query( $query ) {
460
460
                $this->query_vars = wp_parse_args( $query );
469
469
         *
470
470
         * @global wpdb $wpdb WordPress database abstraction object.
471
471
         *
472
 
         * @return array The list of comments.
 
472
         * @return int|array The list of comments.
473
473
         */
474
474
        public function get_comments() {
475
475
                global $wpdb;
482
482
                $this->meta_query = new WP_Meta_Query();
483
483
                $this->meta_query->parse_query_vars( $this->query_vars );
484
484
 
485
 
                if ( ! empty( $this->meta_query->queries ) ) {
486
 
                        $meta_query_clauses = $this->meta_query->get_sql( 'comment', $wpdb->comments, 'comment_ID', $this );
487
 
                }
488
 
 
489
485
                /**
490
486
                 * Fires before comments are retrieved.
491
487
                 *
495
491
                 */
496
492
                do_action_ref_array( 'pre_get_comments', array( &$this ) );
497
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
 
498
500
                // $args can include anything. Only use the args defined in the query_var_defaults to compute the key.
499
501
                $key = md5( serialize( wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ) ) );
500
502
                $last_changed = wp_cache_get( 'last_changed', 'comment' );
696
698
 
697
699
                // Parse comment IDs for an IN clause.
698
700
                if ( ! empty( $this->query_vars['comment__in'] ) ) {
699
 
                        $where[] = 'comment_ID IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['comment__in'] ) ) . ' )';
 
701
                        $where[] = "$wpdb->comments.comment_ID IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['comment__in'] ) ) . ' )';
700
702
                }
701
703
 
702
704
                // Parse comment IDs for a NOT IN clause.
703
705
                if ( ! empty( $this->query_vars['comment__not_in'] ) ) {
704
 
                        $where[] = 'comment_ID NOT IN ( ' . implode( ',', wp_parse_id_list( $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'] ) ) . ' )';
705
707
                }
706
708
 
707
709
                // Parse comment post IDs for an IN clause.
893
895
                $comments = apply_filters_ref_array( 'the_comments', array( $results, &$this ) );
894
896
 
895
897
                wp_cache_add( $cache_key, $comments, 'comment' );
 
898
                if ( '*' === $fields ) {
 
899
                        update_comment_cache( $comments );
 
900
                }
896
901
 
897
902
                $this->comments = $comments;
898
903
                return $this->comments;
901
906
        /**
902
907
         * Used internally to generate an SQL string for searching across multiple columns
903
908
         *
 
909
         * @since 3.1.0
904
910
         * @access protected
905
 
         * @since 3.1.0
 
911
         *
 
912
         * @global wpdb $wpdb
906
913
         *
907
914
         * @param string $string
908
915
         * @param array $cols
930
937
         * @global wpdb $wpdb WordPress database abstraction object.
931
938
         *
932
939
         * @param string $orderby Alias for the field to order by.
933
 
         * @return string|bool Value to used in the ORDER clause. False otherwise.
 
940
         * @return string|false Value to used in the ORDER clause. False otherwise.
934
941
         */
935
942
        protected function parse_orderby( $orderby ) {
936
943
                global $wpdb;
967
974
                $parsed = false;
968
975
                if ( $orderby == $this->query_vars['meta_key'] || $orderby == 'meta_value' ) {
969
976
                        $parsed = "$wpdb->commentmeta.meta_value";
970
 
                } else if ( $orderby == 'meta_value_num' ) {
 
977
                } elseif ( $orderby == 'meta_value_num' ) {
971
978
                        $parsed = "$wpdb->commentmeta.meta_value+0";
972
 
                } else if ( in_array( $orderby, $allowed_keys ) ) {
 
979
                } elseif ( in_array( $orderby, $allowed_keys ) ) {
973
980
 
974
981
                        if ( isset( $meta_query_clauses[ $orderby ] ) ) {
975
982
                                $meta_clause = $meta_query_clauses[ $orderby ];
1027
1034
}
1028
1035
 
1029
1036
/**
 
1037
 * Gets the default comment status for a post type.
 
1038
 *
 
1039
 * @since 4.3.0
 
1040
 *
 
1041
 * @param string $post_type    Optional. Post type. Default 'post'.
 
1042
 * @param string $comment_type Optional. Comment type. Default 'comment'.
 
1043
 * @return string Expected return value is 'open' or 'closed'.
 
1044
 */
 
1045
function get_default_comment_status( $post_type = 'post', $comment_type = 'comment' ) {
 
1046
        switch ( $comment_type ) {
 
1047
                case 'pingback' :
 
1048
                case 'trackback' :
 
1049
                        $supports = 'trackbacks';
 
1050
                        $option = 'ping';
 
1051
                        break;
 
1052
                default :
 
1053
                        $supports = 'comments';
 
1054
                        $option = 'comment';
 
1055
        }
 
1056
 
 
1057
        // Set the status.
 
1058
        if ( 'page' === $post_type ) {
 
1059
                $status = 'closed';
 
1060
        } elseif ( post_type_supports( $post_type, $supports ) ) {
 
1061
                $status = get_option( "default_{$option}_status" );
 
1062
        } else {
 
1063
                $status = 'closed';
 
1064
        }
 
1065
 
 
1066
        /**
 
1067
         * Filter the default comment status for the given post type.
 
1068
         *
 
1069
         * @since 4.3.0
 
1070
         *
 
1071
         * @param string $status       Default status for the given post type,
 
1072
         *                             either 'open' or 'closed'.
 
1073
         * @param string $post_type    Post type. Default is `post`.
 
1074
         * @param string $comment_type Type of comment. Default is `comment`.
 
1075
         */
 
1076
        return apply_filters( 'get_default_comment_status' , $status, $post_type, $comment_type );
 
1077
}
 
1078
 
 
1079
/**
1030
1080
 * The date the last comment was modified.
1031
1081
 *
1032
1082
 * @since 1.5.0
1033
1083
 *
1034
1084
 * @global wpdb $wpdb WordPress database abstraction object.
 
1085
 * @staticvar array $cache_lastcommentmodified
1035
1086
 *
1036
1087
 * @param string $timezone Which timezone to use in reference to 'gmt', 'blog',
1037
1088
 *              or 'server' locations.
1294
1345
 * @global wpdb $wpdb WordPress database abstraction object.
1295
1346
 *
1296
1347
 * @param array $commentdata Contains information on the comment
1297
 
 * @return mixed Signifies the approval status (0|1|'spam')
 
1348
 * @return int|string Signifies the approval status (0|1|'spam')
1298
1349
 */
1299
1350
function wp_allow_comment( $commentdata ) {
1300
1351
        global $wpdb;
1482
1533
 *
1483
1534
 * @uses Walker_Comment
1484
1535
 *
 
1536
 * @global WP_Query $wp_query
 
1537
 *
1485
1538
 * @param array $comments Optional array of comment objects. Defaults to $wp_query->comments
1486
 
 * @param int $per_page Optional comments per page.
1487
 
 * @param boolean $threaded Optional control over flat or threaded comments.
 
1539
 * @param int   $per_page Optional comments per page.
 
1540
 * @param bool  $threaded Optional control over flat or threaded comments.
1488
1541
 * @return int Number of comment pages.
1489
1542
 */
1490
1543
function get_comment_pages_count( $comments = null, $per_page = null, $threaded = null ) {
1527
1580
 *
1528
1581
 * @since 2.7.0
1529
1582
 *
 
1583
 * @global wpdb $wpdb
 
1584
 *
1530
1585
 * @param int $comment_ID Comment ID.
1531
1586
 * @param array $args Optional args.
1532
1587
 * @return int|null Comment page number or null on error.
1649
1704
 *
1650
1705
 * @since 2.5.0
1651
1706
 *
 
1707
 * @global wpdb $wpdb
 
1708
 *
1652
1709
 * @param int $post_id Optional. Post ID.
1653
 
 * @return object Comment stats.
 
1710
 * @return object|array Comment stats.
1654
1711
 */
1655
1712
function wp_count_comments( $post_id = 0 ) {
1656
1713
        global $wpdb;
2082
2139
}
2083
2140
 
2084
2141
/**
2085
 
 * Inserts a comment to the database.
2086
 
 *
2087
 
 * The available comment data key names are 'comment_author_IP', 'comment_date',
2088
 
 * 'comment_date_gmt', 'comment_parent', 'comment_approved', and 'user_id'.
 
2142
 * Inserts a comment into the database.
2089
2143
 *
2090
2144
 * @since 2.0.0
2091
2145
 *
2092
2146
 * @global wpdb $wpdb WordPress database abstraction object.
2093
2147
 *
2094
 
 * @param array $commentdata Contains information on the comment.
2095
 
 * @return int|bool The new comment's ID on success, false on failure.
 
2148
 * @param array $commentdata {
 
2149
 *     Array of arguments for inserting a new comment.
 
2150
 *
 
2151
 *     @type string     $comment_agent        The HTTP user agent of the `$comment_author` when
 
2152
 *                                            the comment was submitted. Default empty.
 
2153
 *     @type int|string $comment_approved     Whether the comment has been approved. Default 1.
 
2154
 *     @type string     $comment_author       The name of the author of the comment. Default empty.
 
2155
 *     @type string     $comment_author_email The email address of the `$comment_author`. Default empty.
 
2156
 *     @type string     $comment_author_IP    The IP address of the `$comment_author`. Default empty.
 
2157
 *     @type string     $comment_author_url   The URL address of the `$comment_author`. Default empty.
 
2158
 *     @type string     $comment_content      The content of the comment. Default empty.
 
2159
 *     @type string     $comment_date         The date the comment was submitted. To set the date
 
2160
 *                                            manually, `$comment_date_gmt` must also be specified.
 
2161
 *                                            Default is the current time.
 
2162
 *     @type string     $comment_date_gmt     The date the comment was submitted in the GMT timezone.
 
2163
 *                                            Default is `$comment_date` in the site's GMT timezone.
 
2164
 *     @type int        $comment_karma        The karma of the comment. Default 0.
 
2165
 *     @type int        $comment_parent       ID of this comment's parent, if any. Default 0.
 
2166
 *     @type int        $comment_post_ID      ID of the post that relates to the comment, if any.
 
2167
 *                                            Default empty.
 
2168
 *     @type string     $comment_type         Comment type. Default empty.
 
2169
 *     @type int        $user_id              ID of the user who submitted the comment. Default 0.
 
2170
 * }
 
2171
 * @return int|false The new comment's ID on success, false on failure.
2096
2172
 */
2097
2173
function wp_insert_comment( $commentdata ) {
2098
2174
        global $wpdb;
2238
2314
 * See {@link https://core.trac.wordpress.org/ticket/9235}
2239
2315
 *
2240
2316
 * @since 1.5.0
2241
 
 * @param array $commentdata Contains information on the comment.
2242
 
 * @return int|bool The ID of the comment on success, false on failure.
 
2317
 * @since 4.3.0 'comment_agent' and 'comment_author_IP' can be set via `$commentdata`.
 
2318
 *
 
2319
 * @see wp_insert_comment()
 
2320
 *
 
2321
 * @global wpdb $wpdb
 
2322
 *
 
2323
 * @param array $commentdata Contains information on the comment. See wp_insert_comment()
 
2324
 *                           for information on accepted arguments.
 
2325
 * @return int|false The ID of the comment on success, false on failure.
2243
2326
 */
2244
2327
function wp_new_comment( $commentdata ) {
2245
2328
        global $wpdb;
2270
2353
        $parent_status = ( 0 < $commentdata['comment_parent'] ) ? wp_get_comment_status($commentdata['comment_parent']) : '';
2271
2354
        $commentdata['comment_parent'] = ( 'approved' == $parent_status || 'unapproved' == $parent_status ) ? $commentdata['comment_parent'] : 0;
2272
2355
 
2273
 
        $commentdata['comment_author_IP'] = preg_replace( '/[^0-9a-fA-F:., ]/', '',$_SERVER['REMOTE_ADDR'] );
2274
 
        $commentdata['comment_agent']     = isset( $_SERVER['HTTP_USER_AGENT'] ) ? substr( $_SERVER['HTTP_USER_AGENT'], 0, 254 ) : '';
 
2356
        if ( ! isset( $commentdata['comment_author_IP'] ) ) {
 
2357
                $commentdata['comment_author_IP'] = $_SERVER['REMOTE_ADDR'];
 
2358
        }
 
2359
        $commentdata['comment_author_IP'] = preg_replace( '/[^0-9a-fA-F:., ]/', '', $commentdata['comment_author_IP'] );
 
2360
 
 
2361
        if ( ! isset( $commentdata['comment_agent'] ) ) {
 
2362
                $commentdata['comment_agent'] = isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT']: '';
 
2363
        }
 
2364
        $commentdata['comment_agent'] = substr( $commentdata['comment_agent'], 0, 254 );
2275
2365
 
2276
2366
        if ( empty( $commentdata['comment_date'] ) ) {
2277
2367
                $commentdata['comment_date'] = current_time('mysql');
2338
2428
 *
2339
2429
 * @since 1.0.0
2340
2430
 *
 
2431
 * global wpdb $wpdb
 
2432
 *
2341
2433
 * @param int $comment_id Comment ID.
2342
2434
 * @param string $comment_status New comment status, either 'hold', 'approve', 'spam', or 'trash'.
2343
2435
 * @param bool $wp_error Whether to return a WP_Error object if there is a failure. Default is false.
2522
2614
 * @since 2.1.0
2523
2615
 * @see wp_update_comment_count_now() For what could cause a false return value
2524
2616
 *
 
2617
 * @staticvar array $_deferred
 
2618
 *
2525
2619
 * @param int $post_id Post ID
2526
2620
 * @param bool $do_deferred Whether to process previously deferred post comment counts
2527
 
 * @return bool|null True on success, false on failure
 
2621
 * @return bool|void True on success, false on failure
2528
2622
 */
2529
2623
function wp_update_comment_count($post_id, $do_deferred=false) {
2530
2624
        static $_deferred = array();
2767
2861
 * Pings back the links found in a post.
2768
2862
 *
2769
2863
 * @since 0.71
2770
 
 * @uses $wp_version
 
2864
 *
 
2865
 * @global string $wp_version
2771
2866
 *
2772
2867
 * @param string $content Post content to check for links.
2773
2868
 * @param int $post_ID Post ID.
2880
2975
 * @param string $title Title of post.
2881
2976
 * @param string $excerpt Excerpt of post.
2882
2977
 * @param int $ID Post ID.
2883
 
 * @return mixed Database query from update.
 
2978
 * @return int|false|void Database query from update.
2884
2979
 */
2885
2980
function trackback($trackback_url, $title, $excerpt, $ID) {
2886
2981
        global $wpdb;
2910
3005
 * Send a pingback.
2911
3006
 *
2912
3007
 * @since 1.2.0
2913
 
 * @uses $wp_version
 
3008
 *
 
3009
 * @global string $wp_version
2914
3010
 *
2915
3011
 * @param string $server Host of blog to connect to.
2916
3012
 * @param string $path Path to send the ping.
3007
3103
 * @access private
3008
3104
 * @since 2.7.0
3009
3105
 *
3010
 
 * @param object $posts Post data object.
3011
 
 * @param object $query Query object.
3012
 
 * @return object
 
3106
 * @param WP_Post  $posts Post data object.
 
3107
 * @param WP_Query $query Query object.
 
3108
 * @return array
3013
3109
 */
3014
3110
function _close_comments_for_old_posts( $posts, $query ) {
3015
3111
        if ( empty( $posts ) || ! $query->is_singular() || ! get_option( 'close_comments_for_old_posts' ) )