template-tags
2 weeks ago
multibyte.php
2 weeks ago
query.php
2 weeks ago
url.php
2 weeks ago
utils.php
2 weeks ago
query.php
98 lines
| 1 | <?php |
| 2 | /** |
| 3 | * A set of functions to manipulate queries or query properties. |
| 4 | * |
| 5 | * @since 4.9.5 |
| 6 | */ |
| 7 | |
| 8 | if ( ! function_exists( 'tribe_filter_meta_query' ) ) { |
| 9 | /** |
| 10 | * Removes meta query entries based on key and value. |
| 11 | * |
| 12 | * Example usage to remove all date-related meta queries, using a regular expression: |
| 13 | * |
| 14 | * $query->meta_query = tribe_filter_meta_query( |
| 15 | * $args['meta_query'], |
| 16 | * array( 'key' => '/_Event(Start|End)Date(UTC)/' ) |
| 17 | * ); |
| 18 | * |
| 19 | * @since 4.9.5 |
| 20 | * |
| 21 | * @param array $meta_query The meta query array to filter, usually the content of the `$query->meta_query` |
| 22 | * property. |
| 23 | * @param array $where A map of criteria for the filtering that will be applied with OR logic: if an |
| 24 | * entry matches even one then it will be removed. If the value of the comparison is a |
| 25 | * regular expression, with fences, then it will be used for a `preg_match` check against |
| 26 | * the key, not a simple comparison. |
| 27 | * |
| 28 | * @return array The filtered meta query array. |
| 29 | */ |
| 30 | function tribe_filter_meta_query( array $meta_query, array $where ) { |
| 31 | $filtered = []; |
| 32 | |
| 33 | foreach ( $meta_query as $key => $entry ) { |
| 34 | if ( ! is_array( $entry ) ) { |
| 35 | $filtered[ $key ] = $entry; |
| 36 | continue; |
| 37 | } |
| 38 | |
| 39 | foreach ( $where as $where_key => $where_value ) { |
| 40 | if ( isset( $entry[ $where_key ] ) ) { |
| 41 | if ( tribe_is_regex( $where_value ) ) { |
| 42 | $var = $entry[ $where_key ]; |
| 43 | if ( preg_match( $where_value, $var ) ) { |
| 44 | continue 2; |
| 45 | } |
| 46 | } elseif ( $entry[ $where_key ] == $where_value ) { |
| 47 | continue 2; |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | $filtered[ $key ] = $entry; |
| 52 | } |
| 53 | |
| 54 | return $filtered; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | if ( ! function_exists( 'tribe_normalize_orderby' ) ) { |
| 59 | |
| 60 | /** |
| 61 | * Normalizes an `orderby` string or array to an map of keys and orders. |
| 62 | * |
| 63 | * Note the function and the variables use the "orderby" (no spaces) name to stick |
| 64 | * with the WordPress query standard. |
| 65 | * |
| 66 | * @since 4.12.6 |
| 67 | * |
| 68 | * @param string|array<string,string> $orderby Either an `orderby` key, a list of `orderby` |
| 69 | * keys or a map of `orderby` clauses. |
| 70 | * @param string $order The default order that should be applied to `orderby` entries that |
| 71 | * lack one. |
| 72 | * |
| 73 | * @return array The normalized `orderby` array, in the format supported by WordPress queries: |
| 74 | * `[ <key_1> => <order>, <key_2> => <order>, ... ]`. |
| 75 | */ |
| 76 | function tribe_normalize_orderby( $orderby, $order = 'ASC' ) { |
| 77 | // Make the `orderby` part an array. |
| 78 | $orderby_arr = (array) $orderby; |
| 79 | $normalized = []; |
| 80 | |
| 81 | foreach ( $orderby_arr as $by_key => $direction ) { |
| 82 | if ( empty( $direction ) ) { |
| 83 | continue; |
| 84 | } |
| 85 | |
| 86 | if ( is_numeric( $by_key ) ) { |
| 87 | // It's an entry where the key is just listed, relying on the default order. |
| 88 | $by_key = $direction; |
| 89 | $direction = $order; |
| 90 | } |
| 91 | |
| 92 | $normalized[ $by_key ] = $direction; |
| 93 | } |
| 94 | |
| 95 | return $normalized; |
| 96 | } |
| 97 | } |
| 98 |