1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Integrations\WooCommerce\Controllers; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Controllers\AbstractController; |
6
|
|
|
use GeminiLabs\SiteReviews\Helpers\Arr; |
7
|
|
|
use GeminiLabs\SiteReviews\Helpers\Cast; |
8
|
|
|
use GeminiLabs\SiteReviews\Integrations\WooCommerce\Elementor\Widgets\ProductRating; |
9
|
|
|
use GeminiLabs\SiteReviews\Integrations\WooCommerce\Widgets\WidgetRatingFilter; |
10
|
|
|
use GeminiLabs\SiteReviews\Integrations\WooCommerce\Widgets\WidgetRecentReviews; |
11
|
|
|
use GeminiLabs\SiteReviews\Modules\SchemaParser; |
12
|
|
|
use GeminiLabs\SiteReviews\Review; |
13
|
|
|
|
14
|
|
|
class MainController extends AbstractController |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @action admin_enqueue_scripts |
18
|
|
|
*/ |
19
|
|
|
public function enqueueInlineAdminStyles(): void |
20
|
|
|
{ |
21
|
|
|
$css = '.woocommerce-review-activity-card .woocommerce-activity-card__actions button.is-tertiary:not(.is-destructive) {display:none}'; |
22
|
|
|
wp_add_inline_style('wc-admin-app', $css); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @filter site-reviews/enqueue/public/inline-styles |
27
|
|
|
*/ |
28
|
|
|
public function filterInlineStyles(string $css): string |
29
|
|
|
{ |
30
|
|
|
$css .= 'ul.glsr li a{display:flex;justify-content:space-between;}'; // fix rating filter widget |
31
|
|
|
$css .= '.glsr.woocommerce-product-rating{align-items:center;display:inline-flex;gap:.5em;}'; |
32
|
|
|
$css .= '.glsr.woocommerce-product-rating .woocommerce-review-link{top:-1px!important;}'; // fix product title rating position |
33
|
|
|
$style = glsr_get_option('integrations.woocommerce.style'); |
34
|
|
|
$colors = [ |
35
|
|
|
'black' => '#212121', |
36
|
|
|
'woocommerce' => '#96588A', |
37
|
|
|
]; |
38
|
|
|
if (!array_key_exists($style, $colors)) { |
39
|
|
|
return $css; |
40
|
|
|
} |
41
|
|
|
$css = str_replace('assets/images/stars/default/', "assets/images/stars/{$style}/", $css); |
42
|
|
|
$css .= ".glsr:not([data-theme]) .glsr-bar-background-percent{--glsr-bar-bg:{$colors[$style]};}"; |
43
|
|
|
return $css; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return int |
48
|
|
|
* |
49
|
|
|
* @filter woocommerce_product_reviews_pending_count |
50
|
|
|
*/ |
51
|
|
|
public function filterMenuPendingCount() |
52
|
|
|
{ |
53
|
|
|
return 0; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $status |
58
|
|
|
* @param string $postType |
59
|
|
|
* @param string $commentType |
60
|
|
|
* |
61
|
|
|
* @return string |
62
|
|
|
* |
63
|
|
|
* @filter get_default_comment_status |
64
|
|
|
*/ |
65
|
|
|
public function filterProductCommentStatus($status, $postType, $commentType) |
66
|
|
|
{ |
67
|
|
|
if ('product' === $postType && 'comment' === $commentType) { |
68
|
|
|
return 'open'; |
69
|
|
|
} |
70
|
|
|
return $status; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param array $settings |
75
|
|
|
* @param string $section |
76
|
|
|
* |
77
|
|
|
* @return array |
78
|
|
|
* |
79
|
|
|
* @filter woocommerce_get_settings_products |
80
|
|
|
*/ |
81
|
|
|
public function filterProductSettings($settings, $section) |
82
|
|
|
{ |
83
|
|
|
if (!empty($section)) { |
84
|
|
|
return $settings; |
85
|
|
|
} |
86
|
|
|
$disabled = ['woocommerce_enable_review_rating', 'woocommerce_review_rating_required']; |
87
|
|
|
foreach ($settings as &$setting) { |
88
|
|
|
if (in_array(Arr::get($setting, 'id'), $disabled)) { |
89
|
|
|
$setting = Arr::set($setting, 'custom_attributes.disabled', true); |
90
|
|
|
$setting['desc'] = sprintf('%s <span class="required">(%s)</span>', |
91
|
|
|
$setting['desc'], |
92
|
|
|
_x('managed by Site Reviews', 'admin-text', 'site-reviews') |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
return $settings; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @filter site-reviews/enqueue/public/inline-script/after |
101
|
|
|
*/ |
102
|
|
|
public function filterPublicInlineScript(string $script): string |
103
|
|
|
{ |
104
|
|
|
$script .= '"undefined"!==typeof jQuery&&(jQuery(".wc-tabs .reviews_tab a").on("click",function(){setTimeout(function(){GLSR.Event.trigger("site-reviews-themes/swiper/resize")},25)}));'; |
105
|
|
|
return $script; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @filter site-reviews/schema/generate |
110
|
|
|
*/ |
111
|
|
|
public function filterRankmathSchemaPreview(array $data, SchemaParser $parser): array |
112
|
|
|
{ |
113
|
|
|
if (!did_action('rank_math/json_ld/preview')) { |
114
|
|
|
return $data; // only run this for the preview |
115
|
|
|
} |
116
|
|
|
if (!$url = wp_get_referer()) { |
117
|
|
|
return $data; |
118
|
|
|
} |
119
|
|
|
$urlQuery = (string) wp_parse_url($url, \PHP_URL_QUERY); |
120
|
|
|
$query = wp_parse_args($urlQuery); |
121
|
|
|
$postId = Cast::toInt($query['post'] ?? 0); |
122
|
|
|
if (!$product = wc_get_product($postId)) { |
123
|
|
|
return $data; |
124
|
|
|
} |
125
|
|
|
if (!$product->get_reviews_allowed()) { |
126
|
|
|
return $data; |
127
|
|
|
} |
128
|
|
|
return $parser->buildReviewSchema([ |
129
|
|
|
'assigned_posts' => $postId, |
130
|
|
|
]); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return string |
135
|
|
|
* |
136
|
|
|
* @filter option_woocommerce_enable_review_rating |
137
|
|
|
* @filter option_woocommerce_review_rating_required |
138
|
|
|
*/ |
139
|
|
|
public function filterRatingOption() |
140
|
|
|
{ |
141
|
|
|
return 'yes'; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param \GeminiLabs\SiteReviews\Modules\Html\Tags\ReviewAuthorTag $tag |
146
|
|
|
* |
147
|
|
|
* @filter site-reviews/review/value/author |
148
|
|
|
*/ |
149
|
|
|
public function filterReviewAuthorTagValue(string $value, $tag): string |
150
|
|
|
{ |
151
|
|
|
if ($tag->review->hasVerifiedOwner() && 'yes' === get_option('woocommerce_review_rating_verification_label')) { // @phpstan-ignore-line |
152
|
|
|
$text = esc_attr__('verified owner', 'site-reviews'); |
153
|
|
|
$value = sprintf('%s <em class="woocommerce-review__verified verified">(%s)</em>', $value, $text); |
154
|
|
|
} |
155
|
|
|
return $value; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @filter site-reviews/review/call/hasVerifiedOwner |
160
|
|
|
*/ |
161
|
|
|
public function filterReviewCallbackHasVerifiedOwner(Review $review): bool |
162
|
|
|
{ |
163
|
|
|
$verified = get_post_meta($review->ID, '_verified', true); |
164
|
|
|
if ('' !== $verified) { |
165
|
|
|
return (bool) $verified; |
166
|
|
|
} |
167
|
|
|
$review->refresh(); // refresh the review first! |
168
|
|
|
$verified = false; |
169
|
|
|
foreach ($review->assigned_posts as $postId) { |
170
|
|
|
if ('product' === get_post_type($postId)) { |
171
|
|
|
$verified = wc_customer_bought_product($review->email, $review->author_id, $postId); |
172
|
|
|
break; // only check the first product |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
update_post_meta($review->ID, '_verified', (int) $verified); |
176
|
|
|
return $verified; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @filter site-reviews/review/call/product |
181
|
|
|
*/ |
182
|
|
|
public function filterReviewCallbackProduct(Review $review): ?\WC_Product |
183
|
|
|
{ |
184
|
|
|
foreach ($review->assigned_posts as $postId) { |
185
|
|
|
if ('product' !== get_post_type($postId)) { |
186
|
|
|
continue; |
187
|
|
|
} |
188
|
|
|
if ($product = wc_get_product($postId)) { |
189
|
|
|
return $product; // only return the first found product |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
return null; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @action admin_init |
197
|
|
|
*/ |
198
|
|
|
public function redirectProductReviews(): void |
199
|
|
|
{ |
200
|
|
|
global $pagenow; |
201
|
|
|
if ('edit.php' === $pagenow |
202
|
|
|
&& 'product' === filter_input(INPUT_GET, 'post_type') |
203
|
|
|
&& 'product-reviews' === filter_input(INPUT_GET, 'page')) { |
204
|
|
|
wp_redirect(add_query_arg('notice', 'product-reviews', glsr_admin_url()), 301); |
205
|
|
|
exit; |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @action elementor/widgets/register |
211
|
|
|
*/ |
212
|
|
|
public function registerElementorWidgets(): void |
213
|
|
|
{ |
214
|
|
|
$widgets = \Elementor\Plugin::instance()->widgets_manager; |
215
|
|
|
$widgets->unregister('woocommerce-product-rating'); |
216
|
|
|
if (class_exists('ElementorPro\Modules\Woocommerce\Widgets\Product_Rating')) { |
217
|
|
|
$widgets->register(new ProductRating()); |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @action widgets_init |
223
|
|
|
*/ |
224
|
|
|
public function registerWidgets(): void |
225
|
|
|
{ |
226
|
|
|
unregister_widget('WC_Widget_Recent_Reviews'); |
227
|
|
|
unregister_widget('WC_Widget_Rating_Filter'); |
228
|
|
|
register_widget(WidgetRecentReviews::class); |
229
|
|
|
register_widget(WidgetRatingFilter::class); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @param array $args |
234
|
|
|
* |
235
|
|
|
* @return array |
236
|
|
|
* |
237
|
|
|
* @action woocommerce_register_post_type_product |
238
|
|
|
*/ |
239
|
|
|
public function removeWoocommerceReviews($args) |
240
|
|
|
{ |
241
|
|
|
if (array_key_exists('supports', $args)) { |
242
|
|
|
$args['supports'] = array_diff($args['supports'], ['comments']); |
243
|
|
|
} |
244
|
|
|
return $args; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @action admin_notices |
249
|
|
|
*/ |
250
|
|
|
public function renderNotice(): void |
251
|
|
|
{ |
252
|
|
|
$screen = glsr_current_screen(); |
253
|
|
|
if ('edit' !== $screen->base || 'edit-site-review' !== $screen->id) { |
254
|
|
|
return; |
255
|
|
|
} |
256
|
|
|
if ('product-reviews' !== filter_input(INPUT_GET, 'notice')) { |
257
|
|
|
return; |
258
|
|
|
} |
259
|
|
|
glsr()->render('integrations/woocommerce/notices/reviews'); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* @action site-reviews/review/created |
264
|
|
|
*/ |
265
|
|
|
public function verifyProductOwner(Review $review): void |
266
|
|
|
{ |
267
|
|
|
$review->hasVerifiedOwner(); |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|