mirror of
https://github.com/WordPress/WordPress.git
synced 2024-09-28 15:17:42 +02:00
Move _wp_search_sql() into WP_Object_Query. Introduce WP_Comment_Search. See #15032
git-svn-id: http://svn.automattic.com/wordpress/trunk@15723 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
38b834c848
commit
31797f691a
@ -546,7 +546,7 @@ class WP_Object_Query {
|
|||||||
/*
|
/*
|
||||||
* Populates the $meta_query property
|
* Populates the $meta_query property
|
||||||
*
|
*
|
||||||
* @access private
|
* @access protected
|
||||||
* @since 3.1.0
|
* @since 3.1.0
|
||||||
*
|
*
|
||||||
* @param array $qv The query variables
|
* @param array $qv The query variables
|
||||||
@ -570,7 +570,7 @@ class WP_Object_Query {
|
|||||||
/*
|
/*
|
||||||
* Used internally to generate an SQL string for searching across multiple meta key = value pairs
|
* Used internally to generate an SQL string for searching across multiple meta key = value pairs
|
||||||
*
|
*
|
||||||
* @access private
|
* @access protected
|
||||||
* @since 3.1.0
|
* @since 3.1.0
|
||||||
*
|
*
|
||||||
* @param string $primary_table
|
* @param string $primary_table
|
||||||
@ -621,6 +621,26 @@ class WP_Object_Query {
|
|||||||
|
|
||||||
return array( $join, $where );
|
return array( $join, $where );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Used internally to generate an SQL string for searching across multiple columns
|
||||||
|
*
|
||||||
|
* @access protected
|
||||||
|
* @since 3.1.0
|
||||||
|
*
|
||||||
|
* @param string $string
|
||||||
|
* @param array $cols
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
function get_search_sql( $string, $cols ) {
|
||||||
|
$string = esc_sql( $string );
|
||||||
|
|
||||||
|
$searches = array();
|
||||||
|
foreach ( $cols as $col )
|
||||||
|
$searches[] = "$col LIKE '%$string%'";
|
||||||
|
|
||||||
|
return ' AND (' . implode(' OR ', $searches) . ')';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -188,123 +188,131 @@ function &get_comment(&$comment, $output = OBJECT) {
|
|||||||
* @return array List of comments.
|
* @return array List of comments.
|
||||||
*/
|
*/
|
||||||
function get_comments( $args = '' ) {
|
function get_comments( $args = '' ) {
|
||||||
global $wpdb;
|
$query = new WP_Comment_Query;
|
||||||
|
return $query->query( $args );
|
||||||
|
}
|
||||||
|
|
||||||
$defaults = array(
|
class WP_Comment_Query extends WP_Object_Query {
|
||||||
'author_email' => '',
|
|
||||||
'ID' => '',
|
|
||||||
'karma' => '',
|
|
||||||
'number' => '',
|
|
||||||
'offset' => '',
|
|
||||||
'orderby' => '',
|
|
||||||
'order' => 'DESC',
|
|
||||||
'parent' => '',
|
|
||||||
'post_ID' => '',
|
|
||||||
'post_id' => 0,
|
|
||||||
'status' => '',
|
|
||||||
'type' => '',
|
|
||||||
'user_id' => '',
|
|
||||||
'search' => '',
|
|
||||||
'count' => false
|
|
||||||
);
|
|
||||||
|
|
||||||
$args = wp_parse_args( $args, $defaults );
|
function query( $args ) {
|
||||||
extract( $args, EXTR_SKIP );
|
global $wpdb;
|
||||||
|
|
||||||
// $args can be whatever, only use the args defined in defaults to compute the key
|
$defaults = array(
|
||||||
$key = md5( serialize( compact(array_keys($defaults)) ) );
|
'author_email' => '',
|
||||||
$last_changed = wp_cache_get('last_changed', 'comment');
|
'ID' => '',
|
||||||
if ( !$last_changed ) {
|
'karma' => '',
|
||||||
$last_changed = time();
|
'number' => '',
|
||||||
wp_cache_set('last_changed', $last_changed, 'comment');
|
'offset' => '',
|
||||||
}
|
'orderby' => '',
|
||||||
$cache_key = "get_comments:$key:$last_changed";
|
'order' => 'DESC',
|
||||||
|
'parent' => '',
|
||||||
if ( $cache = wp_cache_get( $cache_key, 'comment' ) ) {
|
'post_ID' => '',
|
||||||
return $cache;
|
'post_id' => 0,
|
||||||
}
|
'status' => '',
|
||||||
|
'type' => '',
|
||||||
$post_id = absint($post_id);
|
'user_id' => '',
|
||||||
|
'search' => '',
|
||||||
if ( 'hold' == $status )
|
'count' => false
|
||||||
$approved = "comment_approved = '0'";
|
|
||||||
elseif ( 'approve' == $status )
|
|
||||||
$approved = "comment_approved = '1'";
|
|
||||||
elseif ( 'spam' == $status )
|
|
||||||
$approved = "comment_approved = 'spam'";
|
|
||||||
elseif ( 'trash' == $status )
|
|
||||||
$approved = "comment_approved = 'trash'";
|
|
||||||
else
|
|
||||||
$approved = "( comment_approved = '0' OR comment_approved = '1' )";
|
|
||||||
|
|
||||||
$order = ( 'ASC' == strtoupper($order) ) ? 'ASC' : 'DESC';
|
|
||||||
|
|
||||||
if ( ! empty( $orderby ) ) {
|
|
||||||
$ordersby = is_array($orderby) ? $orderby : preg_split('/[,\s]/', $orderby);
|
|
||||||
$ordersby = array_intersect(
|
|
||||||
$ordersby,
|
|
||||||
array(
|
|
||||||
'comment_agent',
|
|
||||||
'comment_approved',
|
|
||||||
'comment_author',
|
|
||||||
'comment_author_email',
|
|
||||||
'comment_author_IP',
|
|
||||||
'comment_author_url',
|
|
||||||
'comment_content',
|
|
||||||
'comment_date',
|
|
||||||
'comment_date_gmt',
|
|
||||||
'comment_ID',
|
|
||||||
'comment_karma',
|
|
||||||
'comment_parent',
|
|
||||||
'comment_post_ID',
|
|
||||||
'comment_type',
|
|
||||||
'user_id',
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
$orderby = empty( $ordersby ) ? 'comment_date_gmt' : implode(', ', $ordersby);
|
|
||||||
} else {
|
|
||||||
$orderby = 'comment_date_gmt';
|
|
||||||
}
|
|
||||||
|
|
||||||
$number = absint($number);
|
$args = wp_parse_args( $args, $defaults );
|
||||||
$offset = absint($offset);
|
extract( $args, EXTR_SKIP );
|
||||||
|
|
||||||
if ( !empty($number) ) {
|
// $args can be whatever, only use the args defined in defaults to compute the key
|
||||||
if ( $offset )
|
$key = md5( serialize( compact(array_keys($defaults)) ) );
|
||||||
$limit = 'LIMIT ' . $offset . ',' . $number;
|
$last_changed = wp_cache_get('last_changed', 'comment');
|
||||||
|
if ( !$last_changed ) {
|
||||||
|
$last_changed = time();
|
||||||
|
wp_cache_set('last_changed', $last_changed, 'comment');
|
||||||
|
}
|
||||||
|
$cache_key = "get_comments:$key:$last_changed";
|
||||||
|
|
||||||
|
if ( $cache = wp_cache_get( $cache_key, 'comment' ) ) {
|
||||||
|
return $cache;
|
||||||
|
}
|
||||||
|
|
||||||
|
$post_id = absint($post_id);
|
||||||
|
|
||||||
|
if ( 'hold' == $status )
|
||||||
|
$approved = "comment_approved = '0'";
|
||||||
|
elseif ( 'approve' == $status )
|
||||||
|
$approved = "comment_approved = '1'";
|
||||||
|
elseif ( 'spam' == $status )
|
||||||
|
$approved = "comment_approved = 'spam'";
|
||||||
|
elseif ( 'trash' == $status )
|
||||||
|
$approved = "comment_approved = 'trash'";
|
||||||
else
|
else
|
||||||
$limit = 'LIMIT ' . $number;
|
$approved = "( comment_approved = '0' OR comment_approved = '1' )";
|
||||||
} else {
|
|
||||||
$limit = '';
|
$order = ( 'ASC' == strtoupper($order) ) ? 'ASC' : 'DESC';
|
||||||
|
|
||||||
|
if ( ! empty( $orderby ) ) {
|
||||||
|
$ordersby = is_array($orderby) ? $orderby : preg_split('/[,\s]/', $orderby);
|
||||||
|
$ordersby = array_intersect(
|
||||||
|
$ordersby,
|
||||||
|
array(
|
||||||
|
'comment_agent',
|
||||||
|
'comment_approved',
|
||||||
|
'comment_author',
|
||||||
|
'comment_author_email',
|
||||||
|
'comment_author_IP',
|
||||||
|
'comment_author_url',
|
||||||
|
'comment_content',
|
||||||
|
'comment_date',
|
||||||
|
'comment_date_gmt',
|
||||||
|
'comment_ID',
|
||||||
|
'comment_karma',
|
||||||
|
'comment_parent',
|
||||||
|
'comment_post_ID',
|
||||||
|
'comment_type',
|
||||||
|
'user_id',
|
||||||
|
)
|
||||||
|
);
|
||||||
|
$orderby = empty( $ordersby ) ? 'comment_date_gmt' : implode(', ', $ordersby);
|
||||||
|
} else {
|
||||||
|
$orderby = 'comment_date_gmt';
|
||||||
|
}
|
||||||
|
|
||||||
|
$number = absint($number);
|
||||||
|
$offset = absint($offset);
|
||||||
|
|
||||||
|
if ( !empty($number) ) {
|
||||||
|
if ( $offset )
|
||||||
|
$limit = 'LIMIT ' . $offset . ',' . $number;
|
||||||
|
else
|
||||||
|
$limit = 'LIMIT ' . $number;
|
||||||
|
} else {
|
||||||
|
$limit = '';
|
||||||
|
}
|
||||||
|
|
||||||
|
$post_where = "WHERE $approved";
|
||||||
|
|
||||||
|
if ( ! empty($post_id) )
|
||||||
|
$post_where .= $wpdb->prepare( ' AND comment_post_ID = %d', $post_id );
|
||||||
|
if ( '' !== $author_email )
|
||||||
|
$post_where .= $wpdb->prepare( 'AND comment_author_email = %s', $author_email );
|
||||||
|
if ( '' !== $karma )
|
||||||
|
$post_where .= $wpdb->prepare( 'AND comment_karma = %d', $karma );
|
||||||
|
if ( 'comment' == $type )
|
||||||
|
$post_where .= " AND comment_type = ''";
|
||||||
|
elseif ( ! empty( $type ) )
|
||||||
|
$post_where .= $wpdb->prepare( ' AND comment_type = %s', $type );
|
||||||
|
if ( '' !== $parent )
|
||||||
|
$post_where .= $wpdb->prepare( ' AND comment_parent = %d', $parent );
|
||||||
|
if ( '' !== $user_id )
|
||||||
|
$post_where .= $wpdb->prepare( ' AND user_id = %d', $user_id );
|
||||||
|
if ( '' !== $search )
|
||||||
|
$post_where .= $this->get_search_sql( $search, array( 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_author_IP', 'comment_content' ) );
|
||||||
|
|
||||||
|
if ( $count )
|
||||||
|
return $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->comments $post_where ORDER BY $orderby $order $limit" );
|
||||||
|
|
||||||
|
$comments = $wpdb->get_results( "SELECT * FROM $wpdb->comments $post_where ORDER BY $orderby $order $limit" );
|
||||||
|
|
||||||
|
wp_cache_add( $cache_key, $comments, 'comment' );
|
||||||
|
|
||||||
|
return $comments;
|
||||||
}
|
}
|
||||||
|
|
||||||
$post_where = "WHERE $approved";
|
|
||||||
|
|
||||||
if ( ! empty($post_id) )
|
|
||||||
$post_where .= $wpdb->prepare( ' AND comment_post_ID = %d', $post_id );
|
|
||||||
if ( '' !== $author_email )
|
|
||||||
$post_where .= $wpdb->prepare( 'AND comment_author_email = %s', $author_email );
|
|
||||||
if ( '' !== $karma )
|
|
||||||
$post_where .= $wpdb->prepare( 'AND comment_karma = %d', $karma );
|
|
||||||
if ( 'comment' == $type )
|
|
||||||
$post_where .= " AND comment_type = ''";
|
|
||||||
elseif ( ! empty( $type ) )
|
|
||||||
$post_where .= $wpdb->prepare( ' AND comment_type = %s', $type );
|
|
||||||
if ( '' !== $parent )
|
|
||||||
$post_where .= $wpdb->prepare( ' AND comment_parent = %d', $parent );
|
|
||||||
if ( '' !== $user_id )
|
|
||||||
$post_where .= $wpdb->prepare( ' AND user_id = %d', $user_id );
|
|
||||||
if ( '' !== $search )
|
|
||||||
$post_where .= _wp_search_sql($search, array('comment_author', 'comment_author_email', 'comment_author_url', 'comment_author_IP', 'comment_content'));
|
|
||||||
|
|
||||||
if ( $count )
|
|
||||||
return $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->comments $post_where ORDER BY $orderby $order $limit" );
|
|
||||||
|
|
||||||
$comments = $wpdb->get_results( "SELECT * FROM $wpdb->comments $post_where ORDER BY $orderby $order $limit" );
|
|
||||||
|
|
||||||
wp_cache_add( $cache_key, $comments, 'comment' );
|
|
||||||
|
|
||||||
return $comments;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -4236,26 +4236,6 @@ function get_file_data( $file, $default_headers, $context = '' ) {
|
|||||||
return $file_data;
|
return $file_data;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* Used internally to generate an SQL string for searching across multiple columns
|
|
||||||
*
|
|
||||||
* @access private
|
|
||||||
* @since 3.1.0
|
|
||||||
*
|
|
||||||
* @param string $string
|
|
||||||
* @param array $cols
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
function _wp_search_sql($string, $cols) {
|
|
||||||
$string = esc_sql($string);
|
|
||||||
|
|
||||||
$searches = array();
|
|
||||||
foreach ( $cols as $col )
|
|
||||||
$searches[] = "$col LIKE '%$string%'";
|
|
||||||
|
|
||||||
return ' AND (' . implode(' OR ', $searches) . ')';
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Used internally to tidy up the search terms
|
* Used internally to tidy up the search terms
|
||||||
*
|
*
|
||||||
|
@ -441,7 +441,7 @@ class WP_User_Query extends WP_Object_Query {
|
|||||||
|
|
||||||
$search = trim( $qv['search'] );
|
$search = trim( $qv['search'] );
|
||||||
if ( $search ) {
|
if ( $search ) {
|
||||||
$this->query_where .= _wp_search_sql( $search, array('user_login', 'user_nicename', 'user_email', 'user_url', 'display_name') );
|
$this->query_where .= $this->get_search_sql( $search, array( 'user_login', 'user_nicename', 'user_email', 'user_url', 'display_name' ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->parse_meta_query( $qv );
|
$this->parse_meta_query( $qv );
|
||||||
|
Loading…
Reference in New Issue
Block a user