1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Integrations\UltimateMember\Controllers; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Contracts\TagContract; |
6
|
|
|
use GeminiLabs\SiteReviews\Controllers\AbstractController; |
7
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\Builder; |
8
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\Template; |
9
|
|
|
use GeminiLabs\SiteReviews\Shortcodes\SiteReviewsFormShortcode; |
10
|
|
|
|
11
|
|
|
class ProfileController extends AbstractController |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @filter site-reviews/enqueue/public/inline-script/after |
15
|
|
|
*/ |
16
|
|
|
public function filterInlineScript(string $javascript): string |
17
|
|
|
{ |
18
|
|
|
if (!$this->hasVisibilityPermission()) { |
19
|
|
|
return $javascript; |
20
|
|
|
} |
21
|
|
|
return $javascript.'document.addEventListener("DOMContentLoaded", () => {'. |
22
|
|
|
'GLSR.Event.on("site-reviews/form/handle", (response, form) => {'. |
23
|
|
|
'if (true !== response.success || "undefined" === typeof response.html) return;'. |
24
|
|
|
'form.classList.add("glsr-hide-form");'. |
25
|
|
|
'form.insertAdjacentHTML("afterend", "<p class=\"glsr-no-margins glsr-form-success\">"+response.message+"</p>");'. |
26
|
|
|
'form.remove()'. |
27
|
|
|
'})'. |
28
|
|
|
'})'; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @filter um_user_profile_tabs |
33
|
|
|
*/ |
34
|
|
|
public function filterProfileTabs(array $tabs): array |
35
|
|
|
{ |
36
|
|
|
if (!$this->hasVisibilityPermission()) { |
37
|
|
|
return $tabs; |
38
|
|
|
} |
39
|
|
|
$tabs['user_reviews'] = [ |
40
|
|
|
'icon' => 'um-faicon-star', |
41
|
|
|
'name' => __('Reviews', 'site-reviews'), |
42
|
|
|
]; |
43
|
|
|
return $tabs; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @filter site-reviews/review/value/author |
48
|
|
|
*/ |
49
|
|
|
public function filterReviewAuthorValue(string $value, TagContract $tag): string |
50
|
|
|
{ |
51
|
|
|
if ($user = $tag->review->user()) { // @phpstan-ignore-line |
52
|
|
|
if ($url = um_user_profile_url($user->ID)) { |
53
|
|
|
return sprintf('<a href="%s">%s</a>', $url, $value); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
return $value; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @filter site-reviews/reviews/fallback |
61
|
|
|
*/ |
62
|
|
|
public function filterReviewsFallback(string $fallback, array $args): string |
63
|
|
|
{ |
64
|
|
|
if ($args['fallback'] !== __('There are no reviews yet. Be the first one to write one.', 'site-reviews')) { |
65
|
|
|
return $fallback; |
66
|
|
|
} |
67
|
|
|
$value = get_current_user_id() === um_get_requested_user() |
68
|
|
|
? __('You have not received any reviews.', 'site-reviews') |
69
|
|
|
: __('This person has not received any reviews.', 'site-reviews'); |
70
|
|
|
return glsr(Builder::class)->p([ |
71
|
|
|
'class' => 'glsr-no-margins', |
72
|
|
|
'text' => $value, |
73
|
|
|
]); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @filter site-reviews/summary/value/percentages |
78
|
|
|
*/ |
79
|
|
|
public function filterSummaryPercentagesValue(string $value, TagContract $tag): string |
80
|
|
|
{ |
81
|
|
|
if (!empty(array_sum($tag->ratings))) { // @phpstan-ignore-line |
82
|
|
|
return $value; |
83
|
|
|
} |
84
|
|
|
return ''; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @filter site-reviews/summary/value/text |
89
|
|
|
*/ |
90
|
|
|
public function filterSummaryRatingValue(): string |
91
|
|
|
{ |
92
|
|
|
$value = get_current_user_id() === um_get_requested_user() |
93
|
|
|
? __('Your rating', 'site-reviews') |
94
|
|
|
: __('User rating', 'site-reviews'); |
95
|
|
|
return glsr(Builder::class)->h4($value); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @filter site-reviews/summary/value/rating |
100
|
|
|
*/ |
101
|
|
|
public function filterSummaryTextValue(string $value, TagContract $tag): string |
102
|
|
|
{ |
103
|
|
|
if (!empty(array_sum($tag->ratings))) { // @phpstan-ignore-line |
104
|
|
|
return $value; |
105
|
|
|
} |
106
|
|
|
if (get_current_user_id() === um_get_requested_user()) { |
107
|
|
|
return __('No one has reviewed you yet.', 'site-reviews'); |
108
|
|
|
} |
109
|
|
|
return __('No one has reviewed this person yet.', 'site-reviews'); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @action um_profile_content_user_reviews |
114
|
|
|
*/ |
115
|
|
|
public function renderReviewsTab(): void |
116
|
|
|
{ |
117
|
|
|
if (!$this->hasVisibilityPermission()) { |
118
|
|
|
return; |
119
|
|
|
} |
120
|
|
|
glsr(Template::class)->render('templates/ultimatemember/profile-reviews', [ |
121
|
|
|
'context' => [ |
122
|
|
|
'form' => $this->shortcodeForm(), |
123
|
|
|
'reviews' => $this->shortcodeReviews(), |
124
|
|
|
'summary' => $this->shortcodeSummary(), |
125
|
|
|
], |
126
|
|
|
]); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
protected function hasVisibilityPermission(): bool |
130
|
|
|
{ |
131
|
|
|
if (!glsr_get_option('integrations.ultimatemember.display_reviews_tab', false, 'bool')) { |
132
|
|
|
return false; |
133
|
|
|
} |
134
|
|
|
if (!um_is_core_page('user')) { |
135
|
|
|
return false; |
136
|
|
|
} |
137
|
|
|
$roles = glsr_get_option('integrations.ultimatemember.reviews_tab_roles'); |
138
|
|
|
$visibility = glsr_get_option('integrations.ultimatemember.reviews_tab_visibility'); |
139
|
|
|
$user = wp_get_current_user(); |
140
|
|
|
$userHasRole = !empty(array_intersect($roles, (array) $user->roles)); |
141
|
|
|
$userIsOwner = $user->ID === um_get_requested_user(); |
142
|
|
|
if ('guest' === $visibility && $user->ID) { |
143
|
|
|
return false; |
144
|
|
|
} |
145
|
|
|
if ('member' === $visibility && !$user->ID) { |
146
|
|
|
return false; |
147
|
|
|
} |
148
|
|
|
if ('owner' === $visibility && !$userIsOwner) { |
149
|
|
|
return false; |
150
|
|
|
} |
151
|
|
|
if ('roles' === $visibility && !$userHasRole) { |
152
|
|
|
return false; |
153
|
|
|
} |
154
|
|
|
if ('owner_roles' === $visibility && !$userIsOwner && !$userHasRole) { |
155
|
|
|
return false; |
156
|
|
|
} |
157
|
|
|
return true; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
protected function shortcodeForm(): string |
161
|
|
|
{ |
162
|
|
|
if (!is_user_logged_in()) { |
163
|
|
|
$text = sprintf(__('You must be %s to review this person.', 'site-reviews'), glsr(SiteReviewsFormShortcode::class)->loginLink()); |
164
|
|
|
return glsr(Template::class)->build('templates/login-register', [ |
165
|
|
|
'context' => compact('text'), |
166
|
|
|
]); |
167
|
|
|
} |
168
|
|
|
if (get_current_user_id() === um_get_requested_user()) { |
|
|
|
|
169
|
|
|
return ''; |
170
|
|
|
} |
171
|
|
|
$ratings = glsr_get_ratings([ |
172
|
|
|
'assigned_users' => um_get_requested_user(), |
173
|
|
|
'status' => 'all', |
174
|
|
|
'user__in' => get_current_user_id(), |
175
|
|
|
]); |
176
|
|
|
if (0 < $ratings->reviews) { |
177
|
|
|
return ''; |
178
|
|
|
} |
179
|
|
|
return do_shortcode(glsr_get_option('integrations.ultimatemember.form')); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
protected function shortcodeReviews(): string |
183
|
|
|
{ |
184
|
|
|
add_filter('site-reviews/review/value/author', [$this, 'filterReviewAuthorValue'], 20, 2); |
185
|
|
|
add_filter('site-reviews/reviews/fallback', [$this, 'filterReviewsFallback'], 20, 2); |
186
|
|
|
$shortcode = do_shortcode(glsr_get_option('integrations.ultimatemember.reviews')); |
187
|
|
|
remove_filter('site-reviews/review/value/author', [$this, 'filterReviewAuthorValue'], 20); |
188
|
|
|
remove_filter('site-reviews/reviews/fallback', [$this, 'filterReviewsFallback'], 20); |
189
|
|
|
return $shortcode; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
protected function shortcodeSummary(): string |
193
|
|
|
{ |
194
|
|
|
add_filter('site-reviews/summary/value/percentages', [$this, 'filterSummaryPercentagesValue'], 20, 2); |
195
|
|
|
add_filter('site-reviews/summary/value/rating', [$this, 'filterSummaryRatingValue'], 20); |
196
|
|
|
add_filter('site-reviews/summary/value/text', [$this, 'filterSummaryTextValue'], 20, 2); |
197
|
|
|
$shortcode = do_shortcode(glsr_get_option('integrations.ultimatemember.summary')); |
198
|
|
|
remove_filter('site-reviews/summary/value/percentages', [$this, 'filterSummaryPercentagesValue'], 20); |
199
|
|
|
remove_filter('site-reviews/summary/value/rating', [$this, 'filterSummaryRatingValue'], 20); |
200
|
|
|
remove_filter('site-reviews/summary/value/text', [$this, 'filterSummaryTextValue'], 20); |
201
|
|
|
return $shortcode; |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|