Make WordPress Core


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/comment.php

    r16638 r17053  
    4545                return false; // If moderation is set to manual
    4646
     47        $comment = apply_filters( 'comment_text', $comment );
     48
    4749        // Check # of external links
    4850        if ( $max_links = get_option( 'comment_max_links' ) ) {
    49                 $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 );
    5052                $num_links = apply_filters( 'comment_max_links_url', $num_links, $url ); // provide for counting of $url as a link
    5153                if ( $num_links >= $max_links )
    … …  
    179181 */
    180182function get_comments( $args = '' ) {
    181         global $wpdb;
    182 
    183         $defaults = array(
    184                 'author_email' => '',
    185                 'ID' => '',
    186                 'karma' => '',
    187                 'number' => '',
    188                 'offset' => '',
    189                 'orderby' => '',
    190                 'order' => 'DESC',
    191                 'parent' => '',
    192                 'post_ID' => '',
    193                 'post_id' => 0,
    194                 'status' => '',
    195                 'type' => '',
    196                 'user_id' => '',
    197         );
    198 
    199         $args = wp_parse_args( $args, $defaults );
    200         extract( $args, EXTR_SKIP );
    201 
    202         // $args can be whatever, only use the args defined in defaults to compute the key
    203         $key = md5( serialize( compact(array_keys($defaults)) )  );
    204         $last_changed = wp_cache_get('last_changed', 'comment');
    205         if ( !$last_changed ) {
    206                 $last_changed = time();
    207                 wp_cache_set('last_changed', $last_changed, 'comment');
    208         }
    209         $cache_key = "get_comments:$key:$last_changed";
    210 
    211         if ( $cache = wp_cache_get( $cache_key, 'comment' ) ) {
    212                 return $cache;
    213         }
    214 
    215         $post_id = absint($post_id);
    216 
    217         if ( 'hold' == $status )
    218                 $approved = "comment_approved = '0'";
    219         elseif ( 'approve' == $status )
    220                 $approved = "comment_approved = '1'";
    221         elseif ( 'spam' == $status )
    222                 $approved = "comment_approved = 'spam'";
    223         elseif ( 'trash' == $status )
    224                 $approved = "comment_approved = 'trash'";
    225         else
    226                 $approved = "( comment_approved = '0' OR comment_approved = '1' )";
    227 
    228         $order = ( 'ASC' == $order ) ? 'ASC' : 'DESC';
    229 
    230         if ( ! empty( $orderby ) ) {
    231                 $ordersby = is_array($orderby) ? $orderby : preg_split('/[,\s]/', $orderby);
    232                 $ordersby = array_intersect(
    233                         $ordersby,
    234                         array(
    235                                 'comment_agent',
    236                                 'comment_approved',
    237                                 'comment_author',
    238                                 'comment_author_email',
    239                                 'comment_author_IP',
    240                                 'comment_author_url',
    241                                 'comment_content',
    242                                 'comment_date',
    243                                 'comment_date_gmt',
    244                                 'comment_ID',
    245                                 'comment_karma',
    246                                 'comment_parent',
    247                                 'comment_post_ID',
    248                                 'comment_type',
    249                                 'user_id',
    250                         )
     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 */
     192class 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
    251221                );
    252                 $orderby = empty( $ordersby ) ? 'comment_date_gmt' : implode(', ', $ordersby);
    253         } else {
    254                 $orderby = 'comment_date_gmt';
    255         }
    256 
    257         $number = absint($number);
    258         $offset = absint($offset);
    259 
    260         if ( !empty($number) ) {
    261                 if ( $offset )
    262                         $number = 'LIMIT ' . $offset . ',' . $number;
     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'";
    263250                else
    264                         $number = 'LIMIT ' . $number;
    265 
    266         } else {
    267                 $number = '';
    268         }
    269 
    270         $post_where = '';
    271 
    272         if ( ! empty($post_id) )
    273                 $post_where .= $wpdb->prepare( 'comment_post_ID = %d AND ', $post_id );
    274         if ( '' !== $author_email )
    275                 $post_where .= $wpdb->prepare( 'comment_author_email = %s AND ', $author_email );
    276         if ( '' !== $karma )
    277                 $post_where .= $wpdb->prepare( 'comment_karma = %d AND ', $karma );
    278         if ( 'comment' == $type )
    279                 $post_where .= "comment_type = '' AND ";
    280         elseif ( ! empty( $type ) )
    281                 $post_where .= $wpdb->prepare( 'comment_type = %s AND ', $type );
    282         if ( '' !== $parent )
    283                 $post_where .= $wpdb->prepare( 'comment_parent = %d AND ', $parent );
    284         if ( '' !== $user_id )
    285                 $post_where .= $wpdb->prepare( 'user_id = %d AND ', $user_id );
    286 
    287         $comments = $wpdb->get_results( "SELECT * FROM $wpdb->comments WHERE $post_where $approved ORDER BY $orderby $order $number" );
    288         wp_cache_add( $cache_key, $comments, 'comment' );
    289 
    290         return $comments;
     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        }
    291359}
    292360
    … …  
    423491 *
    424492 * @param int $comment_id Comment ID.
    425  * @param string $key Metadata name.
    426  * @param mixed $value Metadata value.
     493 * @param string $meta_key Metadata name.
     494 * @param mixed $meta_value Metadata value.
    427495 * @param bool $unique Optional, default is false. Whether the same key should not be added.
    428496 * @return bool False for failure. True for success.
    … …  
    482550 *
    483551 * @param int $comment_id Comment ID.
    484  * @param string $key Metadata key.
    485  * @param mixed $value Metadata value.
     552 * @param string $meta_key Metadata key.
     553 * @param mixed $meta_value Metadata value.
    486554 * @param mixed $prev_value Optional. Previous value to check before removing.
    487555 * @return bool False on failure, true if success.
    … …  
    572640        }
    573641
    574         $approved = apply_filters('pre_comment_approved', $approved);
     642        $approved = apply_filters( 'pre_comment_approved', $approved, $commentdata );
    575643        return $approved;
    576644}
    … …  
    10871155        if ( $new_status != $old_status ) {
    10881156                do_action('transition_comment_status', $new_status, $old_status, $comment);
    1089                 do_action("comment_${old_status}_to_$new_status", $comment);
    1090         }
    1091         do_action("comment_${new_status}_$comment->comment_type", $comment->comment_ID, $comment);
     1157                do_action("comment_{$old_status}_to_{$new_status}", $comment);
     1158        }
     1159        do_action("comment_{$new_status}_{$comment->comment_type}", $comment->comment_ID, $comment);
    10921160}
    10931161
    … …  
    11191187                $comment_author_url = $_COOKIE['comment_author_url_'.COOKIEHASH];
    11201188
    1121         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'));
    11221190}
    11231191
    … …  
    12291297 * filter for processing the comment data before the function handles it.
    12301298 *
     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 *
    12311303 * @since 1.5.0
    12321304 * @uses apply_filters() Calls 'preprocess_comment' hook on $commentdata parameter array before processing
    … …  
    12731345
    12741346                if ( get_option('comments_notify') && $commentdata['comment_approved'] && ( ! isset( $commentdata['user_id'] ) || $post->post_author != $commentdata['user_id'] ) )
    1275                         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'] : '' );
    12761348        }
    12771349
    … …  
    16851757        global $wp_version;
    16861758        include_once(ABSPATH . WPINC . '/class-IXR.php');
     1759        include_once(ABSPATH . WPINC . '/class-wp-http-ixr-client.php');
    16871760
    16881761        // original code by Mort (http://mort.mine.nu:8080)
    … …  
    17181791                                if ( isset($test['query']) )
    17191792                                        $post_links[] = $link_test;
    1720                                 elseif ( ($test['path'] != '/') && ($test['path'] != '') )
     1793                                elseif ( isset( $test['path'] ) && ( $test['path'] != '/' ) && ( $test['path'] != '' ) )
    17211794                                        $post_links[] = $link_test;
    17221795                        }
    … …  
    17271800
    17281801        foreach ( (array) $post_links as $pagelinkedto ) {
    1729                 $pingback_server_url = discover_pingback_server_uri($pagelinkedto, 2048);
     1802                $pingback_server_url = discover_pingback_server_uri( $pagelinkedto );
    17301803
    17311804                if ( $pingback_server_url ) {
    … …  
    17351808
    17361809                        // using a timeout of 3 seconds should be enough to cover slow servers
    1737                         $client = new IXR_Client($pingback_server_url);
     1810                        $client = new WP_HTTP_IXR_Client($pingback_server_url);
    17381811                        $client->timeout = 3;
    17391812                        $client->useragent = apply_filters( 'pingback_useragent', $client->useragent . ' -- WordPress/' . $wp_version, $client->useragent, $pingback_server_url, $pagelinkedto, $pagelinkedfrom);
    … …  
    17961869                return;
    17971870
    1798         $tb_url = addslashes( $trackback_url );
    1799         $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET pinged = CONCAT(pinged, '\n', '$tb_url') WHERE ID = %d", $ID) );
    1800         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) );
    18011873}
    18021874
    … …  
    18141886        global $wp_version;
    18151887        include_once(ABSPATH . WPINC . '/class-IXR.php');
     1888        include_once(ABSPATH . WPINC . '/class-wp-http-ixr-client.php');
    18161889
    18171890        // using a timeout of 3 seconds should be enough to cover slow servers
    1818         $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));
    18191892        $client->timeout = 3;
    18201893        $client->useragent .= ' -- WordPress/'.$wp_version;
    … …  
    18381911 * @subpackage Cache
    18391912 *
    1840  * @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
    18411914 */
    18421915function clean_comment_cache($ids) {
    18431916        foreach ( (array) $ids as $id )
    18441917                wp_cache_delete($id, 'comment');
     1918
     1919        wp_cache_set('last_changed', time(), 'comment');
    18451920}
    18461921
Note: See TracChangeset for help on using the changeset viewer.