@@ -11,385 +11,385 @@ |
||
11 | 11 | |
12 | 12 | class CountsManager |
13 | 13 | { |
14 | - const LIMIT = 500; |
|
15 | - const META_AVERAGE = '_glsr_average'; |
|
16 | - const META_COUNT = '_glsr_count'; |
|
17 | - const META_RANKING = '_glsr_ranking'; |
|
14 | + const LIMIT = 500; |
|
15 | + const META_AVERAGE = '_glsr_average'; |
|
16 | + const META_COUNT = '_glsr_count'; |
|
17 | + const META_RANKING = '_glsr_ranking'; |
|
18 | 18 | |
19 | - /** |
|
20 | - * @return array |
|
21 | - * @todo verify the additional type checks are needed |
|
22 | - */ |
|
23 | - public function buildCounts(array $args = []) |
|
24 | - { |
|
25 | - $counts = []; |
|
26 | - $query = $this->queryReviews($args); |
|
27 | - while ($query) { |
|
28 | - $types = array_keys(array_flip(glsr_array_column($query->reviews, 'type'))); |
|
29 | - $types = array_unique(array_merge(['local'], $types)); |
|
30 | - foreach ($types as $type) { |
|
31 | - $type = $this->normalizeType($type); |
|
32 | - if (isset($counts[$type])) { |
|
33 | - continue; |
|
34 | - } |
|
35 | - $counts[$type] = array_fill_keys(range(0, glsr()->constant('MAX_RATING', Rating::class)), 0); |
|
36 | - } |
|
37 | - foreach ($query->reviews as $review) { |
|
38 | - $type = $this->normalizeType($review->type); |
|
39 | - ++$counts[$type][$review->rating]; |
|
40 | - } |
|
41 | - $query = $query->has_more |
|
42 | - ? $this->queryReviews($args, end($query->reviews)->ID) |
|
43 | - : false; |
|
44 | - } |
|
45 | - return $counts; |
|
46 | - } |
|
19 | + /** |
|
20 | + * @return array |
|
21 | + * @todo verify the additional type checks are needed |
|
22 | + */ |
|
23 | + public function buildCounts(array $args = []) |
|
24 | + { |
|
25 | + $counts = []; |
|
26 | + $query = $this->queryReviews($args); |
|
27 | + while ($query) { |
|
28 | + $types = array_keys(array_flip(glsr_array_column($query->reviews, 'type'))); |
|
29 | + $types = array_unique(array_merge(['local'], $types)); |
|
30 | + foreach ($types as $type) { |
|
31 | + $type = $this->normalizeType($type); |
|
32 | + if (isset($counts[$type])) { |
|
33 | + continue; |
|
34 | + } |
|
35 | + $counts[$type] = array_fill_keys(range(0, glsr()->constant('MAX_RATING', Rating::class)), 0); |
|
36 | + } |
|
37 | + foreach ($query->reviews as $review) { |
|
38 | + $type = $this->normalizeType($review->type); |
|
39 | + ++$counts[$type][$review->rating]; |
|
40 | + } |
|
41 | + $query = $query->has_more |
|
42 | + ? $this->queryReviews($args, end($query->reviews)->ID) |
|
43 | + : false; |
|
44 | + } |
|
45 | + return $counts; |
|
46 | + } |
|
47 | 47 | |
48 | - /** |
|
49 | - * @param int $postId |
|
50 | - * @return array |
|
51 | - */ |
|
52 | - public function buildPostCounts($postId) |
|
53 | - { |
|
54 | - return $this->buildCounts(['post_ids' => [$postId]]); |
|
55 | - } |
|
48 | + /** |
|
49 | + * @param int $postId |
|
50 | + * @return array |
|
51 | + */ |
|
52 | + public function buildPostCounts($postId) |
|
53 | + { |
|
54 | + return $this->buildCounts(['post_ids' => [$postId]]); |
|
55 | + } |
|
56 | 56 | |
57 | - /** |
|
58 | - * @param int $termTaxonomyId |
|
59 | - * @return array |
|
60 | - */ |
|
61 | - public function buildTermCounts($termTaxonomyId) |
|
62 | - { |
|
63 | - return $this->buildCounts(['term_ids' => [$termTaxonomyId]]); |
|
64 | - } |
|
57 | + /** |
|
58 | + * @param int $termTaxonomyId |
|
59 | + * @return array |
|
60 | + */ |
|
61 | + public function buildTermCounts($termTaxonomyId) |
|
62 | + { |
|
63 | + return $this->buildCounts(['term_ids' => [$termTaxonomyId]]); |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * @return void |
|
68 | - */ |
|
69 | - public function countAll() |
|
70 | - { |
|
71 | - $terms = glsr(Database::class)->getTerms(['fields' => 'all']); |
|
72 | - foreach ($terms as $term) { |
|
73 | - $this->setTermCounts($term->term_id, $this->buildTermCounts($term->term_taxonomy_id)); |
|
74 | - } |
|
75 | - $postIds = glsr(SqlQueries::class)->getReviewsMeta('assigned_to'); |
|
76 | - foreach ($postIds as $postId) { |
|
77 | - $this->setPostCounts($postId, $this->buildPostCounts($postId)); |
|
78 | - } |
|
79 | - $this->setCounts($this->buildCounts()); |
|
80 | - } |
|
66 | + /** |
|
67 | + * @return void |
|
68 | + */ |
|
69 | + public function countAll() |
|
70 | + { |
|
71 | + $terms = glsr(Database::class)->getTerms(['fields' => 'all']); |
|
72 | + foreach ($terms as $term) { |
|
73 | + $this->setTermCounts($term->term_id, $this->buildTermCounts($term->term_taxonomy_id)); |
|
74 | + } |
|
75 | + $postIds = glsr(SqlQueries::class)->getReviewsMeta('assigned_to'); |
|
76 | + foreach ($postIds as $postId) { |
|
77 | + $this->setPostCounts($postId, $this->buildPostCounts($postId)); |
|
78 | + } |
|
79 | + $this->setCounts($this->buildCounts()); |
|
80 | + } |
|
81 | 81 | |
82 | - /** |
|
83 | - * @return void |
|
84 | - */ |
|
85 | - public function decrease(Review $review) |
|
86 | - { |
|
87 | - $this->decreaseCounts($review); |
|
88 | - $this->decreasePostCounts($review); |
|
89 | - $this->decreaseTermCounts($review); |
|
90 | - } |
|
82 | + /** |
|
83 | + * @return void |
|
84 | + */ |
|
85 | + public function decrease(Review $review) |
|
86 | + { |
|
87 | + $this->decreaseCounts($review); |
|
88 | + $this->decreasePostCounts($review); |
|
89 | + $this->decreaseTermCounts($review); |
|
90 | + } |
|
91 | 91 | |
92 | - /** |
|
93 | - * @return void |
|
94 | - */ |
|
95 | - public function decreaseCounts(Review $review) |
|
96 | - { |
|
97 | - $this->setCounts($this->decreaseRating( |
|
98 | - $this->getCounts(), |
|
99 | - $review->review_type, |
|
100 | - $review->rating |
|
101 | - )); |
|
102 | - } |
|
92 | + /** |
|
93 | + * @return void |
|
94 | + */ |
|
95 | + public function decreaseCounts(Review $review) |
|
96 | + { |
|
97 | + $this->setCounts($this->decreaseRating( |
|
98 | + $this->getCounts(), |
|
99 | + $review->review_type, |
|
100 | + $review->rating |
|
101 | + )); |
|
102 | + } |
|
103 | 103 | |
104 | - /** |
|
105 | - * @return void |
|
106 | - */ |
|
107 | - public function decreasePostCounts(Review $review) |
|
108 | - { |
|
109 | - if (empty($counts = $this->getPostCounts($review->assigned_to))) { |
|
110 | - return; |
|
111 | - } |
|
112 | - $counts = $this->decreaseRating($counts, $review->review_type, $review->rating); |
|
113 | - $this->setPostCounts($review->assigned_to, $counts); |
|
114 | - } |
|
104 | + /** |
|
105 | + * @return void |
|
106 | + */ |
|
107 | + public function decreasePostCounts(Review $review) |
|
108 | + { |
|
109 | + if (empty($counts = $this->getPostCounts($review->assigned_to))) { |
|
110 | + return; |
|
111 | + } |
|
112 | + $counts = $this->decreaseRating($counts, $review->review_type, $review->rating); |
|
113 | + $this->setPostCounts($review->assigned_to, $counts); |
|
114 | + } |
|
115 | 115 | |
116 | - /** |
|
117 | - * @return void |
|
118 | - */ |
|
119 | - public function decreaseTermCounts(Review $review) |
|
120 | - { |
|
121 | - foreach ($review->term_ids as $termId) { |
|
122 | - if (empty($counts = $this->getTermCounts($termId))) { |
|
123 | - continue; |
|
124 | - } |
|
125 | - $counts = $this->decreaseRating($counts, $review->review_type, $review->rating); |
|
126 | - $this->setTermCounts($termId, $counts); |
|
127 | - } |
|
128 | - } |
|
116 | + /** |
|
117 | + * @return void |
|
118 | + */ |
|
119 | + public function decreaseTermCounts(Review $review) |
|
120 | + { |
|
121 | + foreach ($review->term_ids as $termId) { |
|
122 | + if (empty($counts = $this->getTermCounts($termId))) { |
|
123 | + continue; |
|
124 | + } |
|
125 | + $counts = $this->decreaseRating($counts, $review->review_type, $review->rating); |
|
126 | + $this->setTermCounts($termId, $counts); |
|
127 | + } |
|
128 | + } |
|
129 | 129 | |
130 | - /** |
|
131 | - * @return array |
|
132 | - */ |
|
133 | - public function flatten(array $reviewCounts, array $args = []) |
|
134 | - { |
|
135 | - $counts = []; |
|
136 | - array_walk_recursive($reviewCounts, function ($num, $index) use (&$counts) { |
|
137 | - $counts[$index] = $num + intval(glsr_get($counts, $index, 0)); |
|
138 | - }); |
|
139 | - $args = wp_parse_args($args, [ |
|
140 | - 'max' => glsr()->constant('MAX_RATING', Rating::class), |
|
141 | - 'min' => glsr()->constant('MIN_RATING', Rating::class), |
|
142 | - ]); |
|
143 | - foreach ($counts as $index => &$num) { |
|
144 | - if ($index >= intval($args['min']) && $index <= intval($args['max'])) { |
|
145 | - continue; |
|
146 | - } |
|
147 | - $num = 0; |
|
148 | - } |
|
149 | - return $counts; |
|
150 | - } |
|
130 | + /** |
|
131 | + * @return array |
|
132 | + */ |
|
133 | + public function flatten(array $reviewCounts, array $args = []) |
|
134 | + { |
|
135 | + $counts = []; |
|
136 | + array_walk_recursive($reviewCounts, function ($num, $index) use (&$counts) { |
|
137 | + $counts[$index] = $num + intval(glsr_get($counts, $index, 0)); |
|
138 | + }); |
|
139 | + $args = wp_parse_args($args, [ |
|
140 | + 'max' => glsr()->constant('MAX_RATING', Rating::class), |
|
141 | + 'min' => glsr()->constant('MIN_RATING', Rating::class), |
|
142 | + ]); |
|
143 | + foreach ($counts as $index => &$num) { |
|
144 | + if ($index >= intval($args['min']) && $index <= intval($args['max'])) { |
|
145 | + continue; |
|
146 | + } |
|
147 | + $num = 0; |
|
148 | + } |
|
149 | + return $counts; |
|
150 | + } |
|
151 | 151 | |
152 | - /** |
|
153 | - * @return array |
|
154 | - */ |
|
155 | - public function get(array $args = []) |
|
156 | - { |
|
157 | - $args = $this->normalizeArgs($args); |
|
158 | - $counts = []; |
|
159 | - if ($this->isMixedCount($args)) { |
|
160 | - $counts = [$this->buildCounts($args)]; // force query the database |
|
161 | - } else { |
|
162 | - foreach ($args['post_ids'] as $postId) { |
|
163 | - $counts[] = $this->getPostCounts($postId); |
|
164 | - } |
|
165 | - foreach ($args['term_ids'] as $termId) { |
|
166 | - $counts[] = $this->getTermCounts($termId); |
|
167 | - } |
|
168 | - if (empty($counts)) { |
|
169 | - $counts[] = $this->getCounts(); |
|
170 | - } |
|
171 | - } |
|
172 | - return in_array($args['type'], ['', 'all']) |
|
173 | - ? $this->normalize([$this->flatten($counts)]) |
|
174 | - : $this->normalize(glsr_array_column($counts, $args['type'])); |
|
175 | - } |
|
152 | + /** |
|
153 | + * @return array |
|
154 | + */ |
|
155 | + public function get(array $args = []) |
|
156 | + { |
|
157 | + $args = $this->normalizeArgs($args); |
|
158 | + $counts = []; |
|
159 | + if ($this->isMixedCount($args)) { |
|
160 | + $counts = [$this->buildCounts($args)]; // force query the database |
|
161 | + } else { |
|
162 | + foreach ($args['post_ids'] as $postId) { |
|
163 | + $counts[] = $this->getPostCounts($postId); |
|
164 | + } |
|
165 | + foreach ($args['term_ids'] as $termId) { |
|
166 | + $counts[] = $this->getTermCounts($termId); |
|
167 | + } |
|
168 | + if (empty($counts)) { |
|
169 | + $counts[] = $this->getCounts(); |
|
170 | + } |
|
171 | + } |
|
172 | + return in_array($args['type'], ['', 'all']) |
|
173 | + ? $this->normalize([$this->flatten($counts)]) |
|
174 | + : $this->normalize(glsr_array_column($counts, $args['type'])); |
|
175 | + } |
|
176 | 176 | |
177 | - /** |
|
178 | - * @return array |
|
179 | - */ |
|
180 | - public function getCounts() |
|
181 | - { |
|
182 | - $counts = glsr(OptionManager::class)->get('counts', []); |
|
183 | - if (!is_array($counts)) { |
|
184 | - glsr_log()->error('Review counts is not an array; possibly due to incorrectly imported reviews.')->debug($counts); |
|
185 | - return []; |
|
186 | - } |
|
187 | - return $counts; |
|
188 | - } |
|
177 | + /** |
|
178 | + * @return array |
|
179 | + */ |
|
180 | + public function getCounts() |
|
181 | + { |
|
182 | + $counts = glsr(OptionManager::class)->get('counts', []); |
|
183 | + if (!is_array($counts)) { |
|
184 | + glsr_log()->error('Review counts is not an array; possibly due to incorrectly imported reviews.')->debug($counts); |
|
185 | + return []; |
|
186 | + } |
|
187 | + return $counts; |
|
188 | + } |
|
189 | 189 | |
190 | - /** |
|
191 | - * @param int $postId |
|
192 | - * @return array |
|
193 | - */ |
|
194 | - public function getPostCounts($postId) |
|
195 | - { |
|
196 | - return array_filter((array) get_post_meta($postId, static::META_COUNT, true)); |
|
197 | - } |
|
190 | + /** |
|
191 | + * @param int $postId |
|
192 | + * @return array |
|
193 | + */ |
|
194 | + public function getPostCounts($postId) |
|
195 | + { |
|
196 | + return array_filter((array) get_post_meta($postId, static::META_COUNT, true)); |
|
197 | + } |
|
198 | 198 | |
199 | - /** |
|
200 | - * @param int $termId |
|
201 | - * @return array |
|
202 | - */ |
|
203 | - public function getTermCounts($termId) |
|
204 | - { |
|
205 | - return array_filter((array) get_term_meta($termId, static::META_COUNT, true)); |
|
206 | - } |
|
199 | + /** |
|
200 | + * @param int $termId |
|
201 | + * @return array |
|
202 | + */ |
|
203 | + public function getTermCounts($termId) |
|
204 | + { |
|
205 | + return array_filter((array) get_term_meta($termId, static::META_COUNT, true)); |
|
206 | + } |
|
207 | 207 | |
208 | - /** |
|
209 | - * @return void |
|
210 | - */ |
|
211 | - public function increase(Review $review) |
|
212 | - { |
|
213 | - $this->increaseCounts($review); |
|
214 | - $this->increasePostCounts($review); |
|
215 | - $this->increaseTermCounts($review); |
|
216 | - } |
|
208 | + /** |
|
209 | + * @return void |
|
210 | + */ |
|
211 | + public function increase(Review $review) |
|
212 | + { |
|
213 | + $this->increaseCounts($review); |
|
214 | + $this->increasePostCounts($review); |
|
215 | + $this->increaseTermCounts($review); |
|
216 | + } |
|
217 | 217 | |
218 | - /** |
|
219 | - * @return void |
|
220 | - */ |
|
221 | - public function increaseCounts(Review $review) |
|
222 | - { |
|
223 | - if (empty($counts = $this->getCounts())) { |
|
224 | - $counts = $this->buildCounts(); |
|
225 | - } |
|
226 | - $this->setCounts($this->increaseRating($counts, $review->review_type, $review->rating)); |
|
227 | - } |
|
218 | + /** |
|
219 | + * @return void |
|
220 | + */ |
|
221 | + public function increaseCounts(Review $review) |
|
222 | + { |
|
223 | + if (empty($counts = $this->getCounts())) { |
|
224 | + $counts = $this->buildCounts(); |
|
225 | + } |
|
226 | + $this->setCounts($this->increaseRating($counts, $review->review_type, $review->rating)); |
|
227 | + } |
|
228 | 228 | |
229 | - /** |
|
230 | - * @return void |
|
231 | - */ |
|
232 | - public function increasePostCounts(Review $review) |
|
233 | - { |
|
234 | - if (!(get_post($review->assigned_to) instanceof WP_Post)) { |
|
235 | - return; |
|
236 | - } |
|
237 | - $counts = $this->getPostCounts($review->assigned_to); |
|
238 | - $counts = empty($counts) |
|
239 | - ? $this->buildPostCounts($review->assigned_to) |
|
240 | - : $this->increaseRating($counts, $review->review_type, $review->rating); |
|
241 | - $this->setPostCounts($review->assigned_to, $counts); |
|
242 | - } |
|
229 | + /** |
|
230 | + * @return void |
|
231 | + */ |
|
232 | + public function increasePostCounts(Review $review) |
|
233 | + { |
|
234 | + if (!(get_post($review->assigned_to) instanceof WP_Post)) { |
|
235 | + return; |
|
236 | + } |
|
237 | + $counts = $this->getPostCounts($review->assigned_to); |
|
238 | + $counts = empty($counts) |
|
239 | + ? $this->buildPostCounts($review->assigned_to) |
|
240 | + : $this->increaseRating($counts, $review->review_type, $review->rating); |
|
241 | + $this->setPostCounts($review->assigned_to, $counts); |
|
242 | + } |
|
243 | 243 | |
244 | - /** |
|
245 | - * @return void |
|
246 | - */ |
|
247 | - public function increaseTermCounts(Review $review) |
|
248 | - { |
|
249 | - $terms = glsr(ReviewManager::class)->normalizeTerms(implode(',', $review->term_ids)); |
|
250 | - foreach ($terms as $term) { |
|
251 | - $counts = $this->getTermCounts($term['term_id']); |
|
252 | - $counts = empty($counts) |
|
253 | - ? $this->buildTermCounts($term['term_taxonomy_id']) |
|
254 | - : $this->increaseRating($counts, $review->review_type, $review->rating); |
|
255 | - $this->setTermCounts($term['term_id'], $counts); |
|
256 | - } |
|
257 | - } |
|
244 | + /** |
|
245 | + * @return void |
|
246 | + */ |
|
247 | + public function increaseTermCounts(Review $review) |
|
248 | + { |
|
249 | + $terms = glsr(ReviewManager::class)->normalizeTerms(implode(',', $review->term_ids)); |
|
250 | + foreach ($terms as $term) { |
|
251 | + $counts = $this->getTermCounts($term['term_id']); |
|
252 | + $counts = empty($counts) |
|
253 | + ? $this->buildTermCounts($term['term_taxonomy_id']) |
|
254 | + : $this->increaseRating($counts, $review->review_type, $review->rating); |
|
255 | + $this->setTermCounts($term['term_id'], $counts); |
|
256 | + } |
|
257 | + } |
|
258 | 258 | |
259 | - /** |
|
260 | - * @return void |
|
261 | - */ |
|
262 | - public function setCounts(array $reviewCounts) |
|
263 | - { |
|
264 | - glsr(OptionManager::class)->set('counts', $reviewCounts); |
|
265 | - } |
|
259 | + /** |
|
260 | + * @return void |
|
261 | + */ |
|
262 | + public function setCounts(array $reviewCounts) |
|
263 | + { |
|
264 | + glsr(OptionManager::class)->set('counts', $reviewCounts); |
|
265 | + } |
|
266 | 266 | |
267 | - /** |
|
268 | - * @param int $postId |
|
269 | - * @return void |
|
270 | - */ |
|
271 | - public function setPostCounts($postId, array $reviewCounts) |
|
272 | - { |
|
273 | - $ratingCounts = $this->flatten($reviewCounts); |
|
274 | - update_post_meta($postId, static::META_COUNT, $reviewCounts); |
|
275 | - update_post_meta($postId, static::META_AVERAGE, glsr(Rating::class)->getAverage($ratingCounts)); |
|
276 | - update_post_meta($postId, static::META_RANKING, glsr(Rating::class)->getRanking($ratingCounts)); |
|
277 | - } |
|
267 | + /** |
|
268 | + * @param int $postId |
|
269 | + * @return void |
|
270 | + */ |
|
271 | + public function setPostCounts($postId, array $reviewCounts) |
|
272 | + { |
|
273 | + $ratingCounts = $this->flatten($reviewCounts); |
|
274 | + update_post_meta($postId, static::META_COUNT, $reviewCounts); |
|
275 | + update_post_meta($postId, static::META_AVERAGE, glsr(Rating::class)->getAverage($ratingCounts)); |
|
276 | + update_post_meta($postId, static::META_RANKING, glsr(Rating::class)->getRanking($ratingCounts)); |
|
277 | + } |
|
278 | 278 | |
279 | - /** |
|
280 | - * @param int $termId |
|
281 | - * @return void |
|
282 | - */ |
|
283 | - public function setTermCounts($termId, array $reviewCounts) |
|
284 | - { |
|
285 | - $term = get_term($termId, Application::TAXONOMY); |
|
286 | - if (!isset($term->term_id)) { |
|
287 | - return; |
|
288 | - } |
|
289 | - $ratingCounts = $this->flatten($reviewCounts); |
|
290 | - update_term_meta($termId, static::META_COUNT, $reviewCounts); |
|
291 | - update_term_meta($termId, static::META_AVERAGE, glsr(Rating::class)->getAverage($ratingCounts)); |
|
292 | - update_term_meta($termId, static::META_RANKING, glsr(Rating::class)->getRanking($ratingCounts)); |
|
293 | - } |
|
279 | + /** |
|
280 | + * @param int $termId |
|
281 | + * @return void |
|
282 | + */ |
|
283 | + public function setTermCounts($termId, array $reviewCounts) |
|
284 | + { |
|
285 | + $term = get_term($termId, Application::TAXONOMY); |
|
286 | + if (!isset($term->term_id)) { |
|
287 | + return; |
|
288 | + } |
|
289 | + $ratingCounts = $this->flatten($reviewCounts); |
|
290 | + update_term_meta($termId, static::META_COUNT, $reviewCounts); |
|
291 | + update_term_meta($termId, static::META_AVERAGE, glsr(Rating::class)->getAverage($ratingCounts)); |
|
292 | + update_term_meta($termId, static::META_RANKING, glsr(Rating::class)->getRanking($ratingCounts)); |
|
293 | + } |
|
294 | 294 | |
295 | - /** |
|
296 | - * @param string $type |
|
297 | - * @param int $rating |
|
298 | - * @return array |
|
299 | - */ |
|
300 | - protected function decreaseRating(array $reviewCounts, $type, $rating) |
|
301 | - { |
|
302 | - if (isset($reviewCounts[$type][$rating])) { |
|
303 | - $reviewCounts[$type][$rating] = max(0, $reviewCounts[$type][$rating] - 1); |
|
304 | - } |
|
305 | - return $reviewCounts; |
|
306 | - } |
|
295 | + /** |
|
296 | + * @param string $type |
|
297 | + * @param int $rating |
|
298 | + * @return array |
|
299 | + */ |
|
300 | + protected function decreaseRating(array $reviewCounts, $type, $rating) |
|
301 | + { |
|
302 | + if (isset($reviewCounts[$type][$rating])) { |
|
303 | + $reviewCounts[$type][$rating] = max(0, $reviewCounts[$type][$rating] - 1); |
|
304 | + } |
|
305 | + return $reviewCounts; |
|
306 | + } |
|
307 | 307 | |
308 | - /** |
|
309 | - * @param string $type |
|
310 | - * @param int $rating |
|
311 | - * @return array |
|
312 | - */ |
|
313 | - protected function increaseRating(array $reviewCounts, $type, $rating) |
|
314 | - { |
|
315 | - if (!array_key_exists($type, glsr()->reviewTypes)) { |
|
316 | - return $reviewCounts; |
|
317 | - } |
|
318 | - if (!array_key_exists($type, $reviewCounts)) { |
|
319 | - $reviewCounts[$type] = []; |
|
320 | - } |
|
321 | - $reviewCounts = $this->normalize($reviewCounts); |
|
322 | - $reviewCounts[$type][$rating] = intval($reviewCounts[$type][$rating]) + 1; |
|
323 | - return $reviewCounts; |
|
324 | - } |
|
308 | + /** |
|
309 | + * @param string $type |
|
310 | + * @param int $rating |
|
311 | + * @return array |
|
312 | + */ |
|
313 | + protected function increaseRating(array $reviewCounts, $type, $rating) |
|
314 | + { |
|
315 | + if (!array_key_exists($type, glsr()->reviewTypes)) { |
|
316 | + return $reviewCounts; |
|
317 | + } |
|
318 | + if (!array_key_exists($type, $reviewCounts)) { |
|
319 | + $reviewCounts[$type] = []; |
|
320 | + } |
|
321 | + $reviewCounts = $this->normalize($reviewCounts); |
|
322 | + $reviewCounts[$type][$rating] = intval($reviewCounts[$type][$rating]) + 1; |
|
323 | + return $reviewCounts; |
|
324 | + } |
|
325 | 325 | |
326 | - /** |
|
327 | - * @return bool |
|
328 | - */ |
|
329 | - protected function isMixedCount(array $args) |
|
330 | - { |
|
331 | - return !empty($args['post_ids']) && !empty($args['term_ids']); |
|
332 | - } |
|
326 | + /** |
|
327 | + * @return bool |
|
328 | + */ |
|
329 | + protected function isMixedCount(array $args) |
|
330 | + { |
|
331 | + return !empty($args['post_ids']) && !empty($args['term_ids']); |
|
332 | + } |
|
333 | 333 | |
334 | - /** |
|
335 | - * @return array |
|
336 | - */ |
|
337 | - protected function normalize(array $reviewCounts) |
|
338 | - { |
|
339 | - if (empty($reviewCounts)) { |
|
340 | - $reviewCounts = [[]]; |
|
341 | - } |
|
342 | - foreach ($reviewCounts as &$counts) { |
|
343 | - foreach (range(0, glsr()->constant('MAX_RATING', Rating::class)) as $index) { |
|
344 | - if (isset($counts[$index])) { |
|
345 | - continue; |
|
346 | - } |
|
347 | - $counts[$index] = 0; |
|
348 | - } |
|
349 | - ksort($counts); |
|
350 | - } |
|
351 | - return $reviewCounts; |
|
352 | - } |
|
334 | + /** |
|
335 | + * @return array |
|
336 | + */ |
|
337 | + protected function normalize(array $reviewCounts) |
|
338 | + { |
|
339 | + if (empty($reviewCounts)) { |
|
340 | + $reviewCounts = [[]]; |
|
341 | + } |
|
342 | + foreach ($reviewCounts as &$counts) { |
|
343 | + foreach (range(0, glsr()->constant('MAX_RATING', Rating::class)) as $index) { |
|
344 | + if (isset($counts[$index])) { |
|
345 | + continue; |
|
346 | + } |
|
347 | + $counts[$index] = 0; |
|
348 | + } |
|
349 | + ksort($counts); |
|
350 | + } |
|
351 | + return $reviewCounts; |
|
352 | + } |
|
353 | 353 | |
354 | - /** |
|
355 | - * @return array |
|
356 | - */ |
|
357 | - protected function normalizeArgs(array $args) |
|
358 | - { |
|
359 | - $args = wp_parse_args(array_filter($args), [ |
|
360 | - 'post_ids' => [], |
|
361 | - 'term_ids' => [], |
|
362 | - 'type' => 'local', |
|
363 | - ]); |
|
364 | - $args['post_ids'] = glsr(Polylang::class)->getPostIds($args['post_ids']); |
|
365 | - $args['type'] = $this->normalizeType($args['type']); |
|
366 | - return $args; |
|
367 | - } |
|
354 | + /** |
|
355 | + * @return array |
|
356 | + */ |
|
357 | + protected function normalizeArgs(array $args) |
|
358 | + { |
|
359 | + $args = wp_parse_args(array_filter($args), [ |
|
360 | + 'post_ids' => [], |
|
361 | + 'term_ids' => [], |
|
362 | + 'type' => 'local', |
|
363 | + ]); |
|
364 | + $args['post_ids'] = glsr(Polylang::class)->getPostIds($args['post_ids']); |
|
365 | + $args['type'] = $this->normalizeType($args['type']); |
|
366 | + return $args; |
|
367 | + } |
|
368 | 368 | |
369 | - /** |
|
370 | - * @param string $type |
|
371 | - * @return string |
|
372 | - */ |
|
373 | - protected function normalizeType($type) |
|
374 | - { |
|
375 | - return empty($type) || !is_string($type) |
|
376 | - ? 'local' |
|
377 | - : $type; |
|
378 | - } |
|
369 | + /** |
|
370 | + * @param string $type |
|
371 | + * @return string |
|
372 | + */ |
|
373 | + protected function normalizeType($type) |
|
374 | + { |
|
375 | + return empty($type) || !is_string($type) |
|
376 | + ? 'local' |
|
377 | + : $type; |
|
378 | + } |
|
379 | 379 | |
380 | - /** |
|
381 | - * @param int $lastPostId |
|
382 | - * @return object |
|
383 | - */ |
|
384 | - protected function queryReviews(array $args = [], $lastPostId = 0) |
|
385 | - { |
|
386 | - $reviews = glsr(SqlQueries::class)->getReviewCounts($args, $lastPostId, static::LIMIT); |
|
387 | - $hasMore = is_array($reviews) |
|
388 | - ? count($reviews) == static::LIMIT |
|
389 | - : false; |
|
390 | - return (object) [ |
|
391 | - 'has_more' => $hasMore, |
|
392 | - 'reviews' => $reviews, |
|
393 | - ]; |
|
394 | - } |
|
380 | + /** |
|
381 | + * @param int $lastPostId |
|
382 | + * @return object |
|
383 | + */ |
|
384 | + protected function queryReviews(array $args = [], $lastPostId = 0) |
|
385 | + { |
|
386 | + $reviews = glsr(SqlQueries::class)->getReviewCounts($args, $lastPostId, static::LIMIT); |
|
387 | + $hasMore = is_array($reviews) |
|
388 | + ? count($reviews) == static::LIMIT |
|
389 | + : false; |
|
390 | + return (object) [ |
|
391 | + 'has_more' => $hasMore, |
|
392 | + 'reviews' => $reviews, |
|
393 | + ]; |
|
394 | + } |
|
395 | 395 | } |
@@ -11,173 +11,173 @@ |
||
11 | 11 | |
12 | 12 | class Review implements \ArrayAccess |
13 | 13 | { |
14 | - public $assigned_to; |
|
15 | - public $author; |
|
16 | - public $avatar; |
|
17 | - public $content; |
|
18 | - public $custom; |
|
19 | - public $date; |
|
20 | - public $email; |
|
21 | - public $ID; |
|
22 | - public $ip_address; |
|
23 | - public $modified; |
|
24 | - public $pinned; |
|
25 | - public $rating; |
|
26 | - public $response; |
|
27 | - public $review_id; |
|
28 | - public $review_type; |
|
29 | - public $status; |
|
30 | - public $term_ids; |
|
31 | - public $title; |
|
32 | - public $url; |
|
33 | - public $user_id; |
|
14 | + public $assigned_to; |
|
15 | + public $author; |
|
16 | + public $avatar; |
|
17 | + public $content; |
|
18 | + public $custom; |
|
19 | + public $date; |
|
20 | + public $email; |
|
21 | + public $ID; |
|
22 | + public $ip_address; |
|
23 | + public $modified; |
|
24 | + public $pinned; |
|
25 | + public $rating; |
|
26 | + public $response; |
|
27 | + public $review_id; |
|
28 | + public $review_type; |
|
29 | + public $status; |
|
30 | + public $term_ids; |
|
31 | + public $title; |
|
32 | + public $url; |
|
33 | + public $user_id; |
|
34 | 34 | |
35 | - public function __construct(WP_Post $post) |
|
36 | - { |
|
37 | - if (Application::POST_TYPE != $post->post_type) { |
|
38 | - return; |
|
39 | - } |
|
40 | - $this->content = $post->post_content; |
|
41 | - $this->date = $post->post_date; |
|
42 | - $this->ID = intval($post->ID); |
|
43 | - $this->status = $post->post_status; |
|
44 | - $this->title = $post->post_title; |
|
45 | - $this->user_id = intval($post->post_author); |
|
46 | - $this->setProperties($post); |
|
47 | - $this->setTermIds($post); |
|
48 | - } |
|
35 | + public function __construct(WP_Post $post) |
|
36 | + { |
|
37 | + if (Application::POST_TYPE != $post->post_type) { |
|
38 | + return; |
|
39 | + } |
|
40 | + $this->content = $post->post_content; |
|
41 | + $this->date = $post->post_date; |
|
42 | + $this->ID = intval($post->ID); |
|
43 | + $this->status = $post->post_status; |
|
44 | + $this->title = $post->post_title; |
|
45 | + $this->user_id = intval($post->post_author); |
|
46 | + $this->setProperties($post); |
|
47 | + $this->setTermIds($post); |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * @return mixed |
|
52 | - */ |
|
53 | - public function __get($key) |
|
54 | - { |
|
55 | - return $this->offsetGet($key); |
|
56 | - } |
|
50 | + /** |
|
51 | + * @return mixed |
|
52 | + */ |
|
53 | + public function __get($key) |
|
54 | + { |
|
55 | + return $this->offsetGet($key); |
|
56 | + } |
|
57 | 57 | |
58 | - /** |
|
59 | - * @return string |
|
60 | - */ |
|
61 | - public function __toString() |
|
62 | - { |
|
63 | - return (string) $this->build(); |
|
64 | - } |
|
58 | + /** |
|
59 | + * @return string |
|
60 | + */ |
|
61 | + public function __toString() |
|
62 | + { |
|
63 | + return (string) $this->build(); |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * @return ReviewHtml |
|
68 | - */ |
|
69 | - public function build(array $args = []) |
|
70 | - { |
|
71 | - if (empty($this->ID)) { |
|
72 | - return new ReviewHtml($this); |
|
73 | - } |
|
74 | - $partial = glsr(SiteReviewsPartial::class); |
|
75 | - $partial->args = glsr(SiteReviewsDefaults::class)->merge($args); |
|
76 | - $partial->options = glsr(Helper::class)->flattenArray(glsr(OptionManager::class)->all()); |
|
77 | - return $partial->buildReview($this); |
|
78 | - } |
|
66 | + /** |
|
67 | + * @return ReviewHtml |
|
68 | + */ |
|
69 | + public function build(array $args = []) |
|
70 | + { |
|
71 | + if (empty($this->ID)) { |
|
72 | + return new ReviewHtml($this); |
|
73 | + } |
|
74 | + $partial = glsr(SiteReviewsPartial::class); |
|
75 | + $partial->args = glsr(SiteReviewsDefaults::class)->merge($args); |
|
76 | + $partial->options = glsr(Helper::class)->flattenArray(glsr(OptionManager::class)->all()); |
|
77 | + return $partial->buildReview($this); |
|
78 | + } |
|
79 | 79 | |
80 | - /** |
|
81 | - * @param mixed $key |
|
82 | - * @return bool |
|
83 | - */ |
|
84 | - public function offsetExists($key) |
|
85 | - { |
|
86 | - return property_exists($this, $key) || array_key_exists($key, (array) $this->custom); |
|
87 | - } |
|
80 | + /** |
|
81 | + * @param mixed $key |
|
82 | + * @return bool |
|
83 | + */ |
|
84 | + public function offsetExists($key) |
|
85 | + { |
|
86 | + return property_exists($this, $key) || array_key_exists($key, (array) $this->custom); |
|
87 | + } |
|
88 | 88 | |
89 | - /** |
|
90 | - * @param mixed $key |
|
91 | - * @return mixed |
|
92 | - */ |
|
93 | - public function offsetGet($key) |
|
94 | - { |
|
95 | - return property_exists($this, $key) |
|
96 | - ? $this->$key |
|
97 | - : glsr_get($this->custom, $key, null); |
|
98 | - } |
|
89 | + /** |
|
90 | + * @param mixed $key |
|
91 | + * @return mixed |
|
92 | + */ |
|
93 | + public function offsetGet($key) |
|
94 | + { |
|
95 | + return property_exists($this, $key) |
|
96 | + ? $this->$key |
|
97 | + : glsr_get($this->custom, $key, null); |
|
98 | + } |
|
99 | 99 | |
100 | - /** |
|
101 | - * @param mixed $key |
|
102 | - * @param mixed $value |
|
103 | - * @return void |
|
104 | - */ |
|
105 | - public function offsetSet($key, $value) |
|
106 | - { |
|
107 | - if (property_exists($this, $key)) { |
|
108 | - $this->$key = $value; |
|
109 | - return; |
|
110 | - } |
|
111 | - if (!is_array($this->custom)) { |
|
112 | - $this->custom = array_filter((array) $this->custom); |
|
113 | - } |
|
114 | - $this->custom[$key] = $value; |
|
115 | - } |
|
100 | + /** |
|
101 | + * @param mixed $key |
|
102 | + * @param mixed $value |
|
103 | + * @return void |
|
104 | + */ |
|
105 | + public function offsetSet($key, $value) |
|
106 | + { |
|
107 | + if (property_exists($this, $key)) { |
|
108 | + $this->$key = $value; |
|
109 | + return; |
|
110 | + } |
|
111 | + if (!is_array($this->custom)) { |
|
112 | + $this->custom = array_filter((array) $this->custom); |
|
113 | + } |
|
114 | + $this->custom[$key] = $value; |
|
115 | + } |
|
116 | 116 | |
117 | - /** |
|
118 | - * @param mixed $key |
|
119 | - * @return void |
|
120 | - */ |
|
121 | - public function offsetUnset($key) |
|
122 | - { |
|
123 | - $this->offsetSet($key, null); |
|
124 | - } |
|
117 | + /** |
|
118 | + * @param mixed $key |
|
119 | + * @return void |
|
120 | + */ |
|
121 | + public function offsetUnset($key) |
|
122 | + { |
|
123 | + $this->offsetSet($key, null); |
|
124 | + } |
|
125 | 125 | |
126 | - /** |
|
127 | - * @return void |
|
128 | - */ |
|
129 | - public function render() |
|
130 | - { |
|
131 | - echo $this->build(); |
|
132 | - } |
|
126 | + /** |
|
127 | + * @return void |
|
128 | + */ |
|
129 | + public function render() |
|
130 | + { |
|
131 | + echo $this->build(); |
|
132 | + } |
|
133 | 133 | |
134 | - /** |
|
135 | - * @return bool |
|
136 | - */ |
|
137 | - protected function isModified(array $properties) |
|
138 | - { |
|
139 | - return $this->date != $properties['date'] |
|
140 | - || $this->content != $properties['content'] |
|
141 | - || $this->title != $properties['title']; |
|
142 | - } |
|
134 | + /** |
|
135 | + * @return bool |
|
136 | + */ |
|
137 | + protected function isModified(array $properties) |
|
138 | + { |
|
139 | + return $this->date != $properties['date'] |
|
140 | + || $this->content != $properties['content'] |
|
141 | + || $this->title != $properties['title']; |
|
142 | + } |
|
143 | 143 | |
144 | - /** |
|
145 | - * @return void |
|
146 | - */ |
|
147 | - protected function setProperties(WP_Post $post) |
|
148 | - { |
|
149 | - $defaults = [ |
|
150 | - 'author' => __('Anonymous', 'site-reviews'), |
|
151 | - 'date' => '', |
|
152 | - 'review_id' => '', |
|
153 | - 'review_type' => 'local', |
|
154 | - ]; |
|
155 | - $meta = array_filter( |
|
156 | - array_map('array_shift', array_filter((array) get_post_meta($post->ID))), |
|
157 | - 'strlen' |
|
158 | - ); |
|
159 | - $meta = array_merge($defaults, glsr(Helper::class)->unprefixArrayKeys($meta)); |
|
160 | - $properties = glsr(CreateReviewDefaults::class)->restrict(array_merge($defaults, $meta)); |
|
161 | - $this->modified = $this->isModified($properties); |
|
162 | - array_walk($properties, function ($value, $key) { |
|
163 | - if (!property_exists($this, $key) || isset($this->$key)) { |
|
164 | - return; |
|
165 | - } |
|
166 | - $this->$key = maybe_unserialize($value); |
|
167 | - }); |
|
168 | - } |
|
144 | + /** |
|
145 | + * @return void |
|
146 | + */ |
|
147 | + protected function setProperties(WP_Post $post) |
|
148 | + { |
|
149 | + $defaults = [ |
|
150 | + 'author' => __('Anonymous', 'site-reviews'), |
|
151 | + 'date' => '', |
|
152 | + 'review_id' => '', |
|
153 | + 'review_type' => 'local', |
|
154 | + ]; |
|
155 | + $meta = array_filter( |
|
156 | + array_map('array_shift', array_filter((array) get_post_meta($post->ID))), |
|
157 | + 'strlen' |
|
158 | + ); |
|
159 | + $meta = array_merge($defaults, glsr(Helper::class)->unprefixArrayKeys($meta)); |
|
160 | + $properties = glsr(CreateReviewDefaults::class)->restrict(array_merge($defaults, $meta)); |
|
161 | + $this->modified = $this->isModified($properties); |
|
162 | + array_walk($properties, function ($value, $key) { |
|
163 | + if (!property_exists($this, $key) || isset($this->$key)) { |
|
164 | + return; |
|
165 | + } |
|
166 | + $this->$key = maybe_unserialize($value); |
|
167 | + }); |
|
168 | + } |
|
169 | 169 | |
170 | - /** |
|
171 | - * @return void |
|
172 | - */ |
|
173 | - protected function setTermIds(WP_Post $post) |
|
174 | - { |
|
175 | - $this->term_ids = []; |
|
176 | - if (!is_array($terms = get_the_terms($post, Application::TAXONOMY))) { |
|
177 | - return; |
|
178 | - } |
|
179 | - foreach ($terms as $term) { |
|
180 | - $this->term_ids[] = $term->term_id; |
|
181 | - } |
|
182 | - } |
|
170 | + /** |
|
171 | + * @return void |
|
172 | + */ |
|
173 | + protected function setTermIds(WP_Post $post) |
|
174 | + { |
|
175 | + $this->term_ids = []; |
|
176 | + if (!is_array($terms = get_the_terms($post, Application::TAXONOMY))) { |
|
177 | + return; |
|
178 | + } |
|
179 | + foreach ($terms as $term) { |
|
180 | + $this->term_ids[] = $term->term_id; |
|
181 | + } |
|
182 | + } |
|
183 | 183 | } |
@@ -11,139 +11,139 @@ |
||
11 | 11 | |
12 | 12 | class ReviewController extends Controller |
13 | 13 | { |
14 | - /** |
|
15 | - * @param int $postId |
|
16 | - * @param array $terms |
|
17 | - * @param array $newTTIds |
|
18 | - * @param string $taxonomy |
|
19 | - * @param bool $append |
|
20 | - * @param array $oldTTIds |
|
21 | - * @return void |
|
22 | - * @action set_object_terms |
|
23 | - */ |
|
24 | - public function onAfterChangeCategory($postId, $terms, $newTTIds, $taxonomy, $append, $oldTTIds) |
|
25 | - { |
|
26 | - sort($newTTIds); |
|
27 | - sort($oldTTIds); |
|
28 | - if ($newTTIds === $oldTTIds || !$this->isReviewPostId($postId)) { |
|
29 | - return; |
|
30 | - } |
|
31 | - $review = glsr_get_review($postId); |
|
32 | - $ignoredIds = array_intersect($oldTTIds, $newTTIds); |
|
33 | - $decreasedIds = array_diff($oldTTIds, $ignoredIds); |
|
34 | - $increasedIds = array_diff($newTTIds, $ignoredIds); |
|
35 | - if ($review->term_ids = glsr(Database::class)->getTermIds($decreasedIds, 'term_taxonomy_id')) { |
|
36 | - glsr(CountsManager::class)->decreaseTermCounts($review); |
|
37 | - } |
|
38 | - if ($review->term_ids = glsr(Database::class)->getTermIds($increasedIds, 'term_taxonomy_id')) { |
|
39 | - glsr(CountsManager::class)->increaseTermCounts($review); |
|
40 | - } |
|
41 | - } |
|
14 | + /** |
|
15 | + * @param int $postId |
|
16 | + * @param array $terms |
|
17 | + * @param array $newTTIds |
|
18 | + * @param string $taxonomy |
|
19 | + * @param bool $append |
|
20 | + * @param array $oldTTIds |
|
21 | + * @return void |
|
22 | + * @action set_object_terms |
|
23 | + */ |
|
24 | + public function onAfterChangeCategory($postId, $terms, $newTTIds, $taxonomy, $append, $oldTTIds) |
|
25 | + { |
|
26 | + sort($newTTIds); |
|
27 | + sort($oldTTIds); |
|
28 | + if ($newTTIds === $oldTTIds || !$this->isReviewPostId($postId)) { |
|
29 | + return; |
|
30 | + } |
|
31 | + $review = glsr_get_review($postId); |
|
32 | + $ignoredIds = array_intersect($oldTTIds, $newTTIds); |
|
33 | + $decreasedIds = array_diff($oldTTIds, $ignoredIds); |
|
34 | + $increasedIds = array_diff($newTTIds, $ignoredIds); |
|
35 | + if ($review->term_ids = glsr(Database::class)->getTermIds($decreasedIds, 'term_taxonomy_id')) { |
|
36 | + glsr(CountsManager::class)->decreaseTermCounts($review); |
|
37 | + } |
|
38 | + if ($review->term_ids = glsr(Database::class)->getTermIds($increasedIds, 'term_taxonomy_id')) { |
|
39 | + glsr(CountsManager::class)->increaseTermCounts($review); |
|
40 | + } |
|
41 | + } |
|
42 | 42 | |
43 | - /** |
|
44 | - * @param string $oldStatus |
|
45 | - * @param string $newStatus |
|
46 | - * @param WP_Post $post |
|
47 | - * @return void |
|
48 | - * @action transition_post_status |
|
49 | - */ |
|
50 | - public function onAfterChangeStatus($newStatus, $oldStatus, $post) |
|
51 | - { |
|
52 | - if (Application::POST_TYPE != glsr_get($post, 'post_type') || in_array($oldStatus, ['new', $newStatus])) { |
|
53 | - return; |
|
54 | - } |
|
55 | - $review = glsr_get_review($post); |
|
56 | - if ('publish' == $post->post_status) { |
|
57 | - glsr(CountsManager::class)->increase($review); |
|
58 | - } else { |
|
59 | - glsr(CountsManager::class)->decrease($review); |
|
60 | - } |
|
61 | - } |
|
43 | + /** |
|
44 | + * @param string $oldStatus |
|
45 | + * @param string $newStatus |
|
46 | + * @param WP_Post $post |
|
47 | + * @return void |
|
48 | + * @action transition_post_status |
|
49 | + */ |
|
50 | + public function onAfterChangeStatus($newStatus, $oldStatus, $post) |
|
51 | + { |
|
52 | + if (Application::POST_TYPE != glsr_get($post, 'post_type') || in_array($oldStatus, ['new', $newStatus])) { |
|
53 | + return; |
|
54 | + } |
|
55 | + $review = glsr_get_review($post); |
|
56 | + if ('publish' == $post->post_status) { |
|
57 | + glsr(CountsManager::class)->increase($review); |
|
58 | + } else { |
|
59 | + glsr(CountsManager::class)->decrease($review); |
|
60 | + } |
|
61 | + } |
|
62 | 62 | |
63 | - /** |
|
64 | - * @return void |
|
65 | - * @action site-reviews/review/created |
|
66 | - */ |
|
67 | - public function onAfterCreate(Review $review) |
|
68 | - { |
|
69 | - if ('publish' !== $review->status) { |
|
70 | - return; |
|
71 | - } |
|
72 | - glsr(CountsManager::class)->increaseCounts($review); |
|
73 | - glsr(CountsManager::class)->increasePostCounts($review); |
|
74 | - } |
|
63 | + /** |
|
64 | + * @return void |
|
65 | + * @action site-reviews/review/created |
|
66 | + */ |
|
67 | + public function onAfterCreate(Review $review) |
|
68 | + { |
|
69 | + if ('publish' !== $review->status) { |
|
70 | + return; |
|
71 | + } |
|
72 | + glsr(CountsManager::class)->increaseCounts($review); |
|
73 | + glsr(CountsManager::class)->increasePostCounts($review); |
|
74 | + } |
|
75 | 75 | |
76 | - /** |
|
77 | - * @param int $postId |
|
78 | - * @return void |
|
79 | - * @action before_delete_post |
|
80 | - */ |
|
81 | - public function onBeforeDelete($postId) |
|
82 | - { |
|
83 | - if (!$this->isReviewPostId($postId)) { |
|
84 | - return; |
|
85 | - } |
|
86 | - $review = glsr_get_review($postId); |
|
87 | - if ('trash' !== $review->status) { // do not run for trashed posts |
|
88 | - glsr(CountsManager::class)->decrease($review); |
|
89 | - } |
|
90 | - } |
|
76 | + /** |
|
77 | + * @param int $postId |
|
78 | + * @return void |
|
79 | + * @action before_delete_post |
|
80 | + */ |
|
81 | + public function onBeforeDelete($postId) |
|
82 | + { |
|
83 | + if (!$this->isReviewPostId($postId)) { |
|
84 | + return; |
|
85 | + } |
|
86 | + $review = glsr_get_review($postId); |
|
87 | + if ('trash' !== $review->status) { // do not run for trashed posts |
|
88 | + glsr(CountsManager::class)->decrease($review); |
|
89 | + } |
|
90 | + } |
|
91 | 91 | |
92 | - /** |
|
93 | - * @param int $metaId |
|
94 | - * @param int $postId |
|
95 | - * @param string $metaKey |
|
96 | - * @param mixed $metaValue |
|
97 | - * @return void |
|
98 | - * @action update_postmeta |
|
99 | - */ |
|
100 | - public function onBeforeUpdate($metaId, $postId, $metaKey, $metaValue) |
|
101 | - { |
|
102 | - if (!$this->isReviewPostId($postId)) { |
|
103 | - return; |
|
104 | - } |
|
105 | - $metaKey = glsr(Helper::class)->removePrefix('_', $metaKey); |
|
106 | - if (!in_array($metaKey, ['assigned_to', 'rating', 'review_type'])) { |
|
107 | - return; |
|
108 | - } |
|
109 | - $review = glsr_get_review($postId); |
|
110 | - if ($review->$metaKey == $metaValue) { |
|
111 | - return; |
|
112 | - } |
|
113 | - $method = glsr(Helper::class)->buildMethodName($metaKey, 'onBeforeChange'); |
|
114 | - call_user_func([$this, $method], $review, $metaValue); |
|
115 | - } |
|
92 | + /** |
|
93 | + * @param int $metaId |
|
94 | + * @param int $postId |
|
95 | + * @param string $metaKey |
|
96 | + * @param mixed $metaValue |
|
97 | + * @return void |
|
98 | + * @action update_postmeta |
|
99 | + */ |
|
100 | + public function onBeforeUpdate($metaId, $postId, $metaKey, $metaValue) |
|
101 | + { |
|
102 | + if (!$this->isReviewPostId($postId)) { |
|
103 | + return; |
|
104 | + } |
|
105 | + $metaKey = glsr(Helper::class)->removePrefix('_', $metaKey); |
|
106 | + if (!in_array($metaKey, ['assigned_to', 'rating', 'review_type'])) { |
|
107 | + return; |
|
108 | + } |
|
109 | + $review = glsr_get_review($postId); |
|
110 | + if ($review->$metaKey == $metaValue) { |
|
111 | + return; |
|
112 | + } |
|
113 | + $method = glsr(Helper::class)->buildMethodName($metaKey, 'onBeforeChange'); |
|
114 | + call_user_func([$this, $method], $review, $metaValue); |
|
115 | + } |
|
116 | 116 | |
117 | - /** |
|
118 | - * @param string|int $assignedTo |
|
119 | - * @return void |
|
120 | - */ |
|
121 | - public function onBeforeChangeAssignedTo(Review $review, $assignedTo) |
|
122 | - { |
|
123 | - glsr(CountsManager::class)->decreasePostCounts($review); |
|
124 | - $review->assigned_to = $assignedTo; |
|
125 | - glsr(CountsManager::class)->increasePostCounts($review); |
|
126 | - } |
|
117 | + /** |
|
118 | + * @param string|int $assignedTo |
|
119 | + * @return void |
|
120 | + */ |
|
121 | + public function onBeforeChangeAssignedTo(Review $review, $assignedTo) |
|
122 | + { |
|
123 | + glsr(CountsManager::class)->decreasePostCounts($review); |
|
124 | + $review->assigned_to = $assignedTo; |
|
125 | + glsr(CountsManager::class)->increasePostCounts($review); |
|
126 | + } |
|
127 | 127 | |
128 | - /** |
|
129 | - * @param string|int $rating |
|
130 | - * @return void |
|
131 | - */ |
|
132 | - public function onBeforeChangeRating(Review $review, $rating) |
|
133 | - { |
|
134 | - glsr(CountsManager::class)->decrease($review); |
|
135 | - $review->rating = $rating; |
|
136 | - glsr(CountsManager::class)->increase($review); |
|
137 | - } |
|
128 | + /** |
|
129 | + * @param string|int $rating |
|
130 | + * @return void |
|
131 | + */ |
|
132 | + public function onBeforeChangeRating(Review $review, $rating) |
|
133 | + { |
|
134 | + glsr(CountsManager::class)->decrease($review); |
|
135 | + $review->rating = $rating; |
|
136 | + glsr(CountsManager::class)->increase($review); |
|
137 | + } |
|
138 | 138 | |
139 | - /** |
|
140 | - * @param string $reviewType |
|
141 | - * @return void |
|
142 | - */ |
|
143 | - public function onBeforeChangeReviewType(Review $review, $reviewType) |
|
144 | - { |
|
145 | - glsr(CountsManager::class)->decrease($review); |
|
146 | - $review->review_type = $reviewType; |
|
147 | - glsr(CountsManager::class)->increase($review); |
|
148 | - } |
|
139 | + /** |
|
140 | + * @param string $reviewType |
|
141 | + * @return void |
|
142 | + */ |
|
143 | + public function onBeforeChangeReviewType(Review $review, $reviewType) |
|
144 | + { |
|
145 | + glsr(CountsManager::class)->decrease($review); |
|
146 | + $review->review_type = $reviewType; |
|
147 | + glsr(CountsManager::class)->increase($review); |
|
148 | + } |
|
149 | 149 | } |
@@ -6,140 +6,140 @@ |
||
6 | 6 | |
7 | 7 | class CreateReview |
8 | 8 | { |
9 | - public $ajax_request; |
|
10 | - public $assigned_to; |
|
11 | - public $author; |
|
12 | - public $avatar; |
|
13 | - public $blacklisted; |
|
14 | - public $category; |
|
15 | - public $content; |
|
16 | - public $custom; |
|
17 | - public $date; |
|
18 | - public $email; |
|
19 | - public $form_id; |
|
20 | - public $ip_address; |
|
21 | - public $post_id; |
|
22 | - public $rating; |
|
23 | - public $referer; |
|
24 | - public $request; |
|
25 | - public $response; |
|
26 | - public $terms; |
|
27 | - public $title; |
|
28 | - public $url; |
|
9 | + public $ajax_request; |
|
10 | + public $assigned_to; |
|
11 | + public $author; |
|
12 | + public $avatar; |
|
13 | + public $blacklisted; |
|
14 | + public $category; |
|
15 | + public $content; |
|
16 | + public $custom; |
|
17 | + public $date; |
|
18 | + public $email; |
|
19 | + public $form_id; |
|
20 | + public $ip_address; |
|
21 | + public $post_id; |
|
22 | + public $rating; |
|
23 | + public $referer; |
|
24 | + public $request; |
|
25 | + public $response; |
|
26 | + public $terms; |
|
27 | + public $title; |
|
28 | + public $url; |
|
29 | 29 | |
30 | - public function __construct($input) |
|
31 | - { |
|
32 | - $this->request = $input; |
|
33 | - $this->ajax_request = isset($input['_ajax_request']); |
|
34 | - $this->assigned_to = $this->getNumeric('assign_to'); |
|
35 | - $this->author = sanitize_text_field($this->getUser('name')); |
|
36 | - $this->avatar = $this->getAvatar(); |
|
37 | - $this->blacklisted = isset($input['blacklisted']); |
|
38 | - $this->category = $this->getCategory(); |
|
39 | - $this->content = sanitize_textarea_field($this->get('content')); |
|
40 | - $this->custom = $this->getCustom(); |
|
41 | - $this->date = $this->getDate('date'); |
|
42 | - $this->email = sanitize_email($this->getUser('email')); |
|
43 | - $this->form_id = sanitize_key($this->get('form_id')); |
|
44 | - $this->ip_address = $this->get('ip_address'); |
|
45 | - $this->post_id = intval($this->get('_post_id')); |
|
46 | - $this->rating = intval($this->get('rating')); |
|
47 | - $this->referer = $this->get('_referer'); |
|
48 | - $this->response = sanitize_textarea_field($this->get('response')); |
|
49 | - $this->terms = !empty($input['terms']); |
|
50 | - $this->title = sanitize_text_field($this->get('title')); |
|
51 | - $this->url = esc_url_raw($this->get('url')); |
|
52 | - } |
|
30 | + public function __construct($input) |
|
31 | + { |
|
32 | + $this->request = $input; |
|
33 | + $this->ajax_request = isset($input['_ajax_request']); |
|
34 | + $this->assigned_to = $this->getNumeric('assign_to'); |
|
35 | + $this->author = sanitize_text_field($this->getUser('name')); |
|
36 | + $this->avatar = $this->getAvatar(); |
|
37 | + $this->blacklisted = isset($input['blacklisted']); |
|
38 | + $this->category = $this->getCategory(); |
|
39 | + $this->content = sanitize_textarea_field($this->get('content')); |
|
40 | + $this->custom = $this->getCustom(); |
|
41 | + $this->date = $this->getDate('date'); |
|
42 | + $this->email = sanitize_email($this->getUser('email')); |
|
43 | + $this->form_id = sanitize_key($this->get('form_id')); |
|
44 | + $this->ip_address = $this->get('ip_address'); |
|
45 | + $this->post_id = intval($this->get('_post_id')); |
|
46 | + $this->rating = intval($this->get('rating')); |
|
47 | + $this->referer = $this->get('_referer'); |
|
48 | + $this->response = sanitize_textarea_field($this->get('response')); |
|
49 | + $this->terms = !empty($input['terms']); |
|
50 | + $this->title = sanitize_text_field($this->get('title')); |
|
51 | + $this->url = esc_url_raw($this->get('url')); |
|
52 | + } |
|
53 | 53 | |
54 | - /** |
|
55 | - * @param string $key |
|
56 | - * @return string |
|
57 | - */ |
|
58 | - protected function get($key) |
|
59 | - { |
|
60 | - return (string) glsr_get($this->request, $key); |
|
61 | - } |
|
54 | + /** |
|
55 | + * @param string $key |
|
56 | + * @return string |
|
57 | + */ |
|
58 | + protected function get($key) |
|
59 | + { |
|
60 | + return (string) glsr_get($this->request, $key); |
|
61 | + } |
|
62 | 62 | |
63 | - /** |
|
64 | - * @return string |
|
65 | - */ |
|
66 | - protected function getAvatar() |
|
67 | - { |
|
68 | - $avatar = $this->get('avatar'); |
|
69 | - return !filter_var($avatar, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED) |
|
70 | - ? (string) get_avatar_url($this->get('email')) |
|
71 | - : $avatar; |
|
72 | - } |
|
63 | + /** |
|
64 | + * @return string |
|
65 | + */ |
|
66 | + protected function getAvatar() |
|
67 | + { |
|
68 | + $avatar = $this->get('avatar'); |
|
69 | + return !filter_var($avatar, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED) |
|
70 | + ? (string) get_avatar_url($this->get('email')) |
|
71 | + : $avatar; |
|
72 | + } |
|
73 | 73 | |
74 | - /** |
|
75 | - * @return string |
|
76 | - */ |
|
77 | - protected function getCategory() |
|
78 | - { |
|
79 | - $categories = glsr(Helper::class)->convertStringToArray($this->get('category')); |
|
80 | - return sanitize_key(glsr_get($categories, 0)); |
|
81 | - } |
|
74 | + /** |
|
75 | + * @return string |
|
76 | + */ |
|
77 | + protected function getCategory() |
|
78 | + { |
|
79 | + $categories = glsr(Helper::class)->convertStringToArray($this->get('category')); |
|
80 | + return sanitize_key(glsr_get($categories, 0)); |
|
81 | + } |
|
82 | 82 | |
83 | - /** |
|
84 | - * @return array |
|
85 | - */ |
|
86 | - protected function getCustom() |
|
87 | - { |
|
88 | - $unset = [ |
|
89 | - '_action', '_ajax_request', '_counter', '_nonce', '_post_id', '_recaptcha-token', |
|
90 | - '_referer', 'assign_to', 'category', 'content', 'date', 'email', 'excluded', 'form_id', |
|
91 | - 'gotcha', 'ip_address', 'name', 'rating', 'response', 'terms', 'title', 'url', |
|
92 | - ]; |
|
93 | - $unset = apply_filters('site-reviews/create/unset-keys-from-custom', $unset); |
|
94 | - $custom = $this->request; |
|
95 | - foreach ($unset as $value) { |
|
96 | - unset($custom[$value]); |
|
97 | - } |
|
98 | - return $custom; |
|
99 | - } |
|
83 | + /** |
|
84 | + * @return array |
|
85 | + */ |
|
86 | + protected function getCustom() |
|
87 | + { |
|
88 | + $unset = [ |
|
89 | + '_action', '_ajax_request', '_counter', '_nonce', '_post_id', '_recaptcha-token', |
|
90 | + '_referer', 'assign_to', 'category', 'content', 'date', 'email', 'excluded', 'form_id', |
|
91 | + 'gotcha', 'ip_address', 'name', 'rating', 'response', 'terms', 'title', 'url', |
|
92 | + ]; |
|
93 | + $unset = apply_filters('site-reviews/create/unset-keys-from-custom', $unset); |
|
94 | + $custom = $this->request; |
|
95 | + foreach ($unset as $value) { |
|
96 | + unset($custom[$value]); |
|
97 | + } |
|
98 | + return $custom; |
|
99 | + } |
|
100 | 100 | |
101 | - /** |
|
102 | - * @param string $key |
|
103 | - * @return string |
|
104 | - */ |
|
105 | - protected function getDate($key) |
|
106 | - { |
|
107 | - $date = strtotime($this->get($key)); |
|
108 | - if (false === $date) { |
|
109 | - $date = time(); |
|
110 | - } |
|
111 | - return get_date_from_gmt(gmdate('Y-m-d H:i:s', $date)); |
|
112 | - } |
|
101 | + /** |
|
102 | + * @param string $key |
|
103 | + * @return string |
|
104 | + */ |
|
105 | + protected function getDate($key) |
|
106 | + { |
|
107 | + $date = strtotime($this->get($key)); |
|
108 | + if (false === $date) { |
|
109 | + $date = time(); |
|
110 | + } |
|
111 | + return get_date_from_gmt(gmdate('Y-m-d H:i:s', $date)); |
|
112 | + } |
|
113 | 113 | |
114 | - /** |
|
115 | - * @param string $key |
|
116 | - * @return string |
|
117 | - */ |
|
118 | - protected function getUser($key) |
|
119 | - { |
|
120 | - $value = $this->get($key); |
|
121 | - if (empty($value)) { |
|
122 | - $user = wp_get_current_user(); |
|
123 | - $userValues = [ |
|
124 | - 'email' => 'user_email', |
|
125 | - 'name' => 'display_name', |
|
126 | - ]; |
|
127 | - if ($user->exists() && array_key_exists($key, $userValues)) { |
|
128 | - return $user->{$userValues[$key]}; |
|
129 | - } |
|
130 | - } |
|
131 | - return $value; |
|
132 | - } |
|
114 | + /** |
|
115 | + * @param string $key |
|
116 | + * @return string |
|
117 | + */ |
|
118 | + protected function getUser($key) |
|
119 | + { |
|
120 | + $value = $this->get($key); |
|
121 | + if (empty($value)) { |
|
122 | + $user = wp_get_current_user(); |
|
123 | + $userValues = [ |
|
124 | + 'email' => 'user_email', |
|
125 | + 'name' => 'display_name', |
|
126 | + ]; |
|
127 | + if ($user->exists() && array_key_exists($key, $userValues)) { |
|
128 | + return $user->{$userValues[$key]}; |
|
129 | + } |
|
130 | + } |
|
131 | + return $value; |
|
132 | + } |
|
133 | 133 | |
134 | - /** |
|
135 | - * @param string $key |
|
136 | - * @return string |
|
137 | - */ |
|
138 | - protected function getNumeric($key) |
|
139 | - { |
|
140 | - $value = $this->get($key); |
|
141 | - return is_numeric($value) |
|
142 | - ? $value |
|
143 | - : ''; |
|
144 | - } |
|
134 | + /** |
|
135 | + * @param string $key |
|
136 | + * @return string |
|
137 | + */ |
|
138 | + protected function getNumeric($key) |
|
139 | + { |
|
140 | + $value = $this->get($key); |
|
141 | + return is_numeric($value) |
|
142 | + ? $value |
|
143 | + : ''; |
|
144 | + } |
|
145 | 145 | } |
@@ -19,382 +19,382 @@ |
||
19 | 19 | |
20 | 20 | class SiteReviews |
21 | 21 | { |
22 | - /** |
|
23 | - * @var array |
|
24 | - */ |
|
25 | - public $args; |
|
22 | + /** |
|
23 | + * @var array |
|
24 | + */ |
|
25 | + public $args; |
|
26 | 26 | |
27 | - /** |
|
28 | - * @var Review |
|
29 | - */ |
|
30 | - public $current; |
|
27 | + /** |
|
28 | + * @var Review |
|
29 | + */ |
|
30 | + public $current; |
|
31 | 31 | |
32 | - /** |
|
33 | - * @var array |
|
34 | - */ |
|
35 | - public $options; |
|
32 | + /** |
|
33 | + * @var array |
|
34 | + */ |
|
35 | + public $options; |
|
36 | 36 | |
37 | - /** |
|
38 | - * @var Reviews |
|
39 | - */ |
|
40 | - protected $reviews; |
|
37 | + /** |
|
38 | + * @var Reviews |
|
39 | + */ |
|
40 | + protected $reviews; |
|
41 | 41 | |
42 | - /** |
|
43 | - * @param Reviews|null $reviews |
|
44 | - * @return ReviewsHtml |
|
45 | - */ |
|
46 | - public function build(array $args = [], $reviews = null) |
|
47 | - { |
|
48 | - $this->args = glsr(SiteReviewsDefaults::class)->merge($args); |
|
49 | - $this->options = glsr(Helper::class)->flattenArray(glsr(OptionManager::class)->all()); |
|
50 | - $this->reviews = $reviews instanceof Reviews |
|
51 | - ? $reviews |
|
52 | - : glsr(ReviewManager::class)->get($this->args); |
|
53 | - $this->generateSchema(); |
|
54 | - return $this->buildReviews(); |
|
55 | - } |
|
42 | + /** |
|
43 | + * @param Reviews|null $reviews |
|
44 | + * @return ReviewsHtml |
|
45 | + */ |
|
46 | + public function build(array $args = [], $reviews = null) |
|
47 | + { |
|
48 | + $this->args = glsr(SiteReviewsDefaults::class)->merge($args); |
|
49 | + $this->options = glsr(Helper::class)->flattenArray(glsr(OptionManager::class)->all()); |
|
50 | + $this->reviews = $reviews instanceof Reviews |
|
51 | + ? $reviews |
|
52 | + : glsr(ReviewManager::class)->get($this->args); |
|
53 | + $this->generateSchema(); |
|
54 | + return $this->buildReviews(); |
|
55 | + } |
|
56 | 56 | |
57 | - /** |
|
58 | - * @return ReviewHtml |
|
59 | - */ |
|
60 | - public function buildReview(Review $review) |
|
61 | - { |
|
62 | - $review = apply_filters('site-reviews/review/build/before', $review); |
|
63 | - $this->current = $review; |
|
64 | - $renderedFields = []; |
|
65 | - foreach ($review as $key => $value) { |
|
66 | - $method = glsr(Helper::class)->buildMethodName($key, 'buildOption'); |
|
67 | - $field = method_exists($this, $method) |
|
68 | - ? $this->$method($key, $value) |
|
69 | - : false; |
|
70 | - $field = apply_filters('site-reviews/review/build/'.$key, $field, $value, $this, $review); |
|
71 | - if (false === $field) { |
|
72 | - continue; |
|
73 | - } |
|
74 | - $renderedFields[$key] = $field; |
|
75 | - } |
|
76 | - $this->wrap($renderedFields, $review); |
|
77 | - $renderedFields = apply_filters('site-reviews/review/build/after', $renderedFields, $review, $this); |
|
78 | - $this->current = null; |
|
79 | - return new ReviewHtml($review, (array) $renderedFields); |
|
80 | - } |
|
57 | + /** |
|
58 | + * @return ReviewHtml |
|
59 | + */ |
|
60 | + public function buildReview(Review $review) |
|
61 | + { |
|
62 | + $review = apply_filters('site-reviews/review/build/before', $review); |
|
63 | + $this->current = $review; |
|
64 | + $renderedFields = []; |
|
65 | + foreach ($review as $key => $value) { |
|
66 | + $method = glsr(Helper::class)->buildMethodName($key, 'buildOption'); |
|
67 | + $field = method_exists($this, $method) |
|
68 | + ? $this->$method($key, $value) |
|
69 | + : false; |
|
70 | + $field = apply_filters('site-reviews/review/build/'.$key, $field, $value, $this, $review); |
|
71 | + if (false === $field) { |
|
72 | + continue; |
|
73 | + } |
|
74 | + $renderedFields[$key] = $field; |
|
75 | + } |
|
76 | + $this->wrap($renderedFields, $review); |
|
77 | + $renderedFields = apply_filters('site-reviews/review/build/after', $renderedFields, $review, $this); |
|
78 | + $this->current = null; |
|
79 | + return new ReviewHtml($review, (array) $renderedFields); |
|
80 | + } |
|
81 | 81 | |
82 | - /** |
|
83 | - * @return ReviewsHtml |
|
84 | - */ |
|
85 | - public function buildReviews() |
|
86 | - { |
|
87 | - $renderedReviews = []; |
|
88 | - foreach ($this->reviews as $index => $review) { |
|
89 | - $renderedReviews[] = $this->buildReview($review); |
|
90 | - } |
|
91 | - return new ReviewsHtml($renderedReviews, $this->reviews->max_num_pages, $this->args); |
|
92 | - } |
|
82 | + /** |
|
83 | + * @return ReviewsHtml |
|
84 | + */ |
|
85 | + public function buildReviews() |
|
86 | + { |
|
87 | + $renderedReviews = []; |
|
88 | + foreach ($this->reviews as $index => $review) { |
|
89 | + $renderedReviews[] = $this->buildReview($review); |
|
90 | + } |
|
91 | + return new ReviewsHtml($renderedReviews, $this->reviews->max_num_pages, $this->args); |
|
92 | + } |
|
93 | 93 | |
94 | - /** |
|
95 | - * @return void |
|
96 | - */ |
|
97 | - public function generateSchema() |
|
98 | - { |
|
99 | - if (!wp_validate_boolean($this->args['schema'])) { |
|
100 | - return; |
|
101 | - } |
|
102 | - glsr(Schema::class)->store( |
|
103 | - glsr(Schema::class)->build($this->args) |
|
104 | - ); |
|
105 | - } |
|
94 | + /** |
|
95 | + * @return void |
|
96 | + */ |
|
97 | + public function generateSchema() |
|
98 | + { |
|
99 | + if (!wp_validate_boolean($this->args['schema'])) { |
|
100 | + return; |
|
101 | + } |
|
102 | + glsr(Schema::class)->store( |
|
103 | + glsr(Schema::class)->build($this->args) |
|
104 | + ); |
|
105 | + } |
|
106 | 106 | |
107 | - /** |
|
108 | - * @param string $key |
|
109 | - * @param string $path |
|
110 | - * @return bool |
|
111 | - */ |
|
112 | - public function isHidden($key, $path = '') |
|
113 | - { |
|
114 | - $isOptionEnabled = !empty($path) |
|
115 | - ? $this->isOptionEnabled($path) |
|
116 | - : true; |
|
117 | - return in_array($key, $this->args['hide']) || !$isOptionEnabled; |
|
118 | - } |
|
107 | + /** |
|
108 | + * @param string $key |
|
109 | + * @param string $path |
|
110 | + * @return bool |
|
111 | + */ |
|
112 | + public function isHidden($key, $path = '') |
|
113 | + { |
|
114 | + $isOptionEnabled = !empty($path) |
|
115 | + ? $this->isOptionEnabled($path) |
|
116 | + : true; |
|
117 | + return in_array($key, $this->args['hide']) || !$isOptionEnabled; |
|
118 | + } |
|
119 | 119 | |
120 | - /** |
|
121 | - * @param string $key |
|
122 | - * @param string $value |
|
123 | - * @return void|string |
|
124 | - */ |
|
125 | - protected function buildOptionAssignedTo($key, $value) |
|
126 | - { |
|
127 | - if ($this->isHidden($key, 'settings.reviews.assigned_links')) { |
|
128 | - return; |
|
129 | - } |
|
130 | - $post = glsr(Polylang::class)->getPost($value); |
|
131 | - if (!($post instanceof WP_Post)) { |
|
132 | - return; |
|
133 | - } |
|
134 | - $permalink = glsr(Builder::class)->a(get_the_title($post->ID), [ |
|
135 | - 'href' => get_the_permalink($post->ID), |
|
136 | - ]); |
|
137 | - $assignedTo = sprintf(__('Review of %s', 'site-reviews'), $permalink); |
|
138 | - return '<span>'.$assignedTo.'</span>'; |
|
139 | - } |
|
120 | + /** |
|
121 | + * @param string $key |
|
122 | + * @param string $value |
|
123 | + * @return void|string |
|
124 | + */ |
|
125 | + protected function buildOptionAssignedTo($key, $value) |
|
126 | + { |
|
127 | + if ($this->isHidden($key, 'settings.reviews.assigned_links')) { |
|
128 | + return; |
|
129 | + } |
|
130 | + $post = glsr(Polylang::class)->getPost($value); |
|
131 | + if (!($post instanceof WP_Post)) { |
|
132 | + return; |
|
133 | + } |
|
134 | + $permalink = glsr(Builder::class)->a(get_the_title($post->ID), [ |
|
135 | + 'href' => get_the_permalink($post->ID), |
|
136 | + ]); |
|
137 | + $assignedTo = sprintf(__('Review of %s', 'site-reviews'), $permalink); |
|
138 | + return '<span>'.$assignedTo.'</span>'; |
|
139 | + } |
|
140 | 140 | |
141 | - /** |
|
142 | - * @param string $key |
|
143 | - * @param string $value |
|
144 | - * @return void|string |
|
145 | - */ |
|
146 | - protected function buildOptionAuthor($key, $value) |
|
147 | - { |
|
148 | - if (!$this->isHidden($key)) { |
|
149 | - $name = glsr(Helper::class)->convertName( |
|
150 | - $value, |
|
151 | - glsr_get_option('reviews.name.format'), |
|
152 | - glsr_get_option('reviews.name.initial') |
|
153 | - ); |
|
154 | - return '<span>'.$name.'</span>'; |
|
155 | - } |
|
156 | - } |
|
141 | + /** |
|
142 | + * @param string $key |
|
143 | + * @param string $value |
|
144 | + * @return void|string |
|
145 | + */ |
|
146 | + protected function buildOptionAuthor($key, $value) |
|
147 | + { |
|
148 | + if (!$this->isHidden($key)) { |
|
149 | + $name = glsr(Helper::class)->convertName( |
|
150 | + $value, |
|
151 | + glsr_get_option('reviews.name.format'), |
|
152 | + glsr_get_option('reviews.name.initial') |
|
153 | + ); |
|
154 | + return '<span>'.$name.'</span>'; |
|
155 | + } |
|
156 | + } |
|
157 | 157 | |
158 | - /** |
|
159 | - * @param string $key |
|
160 | - * @param string $value |
|
161 | - * @return void|string |
|
162 | - */ |
|
163 | - protected function buildOptionAvatar($key, $value) |
|
164 | - { |
|
165 | - if ($this->isHidden($key, 'settings.reviews.avatars')) { |
|
166 | - return; |
|
167 | - } |
|
168 | - $size = $this->getOption('settings.reviews.avatars_size', 40); |
|
169 | - return glsr(Builder::class)->img([ |
|
170 | - 'height' => $size, |
|
171 | - 'src' => $this->generateAvatar($value), |
|
172 | - 'style' => sprintf('width:%1$spx; height:%1$spx;', $size), |
|
173 | - 'width' => $size, |
|
174 | - ]); |
|
175 | - } |
|
158 | + /** |
|
159 | + * @param string $key |
|
160 | + * @param string $value |
|
161 | + * @return void|string |
|
162 | + */ |
|
163 | + protected function buildOptionAvatar($key, $value) |
|
164 | + { |
|
165 | + if ($this->isHidden($key, 'settings.reviews.avatars')) { |
|
166 | + return; |
|
167 | + } |
|
168 | + $size = $this->getOption('settings.reviews.avatars_size', 40); |
|
169 | + return glsr(Builder::class)->img([ |
|
170 | + 'height' => $size, |
|
171 | + 'src' => $this->generateAvatar($value), |
|
172 | + 'style' => sprintf('width:%1$spx; height:%1$spx;', $size), |
|
173 | + 'width' => $size, |
|
174 | + ]); |
|
175 | + } |
|
176 | 176 | |
177 | - /** |
|
178 | - * @param string $key |
|
179 | - * @param string $value |
|
180 | - * @return void|string |
|
181 | - */ |
|
182 | - protected function buildOptionContent($key, $value) |
|
183 | - { |
|
184 | - $text = $this->normalizeText($value); |
|
185 | - if (!$this->isHiddenOrEmpty($key, $text)) { |
|
186 | - return '<p>'.$text.'</p>'; |
|
187 | - } |
|
188 | - } |
|
177 | + /** |
|
178 | + * @param string $key |
|
179 | + * @param string $value |
|
180 | + * @return void|string |
|
181 | + */ |
|
182 | + protected function buildOptionContent($key, $value) |
|
183 | + { |
|
184 | + $text = $this->normalizeText($value); |
|
185 | + if (!$this->isHiddenOrEmpty($key, $text)) { |
|
186 | + return '<p>'.$text.'</p>'; |
|
187 | + } |
|
188 | + } |
|
189 | 189 | |
190 | - /** |
|
191 | - * @param string $key |
|
192 | - * @param string $value |
|
193 | - * @return void|string |
|
194 | - */ |
|
195 | - protected function buildOptionDate($key, $value) |
|
196 | - { |
|
197 | - if ($this->isHidden($key)) { |
|
198 | - return; |
|
199 | - } |
|
200 | - $dateFormat = $this->getOption('settings.reviews.date.format', 'default'); |
|
201 | - if ('relative' == $dateFormat) { |
|
202 | - $date = glsr(Date::class)->relative($value); |
|
203 | - } else { |
|
204 | - $format = 'custom' == $dateFormat |
|
205 | - ? $this->getOption('settings.reviews.date.custom', 'M j, Y') |
|
206 | - : glsr(OptionManager::class)->getWP('date_format', 'F j, Y'); |
|
207 | - $date = date_i18n($format, strtotime($value)); |
|
208 | - } |
|
209 | - return '<span>'.$date.'</span>'; |
|
210 | - } |
|
190 | + /** |
|
191 | + * @param string $key |
|
192 | + * @param string $value |
|
193 | + * @return void|string |
|
194 | + */ |
|
195 | + protected function buildOptionDate($key, $value) |
|
196 | + { |
|
197 | + if ($this->isHidden($key)) { |
|
198 | + return; |
|
199 | + } |
|
200 | + $dateFormat = $this->getOption('settings.reviews.date.format', 'default'); |
|
201 | + if ('relative' == $dateFormat) { |
|
202 | + $date = glsr(Date::class)->relative($value); |
|
203 | + } else { |
|
204 | + $format = 'custom' == $dateFormat |
|
205 | + ? $this->getOption('settings.reviews.date.custom', 'M j, Y') |
|
206 | + : glsr(OptionManager::class)->getWP('date_format', 'F j, Y'); |
|
207 | + $date = date_i18n($format, strtotime($value)); |
|
208 | + } |
|
209 | + return '<span>'.$date.'</span>'; |
|
210 | + } |
|
211 | 211 | |
212 | - /** |
|
213 | - * @param string $key |
|
214 | - * @param string $value |
|
215 | - * @return void|string |
|
216 | - */ |
|
217 | - protected function buildOptionRating($key, $value) |
|
218 | - { |
|
219 | - if (!$this->isHiddenOrEmpty($key, $value)) { |
|
220 | - return glsr_star_rating($value); |
|
221 | - } |
|
222 | - } |
|
212 | + /** |
|
213 | + * @param string $key |
|
214 | + * @param string $value |
|
215 | + * @return void|string |
|
216 | + */ |
|
217 | + protected function buildOptionRating($key, $value) |
|
218 | + { |
|
219 | + if (!$this->isHiddenOrEmpty($key, $value)) { |
|
220 | + return glsr_star_rating($value); |
|
221 | + } |
|
222 | + } |
|
223 | 223 | |
224 | - /** |
|
225 | - * @param string $key |
|
226 | - * @param string $value |
|
227 | - * @return void|string |
|
228 | - */ |
|
229 | - protected function buildOptionResponse($key, $value) |
|
230 | - { |
|
231 | - if ($this->isHiddenOrEmpty($key, $value)) { |
|
232 | - return; |
|
233 | - } |
|
234 | - $title = sprintf(__('Response from %s', 'site-reviews'), get_bloginfo('name')); |
|
235 | - $text = $this->normalizeText($value); |
|
236 | - $text = '<p><strong>'.$title.'</strong></p><p>'.$text.'</p>'; |
|
237 | - $response = glsr(Builder::class)->div($text, ['class' => 'glsr-review-response-inner']); |
|
238 | - $background = glsr(Builder::class)->div(['class' => 'glsr-review-response-background']); |
|
239 | - return $response.$background; |
|
240 | - } |
|
224 | + /** |
|
225 | + * @param string $key |
|
226 | + * @param string $value |
|
227 | + * @return void|string |
|
228 | + */ |
|
229 | + protected function buildOptionResponse($key, $value) |
|
230 | + { |
|
231 | + if ($this->isHiddenOrEmpty($key, $value)) { |
|
232 | + return; |
|
233 | + } |
|
234 | + $title = sprintf(__('Response from %s', 'site-reviews'), get_bloginfo('name')); |
|
235 | + $text = $this->normalizeText($value); |
|
236 | + $text = '<p><strong>'.$title.'</strong></p><p>'.$text.'</p>'; |
|
237 | + $response = glsr(Builder::class)->div($text, ['class' => 'glsr-review-response-inner']); |
|
238 | + $background = glsr(Builder::class)->div(['class' => 'glsr-review-response-background']); |
|
239 | + return $response.$background; |
|
240 | + } |
|
241 | 241 | |
242 | - /** |
|
243 | - * @param string $key |
|
244 | - * @param string $value |
|
245 | - * @return void|string |
|
246 | - */ |
|
247 | - protected function buildOptionTitle($key, $value) |
|
248 | - { |
|
249 | - if ($this->isHidden($key)) { |
|
250 | - return; |
|
251 | - } |
|
252 | - if (empty($value)) { |
|
253 | - $value = __('No Title', 'site-reviews'); |
|
254 | - } |
|
255 | - return '<h3>'.$value.'</h3>'; |
|
256 | - } |
|
242 | + /** |
|
243 | + * @param string $key |
|
244 | + * @param string $value |
|
245 | + * @return void|string |
|
246 | + */ |
|
247 | + protected function buildOptionTitle($key, $value) |
|
248 | + { |
|
249 | + if ($this->isHidden($key)) { |
|
250 | + return; |
|
251 | + } |
|
252 | + if (empty($value)) { |
|
253 | + $value = __('No Title', 'site-reviews'); |
|
254 | + } |
|
255 | + return '<h3>'.$value.'</h3>'; |
|
256 | + } |
|
257 | 257 | |
258 | - /** |
|
259 | - * @param string $avatarUrl |
|
260 | - * @return string |
|
261 | - */ |
|
262 | - protected function generateAvatar($avatarUrl) |
|
263 | - { |
|
264 | - if (!$this->isOptionEnabled('settings.reviews.avatars_regenerate') || 'local' != $this->current->review_type) { |
|
265 | - return $avatarUrl; |
|
266 | - } |
|
267 | - $authorIdOrEmail = get_the_author_meta('ID', $this->current->user_id); |
|
268 | - if (empty($authorIdOrEmail)) { |
|
269 | - $authorIdOrEmail = $this->current->email; |
|
270 | - } |
|
271 | - if ($newAvatar = get_avatar_url($authorIdOrEmail)) { |
|
272 | - return $newAvatar; |
|
273 | - } |
|
274 | - return $avatarUrl; |
|
275 | - } |
|
258 | + /** |
|
259 | + * @param string $avatarUrl |
|
260 | + * @return string |
|
261 | + */ |
|
262 | + protected function generateAvatar($avatarUrl) |
|
263 | + { |
|
264 | + if (!$this->isOptionEnabled('settings.reviews.avatars_regenerate') || 'local' != $this->current->review_type) { |
|
265 | + return $avatarUrl; |
|
266 | + } |
|
267 | + $authorIdOrEmail = get_the_author_meta('ID', $this->current->user_id); |
|
268 | + if (empty($authorIdOrEmail)) { |
|
269 | + $authorIdOrEmail = $this->current->email; |
|
270 | + } |
|
271 | + if ($newAvatar = get_avatar_url($authorIdOrEmail)) { |
|
272 | + return $newAvatar; |
|
273 | + } |
|
274 | + return $avatarUrl; |
|
275 | + } |
|
276 | 276 | |
277 | - /** |
|
278 | - * @param string $text |
|
279 | - * @return string |
|
280 | - */ |
|
281 | - protected function getExcerpt($text) |
|
282 | - { |
|
283 | - $limit = intval($this->getOption('settings.reviews.excerpts_length', 55)); |
|
284 | - $split = extension_loaded('intl') |
|
285 | - ? $this->getExcerptIntlSplit($text, $limit) |
|
286 | - : $this->getExcerptSplit($text, $limit); |
|
287 | - $hiddenText = substr($text, $split); |
|
288 | - if (!empty($hiddenText)) { |
|
289 | - $showMore = glsr(Builder::class)->span($hiddenText, [ |
|
290 | - 'class' => 'glsr-hidden glsr-hidden-text', |
|
291 | - 'data-show-less' => __('Show less', 'site-reviews'), |
|
292 | - 'data-show-more' => __('Show more', 'site-reviews'), |
|
293 | - ]); |
|
294 | - $text = ltrim(substr($text, 0, $split)).$showMore; |
|
295 | - } |
|
296 | - return $text; |
|
297 | - } |
|
277 | + /** |
|
278 | + * @param string $text |
|
279 | + * @return string |
|
280 | + */ |
|
281 | + protected function getExcerpt($text) |
|
282 | + { |
|
283 | + $limit = intval($this->getOption('settings.reviews.excerpts_length', 55)); |
|
284 | + $split = extension_loaded('intl') |
|
285 | + ? $this->getExcerptIntlSplit($text, $limit) |
|
286 | + : $this->getExcerptSplit($text, $limit); |
|
287 | + $hiddenText = substr($text, $split); |
|
288 | + if (!empty($hiddenText)) { |
|
289 | + $showMore = glsr(Builder::class)->span($hiddenText, [ |
|
290 | + 'class' => 'glsr-hidden glsr-hidden-text', |
|
291 | + 'data-show-less' => __('Show less', 'site-reviews'), |
|
292 | + 'data-show-more' => __('Show more', 'site-reviews'), |
|
293 | + ]); |
|
294 | + $text = ltrim(substr($text, 0, $split)).$showMore; |
|
295 | + } |
|
296 | + return $text; |
|
297 | + } |
|
298 | 298 | |
299 | - /** |
|
300 | - * @param string $text |
|
301 | - * @param int $limit |
|
302 | - * @return int |
|
303 | - */ |
|
304 | - protected function getExcerptIntlSplit($text, $limit) |
|
305 | - { |
|
306 | - $words = IntlRuleBasedBreakIterator::createWordInstance(''); |
|
307 | - $words->setText($text); |
|
308 | - $count = 0; |
|
309 | - foreach ($words as $offset) { |
|
310 | - if (IntlRuleBasedBreakIterator::WORD_NONE === $words->getRuleStatus()) { |
|
311 | - continue; |
|
312 | - } |
|
313 | - ++$count; |
|
314 | - if ($count != $limit) { |
|
315 | - continue; |
|
316 | - } |
|
317 | - return $offset; |
|
318 | - } |
|
319 | - return strlen($text); |
|
320 | - } |
|
299 | + /** |
|
300 | + * @param string $text |
|
301 | + * @param int $limit |
|
302 | + * @return int |
|
303 | + */ |
|
304 | + protected function getExcerptIntlSplit($text, $limit) |
|
305 | + { |
|
306 | + $words = IntlRuleBasedBreakIterator::createWordInstance(''); |
|
307 | + $words->setText($text); |
|
308 | + $count = 0; |
|
309 | + foreach ($words as $offset) { |
|
310 | + if (IntlRuleBasedBreakIterator::WORD_NONE === $words->getRuleStatus()) { |
|
311 | + continue; |
|
312 | + } |
|
313 | + ++$count; |
|
314 | + if ($count != $limit) { |
|
315 | + continue; |
|
316 | + } |
|
317 | + return $offset; |
|
318 | + } |
|
319 | + return strlen($text); |
|
320 | + } |
|
321 | 321 | |
322 | - /** |
|
323 | - * @param string $text |
|
324 | - * @param int $limit |
|
325 | - * @return int |
|
326 | - */ |
|
327 | - protected function getExcerptSplit($text, $limit) |
|
328 | - { |
|
329 | - if (str_word_count($text, 0) > $limit) { |
|
330 | - $words = array_keys(str_word_count($text, 2)); |
|
331 | - return $words[$limit]; |
|
332 | - } |
|
333 | - return strlen($text); |
|
334 | - } |
|
322 | + /** |
|
323 | + * @param string $text |
|
324 | + * @param int $limit |
|
325 | + * @return int |
|
326 | + */ |
|
327 | + protected function getExcerptSplit($text, $limit) |
|
328 | + { |
|
329 | + if (str_word_count($text, 0) > $limit) { |
|
330 | + $words = array_keys(str_word_count($text, 2)); |
|
331 | + return $words[$limit]; |
|
332 | + } |
|
333 | + return strlen($text); |
|
334 | + } |
|
335 | 335 | |
336 | - /** |
|
337 | - * @param string $path |
|
338 | - * @param mixed $fallback |
|
339 | - * @return mixed |
|
340 | - */ |
|
341 | - protected function getOption($path, $fallback = '') |
|
342 | - { |
|
343 | - if (array_key_exists($path, $this->options)) { |
|
344 | - return $this->options[$path]; |
|
345 | - } |
|
346 | - return $fallback; |
|
347 | - } |
|
336 | + /** |
|
337 | + * @param string $path |
|
338 | + * @param mixed $fallback |
|
339 | + * @return mixed |
|
340 | + */ |
|
341 | + protected function getOption($path, $fallback = '') |
|
342 | + { |
|
343 | + if (array_key_exists($path, $this->options)) { |
|
344 | + return $this->options[$path]; |
|
345 | + } |
|
346 | + return $fallback; |
|
347 | + } |
|
348 | 348 | |
349 | - /** |
|
350 | - * @param string $key |
|
351 | - * @param string $value |
|
352 | - * @return bool |
|
353 | - */ |
|
354 | - protected function isHiddenOrEmpty($key, $value) |
|
355 | - { |
|
356 | - return $this->isHidden($key) || empty($value); |
|
357 | - } |
|
349 | + /** |
|
350 | + * @param string $key |
|
351 | + * @param string $value |
|
352 | + * @return bool |
|
353 | + */ |
|
354 | + protected function isHiddenOrEmpty($key, $value) |
|
355 | + { |
|
356 | + return $this->isHidden($key) || empty($value); |
|
357 | + } |
|
358 | 358 | |
359 | - /** |
|
360 | - * @param string $path |
|
361 | - * @return bool |
|
362 | - */ |
|
363 | - protected function isOptionEnabled($path) |
|
364 | - { |
|
365 | - return 'yes' == $this->getOption($path); |
|
366 | - } |
|
359 | + /** |
|
360 | + * @param string $path |
|
361 | + * @return bool |
|
362 | + */ |
|
363 | + protected function isOptionEnabled($path) |
|
364 | + { |
|
365 | + return 'yes' == $this->getOption($path); |
|
366 | + } |
|
367 | 367 | |
368 | - /** |
|
369 | - * @param string $text |
|
370 | - * @return string |
|
371 | - */ |
|
372 | - protected function normalizeText($text) |
|
373 | - { |
|
374 | - $text = wp_kses($text, wp_kses_allowed_html()); |
|
375 | - $text = convert_smilies(strip_shortcodes($text)); |
|
376 | - $text = str_replace(']]>', ']]>', $text); |
|
377 | - $text = preg_replace('/(\R){2,}/', '$1', $text); |
|
378 | - if ($this->isOptionEnabled('settings.reviews.excerpts')) { |
|
379 | - $text = $this->getExcerpt($text); |
|
380 | - } |
|
381 | - return wptexturize(nl2br($text)); |
|
382 | - } |
|
368 | + /** |
|
369 | + * @param string $text |
|
370 | + * @return string |
|
371 | + */ |
|
372 | + protected function normalizeText($text) |
|
373 | + { |
|
374 | + $text = wp_kses($text, wp_kses_allowed_html()); |
|
375 | + $text = convert_smilies(strip_shortcodes($text)); |
|
376 | + $text = str_replace(']]>', ']]>', $text); |
|
377 | + $text = preg_replace('/(\R){2,}/', '$1', $text); |
|
378 | + if ($this->isOptionEnabled('settings.reviews.excerpts')) { |
|
379 | + $text = $this->getExcerpt($text); |
|
380 | + } |
|
381 | + return wptexturize(nl2br($text)); |
|
382 | + } |
|
383 | 383 | |
384 | - /** |
|
385 | - * @return void |
|
386 | - */ |
|
387 | - protected function wrap(array &$renderedFields, Review $review) |
|
388 | - { |
|
389 | - $renderedFields = apply_filters('site-reviews/review/wrap', $renderedFields, $review, $this); |
|
390 | - array_walk($renderedFields, function (&$value, $key) use ($review) { |
|
391 | - $value = apply_filters('site-reviews/review/wrap/'.$key, $value, $review); |
|
392 | - if (empty($value)) { |
|
393 | - return; |
|
394 | - } |
|
395 | - $value = glsr(Builder::class)->div($value, [ |
|
396 | - 'class' => 'glsr-review-'.$key, |
|
397 | - ]); |
|
398 | - }); |
|
399 | - } |
|
384 | + /** |
|
385 | + * @return void |
|
386 | + */ |
|
387 | + protected function wrap(array &$renderedFields, Review $review) |
|
388 | + { |
|
389 | + $renderedFields = apply_filters('site-reviews/review/wrap', $renderedFields, $review, $this); |
|
390 | + array_walk($renderedFields, function (&$value, $key) use ($review) { |
|
391 | + $value = apply_filters('site-reviews/review/wrap/'.$key, $value, $review); |
|
392 | + if (empty($value)) { |
|
393 | + return; |
|
394 | + } |
|
395 | + $value = glsr(Builder::class)->div($value, [ |
|
396 | + 'class' => 'glsr-review-'.$key, |
|
397 | + ]); |
|
398 | + }); |
|
399 | + } |
|
400 | 400 | } |
@@ -9,69 +9,69 @@ |
||
9 | 9 | |
10 | 10 | class Upgrade_4_0_2 |
11 | 11 | { |
12 | - use Arr; |
|
12 | + use Arr; |
|
13 | 13 | |
14 | - public function __construct() |
|
15 | - { |
|
16 | - $this->migrateSettings(); |
|
17 | - $this->protectMetaKeys(); |
|
18 | - $this->deleteSessions(); |
|
19 | - delete_transient(Application::ID.'_cloudflare_ips'); |
|
20 | - } |
|
14 | + public function __construct() |
|
15 | + { |
|
16 | + $this->migrateSettings(); |
|
17 | + $this->protectMetaKeys(); |
|
18 | + $this->deleteSessions(); |
|
19 | + delete_transient(Application::ID.'_cloudflare_ips'); |
|
20 | + } |
|
21 | 21 | |
22 | - /** |
|
23 | - * @return void |
|
24 | - */ |
|
25 | - public function deleteSessions() |
|
26 | - { |
|
27 | - global $wpdb; |
|
28 | - $wpdb->query(" |
|
22 | + /** |
|
23 | + * @return void |
|
24 | + */ |
|
25 | + public function deleteSessions() |
|
26 | + { |
|
27 | + global $wpdb; |
|
28 | + $wpdb->query(" |
|
29 | 29 | DELETE |
30 | 30 | FROM {$wpdb->options} |
31 | 31 | WHERE option_name LIKE '_glsr_session%' |
32 | 32 | "); |
33 | - } |
|
33 | + } |
|
34 | 34 | |
35 | - /** |
|
36 | - * @return void |
|
37 | - */ |
|
38 | - public function migrateSettings() |
|
39 | - { |
|
40 | - if ($settings = get_option(OptionManager::databaseKey(3))) { |
|
41 | - $multilingual = 'yes' == $this->dataGet($settings, 'settings.general.support.polylang') |
|
42 | - ? 'polylang' |
|
43 | - : ''; |
|
44 | - $settings = $this->dataSet($settings, 'settings.general.multilingual', $multilingual); |
|
45 | - $settings = $this->dataSet($settings, 'settings.general.rebusify', 'no'); |
|
46 | - $settings = $this->dataSet($settings, 'settings.general.rebusify_email', ''); |
|
47 | - $settings = $this->dataSet($settings, 'settings.general.rebusify_serial', ''); |
|
48 | - $settings = $this->dataSet($settings, 'settings.reviews.name.format', ''); |
|
49 | - $settings = $this->dataSet($settings, 'settings.reviews.name.initial', ''); |
|
50 | - $settings = $this->dataSet($settings, 'settings.submissions.blacklist.integration', ''); |
|
51 | - $settings = $this->dataSet($settings, 'settings.submissions.limit', ''); |
|
52 | - $settings = $this->dataSet($settings, 'settings.submissions.limit_whitelist.email', ''); |
|
53 | - $settings = $this->dataSet($settings, 'settings.submissions.limit_whitelist.ip_address', ''); |
|
54 | - $settings = $this->dataSet($settings, 'settings.submissions.limit_whitelist.username', ''); |
|
55 | - unset($settings['settings']['general']['support']); |
|
56 | - update_option(OptionManager::databaseKey(4), $settings); |
|
57 | - } |
|
58 | - } |
|
35 | + /** |
|
36 | + * @return void |
|
37 | + */ |
|
38 | + public function migrateSettings() |
|
39 | + { |
|
40 | + if ($settings = get_option(OptionManager::databaseKey(3))) { |
|
41 | + $multilingual = 'yes' == $this->dataGet($settings, 'settings.general.support.polylang') |
|
42 | + ? 'polylang' |
|
43 | + : ''; |
|
44 | + $settings = $this->dataSet($settings, 'settings.general.multilingual', $multilingual); |
|
45 | + $settings = $this->dataSet($settings, 'settings.general.rebusify', 'no'); |
|
46 | + $settings = $this->dataSet($settings, 'settings.general.rebusify_email', ''); |
|
47 | + $settings = $this->dataSet($settings, 'settings.general.rebusify_serial', ''); |
|
48 | + $settings = $this->dataSet($settings, 'settings.reviews.name.format', ''); |
|
49 | + $settings = $this->dataSet($settings, 'settings.reviews.name.initial', ''); |
|
50 | + $settings = $this->dataSet($settings, 'settings.submissions.blacklist.integration', ''); |
|
51 | + $settings = $this->dataSet($settings, 'settings.submissions.limit', ''); |
|
52 | + $settings = $this->dataSet($settings, 'settings.submissions.limit_whitelist.email', ''); |
|
53 | + $settings = $this->dataSet($settings, 'settings.submissions.limit_whitelist.ip_address', ''); |
|
54 | + $settings = $this->dataSet($settings, 'settings.submissions.limit_whitelist.username', ''); |
|
55 | + unset($settings['settings']['general']['support']); |
|
56 | + update_option(OptionManager::databaseKey(4), $settings); |
|
57 | + } |
|
58 | + } |
|
59 | 59 | |
60 | - /** |
|
61 | - * @return void |
|
62 | - */ |
|
63 | - public function protectMetaKeys() |
|
64 | - { |
|
65 | - global $wpdb; |
|
66 | - $keys = array_keys(glsr(CreateReviewDefaults::class)->defaults()); |
|
67 | - $keys = implode("','", $keys); |
|
68 | - $postType = Application::POST_TYPE; |
|
69 | - $wpdb->query(" |
|
60 | + /** |
|
61 | + * @return void |
|
62 | + */ |
|
63 | + public function protectMetaKeys() |
|
64 | + { |
|
65 | + global $wpdb; |
|
66 | + $keys = array_keys(glsr(CreateReviewDefaults::class)->defaults()); |
|
67 | + $keys = implode("','", $keys); |
|
68 | + $postType = Application::POST_TYPE; |
|
69 | + $wpdb->query(" |
|
70 | 70 | UPDATE {$wpdb->postmeta} pm |
71 | 71 | INNER JOIN {$wpdb->posts} p ON p.id = pm.post_id |
72 | 72 | SET pm.meta_key = CONCAT('_', pm.meta_key) |
73 | 73 | WHERE pm.meta_key IN ('{$keys}') |
74 | 74 | AND p.post_type = '{$postType}' |
75 | 75 | "); |
76 | - } |
|
76 | + } |
|
77 | 77 | } |
@@ -8,97 +8,97 @@ |
||
8 | 8 | |
9 | 9 | class NoticeController extends Controller |
10 | 10 | { |
11 | - const USER_META_KEY = '_glsr_notices'; |
|
11 | + const USER_META_KEY = '_glsr_notices'; |
|
12 | 12 | |
13 | - /** |
|
14 | - * @return void |
|
15 | - * @action admin_notices |
|
16 | - */ |
|
17 | - public function filterAdminNotices() |
|
18 | - { |
|
19 | - $screen = glsr_current_screen(); |
|
20 | - $this->renderWelcomeNotice($screen->post_type); |
|
21 | - $this->renderRebusifyNotice($screen->post_type); |
|
22 | - $this->renderAddonsNotice($screen->id); |
|
23 | - } |
|
13 | + /** |
|
14 | + * @return void |
|
15 | + * @action admin_notices |
|
16 | + */ |
|
17 | + public function filterAdminNotices() |
|
18 | + { |
|
19 | + $screen = glsr_current_screen(); |
|
20 | + $this->renderWelcomeNotice($screen->post_type); |
|
21 | + $this->renderRebusifyNotice($screen->post_type); |
|
22 | + $this->renderAddonsNotice($screen->id); |
|
23 | + } |
|
24 | 24 | |
25 | - /** |
|
26 | - * @return void |
|
27 | - */ |
|
28 | - public function routerDismissNotice(array $request) |
|
29 | - { |
|
30 | - if ($key = glsr_get($request, 'notice')) { |
|
31 | - $this->dismissNotice($key); |
|
32 | - } |
|
33 | - } |
|
25 | + /** |
|
26 | + * @return void |
|
27 | + */ |
|
28 | + public function routerDismissNotice(array $request) |
|
29 | + { |
|
30 | + if ($key = glsr_get($request, 'notice')) { |
|
31 | + $this->dismissNotice($key); |
|
32 | + } |
|
33 | + } |
|
34 | 34 | |
35 | - /** |
|
36 | - * @param string $key |
|
37 | - * @return void |
|
38 | - */ |
|
39 | - protected function dismissNotice($key) |
|
40 | - { |
|
41 | - $this->setUserMeta($key, glsr()->version('major')); |
|
42 | - } |
|
35 | + /** |
|
36 | + * @param string $key |
|
37 | + * @return void |
|
38 | + */ |
|
39 | + protected function dismissNotice($key) |
|
40 | + { |
|
41 | + $this->setUserMeta($key, glsr()->version('major')); |
|
42 | + } |
|
43 | 43 | |
44 | - /** |
|
45 | - * @param string $key |
|
46 | - * @param mixed $fallback |
|
47 | - * @return mixed |
|
48 | - */ |
|
49 | - protected function getUserMeta($key, $fallback) |
|
50 | - { |
|
51 | - $meta = get_user_meta(get_current_user_id(), static::USER_META_KEY, true); |
|
52 | - return glsr_get($meta, $key, $fallback); |
|
53 | - } |
|
44 | + /** |
|
45 | + * @param string $key |
|
46 | + * @param mixed $fallback |
|
47 | + * @return mixed |
|
48 | + */ |
|
49 | + protected function getUserMeta($key, $fallback) |
|
50 | + { |
|
51 | + $meta = get_user_meta(get_current_user_id(), static::USER_META_KEY, true); |
|
52 | + return glsr_get($meta, $key, $fallback); |
|
53 | + } |
|
54 | 54 | |
55 | - /** |
|
56 | - * @param string $screenId |
|
57 | - * @return void |
|
58 | - */ |
|
59 | - protected function renderAddonsNotice($screenId) |
|
60 | - { |
|
61 | - if (Application::POST_TYPE.'_page_addons' == $screenId) { |
|
62 | - echo glsr()->render('partials/notices/addons'); |
|
63 | - } |
|
64 | - } |
|
55 | + /** |
|
56 | + * @param string $screenId |
|
57 | + * @return void |
|
58 | + */ |
|
59 | + protected function renderAddonsNotice($screenId) |
|
60 | + { |
|
61 | + if (Application::POST_TYPE.'_page_addons' == $screenId) { |
|
62 | + echo glsr()->render('partials/notices/addons'); |
|
63 | + } |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * @param string $screenPostType |
|
68 | - * @return void |
|
69 | - */ |
|
70 | - protected function renderRebusifyNotice($screenPostType) |
|
71 | - { |
|
72 | - if (Application::POST_TYPE == $screenPostType |
|
73 | - && version_compare(glsr()->version('major'), $this->getUserMeta('rebusify', 0), '>') |
|
74 | - && !glsr(OptionManager::class)->getBool('settings.general.rebusify')) { |
|
75 | - echo glsr()->render('partials/notices/rebusify'); |
|
76 | - } |
|
77 | - } |
|
66 | + /** |
|
67 | + * @param string $screenPostType |
|
68 | + * @return void |
|
69 | + */ |
|
70 | + protected function renderRebusifyNotice($screenPostType) |
|
71 | + { |
|
72 | + if (Application::POST_TYPE == $screenPostType |
|
73 | + && version_compare(glsr()->version('major'), $this->getUserMeta('rebusify', 0), '>') |
|
74 | + && !glsr(OptionManager::class)->getBool('settings.general.rebusify')) { |
|
75 | + echo glsr()->render('partials/notices/rebusify'); |
|
76 | + } |
|
77 | + } |
|
78 | 78 | |
79 | - /** |
|
80 | - * @param string $screenPostType |
|
81 | - * @return void |
|
82 | - */ |
|
83 | - protected function renderWelcomeNotice($screenPostType) |
|
84 | - { |
|
85 | - if (Application::POST_TYPE == $screenPostType |
|
86 | - && version_compare(glsr()->version('major'), $this->getUserMeta('welcome', 0), '>')) { |
|
87 | - echo glsr()->render('partials/notices/welcome'); |
|
88 | - } |
|
89 | - } |
|
79 | + /** |
|
80 | + * @param string $screenPostType |
|
81 | + * @return void |
|
82 | + */ |
|
83 | + protected function renderWelcomeNotice($screenPostType) |
|
84 | + { |
|
85 | + if (Application::POST_TYPE == $screenPostType |
|
86 | + && version_compare(glsr()->version('major'), $this->getUserMeta('welcome', 0), '>')) { |
|
87 | + echo glsr()->render('partials/notices/welcome'); |
|
88 | + } |
|
89 | + } |
|
90 | 90 | |
91 | - /** |
|
92 | - * @param string $key |
|
93 | - * @param mixed $fallback |
|
94 | - * @return mixed |
|
95 | - */ |
|
96 | - protected function setUserMeta($key, $value) |
|
97 | - { |
|
98 | - $userId = get_current_user_id(); |
|
99 | - $meta = (array) get_user_meta($userId, static::USER_META_KEY, true); |
|
100 | - $meta = array_filter(wp_parse_args($meta, [])); |
|
101 | - $meta[$key] = $value; |
|
102 | - update_user_meta($userId, static::USER_META_KEY, $meta); |
|
103 | - } |
|
91 | + /** |
|
92 | + * @param string $key |
|
93 | + * @param mixed $fallback |
|
94 | + * @return mixed |
|
95 | + */ |
|
96 | + protected function setUserMeta($key, $value) |
|
97 | + { |
|
98 | + $userId = get_current_user_id(); |
|
99 | + $meta = (array) get_user_meta($userId, static::USER_META_KEY, true); |
|
100 | + $meta = array_filter(wp_parse_args($meta, [])); |
|
101 | + $meta[$key] = $value; |
|
102 | + update_user_meta($userId, static::USER_META_KEY, $meta); |
|
103 | + } |
|
104 | 104 | } |
@@ -9,124 +9,124 @@ |
||
9 | 9 | |
10 | 10 | class Helper |
11 | 11 | { |
12 | - use Arr; |
|
13 | - use Str; |
|
12 | + use Arr; |
|
13 | + use Str; |
|
14 | 14 | |
15 | - /** |
|
16 | - * @param string $name |
|
17 | - * @param string $path |
|
18 | - * @return string |
|
19 | - */ |
|
20 | - public function buildClassName($name, $path = '') |
|
21 | - { |
|
22 | - $className = $this->camelCase($name); |
|
23 | - $path = ltrim(str_replace(__NAMESPACE__, '', $path), '\\'); |
|
24 | - return !empty($path) |
|
25 | - ? __NAMESPACE__.'\\'.$path.'\\'.$className |
|
26 | - : $className; |
|
27 | - } |
|
15 | + /** |
|
16 | + * @param string $name |
|
17 | + * @param string $path |
|
18 | + * @return string |
|
19 | + */ |
|
20 | + public function buildClassName($name, $path = '') |
|
21 | + { |
|
22 | + $className = $this->camelCase($name); |
|
23 | + $path = ltrim(str_replace(__NAMESPACE__, '', $path), '\\'); |
|
24 | + return !empty($path) |
|
25 | + ? __NAMESPACE__.'\\'.$path.'\\'.$className |
|
26 | + : $className; |
|
27 | + } |
|
28 | 28 | |
29 | - /** |
|
30 | - * @param string $name |
|
31 | - * @param string $prefix |
|
32 | - * @return string |
|
33 | - */ |
|
34 | - public function buildMethodName($name, $prefix = '') |
|
35 | - { |
|
36 | - return lcfirst($prefix.$this->buildClassName($name)); |
|
37 | - } |
|
29 | + /** |
|
30 | + * @param string $name |
|
31 | + * @param string $prefix |
|
32 | + * @return string |
|
33 | + */ |
|
34 | + public function buildMethodName($name, $prefix = '') |
|
35 | + { |
|
36 | + return lcfirst($prefix.$this->buildClassName($name)); |
|
37 | + } |
|
38 | 38 | |
39 | - /** |
|
40 | - * @param string $name |
|
41 | - * @return string |
|
42 | - */ |
|
43 | - public function buildPropertyName($name) |
|
44 | - { |
|
45 | - return lcfirst($this->buildClassName($name)); |
|
46 | - } |
|
39 | + /** |
|
40 | + * @param string $name |
|
41 | + * @return string |
|
42 | + */ |
|
43 | + public function buildPropertyName($name) |
|
44 | + { |
|
45 | + return lcfirst($this->buildClassName($name)); |
|
46 | + } |
|
47 | 47 | |
48 | - /** |
|
49 | - * @param string $cast |
|
50 | - * @param mixed $value |
|
51 | - * @return mixed |
|
52 | - */ |
|
53 | - public function castTo($cast = '', $value) |
|
54 | - { |
|
55 | - switch ($cast) { |
|
56 | - case 'array': |
|
57 | - return (array) $value; |
|
58 | - case 'bool': |
|
59 | - case 'boolean': |
|
60 | - return filter_var($value, FILTER_VALIDATE_BOOLEAN); |
|
61 | - case 'float': |
|
62 | - return (float) filter_var($value, FILTER_VALIDATE_FLOAT, FILTER_FLAG_ALLOW_THOUSAND); |
|
63 | - case 'int': |
|
64 | - case 'integer': |
|
65 | - return (int) filter_var($value, FILTER_VALIDATE_INT); |
|
66 | - case 'object': |
|
67 | - return (object) (array) $value; |
|
68 | - case 'str': |
|
69 | - case 'string': |
|
70 | - if (is_object($value) && in_array('__toString', get_class_methods($value))) { |
|
71 | - return (string) $value->__toString(); |
|
72 | - } |
|
73 | - if (is_array($value) || is_object($value)) { |
|
74 | - return serialize($value); |
|
75 | - } |
|
76 | - return (string) $value; |
|
77 | - default: |
|
78 | - return $value; |
|
79 | - } |
|
80 | - } |
|
48 | + /** |
|
49 | + * @param string $cast |
|
50 | + * @param mixed $value |
|
51 | + * @return mixed |
|
52 | + */ |
|
53 | + public function castTo($cast = '', $value) |
|
54 | + { |
|
55 | + switch ($cast) { |
|
56 | + case 'array': |
|
57 | + return (array) $value; |
|
58 | + case 'bool': |
|
59 | + case 'boolean': |
|
60 | + return filter_var($value, FILTER_VALIDATE_BOOLEAN); |
|
61 | + case 'float': |
|
62 | + return (float) filter_var($value, FILTER_VALIDATE_FLOAT, FILTER_FLAG_ALLOW_THOUSAND); |
|
63 | + case 'int': |
|
64 | + case 'integer': |
|
65 | + return (int) filter_var($value, FILTER_VALIDATE_INT); |
|
66 | + case 'object': |
|
67 | + return (object) (array) $value; |
|
68 | + case 'str': |
|
69 | + case 'string': |
|
70 | + if (is_object($value) && in_array('__toString', get_class_methods($value))) { |
|
71 | + return (string) $value->__toString(); |
|
72 | + } |
|
73 | + if (is_array($value) || is_object($value)) { |
|
74 | + return serialize($value); |
|
75 | + } |
|
76 | + return (string) $value; |
|
77 | + default: |
|
78 | + return $value; |
|
79 | + } |
|
80 | + } |
|
81 | 81 | |
82 | - /** |
|
83 | - * @param string $key |
|
84 | - * @return mixed |
|
85 | - */ |
|
86 | - public function filterInput($key, array $request = []) |
|
87 | - { |
|
88 | - if (isset($request[$key])) { |
|
89 | - return $request[$key]; |
|
90 | - } |
|
91 | - $variable = filter_input(INPUT_POST, $key); |
|
92 | - if (is_null($variable) && isset($_POST[$key])) { |
|
93 | - $variable = $_POST[$key]; |
|
94 | - } |
|
95 | - return $variable; |
|
96 | - } |
|
82 | + /** |
|
83 | + * @param string $key |
|
84 | + * @return mixed |
|
85 | + */ |
|
86 | + public function filterInput($key, array $request = []) |
|
87 | + { |
|
88 | + if (isset($request[$key])) { |
|
89 | + return $request[$key]; |
|
90 | + } |
|
91 | + $variable = filter_input(INPUT_POST, $key); |
|
92 | + if (is_null($variable) && isset($_POST[$key])) { |
|
93 | + $variable = $_POST[$key]; |
|
94 | + } |
|
95 | + return $variable; |
|
96 | + } |
|
97 | 97 | |
98 | - /** |
|
99 | - * @param string $key |
|
100 | - * @return array |
|
101 | - */ |
|
102 | - public function filterInputArray($key) |
|
103 | - { |
|
104 | - $variable = filter_input(INPUT_POST, $key, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
105 | - if (empty($variable) && !empty($_POST[$key]) && is_array($_POST[$key])) { |
|
106 | - $variable = $_POST[$key]; |
|
107 | - } |
|
108 | - return (array) $variable; |
|
109 | - } |
|
98 | + /** |
|
99 | + * @param string $key |
|
100 | + * @return array |
|
101 | + */ |
|
102 | + public function filterInputArray($key) |
|
103 | + { |
|
104 | + $variable = filter_input(INPUT_POST, $key, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
105 | + if (empty($variable) && !empty($_POST[$key]) && is_array($_POST[$key])) { |
|
106 | + $variable = $_POST[$key]; |
|
107 | + } |
|
108 | + return (array) $variable; |
|
109 | + } |
|
110 | 110 | |
111 | - /** |
|
112 | - * @return string |
|
113 | - */ |
|
114 | - public function getIpAddress() |
|
115 | - { |
|
116 | - $cloudflareIps = glsr(Cache::class)->getCloudflareIps(); |
|
117 | - $whitelist = [ |
|
118 | - Whip::CLOUDFLARE_HEADERS => [Whip::IPV4 => $cloudflareIps['v4']], |
|
119 | - // Whip::CUSTOM_HEADERS => [Whip::IPV4 => ['127.0.0.1']], |
|
120 | - ]; |
|
121 | - if (defined('AF_INET6')) { |
|
122 | - $whitelist[Whip::CLOUDFLARE_HEADERS][Whip::IPV6] = $cloudflareIps['v6']; |
|
123 | - // $whitelist[Whip::CUSTOM_HEADERS][Whip::IPV6] = ['::1']; |
|
124 | - } |
|
125 | - $whitelist = apply_filters('site-reviews/whip/whitelist', $whitelist); |
|
126 | - $methods = Whip::CUSTOM_HEADERS | Whip::CLOUDFLARE_HEADERS | Whip::REMOTE_ADDR; |
|
127 | - $methods = apply_filters('site-reviews/whip/methods', $methods); |
|
128 | - $whip = new Whip($methods, $whitelist); |
|
129 | - do_action_ref_array('site-reviews/whip', [$whip]); |
|
130 | - return (string) $whip->getValidIpAddress(); |
|
131 | - } |
|
111 | + /** |
|
112 | + * @return string |
|
113 | + */ |
|
114 | + public function getIpAddress() |
|
115 | + { |
|
116 | + $cloudflareIps = glsr(Cache::class)->getCloudflareIps(); |
|
117 | + $whitelist = [ |
|
118 | + Whip::CLOUDFLARE_HEADERS => [Whip::IPV4 => $cloudflareIps['v4']], |
|
119 | + // Whip::CUSTOM_HEADERS => [Whip::IPV4 => ['127.0.0.1']], |
|
120 | + ]; |
|
121 | + if (defined('AF_INET6')) { |
|
122 | + $whitelist[Whip::CLOUDFLARE_HEADERS][Whip::IPV6] = $cloudflareIps['v6']; |
|
123 | + // $whitelist[Whip::CUSTOM_HEADERS][Whip::IPV6] = ['::1']; |
|
124 | + } |
|
125 | + $whitelist = apply_filters('site-reviews/whip/whitelist', $whitelist); |
|
126 | + $methods = Whip::CUSTOM_HEADERS | Whip::CLOUDFLARE_HEADERS | Whip::REMOTE_ADDR; |
|
127 | + $methods = apply_filters('site-reviews/whip/methods', $methods); |
|
128 | + $whip = new Whip($methods, $whitelist); |
|
129 | + do_action_ref_array('site-reviews/whip', [$whip]); |
|
130 | + return (string) $whip->getValidIpAddress(); |
|
131 | + } |
|
132 | 132 | } |