| 1 |
<?php |
| 2 |
|
| 3 |
class WP_Stream_Query { |
| 4 |
|
| 5 |
public static $instance; |
| 6 |
|
| 7 |
/** |
| 8 |
* @return WP_Stream_Query |
| 9 |
*/ |
| 10 |
public static function instance() { |
| 11 |
if ( ! self::$instance ) { |
| 12 |
$class = __CLASS__; |
| 13 |
self::$instance = new $class; |
| 14 |
} |
| 15 |
|
| 16 |
return self::$instance; |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Query Stream records |
| 21 |
* |
| 22 |
* @param array Query args |
| 23 |
* |
| 24 |
* @return array Stream Records |
| 25 |
*/ |
| 26 |
public function query( $args ) { |
| 27 |
global $wpdb; |
| 28 |
|
| 29 |
$defaults = array( |
| 30 |
// Search param |
| 31 |
'search' => null, |
| 32 |
'search_field' => 'summary', |
| 33 |
'record_after' => null, // Deprecated, use date_after instead |
| 34 |
// Date-based filters |
| 35 |
'date' => null, // Ex: 2014-02-17 |
| 36 |
'date_from' => null, // Ex: 2014-02-17 |
| 37 |
'date_to' => null, // Ex: 2014-02-17 |
| 38 |
'date_after' => null, // Ex: 2014-02-17T15:19:21+00:00 |
| 39 |
'date_before' => null, // Ex: 2014-02-17T15:19:21+00:00 |
| 40 |
// Record ID filters |
| 41 |
'record' => null, |
| 42 |
'record__in' => array(), |
| 43 |
'record__not_in' => array(), |
| 44 |
// Pagination params |
| 45 |
'records_per_page' => get_option( 'posts_per_page', 20 ), |
| 46 |
'paged' => 1, |
| 47 |
// Order |
| 48 |
'order' => 'desc', |
| 49 |
'orderby' => isset( $args['search'] ) ? '_score' : 'date', |
| 50 |
// Meta/Taxonomy sub queries |
| 51 |
'meta' => array(), |
| 52 |
// Data aggregations |
| 53 |
'aggregations' => array(), |
| 54 |
// Fields selection |
| 55 |
'fields' => null, |
| 56 |
); |
| 57 |
|
| 58 |
// Additional property fields |
| 59 |
$properties = array( |
| 60 |
'type' => 'stream', |
| 61 |
'author' => null, |
| 62 |
'author_role' => null, |
| 63 |
'ip' => null, |
| 64 |
'object_id' => null, |
| 65 |
'site_id' => null, |
| 66 |
'blog_id' => null, |
| 67 |
'visibility' => 'publish', |
| 68 |
'connector' => null, |
| 69 |
'context' => null, |
| 70 |
'action' => null, |
| 71 |
); |
| 72 |
|
| 73 |
/** |
| 74 |
* Filter allows additional query properties to be added |
| 75 |
* |
| 76 |
* @return array Array of query properties |
| 77 |
*/ |
| 78 |
$properties = apply_filters( 'wp_stream_query_properties', $properties ); |
| 79 |
|
| 80 |
// Add property fields to defaults, including their __in/__not_in variations |
| 81 |
foreach ( $properties as $property => $default ) { |
| 82 |
if ( ! isset( $defaults[ $property ] ) ) { |
| 83 |
$defaults[ $property ] = $default; |
| 84 |
} |
| 85 |
$defaults[ "{$property}__in" ] = array(); |
| 86 |
$defaults[ "{$property}__not_in" ] = array(); |
| 87 |
} |
| 88 |
|
| 89 |
$args = wp_parse_args( $args, $defaults ); |
| 90 |
|
| 91 |
/** |
| 92 |
* Filter allows additional arguments to query $args |
| 93 |
* |
| 94 |
* @return array Array of query arguments |
| 95 |
*/ |
| 96 |
$args = apply_filters( 'wp_stream_query_args', $args ); |
| 97 |
|
| 98 |
$query = array(); |
| 99 |
$filters = array(); |
| 100 |
$fields = array(); |
| 101 |
|
| 102 |
// PARSE SEARCH |
| 103 |
if ( $args['search'] ) { |
| 104 |
if ( $args['search_field'] ) { |
| 105 |
$search_field = $args['search_field']; |
| 106 |
$query['query']['match'][ $search_field ] = $args['search']; |
| 107 |
} else { |
| 108 |
$query['query']['match']['summary'] = $args['search']; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
// PARSE FIELDS |
| 113 |
if ( $args['fields'] ) { |
| 114 |
$fields = is_array( $args['fields'] ) ? $args['fields'] : explode( ',', $args['fields'] ); |
| 115 |
} |
| 116 |
|
| 117 |
// PARSE DATE |
| 118 |
if ( $args['date_from'] ) { |
| 119 |
$filters[]['range']['created']['gte'] = wp_stream_get_iso_8601_extended_date( strtotime( $args['date_from'] . ' 00:00:00' ), get_option( 'gmt_offset' ) ); |
| 120 |
} |
| 121 |
|
| 122 |
if ( $args['date_to'] ) { |
| 123 |
$filters[]['range']['created']['lte'] = wp_stream_get_iso_8601_extended_date( strtotime( $args['date_to'] . ' 23:59:59' ), get_option( 'gmt_offset' ) ); |
| 124 |
} |
| 125 |
|
| 126 |
// Support deprecated argument replaced by date_after |
| 127 |
if ( $args['record_after'] && ! $args['date_after'] ) { |
| 128 |
$args['date_after'] = $args['record_after']; |
| 129 |
} |
| 130 |
|
| 131 |
if ( $args['date_after'] ) { |
| 132 |
$filters[]['range']['created']['gt'] = wp_stream_get_iso_8601_extended_date( strtotime( $args['date_after'] ) ); |
| 133 |
} |
| 134 |
|
| 135 |
if ( $args['date_before'] ) { |
| 136 |
$filters[]['range']['created']['lt'] = wp_stream_get_iso_8601_extended_date( strtotime( $args['date_before'] ) ); |
| 137 |
} |
| 138 |
|
| 139 |
if ( $args['date'] ) { |
| 140 |
$filters[]['range']['created'] = array( |
| 141 |
'gte' => wp_stream_get_iso_8601_extended_date( strtotime( $args['date'] . ' 00:00:00' ), get_option( 'gmt_offset' ) ), |
| 142 |
'lte' => wp_stream_get_iso_8601_extended_date( strtotime( $args['date'] . ' 23:59:59' ), get_option( 'gmt_offset' ) ), |
| 143 |
); |
| 144 |
} |
| 145 |
|
| 146 |
// PARSE RECORD |
| 147 |
if ( $args['record'] ) { |
| 148 |
$filters[]['ids']['values'] = array( $args['record'] ); |
| 149 |
} |
| 150 |
|
| 151 |
if ( $args['record__in'] && is_array( $args['record__in'] ) ) { |
| 152 |
$values = is_array( $args['record__in'] ) ? $args['record__in'] : array_map( 'trim', explode( ',', $args['record__in'] ) ); |
| 153 |
|
| 154 |
$filters[]['ids']['values'] = $values; |
| 155 |
} |
| 156 |
|
| 157 |
if ( $args['record__not_in'] && is_array( $args['record__not_in'] ) ) { |
| 158 |
$values = is_array( $args['record__not_in'] ) ? $args['record__not_in'] : array_map( 'trim', explode( ',', $args['record__not_in'] ) ); |
| 159 |
|
| 160 |
$filters[]['not']['ids']['values'] = $values; |
| 161 |
} |
| 162 |
|
| 163 |
// PARSE PROPERTIES |
| 164 |
foreach ( $properties as $property => $default ) { |
| 165 |
if ( $args[ $property ] ) { |
| 166 |
$filters[]['term'][ $property ] = $args[ $property ]; |
| 167 |
} |
| 168 |
|
| 169 |
if ( $args[ "{$property}__in" ] ) { |
| 170 |
$values = is_array( $args[ "{$property}__in" ] ) ? $args[ "{$property}__in" ] : array_map( 'trim', explode( ',', $args[ "{$property}__in" ] ) ); |
| 171 |
$property_in = array(); |
| 172 |
foreach ( $values as $value ) { |
| 173 |
$property_in[]['term'][ $property ] = $value; |
| 174 |
} |
| 175 |
$filters[]['or'] = $property_in; |
| 176 |
} |
| 177 |
|
| 178 |
if ( $args[ "{$property}__not_in" ] ) { |
| 179 |
$values = is_array( $args[ "{$property}__not_in" ] ) ? $args[ "{$property}__not_in" ] : array_map( 'trim', explode( ',', $args[ "{$property}__not_in" ] ) ); |
| 180 |
$property_not_in = array(); |
| 181 |
foreach ( $values as $value ) { |
| 182 |
$property_not_in[]['not']['term'][ $property ] = $value; |
| 183 |
} |
| 184 |
$filters[]['or'] = $property_not_in; |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
// PARSE PAGINATION |
| 189 |
if ( $args['records_per_page'] ) { |
| 190 |
if ( $args['records_per_page'] >= 0 ) { |
| 191 |
$query['size'] = (int) $args['records_per_page']; |
| 192 |
} else { |
| 193 |
$query['size'] = 999999; // Actual limit placed on "unlimited" results |
| 194 |
} |
| 195 |
} else { |
| 196 |
$query['size'] = get_option( 'posts_per_page', 20 ); |
| 197 |
} |
| 198 |
|
| 199 |
if ( $args['paged'] ) { |
| 200 |
$query['from'] = ( (int) $args['paged'] - 1 ) * $query['size']; |
| 201 |
} |
| 202 |
|
| 203 |
// PARSE ORDER |
| 204 |
$query['sort'] = array(); |
| 205 |
|
| 206 |
$orderby = ! empty( $args['orderby'] ) ? $args['orderby'] : 'created'; |
| 207 |
$order = ! empty( $args['order'] ) ? $args['order'] : 'desc'; |
| 208 |
|
| 209 |
if ( 'date' === $orderby ) { |
| 210 |
$orderby = 'created'; |
| 211 |
} |
| 212 |
|
| 213 |
$query['sort'][][ $orderby ]['order'] = $order; |
| 214 |
|
| 215 |
// PARSE META |
| 216 |
if ( $args['meta'] ) { |
| 217 |
$meta = (array) $args['meta']; |
| 218 |
foreach ( $meta as $key => $values ) { |
| 219 |
if ( ! is_array( $values ) ) { |
| 220 |
$values = (array) $values; |
| 221 |
} |
| 222 |
$filters[]['nested'] = array( |
| 223 |
'path' => 'stream_meta', |
| 224 |
'filter' => array( |
| 225 |
'terms' => array( |
| 226 |
$key => $values, |
| 227 |
), |
| 228 |
), |
| 229 |
); |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
// PARSE AGGREGATIONS |
| 234 |
if ( ! empty( $args['aggregations'] ) ) { |
| 235 |
foreach ( $args['aggregations'] as $aggregation_term ) { |
| 236 |
$query['aggregations'][ $aggregation_term ]['terms']['field'] = $aggregation_term; |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
// Add filters to query |
| 241 |
if ( ! empty( $filters ) ) { |
| 242 |
if ( count( $filters ) > 1 ) { |
| 243 |
$query['filter']['and'] = $filters; |
| 244 |
} else { |
| 245 |
$query['filter'] = current( $filters ); |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* Filter allows the final query args to be modified |
| 251 |
* |
| 252 |
* @return array Array of query arguments |
| 253 |
*/ |
| 254 |
$query = apply_filters( 'wp_stream_db_query', $query ); |
| 255 |
|
| 256 |
/** |
| 257 |
* Filter allows the final query fields to be modified |
| 258 |
* |
| 259 |
* @return array Array of query fields |
| 260 |
*/ |
| 261 |
$fields = apply_filters( 'wp_stream_db_fields', $fields ); |
| 262 |
|
| 263 |
/** |
| 264 |
* Query results |
| 265 |
* |
| 266 |
* @var array |
| 267 |
*/ |
| 268 |
return WP_Stream::$db->query( $query, $fields ); |
| 269 |
} |
| 270 |
} |
| 271 |
|