| 1 |
<?php |
| 2 |
|
| 3 |
namespace FL\Assistant\Data\Repository; |
| 4 |
|
| 5 |
use FL\Assistant\Data\Transformers\NotationsTransformer; |
| 6 |
|
| 7 |
class NotationsRepository { |
| 8 |
|
| 9 |
protected $transformer; |
| 10 |
|
| 11 |
public function __construct( NotationsTransformer $transformer ) { |
| 12 |
$this->transformer = $transformer; |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* @param array $args |
| 17 |
* |
| 18 |
* @return \WP_Query |
| 19 |
*/ |
| 20 |
public function query( array $args = [] ) { |
| 21 |
$args = array_merge( |
| 22 |
$args, [ |
| 23 |
'post_type' => 'fl_asst_notation', |
| 24 |
] |
| 25 |
); |
| 26 |
|
| 27 |
$query = new \WP_Query( $args ); |
| 28 |
return $this->transformer->transform_array( $query->posts ); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Get array of notations with the corresponding meta values. |
| 33 |
* @return array |
| 34 |
*/ |
| 35 |
public function get_by_meta( $meta ) { |
| 36 |
$meta_query = []; |
| 37 |
|
| 38 |
foreach ( $meta as $key => $value ) { |
| 39 |
$meta_query[] = [ |
| 40 |
'key' => $key, |
| 41 |
'value' => $value, |
| 42 |
'compare' => '=', |
| 43 |
]; |
| 44 |
} |
| 45 |
|
| 46 |
$query = new \WP_Query( |
| 47 |
[ |
| 48 |
'post_type' => 'fl_asst_notation', |
| 49 |
'meta_query' => $meta_query, |
| 50 |
] |
| 51 |
); |
| 52 |
|
| 53 |
return $this->transformer->transform_array( $query->posts ); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Get an array of notations for the given object. |
| 58 |
* @return array |
| 59 |
*/ |
| 60 |
public function get_notations( $object_type, $object_id ) { |
| 61 |
return $this->get_by_meta( |
| 62 |
[ |
| 63 |
'fl_asst_notation_object_type' => $object_type, |
| 64 |
'fl_asst_notation_object_id' => (int) $object_id, |
| 65 |
] |
| 66 |
); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Get an array of favorites for the given object. |
| 71 |
* @return array |
| 72 |
*/ |
| 73 |
public function get_favorites( $object_type, $object_id, $user_id ) { |
| 74 |
return $this->get_by_meta( |
| 75 |
[ |
| 76 |
'fl_asst_notation_type' => 'favorite', |
| 77 |
'fl_asst_notation_object_type' => $object_type, |
| 78 |
'fl_asst_notation_object_id' => (int) $object_id, |
| 79 |
'fl_asst_notation_user_id' => (int) $user_id, |
| 80 |
] |
| 81 |
); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Get an array of labels for the given object. |
| 86 |
* @return array |
| 87 |
*/ |
| 88 |
public function get_labels( $object_type, $object_id ) { |
| 89 |
return $this->get_by_meta( |
| 90 |
[ |
| 91 |
'fl_asst_notation_type' => 'label', |
| 92 |
'fl_asst_notation_object_type' => $object_type, |
| 93 |
'fl_asst_notation_object_id' => (int) $object_id, |
| 94 |
] |
| 95 |
); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Get an array of label notations for the given label ID. |
| 100 |
* @return array |
| 101 |
*/ |
| 102 |
public function get_labels_by_id( $object_type, $label_id ) { |
| 103 |
return $this->get_by_meta( |
| 104 |
[ |
| 105 |
'fl_asst_notation_type' => 'label', |
| 106 |
'fl_asst_notation_object_type' => $object_type, |
| 107 |
'fl_asst_notation_label_id' => (int) $label_id, |
| 108 |
] |
| 109 |
); |
| 110 |
} |
| 111 |
} |
| 112 |
|