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\Integrations\WooCommerce\Elementor\Widgets\ProductRating; |
8
|
|
|
use GeminiLabs\SiteReviews\Integrations\WooCommerce\Widgets\WidgetRatingFilter; |
9
|
|
|
use GeminiLabs\SiteReviews\Integrations\WooCommerce\Widgets\WidgetRecentReviews; |
10
|
|
|
use GeminiLabs\SiteReviews\Review; |
11
|
|
|
|
12
|
|
|
class MainController extends AbstractController |
13
|
|
|
{ |
14
|
|
|
public const VERIFIED_META_KEY = '_verified'; |
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
|
|
|
* @return string |
110
|
|
|
* |
111
|
|
|
* @filter option_woocommerce_enable_review_rating |
112
|
|
|
* @filter option_woocommerce_review_rating_required |
113
|
|
|
*/ |
114
|
|
|
public function filterRatingOption() |
115
|
|
|
{ |
116
|
|
|
return 'yes'; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return \WC_Product|false |
121
|
|
|
* |
122
|
|
|
* @filter site-reviews/review/call/product |
123
|
|
|
*/ |
124
|
|
|
public function filterReviewProductMethod(Review $review) |
125
|
|
|
{ |
126
|
|
|
if ($product = wc_get_product(Arr::get($review->assigned_posts, 0))) { |
127
|
|
|
return $product; |
128
|
|
|
} |
129
|
|
|
return false; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param \GeminiLabs\SiteReviews\Modules\Html\Tags\ReviewAuthorTag $tag |
134
|
|
|
* |
135
|
|
|
* @filter site-reviews/review/value/author |
136
|
|
|
*/ |
137
|
|
|
public function filterReviewAuthorTagValue(string $value, $tag): string |
138
|
|
|
{ |
139
|
|
|
if ($tag->review->hasVerifiedOwner() && 'yes' === get_option('woocommerce_review_rating_verification_label')) { // @phpstan-ignore-line |
140
|
|
|
$text = esc_attr__('verified owner', 'site-reviews'); |
141
|
|
|
$value = sprintf('%s <em class="woocommerce-review__verified verified">(%s)</em>', $value, $text); |
142
|
|
|
} |
143
|
|
|
return $value; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @filter site-reviews/review/call/hasVerifiedOwner |
148
|
|
|
*/ |
149
|
|
|
public function hasVerifiedOwner(Review $review): bool |
150
|
|
|
{ |
151
|
|
|
$verified = get_post_meta($review->ID, static::VERIFIED_META_KEY, true); |
152
|
|
|
return '' === $verified |
153
|
|
|
? $this->verifyProductOwner($review) |
154
|
|
|
: (bool) $verified; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @action admin_init |
159
|
|
|
*/ |
160
|
|
|
public function redirectProductReviews(): void |
161
|
|
|
{ |
162
|
|
|
global $pagenow; |
163
|
|
|
if ('edit.php' === $pagenow |
164
|
|
|
&& 'product' === filter_input(INPUT_GET, 'post_type') |
165
|
|
|
&& 'product-reviews' === filter_input(INPUT_GET, 'page')) { |
166
|
|
|
wp_redirect(add_query_arg('notice', 'product-reviews', glsr_admin_url()), 301); |
167
|
|
|
exit; |
|
|
|
|
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @action elementor/widgets/register |
173
|
|
|
*/ |
174
|
|
|
public function registerElementorWidgets(): void |
175
|
|
|
{ |
176
|
|
|
$widgets = \Elementor\Plugin::instance()->widgets_manager; |
177
|
|
|
$widgets->unregister('woocommerce-product-rating'); |
178
|
|
|
if (class_exists('ElementorPro\Modules\Woocommerce\Widgets\Product_Rating')) { |
179
|
|
|
$widgets->register(new ProductRating()); |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @action widgets_init |
185
|
|
|
*/ |
186
|
|
|
public function registerWidgets(): void |
187
|
|
|
{ |
188
|
|
|
unregister_widget('WC_Widget_Recent_Reviews'); |
189
|
|
|
unregister_widget('WC_Widget_Rating_Filter'); |
190
|
|
|
register_widget(WidgetRecentReviews::class); |
191
|
|
|
register_widget(WidgetRatingFilter::class); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param array $args |
196
|
|
|
* |
197
|
|
|
* @return array |
198
|
|
|
* |
199
|
|
|
* @action woocommerce_register_post_type_product |
200
|
|
|
*/ |
201
|
|
|
public function removeWoocommerceReviews($args) |
202
|
|
|
{ |
203
|
|
|
if (array_key_exists('supports', $args)) { |
204
|
|
|
$args['supports'] = array_diff($args['supports'], ['comments']); |
205
|
|
|
} |
206
|
|
|
return $args; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @action admin_notices |
211
|
|
|
*/ |
212
|
|
|
public function renderNotice(): void |
213
|
|
|
{ |
214
|
|
|
$screen = glsr_current_screen(); |
215
|
|
|
if ('edit' !== $screen->base || 'edit-site-review' !== $screen->id) { |
216
|
|
|
return; |
217
|
|
|
} |
218
|
|
|
if ('product-reviews' !== filter_input(INPUT_GET, 'notice')) { |
219
|
|
|
return; |
220
|
|
|
} |
221
|
|
|
glsr()->render('integrations/woocommerce/notices/reviews'); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @return void|bool |
226
|
|
|
* |
227
|
|
|
* @see $this->hasVerifiedOwner() |
228
|
|
|
* |
229
|
|
|
* @action site-reviews/review/created |
230
|
|
|
*/ |
231
|
|
|
public function verifyProductOwner(Review $review) |
232
|
|
|
{ |
233
|
|
|
$review->refresh(); // refresh the review first! |
234
|
|
|
$verified = false; |
235
|
|
|
foreach ($review->assigned_posts as $postId) { |
236
|
|
|
if ('product' === get_post_type($postId)) { |
237
|
|
|
$verified = wc_customer_bought_product($review->email, $review->author_id, $postId); |
238
|
|
|
break; |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
update_post_meta($review->ID, static::VERIFIED_META_KEY, (int) $verified); |
242
|
|
|
return $verified; |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.