summaryrefslogtreecommitdiffstatshomepage
path: root/wp-includes/class.wp-object-query.php
blob: 1f3907ba8e53e4437df696c3c8b1105cad8ed8ac (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?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 meta key = value pairs
	 *
	 * @access protected
	 * @since 3.1.0
	 *
	 * @param array $meta_query List of metadata queries. A single query is an associative array:
	 * - 'key' string The meta key
	 * - 'value' string|array The meta value
	 * - 'compare' (optional) string How to compare the key to the value.
	 *		Possible values: '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'.
	 *		Default: '='
	 * - 'type' string (optional) The type of the value.
	 *		Possible values: 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED'.
	 *		Default: 'CHAR'
	 *
	 * @param string $primary_table
	 * @param string $primary_id_column
	 * @param string $meta_table
	 * @param string $meta_id_column
	 * @return array( $join_sql, $where_sql )
	 */
	function get_meta_sql( $meta_query, $primary_table, $primary_id_column, $meta_table, $meta_id_column ) {
		global $wpdb;

		$clauses = array();

		$join = '';
		$where = '';
		$i = 0;
		foreach ( $meta_query as $q ) {
			$meta_key = isset( $q['key'] ) ? trim( $q['key'] ) : '';
			$meta_value = isset( $q['value'] ) ? $q['value'] : '';
			$meta_compare = isset( $q['compare'] ) ? strtoupper( $q['compare'] ) : '=';
			$meta_type = isset( $q['type'] ) ? strtoupper( $q['type'] ) : 'CHAR';

			if ( ! in_array( $meta_compare, array( '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) )
				$meta_compare = '=';

			if ( 'NUMERIC' == $meta_type )
				$meta_type = 'SIGNED';
			elseif ( ! in_array( $meta_type, array( 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED' ) ) )
				$meta_type = 'CHAR';

			if ( empty( $meta_key ) && empty( $meta_value ) )
				continue;

			$alias = $i ? 'mt' . $i : $meta_table;

			$join .= "\nINNER JOIN $meta_table";
			$join .= $i ? " AS $alias" : '';
			$join .= " ON ($primary_table.$primary_id_column = $alias.$meta_id_column)";

			$i++;

			if ( !empty( $meta_key ) )
				$where .= $wpdb->prepare( " AND $alias.meta_key = %s", $meta_key );

			if ( in_array( $meta_compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) {
				if ( ! is_array( $meta_value ) )
					$meta_value = preg_split( '/[,\s]+/', $meta_value );
			} else {
				$meta_value = trim( $meta_value );
			}

			if ( empty( $meta_value ) )
				continue;

			if ( 'IN' == substr( $meta_compare, -2) ) {
				$meta_field_types = substr( str_repeat( ',%s', count( $meta_value ) ), 1 );
				$meta_compare_string = "($meta_field_types)";
				unset( $meta_field_types );
			} elseif ( 'BETWEEN' == substr( $meta_compare, -7) ) {
				$meta_value = array_slice( $meta_value, 0, 2 );
				$meta_compare_string = '%s AND %s';
			} elseif ( 'LIKE' == substr( $meta_compare, -4 ) ) {
				$meta_value = '%' . like_escape( $meta_value ) . '%';
				$meta_compare_string = '%s';
			} else {
				$meta_compare_string = '%s';
			}
			$where .= $wpdb->prepare( " AND CAST($alias.meta_value AS {$meta_type}) {$meta_compare} {$meta_compare_string}", $meta_value );

			unset( $meta_compare_string );
		}

		return apply_filters( 'get_meta_sql', compact( 'join', 'where' ), $meta_query, $primary_table, $primary_id_column, $meta_table, $meta_id_column );
	}

	/*
	 * Used internally to generate an SQL string for searching across multiple taxonomies
	 *
	 * @access protected
	 * @since 3.1.0
	 *
	 * @param array $tax_query List of taxonomy queries. A single taxonomy query is an associative array:
	 * - 'taxonomy' string|array The taxonomy being queried
	 * - 'terms' string|array The list of terms
	 * - 'field' string (optional) Which term field is being used.
	 *		Possible values: 'term_id', 'slug' or 'name'
	 *		Default: 'slug'
	 * - 'operator' string (optional)
	 *		Possible values: 'IN' and 'NOT IN'.
	 *		Default: 'IN'
	 * - 'include_children' bool (optional) Whether to include child terms.
	 *		Default: true
	 *
	 * @param string $object_id_column
	 * @return string
	 */
	function get_tax_sql( $tax_query, $object_id_column ) {
		global $wpdb;

		$sql = array();
		foreach ( $tax_query as $query ) {
			if ( !isset( $query['include_children'] ) )
				$query['include_children'] = true;

			$query['do_query'] = false;

			$sql_single = get_objects_in_term( $query['terms'], $query['taxonomy'], $query );

			if ( empty( $sql_single ) )
				return ' AND 0 = 1';

			$sql[] = $sql_single;
		}

		if ( 1 == count( $sql ) ) {
			$ids = $wpdb->get_col( $sql[0] );
		} else {
			$r = "SELECT object_id FROM $wpdb->term_relationships WHERE 1=1";
			foreach ( $sql as $query )
				$r .= " AND object_id IN ($query)";

			$ids = $wpdb->get_col( $r );
		}

		if ( !empty( $ids ) )
			return " AND $object_id_column IN(" . implode( ', ', $ids ) . ")";
		else
			return ' AND 0 = 1';
	}

	/*
	 * 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) . ')';
	}
}

?>