|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Integrations\WPLoyalty; |
|
4
|
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Controllers\AbstractController; |
|
6
|
|
|
use GeminiLabs\SiteReviews\Database\ReviewManager; |
|
7
|
|
|
use GeminiLabs\SiteReviews\Helpers\Cast; |
|
8
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\ReviewForm; |
|
9
|
|
|
use GeminiLabs\SiteReviews\Review; |
|
10
|
|
|
use Wlr\App\Helpers\EarnCampaign; |
|
11
|
|
|
use Wlr\App\Helpers\Woocommerce; |
|
12
|
|
|
use Wlr\App\Premium\Helpers\ProductReview; |
|
13
|
|
|
use Wlr\App\Premium\Helpers\Referral; |
|
14
|
|
|
|
|
15
|
|
|
class Controller extends AbstractController |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @filter site-reviews/review-form/fields/visible |
|
19
|
|
|
*/ |
|
20
|
|
|
public function filterFieldAfter(array $fields, ReviewForm $form): array |
|
21
|
|
|
{ |
|
22
|
|
|
$assignedPostId = Cast::toInt($form->args->assigned_posts); |
|
23
|
|
|
if (empty($assignedPostId)) { |
|
24
|
|
|
return $fields; |
|
25
|
|
|
} |
|
26
|
|
|
if (!$product = Woocommerce::getInstance()->getProduct($assignedPostId)) { |
|
27
|
|
|
return $fields; |
|
28
|
|
|
} |
|
29
|
|
|
if (!is_user_logged_in() || Woocommerce::getInstance()->isBannedUser()) { |
|
30
|
|
|
return $fields; |
|
31
|
|
|
} |
|
32
|
|
|
if (apply_filters('wlr_hide_product_review_message', false, [])) { |
|
33
|
|
|
return $fields; |
|
34
|
|
|
} |
|
35
|
|
|
foreach ($fields as $field) { |
|
36
|
|
|
if ('content' !== $field->original_name) { |
|
37
|
|
|
continue; |
|
38
|
|
|
} |
|
39
|
|
|
$rewardsList = EarnCampaign::getInstance()->getActionEarning(['product_review'], [ |
|
40
|
|
|
'is_calculate_based' => 'product', |
|
41
|
|
|
'product' => $product, |
|
42
|
|
|
'product_id' => $assignedPostId, |
|
43
|
|
|
'user_email' => Woocommerce::getInstance()->get_login_user_email(), |
|
44
|
|
|
]); |
|
45
|
|
|
$messages = [$field->after]; |
|
46
|
|
|
foreach ($rewardsList as $action => $rewards) { |
|
47
|
|
|
foreach ($rewards as $key => $reward) { |
|
48
|
|
|
$messages[] = $reward['messages'] ?? ''; |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
$field->after = implode('<br/>', array_filter($messages)); |
|
52
|
|
|
break; |
|
53
|
|
|
} |
|
54
|
|
|
return $fields; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @action site-reviews/review/approved |
|
59
|
|
|
*/ |
|
60
|
|
|
public function onApprovedReview(Review $review): void |
|
61
|
|
|
{ |
|
62
|
|
|
$review->refresh(); |
|
63
|
|
|
$this->maybeEarnPoints($review); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @action site-reviews/review/created |
|
68
|
|
|
*/ |
|
69
|
|
|
public function onCreatedReview(Review $review): void |
|
70
|
|
|
{ |
|
71
|
|
|
if (!is_user_logged_in()) { |
|
72
|
|
|
return; |
|
73
|
|
|
} |
|
74
|
|
|
$review->refresh(); |
|
75
|
|
|
if (!$review->is_approved) { |
|
76
|
|
|
return; |
|
77
|
|
|
} |
|
78
|
|
|
$this->maybeEarnPoints($review); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
protected function earnPoints(string $customerEmail, int $productId): void |
|
82
|
|
|
{ |
|
83
|
|
|
$data = [ |
|
84
|
|
|
'is_calculate_based' => 'product', |
|
85
|
|
|
'product' => Woocommerce::getInstance()->getProduct($productId), |
|
86
|
|
|
'product_id' => $productId, |
|
87
|
|
|
'user_email' => $customerEmail, |
|
88
|
|
|
]; |
|
89
|
|
|
(new ProductReview())->applyEarnProductReview($data); |
|
90
|
|
|
(new Referral())->doReferralCheck($data); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
protected function maybeEarnPoints(Review $review): void |
|
94
|
|
|
{ |
|
95
|
|
|
if (!$user = $review->user()) { |
|
96
|
|
|
return; |
|
97
|
|
|
} |
|
98
|
|
|
if (Woocommerce::getInstance()->isBannedUser($user->user_email)) { |
|
99
|
|
|
return; |
|
100
|
|
|
} |
|
101
|
|
|
foreach ($review->assigned_posts as $postId) { |
|
102
|
|
|
if (Woocommerce::getInstance()->getProduct((int) $postId)) { |
|
103
|
|
|
$this->earnPoints($user->user_email, (int) $postId); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|