1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Shortcodes; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Helper; |
6
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\Builder; |
7
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\Tags\FormFieldsTag; |
8
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\Template; |
9
|
|
|
use GeminiLabs\SiteReviews\Modules\Style; |
10
|
|
|
|
11
|
|
|
class SiteReviewsFormShortcode extends Shortcode |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
public $args; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var \GeminiLabs\SiteReviews\Arguments |
20
|
|
|
*/ |
21
|
|
|
protected $with; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* {@inheritdoc} |
25
|
|
|
*/ |
26
|
|
|
public function buildTemplate(array $args = []) |
27
|
|
|
{ |
28
|
|
|
$this->args = $args; |
29
|
|
|
if (!is_user_logged_in() && glsr_get_option('general.require.login', false, 'bool')) { |
30
|
|
|
$this->debug(); |
31
|
|
|
return $this->loginOrRegister(); |
32
|
|
|
} |
33
|
|
|
$this->with = $this->with(); |
34
|
|
|
$fields = $this->buildTemplateFieldTags(); |
35
|
|
|
$this->debug(compact('fields')); |
36
|
|
|
return glsr(Template::class)->build('templates/reviews-form', [ |
37
|
|
|
'args' => $args, |
38
|
|
|
'context' => [ |
39
|
|
|
'class' => $this->getClasses(), |
40
|
|
|
'fields' => glsr()->filterString('form/build/fields', $fields, $this->with, $this), |
41
|
|
|
'id' => '', // @deprecated in v5.0 |
42
|
|
|
'response' => $this->buildTemplateTag('response'), |
43
|
|
|
'submit_button' => $this->buildTemplateTag('submit_button'), |
44
|
|
|
], |
45
|
|
|
'form' => $fields, |
46
|
|
|
]); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $url |
51
|
|
|
* @param string $redirect |
52
|
|
|
* @param bool $forceReauth |
53
|
|
|
* @return string |
54
|
|
|
* @filter login_url |
55
|
|
|
*/ |
56
|
|
|
public function filterLoginUrl($url, $redirect, $forceReauth) |
57
|
|
|
{ |
58
|
|
|
if ($loginUrl = glsr_get_option('general.require.login_url')) { |
59
|
|
|
if (!empty($redirect)) { |
60
|
|
|
$loginUrl = add_query_arg('redirect_to', urlencode($redirect), $loginUrl); |
61
|
|
|
} |
62
|
|
|
if ($forceReauth) { |
63
|
|
|
$loginUrl = add_query_arg('reauth', '1', $loginUrl); |
64
|
|
|
} |
65
|
|
|
return $loginUrl; |
66
|
|
|
} |
67
|
|
|
return $url; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $url |
72
|
|
|
* @return string |
73
|
|
|
* @filter register_url |
74
|
|
|
*/ |
75
|
|
|
public function filterRegisterUrl($url) |
76
|
|
|
{ |
77
|
|
|
if ($registerUrl = glsr_get_option('general.require.register_url')) { |
78
|
|
|
return $registerUrl; |
79
|
|
|
} |
80
|
|
|
return $url; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return \GeminiLabs\SiteReviews\Modules\Html\Form |
85
|
|
|
*/ |
86
|
|
|
protected function buildTemplateFieldTags() |
87
|
|
|
{ |
88
|
|
|
$parameters = [ |
89
|
|
|
'args' => $this->args, |
90
|
|
|
'tag' => 'fields', |
91
|
|
|
]; |
92
|
|
|
return glsr(FormFieldsTag::class, $parameters)->handleFor('form', null, $this->with); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $tag |
97
|
|
|
* @return false|string |
98
|
|
|
*/ |
99
|
|
|
protected function buildTemplateTag($tag) |
100
|
|
|
{ |
101
|
|
|
$args = $this->args; |
102
|
|
|
$className = Helper::buildClassName(['form', $tag, 'tag'], 'Modules\Html\Tags'); |
103
|
|
|
$field = class_exists($className) |
104
|
|
|
? glsr($className, compact('tag', 'args'))->handleFor('form', null, $this->with) |
105
|
|
|
: null; |
106
|
|
|
return glsr()->filterString('form/build/'.$tag, $field, $this->with, $this); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return void |
111
|
|
|
*/ |
112
|
|
|
protected function debug(array $data = []) |
113
|
|
|
{ |
114
|
|
|
if (!empty($this->args['debug']) && !empty($data['fields'])) { |
115
|
|
|
$fields = $data['fields']; |
116
|
|
|
$data = [ |
117
|
|
|
'fields' => [ |
118
|
|
|
'hidden' => $fields->hidden(), |
119
|
|
|
'visible' => $fields->visible(), |
120
|
|
|
], |
121
|
|
|
'with' => $this->with->toArray(), |
122
|
|
|
]; |
123
|
|
|
} |
124
|
|
|
parent::debug($data); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
|
|
protected function getClasses() |
131
|
|
|
{ |
132
|
|
|
$classes = [ |
133
|
|
|
'glsr-review-form', |
134
|
|
|
glsr(Style::class)->classes('form'), |
135
|
|
|
$this->args['class'], |
136
|
|
|
]; |
137
|
|
|
if (!empty($this->with->errors)) { |
138
|
|
|
$classes[] = glsr(Style::class)->validation('form_error'); |
139
|
|
|
} |
140
|
|
|
return trim(implode(' ', array_filter($classes))); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return array |
145
|
|
|
*/ |
146
|
7 |
|
protected function hideOptions() |
147
|
|
|
{ |
148
|
|
|
return [ |
149
|
7 |
|
'rating' => _x('Hide the rating field', 'admin-text', 'site-reviews'), |
150
|
7 |
|
'title' => _x('Hide the title field', 'admin-text', 'site-reviews'), |
151
|
7 |
|
'content' => _x('Hide the review field', 'admin-text', 'site-reviews'), |
152
|
7 |
|
'name' => _x('Hide the name field', 'admin-text', 'site-reviews'), |
153
|
7 |
|
'email' => _x('Hide the email field', 'admin-text', 'site-reviews'), |
154
|
7 |
|
'terms' => _x('Hide the terms field', 'admin-text', 'site-reviews'), |
155
|
|
|
]; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @return string |
160
|
|
|
*/ |
161
|
|
|
protected function loginOrRegister() |
162
|
|
|
{ |
163
|
|
|
return glsr(Template::class)->build('templates/login-register', [ |
164
|
|
|
'context' => [ |
165
|
|
|
'text' => trim($this->loginText().' '.$this->registerText()), |
166
|
|
|
], |
167
|
|
|
]); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @return string |
172
|
|
|
*/ |
173
|
|
|
protected function loginText() |
174
|
|
|
{ |
175
|
|
|
add_filter('login_url', [$this, 'filterLoginUrl'], 20, 3); |
176
|
|
|
$loginUrl = wp_login_url(strval(get_permalink())); |
177
|
|
|
remove_filter('login_url', [$this, 'filterLoginUrl'], 20); |
178
|
|
|
$loginLink = glsr(Builder::class)->a([ |
179
|
|
|
'href' => $loginUrl, |
180
|
|
|
'text' => __('logged in', 'site-reviews'), |
181
|
|
|
]); |
182
|
|
|
return sprintf(__('You must be %s to submit a review.', 'site-reviews'), $loginLink); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @return void|string |
187
|
|
|
*/ |
188
|
|
|
protected function registerText() |
189
|
|
|
{ |
190
|
|
|
if (get_option('users_can_register') && glsr_get_option('general.require.login', false, 'bool')) { |
191
|
|
|
add_filter('register_url', [$this, 'filterRegisterUrl'], 20, 3); |
192
|
|
|
$registerUrl = wp_registration_url(); |
193
|
|
|
remove_filter('register_url', [$this, 'filterRegisterUrl'], 20); |
194
|
|
|
$registerLink = glsr(Builder::class)->a([ |
195
|
|
|
'href' => $registerUrl, |
196
|
|
|
'text' => __('register', 'site-reviews'), |
197
|
|
|
]); |
198
|
|
|
return sprintf(__('You may also %s for an account.', 'site-reviews'), $registerLink); |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @return \GeminiLabs\SiteReviews\Arguments |
204
|
|
|
*/ |
205
|
|
|
protected function with() |
206
|
|
|
{ |
207
|
|
|
return glsr()->args([ |
208
|
|
|
'errors' => glsr()->sessionPluck($this->args['id'].'errors', []), |
209
|
|
|
'message' => glsr()->sessionPluck($this->args['id'].'message', ''), |
210
|
|
|
'required' => glsr_get_option('submissions.required', []), |
211
|
|
|
'values' => glsr()->sessionPluck($this->args['id'].'values', []), |
212
|
|
|
]); |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|