1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Shortcodes; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\Builder; |
6
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\ReviewForm; |
7
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\Template; |
8
|
|
|
|
9
|
|
|
class SiteReviewsFormShortcode extends Shortcode |
10
|
|
|
{ |
11
|
|
|
public function buildTemplate(): string |
12
|
|
|
{ |
13
|
|
|
if (!is_user_logged_in() && glsr_get_option('general.require.login', false, 'bool')) { |
14
|
|
|
$this->debug(); |
15
|
|
|
return $this->loginOrRegister(); |
16
|
|
|
} |
17
|
|
|
$form = new ReviewForm($this->args); |
18
|
|
|
$this->debug(compact('form')); |
19
|
|
|
return $form->build(); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function description(): string |
23
|
|
|
{ |
24
|
|
|
return esc_html_x('Display a review form', 'admin-text', 'site-reviews'); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param string $url |
29
|
|
|
* @param string $redirect |
30
|
|
|
* @param bool $forceReauth |
31
|
|
|
* |
32
|
|
|
* @filter login_url |
33
|
|
|
*/ |
34
|
|
|
public function filterLoginUrl($url, $redirect, $forceReauth): string |
35
|
|
|
{ |
36
|
|
|
if ($loginUrl = glsr_get_option('general.require.login_url')) { |
37
|
|
|
if (!empty($redirect)) { |
38
|
|
|
$loginUrl = add_query_arg('redirect_to', urlencode($redirect), $loginUrl); |
39
|
|
|
} |
40
|
|
|
if ($forceReauth) { |
41
|
|
|
$loginUrl = add_query_arg('reauth', '1', $loginUrl); |
42
|
|
|
} |
43
|
|
|
return $loginUrl; |
44
|
|
|
} |
45
|
|
|
return $url; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $url |
50
|
|
|
* |
51
|
|
|
* @filter register_url |
52
|
|
|
*/ |
53
|
|
|
public function filterRegisterUrl($url): string |
54
|
|
|
{ |
55
|
|
|
if ($registerUrl = glsr_get_option('general.require.register_url')) { |
56
|
|
|
return $registerUrl; |
57
|
|
|
} |
58
|
|
|
return $url; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function loginLink(): string |
62
|
|
|
{ |
63
|
|
|
return glsr(Builder::class)->a([ |
64
|
|
|
'href' => $this->loginUrl(), |
65
|
|
|
'text' => __('logged in', 'site-reviews'), |
66
|
|
|
]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function loginOrRegister(): string |
70
|
|
|
{ |
71
|
|
|
$loginText = sprintf(__('You must be %s to submit a review.', 'site-reviews'), $this->loginLink()); |
72
|
|
|
$registerLink = $this->registerLink(); |
73
|
|
|
$registerText = ''; |
74
|
|
|
if (glsr_get_option('general.require.register', false, 'bool') && !empty($registerLink)) { |
75
|
|
|
$registerText = sprintf(__('You may also %s for an account.', 'site-reviews'), $registerLink); |
76
|
|
|
} |
77
|
|
|
return glsr(Template::class)->build('templates/login-register', [ |
78
|
|
|
'context' => [ |
79
|
|
|
'text' => trim("{$loginText} {$registerText}"), |
80
|
|
|
], |
81
|
|
|
]); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function loginUrl(): string |
85
|
|
|
{ |
86
|
|
|
add_filter('login_url', [$this, 'filterLoginUrl'], 20, 3); |
87
|
|
|
$url = wp_login_url(strval(get_permalink())); |
88
|
|
|
remove_filter('login_url', [$this, 'filterLoginUrl'], 20); |
89
|
|
|
return $url; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function name(): string |
93
|
|
|
{ |
94
|
|
|
return esc_html_x('Review Form', 'admin-text', 'site-reviews'); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function registerLink(): string |
98
|
|
|
{ |
99
|
|
|
$registerUrl = $this->registerUrl(); |
100
|
|
|
if (empty($registerUrl)) { |
101
|
|
|
return ''; |
102
|
|
|
} |
103
|
|
|
return glsr(Builder::class)->a([ |
104
|
|
|
'href' => $registerUrl, |
105
|
|
|
'text' => __('register', 'site-reviews'), |
106
|
|
|
]); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function registerUrl(): string |
110
|
8 |
|
{ |
111
|
|
|
if (!get_option('users_can_register')) { |
112
|
8 |
|
return ''; |
113
|
8 |
|
} |
114
|
8 |
|
add_filter('register_url', [$this, 'filterRegisterUrl'], 20); |
115
|
8 |
|
$url = wp_registration_url(); |
116
|
8 |
|
remove_filter('register_url', [$this, 'filterRegisterUrl'], 20); |
117
|
8 |
|
return $url; |
118
|
8 |
|
} |
119
|
8 |
|
|
120
|
|
|
protected function config(): array |
121
|
|
|
{ |
122
|
|
|
return [ |
123
|
|
|
// 'form' => [ |
124
|
|
|
// 'label' => esc_html_x('Use a Custom Form', 'admin-text', 'site-reviews-forms'), |
125
|
|
|
// 'placeholder' => esc_html_x('Select a Form...', 'admin-text', 'site-reviews-forms'), |
126
|
|
|
// 'type' => 'select', |
127
|
|
|
// ], |
128
|
|
|
'assigned_posts' => [ |
129
|
|
|
'label' => esc_html_x('Assign Reviews to Pages', 'admin-text', 'site-reviews'), |
130
|
|
|
'multiple' => true, |
131
|
|
|
'placeholder' => esc_html_x('Select a Page...', 'admin-text', 'site-reviews'), |
132
|
|
|
'type' => 'select', |
133
|
|
|
], |
134
|
|
|
'assigned_terms' => [ |
135
|
|
|
'label' => esc_html_x('Assign Reviews to Categories', 'admin-text', 'site-reviews'), |
136
|
|
|
'multiple' => true, |
137
|
|
|
'placeholder' => esc_html_x('Select a Category...', 'admin-text', 'site-reviews'), |
138
|
|
|
'type' => 'select', |
139
|
|
|
], |
140
|
|
|
'assigned_users' => [ |
141
|
|
|
'label' => esc_html_x('Assign Reviews to Users', 'admin-text', 'site-reviews'), |
142
|
|
|
'multiple' => true, |
143
|
|
|
'placeholder' => esc_html_x('Select a User...', 'admin-text', 'site-reviews'), |
144
|
|
|
'type' => 'select', |
145
|
|
|
], |
146
|
|
|
'hide' => [ |
147
|
|
|
'group' => 'hide', |
148
|
|
|
'options' => $this->options('hide'), |
149
|
|
|
'type' => 'checkbox', |
150
|
|
|
], |
151
|
|
|
'reviews_id' => [ |
152
|
|
|
'description' => esc_html_x('Enter the Custom ID of a reviews block, shortcode, or widget where the review should be displayed after submission.', 'admin-text', 'site-reviews'), |
153
|
|
|
'label' => esc_html_x('Reviews ID', 'admin-text', 'site-reviews'), |
154
|
|
|
'group' => 'advanced', |
155
|
|
|
'type' => 'text', |
156
|
|
|
], |
157
|
|
|
'id' => [ |
158
|
|
|
'description' => esc_html_x('This should be a unique value.', 'admin-text', 'site-reviews'), |
159
|
|
|
'group' => 'advanced', |
160
|
|
|
'label' => esc_html_x('Custom ID', 'admin-text', 'site-reviews'), |
161
|
|
|
'type' => 'text', |
162
|
|
|
], |
163
|
|
|
'class' => [ |
164
|
|
|
'description' => esc_html_x('Separate multiple classes with spaces.', 'admin-text', 'site-reviews'), |
165
|
|
|
'group' => 'advanced', |
166
|
|
|
'label' => esc_html_x('Additional CSS classes', 'admin-text', 'site-reviews'), |
167
|
|
|
'type' => 'text', |
168
|
|
|
], |
169
|
|
|
]; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
protected function debug(array $data = []): void |
173
|
|
|
{ |
174
|
|
|
if (!empty($this->args['debug']) && !empty($data['form'])) { |
175
|
|
|
$form = $data['form']; |
176
|
|
|
$data = [ |
177
|
|
|
'fields' => [ |
178
|
|
|
'hidden' => $form->hidden(), |
179
|
|
|
'visible' => $form->visible(), |
180
|
|
|
], |
181
|
|
|
'session' => $form->session()->toArray(), |
182
|
|
|
]; |
183
|
|
|
} |
184
|
|
|
parent::debug($data); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
protected function hideOptions(): array |
188
|
|
|
{ |
189
|
|
|
return [ |
190
|
|
|
'rating' => _x('Hide the rating field', 'admin-text', 'site-reviews'), |
191
|
|
|
'title' => _x('Hide the title field', 'admin-text', 'site-reviews'), |
192
|
|
|
'content' => _x('Hide the review field', 'admin-text', 'site-reviews'), |
193
|
|
|
'name' => _x('Hide the name field', 'admin-text', 'site-reviews'), |
194
|
|
|
'email' => _x('Hide the email field', 'admin-text', 'site-reviews'), |
195
|
|
|
'terms' => _x('Hide the terms field', 'admin-text', 'site-reviews'), |
196
|
|
|
]; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @param string $value |
201
|
|
|
*/ |
202
|
|
|
protected function normalizeAssignedPosts($value): string |
203
|
|
|
{ |
204
|
|
|
$postIds = parent::normalizeAssignedPosts($value); |
205
|
|
|
$postIds = explode(',', $postIds); |
206
|
|
|
$postIds = array_filter($postIds, 'is_numeric'); // don't use post_types here |
207
|
|
|
return implode(',', $postIds); |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|