mirror of
git://develop.git.wordpress.org/
synced 2025-02-27 10:02:43 +01:00
Move _wp_search_sql() into WP_Object_Query. Introduce WP_Comment_Search. See #15032
git-svn-id: https://develop.svn.wordpress.org/trunk@15723 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
4360f8c1a4
commit
12fdf54fdd
@ -546,7 +546,7 @@ class WP_Object_Query {
|
||||
/*
|
||||
* Populates the $meta_query property
|
||||
*
|
||||
* @access private
|
||||
* @access protected
|
||||
* @since 3.1.0
|
||||
*
|
||||
* @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
|
||||
*
|
||||
* @access private
|
||||
* @access protected
|
||||
* @since 3.1.0
|
||||
*
|
||||
* @param string $primary_table
|
||||
@ -621,6 +621,26 @@ class WP_Object_Query {
|
||||
|
||||
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.
|
||||
*/
|
||||
function get_comments( $args = '' ) {
|
||||
global $wpdb;
|
||||
$query = new WP_Comment_Query;
|
||||
return $query->query( $args );
|
||||
}
|
||||
|
||||
$defaults = array(
|
||||
'author_email' => '',
|
||||
'ID' => '',
|
||||
'karma' => '',
|
||||
'number' => '',
|
||||
'offset' => '',
|
||||
'orderby' => '',
|
||||
'order' => 'DESC',
|
||||
'parent' => '',
|
||||
'post_ID' => '',
|
||||
'post_id' => 0,
|
||||
'status' => '',
|
||||
'type' => '',
|
||||
'user_id' => '',
|
||||
'search' => '',
|
||||
'count' => false
|
||||
);
|
||||
class WP_Comment_Query extends WP_Object_Query {
|
||||
|
||||
$args = wp_parse_args( $args, $defaults );
|
||||
extract( $args, EXTR_SKIP );
|
||||
function query( $args ) {
|
||||
global $wpdb;
|
||||
|
||||
// $args can be whatever, only use the args defined in defaults to compute the key
|
||||
$key = md5( serialize( compact(array_keys($defaults)) ) );
|
||||
$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
|
||||
$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',
|
||||
)
|
||||
$defaults = array(
|
||||
'author_email' => '',
|
||||
'ID' => '',
|
||||
'karma' => '',
|
||||
'number' => '',
|
||||
'offset' => '',
|
||||
'orderby' => '',
|
||||
'order' => 'DESC',
|
||||
'parent' => '',
|
||||
'post_ID' => '',
|
||||
'post_id' => 0,
|
||||
'status' => '',
|
||||
'type' => '',
|
||||
'user_id' => '',
|
||||
'search' => '',
|
||||
'count' => false
|
||||
);
|
||||
$orderby = empty( $ordersby ) ? 'comment_date_gmt' : implode(', ', $ordersby);
|
||||
} else {
|
||||
$orderby = 'comment_date_gmt';
|
||||
}
|
||||
|
||||
$number = absint($number);
|
||||
$offset = absint($offset);
|
||||
$args = wp_parse_args( $args, $defaults );
|
||||
extract( $args, EXTR_SKIP );
|
||||
|
||||
if ( !empty($number) ) {
|
||||
if ( $offset )
|
||||
$limit = 'LIMIT ' . $offset . ',' . $number;
|
||||
// $args can be whatever, only use the args defined in defaults to compute the key
|
||||
$key = md5( serialize( compact(array_keys($defaults)) ) );
|
||||
$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
|
||||
$limit = 'LIMIT ' . $number;
|
||||
} else {
|
||||
$limit = '';
|
||||
$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);
|
||||
$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;
|
||||
}
|
||||
|
||||
/*
|
||||
* 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
|
||||
*
|
||||
|
@ -441,7 +441,7 @@ class WP_User_Query extends WP_Object_Query {
|
||||
|
||||
$search = trim( $qv['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 );
|
||||
|
Loading…
x
Reference in New Issue
Block a user