PluginProbe ʕ •ᴥ•ʔ
Paid Membership Plugin, Ecommerce, User Registration Form, Login Form, User Profile & Restrict Content – ProfilePress / 3.1
Paid Membership Plugin, Ecommerce, User Registration Form, Login Form, User Profile & Restrict Content – ProfilePress v3.1
4.16.18 4.16.17 4.16.16 trunk 1.0 1.0.1 1.0.2 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.5a 1.1.6 1.1.7 1.2 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.4 1.4.1 1.4.2 1.5 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.7 1.7.1 1.7.2 1.8 1.8.1 1.8.10 1.8.2 1.8.3 1.8.4 1.8.5 1.8.6 1.8.7 1.8.8 1.8.9 1.9 1.9.1 1.9.10 1.9.11 1.9.12 1.9.13 1.9.2 1.9.3 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.1.9 2.2.10 2.2.11 2.2.12 2.2.13 2.2.14 2.2.15 2.2.16 2.2.2 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 3.0 3.1 3.1.1 3.1.10 3.1.11 3.1.12 3.1.13 3.1.14 3.1.15 3.1.16 3.1.17 3.1.18 3.1.19 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.10 3.2.11 3.2.12 3.2.13 3.2.14 3.2.15 3.2.16 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 3.2.8 3.2.9 4.0.0 4.0.1 4.0.2 4.0.3 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.10.0 4.10.1 4.10.2 4.10.3 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.13.3 4.13.4 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.15.0 4.15.1 4.15.10 4.15.11 4.15.12 4.15.13 4.15.14 4.15.15 4.15.16 4.15.17 4.15.18 4.15.19 4.15.2 4.15.20 4.15.20.1 4.15.21 4.15.22 4.15.23 4.15.24 4.15.25 4.15.3 4.15.4 4.15.5 4.15.6 4.15.7 4.15.8 4.15.9 4.16.0 4.16.1 4.16.10 4.16.11 4.16.12 4.16.13 4.16.14 4.16.15 4.16.2 4.16.3 4.16.4 4.16.5 4.16.6 4.16.7 4.16.8 4.16.9 4.2.0 4.3.0 4.3.1 4.3.2 4.4.0 4.4.1 4.5.0 4.5.1 4.5.2 4.5.3 4.5.4 4.5.5 4.6.0 4.7.0 4.8.0 4.9.0
wp-user-avatar / src / ShortcodeParser / Builder / FieldsShortcodeCallback.php
wp-user-avatar / src / ShortcodeParser / Builder Last commit date
EditProfileBuilder.php 5 years ago FieldsShortcodeCallback.php 5 years ago FrontendProfileBuilder.php 5 years ago GlobalShortcodes.php 5 years ago LoginFormBuilder.php 5 years ago PasswordResetBuilder.php 5 years ago RegistrationFormBuilder.php 5 years ago builder-preview.php 5 years ago index.php 5 years ago
FieldsShortcodeCallback.php
1124 lines
1 <?php
2
3 namespace ProfilePress\Core\ShortcodeParser\Builder;
4
5 use ProfilePress\Core\Classes\ExtensionManager as EM;
6 use ProfilePress\Core\Classes\FormRepository;
7 use ProfilePress\Core\Classes\PROFILEPRESS_sql;
8 use ProfilePress\Core\Classes\UserAvatar;
9
10 class FieldsShortcodeCallback
11 {
12 protected $form_type;
13
14 protected $form_name;
15
16 protected $tag_name;
17
18 /** @var \WP_User */
19 private $current_user;
20
21 public function __construct($form_type)
22 {
23 $this->form_type = $form_type;
24
25 $this->form_name = $form_type == FormRepository::REGISTRATION_TYPE ? 'registration' : 'edit_profile';
26
27 $this->tag_name = $form_type == FormRepository::REGISTRATION_TYPE ? 'reg' : 'eup';
28
29 add_action('init', [$this, 'get_current_user']);
30 }
31
32 public function get_current_user()
33 {
34 $current_user = wp_get_current_user();
35 if ($current_user instanceof \WP_User) {
36 $this->current_user = $current_user;
37 }
38 }
39
40 public function GET_POST()
41 {
42 return array_merge($_GET, $_POST);
43 }
44
45 /**
46 * Is field a required field?
47 *
48 * @param array $atts
49 *
50 * @return bool
51 */
52 public function is_field_required($atts)
53 {
54 $atts = ppress_normalize_attributes($atts);
55
56 return isset($atts['required']) && ($atts['required'] === true || $atts['required'] == 'true' || $atts['required'] == '1');
57 }
58
59 /**
60 * Rewrite custom field key to something more human readable.
61 *
62 * @param string $key field key
63 *
64 * @return string
65 */
66 public function human_readable_field_key($key)
67 {
68 return ucfirst(str_replace('_', ' ', $key));
69 }
70
71 public function valid_field_atts($atts)
72 {
73 if ( ! is_array($atts)) return $atts;
74
75 $invalid_atts = array('enforce', 'key', 'field_key', 'limit', 'options', 'checkbox_text', 'date_format', 'field_width', 'icon');
76
77 $valid_atts = array();
78
79 foreach ($atts as $key => $value) {
80 if ( ! in_array($key, $invalid_atts)) {
81 $valid_atts[$key] = $value;
82 }
83 }
84
85 return $valid_atts;
86 }
87
88 public function field_attributes($field_name, $atts, $required = 'false')
89 {
90 $_POST = $this->GET_POST();
91
92 if ($field_name !== $this->tag_name . '_submit') {
93 $atts['required'] = isset($atts['required']) ? $atts['required'] : $required;
94 }
95
96 if ( ! in_array($field_name, ['ignore_value'])) {
97 $atts['value'] = isset($_POST[$field_name]) ? esc_attr($_POST[$field_name]) : @esc_attr($atts['value']);
98 }
99
100 $output = [];
101
102 foreach ($atts as $key => $value) {
103 // add class to submit button.
104 if ($field_name == $this->tag_name . '_submit' && $key == 'class') {
105 $value = 'pp-submit-form ' . $value;
106 }
107
108 if ($key != 'required' && ! empty($value)) {
109 $value = esc_attr($value);
110 $output[] = "$key=\"$value\"";
111 }
112 }
113
114 $output = implode(' ', $output);
115
116 if ($this->is_field_required($atts)) {
117 $output .= ' required="required"';
118 }
119
120 return $output;
121 }
122
123 /**
124 * @param array $atts
125 *
126 * @return string
127 */
128 public function username($atts)
129 {
130 $required = true;
131
132 if (empty($atts)) $atts = [];
133
134 // we are using + cos we dont want to override array value if it already exist.
135 $atts = $atts + ['placeholder' => esc_html__('Username', 'wp-user-avatar')];
136
137 if ($this->form_type == FormRepository::EDIT_PROFILE_TYPE) {
138
139 $atts = $atts + ['disabled' => 'disabled'];
140
141 $atts['value'] = esc_attr($this->current_user->user_login);
142
143 $required = false;
144 }
145
146 $attributes = $this->field_attributes($this->tag_name . '_username', $this->valid_field_atts(ppress_normalize_attributes($atts)), $required);
147
148 $html = "<input name='" . $this->tag_name . "_username' type='text' $attributes>";
149
150 return apply_filters('ppress_' . $this->form_name . '_username_field', $html, $atts);
151 }
152
153 /**
154 * @param array $atts
155 *
156 * @return string
157 */
158 public function password($atts)
159 {
160 if (empty($atts)) $atts = [];
161
162 $atts = $atts + ['placeholder' => esc_html__('Password', 'wp-user-avatar')];
163
164 $attributes = $this->field_attributes($this->tag_name . '_password', $this->valid_field_atts(ppress_normalize_attributes($atts)), true);
165
166 if ($this->form_type == FormRepository::EDIT_PROFILE_TYPE) {
167 $attributes = $this->field_attributes('ignore_value', $this->valid_field_atts(ppress_normalize_attributes($atts)), false);
168 }
169
170 $html = "<input name='" . $this->tag_name . "_password' type='password' $attributes>";
171
172 if ($this->form_type == FormRepository::REGISTRATION_TYPE) {
173 $html .= '<input name="' . $this->tag_name . '_password_present" type="hidden" value="true">';
174 }
175
176 return apply_filters('ppress_' . $this->form_name . '_password_field', $html, $atts);
177 }
178
179 /**
180 * @param array $atts
181 *
182 * @return string
183 */
184 public function confirm_password($atts)
185 {
186 if (empty($atts)) $atts = [];
187
188 $atts = $atts + ['placeholder' => esc_html__('Confirm Password', 'wp-user-avatar')];
189
190 $attributes = $this->field_attributes($this->tag_name . '_password2', $this->valid_field_atts(ppress_normalize_attributes($atts)), true);
191
192 $html = "<input name='" . $this->tag_name . "_password2' type='password' $attributes>";
193
194 return apply_filters('ppress_' . $this->form_name . '_confirm_password_field', $html, $atts);
195 }
196
197 /**
198 * Callback function for email
199 *
200 * @param $atts
201 *
202 * @return string
203 */
204 public function email($atts)
205 {
206 $required = true;
207
208 if (empty($atts)) $atts = [];
209
210 $atts = $atts + ['placeholder' => esc_html__('Email Address', 'wp-user-avatar')];
211
212 if ($this->form_type == FormRepository::EDIT_PROFILE_TYPE) {
213 $required = false;
214 // default username saved in DB
215 $atts['value'] = esc_attr($this->current_user->user_email);
216 }
217
218 $attributes = $this->field_attributes($this->tag_name . '_email', $this->valid_field_atts(ppress_normalize_attributes($atts)), $required);
219
220 $html = "<input name='" . $this->tag_name . "_email' type='text' $attributes>";
221
222 return apply_filters('ppress_' . $this->form_name . '_email_field', $html, $atts);
223 }
224
225 /**
226 * @param array $atts
227 *
228 * @return string
229 */
230 public function confirm_email($atts)
231 {
232 if (empty($atts)) $atts = [];
233
234 $atts = $atts + ['placeholder' => esc_html__('Confirm Email Address', 'wp-user-avatar')];
235
236 $attributes = $this->field_attributes($this->tag_name . '_email2', $this->valid_field_atts(ppress_normalize_attributes($atts)), true);
237
238 $html = "<input name='" . $this->tag_name . "_email2' type='text' $attributes>";
239
240 return apply_filters('ppress_' . $this->form_name . '_confirm_email_field', $html, $atts);
241
242 }
243
244 /**
245 * @param $atts
246 *
247 * @return string
248 */
249 public function website($atts)
250 {
251 if (empty($atts)) $atts = [];
252
253 $atts = $atts + ['placeholder' => esc_html__('Website', 'wp-user-avatar')];
254
255 if ($this->form_type == FormRepository::EDIT_PROFILE_TYPE) {
256 // default username saved in DB
257 $atts['value'] = esc_attr($this->current_user->user_url);
258 }
259
260 $field_name = $this->tag_name . '_website';
261
262 $attributes = $this->field_attributes($field_name, $this->valid_field_atts(ppress_normalize_attributes($atts)));
263
264 $html = "<input name='$field_name' type='text' $attributes>";
265
266 if ($this->form_type == FormRepository::REGISTRATION_TYPE && $this->is_field_required($atts)) {
267 $value = apply_filters('ppress_website_required_field', esc_html__('Website', 'wp-user-avatar'));
268 $html .= "<input name='required-fields[$field_name]' type='hidden' value='$value'>";
269 }
270
271 return apply_filters('ppress_' . $this->form_name . '_website_field', $html, $atts);
272 }
273
274
275 /**
276 * Callback function for nickname
277 *
278 * @param $atts
279 *
280 * @return string
281 */
282 public function nickname($atts)
283 {
284 if (empty($atts)) $atts = [];
285
286 $atts = $atts + ['placeholder' => esc_html__('Nickname', 'wp-user-avatar')];
287
288 if ($this->form_type == FormRepository::EDIT_PROFILE_TYPE) {
289 // default username saved in DB
290 $atts['value'] = esc_attr($this->current_user->nickname);
291 }
292
293 $field_name = $this->tag_name . '_nickname';
294
295 $attributes = $this->field_attributes($field_name, $this->valid_field_atts(ppress_normalize_attributes($atts)));
296
297 $html = "<input name='$field_name' type='text' $attributes>";
298
299 if ($this->form_type == FormRepository::REGISTRATION_TYPE && $this->is_field_required($atts)) {
300 $value = apply_filters('ppress_nickname_required_field', esc_html__('Nickname', 'wp-user-avatar'));
301 $html .= "<input name='required-fields[$field_name]' type='hidden' value='$value'>";
302 }
303
304 return apply_filters('ppress_' . $this->form_name . '_nickname_field', $html, $atts);
305 }
306
307 /**
308 * Callback function for nickname
309 *
310 * @param $atts
311 *
312 * @return string
313 */
314 public function display_name($atts)
315 {
316 if (empty($atts)) $atts = [];
317
318 $atts = $atts + ['placeholder' => esc_html__('Display Name', 'wp-user-avatar')];
319
320 if ($this->form_type == FormRepository::EDIT_PROFILE_TYPE) {
321 // default username saved in DB
322 $atts['value'] = esc_attr($this->current_user->display_name);
323 }
324
325 $field_name = $this->tag_name . '_display_name';
326
327 $attributes = $this->field_attributes($field_name, $this->valid_field_atts(ppress_normalize_attributes($atts)));
328
329 $html = "<input name='$field_name' type='text' $attributes>";
330
331 if ($this->form_type == FormRepository::REGISTRATION_TYPE && $this->is_field_required($atts)) {
332 $value = apply_filters('ppress_display_name_required_field', esc_html__('Display name', 'wp-user-avatar'));
333 $html .= "<input name='required-fields[$field_name]' type='hidden' value='$value'>";
334 }
335
336 return apply_filters('ppress_' . $this->form_name . '_display_name_field', $html, $atts);
337 }
338
339 /**
340 * Callback function for first name
341 *
342 * @param $atts
343 *
344 * @return string
345 */
346 public function first_name($atts)
347 {
348 if (empty($atts)) $atts = [];
349
350 $atts = $atts + ['placeholder' => esc_html__('First Name', 'wp-user-avatar')];
351
352 if ($this->form_type == FormRepository::EDIT_PROFILE_TYPE) {
353 // default username saved in DB
354 $atts['value'] = esc_attr($this->current_user->first_name);
355 }
356
357 $field_name = $this->tag_name . '_first_name';
358
359 $attributes = $this->field_attributes($field_name, $this->valid_field_atts(ppress_normalize_attributes($atts)));
360
361 $html = "<input name='$field_name' type='text' $attributes>";
362
363 if ($this->form_type == FormRepository::REGISTRATION_TYPE && $this->is_field_required($atts)) {
364 $value = apply_filters('ppress_first_name_required_field', esc_html__('First name', 'wp-user-avatar'));
365 $html .= "<input name='required-fields[$field_name]' type='hidden' value='$value'>";
366 }
367
368 return apply_filters('ppress_' . $this->form_name . '_first_name_field', $html, $atts);
369 }
370
371
372 /**
373 * Callback for last name
374 *
375 * @param $atts
376 *
377 * @return string
378 */
379 public function last_name($atts)
380 {
381 if (empty($atts)) $atts = [];
382
383 $atts = $atts + ['placeholder' => esc_html__('Last Name', 'wp-user-avatar')];
384
385 if ($this->form_type == FormRepository::EDIT_PROFILE_TYPE) {
386 // default username saved in DB
387 $atts['value'] = esc_attr($this->current_user->last_name);
388 }
389
390 $field_name = $this->tag_name . '_last_name';
391
392 $attributes = $this->field_attributes($field_name, $this->valid_field_atts(ppress_normalize_attributes($atts)));
393
394 $html = "<input name='$field_name' type='text' $attributes>";
395
396 if ($this->form_type == FormRepository::REGISTRATION_TYPE && $this->is_field_required($atts)) {
397 $value = apply_filters('ppress_last_name_required_field', esc_html__('Last name', 'wp-user-avatar'));
398 $html .= "<input name='required-fields[$field_name]' type='hidden' value='$value'>";
399 }
400
401 return apply_filters('ppress_' . $this->form_name . '_last_name_field', $html, $atts);
402 }
403
404 /**
405 * @param $atts
406 *
407 * @return string
408 */
409 public function bio($atts)
410 {
411 if (empty($atts)) $atts = [];
412
413 $atts = $atts + ['placeholder' => esc_html__('Biographical Info', 'wp-user-avatar')];
414
415 if ($this->form_type == FormRepository::EDIT_PROFILE_TYPE) {
416 // default username saved in DB
417 $atts['value'] = esc_attr($this->current_user->description);
418 }
419
420 $field_name = $this->tag_name . '_bio';
421
422 $attributes = $this->field_attributes('ignore_value', $this->valid_field_atts(ppress_normalize_attributes($atts)));
423
424 $value = isset($_POST[$field_name]) ? wp_kses_post($_POST[$field_name]) : @wp_kses_post($atts['value']);
425
426 $html = "<textarea name=\"$field_name\" $attributes>$value</textarea>";
427
428 if ($this->form_type == FormRepository::REGISTRATION_TYPE && $this->is_field_required($atts)) {
429 $value = apply_filters('ppress_bio_required_field', esc_html__('Bio description', 'wp-user-avatar'));
430 $html .= "<input name='required-fields[$field_name]' type='hidden' value='$value'>";
431 }
432
433 return apply_filters('ppress_' . $this->form_name . '_bio_field', $html, $atts);
434 }
435
436 /**
437 * Upload avatar field
438 */
439 public function avatar($atts)
440 {
441 if (empty($atts)) $atts = [];
442
443 $field_name = $this->tag_name . '_avatar';
444
445 $attributes = $this->field_attributes($field_name, $this->valid_field_atts(ppress_normalize_attributes($atts)));
446
447 $html = "<input name='$field_name' type='file' $attributes>";
448
449 if ($this->form_type == FormRepository::REGISTRATION_TYPE && $this->is_field_required($atts)) {
450 $value = apply_filters('ppress_avatar_required_field', esc_html__('Profile picture', 'wp-user-avatar'));
451 $html .= "<input name='required-fields[$field_name]' type='hidden' value='$value'>";
452 }
453
454 return apply_filters('ppress_' . $this->form_name . '_avatar_field', $html, $atts);
455 }
456
457 /**
458 * Upload cover image field
459 */
460 public function cover_image($atts)
461 {
462 if (empty($atts)) $atts = [];
463
464 $field_name = $this->tag_name . '_cover_image';
465
466 $attributes = $this->field_attributes($field_name, $this->valid_field_atts(ppress_normalize_attributes($atts)));
467
468 $html = "<input name='$field_name' type='file' $attributes>";
469
470 if ($this->form_type == FormRepository::REGISTRATION_TYPE && $this->is_field_required($atts)) {
471 $value = apply_filters('ppress_cover_image_required_field', esc_html__('Cover image', 'wp-user-avatar'));
472 $html .= "<input name='required-fields[$field_name]' type='hidden' value='$value'>";
473 }
474
475 return apply_filters('ppress_' . $this->form_name . '_cover_image_field', $html, $atts);
476 }
477
478 /**
479 * @param $atts
480 *
481 * @return string
482 */
483 public function textbox_field($atts)
484 {
485 if (empty($atts)) $atts = [];
486
487 $atts = array_replace(['type' => 'text'], $atts);
488
489 if ( ! isset($atts['key']) || empty($atts['key'])) {
490 return esc_html__('Field key is missing', 'wp-user-avatar');
491 }
492
493 $key = ppress_sanitize_key($atts['key']);
494
495 $type = sanitize_text_field($atts['type']);
496
497 if ($this->form_type == FormRepository::EDIT_PROFILE_TYPE) {
498 $db_data = ! empty($atts['value']) ? sanitize_text_field($atts['value']) : $this->current_user->$key;
499 $atts['value'] = isset($_POST[$key]) ? esc_attr($_POST[$key]) : $db_data;
500 }
501
502 $attributes = $this->field_attributes($key, $this->valid_field_atts(ppress_normalize_attributes($atts)));
503
504 $html = "<input name='" . $key . "' type='$type' $attributes>";
505
506 if ($this->form_type == FormRepository::REGISTRATION_TYPE && $this->is_field_required($atts)) {
507 $value = apply_filters('ppress_custom_required_field', $this->human_readable_field_key($key), $key);
508 $html .= "<input name='required-fields[$key]' type='hidden' value='$value'>";
509 }
510
511 return $html;
512 }
513
514 public function number_field($atts)
515 {
516 if (empty($atts)) $atts = [];
517
518 $atts['type'] = 'number';
519
520 return $this->textbox_field($atts);
521 }
522
523 /**
524 * @param $atts
525 *
526 * @return string
527 */
528 public function cf_password_field($atts)
529 {
530 if (empty($atts)) $atts = [];
531
532 $atts['type'] = 'password';
533
534 return $this->textbox_field($atts);
535 }
536
537 /**
538 * @param $atts
539 *
540 * @return string
541 */
542 public function country_field($atts)
543 {
544 if (empty($atts)) $atts = [];
545
546 if (empty($atts['key'])) {
547 return esc_html__('Field key is missing', 'wp-user-avatar');
548 }
549
550 $key = ppress_sanitize_key($atts['key']);
551
552 $value = isset($_POST[$key]) ? sanitize_text_field($_POST[$key]) : @sanitize_text_field($atts['value']);
553
554 if ($this->form_type == FormRepository::EDIT_PROFILE_TYPE) {
555 $db_data = ! empty($atts['value']) ? sanitize_text_field($atts['value']) : $this->current_user->$key;
556 $value = isset($_POST[$key]) ? sanitize_text_field($_POST[$key]) : $db_data;
557 }
558
559 $countries = ppress_array_of_world_countries();
560
561 $attributes = $this->field_attributes('ignore_value', $this->valid_field_atts($atts));
562
563 $html = "<select name='$key' $attributes>";
564 $html .= '<option value="">' . esc_html__('Select country', 'wp-user-avatar') . '&hellip;</option>';
565
566 foreach ($countries as $ckey => $cvalue) {
567 $html .= '<option value="' . esc_attr($ckey) . '" ' . selected($value, $ckey, false) . '>' . $cvalue . '</option>';
568 }
569
570 $html .= '</select>';
571
572 return $html;
573 }
574
575 public static function hasTime($string)
576 {
577 $timeStrings = ['H', 'h', 'G', 'i', 'S', 's', 'K'];
578 foreach ($timeStrings as $timeString) {
579 if (strpos($string, $timeString) != false) {
580 return true;
581 }
582 }
583
584 return false;
585 }
586
587 public static function hasDate($string)
588 {
589 $dateStrings = ['d', 'D', 'l', 'j', 'J', 'w', 'W', 'F', 'm', 'n', 'M', 'U', 'Y', 'y', 'Z'];
590 foreach ($dateStrings as $dateString) {
591 if (strpos($string, $dateString) != false) {
592 return 'true';
593 }
594 }
595
596 return false;
597 }
598
599 /**
600 * @param $atts
601 *
602 * @return string
603 */
604 public function date_field($atts)
605 {
606 if (empty($atts)) $atts = [];
607
608 if (empty($atts['key'])) return esc_html__('Field key is missing', 'wp-user-avatar');
609
610 $key = ppress_sanitize_key($atts['key']);
611
612 $atts['class'] = "pp_datepicker $key " . @$atts['class'];
613
614 if ($this->form_type == FormRepository::EDIT_PROFILE_TYPE) {
615 $atts['value'] = isset($_POST[$key]) ? esc_attr($_POST[$key]) : $this->current_user->$key;
616 }
617
618 $attributes = $this->field_attributes($key, $this->valid_field_atts($atts));
619
620 $html = "<input name='" . $key . "' type='text' $attributes>";
621
622 if ($this->form_type == FormRepository::REGISTRATION_TYPE && $this->is_field_required($atts)) {
623 $value = apply_filters('ppress_custom_required_field', $this->human_readable_field_key($key), $key);
624 $html .= "<input name='required-fields[$key]' type='hidden' value='$value'>";
625 }
626
627 $dateFormat = ! empty($atts['date_format']) ? $atts['date_format'] : 'Y-m-d';
628 // defined fields in custom fields settings page has date format saved in options $atts
629 if ( ! empty($atts['options'])) {
630 $dateFormat = $atts['options'];
631 }
632
633 $hasTime = self::hasTime($dateFormat);
634 $time24 = false;
635
636 if ($hasTime && strpos($dateFormat, 'H') !== false) {
637 $time24 = true;
638 }
639
640 $config = apply_filters('ppress_frontend_flatpickr_date_config', [
641 'dateFormat' => $dateFormat,
642 'enableTime' => $hasTime,
643 'noCalendar' => ! self::hasDate($dateFormat),
644 'disableMobile' => true,
645 'time_24hr' => $time24
646 ]);
647
648 $html .= sprintf(
649 '<script type="text/javascript">jQuery(function() {jQuery( ".pp_datepicker.%s" ).flatpickr(%s);});</script>',
650 $key, json_encode($config)
651 );
652
653 return $html;
654 }
655
656 /**
657 * @param $atts
658 *
659 * @return string
660 */
661 public function textarea_field($atts)
662 {
663 if (empty($atts)) $atts = [];
664
665 if (empty($atts['key'])) return esc_html__('Field key is missing', 'wp-user-avatar');
666
667 $key = ppress_sanitize_key($atts['key']);
668
669 $value = isset($_POST[$key]) ? esc_textarea($_POST[$key]) : @esc_textarea($atts['value']);
670
671 if ($this->form_type == FormRepository::EDIT_PROFILE_TYPE) {
672 $value = isset($_POST[$key]) ? esc_textarea($_POST[$key]) : $this->current_user->$key;
673 }
674
675 $attributes = $this->field_attributes($key, $this->valid_field_atts(ppress_normalize_attributes($atts)));
676
677 $html = "<textarea name=\"$key\" $attributes>$value</textarea>";
678
679 if ($this->form_type == FormRepository::REGISTRATION_TYPE && $this->is_field_required($atts)) {
680 $value = apply_filters('ppress_custom_required_field', $this->human_readable_field_key($key), $key);
681 $html .= "<input name='required-fields[$key]' type='hidden' value='$value'>";
682 }
683
684 return $html;
685 }
686
687 public function select_dropdown_field($atts)
688 {
689 if (empty($atts)) $atts = [];
690
691 if (empty($atts['key'])) return esc_html__('Field key is missing', 'wp-user-avatar');
692
693 $key = ppress_sanitize_key($atts['key']);
694
695 if ( ! isset($atts['options']) || empty($atts['options'])) return esc_html__('No dropdown option found.', 'wp-user-avatar');
696
697 $is_multiple = isset($atts['is_multiple']) && $atts['is_multiple'] == '1' ? 'multiple' : '';
698 $select2_class_name = $is_multiple == 'multiple' ? 'ppress-select2 ' : '';
699 $data_placeholder_attr = $is_multiple == 'multiple' ? ' data-placeholder="' . @esc_attr($atts['placeholder']) . '"' : '';
700
701 $atts['class'] = $select2_class_name . @esc_attr($atts['class']);
702
703 $attributes = $this->field_attributes('ignore_value', $this->valid_field_atts(ppress_normalize_attributes($atts)));
704
705 $select_tag_key = $is_multiple == 'multiple' ? "{$key}[]" : $key;
706 $html = "<input type='hidden' name=\"$select_tag_key\" value=''>";
707 $html .= "<select name=\"$select_tag_key\"$data_placeholder_attr $attributes $is_multiple>";
708
709 $option_values = is_array($atts['options']) ? implode(',', $atts['options']) : $atts['options'];
710
711 if ( ! empty($option_values)) {
712
713 $_POST = $this->GET_POST();
714
715 $option_values = explode(',', $option_values);
716
717 foreach ($option_values as $value) {
718 $value = trim($value);
719
720 $selected = is_array(@$_POST[$key]) && in_array($value, @$_POST[$key]) ? 'selected="selected"' : @selected(@$_POST[$key], $value, false);
721
722 if ($this->form_type == FormRepository::EDIT_PROFILE_TYPE) {
723 $selected = '';
724 if (is_array(@$_POST[$key]) && in_array($value, @$_POST[$key])) $selected = 'selected="selected"';
725 // !isset($_POST[ $key ] is called to not run the succeeding code if the form is submitted.
726 // to enable the select dropdown retain the submitted options when an error occur/ prevent the form from saving.
727 elseif ( ! isset($_POST[$key]) && is_array($this->current_user->$key) && in_array($value, $this->current_user->$key)) {
728 $selected = 'selected="selected"';
729 } elseif ( ! isset($_POST[$key]) && ! is_array($this->current_user->$key) && $value == $this->current_user->$key) {
730 $selected = 'selected="selected"';
731 }
732 }
733
734 $html .= "<option value=\"$value\" $selected>$value</option>";
735 }
736 }
737
738 $html .= '</select>';
739 // if field is required, add an hidden field
740 if ($this->form_type == FormRepository::REGISTRATION_TYPE && $this->is_field_required($atts)) {
741 $value = apply_filters('ppress_custom_required_field', $this->human_readable_field_key($key), $key);
742 $html .= "<input name='required-fields[$key]' type='hidden' value='$value'>";
743 }
744
745 if ($is_multiple) {
746 $limit = absint(@$atts['limit']);
747 $html .= $this->select2_js_script($key, $limit);
748 }
749
750 return $html;
751 }
752
753 public function radio_buttons_field($atts)
754 {
755 if (empty($atts)) $atts = [];
756
757 if (empty($atts['key'])) {
758 return esc_html__('Field key is missing', 'wp-user-avatar');
759 }
760
761 $key = ppress_sanitize_key($atts['key']);
762
763 if ( ! isset($atts['options']) || empty($atts['options'])) {
764 return esc_html__('No radio choice found.', 'wp-user-avatar');
765 }
766
767 $attributes = $this->field_attributes('ignore_value', $this->valid_field_atts(ppress_normalize_attributes($atts)));
768
769 $option_values = explode(',', $atts['options']);
770
771 $_POST = $this->GET_POST();
772
773 $html = '<div class="pp-radios-container">';
774
775 foreach ($option_values as $value) {
776 $value = trim($value);
777
778 $checked = @checked($_POST[$key], $value, false);
779
780 if ($this->form_type == FormRepository::EDIT_PROFILE_TYPE) {
781 $db_data = ! empty($atts['value']) ? sanitize_text_field($atts['value']) : $this->current_user->$key;
782 $checked = @checked(
783 isset($_POST[$key]) && ! empty($_POST[$key]) ? $_POST[$key] : $db_data,
784 $value,
785 false
786 );
787 }
788
789 $backward_compat_class = '';
790 if ($this->form_type == FormRepository::REGISTRATION_TYPE) {
791 $backward_compat_class = ' profilepress-reg-label';
792 }
793
794 $html .= '<div class="pp-radio-wrap">';
795 $html .= "<input type='radio' name=\"$key\" value=\"$value\" id=\"$value\" $checked $attributes>";
796 $html .= "<label class=\"pp-form-label{$backward_compat_class}\" for=\"$value\">$value</label>";
797 $html .= '</div>';
798 }
799
800 $html .= '</div>';
801
802 if ($this->form_type == FormRepository::REGISTRATION_TYPE && $this->is_field_required($atts)) {
803 $value = apply_filters('ppress_custom_required_field', $this->human_readable_field_key($key), $key);
804 $html .= "<input name='required-fields[$key]' type='hidden' value='$value'>";
805 }
806
807 return $html;
808 }
809
810 public function checkbox_list_field($atts)
811 {
812 if (empty($atts)) $atts = [];
813
814 if (empty($atts['key'])) return esc_html__('Field key is missing', 'wp-user-avatar');
815
816 $key = ppress_sanitize_key($atts['key']);
817
818 if ( ! isset($atts['options']) || empty($atts['options'])) {
819 return esc_html__('No checkbox choice found.', 'wp-user-avatar');
820 }
821
822 $attributes = $this->field_attributes('ignore_value', $this->valid_field_atts(ppress_normalize_attributes($atts)));
823
824 $checkbox_tag_key = "{$key}[]";
825
826 $option_values = explode(',', $atts['options']);
827
828 $html = '<div class="pp-checkboxes-container">';
829
830 $_POST = $this->GET_POST();
831
832 foreach ($option_values as $value) {
833
834 $value = trim($value);
835
836 $checked = is_array(@$_POST[$key]) && in_array($value, @$_POST[$key]) ? 'checked="checked"' : @checked(@$_POST[$key], $value, false);
837
838 if ($this->form_type == FormRepository::EDIT_PROFILE_TYPE) {
839 $checked = '';
840 if (isset($_POST[$key]) && is_array(@$_POST[$key]) && in_array($value, @$_POST[$key])) {
841 $checked = 'checked="checked"';
842 } elseif ( ! isset($_POST[$key]) && is_array($this->current_user->$key) && in_array($value, $this->current_user->$key)) {
843 $checked = 'checked="checked"';
844 } elseif ( ! isset($_POST[$key]) && ! is_array($this->current_user->$key) && $value == $this->current_user->$key) {
845 $checked = 'checked="checked"';
846 }
847 }
848
849 $html .= '<div class="pp-checkbox-wrap pp-multi-checkbox">';
850 $html .= "<input type='checkbox' name=\"$checkbox_tag_key\" value=\"$value\" id=\"$value\" $attributes $checked>";
851 $html .= "<label class='pp-form-label' for=\"$value\">$value</label>";
852 $html .= '</div>';
853 }
854
855 $html .= '</div>';
856
857 if ($this->form_type == FormRepository::REGISTRATION_TYPE && $this->is_field_required($atts)) {
858 $value = apply_filters('ppress_custom_required_field', $this->human_readable_field_key($key), $key);
859 $html .= "<input name='required-fields[$key]' type='hidden' value='$value'>";
860 }
861
862 return $html;
863 }
864
865 public function single_checkbox_field($atts)
866 {
867 if (empty($atts)) $atts = [];
868
869 $_POST = $this->GET_POST();
870
871 $attributes = $this->field_attributes('ignore_value', $this->valid_field_atts(ppress_normalize_attributes($atts)));
872
873 if (empty($atts['key'])) return esc_html__('Field key is missing', 'wp-user-avatar');
874
875 $key = ppress_sanitize_key($atts['key']);
876
877 $html = '<div class="pp-checkbox-wrap pp-single-checkbox">';
878 $field_label = isset($atts['checkbox_text']) ? html_entity_decode($atts['checkbox_text']) : '';
879
880 // checked for checkbox
881 $checked = checked(isset($_POST[$key]) ? isset($_POST[$key]) : '', 'true', false);
882
883 if ($this->form_type == FormRepository::EDIT_PROFILE_TYPE) {
884 $db_data = ! empty($atts['value']) ? sanitize_text_field($atts['value']) : $this->current_user->$key;
885 $checked = @checked(
886 ! empty($_POST[$key]) ? $_POST[$key] : $db_data,
887 'true',
888 false
889 );
890 }
891
892 $html .= "<input type='hidden' name=\"$key\" value=\"false\" style='display: none'>";
893 $html .= "<input type='checkbox' name=\"$key\" value=\"true\" id=\"$key\" $checked $attributes>";
894 $html .= "<label class='pp-form-label' for=\"$key\">$field_label</label>";
895 $html .= '</div>';
896
897 if ($this->form_type == FormRepository::REGISTRATION_TYPE && $this->is_field_required($atts)) {
898 $value = apply_filters('ppress_custom_required_field', $this->human_readable_field_key($key), $key);
899 $html .= "<input name='required-fields[$key]' type='hidden' value='$value'>";
900 }
901
902 return $html;
903 }
904
905 /**
906 * @param $atts
907 *
908 * @return string
909 */
910 public function custom_profile_field($atts)
911 {
912 if ( ! EM::is_enabled(EM::CUSTOM_FIELDS)) return '';
913
914 if (empty($atts)) $atts = [];
915
916 $_POST = $this->GET_POST();
917
918 $atts = ppress_normalize_attributes($atts);
919
920 $key = ppress_sanitize_key($atts['key']);
921
922 if (empty($key)) return esc_html__('Field key is missing', 'wp-user-avatar');
923
924 $type = ! empty($atts['type']) ? $atts['type'] : PROFILEPRESS_sql::get_field_type($key);
925
926 if (empty($type) || ! $type) {
927 $type = 'text';
928 }
929
930 $html = esc_html__('custom field not defined', 'wp-user-avatar');
931
932 if ($type == 'select') {
933 $atts['options'] = PROFILEPRESS_sql::get_field_option_values($key);
934 $atts['is_multiple'] = ppress_is_select_field_multi_selectable($key) ? '1' : '';
935 $html = $this->select_dropdown_field($atts);
936 }
937
938 if ($type == 'radio') {
939 $atts['key'] = $key;
940 $atts['options'] = PROFILEPRESS_sql::get_field_option_values($key);
941 $html = $this->radio_buttons_field($atts);
942 }
943
944 if ($type == 'agreeable') {
945 $atts['key'] = $key;
946 $atts['checkbox_text'] = $field_label = html_entity_decode(PROFILEPRESS_sql::get_field_label($key));
947 $html = $this->single_checkbox_field($atts);
948 }
949
950 if ($type == 'checkbox') {
951 $atts['key'] = $key;
952 $atts['options'] = PROFILEPRESS_sql::get_field_option_values($key);
953
954 $html = $this->checkbox_list_field($atts);
955 }
956
957 if ($type == 'textarea') {
958 $html = $this->textarea_field($atts);
959 }
960
961 if ($type == 'country') {
962 $html = $this->country_field($atts);
963 }
964
965 if ($type == 'date') {
966 $atts['options'] = PROFILEPRESS_sql::get_field_option_values($key);
967 $html = $this->date_field($atts);
968 }
969
970 if ('file' == $type) {
971
972 $attributes = $this->field_attributes($key, $this->valid_field_atts($atts));
973
974 $html = "<input name='" . $key . "' type='file' $attributes>";
975 // if field is required, add an hidden field
976 if ($this->form_type == FormRepository::REGISTRATION_TYPE && $this->is_field_required($atts)) {
977 $html .= "<input name='required-" . $key . "' type='hidden' value='true' style='display:none'>";
978 }
979 }
980
981 if (in_array($type, ['text', 'password', 'email', 'tel', 'number', 'hidden'])) {
982 $atts['type'] = $type;
983 $html = $this->textbox_field($atts);
984 }
985
986 return apply_filters('ppress_' . $this->form_name . '_cpf_field', $html, $atts);
987 }
988
989 /**
990 * Callback function for submit button
991 *
992 * @param $atts
993 *
994 * @return string
995 */
996 public function submit($atts)
997 {
998 if (empty($atts)) $atts = [];
999
1000 $field_name = isset($atts['name']) ? esc_attr($atts['name']) : $this->tag_name . '_submit';
1001 $value = esc_html__('Sign Up', 'wp-user-avatar');
1002
1003 if ($this->form_type == FormRepository::EDIT_PROFILE_TYPE) {
1004 $value = esc_html__('Save Changes', 'wp-user-avatar');;
1005 }
1006
1007 $atts = apply_filters('ppress_' . $this->form_name . '_submit_field_atts', $this->valid_field_atts(ppress_normalize_attributes($atts)));
1008 $atts['value'] = isset($atts['value']) ? esc_attr($atts['value']) : $value;
1009
1010
1011 $form_type = $this->form_type;
1012 $form_id = isset($GLOBALS['pp_registration_form_id']) ? $GLOBALS['pp_registration_form_id'] : 0;
1013 if ($form_type == FormRepository::EDIT_PROFILE_TYPE) {
1014 $form_id = isset($GLOBALS['pp_edit_profile_form_id']) ? $GLOBALS['pp_edit_profile_form_id'] : 0;
1015 }
1016
1017 if (isset($GLOBALS['pp_melange_form_id'])) {
1018 $form_id = $GLOBALS['pp_melange_form_id'];
1019 $form_type = FormRepository::MELANGE_TYPE;
1020 }
1021
1022 $processing_label = ! empty($atts['processing_label']) ? $atts['processing_label'] : FormRepository::get_processing_label($form_id, $form_type);
1023
1024
1025 $attributes = $this->field_attributes($field_name, $atts);
1026
1027 $html = sprintf(
1028 '<input data-pp-submit-label="%1$s" data-pp-processing-label="%2$s" name="%3$s" type="submit" %4$s>',
1029 $atts['value'],
1030 esc_attr($processing_label),
1031 $field_name,
1032 $attributes
1033 );
1034
1035 $html .= ppress_nonce_field();
1036
1037 return apply_filters('ppress_' . $this->form_name . '_submit_field', $html, $atts);
1038 }
1039
1040 public function select2_js_script($key, $limit = 0)
1041 {
1042 $limit = absint($limit);
1043
1044 return <<<SCRIPT
1045 <script type='text/javascript'>
1046 jQuery(function() {
1047 var selector = jQuery('select[name^="$key"].ppress-select2');
1048 selector.select2({width: '100%', maximumSelectionLength: $limit});
1049 });
1050 </script>
1051 SCRIPT;
1052 }
1053
1054 /**
1055 * Remove a user avatar
1056 *
1057 * @param $atts
1058 *
1059 * @return string
1060 */
1061 public function remove_user_avatar($atts)
1062 {
1063 if (empty($atts)) $atts = [];
1064
1065 $other_atts_html = ppress_other_field_atts($atts);
1066
1067 $atts = shortcode_atts([
1068 'class' => '',
1069 'id' => '',
1070 'title' => '',
1071 'label' => esc_html__('Delete Avatar', 'wp-user-avatar'),
1072 ], $atts
1073 );
1074
1075 $atts = apply_filters('ppress_edit_profile_remove_avatar_button_atts', $atts);
1076
1077 $class = 'class="pp-del-profile-avatar ' . $atts['class'] . '"';
1078 $label = ! empty($atts['label']) ? $atts['label'] : null;
1079 $id = ! empty($atts['id']) ? 'id="' . $atts['id'] . '"' : null;
1080 $title = 'title="' . $atts['title'] . '"';
1081
1082 // ensure a profile avatar for the user is available before the remove button gets displayed
1083 if (UserAvatar::user_has_pp_avatar($this->current_user->ID)) {
1084 $button = "<button type=\"submit\" name=\"eup_remove_avatar\" value=\"removed\" $class $id $title $other_atts_html>$label</button>";
1085
1086 return apply_filters('ppress_edit_profile_remove_avatar_button', $button, $atts);
1087 }
1088 }
1089
1090 /**
1091 * Remove a user cover image
1092 *
1093 * @param $atts
1094 *
1095 * @return string
1096 */
1097 public function remove_cover_image($atts)
1098 {
1099 if (empty($atts)) $atts = [];
1100
1101 $other_atts_html = ppress_other_field_atts($atts);
1102
1103 $atts = shortcode_atts([
1104 'class' => '',
1105 'id' => '',
1106 'title' => '',
1107 'label' => esc_html__('Delete Cover Image', 'wp-user-avatar'),
1108 ], $atts
1109 );
1110
1111 $atts = apply_filters('ppress_edit_profile_remove_cover_image_button_atts', $atts);
1112
1113 $class = 'class="pp-del-cover-image ' . $atts['class'] . '"';
1114 $label = ! empty($atts['label']) ? $atts['label'] : null;
1115 $id = ! empty($atts['id']) ? 'id="' . $atts['id'] . '"' : null;
1116 $title = 'title="' . $atts['title'] . '"';
1117
1118 if (ppress_user_has_cover_image($this->current_user->ID)) {
1119 $button = "<button type=\"submit\" name=\"eup_remove_cover_image\" value=\"removed\" $class $id $title $other_atts_html>$label</button>";
1120
1121 return apply_filters('ppress_edit_profile_remove_cover_image_button', $button, $atts);
1122 }
1123 }
1124 }