Changeset 16051 for trunk/wp-includes/classes.php
- Timestamp:
- 10/28/2010 04:27:18 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-includes/classes.php
r16018 r16051 526 526 527 527 /** 528 * WordPress Query class.529 *530 * Abstract class for handling advanced queries531 *532 * @package WordPress533 * @since 3.1.0534 */535 class WP_Object_Query {536 537 /**538 * Query vars, after parsing539 *540 * @since 3.1.0541 * @access public542 * @var array543 */544 var $query_vars;545 546 /**547 * Retrieve query variable.548 *549 * @since 3.1.0550 * @access public551 *552 * @param string $query_var Query variable key.553 * @return mixed554 */555 function get( $query_var ) {556 if ( isset( $this->query_vars[$query_var] ) )557 return $this->query_vars[$query_var];558 559 return '';560 }561 562 /**563 * Set query variable.564 *565 * @since 3.1.0566 * @access public567 *568 * @param string $query_var Query variable key.569 * @param mixed $value Query variable value.570 */571 function set( $query_var, $value ) {572 $this->query_vars[ $query_var ] = $value;573 }574 575 /*576 * Populates the $meta_query property577 *578 * @access protected579 * @since 3.1.0580 *581 * @param array $qv The query variables582 */583 function parse_meta_query( &$qv ) {584 $meta_query = array();585 586 // Simple query needs to be first for orderby=meta_value to work correctly587 foreach ( array( 'key', 'value', 'compare', 'type' ) as $key ) {588 if ( !empty( $qv[ "meta_$key" ] ) )589 $meta_query[0][ $key ] = $qv[ "meta_$key" ];590 }591 592 if ( !empty( $qv['meta_query'] ) && is_array( $qv['meta_query'] ) ) {593 $meta_query = array_merge( $meta_query, $qv['meta_query'] );594 }595 596 $qv['meta_query'] = $meta_query;597 }598 599 /*600 * Used internally to generate an SQL string for searching across multiple meta key = value pairs601 *602 * @access protected603 * @since 3.1.0604 *605 * @param array $meta_query List of metadata queries. A single query is an associative array:606 * - 'key' string The meta key607 * - 'value' string|array The meta value608 * - 'compare' (optional) string How to compare the key to the value.609 * Possible values: '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'.610 * Default: '='611 * - 'type' string (optional) The type of the value.612 * Possible values: 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED'.613 * Default: 'CHAR'614 *615 * @param string $primary_table616 * @param string $primary_id_column617 * @param string $meta_table618 * @param string $meta_id_column619 * @return array( $join_sql, $where_sql )620 */621 function get_meta_sql( $meta_query, $primary_table, $primary_id_column, $meta_table, $meta_id_column ) {622 global $wpdb;623 624 $clauses = array();625 626 $join = '';627 $where = '';628 $i = 0;629 foreach ( $meta_query as $q ) {630 $meta_key = isset( $q['key'] ) ? trim( $q['key'] ) : '';631 $meta_value = isset( $q['value'] ) ? $q['value'] : '';632 $meta_compare = isset( $q['compare'] ) ? strtoupper( $q['compare'] ) : '=';633 $meta_type = isset( $q['type'] ) ? strtoupper( $q['type'] ) : 'CHAR';634 635 if ( ! in_array( $meta_compare, array( '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) )636 $meta_compare = '=';637 638 if ( 'NUMERIC' == $meta_type )639 $meta_type = 'SIGNED';640 elseif ( ! in_array( $meta_type, array( 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED' ) ) )641 $meta_type = 'CHAR';642 643 if ( empty( $meta_key ) && empty( $meta_value ) )644 continue;645 646 $alias = $i ? 'mt' . $i : $meta_table;647 648 $join .= "\nINNER JOIN $meta_table";649 $join .= $i ? " AS $alias" : '';650 $join .= " ON ($primary_table.$primary_id_column = $alias.$meta_id_column)";651 652 $i++;653 654 if ( !empty( $meta_key ) )655 $where .= $wpdb->prepare( " AND $alias.meta_key = %s", $meta_key );656 657 if ( in_array( $meta_compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) {658 if ( ! is_array( $meta_value ) )659 $meta_value = preg_split( '/[,\s]+/', $meta_value );660 } else {661 $meta_value = trim( $meta_value );662 }663 664 if ( empty( $meta_value ) )665 continue;666 667 if ( 'IN' == substr( $meta_compare, -2) ) {668 $meta_field_types = substr( str_repeat( ',%s', count( $meta_value ) ), 1 );669 $meta_compare_string = "($meta_field_types)";670 unset( $meta_field_types );671 } elseif ( 'BETWEEN' == substr( $meta_compare, -7) ) {672 $meta_value = array_slice( $meta_value, 0, 2 );673 $meta_compare_string = '%s AND %s';674 } elseif ( 'LIKE' == substr( $meta_compare, -4 ) ) {675 $meta_value = '%' . like_escape( $meta_value ) . '%';676 $meta_compare_string = '%s';677 } else {678 $meta_compare_string = '%s';679 }680 $where .= $wpdb->prepare( " AND CAST($alias.meta_value AS {$meta_type}) {$meta_compare} {$meta_compare_string}", $meta_value );681 unset($meta_compare_string);682 }683 684 return array( $join, $where );685 }686 687 /*688 * Used internally to generate an SQL string for searching across multiple taxonomies689 *690 * @access protected691 * @since 3.1.0692 *693 * @param array $tax_query List of taxonomy queries. A single taxonomy query is an associative array:694 * - 'taxonomy' string|array The taxonomy being queried695 * - 'terms' string|array The list of terms696 * - 'field' string (optional) Which term field is being used.697 * Possible values: 'term_id', 'slug' or 'name'698 * Default: 'slug'699 * - 'operator' string (optional)700 * Possible values: 'IN' and 'NOT IN'.701 * Default: 'IN'702 * - 'include_children' bool (optional) Whether to include child terms.703 * Default: true704 *705 * @param string $object_id_column706 * @return string707 */708 function get_tax_sql( $tax_query, $object_id_column ) {709 global $wpdb;710 711 $sql = array();712 foreach ( $tax_query as $query ) {713 if ( !isset( $query['include_children'] ) )714 $query['include_children'] = true;715 716 $query['do_query'] = false;717 718 $sql_single = get_objects_in_term( $query['terms'], $query['taxonomy'], $query );719 720 if ( empty( $sql_single ) )721 return ' AND 0 = 1';722 723 $sql[] = $sql_single;724 }725 726 if ( 1 == count( $sql ) ) {727 $ids = $wpdb->get_col( $sql[0] );728 } else {729 $r = "SELECT object_id FROM $wpdb->term_relationships WHERE 1=1";730 foreach ( $sql as $query )731 $r .= " AND object_id IN ($query)";732 733 $ids = $wpdb->get_col( $r );734 }735 736 if ( !empty( $ids ) )737 return " AND $object_id_column IN(" . implode( ', ', $ids ) . ")";738 else739 return ' AND 0 = 1';740 }741 742 /*743 * Used internally to generate an SQL string for searching across multiple columns744 *745 * @access protected746 * @since 3.1.0747 *748 * @param string $string749 * @param array $cols750 * @return string751 */752 function get_search_sql( $string, $cols ) {753 $string = esc_sql( $string );754 755 $searches = array();756 foreach ( $cols as $col )757 $searches[] = "$col LIKE '%$string%'";758 759 return ' AND (' . implode(' OR ', $searches) . ')';760 }761 }762 763 /**764 528 * WordPress Error class. 765 529 *
Note: See TracChangeset
for help on using the changeset viewer.