2010-10-28 12:27:18 -04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* WordPress Query class.
|
|
|
|
*
|
|
|
|
* Abstract class for handling advanced queries
|
|
|
|
*
|
|
|
|
* @package WordPress
|
|
|
|
* @since 3.1.0
|
|
|
|
*/
|
|
|
|
class WP_Object_Query {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Query vars, after parsing
|
|
|
|
*
|
|
|
|
* @since 3.1.0
|
|
|
|
* @access public
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
var $query_vars;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve query variable.
|
|
|
|
*
|
|
|
|
* @since 3.1.0
|
|
|
|
* @access public
|
|
|
|
*
|
|
|
|
* @param string $query_var Query variable key.
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
function get( $query_var ) {
|
|
|
|
if ( isset( $this->query_vars[$query_var] ) )
|
|
|
|
return $this->query_vars[$query_var];
|
|
|
|
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set query variable.
|
|
|
|
*
|
|
|
|
* @since 3.1.0
|
|
|
|
* @access public
|
|
|
|
*
|
|
|
|
* @param string $query_var Query variable key.
|
|
|
|
* @param mixed $value Query variable value.
|
|
|
|
*/
|
|
|
|
function set( $query_var, $value ) {
|
|
|
|
$this->query_vars[ $query_var ] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Populates the $meta_query property
|
|
|
|
*
|
|
|
|
* @access protected
|
|
|
|
* @since 3.1.0
|
|
|
|
*
|
|
|
|
* @param array $qv The query variables
|
|
|
|
*/
|
|
|
|
function parse_meta_query( &$qv ) {
|
|
|
|
$meta_query = array();
|
|
|
|
|
|
|
|
// Simple query needs to be first for orderby=meta_value to work correctly
|
|
|
|
foreach ( array( 'key', 'value', 'compare', 'type' ) as $key ) {
|
|
|
|
if ( !empty( $qv[ "meta_$key" ] ) )
|
|
|
|
$meta_query[0][ $key ] = $qv[ "meta_$key" ];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !empty( $qv['meta_query'] ) && is_array( $qv['meta_query'] ) ) {
|
|
|
|
$meta_query = array_merge( $meta_query, $qv['meta_query'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
$qv['meta_query'] = $meta_query;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Used internally to generate an SQL string for searching across multiple columns
|
|
|
|
*
|
|
|
|
* @access protected
|
|
|
|
* @since 3.1.0
|
|
|
|
*
|
|
|
|
* @param string $string
|
|
|
|
* @param array $cols
|
2010-11-03 15:31:11 -04:00
|
|
|
* @param bool $wild Whether to allow trailing wildcard searches. Default is false.
|
2010-10-28 12:27:18 -04:00
|
|
|
* @return string
|
|
|
|
*/
|
2010-11-03 15:31:11 -04:00
|
|
|
function get_search_sql( $string, $cols, $wild = false ) {
|
2010-10-28 12:27:18 -04:00
|
|
|
$string = esc_sql( $string );
|
|
|
|
|
|
|
|
$searches = array();
|
2010-11-03 15:31:11 -04:00
|
|
|
$wild_char = ( $wild ) ? '%' : '';
|
2010-11-03 15:02:42 -04:00
|
|
|
foreach ( $cols as $col ) {
|
|
|
|
if ( 'ID' == $col )
|
|
|
|
$searches[] = "$col = '$string'";
|
|
|
|
else
|
2010-11-03 15:31:11 -04:00
|
|
|
$searches[] = "$col LIKE '$string$wild_char'";
|
2010-11-03 15:02:42 -04:00
|
|
|
}
|
2010-10-28 12:27:18 -04:00
|
|
|
|
|
|
|
return ' AND (' . implode(' OR ', $searches) . ')';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|