wpml-tf-feedback-query.php
6.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
/**
* Class WPML_TF_Feedback_Query
*
* @author OnTheGoSystems
*/
class WPML_TF_Feedback_Query {
/** @var WPML_TF_Data_Object_Storage $feedback_storage */
private $feedback_storage;
/** @var WPML_TF_Data_Object_Storage $message_storage */
private $message_storage;
/** @var WPML_TF_Collection_Filter_Factory $collection_filter_factory */
private $collection_filter_factory;
/** @var WPML_TF_Feedback_Collection $unfiltered_feedback_collection */
private $unfiltered_feedback_collection;
/** @var int $unfiltered_items_count */
private $unfiltered_items_count;
/** @var int $trashed_items_count */
private $trashed_items_count;
/** @var int $total_items_count */
private $total_items_count;
/** @var int $filtered_items_count */
private $filtered_items_count;
/** @var bool $is_in_trash */
private $is_in_trash = false;
/**
* WPML_TF_Feedback_Collection_Factory constructor.
*
* @param WPML_TF_Data_Object_Storage $feedback_storage
* @param WPML_TF_Data_Object_Storage $message_storage
* @param WPML_TF_Collection_Filter_Factory $collection_filter_factory
*/
public function __construct(
WPML_TF_Data_Object_Storage $feedback_storage,
WPML_TF_Data_Object_Storage $message_storage,
WPML_TF_Collection_Filter_Factory $collection_filter_factory
) {
$this->feedback_storage = $feedback_storage;
$this->message_storage = $message_storage;
$this->collection_filter_factory = $collection_filter_factory;
}
/**
* @return WPML_TF_Feedback_Collection
*/
public function get_unfiltered_collection() {
if ( ! $this->unfiltered_feedback_collection ) {
$storage_filters = array(
'exclude_rating_only' => true,
);
if ( ! current_user_can( 'manage_options' ) ) {
$storage_filters['reviewer_id'] = get_current_user_id();
}
$feedback_collection_filter = $this->collection_filter_factory->create( 'feedback', $storage_filters );
$this->unfiltered_feedback_collection = $this->feedback_storage->get_collection( $feedback_collection_filter );
$this->unfiltered_items_count = count( $this->unfiltered_feedback_collection );
}
return $this->unfiltered_feedback_collection;
}
/**
* @param array $args
*
* @return WPML_TF_Feedback_Collection
*/
public function get( array $args ) {
$feedback_collection = clone $this->get_unfiltered_collection();
$feedback_collection = $this->trash_filter_collection( $feedback_collection, $args );
$feedback_collection = $this->filter_collection( $feedback_collection, $args );
$feedback_collection = $this->sort_collection( $feedback_collection, $args );
$feedback_collection = $this->apply_pagination( $feedback_collection, $args );
$message_filter_args = array( 'feedback_ids' => $feedback_collection->get_ids() );
$message_collection_filter = $this->collection_filter_factory->create( 'message', $message_filter_args );
/** @var WPML_TF_Message_Collection $message_collection */
$message_collection = $this->message_storage->get_collection( $message_collection_filter );
$feedback_collection->link_messages_to_feedback( $message_collection );
return $feedback_collection;
}
/**
* @param WPML_TF_Feedback_Collection $feedback_collection
* @param array $args
*
* @return WPML_TF_Feedback_Collection
*/
public function trash_filter_collection( WPML_TF_Feedback_Collection $feedback_collection, array $args ) {
if ( isset( $args['status'] ) && 'trash' === $args['status'] ) {
$this->is_in_trash = true;
$feedback_collection->filter_by( 'status', 'trash' );
$this->trashed_items_count = count( $feedback_collection );
$this->total_items_count = $this->unfiltered_items_count - $this->trashed_items_count;
} else {
$feedback_collection->remove_trashed();
$this->total_items_count = count( $feedback_collection );
$this->trashed_items_count = $this->unfiltered_items_count - $this->total_items_count;
}
return $feedback_collection;
}
/**
* @param WPML_TF_Feedback_Collection $feedback_collection
* @param array $args
*
* @return WPML_TF_Feedback_Collection
*/
private function filter_collection( WPML_TF_Feedback_Collection $feedback_collection, array $args ) {
if ( isset( $args['status'] ) && 'trash' !== $args['status'] ) {
$feedback_collection->filter_by( 'status', $args['status'] );
} elseif ( isset( $args['language'] ) ) {
$feedback_collection->filter_by( 'language', $args['language'] );
} elseif ( isset( $args['post_id'] ) ) {
$feedback_collection->filter_by( 'post_id', $args['post_id'] );
}
$this->filtered_items_count = count( $feedback_collection );
return $feedback_collection;
}
/**
* @param WPML_TF_Feedback_Collection $feedback_collection
* @param array $args
*
* @return WPML_TF_Feedback_Collection
*/
private function sort_collection( WPML_TF_Feedback_Collection $feedback_collection, array $args ) {
$order_by = 'pending';
$order = 'desc';
if ( isset( $args['orderby'] ) ) {
$order_by = $args['orderby'];
}
if ( isset( $args['order'] ) ) {
$order = $args['order'];
}
$feedback_collection->sort_collection( $order_by, $order );
return $feedback_collection;
}
/**
* @param WPML_TF_Feedback_Collection $feedback_collection
* @param array $args
*
* @return WPML_TF_Feedback_Collection
*/
private function apply_pagination( WPML_TF_Feedback_Collection $feedback_collection, array $args ) {
if ( isset( $args['paged'], $args['items_per_page'] ) ) {
$offset = $args['items_per_page'] * max( 0, $args['paged'] - 1 );
$feedback_collection->reduce_collection( $offset, $args['items_per_page'] );
}
return $feedback_collection;
}
/**
* @return int
*/
public function get_total_items_count() {
return $this->total_items_count;
}
/** @return int */
public function get_total_trashed_items_count() {
return $this->trashed_items_count;
}
/** @return int */
public function get_filtered_items_count() {
return $this->filtered_items_count;
}
/** @return bool */
public function is_in_trash() {
return $this->is_in_trash;
}
/**
* @param int $feedback_id
* @param bool $with_messages
*
* @return null|\IWPML_TF_Data_Object
*/
public function get_one( $feedback_id, $with_messages = true ) {
$feedback = $this->feedback_storage->get( $feedback_id );
if ( $feedback && $with_messages ) {
$filter_args = array(
'feedback_id' => $feedback_id,
);
$filter = new WPML_TF_Message_Collection_Filter( $filter_args );
$messages = $this->message_storage->get_collection( $filter );
foreach ( $messages as $message ) {
$feedback->add_message( $message );
}
}
return $feedback;
}
}