vendor
10 years ago
admin.php
9 years ago
class-wp-stream-author.php
10 years ago
connector.php
9 years ago
connectors.php
9 years ago
context-query.php
10 years ago
dashboard.php
10 years ago
date-interval.php
10 years ago
db.php
9 years ago
filter-input.php
10 years ago
functions.php
10 years ago
install.php
9 years ago
list-table.php
9 years ago
live-update.php
9 years ago
log.php
10 years ago
network.php
10 years ago
query.php
9 years ago
settings.php
9 years ago
context-query.php
133 lines
| 1 | <?php |
| 2 | |
| 3 | class MainWP_WP_Stream_Context_Query { |
| 4 | |
| 5 | public $relation = null; |
| 6 | |
| 7 | function __construct( $query = false ) { |
| 8 | if ( $query ) { |
| 9 | $this->parse_query_vars( $query ); |
| 10 | } |
| 11 | } |
| 12 | |
| 13 | function parse_query_vars( $query ) { |
| 14 | $context_query = array(); |
| 15 | |
| 16 | // Check for 'flat' query params |
| 17 | foreach ( array( 'connector', 'context', 'action' ) as $key ) { |
| 18 | foreach ( array( '', '__in', '__not_in' ) as $i => $suffix ) { |
| 19 | $lookup = array( '=', 'IN', 'NOT IN' ); |
| 20 | $compare = $lookup[ $i ]; |
| 21 | if ( ! empty( $query[ $key . $suffix ] ) ) { |
| 22 | $context_query[] = array( |
| 23 | $key => array( |
| 24 | 'value' => $query[ $key . $suffix ], |
| 25 | 'compare' => $compare, |
| 26 | ), |
| 27 | ); |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | if ( ! empty( $query['context_query'] ) ) { |
| 33 | $context_query = array_merge( $context_query, $query['context_query'] ); |
| 34 | } |
| 35 | |
| 36 | if ( isset( $context_query['relation'] ) && 'OR' === strtoupper( $context_query['relation'] ) ) { |
| 37 | $this->relation = 'OR'; |
| 38 | } else { |
| 39 | $this->relation = 'AND'; |
| 40 | } |
| 41 | |
| 42 | $this->queries = $context_query; |
| 43 | } |
| 44 | |
| 45 | function get_sql() { |
| 46 | global $wpdb; |
| 47 | |
| 48 | if ( empty( $this->queries ) ) { |
| 49 | return array( 'join' => '', 'where' => '' ); |
| 50 | } |
| 51 | |
| 52 | $context_table = MainWP_WP_Stream_DB::$table_context; |
| 53 | $main_table = MainWP_WP_Stream_DB::$table; |
| 54 | $meta_id_column = 'meta_id'; |
| 55 | |
| 56 | $join = array(); |
| 57 | $where = array(); |
| 58 | |
| 59 | $queries = $this->queries; |
| 60 | |
| 61 | $meta_query = new WP_Meta_Query; |
| 62 | |
| 63 | foreach ( $queries as $i => $query ) { |
| 64 | foreach ( $query as $key => $args ) { |
| 65 | $type = $meta_query->get_cast_for_type( isset( $args['type'] ) ? $args['type'] : '' ); |
| 66 | |
| 67 | $value = isset( $args['value'] ) ? $args['value'] : null; |
| 68 | |
| 69 | // Allow 'context' => array('val1', 'val2') as well |
| 70 | if ( is_null( $value ) ) { |
| 71 | $args = array( 'value' => $args ); |
| 72 | $value = $args['value']; |
| 73 | } |
| 74 | |
| 75 | if ( isset( $args['compare'] ) ) { |
| 76 | $compare = strtoupper( $args['compare'] ); |
| 77 | } else { |
| 78 | $compare = is_array( $value ) ? 'IN' : '='; |
| 79 | } |
| 80 | |
| 81 | $operators = array( |
| 82 | '=', |
| 83 | '!=', |
| 84 | 'LIKE', |
| 85 | 'NOT LIKE', |
| 86 | 'IN', |
| 87 | 'NOT IN', |
| 88 | 'REGEXP', |
| 89 | 'NOT REGEXP', |
| 90 | 'RLIKE', |
| 91 | ); |
| 92 | |
| 93 | if ( ! in_array( $compare, $operators ) ) { |
| 94 | $compare = '='; |
| 95 | } |
| 96 | |
| 97 | if ( 'IN' === substr( $compare, -2 ) ) { |
| 98 | if ( ! is_array( $value ) ) { |
| 99 | $value = preg_split( '/[,\s]+/', $value ); |
| 100 | } |
| 101 | $compare_string = '(' . substr( str_repeat( ',%s', count( $value ) ), 1 ) . ')'; |
| 102 | } elseif ( 'LIKE' === substr( $compare, -4 ) ) { |
| 103 | $value = '%' . like_escape( $value ) . '%'; |
| 104 | $compare_string = '%s'; |
| 105 | } else { |
| 106 | $compare_string = '%s'; |
| 107 | } |
| 108 | |
| 109 | if ( ! empty( $where[ $i ] ) ) { |
| 110 | $where[ $i ] .= ' AND '; |
| 111 | } else { |
| 112 | $where[ $i ] = ''; |
| 113 | } |
| 114 | |
| 115 | $where[ $i ] = ' (' . $where[ $i ] . $wpdb->prepare( "CAST($context_table.$key AS {$type}) {$compare} {$compare_string})", $value ); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | $where = array_filter( $where ); |
| 120 | |
| 121 | if ( empty( $where ) ) { |
| 122 | $where = ''; |
| 123 | } else { |
| 124 | $where = ' AND (' . implode( "\n{$this->relation} ", $where ) . ' )'; |
| 125 | } |
| 126 | |
| 127 | $join = implode( "\n", $join ); |
| 128 | |
| 129 | return apply_filters_ref_array( 'get_context_sql', array( compact( 'join', 'where' ), $this->queries ) ); |
| 130 | } |
| 131 | |
| 132 | } |
| 133 |