Total Complexity | 67 |
Total Lines | 384 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 1 | Features | 0 |
Complex classes like ProductController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ProductController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class ProductController implements ControllerContract |
||
24 | { |
||
25 | use HookProxy; |
||
26 | |||
27 | /** |
||
28 | * @filter render_block_core/shortcode |
||
29 | */ |
||
30 | public function filterAssignedPostsPostId(string $content, array $parsedBlock, ?\WP_Block $parentBlock): string |
||
31 | { |
||
32 | $postId = $parentBlock->context['postId'] ?? 0; |
||
33 | $postType = $parentBlock->context['postType'] ?? ''; |
||
34 | if ('sc_product' !== $postType) { |
||
35 | return $content; |
||
36 | } |
||
37 | if (!str_contains($content, '[site_review')) { |
||
38 | return $content; |
||
39 | } |
||
40 | if (!str_contains($content, 'assigned_posts')) { |
||
41 | return $content; |
||
42 | } |
||
43 | if (!str_contains($content, 'post_id')) { |
||
44 | return $content; |
||
45 | } |
||
46 | $pattern = '/(assigned_posts\s*=\s*(["\']?))(.*?)\2(?=\s|$)/'; |
||
47 | return preg_replace_callback($pattern, function ($match) use ($postId) { |
||
48 | $value = preg_replace('/\bpost_id\b/', $postId, $match[3]); |
||
49 | return $match[1].$value.$match[2]; |
||
50 | }, $content); |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * @filter block_type_metadata_settings |
||
55 | */ |
||
56 | public function filterBlockRenderCallback(array $settings, array $metadata): array |
||
99 | } |
||
100 | |||
101 | public function filterPaginatedLink(array $link, array $args, BuilderContract $builder, Paginate $paginate): array |
||
102 | { |
||
103 | $type = $link['type'] ?? ''; |
||
104 | if (!in_array($type, ['next', 'prev'])) { |
||
105 | return $link; |
||
106 | } |
||
107 | $referer = urldecode(wp_get_referer()); |
||
108 | if (str_contains($referer, 'site-editor.php')) { |
||
109 | parse_str(parse_url($referer, PHP_URL_QUERY), $params); |
||
110 | $template = $params['p'] ?? ''; |
||
111 | if (!str_ends_with($template, 'surecart//single-sc_product')) { |
||
112 | return $link; |
||
113 | } |
||
114 | } else { |
||
115 | $baseUrl = str_replace('%_%', '', $paginate->args->base); |
||
116 | $postId = url_to_postid($baseUrl); |
||
117 | if ('sc_product' !== get_post_type($postId)) { |
||
118 | return $link; |
||
119 | } |
||
120 | } |
||
121 | if ('prev' === $type) { |
||
122 | $svg = \SureCart::svg()->get('arrow-left', ['aria-hidden' => true]); // @phpstan-ignore-line |
||
123 | $svg = wp_kses($svg, sc_allowed_svg_html()); |
||
124 | $args['text'] = $svg.$args['text']; |
||
125 | if (1 >= $paginate->args->current) { |
||
126 | $tag = 'span'; |
||
127 | } |
||
128 | } |
||
129 | if ('next' === $type) { |
||
130 | $svg = \SureCart::svg()->get('arrow-right', ['aria-hidden' => true]); // @phpstan-ignore-line |
||
131 | $svg = wp_kses($svg, sc_allowed_svg_html()); |
||
132 | $args['text'] = $args['text'].$svg; |
||
133 | if ($paginate->args->current >= $paginate->args->total) { |
||
134 | $tag = 'span'; |
||
135 | } |
||
136 | } |
||
137 | $link['link'] = $builder->build('div', [ |
||
138 | 'data-type' => $type, |
||
139 | 'text' => $builder->build($tag ?? 'a', $args), |
||
140 | ]); |
||
141 | return $link; |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * @param string[] $columns |
||
146 | * |
||
147 | * @filter manage_sc-products_columns |
||
148 | */ |
||
149 | public function filterProductColumns(array $columns): array |
||
150 | { |
||
151 | $svg = Svg::get('assets/images/icon.svg', [ |
||
152 | 'height' => 24, |
||
153 | 'style' => 'display:flex; flex-shrink:0; margin: -4px 0;', |
||
154 | ]); |
||
155 | $columns[glsr()->prefix.'rating'] = glsr(Builder::class)->div([ |
||
156 | 'style' => 'display:flex; align-items:center; justify-content:center;', |
||
157 | 'text' => sprintf('%s<span>%s</span>', $svg, _x('Reviews', 'admin-text', 'site-reviews')), |
||
158 | ]); |
||
159 | return $columns; |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * @filter surecart/product/json_schema |
||
164 | */ |
||
165 | public function filterProductSchema(array $schema): array |
||
166 | { |
||
167 | $data = glsr(SchemaParser::class)->generate(); |
||
168 | $aggregateRatingSchema = Arr::get($data, 'aggregateRating'); |
||
169 | $reviewSchema = Arr::get($data, 'review'); |
||
170 | if ($aggregateRatingSchema) { |
||
171 | $schema['aggregateRating'] = $aggregateRatingSchema; |
||
172 | } |
||
173 | if ($reviewSchema) { |
||
174 | $schema['review'] = $reviewSchema; |
||
175 | } |
||
176 | // remove Site Reviews generated schema |
||
177 | add_filter('site-reviews/schema/all', '__return_empty_array'); |
||
178 | return $schema; |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * @filter site-reviews/review/value/author |
||
183 | */ |
||
184 | public function filterReviewAuthorTagValue(string $value, ReviewTag $tag): string |
||
185 | { |
||
186 | $ownership = glsr_get_option('integrations.surecart.ownership', [], 'array'); |
||
187 | if (!in_array('labeled', $ownership)) { |
||
188 | return $value; |
||
189 | } |
||
190 | if ($tag->review->hasProductOwner()) { |
||
191 | $text = esc_attr__('verified owner', 'site-reviews'); |
||
192 | $value = sprintf('%s <em data-verified-owner="1">(%s)</em>', $value, $text); |
||
193 | } |
||
194 | return $value; |
||
195 | } |
||
196 | |||
197 | /** |
||
198 | * @filter site-reviews/review/call/hasProductOwner |
||
199 | */ |
||
200 | public function filterReviewCallbackHasProductOwner(Review $review): bool |
||
201 | { |
||
202 | $verified = get_post_meta($review->ID, '_sc_verified', true); |
||
203 | if ('' !== $verified) { |
||
204 | return (bool) $verified; |
||
205 | } |
||
206 | $review->refresh(); // refresh the review first! |
||
207 | $verified = false; |
||
208 | foreach ($review->assigned_posts as $postId) { |
||
209 | if ('sc_product' === get_post_type($postId)) { |
||
210 | $verified = $this->isProductOwner($review->author_id, $postId); |
||
211 | break; // only check the first product |
||
212 | } |
||
213 | } |
||
214 | update_post_meta($review->ID, '_sc_verified', (int) $verified); |
||
215 | return $verified; |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * @filter site-reviews/build/template/reviews-form |
||
220 | */ |
||
221 | public function filterReviewFormBuild(string $template, array $data): string |
||
222 | { |
||
223 | if ('sc_product' !== get_post_type()) { |
||
224 | return $template; |
||
225 | } |
||
226 | $ownership = glsr_get_option('integrations.surecart.ownership', [], 'array'); |
||
227 | if (!in_array('restricted', $ownership)) { |
||
228 | return $template; |
||
229 | } |
||
230 | if ($this->isProductOwner(get_current_user_id(), get_the_ID())) { |
||
231 | return $template; |
||
232 | } |
||
233 | return glsr(Builder::class)->p([ |
||
234 | 'text' => esc_html__('Only logged in customers who have purchased this product may leave a review.', 'woocommerce'), |
||
235 | ]); |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | * @param \GeminiLabs\SiteReviews\Modules\Html\ReviewField[] $fields |
||
240 | * |
||
241 | * @return \GeminiLabs\SiteReviews\Modules\Html\ReviewField[] |
||
242 | * |
||
243 | * @filter site-reviews/review-form/fields/visible |
||
244 | */ |
||
245 | public function filterReviewFormFields(array $fields, ReviewForm $form): array |
||
246 | { |
||
247 | if (!is_user_logged_in()) { |
||
248 | return $fields; |
||
249 | } |
||
250 | if ('sc_product' !== get_post_type()) { |
||
251 | return $fields; |
||
252 | } |
||
253 | $user = wp_get_current_user(); |
||
254 | array_walk($fields, function ($field) use ($form, $user) { |
||
255 | if (in_array($field->original_name, $form->args()->hide)) { |
||
256 | return; |
||
257 | } |
||
258 | if ('email' === $field->original_name && empty($field->value)) { |
||
259 | $field->value = glsr(Sanitizer::class)->sanitizeUserEmail($user->user_email); |
||
260 | return; |
||
261 | } |
||
262 | if ('name' === $field->original_name && empty($field->value)) { |
||
263 | $field->value = glsr(Sanitizer::class)->sanitizeUserName( |
||
264 | $user->display_name, |
||
265 | $user->user_nicename |
||
266 | ); |
||
267 | } |
||
268 | }); |
||
269 | return $fields; |
||
270 | } |
||
271 | |||
272 | /** |
||
273 | * @filter site-reviews/shortcode/site_reviews/attributes |
||
274 | * @filter site-reviews/shortcode/site_reviews_form/attributes |
||
275 | * @filter site-reviews/shortcode/site_reviews_summary/attributes |
||
276 | */ |
||
277 | public function filterShortcodeAttributes(array $attributes, ShortcodeContract $shortcode): array |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * @action parse_query |
||
292 | */ |
||
293 | public function parseProductQuery(\WP_Query $query): void |
||
312 | } |
||
313 | } |
||
314 | |||
315 | /** |
||
316 | * @action init:11 |
||
317 | */ |
||
318 | public function registerBlocks(): void |
||
319 | { |
||
320 | register_block_type_from_metadata(glsr()->path('assets/blocks/surecart_product_rating')); |
||
321 | register_block_type_from_metadata(glsr()->path('assets/blocks/surecart_product_reviews')); |
||
322 | } |
||
323 | |||
324 | /** |
||
325 | * @filter surecart/product/attributes_set |
||
326 | */ |
||
327 | public function registerProductAttributes(Product $product): void |
||
328 | { |
||
329 | $postId = $product->post->ID ?? 0; |
||
330 | if (0 === $postId) { |
||
331 | return; |
||
332 | } |
||
333 | $ratingInfo = glsr_get_ratings([ |
||
334 | 'assigned_posts' => $postId, |
||
335 | ]); |
||
336 | $product->setAttribute('rating', $ratingInfo->average); |
||
337 | $product->setAttribute('reviews', $ratingInfo->reviews); |
||
338 | $product->setAttribute('ranking', $ratingInfo->ranking); |
||
339 | } |
||
340 | |||
341 | /** |
||
342 | * @action manage_sc-products_custom_column |
||
343 | */ |
||
344 | public function renderProductColumnValues(string $column, $product): void |
||
345 | { |
||
346 | if (glsr()->prefix.'rating' !== $column) { |
||
347 | return; |
||
348 | } |
||
349 | echo glsr(Builder::class)->a([ |
||
350 | 'href' => add_query_arg('assigned_post', $product->post->ID, glsr_admin_url()), |
||
351 | 'text' => $product->reviews, |
||
352 | ]); |
||
353 | } |
||
354 | |||
355 | /** |
||
356 | * @param string $which |
||
357 | * |
||
358 | * @action manage_products_extra_tablenav |
||
359 | */ |
||
360 | public function renderProductTableInlineStyles($which): void |
||
361 | { |
||
362 | if ('top' !== $which) { |
||
363 | return; |
||
364 | } |
||
365 | echo '<style>'. |
||
366 | '@media screen and (min-width: 783px) {'. |
||
367 | '.fixed .column-glsr_rating { width: 5%; }'. |
||
368 | 'th.column-glsr_rating span { display: none; }'. |
||
369 | 'td.column-glsr_rating { text-align: center; }'. |
||
370 | '}'. |
||
371 | '</style>'; |
||
372 | } |
||
373 | |||
374 | /** |
||
375 | * @action site-reviews/review/created |
||
376 | */ |
||
377 | public function verifyProductOwner(Review $review): void |
||
378 | { |
||
379 | $review->hasProductOwner(); |
||
380 | } |
||
381 | |||
382 | protected function buildMetaQuery(string $orderbyKey, string $metaKey): array |
||
388 | ]; |
||
389 | } |
||
390 | |||
391 | protected function isProductOwner(int $userId, int $productId): bool |
||
392 | { |
||
393 | if (!$user = User::getUserBy('id', $userId)) { // @phpstan-ignore-line |
||
409 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths