@@ -20,27 +20,27 @@ discard block |
||
20 | 20 | * @return array |
21 | 21 | * @todo verify the additional type checks are needed |
22 | 22 | */ |
23 | - public function buildCounts(array $args = []) |
|
23 | + public function buildCounts(array $args = []) |
|
24 | 24 | { |
25 | 25 | $counts = []; |
26 | - $query = $this->queryReviews($args); |
|
27 | - while ($query) { |
|
26 | + $query = $this->queryReviews($args); |
|
27 | + while ($query) { |
|
28 | 28 | // glsr_log($query); |
29 | - $types = array_keys(array_flip(glsr_array_column($query->reviews, 'type'))); |
|
30 | - $types = array_unique(array_merge(['local'], $types)); |
|
31 | - foreach ($types as $type) { |
|
32 | - $type = $this->normalizeType($type); |
|
33 | - if (isset($counts[$type])) { |
|
34 | - continue; |
|
29 | + $types = array_keys(array_flip(glsr_array_column($query->reviews, 'type'))); |
|
30 | + $types = array_unique(array_merge(['local'], $types)); |
|
31 | + foreach ($types as $type) { |
|
32 | + $type = $this->normalizeType($type); |
|
33 | + if (isset($counts[$type])) { |
|
34 | + continue; |
|
35 | 35 | } |
36 | - $counts[$type] = array_fill_keys(range(0, glsr()->constant('MAX_RATING', Rating::class)), 0); |
|
37 | - } |
|
38 | - foreach ($query->reviews as $review) { |
|
39 | - $type = $this->normalizeType($review->type); |
|
40 | - ++$counts[$type][$review->rating]; |
|
36 | + $counts[$type] = array_fill_keys(range(0, glsr()->constant('MAX_RATING', Rating::class)), 0); |
|
37 | + } |
|
38 | + foreach ($query->reviews as $review) { |
|
39 | + $type = $this->normalizeType($review->type); |
|
40 | + ++$counts[$type][$review->rating]; |
|
41 | 41 | } |
42 | 42 | $query = $query->has_more |
43 | - ? $this->queryReviews($args, end($query->reviews)->ID) |
|
43 | + ? $this->queryReviews($args, end($query->reviews)->ID) |
|
44 | 44 | : false; |
45 | 45 | } |
46 | 46 | return $counts; |
@@ -50,18 +50,18 @@ discard block |
||
50 | 50 | * @param int $postId |
51 | 51 | * @return array |
52 | 52 | */ |
53 | - public function buildPostCounts($postId) |
|
53 | + public function buildPostCounts($postId) |
|
54 | 54 | { |
55 | - return $this->buildCounts(['post_ids' => [$postId]]); |
|
55 | + return $this->buildCounts(['post_ids' => [$postId]]); |
|
56 | 56 | } |
57 | 57 | |
58 | 58 | /** |
59 | 59 | * @param int $termTaxonomyId |
60 | 60 | * @return array |
61 | 61 | */ |
62 | - public function buildTermCounts($termTaxonomyId) |
|
62 | + public function buildTermCounts($termTaxonomyId) |
|
63 | 63 | { |
64 | - return $this->buildCounts(['term_ids' => [$termTaxonomyId]]); |
|
64 | + return $this->buildCounts(['term_ids' => [$termTaxonomyId]]); |
|
65 | 65 | } |
66 | 66 | |
67 | 67 | /** |
@@ -69,33 +69,33 @@ discard block |
||
69 | 69 | */ |
70 | 70 | public function countAll() |
71 | 71 | { |
72 | - $terms = glsr(Database::class)->getTerms(['fields' => 'all']); |
|
73 | - foreach ($terms as $term) { |
|
74 | - $this->setTermCounts($term->term_id, $this->buildTermCounts($term->term_taxonomy_id)); |
|
72 | + $terms = glsr(Database::class)->getTerms(['fields' => 'all']); |
|
73 | + foreach ($terms as $term) { |
|
74 | + $this->setTermCounts($term->term_id, $this->buildTermCounts($term->term_taxonomy_id)); |
|
75 | 75 | } |
76 | - $postIds = glsr(SqlQueries::class)->getReviewsMeta('assigned_to'); |
|
77 | - foreach ($postIds as $postId) { |
|
78 | - $this->setPostCounts($postId, $this->buildPostCounts($postId)); |
|
76 | + $postIds = glsr(SqlQueries::class)->getReviewsMeta('assigned_to'); |
|
77 | + foreach ($postIds as $postId) { |
|
78 | + $this->setPostCounts($postId, $this->buildPostCounts($postId)); |
|
79 | 79 | } |
80 | - $this->setCounts($this->buildCounts()); |
|
80 | + $this->setCounts($this->buildCounts()); |
|
81 | 81 | } |
82 | 82 | |
83 | 83 | /** |
84 | 84 | * @return void |
85 | 85 | */ |
86 | - public function decrease(Review $review) |
|
86 | + public function decrease(Review $review) |
|
87 | 87 | { |
88 | - $this->decreaseCounts($review); |
|
89 | - $this->decreasePostCounts($review); |
|
90 | - $this->decreaseTermCounts($review); |
|
88 | + $this->decreaseCounts($review); |
|
89 | + $this->decreasePostCounts($review); |
|
90 | + $this->decreaseTermCounts($review); |
|
91 | 91 | } |
92 | 92 | |
93 | 93 | /** |
94 | 94 | * @return void |
95 | 95 | */ |
96 | - public function decreaseCounts(Review $review) |
|
96 | + public function decreaseCounts(Review $review) |
|
97 | 97 | { |
98 | - $this->setCounts($this->decreaseRating( |
|
98 | + $this->setCounts($this->decreaseRating( |
|
99 | 99 | $this->getCounts(), |
100 | 100 | $review->review_type, |
101 | 101 | $review->rating |
@@ -105,46 +105,46 @@ discard block |
||
105 | 105 | /** |
106 | 106 | * @return void |
107 | 107 | */ |
108 | - public function decreasePostCounts(Review $review) |
|
108 | + public function decreasePostCounts(Review $review) |
|
109 | 109 | { |
110 | - if (empty($counts = $this->getPostCounts($review->assigned_to))) { |
|
111 | - return; |
|
112 | - } |
|
113 | - $counts = $this->decreaseRating($counts, $review->review_type, $review->rating); |
|
114 | - $this->setPostCounts($review->assigned_to, $counts); |
|
110 | + if (empty($counts = $this->getPostCounts($review->assigned_to))) { |
|
111 | + return; |
|
112 | + } |
|
113 | + $counts = $this->decreaseRating($counts, $review->review_type, $review->rating); |
|
114 | + $this->setPostCounts($review->assigned_to, $counts); |
|
115 | 115 | } |
116 | 116 | |
117 | 117 | /** |
118 | 118 | * @return void |
119 | 119 | */ |
120 | - public function decreaseTermCounts(Review $review) |
|
120 | + public function decreaseTermCounts(Review $review) |
|
121 | 121 | { |
122 | - foreach ($review->term_ids as $termId) { |
|
123 | - if (empty($counts = $this->getTermCounts($termId))) { |
|
124 | - continue; |
|
125 | - } |
|
126 | - $counts = $this->decreaseRating($counts, $review->review_type, $review->rating); |
|
127 | - $this->setTermCounts($termId, $counts); |
|
122 | + foreach ($review->term_ids as $termId) { |
|
123 | + if (empty($counts = $this->getTermCounts($termId))) { |
|
124 | + continue; |
|
125 | + } |
|
126 | + $counts = $this->decreaseRating($counts, $review->review_type, $review->rating); |
|
127 | + $this->setTermCounts($termId, $counts); |
|
128 | 128 | } |
129 | 129 | } |
130 | 130 | |
131 | 131 | /** |
132 | 132 | * @return array |
133 | 133 | */ |
134 | - public function flatten(array $reviewCounts, array $args = []) |
|
134 | + public function flatten(array $reviewCounts, array $args = []) |
|
135 | 135 | { |
136 | 136 | $counts = []; |
137 | - array_walk_recursive($reviewCounts, function ($num, $index) use (&$counts) { |
|
138 | - $counts[$index] = $num + intval(glsr_get($counts, $index, 0)); |
|
137 | + array_walk_recursive($reviewCounts, function ($num, $index) use (&$counts) { |
|
138 | + $counts[$index] = $num + intval(glsr_get($counts, $index, 0)); |
|
139 | 139 | }); |
140 | - $args = wp_parse_args($args, [ |
|
141 | - 'max' => glsr()->constant('MAX_RATING', Rating::class), |
|
142 | - 'min' => glsr()->constant('MIN_RATING', Rating::class), |
|
140 | + $args = wp_parse_args($args, [ |
|
141 | + 'max' => glsr()->constant('MAX_RATING', Rating::class), |
|
142 | + 'min' => glsr()->constant('MIN_RATING', Rating::class), |
|
143 | 143 | ]); |
144 | - foreach ($counts as $index => &$num) { |
|
145 | - if ($index >= intval($args['min']) && $index <= intval($args['max'])) { |
|
146 | - continue; |
|
147 | - } |
|
144 | + foreach ($counts as $index => &$num) { |
|
145 | + if ($index >= intval($args['min']) && $index <= intval($args['max'])) { |
|
146 | + continue; |
|
147 | + } |
|
148 | 148 | $num = 0; |
149 | 149 | } |
150 | 150 | return $counts; |
@@ -153,26 +153,26 @@ discard block |
||
153 | 153 | /** |
154 | 154 | * @return array |
155 | 155 | */ |
156 | - public function get(array $args = []) |
|
156 | + public function get(array $args = []) |
|
157 | 157 | { |
158 | - $args = $this->normalizeArgs($args); |
|
158 | + $args = $this->normalizeArgs($args); |
|
159 | 159 | $counts = []; |
160 | - if ($this->isMixedCount($args)) { |
|
161 | - $counts = [$this->buildCounts($args)]; // force query the database |
|
162 | - } else { |
|
163 | - foreach ($args['post_ids'] as $postId) { |
|
164 | - $counts[] = $this->getPostCounts($postId); |
|
160 | + if ($this->isMixedCount($args)) { |
|
161 | + $counts = [$this->buildCounts($args)]; // force query the database |
|
162 | + } else { |
|
163 | + foreach ($args['post_ids'] as $postId) { |
|
164 | + $counts[] = $this->getPostCounts($postId); |
|
165 | 165 | } |
166 | - foreach ($args['term_ids'] as $termId) { |
|
167 | - $counts[] = $this->getTermCounts($termId); |
|
166 | + foreach ($args['term_ids'] as $termId) { |
|
167 | + $counts[] = $this->getTermCounts($termId); |
|
168 | 168 | } |
169 | - if (empty($counts)) { |
|
169 | + if (empty($counts)) { |
|
170 | 170 | $counts[] = $this->getCounts(); |
171 | 171 | } |
172 | 172 | } |
173 | - return in_array($args['type'], ['', 'all']) |
|
174 | - ? $this->normalize([$this->flatten($counts)]) |
|
175 | - : $this->normalize(glsr_array_column($counts, $args['type'])); |
|
173 | + return in_array($args['type'], ['', 'all']) |
|
174 | + ? $this->normalize([$this->flatten($counts)]) |
|
175 | + : $this->normalize(glsr_array_column($counts, $args['type'])); |
|
176 | 176 | } |
177 | 177 | |
178 | 178 | /** |
@@ -180,9 +180,9 @@ discard block |
||
180 | 180 | */ |
181 | 181 | public function getCounts() |
182 | 182 | { |
183 | - $counts = glsr(OptionManager::class)->get('counts', []); |
|
184 | - if (!is_array($counts)) { |
|
185 | - glsr_log()->error('$counts is not an array')->debug($counts); |
|
183 | + $counts = glsr(OptionManager::class)->get('counts', []); |
|
184 | + if (!is_array($counts)) { |
|
185 | + glsr_log()->error('$counts is not an array')->debug($counts); |
|
186 | 186 | return []; |
187 | 187 | } |
188 | 188 | return $counts; |
@@ -192,105 +192,105 @@ discard block |
||
192 | 192 | * @param int $postId |
193 | 193 | * @return array |
194 | 194 | */ |
195 | - public function getPostCounts($postId) |
|
195 | + public function getPostCounts($postId) |
|
196 | 196 | { |
197 | - return array_filter((array) get_post_meta($postId, static::META_COUNT, true)); |
|
197 | + return array_filter((array) get_post_meta($postId, static::META_COUNT, true)); |
|
198 | 198 | } |
199 | 199 | |
200 | 200 | /** |
201 | 201 | * @param int $termId |
202 | 202 | * @return array |
203 | 203 | */ |
204 | - public function getTermCounts($termId) |
|
204 | + public function getTermCounts($termId) |
|
205 | 205 | { |
206 | - return array_filter((array) get_term_meta($termId, static::META_COUNT, true)); |
|
206 | + return array_filter((array) get_term_meta($termId, static::META_COUNT, true)); |
|
207 | 207 | } |
208 | 208 | |
209 | 209 | /** |
210 | 210 | * @return void |
211 | 211 | */ |
212 | - public function increase(Review $review) |
|
212 | + public function increase(Review $review) |
|
213 | 213 | { |
214 | - $this->increaseCounts($review); |
|
215 | - $this->increasePostCounts($review); |
|
216 | - $this->increaseTermCounts($review); |
|
214 | + $this->increaseCounts($review); |
|
215 | + $this->increasePostCounts($review); |
|
216 | + $this->increaseTermCounts($review); |
|
217 | 217 | } |
218 | 218 | |
219 | 219 | /** |
220 | 220 | * @return void |
221 | 221 | */ |
222 | - public function increaseCounts(Review $review) |
|
222 | + public function increaseCounts(Review $review) |
|
223 | 223 | { |
224 | - if (empty($counts = $this->getCounts())) { |
|
224 | + if (empty($counts = $this->getCounts())) { |
|
225 | 225 | $counts = $this->buildCounts(); |
226 | 226 | } |
227 | - $this->setCounts($this->increaseRating($counts, $review->review_type, $review->rating)); |
|
227 | + $this->setCounts($this->increaseRating($counts, $review->review_type, $review->rating)); |
|
228 | 228 | } |
229 | 229 | |
230 | 230 | /** |
231 | 231 | * @return void |
232 | 232 | */ |
233 | - public function increasePostCounts(Review $review) |
|
233 | + public function increasePostCounts(Review $review) |
|
234 | 234 | { |
235 | - if (!(get_post($review->assigned_to) instanceof WP_Post)) { |
|
236 | - return; |
|
237 | - } |
|
238 | - $counts = $this->getPostCounts($review->assigned_to); |
|
239 | - $counts = empty($counts) |
|
240 | - ? $this->buildPostCounts($review->assigned_to) |
|
241 | - : $this->increaseRating($counts, $review->review_type, $review->rating); |
|
242 | - $this->setPostCounts($review->assigned_to, $counts); |
|
235 | + if (!(get_post($review->assigned_to) instanceof WP_Post)) { |
|
236 | + return; |
|
237 | + } |
|
238 | + $counts = $this->getPostCounts($review->assigned_to); |
|
239 | + $counts = empty($counts) |
|
240 | + ? $this->buildPostCounts($review->assigned_to) |
|
241 | + : $this->increaseRating($counts, $review->review_type, $review->rating); |
|
242 | + $this->setPostCounts($review->assigned_to, $counts); |
|
243 | 243 | } |
244 | 244 | |
245 | 245 | /** |
246 | 246 | * @return void |
247 | 247 | */ |
248 | - public function increaseTermCounts(Review $review) |
|
248 | + public function increaseTermCounts(Review $review) |
|
249 | 249 | { |
250 | - $terms = glsr(ReviewManager::class)->normalizeTerms(implode(',', $review->term_ids)); |
|
251 | - foreach ($terms as $term) { |
|
252 | - $counts = $this->getTermCounts($term['term_id']); |
|
253 | - $counts = empty($counts) |
|
254 | - ? $this->buildTermCounts($term['term_taxonomy_id']) |
|
255 | - : $this->increaseRating($counts, $review->review_type, $review->rating); |
|
256 | - $this->setTermCounts($term['term_id'], $counts); |
|
250 | + $terms = glsr(ReviewManager::class)->normalizeTerms(implode(',', $review->term_ids)); |
|
251 | + foreach ($terms as $term) { |
|
252 | + $counts = $this->getTermCounts($term['term_id']); |
|
253 | + $counts = empty($counts) |
|
254 | + ? $this->buildTermCounts($term['term_taxonomy_id']) |
|
255 | + : $this->increaseRating($counts, $review->review_type, $review->rating); |
|
256 | + $this->setTermCounts($term['term_id'], $counts); |
|
257 | 257 | } |
258 | 258 | } |
259 | 259 | |
260 | 260 | /** |
261 | 261 | * @return void |
262 | 262 | */ |
263 | - public function setCounts(array $reviewCounts) |
|
263 | + public function setCounts(array $reviewCounts) |
|
264 | 264 | { |
265 | - glsr(OptionManager::class)->set('counts', $reviewCounts); |
|
265 | + glsr(OptionManager::class)->set('counts', $reviewCounts); |
|
266 | 266 | } |
267 | 267 | |
268 | 268 | /** |
269 | 269 | * @param int $postId |
270 | 270 | * @return void |
271 | 271 | */ |
272 | - public function setPostCounts($postId, array $reviewCounts) |
|
272 | + public function setPostCounts($postId, array $reviewCounts) |
|
273 | 273 | { |
274 | - $ratingCounts = $this->flatten($reviewCounts); |
|
275 | - update_post_meta($postId, static::META_COUNT, $reviewCounts); |
|
276 | - update_post_meta($postId, static::META_AVERAGE, glsr(Rating::class)->getAverage($ratingCounts)); |
|
277 | - update_post_meta($postId, static::META_RANKING, glsr(Rating::class)->getRanking($ratingCounts)); |
|
274 | + $ratingCounts = $this->flatten($reviewCounts); |
|
275 | + update_post_meta($postId, static::META_COUNT, $reviewCounts); |
|
276 | + update_post_meta($postId, static::META_AVERAGE, glsr(Rating::class)->getAverage($ratingCounts)); |
|
277 | + update_post_meta($postId, static::META_RANKING, glsr(Rating::class)->getRanking($ratingCounts)); |
|
278 | 278 | } |
279 | 279 | |
280 | 280 | /** |
281 | 281 | * @param int $termId |
282 | 282 | * @return void |
283 | 283 | */ |
284 | - public function setTermCounts($termId, array $reviewCounts) |
|
284 | + public function setTermCounts($termId, array $reviewCounts) |
|
285 | 285 | { |
286 | - $term = get_term($termId, Application::TAXONOMY); |
|
287 | - if (!isset($term->term_id)) { |
|
288 | - return; |
|
289 | - } |
|
290 | - $ratingCounts = $this->flatten($reviewCounts); |
|
291 | - update_term_meta($termId, static::META_COUNT, $reviewCounts); |
|
292 | - update_term_meta($termId, static::META_AVERAGE, glsr(Rating::class)->getAverage($ratingCounts)); |
|
293 | - update_term_meta($termId, static::META_RANKING, glsr(Rating::class)->getRanking($ratingCounts)); |
|
286 | + $term = get_term($termId, Application::TAXONOMY); |
|
287 | + if (!isset($term->term_id)) { |
|
288 | + return; |
|
289 | + } |
|
290 | + $ratingCounts = $this->flatten($reviewCounts); |
|
291 | + update_term_meta($termId, static::META_COUNT, $reviewCounts); |
|
292 | + update_term_meta($termId, static::META_AVERAGE, glsr(Rating::class)->getAverage($ratingCounts)); |
|
293 | + update_term_meta($termId, static::META_RANKING, glsr(Rating::class)->getRanking($ratingCounts)); |
|
294 | 294 | } |
295 | 295 | |
296 | 296 | /** |
@@ -298,10 +298,10 @@ discard block |
||
298 | 298 | * @param int $rating |
299 | 299 | * @return array |
300 | 300 | */ |
301 | - protected function decreaseRating(array $reviewCounts, $type, $rating) |
|
301 | + protected function decreaseRating(array $reviewCounts, $type, $rating) |
|
302 | 302 | { |
303 | - if (isset($reviewCounts[$type][$rating])) { |
|
304 | - $reviewCounts[$type][$rating] = max(0, $reviewCounts[$type][$rating] - 1); |
|
303 | + if (isset($reviewCounts[$type][$rating])) { |
|
304 | + $reviewCounts[$type][$rating] = max(0, $reviewCounts[$type][$rating] - 1); |
|
305 | 305 | } |
306 | 306 | return $reviewCounts; |
307 | 307 | } |
@@ -311,43 +311,43 @@ discard block |
||
311 | 311 | * @param int $rating |
312 | 312 | * @return array |
313 | 313 | */ |
314 | - protected function increaseRating(array $reviewCounts, $type, $rating) |
|
314 | + protected function increaseRating(array $reviewCounts, $type, $rating) |
|
315 | 315 | { |
316 | - if (!array_key_exists($type, glsr()->reviewTypes)) { |
|
316 | + if (!array_key_exists($type, glsr()->reviewTypes)) { |
|
317 | 317 | return $reviewCounts; |
318 | 318 | } |
319 | - if (!array_key_exists($type, $reviewCounts)) { |
|
319 | + if (!array_key_exists($type, $reviewCounts)) { |
|
320 | 320 | $reviewCounts[$type] = []; |
321 | 321 | } |
322 | - $reviewCounts = $this->normalize($reviewCounts); |
|
323 | - $reviewCounts[$type][$rating] = intval($reviewCounts[$type][$rating]) + 1; |
|
322 | + $reviewCounts = $this->normalize($reviewCounts); |
|
323 | + $reviewCounts[$type][$rating] = intval($reviewCounts[$type][$rating]) + 1; |
|
324 | 324 | return $reviewCounts; |
325 | 325 | } |
326 | 326 | |
327 | 327 | /** |
328 | 328 | * @return bool |
329 | 329 | */ |
330 | - protected function isMixedCount(array $args) |
|
330 | + protected function isMixedCount(array $args) |
|
331 | 331 | { |
332 | - return !empty($args['post_ids']) && !empty($args['term_ids']); |
|
332 | + return !empty($args['post_ids']) && !empty($args['term_ids']); |
|
333 | 333 | } |
334 | 334 | |
335 | 335 | /** |
336 | 336 | * @return array |
337 | 337 | */ |
338 | - protected function normalize(array $reviewCounts) |
|
338 | + protected function normalize(array $reviewCounts) |
|
339 | 339 | { |
340 | - if (empty($reviewCounts)) { |
|
340 | + if (empty($reviewCounts)) { |
|
341 | 341 | $reviewCounts = [[]]; |
342 | 342 | } |
343 | - foreach ($reviewCounts as &$counts) { |
|
344 | - foreach (range(0, glsr()->constant('MAX_RATING', Rating::class)) as $index) { |
|
345 | - if (isset($counts[$index])) { |
|
346 | - continue; |
|
347 | - } |
|
343 | + foreach ($reviewCounts as &$counts) { |
|
344 | + foreach (range(0, glsr()->constant('MAX_RATING', Rating::class)) as $index) { |
|
345 | + if (isset($counts[$index])) { |
|
346 | + continue; |
|
347 | + } |
|
348 | 348 | $counts[$index] = 0; |
349 | 349 | } |
350 | - ksort($counts); |
|
350 | + ksort($counts); |
|
351 | 351 | } |
352 | 352 | return $reviewCounts; |
353 | 353 | } |
@@ -355,15 +355,15 @@ discard block |
||
355 | 355 | /** |
356 | 356 | * @return array |
357 | 357 | */ |
358 | - protected function normalizeArgs(array $args) |
|
358 | + protected function normalizeArgs(array $args) |
|
359 | 359 | { |
360 | - $args = wp_parse_args(array_filter($args), [ |
|
360 | + $args = wp_parse_args(array_filter($args), [ |
|
361 | 361 | 'post_ids' => [], |
362 | 362 | 'term_ids' => [], |
363 | 363 | 'type' => 'local', |
364 | 364 | ]); |
365 | - $args['post_ids'] = glsr(Polylang::class)->getPostIds($args['post_ids']); |
|
366 | - $args['type'] = $this->normalizeType($args['type']); |
|
365 | + $args['post_ids'] = glsr(Polylang::class)->getPostIds($args['post_ids']); |
|
366 | + $args['type'] = $this->normalizeType($args['type']); |
|
367 | 367 | return $args; |
368 | 368 | } |
369 | 369 | |
@@ -371,9 +371,9 @@ discard block |
||
371 | 371 | * @param string $type |
372 | 372 | * @return string |
373 | 373 | */ |
374 | - protected function normalizeType($type) |
|
374 | + protected function normalizeType($type) |
|
375 | 375 | { |
376 | - return empty($type) || !is_string($type) |
|
376 | + return empty($type) || !is_string($type) |
|
377 | 377 | ? 'local' |
378 | 378 | : $type; |
379 | 379 | } |
@@ -382,11 +382,11 @@ discard block |
||
382 | 382 | * @param int $lastPostId |
383 | 383 | * @return object |
384 | 384 | */ |
385 | - protected function queryReviews(array $args = [], $lastPostId = 0) |
|
385 | + protected function queryReviews(array $args = [], $lastPostId = 0) |
|
386 | 386 | { |
387 | - $reviews = glsr(SqlQueries::class)->getReviewCounts($args, $lastPostId, static::LIMIT); |
|
388 | - $hasMore = is_array($reviews) |
|
389 | - ? count($reviews) == static::LIMIT |
|
387 | + $reviews = glsr(SqlQueries::class)->getReviewCounts($args, $lastPostId, static::LIMIT); |
|
388 | + $hasMore = is_array($reviews) |
|
389 | + ? count($reviews) == static::LIMIT |
|
390 | 390 | : false; |
391 | 391 | return (object) [ |
392 | 392 | 'has_more' => $hasMore, |
@@ -9,65 +9,65 @@ |
||
9 | 9 | |
10 | 10 | class Reviews extends ArrayObject |
11 | 11 | { |
12 | - /** |
|
13 | - * @var array |
|
14 | - */ |
|
15 | - public $args; |
|
12 | + /** |
|
13 | + * @var array |
|
14 | + */ |
|
15 | + public $args; |
|
16 | 16 | |
17 | - /** |
|
18 | - * @var int |
|
19 | - */ |
|
20 | - public $max_num_pages; |
|
17 | + /** |
|
18 | + * @var int |
|
19 | + */ |
|
20 | + public $max_num_pages; |
|
21 | 21 | |
22 | - /** |
|
23 | - * @var array |
|
24 | - */ |
|
25 | - public $reviews; |
|
22 | + /** |
|
23 | + * @var array |
|
24 | + */ |
|
25 | + public $reviews; |
|
26 | 26 | |
27 | - public function __construct(array $reviews, $maxPageCount, array $args) |
|
28 | - { |
|
29 | - $this->args = $args; |
|
30 | - $this->max_num_pages = $maxPageCount; |
|
31 | - $this->reviews = $reviews; |
|
32 | - parent::__construct($reviews, ArrayObject::STD_PROP_LIST | ArrayObject::ARRAY_AS_PROPS); |
|
33 | - } |
|
27 | + public function __construct(array $reviews, $maxPageCount, array $args) |
|
28 | + { |
|
29 | + $this->args = $args; |
|
30 | + $this->max_num_pages = $maxPageCount; |
|
31 | + $this->reviews = $reviews; |
|
32 | + parent::__construct($reviews, ArrayObject::STD_PROP_LIST | ArrayObject::ARRAY_AS_PROPS); |
|
33 | + } |
|
34 | 34 | |
35 | - /** |
|
36 | - * @return string |
|
37 | - */ |
|
38 | - public function __toString() |
|
39 | - { |
|
40 | - return (string) $this->build(); |
|
41 | - } |
|
35 | + /** |
|
36 | + * @return string |
|
37 | + */ |
|
38 | + public function __toString() |
|
39 | + { |
|
40 | + return (string) $this->build(); |
|
41 | + } |
|
42 | 42 | |
43 | - /** |
|
44 | - * @return ReviewsHtml |
|
45 | - */ |
|
46 | - public function build() |
|
47 | - { |
|
48 | - $args = glsr(SiteReviewsDefaults::class)->merge($this->args); |
|
49 | - return glsr(SiteReviewsPartial::class)->build($args, $this); |
|
50 | - } |
|
43 | + /** |
|
44 | + * @return ReviewsHtml |
|
45 | + */ |
|
46 | + public function build() |
|
47 | + { |
|
48 | + $args = glsr(SiteReviewsDefaults::class)->merge($this->args); |
|
49 | + return glsr(SiteReviewsPartial::class)->build($args, $this); |
|
50 | + } |
|
51 | 51 | |
52 | - /** |
|
53 | - * @param mixed $key |
|
54 | - * @return mixed |
|
55 | - */ |
|
56 | - public function offsetGet($key) |
|
57 | - { |
|
58 | - if (property_exists($this, $key)) { |
|
59 | - return $this->{$key}; |
|
60 | - } |
|
61 | - return array_key_exists($key, $this->reviews) |
|
62 | - ? $this->reviews[$key] |
|
63 | - : null; |
|
64 | - } |
|
52 | + /** |
|
53 | + * @param mixed $key |
|
54 | + * @return mixed |
|
55 | + */ |
|
56 | + public function offsetGet($key) |
|
57 | + { |
|
58 | + if (property_exists($this, $key)) { |
|
59 | + return $this->{$key}; |
|
60 | + } |
|
61 | + return array_key_exists($key, $this->reviews) |
|
62 | + ? $this->reviews[$key] |
|
63 | + : null; |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * @return void |
|
68 | - */ |
|
69 | - public function render() |
|
70 | - { |
|
71 | - echo $this->build(); |
|
72 | - } |
|
66 | + /** |
|
67 | + * @return void |
|
68 | + */ |
|
69 | + public function render() |
|
70 | + { |
|
71 | + echo $this->build(); |
|
72 | + } |
|
73 | 73 | } |
@@ -6,83 +6,83 @@ |
||
6 | 6 | |
7 | 7 | class SiteReviewsSummaryBlock extends BlockGenerator |
8 | 8 | { |
9 | - /** |
|
10 | - * @return array |
|
11 | - */ |
|
12 | - public function attributes() |
|
13 | - { |
|
14 | - return [ |
|
15 | - 'assigned_to' => [ |
|
16 | - 'default' => '', |
|
17 | - 'type' => 'string', |
|
18 | - ], |
|
19 | - 'category' => [ |
|
20 | - 'default' => '', |
|
21 | - 'type' => 'string', |
|
22 | - ], |
|
23 | - 'className' => [ |
|
24 | - 'default' => '', |
|
25 | - 'type' => 'string', |
|
26 | - ], |
|
27 | - 'hide' => [ |
|
28 | - 'default' => '', |
|
29 | - 'type' => 'string', |
|
30 | - ], |
|
31 | - 'post_id' => [ |
|
32 | - 'default' => '', |
|
33 | - 'type' => 'string', |
|
34 | - ], |
|
35 | - 'rating' => [ |
|
36 | - 'default' => '1', |
|
37 | - 'type' => 'number', |
|
38 | - ], |
|
39 | - 'schema' => [ |
|
40 | - 'default' => false, |
|
41 | - 'type' => 'boolean', |
|
42 | - ], |
|
43 | - 'type' => [ |
|
44 | - 'default' => 'local', |
|
45 | - 'type' => 'string', |
|
46 | - ], |
|
47 | - ]; |
|
48 | - } |
|
9 | + /** |
|
10 | + * @return array |
|
11 | + */ |
|
12 | + public function attributes() |
|
13 | + { |
|
14 | + return [ |
|
15 | + 'assigned_to' => [ |
|
16 | + 'default' => '', |
|
17 | + 'type' => 'string', |
|
18 | + ], |
|
19 | + 'category' => [ |
|
20 | + 'default' => '', |
|
21 | + 'type' => 'string', |
|
22 | + ], |
|
23 | + 'className' => [ |
|
24 | + 'default' => '', |
|
25 | + 'type' => 'string', |
|
26 | + ], |
|
27 | + 'hide' => [ |
|
28 | + 'default' => '', |
|
29 | + 'type' => 'string', |
|
30 | + ], |
|
31 | + 'post_id' => [ |
|
32 | + 'default' => '', |
|
33 | + 'type' => 'string', |
|
34 | + ], |
|
35 | + 'rating' => [ |
|
36 | + 'default' => '1', |
|
37 | + 'type' => 'number', |
|
38 | + ], |
|
39 | + 'schema' => [ |
|
40 | + 'default' => false, |
|
41 | + 'type' => 'boolean', |
|
42 | + ], |
|
43 | + 'type' => [ |
|
44 | + 'default' => 'local', |
|
45 | + 'type' => 'string', |
|
46 | + ], |
|
47 | + ]; |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * @return string |
|
52 | - */ |
|
53 | - public function render(array $attributes) |
|
54 | - { |
|
55 | - $attributes['class'] = $attributes['className']; |
|
56 | - $shortcode = glsr(Shortcode::class); |
|
57 | - if ('edit' == filter_input(INPUT_GET, 'context')) { |
|
58 | - $attributes = $this->normalize($attributes); |
|
59 | - $this->filterShortcodeClass(); |
|
60 | - if (!$this->hasVisibleFields($shortcode, $attributes)) { |
|
61 | - $this->filterInterpolation(); |
|
62 | - } |
|
63 | - } |
|
64 | - return $shortcode->buildShortcode($attributes); |
|
65 | - } |
|
50 | + /** |
|
51 | + * @return string |
|
52 | + */ |
|
53 | + public function render(array $attributes) |
|
54 | + { |
|
55 | + $attributes['class'] = $attributes['className']; |
|
56 | + $shortcode = glsr(Shortcode::class); |
|
57 | + if ('edit' == filter_input(INPUT_GET, 'context')) { |
|
58 | + $attributes = $this->normalize($attributes); |
|
59 | + $this->filterShortcodeClass(); |
|
60 | + if (!$this->hasVisibleFields($shortcode, $attributes)) { |
|
61 | + $this->filterInterpolation(); |
|
62 | + } |
|
63 | + } |
|
64 | + return $shortcode->buildShortcode($attributes); |
|
65 | + } |
|
66 | 66 | |
67 | - /** |
|
68 | - * @return void |
|
69 | - */ |
|
70 | - protected function filterInterpolation() |
|
71 | - { |
|
72 | - add_filter('site-reviews/interpolate/reviews-summary', function ($context) { |
|
73 | - $context['class'] = 'glsr-default glsr-block-disabled'; |
|
74 | - $context['text'] = __('You have hidden all of the fields for this block.', 'site-reviews'); |
|
75 | - return $context; |
|
76 | - }); |
|
77 | - } |
|
67 | + /** |
|
68 | + * @return void |
|
69 | + */ |
|
70 | + protected function filterInterpolation() |
|
71 | + { |
|
72 | + add_filter('site-reviews/interpolate/reviews-summary', function ($context) { |
|
73 | + $context['class'] = 'glsr-default glsr-block-disabled'; |
|
74 | + $context['text'] = __('You have hidden all of the fields for this block.', 'site-reviews'); |
|
75 | + return $context; |
|
76 | + }); |
|
77 | + } |
|
78 | 78 | |
79 | - /** |
|
80 | - * @return void |
|
81 | - */ |
|
82 | - protected function filterShortcodeClass() |
|
83 | - { |
|
84 | - add_filter('site-reviews/style', function () { |
|
85 | - return 'default'; |
|
86 | - }); |
|
87 | - } |
|
79 | + /** |
|
80 | + * @return void |
|
81 | + */ |
|
82 | + protected function filterShortcodeClass() |
|
83 | + { |
|
84 | + add_filter('site-reviews/style', function () { |
|
85 | + return 'default'; |
|
86 | + }); |
|
87 | + } |
|
88 | 88 | } |
@@ -6,115 +6,115 @@ |
||
6 | 6 | |
7 | 7 | class SiteReviewsFormBlock extends BlockGenerator |
8 | 8 | { |
9 | - /** |
|
10 | - * @return array |
|
11 | - */ |
|
12 | - public function attributes() |
|
13 | - { |
|
14 | - return [ |
|
15 | - 'assign_to' => [ |
|
16 | - 'default' => '', |
|
17 | - 'type' => 'string', |
|
18 | - ], |
|
19 | - 'category' => [ |
|
20 | - 'default' => '', |
|
21 | - 'type' => 'string', |
|
22 | - ], |
|
23 | - 'className' => [ |
|
24 | - 'default' => '', |
|
25 | - 'type' => 'string', |
|
26 | - ], |
|
27 | - 'hide' => [ |
|
28 | - 'default' => '', |
|
29 | - 'type' => 'string', |
|
30 | - ], |
|
31 | - 'id' => [ |
|
32 | - 'default' => '', |
|
33 | - 'type' => 'string', |
|
34 | - ], |
|
35 | - ]; |
|
36 | - } |
|
9 | + /** |
|
10 | + * @return array |
|
11 | + */ |
|
12 | + public function attributes() |
|
13 | + { |
|
14 | + return [ |
|
15 | + 'assign_to' => [ |
|
16 | + 'default' => '', |
|
17 | + 'type' => 'string', |
|
18 | + ], |
|
19 | + 'category' => [ |
|
20 | + 'default' => '', |
|
21 | + 'type' => 'string', |
|
22 | + ], |
|
23 | + 'className' => [ |
|
24 | + 'default' => '', |
|
25 | + 'type' => 'string', |
|
26 | + ], |
|
27 | + 'hide' => [ |
|
28 | + 'default' => '', |
|
29 | + 'type' => 'string', |
|
30 | + ], |
|
31 | + 'id' => [ |
|
32 | + 'default' => '', |
|
33 | + 'type' => 'string', |
|
34 | + ], |
|
35 | + ]; |
|
36 | + } |
|
37 | 37 | |
38 | - /** |
|
39 | - * @return string |
|
40 | - */ |
|
41 | - public function render(array $attributes) |
|
42 | - { |
|
43 | - $attributes['class'] = $attributes['className']; |
|
44 | - $shortcode = glsr(Shortcode::class); |
|
45 | - if ('edit' == filter_input(INPUT_GET, 'context')) { |
|
46 | - $this->filterFormFields(); |
|
47 | - $this->filterRatingField(); |
|
48 | - $this->filterShortcodeClass(); |
|
49 | - $this->filterSubmitButton(); |
|
50 | - if (!$this->hasVisibleFields($shortcode, $attributes)) { |
|
51 | - $this->filterInterpolation(); |
|
52 | - } |
|
53 | - } |
|
54 | - return $shortcode->buildShortcode($attributes); |
|
55 | - } |
|
38 | + /** |
|
39 | + * @return string |
|
40 | + */ |
|
41 | + public function render(array $attributes) |
|
42 | + { |
|
43 | + $attributes['class'] = $attributes['className']; |
|
44 | + $shortcode = glsr(Shortcode::class); |
|
45 | + if ('edit' == filter_input(INPUT_GET, 'context')) { |
|
46 | + $this->filterFormFields(); |
|
47 | + $this->filterRatingField(); |
|
48 | + $this->filterShortcodeClass(); |
|
49 | + $this->filterSubmitButton(); |
|
50 | + if (!$this->hasVisibleFields($shortcode, $attributes)) { |
|
51 | + $this->filterInterpolation(); |
|
52 | + } |
|
53 | + } |
|
54 | + return $shortcode->buildShortcode($attributes); |
|
55 | + } |
|
56 | 56 | |
57 | - /** |
|
58 | - * @return void |
|
59 | - */ |
|
60 | - protected function filterFormFields() |
|
61 | - { |
|
62 | - add_filter('site-reviews/config/forms/submission-form', function (array $config) { |
|
63 | - array_walk($config, function (&$field) { |
|
64 | - $field['disabled'] = true; |
|
65 | - $field['tabindex'] = '-1'; |
|
66 | - }); |
|
67 | - return $config; |
|
68 | - }); |
|
69 | - } |
|
57 | + /** |
|
58 | + * @return void |
|
59 | + */ |
|
60 | + protected function filterFormFields() |
|
61 | + { |
|
62 | + add_filter('site-reviews/config/forms/submission-form', function (array $config) { |
|
63 | + array_walk($config, function (&$field) { |
|
64 | + $field['disabled'] = true; |
|
65 | + $field['tabindex'] = '-1'; |
|
66 | + }); |
|
67 | + return $config; |
|
68 | + }); |
|
69 | + } |
|
70 | 70 | |
71 | - /** |
|
72 | - * @return void |
|
73 | - */ |
|
74 | - protected function filterInterpolation() |
|
75 | - { |
|
76 | - add_filter('site-reviews/interpolate/reviews-form', function ($context) { |
|
77 | - $context['class'] = 'glsr-default glsr-block-disabled'; |
|
78 | - $context['fields'] = __('You have hidden all of the fields for this block.', 'site-reviews'); |
|
79 | - $context['response'] = ''; |
|
80 | - $context['submit_button'] = ''; |
|
81 | - return $context; |
|
82 | - }); |
|
83 | - } |
|
71 | + /** |
|
72 | + * @return void |
|
73 | + */ |
|
74 | + protected function filterInterpolation() |
|
75 | + { |
|
76 | + add_filter('site-reviews/interpolate/reviews-form', function ($context) { |
|
77 | + $context['class'] = 'glsr-default glsr-block-disabled'; |
|
78 | + $context['fields'] = __('You have hidden all of the fields for this block.', 'site-reviews'); |
|
79 | + $context['response'] = ''; |
|
80 | + $context['submit_button'] = ''; |
|
81 | + return $context; |
|
82 | + }); |
|
83 | + } |
|
84 | 84 | |
85 | - /** |
|
86 | - * @return void |
|
87 | - */ |
|
88 | - protected function filterRatingField() |
|
89 | - { |
|
90 | - add_filter('site-reviews/rendered/field', function ($html, $type, $args) { |
|
91 | - if ('rating' == $args['path']) { |
|
92 | - $stars = '<span class="glsr-stars">'; |
|
93 | - $stars.= str_repeat('<span class="glsr-star glsr-star-empty" aria-hidden="true"></span>', 5); |
|
94 | - $stars.= '</span>'; |
|
95 | - $html = preg_replace('/(.*)(<select.*)(<\/select>)(.*)/', '$1'.$stars.'$4', $html); |
|
96 | - } |
|
97 | - return $html; |
|
98 | - }, 10, 3); |
|
99 | - } |
|
85 | + /** |
|
86 | + * @return void |
|
87 | + */ |
|
88 | + protected function filterRatingField() |
|
89 | + { |
|
90 | + add_filter('site-reviews/rendered/field', function ($html, $type, $args) { |
|
91 | + if ('rating' == $args['path']) { |
|
92 | + $stars = '<span class="glsr-stars">'; |
|
93 | + $stars.= str_repeat('<span class="glsr-star glsr-star-empty" aria-hidden="true"></span>', 5); |
|
94 | + $stars.= '</span>'; |
|
95 | + $html = preg_replace('/(.*)(<select.*)(<\/select>)(.*)/', '$1'.$stars.'$4', $html); |
|
96 | + } |
|
97 | + return $html; |
|
98 | + }, 10, 3); |
|
99 | + } |
|
100 | 100 | |
101 | - /** |
|
102 | - * @return void |
|
103 | - */ |
|
104 | - protected function filterShortcodeClass() |
|
105 | - { |
|
106 | - add_filter('site-reviews/style', function () { |
|
107 | - return 'default'; |
|
108 | - }); |
|
109 | - } |
|
101 | + /** |
|
102 | + * @return void |
|
103 | + */ |
|
104 | + protected function filterShortcodeClass() |
|
105 | + { |
|
106 | + add_filter('site-reviews/style', function () { |
|
107 | + return 'default'; |
|
108 | + }); |
|
109 | + } |
|
110 | 110 | |
111 | - /** |
|
112 | - * @return void |
|
113 | - */ |
|
114 | - protected function filterSubmitButton() |
|
115 | - { |
|
116 | - add_filter('site-reviews/rendered/template/form/submit-button', function ($template) { |
|
117 | - return str_replace('type="submit"', 'tabindex="-1"', $template); |
|
118 | - }); |
|
119 | - } |
|
111 | + /** |
|
112 | + * @return void |
|
113 | + */ |
|
114 | + protected function filterSubmitButton() |
|
115 | + { |
|
116 | + add_filter('site-reviews/rendered/template/form/submit-button', function ($template) { |
|
117 | + return str_replace('type="submit"', 'tabindex="-1"', $template); |
|
118 | + }); |
|
119 | + } |
|
120 | 120 | } |
@@ -6,65 +6,65 @@ |
||
6 | 6 | |
7 | 7 | abstract class BlockGenerator |
8 | 8 | { |
9 | - /** |
|
10 | - * @return array |
|
11 | - */ |
|
12 | - public function attributes() |
|
13 | - { |
|
14 | - return []; |
|
15 | - } |
|
9 | + /** |
|
10 | + * @return array |
|
11 | + */ |
|
12 | + public function attributes() |
|
13 | + { |
|
14 | + return []; |
|
15 | + } |
|
16 | 16 | |
17 | - /** |
|
18 | - * @return array |
|
19 | - */ |
|
20 | - public function normalize(array $attributes) |
|
21 | - { |
|
22 | - $hide = array_flip(explode(',', $attributes['hide'])); |
|
23 | - unset($hide['if_empty']); |
|
24 | - $attributes['hide'] = implode(',', array_keys($hide)); |
|
25 | - if (!isset($attributes['assigned_to'])) { |
|
26 | - return $attributes; |
|
27 | - } |
|
28 | - if ('post_id' == $attributes['assigned_to']) { |
|
29 | - $attributes['assigned_to'] = $attributes['post_id']; |
|
30 | - } elseif ('parent_id' == $attributes['assigned_to']) { |
|
31 | - $attributes['assigned_to'] = wp_get_post_parent_id($attributes['post_id']); |
|
32 | - } |
|
33 | - return $attributes; |
|
34 | - } |
|
17 | + /** |
|
18 | + * @return array |
|
19 | + */ |
|
20 | + public function normalize(array $attributes) |
|
21 | + { |
|
22 | + $hide = array_flip(explode(',', $attributes['hide'])); |
|
23 | + unset($hide['if_empty']); |
|
24 | + $attributes['hide'] = implode(',', array_keys($hide)); |
|
25 | + if (!isset($attributes['assigned_to'])) { |
|
26 | + return $attributes; |
|
27 | + } |
|
28 | + if ('post_id' == $attributes['assigned_to']) { |
|
29 | + $attributes['assigned_to'] = $attributes['post_id']; |
|
30 | + } elseif ('parent_id' == $attributes['assigned_to']) { |
|
31 | + $attributes['assigned_to'] = wp_get_post_parent_id($attributes['post_id']); |
|
32 | + } |
|
33 | + return $attributes; |
|
34 | + } |
|
35 | 35 | |
36 | - /** |
|
37 | - * @return void |
|
38 | - */ |
|
39 | - public function register($block) |
|
40 | - { |
|
41 | - if (!function_exists('register_block_type')) { |
|
42 | - return; |
|
43 | - } |
|
44 | - register_block_type(Application::ID.'/'.$block, [ |
|
45 | - 'attributes' => $this->attributes(), |
|
46 | - 'editor_script' => Application::ID.'/blocks', |
|
47 | - 'editor_style' => Application::ID.'/blocks', |
|
48 | - 'render_callback' => [$this, 'render'], |
|
49 | - 'style' => Application::ID, |
|
50 | - ]); |
|
51 | - } |
|
36 | + /** |
|
37 | + * @return void |
|
38 | + */ |
|
39 | + public function register($block) |
|
40 | + { |
|
41 | + if (!function_exists('register_block_type')) { |
|
42 | + return; |
|
43 | + } |
|
44 | + register_block_type(Application::ID.'/'.$block, [ |
|
45 | + 'attributes' => $this->attributes(), |
|
46 | + 'editor_script' => Application::ID.'/blocks', |
|
47 | + 'editor_style' => Application::ID.'/blocks', |
|
48 | + 'render_callback' => [$this, 'render'], |
|
49 | + 'style' => Application::ID, |
|
50 | + ]); |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * @return void |
|
55 | - */ |
|
56 | - abstract public function render(array $attributes); |
|
53 | + /** |
|
54 | + * @return void |
|
55 | + */ |
|
56 | + abstract public function render(array $attributes); |
|
57 | 57 | |
58 | - /** |
|
59 | - * @param mixed $shortcode |
|
60 | - * @return bool |
|
61 | - */ |
|
62 | - protected function hasVisibleFields($shortcode, array $attributes) |
|
63 | - { |
|
64 | - $args = $shortcode->normalizeAtts($attributes); |
|
65 | - $defaults = $shortcode->getHideOptions(); |
|
66 | - $hide = array_flip($args['hide']); |
|
67 | - unset($defaults['if_empty'], $hide['if_empty']); |
|
68 | - return !empty(array_diff_key($defaults, $hide)); |
|
69 | - } |
|
58 | + /** |
|
59 | + * @param mixed $shortcode |
|
60 | + * @return bool |
|
61 | + */ |
|
62 | + protected function hasVisibleFields($shortcode, array $attributes) |
|
63 | + { |
|
64 | + $args = $shortcode->normalizeAtts($attributes); |
|
65 | + $defaults = $shortcode->getHideOptions(); |
|
66 | + $hide = array_flip($args['hide']); |
|
67 | + unset($defaults['if_empty'], $hide['if_empty']); |
|
68 | + return !empty(array_diff_key($defaults, $hide)); |
|
69 | + } |
|
70 | 70 | } |
@@ -6,124 +6,124 @@ |
||
6 | 6 | |
7 | 7 | class SiteReviewsBlock extends BlockGenerator |
8 | 8 | { |
9 | - /** |
|
10 | - * @return array |
|
11 | - */ |
|
12 | - public function attributes() |
|
13 | - { |
|
14 | - return [ |
|
15 | - 'assigned_to' => [ |
|
16 | - 'default' => '', |
|
17 | - 'type' => 'string', |
|
18 | - ], |
|
19 | - 'category' => [ |
|
20 | - 'default' => '', |
|
21 | - 'type' => 'string', |
|
22 | - ], |
|
23 | - 'className' => [ |
|
24 | - 'default' => '', |
|
25 | - 'type' => 'string', |
|
26 | - ], |
|
27 | - 'count' => [ |
|
28 | - 'default' => 5, |
|
29 | - 'type' => 'number', |
|
30 | - ], |
|
31 | - 'hide' => [ |
|
32 | - 'default' => '', |
|
33 | - 'type' => 'string', |
|
34 | - ], |
|
35 | - 'id' => [ |
|
36 | - 'default' => '', |
|
37 | - 'type' => 'string', |
|
38 | - ], |
|
39 | - 'pagination' => [ |
|
40 | - 'default' => '', |
|
41 | - 'type' => 'string', |
|
42 | - ], |
|
43 | - 'post_id' => [ |
|
44 | - 'default' => '', |
|
45 | - 'type' => 'string', |
|
46 | - ], |
|
47 | - 'rating' => [ |
|
48 | - 'default' => 1, |
|
49 | - 'type' => 'number', |
|
50 | - ], |
|
51 | - 'schema' => [ |
|
52 | - 'default' => false, |
|
53 | - 'type' => 'boolean', |
|
54 | - ], |
|
55 | - 'type' => [ |
|
56 | - 'default' => 'local', |
|
57 | - 'type' => 'string', |
|
58 | - ], |
|
59 | - ]; |
|
60 | - } |
|
9 | + /** |
|
10 | + * @return array |
|
11 | + */ |
|
12 | + public function attributes() |
|
13 | + { |
|
14 | + return [ |
|
15 | + 'assigned_to' => [ |
|
16 | + 'default' => '', |
|
17 | + 'type' => 'string', |
|
18 | + ], |
|
19 | + 'category' => [ |
|
20 | + 'default' => '', |
|
21 | + 'type' => 'string', |
|
22 | + ], |
|
23 | + 'className' => [ |
|
24 | + 'default' => '', |
|
25 | + 'type' => 'string', |
|
26 | + ], |
|
27 | + 'count' => [ |
|
28 | + 'default' => 5, |
|
29 | + 'type' => 'number', |
|
30 | + ], |
|
31 | + 'hide' => [ |
|
32 | + 'default' => '', |
|
33 | + 'type' => 'string', |
|
34 | + ], |
|
35 | + 'id' => [ |
|
36 | + 'default' => '', |
|
37 | + 'type' => 'string', |
|
38 | + ], |
|
39 | + 'pagination' => [ |
|
40 | + 'default' => '', |
|
41 | + 'type' => 'string', |
|
42 | + ], |
|
43 | + 'post_id' => [ |
|
44 | + 'default' => '', |
|
45 | + 'type' => 'string', |
|
46 | + ], |
|
47 | + 'rating' => [ |
|
48 | + 'default' => 1, |
|
49 | + 'type' => 'number', |
|
50 | + ], |
|
51 | + 'schema' => [ |
|
52 | + 'default' => false, |
|
53 | + 'type' => 'boolean', |
|
54 | + ], |
|
55 | + 'type' => [ |
|
56 | + 'default' => 'local', |
|
57 | + 'type' => 'string', |
|
58 | + ], |
|
59 | + ]; |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
63 | - * @return string |
|
64 | - */ |
|
65 | - public function render(array $attributes) |
|
66 | - { |
|
67 | - $attributes['class'] = $attributes['className']; |
|
68 | - $shortcode = glsr(Shortcode::class); |
|
69 | - if ('edit' == filter_input(INPUT_GET, 'context')) { |
|
70 | - $attributes = $this->normalize($attributes); |
|
71 | - $this->filterReviewLinks(); |
|
72 | - $this->filterShortcodeClass(); |
|
73 | - $this->filterShowMoreLinks('content'); |
|
74 | - $this->filterShowMoreLinks('response'); |
|
75 | - if (!$this->hasVisibleFields($shortcode, $attributes)) { |
|
76 | - $this->filterInterpolation(); |
|
77 | - } |
|
78 | - } |
|
79 | - return $shortcode->buildShortcode($attributes); |
|
80 | - } |
|
62 | + /** |
|
63 | + * @return string |
|
64 | + */ |
|
65 | + public function render(array $attributes) |
|
66 | + { |
|
67 | + $attributes['class'] = $attributes['className']; |
|
68 | + $shortcode = glsr(Shortcode::class); |
|
69 | + if ('edit' == filter_input(INPUT_GET, 'context')) { |
|
70 | + $attributes = $this->normalize($attributes); |
|
71 | + $this->filterReviewLinks(); |
|
72 | + $this->filterShortcodeClass(); |
|
73 | + $this->filterShowMoreLinks('content'); |
|
74 | + $this->filterShowMoreLinks('response'); |
|
75 | + if (!$this->hasVisibleFields($shortcode, $attributes)) { |
|
76 | + $this->filterInterpolation(); |
|
77 | + } |
|
78 | + } |
|
79 | + return $shortcode->buildShortcode($attributes); |
|
80 | + } |
|
81 | 81 | |
82 | - /** |
|
83 | - * @return void |
|
84 | - */ |
|
85 | - protected function filterInterpolation() |
|
86 | - { |
|
87 | - add_filter('site-reviews/interpolate/reviews', function ($context) { |
|
88 | - $context['class'] = 'glsr-default glsr-block-disabled'; |
|
89 | - $context['reviews'] = __('You have hidden all of the fields for this block.', 'site-reviews'); |
|
90 | - return $context; |
|
91 | - }); |
|
92 | - } |
|
82 | + /** |
|
83 | + * @return void |
|
84 | + */ |
|
85 | + protected function filterInterpolation() |
|
86 | + { |
|
87 | + add_filter('site-reviews/interpolate/reviews', function ($context) { |
|
88 | + $context['class'] = 'glsr-default glsr-block-disabled'; |
|
89 | + $context['reviews'] = __('You have hidden all of the fields for this block.', 'site-reviews'); |
|
90 | + return $context; |
|
91 | + }); |
|
92 | + } |
|
93 | 93 | |
94 | - /** |
|
95 | - * @return void |
|
96 | - */ |
|
97 | - protected function filterReviewLinks() |
|
98 | - { |
|
99 | - add_filter('site-reviews/rendered/template/reviews', function ($template) { |
|
100 | - return str_replace('<a', '<a tabindex="-1"', $template); |
|
101 | - }); |
|
102 | - } |
|
94 | + /** |
|
95 | + * @return void |
|
96 | + */ |
|
97 | + protected function filterReviewLinks() |
|
98 | + { |
|
99 | + add_filter('site-reviews/rendered/template/reviews', function ($template) { |
|
100 | + return str_replace('<a', '<a tabindex="-1"', $template); |
|
101 | + }); |
|
102 | + } |
|
103 | 103 | |
104 | - /** |
|
105 | - * @return void |
|
106 | - */ |
|
107 | - protected function filterShortcodeClass() |
|
108 | - { |
|
109 | - add_filter('site-reviews/style', function () { |
|
110 | - return 'default'; |
|
111 | - }); |
|
112 | - } |
|
104 | + /** |
|
105 | + * @return void |
|
106 | + */ |
|
107 | + protected function filterShortcodeClass() |
|
108 | + { |
|
109 | + add_filter('site-reviews/style', function () { |
|
110 | + return 'default'; |
|
111 | + }); |
|
112 | + } |
|
113 | 113 | |
114 | - /** |
|
115 | - * @param string $field |
|
116 | - * @return void |
|
117 | - */ |
|
118 | - protected function filterShowMoreLinks($field) |
|
119 | - { |
|
120 | - add_filter('site-reviews/review/wrap/'.$field, function ($value) { |
|
121 | - $value = preg_replace( |
|
122 | - '/(.*)(<span class="glsr-hidden)(.*)(<\/span>)(.*)/s', |
|
123 | - '$1... <a href="#" class="glsr-read-more" tabindex="-1">'.__('Show more', 'site-reviews').'</a>$5', |
|
124 | - $value |
|
125 | - ); |
|
126 | - return $value; |
|
127 | - }); |
|
128 | - } |
|
114 | + /** |
|
115 | + * @param string $field |
|
116 | + * @return void |
|
117 | + */ |
|
118 | + protected function filterShowMoreLinks($field) |
|
119 | + { |
|
120 | + add_filter('site-reviews/review/wrap/'.$field, function ($value) { |
|
121 | + $value = preg_replace( |
|
122 | + '/(.*)(<span class="glsr-hidden)(.*)(<\/span>)(.*)/s', |
|
123 | + '$1... <a href="#" class="glsr-read-more" tabindex="-1">'.__('Show more', 'site-reviews').'</a>$5', |
|
124 | + $value |
|
125 | + ); |
|
126 | + return $value; |
|
127 | + }); |
|
128 | + } |
|
129 | 129 | } |
@@ -7,54 +7,54 @@ |
||
7 | 7 | |
8 | 8 | abstract class Widget extends WP_Widget |
9 | 9 | { |
10 | - /** |
|
11 | - * @var array |
|
12 | - */ |
|
13 | - protected $widgetArgs; |
|
10 | + /** |
|
11 | + * @var array |
|
12 | + */ |
|
13 | + protected $widgetArgs; |
|
14 | 14 | |
15 | - public function __construct($idBase, $name, $values) |
|
16 | - { |
|
17 | - $controlOptions = $widgetOptions = []; |
|
18 | - if (isset($values['class'])) { |
|
19 | - $widgetOptions['classname'] = $values['class']; |
|
20 | - } |
|
21 | - if (isset($values['description'])) { |
|
22 | - $widgetOptions['description'] = $values['description']; |
|
23 | - } |
|
24 | - if (isset($values['width'])) { |
|
25 | - $controlOptions['width'] = $values['width']; |
|
26 | - } |
|
27 | - parent::__construct($idBase, $name, $widgetOptions, $controlOptions); |
|
28 | - } |
|
15 | + public function __construct($idBase, $name, $values) |
|
16 | + { |
|
17 | + $controlOptions = $widgetOptions = []; |
|
18 | + if (isset($values['class'])) { |
|
19 | + $widgetOptions['classname'] = $values['class']; |
|
20 | + } |
|
21 | + if (isset($values['description'])) { |
|
22 | + $widgetOptions['description'] = $values['description']; |
|
23 | + } |
|
24 | + if (isset($values['width'])) { |
|
25 | + $controlOptions['width'] = $values['width']; |
|
26 | + } |
|
27 | + parent::__construct($idBase, $name, $widgetOptions, $controlOptions); |
|
28 | + } |
|
29 | 29 | |
30 | - /** |
|
31 | - * @param string $tag |
|
32 | - * @return void |
|
33 | - */ |
|
34 | - protected function renderField($tag, array $args = []) |
|
35 | - { |
|
36 | - $args = $this->normalizeFieldAttributes($tag, $args); |
|
37 | - $field = glsr(Builder::class)->{$tag}($args['name'], $args); |
|
38 | - echo glsr(Builder::class)->div($field, [ |
|
39 | - 'class' => 'glsr-field', |
|
40 | - ]); |
|
41 | - } |
|
30 | + /** |
|
31 | + * @param string $tag |
|
32 | + * @return void |
|
33 | + */ |
|
34 | + protected function renderField($tag, array $args = []) |
|
35 | + { |
|
36 | + $args = $this->normalizeFieldAttributes($tag, $args); |
|
37 | + $field = glsr(Builder::class)->{$tag}($args['name'], $args); |
|
38 | + echo glsr(Builder::class)->div($field, [ |
|
39 | + 'class' => 'glsr-field', |
|
40 | + ]); |
|
41 | + } |
|
42 | 42 | |
43 | - /** |
|
44 | - * @param string $tag |
|
45 | - * @return array |
|
46 | - */ |
|
47 | - protected function normalizeFieldAttributes($tag, array $args) |
|
48 | - { |
|
49 | - if (empty($args['value'])) { |
|
50 | - $args['value'] = $this->widgetArgs[$args['name']]; |
|
51 | - } |
|
52 | - if (empty($this->widgetArgs['options']) && in_array($tag, ['checkbox', 'radio'])) { |
|
53 | - $args['checked'] = in_array($args['value'], (array) $this->widgetArgs[$args['name']]); |
|
54 | - } |
|
55 | - $args['id'] = $this->get_field_id($args['name']); |
|
56 | - $args['name'] = $this->get_field_name($args['name']); |
|
57 | - $args['is_widget'] = true; |
|
58 | - return $args; |
|
59 | - } |
|
43 | + /** |
|
44 | + * @param string $tag |
|
45 | + * @return array |
|
46 | + */ |
|
47 | + protected function normalizeFieldAttributes($tag, array $args) |
|
48 | + { |
|
49 | + if (empty($args['value'])) { |
|
50 | + $args['value'] = $this->widgetArgs[$args['name']]; |
|
51 | + } |
|
52 | + if (empty($this->widgetArgs['options']) && in_array($tag, ['checkbox', 'radio'])) { |
|
53 | + $args['checked'] = in_array($args['value'], (array) $this->widgetArgs[$args['name']]); |
|
54 | + } |
|
55 | + $args['id'] = $this->get_field_id($args['name']); |
|
56 | + $args['name'] = $this->get_field_name($args['name']); |
|
57 | + $args['is_widget'] = true; |
|
58 | + return $args; |
|
59 | + } |
|
60 | 60 | } |
@@ -8,21 +8,21 @@ |
||
8 | 8 | |
9 | 9 | class RegisterWidgets |
10 | 10 | { |
11 | - /** |
|
12 | - * @return void |
|
13 | - */ |
|
14 | - public function handle(Command $command) |
|
15 | - { |
|
16 | - global $wp_widget_factory; |
|
17 | - foreach ($command->widgets as $key => $values) { |
|
18 | - $widgetClass = glsr(Helper::class)->buildClassName($key.'-widget', 'Widgets'); |
|
19 | - if (!class_exists($widgetClass)) { |
|
20 | - glsr_log()->error(sprintf('Class missing (%s)', $widgetClass)); |
|
21 | - continue; |
|
22 | - } |
|
23 | - // Here we bypass register_widget() in order to pass our custom values to the widget |
|
24 | - $widget = new $widgetClass(Application::ID.'_'.$key, $values['title'], $values); |
|
25 | - $wp_widget_factory->widgets[$widgetClass] = $widget; |
|
26 | - } |
|
27 | - } |
|
11 | + /** |
|
12 | + * @return void |
|
13 | + */ |
|
14 | + public function handle(Command $command) |
|
15 | + { |
|
16 | + global $wp_widget_factory; |
|
17 | + foreach ($command->widgets as $key => $values) { |
|
18 | + $widgetClass = glsr(Helper::class)->buildClassName($key.'-widget', 'Widgets'); |
|
19 | + if (!class_exists($widgetClass)) { |
|
20 | + glsr_log()->error(sprintf('Class missing (%s)', $widgetClass)); |
|
21 | + continue; |
|
22 | + } |
|
23 | + // Here we bypass register_widget() in order to pass our custom values to the widget |
|
24 | + $widget = new $widgetClass(Application::ID.'_'.$key, $values['title'], $values); |
|
25 | + $wp_widget_factory->widgets[$widgetClass] = $widget; |
|
26 | + } |
|
27 | + } |
|
28 | 28 | } |
@@ -7,22 +7,22 @@ |
||
7 | 7 | |
8 | 8 | class RegisterTinymcePopups |
9 | 9 | { |
10 | - /** |
|
11 | - * @return void |
|
12 | - */ |
|
13 | - public function handle(Command $command) |
|
14 | - { |
|
15 | - foreach ($command->popups as $slug => $label) { |
|
16 | - $buttonClass = glsr(Helper::class)->buildClassName($slug.'-popup', 'Shortcodes'); |
|
17 | - if (!class_exists($buttonClass)) { |
|
18 | - glsr_log()->error(sprintf('Class missing (%s)', $buttonClass)); |
|
19 | - continue; |
|
20 | - } |
|
21 | - $shortcode = glsr($buttonClass)->register($slug, [ |
|
22 | - 'label' => $label, |
|
23 | - 'title' => $label, |
|
24 | - ]); |
|
25 | - glsr()->mceShortcodes[$slug] = $shortcode->properties; |
|
26 | - } |
|
27 | - } |
|
10 | + /** |
|
11 | + * @return void |
|
12 | + */ |
|
13 | + public function handle(Command $command) |
|
14 | + { |
|
15 | + foreach ($command->popups as $slug => $label) { |
|
16 | + $buttonClass = glsr(Helper::class)->buildClassName($slug.'-popup', 'Shortcodes'); |
|
17 | + if (!class_exists($buttonClass)) { |
|
18 | + glsr_log()->error(sprintf('Class missing (%s)', $buttonClass)); |
|
19 | + continue; |
|
20 | + } |
|
21 | + $shortcode = glsr($buttonClass)->register($slug, [ |
|
22 | + 'label' => $label, |
|
23 | + 'title' => $label, |
|
24 | + ]); |
|
25 | + glsr()->mceShortcodes[$slug] = $shortcode->properties; |
|
26 | + } |
|
27 | + } |
|
28 | 28 | } |