|
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
|
|
|
$url = um_user_profile_url($user->ID); |
|
|
|
|
|
|
53
|
|
|
return sprintf('<a href="%s">%s</a>', $url, $value); |
|
54
|
|
|
} |
|
55
|
|
|
return $value; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @filter site-reviews/reviews/fallback |
|
60
|
|
|
*/ |
|
61
|
|
|
public function filterReviewsFallback(string $fallback, array $args): string |
|
62
|
|
|
{ |
|
63
|
|
|
if ($args['fallback'] !== __('There are no reviews yet. Be the first one to write one.', 'site-reviews')) { |
|
64
|
|
|
return $fallback; |
|
65
|
|
|
} |
|
66
|
|
|
$value = get_current_user_id() === um_get_requested_user() |
|
|
|
|
|
|
67
|
|
|
? __('You have not received any reviews.', 'site-reviews') |
|
68
|
|
|
: __('This person has not received any reviews.', 'site-reviews'); |
|
69
|
|
|
return glsr(Builder::class)->p([ |
|
70
|
|
|
'class' => 'glsr-no-margins', |
|
71
|
|
|
'text' => $value, |
|
72
|
|
|
]); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @filter site-reviews/summary/value/percentages |
|
77
|
|
|
*/ |
|
78
|
|
|
public function filterSummaryPercentagesValue(string $value, TagContract $tag): string |
|
79
|
|
|
{ |
|
80
|
|
|
if (!empty(array_sum($tag->ratings))) { // @phpstan-ignore-line |
|
|
|
|
|
|
81
|
|
|
return $value; |
|
82
|
|
|
} |
|
83
|
|
|
return ''; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @filter site-reviews/summary/value/text |
|
88
|
|
|
*/ |
|
89
|
|
|
public function filterSummaryRatingValue(string $value, TagContract $tag): string |
|
|
|
|
|
|
90
|
|
|
{ |
|
91
|
|
|
$value = get_current_user_id() === um_get_requested_user() |
|
|
|
|
|
|
92
|
|
|
? __('Your rating', 'site-reviews') |
|
93
|
|
|
: __('User rating', 'site-reviews'); |
|
94
|
|
|
return glsr(Builder::class)->h4($value); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @filter site-reviews/summary/value/rating |
|
99
|
|
|
*/ |
|
100
|
|
|
public function filterSummaryTextValue(string $value, TagContract $tag): string |
|
101
|
|
|
{ |
|
102
|
|
|
if (!empty(array_sum($tag->ratings))) { // @phpstan-ignore-line |
|
|
|
|
|
|
103
|
|
|
return $value; |
|
104
|
|
|
} |
|
105
|
|
|
if (get_current_user_id() === um_get_requested_user()) { |
|
|
|
|
|
|
106
|
|
|
return __('No one has reviewed you yet.', 'site-reviews'); |
|
107
|
|
|
} |
|
108
|
|
|
return __('No one has reviewed this person yet.', 'site-reviews'); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @action um_profile_content_user_reviews |
|
113
|
|
|
*/ |
|
114
|
|
|
public function renderReviewsTab(): void |
|
115
|
|
|
{ |
|
116
|
|
|
if (!$this->hasVisibilityPermission()) { |
|
117
|
|
|
return; |
|
118
|
|
|
} |
|
119
|
|
|
glsr(Template::class)->render('templates/ultimatemember/reviews', [ |
|
120
|
|
|
'context' => [ |
|
121
|
|
|
'form' => $this->shortcodeForm(), |
|
122
|
|
|
'reviews' => $this->shortcodeReviews(), |
|
123
|
|
|
'summary' => $this->shortcodeSummary(), |
|
124
|
|
|
], |
|
125
|
|
|
]); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
protected function hasVisibilityPermission(): bool |
|
129
|
|
|
{ |
|
130
|
|
|
if (!glsr_get_option('integrations.ultimatemember.display_reviews_tab', false, 'bool')) { |
|
131
|
|
|
return false; |
|
132
|
|
|
} |
|
133
|
|
|
if (!um_is_core_page('user')) { |
|
|
|
|
|
|
134
|
|
|
return false; |
|
135
|
|
|
} |
|
136
|
|
|
$roles = glsr_get_option('integrations.ultimatemember.reviews_tab_roles'); |
|
137
|
|
|
$visibility = glsr_get_option('integrations.ultimatemember.reviews_tab_visibility'); |
|
138
|
|
|
$user = wp_get_current_user(); |
|
139
|
|
|
$userHasRole = !empty(array_intersect($roles, (array) $user->roles)); |
|
140
|
|
|
$userIsOwner = $user->ID === um_get_requested_user(); |
|
|
|
|
|
|
141
|
|
|
if ('guest' === $visibility && $user->ID) { |
|
142
|
|
|
return false; |
|
143
|
|
|
} |
|
144
|
|
|
if ('member' === $visibility && !$user->ID) { |
|
145
|
|
|
return false; |
|
146
|
|
|
} |
|
147
|
|
|
if ('owner' === $visibility && !$userIsOwner) { |
|
148
|
|
|
return false; |
|
149
|
|
|
} |
|
150
|
|
|
if ('roles' === $visibility && !$userHasRole) { |
|
151
|
|
|
return false; |
|
152
|
|
|
} |
|
153
|
|
|
if ('owner_roles' === $visibility && !$userIsOwner && !$userHasRole) { |
|
154
|
|
|
return false; |
|
155
|
|
|
} |
|
156
|
|
|
return true; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
protected function shortcodeForm(): string |
|
160
|
|
|
{ |
|
161
|
|
|
if (!is_user_logged_in()) { |
|
162
|
|
|
$text = sprintf(__('You must be %s to review this person.', 'site-reviews'), glsr(SiteReviewsFormShortcode::class)->loginLink()); |
|
163
|
|
|
return glsr(Template::class)->build('templates/login-register', [ |
|
164
|
|
|
'context' => compact('text'), |
|
165
|
|
|
]); |
|
166
|
|
|
} else { |
|
167
|
|
|
$ratings = glsr_get_ratings([ |
|
168
|
|
|
'assigned_users' => um_get_requested_user(), |
|
|
|
|
|
|
169
|
|
|
'status' => 'all', |
|
170
|
|
|
'user__in' => get_current_user_id(), |
|
171
|
|
|
]); |
|
172
|
|
|
if (0 < $ratings->reviews) { |
|
173
|
|
|
return ''; |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
return do_shortcode(glsr_get_option('integrations.ultimatemember.form')); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
protected function shortcodeReviews(): string |
|
180
|
|
|
{ |
|
181
|
|
|
add_filter('site-reviews/review/value/author', [$this, 'filterReviewAuthorValue'], 20, 2); |
|
182
|
|
|
add_filter('site-reviews/reviews/fallback', [$this, 'filterReviewsFallback'], 20, 2); |
|
183
|
|
|
$shortcode = do_shortcode(glsr_get_option('integrations.ultimatemember.reviews')); |
|
184
|
|
|
remove_filter('site-reviews/review/value/author', [$this, 'filterReviewAuthorValue'], 20); |
|
185
|
|
|
remove_filter('site-reviews/reviews/fallback', [$this, 'filterReviewsFallback'], 20); |
|
186
|
|
|
return $shortcode; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
protected function shortcodeSummary(): string |
|
190
|
|
|
{ |
|
191
|
|
|
add_filter('site-reviews/summary/value/percentages', [$this, 'filterSummaryPercentagesValue'], 20, 2); |
|
192
|
|
|
add_filter('site-reviews/summary/value/rating', [$this, 'filterSummaryRatingValue'], 20, 2); |
|
193
|
|
|
add_filter('site-reviews/summary/value/text', [$this, 'filterSummaryTextValue'], 20, 2); |
|
194
|
|
|
$shortcode = do_shortcode(glsr_get_option('integrations.ultimatemember.summary')); |
|
195
|
|
|
remove_filter('site-reviews/summary/value/percentages', [$this, 'filterSummaryPercentagesValue'], 20); |
|
196
|
|
|
remove_filter('site-reviews/summary/value/rating', [$this, 'filterSummaryRatingValue'], 20); |
|
197
|
|
|
remove_filter('site-reviews/summary/value/text', [$this, 'filterSummaryTextValue'], 20); |
|
198
|
|
|
return $shortcode; |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
|