| 1 |
<?php |
| 2 |
/** |
| 3 |
* Query_Helpers. |
| 4 |
* |
| 5 |
* @package WCPOS\WooCommercePOS |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WCPOS\WooCommercePOS\API\V1\Traits; |
| 9 |
|
| 10 |
use WCPOS\WooCommercePOS\Logger; |
| 11 |
|
| 12 |
trait Query_Helpers { |
| 13 |
/** |
| 14 |
* Combine two meta_query arrays. |
| 15 |
* |
| 16 |
* @deprecated The only caller, Customers_Controller, now has its own private |
| 17 |
* wcpos_merge_meta_queries(). Kept because this trait is aliased as |
| 18 |
* public API in includes/API/class-aliases.php, so callers outside |
| 19 |
* this repository may still use it. |
| 20 |
* |
| 21 |
* @param array $meta_query1 First meta query array. |
| 22 |
* @param array $meta_query2 Second meta query array. |
| 23 |
* |
| 24 |
* @return array Combined meta query array. |
| 25 |
*/ |
| 26 |
public function wcpos_combine_meta_queries( $meta_query1, $meta_query2 ) { |
| 27 |
// If either meta_query is empty, return the other. |
| 28 |
if ( empty( $meta_query1 ) ) { |
| 29 |
return $meta_query2; |
| 30 |
} |
| 31 |
if ( empty( $meta_query2 ) ) { |
| 32 |
return $meta_query1; |
| 33 |
} |
| 34 |
|
| 35 |
// Check if both meta_queries have 'AND' as their top-level relation. |
| 36 |
if ( isset( $meta_query1['relation'] ) && 'AND' === $meta_query1['relation'] && |
| 37 |
isset( $meta_query2['relation'] ) && 'AND' === $meta_query2['relation'] ) { |
| 38 |
// Remove the 'relation' element and combine the arrays. |
| 39 |
unset( $meta_query1['relation'], $meta_query2['relation'] ); |
| 40 |
$combined = array_merge( $meta_query1, $meta_query2 ); |
| 41 |
array_unshift( $combined, array( 'relation' => 'AND' ) ); |
| 42 |
return $combined; |
| 43 |
} |
| 44 |
|
| 45 |
// If both meta_queries are not empty and do not both have 'AND', combine them with 'AND' relation. |
| 46 |
return array( |
| 47 |
'relation' => 'AND', |
| 48 |
$meta_query1, |
| 49 |
$meta_query2, |
| 50 |
); |
| 51 |
} |
| 52 |
} |
| 53 |
|