| 1 | <?php |
| 2 | /** |
| 3 | * WP_Option_Query class |
| 4 | * |
| 5 | * @package Plugins/Queries/Options |
| 6 | * @since 4.8.0 |
| 7 | */ |
| 8 | |
| 9 | /** |
| 10 | * Core class used for querying options tables. |
| 11 | * |
| 12 | * @since 4.8.0 |
| 13 | * |
| 14 | * @see WP_Option_Query::__construct() for accepted arguments. |
| 15 | */ |
| 16 | class WP_Option_Query { |
| 17 | |
| 18 | /** |
| 19 | * SQL for database query. |
| 20 | * |
| 21 | * @since 4.8.0 |
| 22 | * @access public |
| 23 | * @var string |
| 24 | */ |
| 25 | public $request; |
| 26 | |
| 27 | /** |
| 28 | * SQL query clauses. |
| 29 | * |
| 30 | * @since 4.8.0 |
| 31 | * @access protected |
| 32 | * @var array |
| 33 | */ |
| 34 | protected $sql_clauses = array( |
| 35 | 'select' => '', |
| 36 | 'from' => '', |
| 37 | 'where' => array(), |
| 38 | 'groupby' => '', |
| 39 | 'orderby' => '', |
| 40 | 'limits' => '', |
| 41 | ); |
| 42 | |
| 43 | /** |
| 44 | * Query vars set by the user. |
| 45 | * |
| 46 | * @since 4.8.0 |
| 47 | * @access public |
| 48 | * @var array |
| 49 | */ |
| 50 | public $query_vars; |
| 51 | |
| 52 | /** |
| 53 | * Default values for query vars. |
| 54 | * |
| 55 | * @since 4.8.0 |
| 56 | * @access public |
| 57 | * @var array |
| 58 | */ |
| 59 | public $query_var_defaults; |
| 60 | |
| 61 | /** |
| 62 | * List of options located by the query. |
| 63 | * |
| 64 | * @since 4.8.0 |
| 65 | * @access public |
| 66 | * @var array |
| 67 | */ |
| 68 | public $options = array(); |
| 69 | |
| 70 | /** |
| 71 | * The amount of found options for the current query. |
| 72 | * |
| 73 | * @since 4.8.0 |
| 74 | * @access public |
| 75 | * @var int |
| 76 | */ |
| 77 | public $found_options = 0; |
| 78 | |
| 79 | /** |
| 80 | * The number of pages. |
| 81 | * |
| 82 | * @since 4.8.0 |
| 83 | * @access public |
| 84 | * @var int |
| 85 | */ |
| 86 | public $max_num_pages = 0; |
| 87 | |
| 88 | /** |
| 89 | * The database object |
| 90 | * |
| 91 | * @since 4.8.0 |
| 92 | * |
| 93 | * @var WPDB |
| 94 | */ |
| 95 | private $db; |
| 96 | |
| 97 | /** |
| 98 | * Sets up the option query, based on the query vars passed. |
| 99 | * |
| 100 | * @since 4.8.0 |
| 101 | * @access public |
| 102 | * |
| 103 | * @param string|array $query { |
| 104 | * Optional. Array or query string of option query parameters. Default empty. |
| 105 | * |
| 106 | * @type int $option_id An option ID to only return that option. Default empty. |
| 107 | * @type array $option_id__in Array of option IDs to include. Default empty. |
| 108 | * @type array $option_id__not_in Array of option IDs to exclude. Default empty. |
| 109 | * @type string $name Limit results to those affiliated with a given name. |
| 110 | * Default empty. |
| 111 | * @type array $name__in Array of types to include affiliated names for. Default empty. |
| 112 | * @type array $name__not_in Array of types to exclude affiliated names for. Default empty. |
| 113 | * @type string $value Limit results to those affiliated with a given value. |
| 114 | * Default empty. |
| 115 | * @type array $value__in Array of types to include affiliated value for. Default empty. |
| 116 | * @type array $value__not_in Array of types to exclude affiliated value for. Default empty. |
| 117 | * @type string $autoload Limit results to those affiliated with a given autoload. |
| 118 | * Default yes. |
| 119 | * @type array $autoload__in Array of types to include affiliated autoload for. Default empty. |
| 120 | * @type array $autoload__not_in Array of types to exclude affiliated autoload for. Default empty. |
| 121 | * @type bool $count Whether to return a option count (true) or array of option objects. |
| 122 | * Default false. |
| 123 | * @type string $fields Site fields to return. Accepts 'names' (returns an array of option names) |
| 124 | * or empty (returns an array of complete option objects). Default empty. |
| 125 | * @type int $number Maximum number of options to retrieve. Default null (no limit). |
| 126 | * @type int $offset Number of options to offset the query. Used to build LIMIT clause. |
| 127 | * Default 0. |
| 128 | * @type bool $no_found_rows Whether to disable the `SQL_CALC_FOUND_ROWS` query. Default true. |
| 129 | * @type string|array $orderby Site status or array of statuses. Accepts 'option_id', 'name', 'value' |
| 130 | * Also accepts false, an empty array, or 'none' to disable `ORDER BY` clause. |
| 131 | * Default 'option_id'. |
| 132 | * @type string $order How to order retrieved options. Accepts 'ASC', 'DESC'. Default 'ASC'. |
| 133 | * @type string $search Search term(s) to retrieve matching options for. Default empty. |
| 134 | * @type array $search_columns Array of column names to be searched. Accepts 'name', 'value'. |
| 135 | * Default empty array. |
| 136 | * |
| 137 | * @type bool $update_option_cache Whether to prime the cache for found options. Default false. |
| 138 | * } |
| 139 | */ |
| 140 | public function __construct( $query = array() ) { |
| 141 | $this->query_var_defaults = array( |
| 142 | 'fields' => '', |
| 143 | 'option_id' => '', |
| 144 | 'option_id__in' => '', |
| 145 | 'option_id__not_in' => '', |
| 146 | 'name' => '', |
| 147 | 'name__in' => '', |
| 148 | 'name__not_in' => '', |
| 149 | 'value' => '', |
| 150 | 'value__in' => '', |
| 151 | 'value__not_in' => '', |
| 152 | 'autoload' => '', |
| 153 | 'autoload__in' => '', |
| 154 | 'autoload__not_in' => '', |
| 155 | 'number' => null, |
| 156 | 'offset' => '', |
| 157 | 'orderby' => 'option_id', |
| 158 | 'order' => 'ASC', |
| 159 | 'search' => '', |
| 160 | 'search_columns' => array(), |
| 161 | 'count' => false, |
| 162 | 'no_found_rows' => true, |
| 163 | 'meta_query' => array(), |
| 164 | 'update_option_cache' => true |
| 165 | ); |
| 166 | |
| 167 | $this->query( $query ); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Parses arguments passed to the option query with default query parameters. |
| 172 | * |
| 173 | * @since 4.8.0 |
| 174 | * @access public |
| 175 | * |
| 176 | * @see WP_Option_Query::__construct() |
| 177 | * |
| 178 | * @param string|array $query Array or string of WP_Option_Query arguments. See WP_Option_Query::__construct(). |
| 179 | */ |
| 180 | public function parse_query( $query = array() ) { |
| 181 | if ( empty( $query ) ) { |
| 182 | $query = $this->query_vars; |
| 183 | } |
| 184 | |
| 185 | $this->query_vars = wp_parse_args( $query, $this->query_var_defaults ); |
| 186 | |
| 187 | /** |
| 188 | * Fires after the option query vars have been parsed. |
| 189 | * |
| 190 | * @since 4.8.0 |
| 191 | * |
| 192 | * @param WP_Option_Query &$this The WP_Option_Query instance (passed by reference). |
| 193 | */ |
| 194 | do_action_ref_array( 'parse_option_query', array( &$this ) ); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Sets up the WordPress query for retrieving options. |
| 199 | * |
| 200 | * @since 4.8.0 |
| 201 | * @access public |
| 202 | * |
| 203 | * @param string|array $query Array or URL query string of parameters. |
| 204 | * @return array|int List of options, or number of options when 'count' is passed as a query var. |
| 205 | */ |
| 206 | public function query( $query ) { |
| 207 | $this->query_vars = wp_parse_args( $query ); |
| 208 | |
| 209 | return $this->get_results(); |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Retrieves a list of options matching the query vars. |
| 214 | * |
| 215 | * @since 4.8.0 |
| 216 | * @access public |
| 217 | * |
| 218 | * @return array|int List of options, or number of options when 'count' is passed as a query var. |
| 219 | */ |
| 220 | public function get_results() { |
| 221 | |
| 222 | // Parse query vars first to help create an accurate cache key below |
| 223 | $this->parse_query(); |
| 224 | |
| 225 | /** |
| 226 | * Fires before options are retrieved. |
| 227 | * |
| 228 | * @since 4.8.0 |
| 229 | * |
| 230 | * @param WP_Option_Query &$this Current instance of WP_Option_Query, passed by reference. |
| 231 | */ |
| 232 | do_action_ref_array( 'pre_get_results', array( &$this ) ); |
| 233 | |
| 234 | // $args can include anything. Only use the args defined in the query_var_defaults to compute the key. |
| 235 | $cache_key = md5( serialize( wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ) ) ); |
| 236 | $use_cache = ( ! wp_installing() || ! is_multisite() ); |
| 237 | |
| 238 | // Look in cache |
| 239 | if ( true === $use_cache ) { |
| 240 | $last_changed = wp_cache_get_last_changed( 'options' ); |
| 241 | $cache_key = "get_options:{$cache_key}:{$last_changed}"; |
| 242 | $cache_value = wp_cache_get( $cache_key, 'options' ); |
| 243 | } else { |
| 244 | $cache_value = false; |
| 245 | } |
| 246 | |
| 247 | // Nothing in (or not using) cache |
| 248 | if ( false === $cache_value ) { |
| 249 | |
| 250 | // Query for options |
| 251 | $this->options = $this->get_options(); |
| 252 | |
| 253 | // Options were found |
| 254 | if ( ! empty( $this->options ) ) { |
| 255 | $this->set_found_options( $this->options ); |
| 256 | } |
| 257 | |
| 258 | // Maybe cache |
| 259 | if ( true === $use_cache ) { |
| 260 | |
| 261 | // Setup array to cache |
| 262 | $cache_value = array( |
| 263 | 'options' => $this->options, |
| 264 | 'found_options' => (int) $this->found_options, |
| 265 | ); |
| 266 | |
| 267 | // Add results to cache to cache |
| 268 | wp_cache_add( $cache_key, $cache_value, 'options' ); |
| 269 | } |
| 270 | |
| 271 | // Cached results found, so set query-object vars |
| 272 | } else { |
| 273 | $this->options = $cache_value['options']; |
| 274 | $this->found_options = (int) $cache_value['found_options']; |
| 275 | } |
| 276 | |
| 277 | // Maybe set max pages if limiting results |
| 278 | if ( ! empty( $this->found_options ) && ! empty( $this->query_vars['number'] ) ) { |
| 279 | $this->max_num_pages = ceil( $this->found_options / $this->query_vars['number'] ); |
| 280 | } |
| 281 | |
| 282 | // If querying for a count only, $options is actually a number |
| 283 | if ( ! empty( $this->query_vars['count'] ) ) { |
| 284 | return (int) $this->options; |
| 285 | } |
| 286 | |
| 287 | // Prime option caches |
| 288 | if ( ( true === $use_cache ) && ( false !== $this->query_vars['update_option_cache'] ) ) { |
| 289 | wp_update_option_caches( $this->options ); |
| 290 | } |
| 291 | |
| 292 | // Fetch full option objects (likely from the primed cache) |
| 293 | $_options = array(); |
| 294 | foreach ( $this->options as $option ) { |
| 295 | $_option = WP_Option::get_instance( $option, $use_cache ); |
| 296 | if ( ! empty( $_option ) ) { |
| 297 | $_options[] = $_option; |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Filters the option query results. |
| 303 | * |
| 304 | * @since 4.8.0 |
| 305 | * |
| 306 | * @param array $results An array of options. |
| 307 | * @param WP_Option_Query &$this Current instance of WP_Option_Query, passed by reference. |
| 308 | */ |
| 309 | $_options = apply_filters_ref_array( 'the_options', array( $_options, &$this ) ); |
| 310 | |
| 311 | // Convert to WP_Option instances. |
| 312 | $this->options = $_options; |
| 313 | |
| 314 | return $this->options; |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * Used internally to get a list of options matching the query vars. |
| 319 | * |
| 320 | * @since 4.8.0 |
| 321 | * @access protected |
| 322 | * |
| 323 | * @return int|array A single count of options if a count query. An array of options if a full query. |
| 324 | */ |
| 325 | protected function get_options() { |
| 326 | global $wpdb; |
| 327 | |
| 328 | // Default ASC/DESC and ORDER BY clause |
| 329 | $order = $this->parse_order( $this->query_vars['order'] ); |
| 330 | $orderby = "option_id {$order}"; |
| 331 | |
| 332 | // Disable ORDER BY with 'none', an empty array, or boolean false. |
| 333 | if ( in_array( $this->query_vars['orderby'], array( 'none', array(), false ), true ) ) { |
| 334 | $orderby = ''; |
| 335 | |
| 336 | // Specific ordering |
| 337 | } elseif ( ! empty( $this->query_vars['orderby'] ) ) { |
| 338 | |
| 339 | // Array or string are supported |
| 340 | $orders_by = is_array( $this->query_vars['orderby'] ) |
| 341 | ? $this->query_vars['orderby'] |
| 342 | : preg_split( '/[,\s]/', $this->query_vars['orderby'] ); |
| 343 | |
| 344 | // Default array to |
| 345 | $orderby_array = array(); |
| 346 | |
| 347 | // Loop through columns to order by |
| 348 | foreach ( $orders_by as $_key => $_value ) { |
| 349 | |
| 350 | // Skip empty values |
| 351 | if ( empty( $_value ) ) { |
| 352 | continue; |
| 353 | } |
| 354 | |
| 355 | // Column ID |
| 356 | if ( is_int( $_key ) ) { |
| 357 | $_orderby = $_value; |
| 358 | $_order = $order; |
| 359 | |
| 360 | // String |
| 361 | } else { |
| 362 | $_orderby = $_key; |
| 363 | $_order = $_value; |
| 364 | } |
| 365 | |
| 366 | // Parse it |
| 367 | $parsed = $this->parse_orderby( $_orderby ); |
| 368 | |
| 369 | // Skip if empty |
| 370 | if ( empty( $parsed ) ) { |
| 371 | continue; |
| 372 | } |
| 373 | |
| 374 | // Add __in clauses together |
| 375 | if ( ( 'option_id__in' === $_orderby ) || ( 'name__in' === $_orderby ) || ( 'value__in' === $_orderby ) ) { |
| 376 | $orderby_array[] = $parsed; |
| 377 | continue; |
| 378 | } |
| 379 | |
| 380 | // ASC/DESC for each ORDER BY |
| 381 | $orderby_array[] = $parsed . ' ' . $this->parse_order( $_order ); |
| 382 | } |
| 383 | |
| 384 | // Concatenate them |
| 385 | $orderby = implode( ', ', $orderby_array ); |
| 386 | } |
| 387 | |
| 388 | // Cast number and offset to ints to sanitize |
| 389 | $number = intval( $this->query_vars['number'] ); |
| 390 | $offset = intval( $this->query_vars['offset'] ); |
| 391 | |
| 392 | // Limit to a specific number |
| 393 | if ( ! empty( $number ) ) { |
| 394 | |
| 395 | // Offset and limit |
| 396 | if ( ! empty( $offset ) ) { |
| 397 | $limits = $offset . ',' . $number; |
| 398 | |
| 399 | // No offset |
| 400 | } else { |
| 401 | $limits = $number; |
| 402 | } |
| 403 | |
| 404 | // No limit |
| 405 | } else { |
| 406 | $limits = ''; |
| 407 | } |
| 408 | |
| 409 | /** fields ************************************************************/ |
| 410 | |
| 411 | // Count or all table columns |
| 412 | $fields = ! empty( $this->query_vars['count'] ) |
| 413 | ? 'COUNT(*)' |
| 414 | : '*'; |
| 415 | |
| 416 | /** option_id *********************************************************/ |
| 417 | |
| 418 | // Parse option IDs for an IN clause. |
| 419 | $option_id = absint( $this->query_vars['option_id'] ); |
| 420 | if ( ! empty( $option_id ) ) { |
| 421 | $this->sql_clauses['where']['option_id'] = $wpdb->prepare( "option_id = %d", $option_id ); |
| 422 | } |
| 423 | |
| 424 | // Parse option IDs for an IN clause. |
| 425 | if ( ! empty( $this->query_vars['option_id__in'] ) ) { |
| 426 | if ( 1 === count( $this->query_vars['option_id__in'] ) ) { |
| 427 | $this->sql_clauses['where']['option_id'] = $wpdb->prepare( "option_id = %d", reset( $this->query_vars['option_id__in'] ) ); |
| 428 | } else { |
| 429 | $this->sql_clauses['where']['option_id__in'] = "option_id IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['option_id__in'] ) ) . ' )'; |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | // Parse option IDs for a NOT IN clause. |
| 434 | if ( ! empty( $this->query_vars['option_id__not_in'] ) ) { |
| 435 | $this->sql_clauses['where']['option_id__not_in'] = "option_id NOT IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['option_id__not_in'] ) ) . ' )'; |
| 436 | } |
| 437 | |
| 438 | /** name **************************************************************/ |
| 439 | |
| 440 | // Parse object names for an IN clause. |
| 441 | if ( ! empty( $this->query_vars['name'] ) ) { |
| 442 | $this->sql_clauses['where']['name'] = $wpdb->prepare( "option_name = %s", $this->query_vars['name'] ); |
| 443 | } |
| 444 | |
| 445 | // Parse object names for an IN clause. |
| 446 | if ( ! empty( $this->query_vars['name__in'] ) ) { |
| 447 | if ( 1 === count( $this->query_vars['name__in'] ) ) { |
| 448 | $this->sql_clauses['where']['name'] = $wpdb->prepare( "option_name = %s", reset( $this->query_vars['name__in'] ) ); |
| 449 | } else { |
| 450 | $this->sql_clauses['where']['name__in'] = "option_name IN ( '" . implode( "','", array_map( 'esc_sql', $this->query_vars['name__in'] ) ) . "' )"; |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | // Parse object names for a NOT IN clause. |
| 455 | if ( ! empty( $this->query_vars['name__not_in'] ) ) { |
| 456 | $this->sql_clauses['where']['name__not_in'] = "option_name NOT IN ( '" . implode( "','", array_map( 'esc_sql', $this->query_vars['name__not_in'] ) ) . "' )"; |
| 457 | } |
| 458 | |
| 459 | /** value *************************************************************/ |
| 460 | |
| 461 | // Parse object values for an IN clause. |
| 462 | if ( ! empty( $this->query_vars['value'] ) ) { |
| 463 | $this->sql_clauses['where']['value'] = $wpdb->prepare( "option_value = %s", $this->query_vars['value'] ); |
| 464 | } |
| 465 | |
| 466 | // Parse object values for an IN clause. |
| 467 | if ( ! empty( $this->query_vars['value__in'] ) ) { |
| 468 | if ( 1 === count( $this->query_vars['value__in'] ) ) { |
| 469 | $this->sql_clauses['where']['value'] = $wpdb->prepare( "option_value = %s", reset( $this->query_vars['value__in'] ) ); |
| 470 | } else { |
| 471 | $this->sql_clauses['where']['value__in'] = "option_value IN ( '" . implode( "','", array_map( 'esc_sql', $this->query_vars['value__in'] ) ) . "' )"; |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | // Parse object values for a NOT IN clause. |
| 476 | if ( ! empty( $this->query_vars['value__not_in'] ) ) { |
| 477 | $this->sql_clauses['where']['value__not_in'] = "option_value NOT IN ( '" . implode( "','", array_map( 'esc_sql', $this->query_vars['value__not_in'] ) ) . "' )"; |
| 478 | } |
| 479 | |
| 480 | /** autoload **********************************************************/ |
| 481 | |
| 482 | // Parse object autoloads for an IN clause. |
| 483 | if ( ! empty( $this->query_vars['autoload'] ) ) { |
| 484 | $this->sql_clauses['where']['autoload'] = $wpdb->prepare( "autoload = %s", $this->query_vars['autoload'] ); |
| 485 | } |
| 486 | |
| 487 | // Parse object autoloads for an IN clause. |
| 488 | if ( ! empty( $this->query_vars['autoload__in'] ) ) { |
| 489 | if ( 1 === count( $this->query_vars['autoload__in'] ) ) { |
| 490 | $this->sql_clauses['where']['autoload'] = $wpdb->prepare( "autoload = %s", reset( $this->query_vars['autoload__in'] ) ); |
| 491 | } else { |
| 492 | $this->sql_clauses['where']['autoload__in'] = "autoload IN ( '" . implode( "','", $wpdb->_escape( $this->query_vars['autoload__in'] ) ) . "' )"; |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | // Parse object autoloads for a NOT IN clause. |
| 497 | if ( ! empty( $this->query_vars['autoload__not_in'] ) ) { |
| 498 | $this->sql_clauses['where']['autoload__not_in'] = "autoload NOT IN ( '" . implode( "','", $wpdb->_escape( $this->query_vars['autoload__not_in'] ) ) . "' )"; |
| 499 | } |
| 500 | |
| 501 | /** Search ************************************************************/ |
| 502 | |
| 503 | // Falsey search strings are ignored. |
| 504 | if ( strlen( $this->query_vars['search'] ) ) { |
| 505 | $search_columns = array(); |
| 506 | |
| 507 | // Searching specific columns |
| 508 | if ( ! empty( $this->query_vars['search_columns'] ) ) { |
| 509 | $search_columns = array_intersect( $this->query_vars['search_columns'], array( 'option_name', 'option_value' ) ); |
| 510 | } |
| 511 | |
| 512 | // Default columns to search by |
| 513 | if ( empty( $search_columns ) ) { |
| 514 | $search_columns = array( 'option_name', 'option_value' ); |
| 515 | } |
| 516 | |
| 517 | /** |
| 518 | * Filters the columns to search in a WP_Option_Query search. |
| 519 | * |
| 520 | * The default columns include 'name' and 'value'. |
| 521 | * |
| 522 | * @since 4.8.0 |
| 523 | * |
| 524 | * @param array $search_columns Array of column names to be searched. |
| 525 | * @param string $search Text being searched. |
| 526 | * @param WP_Option_Query $this The current WP_Option_Query instance. |
| 527 | */ |
| 528 | $search_columns = apply_filters( 'option_search_columns', $search_columns, $this->query_vars['search'], $this ); |
| 529 | |
| 530 | // Get the search query part from columns |
| 531 | $this->sql_clauses['where']['search'] = $this->get_search_sql( $this->query_vars['search'], $search_columns ); |
| 532 | } |
| 533 | |
| 534 | /** where *************************************************************/ |
| 535 | |
| 536 | $where = implode( ' AND ', $this->sql_clauses['where'] ); |
| 537 | |
| 538 | /** Query *************************************************************/ |
| 539 | |
| 540 | $pieces = compact( array( 'fields', 'where', 'orderby', 'limits', 'groupby' ) ); |
| 541 | |
| 542 | /** |
| 543 | * Filters the option query clauses. |
| 544 | * |
| 545 | * @since 4.8.0 |
| 546 | * |
| 547 | * @param array $pieces A compacted array of option query clauses. |
| 548 | * @param WP_Option_Query &$this Current instance of WP_Option_Query, passed by reference. |
| 549 | */ |
| 550 | $clauses = apply_filters_ref_array( 'option_clauses', array( $pieces, &$this ) ); |
| 551 | |
| 552 | // SQL_CALC_FOUND_ROWS |
| 553 | $found_rows = ! empty( $this->query_vars['no_found_rows'] ) |
| 554 | ? '' |
| 555 | : 'SQL_CALC_FOUND_ROWS'; |
| 556 | |
| 557 | // * or COUNT(*)S |
| 558 | $fields = ! empty( $clauses['fields'] ) |
| 559 | ? $clauses['fields'] |
| 560 | : ''; |
| 561 | |
| 562 | // WHERE |
| 563 | $where = ! empty( $clauses['where'] ) |
| 564 | ? "WHERE {$clauses['where']}" |
| 565 | : ''; |
| 566 | |
| 567 | // ORDER BY |
| 568 | $orderby = ! empty( $clauses['orderby'] ) |
| 569 | ? "ORDER BY {$clauses['orderby']}" |
| 570 | : ''; |
| 571 | |
| 572 | // GROUP BY |
| 573 | $groupby = ! empty( $clauses['groupby'] ) |
| 574 | ? "GROUP BY {$clauses['groupby']}" |
| 575 | : ''; |
| 576 | |
| 577 | // LIMIT |
| 578 | $limits = ! empty( $clauses['limits'] ) |
| 579 | ? "LIMIT {$clauses['limits']}" |
| 580 | : ''; |
| 581 | |
| 582 | // Assemble clauses |
| 583 | $this->sql_clauses['select'] = "SELECT {$found_rows} {$fields}"; |
| 584 | $this->sql_clauses['from'] = "FROM {$wpdb->options}"; |
| 585 | $this->sql_clauses['where'] = $where; |
| 586 | $this->sql_clauses['groupby'] = $groupby; |
| 587 | $this->sql_clauses['orderby'] = $orderby; |
| 588 | $this->sql_clauses['limits'] = $limits; |
| 589 | |
| 590 | // Trim spaces off clauses and concatenate into a request |
| 591 | $this->sql_clauses = array_map( 'trim', $this->sql_clauses ); |
| 592 | $this->request = trim( implode( ' ', $this->sql_clauses ) ); |
| 593 | |
| 594 | // Return count or results |
| 595 | return ( $this->query_vars['count'] ) |
| 596 | ? intval( $wpdb->get_var( $this->request ) ) |
| 597 | : $wpdb->get_results( $this->request ); |
| 598 | } |
| 599 | |
| 600 | /** |
| 601 | * Populates found_options and max_num_pages properties for the current query |
| 602 | * if the limit clause was used. |
| 603 | * |
| 604 | * @since 4.8.0 |
| 605 | * @access private |
| 606 | * |
| 607 | * @param array $options Optional array of options |
| 608 | */ |
| 609 | private function set_found_options( $options = array() ) { |
| 610 | global $wpdb; |
| 611 | |
| 612 | if ( ! empty( $this->query_vars['number'] ) && ! empty( $this->query_vars['no_found_rows'] ) ) { |
| 613 | /** |
| 614 | * Filters the query used to retrieve found option count. |
| 615 | * |
| 616 | * @since 4.8.0 |
| 617 | * |
| 618 | * @param string $found_options_query SQL query. Default 'SELECT FOUND_ROWS()'. |
| 619 | * @param WP_Option_Query $option_query The `WP_Option_Query` instance. |
| 620 | */ |
| 621 | $found_options_query = apply_filters( 'found_options_query', 'SELECT FOUND_ROWS()', $this ); |
| 622 | |
| 623 | $this->found_options = (int) $wpdb->get_var( $found_options_query ); |
| 624 | } elseif ( ! empty( $options ) ) { |
| 625 | $this->found_options = count( $options ); |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | /** |
| 630 | * Used internally to generate an SQL string for searching across multiple columns. |
| 631 | * |
| 632 | * @since 4.8.0 |
| 633 | * @access protected |
| 634 | * |
| 635 | * @param string $string Search string. |
| 636 | * @param array $columns Columns to search. |
| 637 | * @return string Search SQL. |
| 638 | */ |
| 639 | protected function get_search_sql( $string = '', $columns = array() ) { |
| 640 | global $wpdb; |
| 641 | |
| 642 | // Bail if parameters are insufficient |
| 643 | if ( empty( $string ) || empty( $columns ) || ! is_array( $columns ) ) { |
| 644 | return ''; |
| 645 | } |
| 646 | |
| 647 | // Default search query |
| 648 | $like = ( false !== strpos( $string, '*' ) ) |
| 649 | ? '%' . implode( '%', array_map( array( $wpdb, 'esc_like' ), explode( '*', $string ) ) ) . '%' |
| 650 | : '%' . $wpdb->esc_like( $string ) . '%'; |
| 651 | |
| 652 | // Default array |
| 653 | $searches = array(); |
| 654 | |
| 655 | // LIKE query parts based on columns |
| 656 | foreach ( $columns as $column ) { |
| 657 | $searches[] = $wpdb->prepare( "{$column} LIKE %s", $like ); |
| 658 | } |
| 659 | |
| 660 | // Concatenate and return |
| 661 | return '(' . implode( ' OR ', array_map( 'trim', $searches ) ) . ')'; |
| 662 | } |
| 663 | |
| 664 | /** |
| 665 | * Parses and sanitizes 'orderby' keys passed to the option query. |
| 666 | * |
| 667 | * @since 4.8.0 |
| 668 | * @access protected |
| 669 | * |
| 670 | * @param string $orderby Meta for the field to order by. |
| 671 | * @return string|false Value to used in the ORDER clause. False otherwise. |
| 672 | */ |
| 673 | protected function parse_orderby( $orderby = '' ) { |
| 674 | |
| 675 | switch ( $orderby ) { |
| 676 | case 'id' : |
| 677 | case 'option_id' : |
| 678 | $parsed = 'option_id'; |
| 679 | break; |
| 680 | case 'name' : |
| 681 | case 'option_name' : |
| 682 | $parsed = 'option_name'; |
| 683 | break; |
| 684 | case 'value' : |
| 685 | case 'option_value' : |
| 686 | $parsed = 'option_value'; |
| 687 | break; |
| 688 | case 'autoload' : |
| 689 | case 'option_autoload' : |
| 690 | $parsed = 'autoload'; |
| 691 | break; |
| 692 | case 'option_id__in' : |
| 693 | $option__in = implode( ',', array_map( 'absint', $this->query_vars['option_id__in'] ) ); |
| 694 | $parsed = "FIELD( option_id, {$option__in} )"; |
| 695 | break; |
| 696 | case 'name__in' : |
| 697 | $option__in = implode( ',', array_map( 'wp_unslash', $this->query_vars['name__in'] ) ); |
| 698 | $parsed = "FIELD( option_name, {$option__in} )"; |
| 699 | break; |
| 700 | case 'value__in' : |
| 701 | $option__in = implode( ',', array_map( 'wp_unslash', $this->query_vars['value__in'] ) ); |
| 702 | $parsed = "FIELD( option_value, {$option__in} )"; |
| 703 | break; |
| 704 | case 'autoload__in' : |
| 705 | $option__in = implode( ',', array_map( 'wp_unslash', $this->query_vars['autoload__in'] ) ); |
| 706 | $parsed = "FIELD( autoload, {$option__in} )"; |
| 707 | break; |
| 708 | default : |
| 709 | $parsed = false; |
| 710 | break; |
| 711 | } |
| 712 | |
| 713 | return $parsed; |
| 714 | } |
| 715 | |
| 716 | /** |
| 717 | * Parses an 'order' query variable and cast it to 'ASC' or 'DESC' as necessary. |
| 718 | * |
| 719 | * @since 4.8.0 |
| 720 | * @access protected |
| 721 | * |
| 722 | * @param string $order The 'order' query variable. |
| 723 | * @return string The sanitized 'order' query variable. |
| 724 | */ |
| 725 | protected function parse_order( $order = '' ) { |
| 726 | |
| 727 | // Default ASC |
| 728 | if ( ! is_string( $order ) || empty( $order ) ) { |
| 729 | return 'ASC'; |
| 730 | } |
| 731 | |
| 732 | // ASC or DESC |
| 733 | return ( 'ASC' === strtoupper( $order ) ) |
| 734 | ? 'ASC' |
| 735 | : 'DESC'; |
| 736 | } |
| 737 | } |