~canonical-sysadmins/wordpress/3.9.x

« back to all changes in this revision

Viewing changes to wp-includes/comment.php

  • Committer: Andrew Glen-Young
  • Date: 2011-03-08 14:47:51 UTC
  • Revision ID: andrew.glen-young@canonical.com-20110308144751-1n6spqgayztf9h77
[AGY] import 3.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 * check fails. If any of the parameter contents match the blacklist of words,
19
19
 * then the check fails.
20
20
 *
21
 
 * If the comment is a trackback and part of the blogroll, then the trackback is
22
 
 * automatically whitelisted. If the comment author was approved before, then
23
 
 * the comment is automatically whitelisted.
 
21
 * If the comment author was approved before, then the comment is
 
22
 * automatically whitelisted.
24
23
 *
25
24
 * If none of the checks fail, then the failback is to set the check to pass
26
25
 * (return true).
45
44
        if ( 1 == get_option('comment_moderation') )
46
45
                return false; // If moderation is set to manual
47
46
 
 
47
        $comment = apply_filters( 'comment_text', $comment );
 
48
 
48
49
        // Check # of external links
49
50
        if ( $max_links = get_option( 'comment_max_links' ) ) {
50
 
                $num_links = preg_match_all( '/<a [^>]*href/i', apply_filters( 'comment_text', $comment ), $out );
 
51
                $num_links = preg_match_all( '/<a [^>]*href/i', $comment, $out );
51
52
                $num_links = apply_filters( 'comment_max_links_url', $num_links, $url ); // provide for counting of $url as a link
52
53
                if ( $num_links >= $max_links )
53
54
                        return false;
80
81
 
81
82
        // Comment whitelisting:
82
83
        if ( 1 == get_option('comment_whitelist')) {
83
 
                if ( 'trackback' == $comment_type || 'pingback' == $comment_type ) { // check if domain is in blogroll
84
 
                        $uri = parse_url($url);
85
 
                        $domain = $uri['host'];
86
 
                        $uri = parse_url( home_url() );
87
 
                        $home_domain = $uri['host'];
88
 
                        if ( $wpdb->get_var($wpdb->prepare("SELECT link_id FROM $wpdb->links WHERE link_url LIKE (%s) LIMIT 1", '%'.$domain.'%')) || $domain == $home_domain )
89
 
                                return true;
90
 
                        else
91
 
                                return false;
92
 
                } elseif ( $author != '' && $email != '' ) {
 
84
                if ( 'trackback' != $comment_type && 'pingback' != $comment_type && $author != '' && $email != '' ) {
93
85
                        // expected_slashed ($author, $email)
94
86
                        $ok_to_comment = $wpdb->get_var("SELECT comment_approved FROM $wpdb->comments WHERE comment_author = '$author' AND comment_author_email = '$email' and comment_approved = '1' LIMIT 1");
95
87
                        if ( ( 1 == $ok_to_comment ) &&
188
180
 * @return array List of comments.
189
181
 */
190
182
function get_comments( $args = '' ) {
191
 
        global $wpdb;
192
 
 
193
 
        $defaults = array(
194
 
                'author_email' => '',
195
 
                'ID' => '',
196
 
                'karma' => '',
197
 
                'number' => '',
198
 
                'offset' => '',
199
 
                'orderby' => '',
200
 
                'order' => 'DESC',
201
 
                'parent' => '',
202
 
                'post_ID' => '',
203
 
                'post_id' => 0,
204
 
                'status' => '',
205
 
                'type' => '',
206
 
                'user_id' => '',
207
 
        );
208
 
 
209
 
        $args = wp_parse_args( $args, $defaults );
210
 
        extract( $args, EXTR_SKIP );
211
 
 
212
 
        // $args can be whatever, only use the args defined in defaults to compute the key
213
 
        $key = md5( serialize( compact(array_keys($defaults)) )  );
214
 
        $last_changed = wp_cache_get('last_changed', 'comment');
215
 
        if ( !$last_changed ) {
216
 
                $last_changed = time();
217
 
                wp_cache_set('last_changed', $last_changed, 'comment');
218
 
        }
219
 
        $cache_key = "get_comments:$key:$last_changed";
220
 
 
221
 
        if ( $cache = wp_cache_get( $cache_key, 'comment' ) ) {
222
 
                return $cache;
223
 
        }
224
 
 
225
 
        $post_id = absint($post_id);
226
 
 
227
 
        if ( 'hold' == $status )
228
 
                $approved = "comment_approved = '0'";
229
 
        elseif ( 'approve' == $status )
230
 
                $approved = "comment_approved = '1'";
231
 
        elseif ( 'spam' == $status )
232
 
                $approved = "comment_approved = 'spam'";
233
 
        elseif ( 'trash' == $status )
234
 
                $approved = "comment_approved = 'trash'";
235
 
        else
236
 
                $approved = "( comment_approved = '0' OR comment_approved = '1' )";
237
 
 
238
 
        $order = ( 'ASC' == $order ) ? 'ASC' : 'DESC';
239
 
 
240
 
        if ( ! empty( $orderby ) ) {
241
 
                $ordersby = is_array($orderby) ? $orderby : preg_split('/[,\s]/', $orderby);
242
 
                $ordersby = array_intersect(
243
 
                        $ordersby,
244
 
                        array(
245
 
                                'comment_agent',
246
 
                                'comment_approved',
247
 
                                'comment_author',
248
 
                                'comment_author_email',
249
 
                                'comment_author_IP',
250
 
                                'comment_author_url',
251
 
                                'comment_content',
252
 
                                'comment_date',
253
 
                                'comment_date_gmt',
254
 
                                'comment_ID',
255
 
                                'comment_karma',
256
 
                                'comment_parent',
257
 
                                'comment_post_ID',
258
 
                                'comment_type',
259
 
                                'user_id',
260
 
                        )
 
183
        $query = new WP_Comment_Query;
 
184
        return $query->query( $args );
 
185
}
 
186
 
 
187
/**
 
188
 * WordPress Comment Query class.
 
189
 *
 
190
 * @since 3.1.0
 
191
 */
 
192
class WP_Comment_Query {
 
193
 
 
194
        /**
 
195
         * Execute the query
 
196
         *
 
197
         * @since 3.1.0
 
198
         *
 
199
         * @param string|array $query_vars
 
200
         * @return int|array
 
201
         */
 
202
        function query( $query_vars ) {
 
203
                global $wpdb;
 
204
 
 
205
                $defaults = array(
 
206
                        'author_email' => '',
 
207
                        'ID' => '',
 
208
                        'karma' => '',
 
209
                        'number' => '',
 
210
                        'offset' => '',
 
211
                        'orderby' => '',
 
212
                        'order' => 'DESC',
 
213
                        'parent' => '',
 
214
                        'post_ID' => '',
 
215
                        'post_id' => 0,
 
216
                        'status' => '',
 
217
                        'type' => '',
 
218
                        'user_id' => '',
 
219
                        'search' => '',
 
220
                        'count' => false
261
221
                );
262
 
                $orderby = empty( $ordersby ) ? 'comment_date_gmt' : implode(', ', $ordersby);
263
 
        } else {
264
 
                $orderby = 'comment_date_gmt';
265
 
        }
266
 
 
267
 
        $number = absint($number);
268
 
        $offset = absint($offset);
269
 
 
270
 
        if ( !empty($number) ) {
271
 
                if ( $offset )
272
 
                        $number = 'LIMIT ' . $offset . ',' . $number;
273
 
                else
274
 
                        $number = 'LIMIT ' . $number;
275
 
 
276
 
        } else {
277
 
                $number = '';
278
 
        }
279
 
 
280
 
        $post_where = '';
281
 
 
282
 
        if ( ! empty($post_id) )
283
 
                $post_where .= $wpdb->prepare( 'comment_post_ID = %d AND ', $post_id );
284
 
        if ( '' !== $author_email )
285
 
                $post_where .= $wpdb->prepare( 'comment_author_email = %s AND ', $author_email );
286
 
        if ( '' !== $karma )
287
 
                $post_where .= $wpdb->prepare( 'comment_karma = %d AND ', $karma );
288
 
        if ( 'comment' == $type )
289
 
                $post_where .= "comment_type = '' AND ";
290
 
        elseif ( ! empty( $type ) )
291
 
                $post_where .= $wpdb->prepare( 'comment_type = %s AND ', $type );
292
 
        if ( '' !== $parent )
293
 
                $post_where .= $wpdb->prepare( 'comment_parent = %d AND ', $parent );
294
 
        if ( '' !== $user_id )
295
 
                $post_where .= $wpdb->prepare( 'user_id = %d AND ', $user_id );
296
 
 
297
 
        $comments = $wpdb->get_results( "SELECT * FROM $wpdb->comments WHERE $post_where $approved ORDER BY $orderby $order $number" );
298
 
        wp_cache_add( $cache_key, $comments, 'comment' );
299
 
 
300
 
        return $comments;
 
222
 
 
223
                $this->query_vars = wp_parse_args( $query_vars, $defaults );
 
224
                do_action_ref_array( 'pre_get_comments', array( &$this ) );
 
225
                extract( $this->query_vars, EXTR_SKIP );
 
226
 
 
227
                // $args can be whatever, only use the args defined in defaults to compute the key
 
228
                $key = md5( serialize( compact(array_keys($defaults)) )  );
 
229
                $last_changed = wp_cache_get('last_changed', 'comment');
 
230
                if ( !$last_changed ) {
 
231
                        $last_changed = time();
 
232
                        wp_cache_set('last_changed', $last_changed, 'comment');
 
233
                }
 
234
                $cache_key = "get_comments:$key:$last_changed";
 
235
 
 
236
                if ( $cache = wp_cache_get( $cache_key, 'comment' ) ) {
 
237
                        return $cache;
 
238
                }
 
239
 
 
240
                $post_id = absint($post_id);
 
241
 
 
242
                if ( 'hold' == $status )
 
243
                        $approved = "comment_approved = '0'";
 
244
                elseif ( 'approve' == $status )
 
245
                        $approved = "comment_approved = '1'";
 
246
                elseif ( 'spam' == $status )
 
247
                        $approved = "comment_approved = 'spam'";
 
248
                elseif ( 'trash' == $status )
 
249
                        $approved = "comment_approved = 'trash'";
 
250
                else
 
251
                        $approved = "( comment_approved = '0' OR comment_approved = '1' )";
 
252
 
 
253
                $order = ( 'ASC' == strtoupper($order) ) ? 'ASC' : 'DESC';
 
254
 
 
255
                if ( ! empty( $orderby ) ) {
 
256
                        $ordersby = is_array($orderby) ? $orderby : preg_split('/[,\s]/', $orderby);
 
257
                        $ordersby = array_intersect(
 
258
                                $ordersby,
 
259
                                array(
 
260
                                        'comment_agent',
 
261
                                        'comment_approved',
 
262
                                        'comment_author',
 
263
                                        'comment_author_email',
 
264
                                        'comment_author_IP',
 
265
                                        'comment_author_url',
 
266
                                        'comment_content',
 
267
                                        'comment_date',
 
268
                                        'comment_date_gmt',
 
269
                                        'comment_ID',
 
270
                                        'comment_karma',
 
271
                                        'comment_parent',
 
272
                                        'comment_post_ID',
 
273
                                        'comment_type',
 
274
                                        'user_id',
 
275
                                )
 
276
                        );
 
277
                        $orderby = empty( $ordersby ) ? 'comment_date_gmt' : implode(', ', $ordersby);
 
278
                } else {
 
279
                        $orderby = 'comment_date_gmt';
 
280
                }
 
281
 
 
282
                $number = absint($number);
 
283
                $offset = absint($offset);
 
284
 
 
285
                if ( !empty($number) ) {
 
286
                        if ( $offset )
 
287
                                $limits = 'LIMIT ' . $offset . ',' . $number;
 
288
                        else
 
289
                                $limits = 'LIMIT ' . $number;
 
290
                } else {
 
291
                        $limits = '';
 
292
                }
 
293
 
 
294
                if ( $count )
 
295
                        $fields = 'COUNT(*)';
 
296
                else
 
297
                        $fields = '*';
 
298
 
 
299
                $join = '';
 
300
                $where = $approved;
 
301
 
 
302
                if ( ! empty($post_id) )
 
303
                        $where .= $wpdb->prepare( ' AND comment_post_ID = %d', $post_id );
 
304
                if ( '' !== $author_email )
 
305
                        $where .= $wpdb->prepare( ' AND comment_author_email = %s', $author_email );
 
306
                if ( '' !== $karma )
 
307
                        $where .= $wpdb->prepare( ' AND comment_karma = %d', $karma );
 
308
                if ( 'comment' == $type ) {
 
309
                        $where .= " AND comment_type = ''";
 
310
                } elseif( 'pings' == $type ) {
 
311
                        $where .= ' AND comment_type IN ("pingback", "trackback")';
 
312
                } elseif ( ! empty( $type ) ) {
 
313
                        $where .= $wpdb->prepare( ' AND comment_type = %s', $type );
 
314
                }
 
315
                if ( '' !== $parent )
 
316
                        $where .= $wpdb->prepare( ' AND comment_parent = %d', $parent );
 
317
                if ( '' !== $user_id )
 
318
                        $where .= $wpdb->prepare( ' AND user_id = %d', $user_id );
 
319
                if ( '' !== $search )
 
320
                        $where .= $this->get_search_sql( $search, array( 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_author_IP', 'comment_content' ) );
 
321
 
 
322
                $pieces = array( 'fields', 'join', 'where', 'orderby', 'order', 'limits' );
 
323
                $clauses = apply_filters_ref_array( 'comments_clauses', array( compact( $pieces ), &$this ) );
 
324
                foreach ( $pieces as $piece )
 
325
                        $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
 
326
 
 
327
                $query = "SELECT $fields FROM $wpdb->comments $join WHERE $where ORDER BY $orderby $order $limits";
 
328
 
 
329
                if ( $count )
 
330
                        return $wpdb->get_var( $query );
 
331
 
 
332
                $comments = $wpdb->get_results( $query );
 
333
                $comments = apply_filters_ref_array( 'the_comments', array( $comments, &$this ) );
 
334
 
 
335
                wp_cache_add( $cache_key, $comments, 'comment' );
 
336
 
 
337
                return $comments;
 
338
        }
 
339
 
 
340
        /*
 
341
         * Used internally to generate an SQL string for searching across multiple columns
 
342
         *
 
343
         * @access protected
 
344
         * @since 3.1.0
 
345
         *
 
346
         * @param string $string
 
347
         * @param array $cols
 
348
         * @return string
 
349
         */
 
350
        function get_search_sql( $string, $cols ) {
 
351
                $string = esc_sql( like_escape( $string ) );
 
352
 
 
353
                $searches = array();
 
354
                foreach ( $cols as $col )
 
355
                        $searches[] = "$col LIKE '%$string%'";
 
356
 
 
357
                return ' AND (' . implode(' OR ', $searches) . ')';
 
358
        }
301
359
}
302
360
 
303
361
/**
432
490
 * @link http://codex.wordpress.org/Function_Reference/add_comment_meta
433
491
 *
434
492
 * @param int $comment_id Comment ID.
435
 
 * @param string $key Metadata name.
436
 
 * @param mixed $value Metadata value.
 
493
 * @param string $meta_key Metadata name.
 
494
 * @param mixed $meta_value Metadata value.
437
495
 * @param bool $unique Optional, default is false. Whether the same key should not be added.
438
496
 * @return bool False for failure. True for success.
439
497
 */
491
549
 * @link http://codex.wordpress.org/Function_Reference/update_comment_meta
492
550
 *
493
551
 * @param int $comment_id Comment ID.
494
 
 * @param string $key Metadata key.
495
 
 * @param mixed $value Metadata value.
 
552
 * @param string $meta_key Metadata key.
 
553
 * @param mixed $meta_value Metadata value.
496
554
 * @param mixed $prev_value Optional. Previous value to check before removing.
497
555
 * @return bool False on failure, true if success.
498
556
 */
581
639
                        $approved = 'spam';
582
640
        }
583
641
 
584
 
        $approved = apply_filters('pre_comment_approved', $approved);
 
642
        $approved = apply_filters( 'pre_comment_approved', $approved, $commentdata );
585
643
        return $approved;
586
644
}
587
645
 
1096
1154
        // Call the hooks
1097
1155
        if ( $new_status != $old_status ) {
1098
1156
                do_action('transition_comment_status', $new_status, $old_status, $comment);
1099
 
                do_action("comment_${old_status}_to_$new_status", $comment);
 
1157
                do_action("comment_{$old_status}_to_{$new_status}", $comment);
1100
1158
        }
1101
 
        do_action("comment_${new_status}_$comment->comment_type", $comment->comment_ID, $comment);
 
1159
        do_action("comment_{$new_status}_{$comment->comment_type}", $comment->comment_ID, $comment);
1102
1160
}
1103
1161
 
1104
1162
/**
1128
1186
        if ( isset($_COOKIE['comment_author_url_'.COOKIEHASH]) )
1129
1187
                $comment_author_url = $_COOKIE['comment_author_url_'.COOKIEHASH];
1130
1188
 
1131
 
        return compact('comment_author', 'comment_author_email', 'comment_author_url');
 
1189
        return apply_filters('wp_get_current_commenter', compact('comment_author', 'comment_author_email', 'comment_author_url'));
1132
1190
}
1133
1191
 
1134
1192
/**
1238
1296
 * and whether comment is approved by WordPress. Also has 'preprocess_comment'
1239
1297
 * filter for processing the comment data before the function handles it.
1240
1298
 *
 
1299
 * We use REMOTE_ADDR here directly. If you are behind a proxy, you should ensure
 
1300
 * that it is properly set, such as in wp-config.php, for your environment.
 
1301
 * See {@link http://core.trac.wordpress.org/ticket/9235}
 
1302
 *
1241
1303
 * @since 1.5.0
1242
1304
 * @uses apply_filters() Calls 'preprocess_comment' hook on $commentdata parameter array before processing
1243
1305
 * @uses do_action() Calls 'comment_post' hook on $comment_ID returned from adding the comment and if the comment was approved.
1282
1344
                $post = &get_post($commentdata['comment_post_ID']); // Don't notify if it's your own comment
1283
1345
 
1284
1346
                if ( get_option('comments_notify') && $commentdata['comment_approved'] && ( ! isset( $commentdata['user_id'] ) || $post->post_author != $commentdata['user_id'] ) )
1285
 
                        wp_notify_postauthor($comment_ID, empty( $commentdata['comment_type'] ) ? $commentdata['comment_type'] : '' );
 
1347
                        wp_notify_postauthor($comment_ID, isset( $commentdata['comment_type'] ) ? $commentdata['comment_type'] : '' );
1286
1348
        }
1287
1349
 
1288
1350
        return $comment_ID;
1654
1716
                                trackback($tb_ping, $post_title, $excerpt, $post_id);
1655
1717
                                $pinged[] = $tb_ping;
1656
1718
                        } else {
1657
 
                                $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, '$tb_ping', '')) WHERE ID = %d", $post_id) );
 
1719
                                $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, %s, '')) WHERE ID = %d", $tb_ping, $post_id) );
1658
1720
                        }
1659
1721
                }
1660
1722
        }
1694
1756
function pingback($content, $post_ID) {
1695
1757
        global $wp_version;
1696
1758
        include_once(ABSPATH . WPINC . '/class-IXR.php');
 
1759
        include_once(ABSPATH . WPINC . '/class-wp-http-ixr-client.php');
1697
1760
 
1698
1761
        // original code by Mort (http://mort.mine.nu:8080)
1699
1762
        $post_links = array();
1727
1790
                        if ( $test = @parse_url($link_test) ) {
1728
1791
                                if ( isset($test['query']) )
1729
1792
                                        $post_links[] = $link_test;
1730
 
                                elseif ( ($test['path'] != '/') && ($test['path'] != '') )
 
1793
                                elseif ( isset( $test['path'] ) && ( $test['path'] != '/' ) && ( $test['path'] != '' ) )
1731
1794
                                        $post_links[] = $link_test;
1732
1795
                        }
1733
1796
                endif;
1736
1799
        do_action_ref_array('pre_ping', array(&$post_links, &$pung));
1737
1800
 
1738
1801
        foreach ( (array) $post_links as $pagelinkedto ) {
1739
 
                $pingback_server_url = discover_pingback_server_uri($pagelinkedto, 2048);
 
1802
                $pingback_server_url = discover_pingback_server_uri( $pagelinkedto );
1740
1803
 
1741
1804
                if ( $pingback_server_url ) {
1742
1805
                        @ set_time_limit( 60 );
1744
1807
                        $pagelinkedfrom = get_permalink($post_ID);
1745
1808
 
1746
1809
                        // using a timeout of 3 seconds should be enough to cover slow servers
1747
 
                        $client = new IXR_Client($pingback_server_url);
 
1810
                        $client = new WP_HTTP_IXR_Client($pingback_server_url);
1748
1811
                        $client->timeout = 3;
1749
1812
                        $client->useragent = apply_filters( 'pingback_useragent', $client->useragent . ' -- WordPress/' . $wp_version, $client->useragent, $pingback_server_url, $pagelinkedto, $pagelinkedfrom);
1750
1813
                        // when set to true, this outputs debug messages by itself
1805
1868
        if ( is_wp_error( $response ) )
1806
1869
                return;
1807
1870
 
1808
 
        $tb_url = addslashes( $trackback_url );
1809
 
        $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET pinged = CONCAT(pinged, '\n', '$tb_url') WHERE ID = %d", $ID) );
1810
 
        return $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, '$tb_url', '')) WHERE ID = %d", $ID) );
 
1871
        $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET pinged = CONCAT(pinged, '\n', %s) WHERE ID = %d", $trackback_url, $ID) );
 
1872
        return $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, %s, '')) WHERE ID = %d", $trackback_url, $ID) );
1811
1873
}
1812
1874
 
1813
1875
/**
1823
1885
function weblog_ping($server = '', $path = '') {
1824
1886
        global $wp_version;
1825
1887
        include_once(ABSPATH . WPINC . '/class-IXR.php');
 
1888
        include_once(ABSPATH . WPINC . '/class-wp-http-ixr-client.php');
1826
1889
 
1827
1890
        // using a timeout of 3 seconds should be enough to cover slow servers
1828
 
        $client = new IXR_Client($server, ((!strlen(trim($path)) || ('/' == $path)) ? false : $path));
 
1891
        $client = new WP_HTTP_IXR_Client($server, ((!strlen(trim($path)) || ('/' == $path)) ? false : $path));
1829
1892
        $client->timeout = 3;
1830
1893
        $client->useragent .= ' -- WordPress/'.$wp_version;
1831
1894
 
1847
1910
 * @package WordPress
1848
1911
 * @subpackage Cache
1849
1912
 *
1850
 
 * @param int|array $id Comment ID or array of comment IDs to remove from cache
 
1913
 * @param int|array $ids Comment ID or array of comment IDs to remove from cache
1851
1914
 */
1852
1915
function clean_comment_cache($ids) {
1853
1916
        foreach ( (array) $ids as $id )
1854
1917
                wp_cache_delete($id, 'comment');
 
1918
 
 
1919
        wp_cache_set('last_changed', time(), 'comment');
1855
1920
}
1856
1921
 
1857
1922
/**