EditProfileBuilder.php
3 years ago
FieldsShortcodeCallback.php
4 months ago
FrontendProfileBuilder.php
10 months ago
GlobalShortcodes.php
3 years ago
LoginFormBuilder.php
2 years ago
PasswordResetBuilder.php
4 weeks ago
RegistrationFormBuilder.php
1 year ago
index.php
5 years ago
FieldsShortcodeCallback.php
1253 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePress\Core\ShortcodeParser\Builder; |
| 4 | |
| 5 | use ProfilePress\Core\Classes\FormRepository; |
| 6 | use ProfilePress\Core\Classes\PROFILEPRESS_sql; |
| 7 | use ProfilePress\Core\Classes\UserAvatar; |
| 8 | use ProfilePress\Core\Membership\CheckoutFields; |
| 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, $form_name = '', $tag_name = '') |
| 22 | { |
| 23 | $this->form_type = $form_type; |
| 24 | |
| 25 | $this->form_name = $form_type == FormRepository::REGISTRATION_TYPE ? 'registration' : 'edit_profile'; |
| 26 | if ( ! empty($form_name)) { |
| 27 | $this->form_name = $form_name; |
| 28 | } |
| 29 | |
| 30 | $this->tag_name = $form_type == FormRepository::REGISTRATION_TYPE ? 'reg' : 'eup'; |
| 31 | if ( ! empty($tag_name)) { |
| 32 | $this->tag_name = $tag_name; |
| 33 | } |
| 34 | |
| 35 | $flag = false; |
| 36 | if (function_exists('wp_get_current_user')) { |
| 37 | $this->get_current_user(); |
| 38 | if (is_object($this->current_user) && method_exists($this->current_user, 'exists') && $this->current_user->exists()) { |
| 39 | $flag = true; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | if ( ! $flag) { |
| 44 | add_action('init', [$this, 'get_current_user']); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | public function get_current_user() |
| 49 | { |
| 50 | $current_user = wp_get_current_user(); |
| 51 | if ($current_user instanceof \WP_User) { |
| 52 | $this->current_user = $current_user; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | public function GET_POST() |
| 57 | { |
| 58 | return array_merge($_GET, $_POST); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Is field a required field? |
| 63 | * |
| 64 | * @param array $atts |
| 65 | * |
| 66 | * @return bool |
| 67 | */ |
| 68 | public function is_field_required($atts) |
| 69 | { |
| 70 | $atts = ppress_normalize_attributes($atts); |
| 71 | |
| 72 | return isset($atts['required']) && in_array($atts['required'], [true, 'true', '1'], true); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Rewrite custom field key to something more human readable. |
| 77 | * |
| 78 | * @param string $key field key |
| 79 | * |
| 80 | * @return string |
| 81 | */ |
| 82 | public function human_readable_field_key($key) |
| 83 | { |
| 84 | $field = PROFILEPRESS_sql::get_profile_custom_field_by_key($key); |
| 85 | |
| 86 | if ($field && ! empty($field['label_name'])) { |
| 87 | return sanitize_text_field($field['label_name']); |
| 88 | } |
| 89 | |
| 90 | return ucfirst(str_replace(['ppress_', '_'], ['', ' '], $key)); |
| 91 | } |
| 92 | |
| 93 | public static function sanitize_field_attributes($atts) |
| 94 | { |
| 95 | if ( ! is_array($atts)) return $atts; |
| 96 | |
| 97 | $invalid_atts = array( |
| 98 | 'enforce', |
| 99 | 'key', |
| 100 | 'type', |
| 101 | 'field_key', |
| 102 | 'limit', |
| 103 | 'options', |
| 104 | 'key_value_options', |
| 105 | 'checkbox_text', |
| 106 | 'date_format', |
| 107 | 'field_width', |
| 108 | 'icon', |
| 109 | 'checked_state', |
| 110 | 'billing_country' |
| 111 | ); |
| 112 | |
| 113 | $valid_atts = array(); |
| 114 | |
| 115 | foreach ($atts as $key => $value) { |
| 116 | if ( ! in_array($key, $invalid_atts) && strpos($key, 'on') !== 0 && is_string($key) && (is_string($value) || ppress_is_boolean($value))) { |
| 117 | $valid_atts[esc_attr($key)] = esc_attr($value); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | return $valid_atts; |
| 122 | } |
| 123 | |
| 124 | public function valid_field_atts($atts) |
| 125 | { |
| 126 | return self::sanitize_field_attributes($atts); |
| 127 | } |
| 128 | |
| 129 | public function field_attributes($field_name, $atts, $required = 'false') |
| 130 | { |
| 131 | $_POST = $this->GET_POST(); |
| 132 | |
| 133 | if ($field_name !== $this->tag_name . '_submit') { |
| 134 | $atts['required'] = isset($atts['required']) ? esc_attr($atts['required']) : $required; |
| 135 | } |
| 136 | |
| 137 | if ( ! in_array($field_name, ['ignore_value'])) { |
| 138 | $atts['value'] = isset($_POST[$field_name]) ? esc_attr($_POST[$field_name]) : |
| 139 | (isset($atts['value']) && is_string($atts['value']) ? esc_attr($atts['value']) : ''); |
| 140 | } |
| 141 | |
| 142 | $output = []; |
| 143 | |
| 144 | foreach ($atts as $key => $value) { |
| 145 | // ensure no leading/trailing space |
| 146 | $key = sanitize_text_field(trim($key)); |
| 147 | |
| 148 | // skip all onXYZ attributes eg onclick, onmouseover etc |
| 149 | if (strpos($key, 'on') === 0) continue; |
| 150 | |
| 151 | // add class to submit button. |
| 152 | if ($field_name == $this->tag_name . '_submit' && $key == 'class') { |
| 153 | $value = 'pp-submit-form ' . $value; |
| 154 | } |
| 155 | |
| 156 | if ($key != 'required' && ! empty($value)) { |
| 157 | $output[] = sprintf('%s="%s"', esc_attr($key), esc_attr($value)); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | $output = implode(' ', $output); |
| 162 | |
| 163 | if ($this->is_field_required($atts)) { |
| 164 | $output .= ' required="required"'; |
| 165 | } |
| 166 | |
| 167 | return $output; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * @param array $atts |
| 172 | * |
| 173 | * @return string |
| 174 | */ |
| 175 | public function username($atts) |
| 176 | { |
| 177 | $required = true; |
| 178 | |
| 179 | if (empty($atts)) $atts = []; |
| 180 | |
| 181 | // we are using + cos we dont want to override array value if it already exist. |
| 182 | $atts = $atts + ['placeholder' => esc_html__('Username', 'wp-user-avatar')]; |
| 183 | |
| 184 | if ($this->form_type == FormRepository::EDIT_PROFILE_TYPE) { |
| 185 | |
| 186 | $atts = $atts + ['disabled' => 'disabled']; |
| 187 | |
| 188 | $atts['value'] = esc_attr($this->current_user->user_login); |
| 189 | |
| 190 | $required = false; |
| 191 | } |
| 192 | |
| 193 | $attributes = $this->field_attributes($this->tag_name . '_username', $this->valid_field_atts(ppress_normalize_attributes($atts)), $required); |
| 194 | |
| 195 | $html = "<input name='" . $this->tag_name . "_username' type='text' $attributes>"; |
| 196 | |
| 197 | return apply_filters('ppress_' . $this->form_name . '_username_field', $html, $atts); |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * @param array $atts |
| 202 | * |
| 203 | * @return string |
| 204 | */ |
| 205 | public function password($atts) |
| 206 | { |
| 207 | if (empty($atts)) $atts = []; |
| 208 | |
| 209 | $atts = $atts + ['placeholder' => esc_html__('Password', 'wp-user-avatar')]; |
| 210 | |
| 211 | $attributes = $this->field_attributes($this->tag_name . '_password', $this->valid_field_atts(ppress_normalize_attributes($atts)), true); |
| 212 | |
| 213 | if ($this->form_type == FormRepository::EDIT_PROFILE_TYPE) { |
| 214 | $attributes = $this->field_attributes('ignore_value', $this->valid_field_atts(ppress_normalize_attributes($atts)), false); |
| 215 | } |
| 216 | |
| 217 | $html = "<input name='" . $this->tag_name . "_password' type='password' $attributes>"; |
| 218 | |
| 219 | if ($this->form_type == FormRepository::REGISTRATION_TYPE) { |
| 220 | $html .= '<input name="' . $this->tag_name . '_password_present" type="hidden" value="true">'; |
| 221 | } |
| 222 | |
| 223 | return apply_filters('ppress_' . $this->form_name . '_password_field', $html, $atts); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * @param array $atts |
| 228 | * |
| 229 | * @return string |
| 230 | */ |
| 231 | public function confirm_password($atts) |
| 232 | { |
| 233 | if (empty($atts)) $atts = []; |
| 234 | |
| 235 | $atts = $atts + ['placeholder' => esc_html__('Confirm Password', 'wp-user-avatar')]; |
| 236 | |
| 237 | $attributes = $this->field_attributes($this->tag_name . '_password2', $this->valid_field_atts(ppress_normalize_attributes($atts)), true); |
| 238 | |
| 239 | $html = "<input name='" . $this->tag_name . "_password2' type='password' $attributes>"; |
| 240 | |
| 241 | return apply_filters('ppress_' . $this->form_name . '_confirm_password_field', $html, $atts); |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Callback function for email |
| 246 | * |
| 247 | * @param $atts |
| 248 | * |
| 249 | * @return string |
| 250 | */ |
| 251 | public function email($atts) |
| 252 | { |
| 253 | $required = true; |
| 254 | |
| 255 | if (empty($atts)) $atts = []; |
| 256 | |
| 257 | $atts = $atts + ['placeholder' => esc_html__('Email Address', 'wp-user-avatar')]; |
| 258 | |
| 259 | if ($this->form_type == FormRepository::EDIT_PROFILE_TYPE) { |
| 260 | $required = false; |
| 261 | // default username saved in DB |
| 262 | $atts['value'] = esc_attr($this->current_user->user_email); |
| 263 | } |
| 264 | |
| 265 | $attributes = $this->field_attributes($this->tag_name . '_email', $this->valid_field_atts(ppress_normalize_attributes($atts)), $required); |
| 266 | |
| 267 | $html = "<input name='" . $this->tag_name . "_email' type='text' $attributes>"; |
| 268 | |
| 269 | return apply_filters('ppress_' . $this->form_name . '_email_field', $html, $atts); |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * @param array $atts |
| 274 | * |
| 275 | * @return string |
| 276 | */ |
| 277 | public function confirm_email($atts) |
| 278 | { |
| 279 | if (empty($atts)) $atts = []; |
| 280 | |
| 281 | $atts = $atts + ['placeholder' => esc_html__('Confirm Email Address', 'wp-user-avatar')]; |
| 282 | |
| 283 | $attributes = $this->field_attributes($this->tag_name . '_email2', $this->valid_field_atts(ppress_normalize_attributes($atts)), true); |
| 284 | |
| 285 | $html = "<input name='" . $this->tag_name . "_email2' type='text' $attributes>"; |
| 286 | |
| 287 | return apply_filters('ppress_' . $this->form_name . '_confirm_email_field', $html, $atts); |
| 288 | |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * @param $atts |
| 293 | * |
| 294 | * @return string |
| 295 | */ |
| 296 | public function website($atts) |
| 297 | { |
| 298 | if (empty($atts)) $atts = []; |
| 299 | |
| 300 | $atts = $atts + ['placeholder' => esc_html__('Website', 'wp-user-avatar')]; |
| 301 | |
| 302 | if ($this->form_type == FormRepository::EDIT_PROFILE_TYPE) { |
| 303 | // default username saved in DB |
| 304 | $atts['value'] = $this->current_user->user_url ?? ''; |
| 305 | } |
| 306 | |
| 307 | $field_name = $this->tag_name . '_website'; |
| 308 | |
| 309 | $attributes = $this->field_attributes($field_name, $this->valid_field_atts(ppress_normalize_attributes($atts))); |
| 310 | |
| 311 | $html = "<input name='$field_name' type='text' $attributes>"; |
| 312 | |
| 313 | if ($this->form_type == FormRepository::REGISTRATION_TYPE && $this->is_field_required($atts)) { |
| 314 | $value = apply_filters('ppress_website_required_field', esc_html__('Website', 'wp-user-avatar')); |
| 315 | $html .= "<input name='required-fields[$field_name]' type='hidden' value='$value'>"; |
| 316 | } |
| 317 | |
| 318 | return apply_filters('ppress_' . $this->form_name . '_website_field', $html, $atts); |
| 319 | } |
| 320 | |
| 321 | |
| 322 | /** |
| 323 | * Callback function for nickname |
| 324 | * |
| 325 | * @param $atts |
| 326 | * |
| 327 | * @return string |
| 328 | */ |
| 329 | public function nickname($atts) |
| 330 | { |
| 331 | if (empty($atts)) $atts = []; |
| 332 | |
| 333 | $atts = $atts + ['placeholder' => esc_html__('Nickname', 'wp-user-avatar')]; |
| 334 | |
| 335 | if ($this->form_type == FormRepository::EDIT_PROFILE_TYPE) { |
| 336 | // default username saved in DB |
| 337 | $atts['value'] = esc_attr($this->current_user->nickname); |
| 338 | } |
| 339 | |
| 340 | $field_name = $this->tag_name . '_nickname'; |
| 341 | |
| 342 | $attributes = $this->field_attributes($field_name, $this->valid_field_atts(ppress_normalize_attributes($atts))); |
| 343 | |
| 344 | $html = "<input name='$field_name' type='text' $attributes>"; |
| 345 | |
| 346 | if ($this->form_type == FormRepository::REGISTRATION_TYPE && $this->is_field_required($atts)) { |
| 347 | $value = apply_filters('ppress_nickname_required_field', esc_html__('Nickname', 'wp-user-avatar')); |
| 348 | $html .= "<input name='required-fields[$field_name]' type='hidden' value='$value'>"; |
| 349 | } |
| 350 | |
| 351 | return apply_filters('ppress_' . $this->form_name . '_nickname_field', $html, $atts); |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * Callback function for nickname |
| 356 | * |
| 357 | * @param $atts |
| 358 | * |
| 359 | * @return string |
| 360 | */ |
| 361 | public function display_name($atts) |
| 362 | { |
| 363 | if (empty($atts)) $atts = []; |
| 364 | |
| 365 | $atts = $atts + ['placeholder' => esc_html__('Display Name', 'wp-user-avatar')]; |
| 366 | |
| 367 | if ($this->form_type == FormRepository::EDIT_PROFILE_TYPE) { |
| 368 | // default username saved in DB |
| 369 | $atts['value'] = esc_attr($this->current_user->display_name); |
| 370 | } |
| 371 | |
| 372 | $field_name = $this->tag_name . '_display_name'; |
| 373 | |
| 374 | $attributes = $this->field_attributes($field_name, $this->valid_field_atts(ppress_normalize_attributes($atts))); |
| 375 | |
| 376 | $html = "<input name='$field_name' type='text' $attributes>"; |
| 377 | |
| 378 | if ($this->form_type == FormRepository::REGISTRATION_TYPE && $this->is_field_required($atts)) { |
| 379 | $value = apply_filters('ppress_display_name_required_field', esc_html__('Display name', 'wp-user-avatar')); |
| 380 | $html .= "<input name='required-fields[$field_name]' type='hidden' value='$value'>"; |
| 381 | } |
| 382 | |
| 383 | return apply_filters('ppress_' . $this->form_name . '_display_name_field', $html, $atts); |
| 384 | } |
| 385 | |
| 386 | /** |
| 387 | * Callback function for first name |
| 388 | * |
| 389 | * @param $atts |
| 390 | * |
| 391 | * @return string |
| 392 | */ |
| 393 | public function first_name($atts) |
| 394 | { |
| 395 | if (empty($atts)) $atts = []; |
| 396 | |
| 397 | $atts = $atts + ['placeholder' => esc_html__('First Name', 'wp-user-avatar')]; |
| 398 | |
| 399 | if ($this->form_type == FormRepository::EDIT_PROFILE_TYPE) { |
| 400 | // default username saved in DB |
| 401 | $atts['value'] = isset($this->current_user->first_name) ? esc_attr($this->current_user->first_name) : ''; |
| 402 | } |
| 403 | |
| 404 | $field_name = $this->tag_name . '_first_name'; |
| 405 | |
| 406 | $attributes = $this->field_attributes($field_name, $this->valid_field_atts(ppress_normalize_attributes($atts))); |
| 407 | |
| 408 | $html = "<input name='$field_name' type='text' $attributes>"; |
| 409 | |
| 410 | if ($this->form_type == FormRepository::REGISTRATION_TYPE && $this->is_field_required($atts)) { |
| 411 | $value = apply_filters('ppress_first_name_required_field', esc_html__('First name', 'wp-user-avatar')); |
| 412 | $html .= "<input name='required-fields[$field_name]' type='hidden' value='$value'>"; |
| 413 | } |
| 414 | |
| 415 | return apply_filters('ppress_' . $this->form_name . '_first_name_field', $html, $atts); |
| 416 | } |
| 417 | |
| 418 | |
| 419 | /** |
| 420 | * Callback for last name |
| 421 | * |
| 422 | * @param $atts |
| 423 | * |
| 424 | * @return string |
| 425 | */ |
| 426 | public function last_name($atts) |
| 427 | { |
| 428 | if (empty($atts)) $atts = []; |
| 429 | |
| 430 | $atts = $atts + ['placeholder' => esc_html__('Last Name', 'wp-user-avatar')]; |
| 431 | |
| 432 | if ($this->form_type == FormRepository::EDIT_PROFILE_TYPE) { |
| 433 | // default username saved in DB |
| 434 | $atts['value'] = isset($this->current_user->last_name) ? esc_attr($this->current_user->last_name) : ''; |
| 435 | } |
| 436 | |
| 437 | $field_name = $this->tag_name . '_last_name'; |
| 438 | |
| 439 | $attributes = $this->field_attributes($field_name, $this->valid_field_atts(ppress_normalize_attributes($atts))); |
| 440 | |
| 441 | $html = "<input name='$field_name' type='text' $attributes>"; |
| 442 | |
| 443 | if ($this->form_type == FormRepository::REGISTRATION_TYPE && $this->is_field_required($atts)) { |
| 444 | $value = apply_filters('ppress_last_name_required_field', esc_html__('Last name', 'wp-user-avatar')); |
| 445 | $html .= "<input name='required-fields[$field_name]' type='hidden' value='$value'>"; |
| 446 | } |
| 447 | |
| 448 | return apply_filters('ppress_' . $this->form_name . '_last_name_field', $html, $atts); |
| 449 | } |
| 450 | |
| 451 | /** |
| 452 | * @param $atts |
| 453 | * |
| 454 | * @return string |
| 455 | */ |
| 456 | public function bio($atts) |
| 457 | { |
| 458 | if (empty($atts)) $atts = []; |
| 459 | |
| 460 | $atts = $atts + ['placeholder' => esc_html__('Biographical Info', 'wp-user-avatar')]; |
| 461 | |
| 462 | if ($this->form_type == FormRepository::EDIT_PROFILE_TYPE) { |
| 463 | // default username saved in DB |
| 464 | $atts['value'] = $this->current_user->description ?? ''; |
| 465 | } |
| 466 | |
| 467 | $field_name = $this->tag_name . '_bio'; |
| 468 | |
| 469 | $attributes = $this->field_attributes('ignore_value', $this->valid_field_atts(ppress_normalize_attributes($atts))); |
| 470 | |
| 471 | $value = isset($_POST[$field_name]) ? wp_kses_post($_POST[$field_name]) : wp_kses_post(ppress_var($atts, 'value', '')); |
| 472 | |
| 473 | $html = "<textarea name=\"$field_name\" $attributes>$value</textarea>"; |
| 474 | |
| 475 | if ($this->form_type == FormRepository::REGISTRATION_TYPE && $this->is_field_required($atts)) { |
| 476 | $value = apply_filters('ppress_bio_required_field', esc_html__('Bio description', 'wp-user-avatar')); |
| 477 | $html .= "<input name='required-fields[$field_name]' type='hidden' value='$value'>"; |
| 478 | } |
| 479 | |
| 480 | return apply_filters('ppress_' . $this->form_name . '_bio_field', $html, $atts); |
| 481 | } |
| 482 | |
| 483 | /** |
| 484 | * Upload avatar field |
| 485 | */ |
| 486 | public function avatar($atts) |
| 487 | { |
| 488 | if (empty($atts)) $atts = []; |
| 489 | |
| 490 | $field_name = $this->tag_name . '_avatar'; |
| 491 | |
| 492 | $attributes = $this->field_attributes($field_name, $this->valid_field_atts(ppress_normalize_attributes($atts))); |
| 493 | |
| 494 | $html = "<input name='$field_name' type='file' $attributes>"; |
| 495 | |
| 496 | if ($this->form_type == FormRepository::REGISTRATION_TYPE && $this->is_field_required($atts)) { |
| 497 | $value = apply_filters('ppress_avatar_required_field', esc_html__('Profile picture', 'wp-user-avatar')); |
| 498 | $html .= "<input name='required-fields[$field_name]' type='hidden' value='$value'>"; |
| 499 | } |
| 500 | |
| 501 | return apply_filters('ppress_' . $this->form_name . '_avatar_field', $html, $atts); |
| 502 | } |
| 503 | |
| 504 | /** |
| 505 | * Upload cover photo field |
| 506 | */ |
| 507 | public function cover_image($atts) |
| 508 | { |
| 509 | if (empty($atts)) $atts = []; |
| 510 | |
| 511 | $field_name = $this->tag_name . '_cover_image'; |
| 512 | |
| 513 | $attributes = $this->field_attributes($field_name, $this->valid_field_atts(ppress_normalize_attributes($atts))); |
| 514 | |
| 515 | $html = "<input name='$field_name' type='file' $attributes>"; |
| 516 | |
| 517 | if ($this->form_type == FormRepository::REGISTRATION_TYPE && $this->is_field_required($atts)) { |
| 518 | $value = apply_filters('ppress_cover_image_required_field', esc_html__('Cover photo', 'wp-user-avatar')); |
| 519 | $html .= "<input name='required-fields[$field_name]' type='hidden' value='$value'>"; |
| 520 | } |
| 521 | |
| 522 | return apply_filters('ppress_' . $this->form_name . '_cover_image_field', $html, $atts); |
| 523 | } |
| 524 | |
| 525 | /** |
| 526 | * @param $atts |
| 527 | * |
| 528 | * @return string |
| 529 | */ |
| 530 | public function textbox_field($atts) |
| 531 | { |
| 532 | if (empty($atts)) $atts = []; |
| 533 | |
| 534 | $atts = array_replace(['type' => 'text'], $atts); |
| 535 | |
| 536 | if (empty($atts['key'])) { |
| 537 | return esc_html__('Field key is missing', 'wp-user-avatar'); |
| 538 | } |
| 539 | |
| 540 | $key = ppress_sanitize_key($atts['key']); |
| 541 | |
| 542 | $type = sanitize_text_field($atts['type']); |
| 543 | |
| 544 | if ($this->form_type == FormRepository::EDIT_PROFILE_TYPE) { |
| 545 | $db_data = isset($atts['value']) ? sanitize_text_field($atts['value']) : ($this->current_user->$key ?? ''); |
| 546 | $atts['value'] = isset($_POST[$key]) ? esc_attr($_POST[$key]) : $db_data; |
| 547 | } |
| 548 | |
| 549 | $attributes = $this->field_attributes($key, $this->valid_field_atts(ppress_normalize_attributes($atts))); |
| 550 | |
| 551 | $html = sprintf('<input name="%s" type="%s" %s>', esc_attr($key), esc_attr($type), $attributes); |
| 552 | |
| 553 | if ($this->form_type == FormRepository::REGISTRATION_TYPE && $this->is_field_required($atts)) { |
| 554 | $value = apply_filters('ppress_custom_required_field', $this->human_readable_field_key($key), $key); |
| 555 | $html .= "<input name='required-fields[$key]' type='hidden' value='$value'>"; |
| 556 | } |
| 557 | |
| 558 | return $html; |
| 559 | } |
| 560 | |
| 561 | public function number_field($atts) |
| 562 | { |
| 563 | if (empty($atts)) $atts = []; |
| 564 | |
| 565 | $atts['type'] = 'number'; |
| 566 | |
| 567 | return $this->textbox_field($atts); |
| 568 | } |
| 569 | |
| 570 | /** |
| 571 | * @param $atts |
| 572 | * |
| 573 | * @return string |
| 574 | */ |
| 575 | public function cf_password_field($atts) |
| 576 | { |
| 577 | if (empty($atts)) $atts = []; |
| 578 | |
| 579 | $atts['type'] = 'password'; |
| 580 | |
| 581 | return $this->textbox_field($atts); |
| 582 | } |
| 583 | |
| 584 | /** |
| 585 | * @param $atts |
| 586 | * |
| 587 | * @return string |
| 588 | */ |
| 589 | public function country_field($atts) |
| 590 | { |
| 591 | if (empty($atts)) $atts = []; |
| 592 | |
| 593 | if (empty($atts['key'])) { |
| 594 | return esc_html__('Field key is missing', 'wp-user-avatar'); |
| 595 | } |
| 596 | |
| 597 | $key = ppress_sanitize_key($atts['key']); |
| 598 | |
| 599 | $value = isset($_POST[$key]) ? sanitize_text_field($_POST[$key]) : sanitize_text_field($atts['value'] ?? ''); |
| 600 | |
| 601 | if ($this->form_type == FormRepository::EDIT_PROFILE_TYPE) { |
| 602 | $db_data = isset($atts['value']) ? sanitize_text_field($atts['value']) : ($this->current_user->$key ?? ''); |
| 603 | $value = isset($_POST[$key]) ? sanitize_text_field($_POST[$key]) : $db_data; |
| 604 | } |
| 605 | |
| 606 | $countries = ppress_array_of_world_countries(); |
| 607 | |
| 608 | $attributes = $this->field_attributes('ignore_value', $this->valid_field_atts($atts)); |
| 609 | |
| 610 | $html = "<select name='$key' $attributes>"; |
| 611 | $html .= '<option value="">' . esc_html__('Select country', 'wp-user-avatar') . '…</option>'; |
| 612 | |
| 613 | foreach ($countries as $ckey => $cvalue) { |
| 614 | $html .= '<option value="' . esc_attr($ckey) . '" ' . selected($value, $ckey, false) . '>' . $cvalue . '</option>'; |
| 615 | } |
| 616 | |
| 617 | $html .= '</select>'; |
| 618 | |
| 619 | return $html; |
| 620 | } |
| 621 | |
| 622 | public static function hasTime($string) |
| 623 | { |
| 624 | $timeStrings = ['H', 'h', 'G', 'i', 'S', 's', 'K']; |
| 625 | foreach ($timeStrings as $timeString) { |
| 626 | if (strpos($string, $timeString) != false) { |
| 627 | return true; |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | return false; |
| 632 | } |
| 633 | |
| 634 | public static function hasDate($string) |
| 635 | { |
| 636 | $dateStrings = ['d', 'D', 'l', 'j', 'J', 'w', 'W', 'F', 'm', 'n', 'M', 'U', 'Y', 'y', 'Z']; |
| 637 | foreach ($dateStrings as $dateString) { |
| 638 | if (strpos($string, $dateString) != false) { |
| 639 | return 'true'; |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | return false; |
| 644 | } |
| 645 | |
| 646 | public static function date_picker_config($field_key, $dateFormat = '') |
| 647 | { |
| 648 | if (empty($dateFormat)) { |
| 649 | |
| 650 | $dateFormat = ppress_var( |
| 651 | PROFILEPRESS_sql::get_profile_custom_field_by_key($field_key), |
| 652 | 'options', |
| 653 | 'Y-m-d', |
| 654 | true |
| 655 | ); |
| 656 | } |
| 657 | |
| 658 | $hasTime = self::hasTime($dateFormat); |
| 659 | $time24 = false; |
| 660 | |
| 661 | if ($hasTime && strpos($dateFormat, 'H') !== false) { |
| 662 | $time24 = true; |
| 663 | } |
| 664 | |
| 665 | return apply_filters('ppress_frontend_flatpickr_date_config', [ |
| 666 | 'allowInput' => true, |
| 667 | 'dateFormat' => $dateFormat, |
| 668 | 'enableTime' => $hasTime, |
| 669 | 'noCalendar' => ! self::hasDate($dateFormat), |
| 670 | 'disableMobile' => true, |
| 671 | 'time_24hr' => $time24, |
| 672 | 'locale' => ['firstDayOfWeek' => absint(get_option('start_of_week', 1))] |
| 673 | ]); |
| 674 | } |
| 675 | |
| 676 | /** |
| 677 | * @param $atts |
| 678 | * |
| 679 | * @return string |
| 680 | */ |
| 681 | public function date_field($atts) |
| 682 | { |
| 683 | if (empty($atts)) $atts = []; |
| 684 | |
| 685 | if (empty($atts['key'])) return esc_html__('Field key is missing', 'wp-user-avatar'); |
| 686 | |
| 687 | $key = ppress_sanitize_key($atts['key']); |
| 688 | |
| 689 | $atts['class'] = "pp_datepicker $key " . esc_attr(ppress_var($atts, 'class', '')); |
| 690 | |
| 691 | |
| 692 | if ($this->form_type == FormRepository::EDIT_PROFILE_TYPE) { |
| 693 | $db_data = isset($atts['value']) ? esc_attr($atts['value']) : ($this->current_user->$key ?? ''); |
| 694 | $atts['value'] = isset($_POST[$key]) ? esc_attr($_POST[$key]) : $db_data; |
| 695 | } |
| 696 | |
| 697 | $attributes = $this->field_attributes($key, $this->valid_field_atts($atts)); |
| 698 | |
| 699 | $html = "<input name='" . $key . "' type='text' $attributes>"; |
| 700 | |
| 701 | if ($this->form_type == FormRepository::REGISTRATION_TYPE && $this->is_field_required($atts)) { |
| 702 | $value = apply_filters('ppress_custom_required_field', $this->human_readable_field_key($key), $key); |
| 703 | $html .= "<input name='required-fields[$key]' type='hidden' value='$value'>"; |
| 704 | } |
| 705 | |
| 706 | $dateFormat = ! empty($atts['date_format']) ? $atts['date_format'] : 'Y-m-d'; |
| 707 | // defined fields in custom fields settings page has date format saved in options $atts |
| 708 | if ( ! empty($atts['options'])) { |
| 709 | $dateFormat = $atts['options']; |
| 710 | } |
| 711 | |
| 712 | $config = self::date_picker_config($key, $dateFormat); |
| 713 | |
| 714 | $html .= sprintf( |
| 715 | '<script type="text/javascript">jQuery(function() {jQuery( ".pp_datepicker.%s" ).flatpickr(%s);});</script>', |
| 716 | $key, json_encode($config) |
| 717 | ); |
| 718 | |
| 719 | return $html; |
| 720 | } |
| 721 | |
| 722 | /** |
| 723 | * @param $atts |
| 724 | * |
| 725 | * @return string |
| 726 | */ |
| 727 | public function textarea_field($atts) |
| 728 | { |
| 729 | if (empty($atts)) $atts = []; |
| 730 | |
| 731 | if (empty($atts['key'])) return esc_html__('Field key is missing', 'wp-user-avatar'); |
| 732 | |
| 733 | $key = ppress_sanitize_key($atts['key']); |
| 734 | |
| 735 | $value = isset($_POST[$key]) ? esc_textarea($_POST[$key]) : esc_textarea($atts['value'] ?? ''); |
| 736 | |
| 737 | if ($this->form_type == FormRepository::EDIT_PROFILE_TYPE) { |
| 738 | $db_data = isset($atts['value']) ? esc_textarea($atts['value']) : ($this->current_user->$key ?? ''); |
| 739 | $value = isset($_POST[$key]) ? esc_textarea($_POST[$key]) : $db_data; |
| 740 | } |
| 741 | |
| 742 | $attributes = $this->field_attributes($key, $this->valid_field_atts(ppress_normalize_attributes($atts))); |
| 743 | |
| 744 | $html = "<textarea name=\"$key\" $attributes>$value</textarea>"; |
| 745 | |
| 746 | if ($this->form_type == FormRepository::REGISTRATION_TYPE && $this->is_field_required($atts)) { |
| 747 | $value = apply_filters('ppress_custom_required_field', $this->human_readable_field_key($key), $key); |
| 748 | $html .= "<input name='required-fields[$key]' type='hidden' value='$value'>"; |
| 749 | } |
| 750 | |
| 751 | return $html; |
| 752 | } |
| 753 | |
| 754 | public function select_dropdown_field($atts) |
| 755 | { |
| 756 | if (empty($atts)) $atts = []; |
| 757 | |
| 758 | if (empty($atts['key'])) return esc_html__('Field key is missing', 'wp-user-avatar'); |
| 759 | |
| 760 | $key = ppress_sanitize_key($atts['key']); |
| 761 | |
| 762 | if (empty($atts['options']) && empty($atts['key_value_options'])) return esc_html__('No dropdown option found.', 'wp-user-avatar'); |
| 763 | |
| 764 | $is_multiple = isset($atts['is_multiple']) && $atts['is_multiple'] == '1' ? 'multiple' : ''; |
| 765 | $select2_class_name = $is_multiple == 'multiple' ? 'ppress-select2 ' : ''; |
| 766 | $data_placeholder_attr = $is_multiple == 'multiple' ? ' data-placeholder="' . esc_attr(ppress_var($atts, 'placeholder')) . '"' : ''; |
| 767 | |
| 768 | $atts['class'] = $select2_class_name . esc_attr(ppress_var($atts, 'class')); |
| 769 | |
| 770 | $attributes = $this->field_attributes('ignore_value', $this->valid_field_atts(ppress_normalize_attributes($atts))); |
| 771 | |
| 772 | $select_tag_key = $is_multiple == 'multiple' ? "{$key}[]" : $key; |
| 773 | $html = "<input type='hidden' name=\"$select_tag_key\" value=''>"; |
| 774 | $html .= "<select name=\"$select_tag_key\"$data_placeholder_attr $attributes $is_multiple>"; |
| 775 | |
| 776 | $option_values = is_array($atts['options']) ? implode(',', $atts['options']) : $atts['options']; |
| 777 | |
| 778 | if ( ! empty($option_values)) { |
| 779 | $option_values = explode(',', $option_values); |
| 780 | $option_values = array_combine($option_values, $option_values); |
| 781 | } |
| 782 | |
| 783 | if ( ! empty($atts['key_value_options'])) { |
| 784 | $option_values = is_string($atts['key_value_options']) ? unserialize(base64_decode($atts['key_value_options']), ['allowed_classes' => false]) : $atts['key_value_options']; |
| 785 | } |
| 786 | |
| 787 | if ( ! empty($option_values)) { |
| 788 | |
| 789 | $_POST = $this->GET_POST(); |
| 790 | |
| 791 | $html .= '<option value="">———</option>'; |
| 792 | foreach ($option_values as $option_value => $value) { |
| 793 | |
| 794 | $option_value = is_string($option_value) ? trim($option_value) : ''; |
| 795 | $value = is_string($value) ? trim($value) : ''; |
| 796 | |
| 797 | if (isset($_POST[$key]) && is_array($_POST[$key]) && in_array($option_value, $_POST[$key])) { |
| 798 | $selected = 'selected="selected"'; |
| 799 | } else { |
| 800 | $selected = selected( |
| 801 | $_POST[$key] ?? '', |
| 802 | $option_value, |
| 803 | false |
| 804 | ); |
| 805 | } |
| 806 | |
| 807 | $db_data = $atts['value'] ?? (isset($this->current_user->$key) ? $this->current_user->$key : ''); |
| 808 | |
| 809 | if ($this->form_type == FormRepository::EDIT_PROFILE_TYPE) { |
| 810 | $selected = ''; |
| 811 | if (isset($_POST[$key]) && is_array($_POST[$key]) && in_array($option_value, $_POST[$key])) $selected = 'selected="selected"'; |
| 812 | // !isset($_POST[ $key ] is called to not run the succeeding code if the form is submitted. |
| 813 | // to enable the select dropdown retain the submitted options when an error occur/ prevent the form from saving. |
| 814 | elseif ( ! isset($_POST[$key]) && isset($db_data) && is_array($db_data) && in_array($option_value, $db_data)) { |
| 815 | $selected = 'selected="selected"'; |
| 816 | } elseif ( ! isset($_POST[$key]) && isset($db_data) && ! is_array($db_data) && $option_value == $db_data) { |
| 817 | $selected = 'selected="selected"'; |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | $option_value = esc_attr($option_value); |
| 822 | $value = esc_attr($value); |
| 823 | |
| 824 | $html .= "<option value=\"$option_value\" $selected>$value</option>"; |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | $html .= '</select>'; |
| 829 | // if field is required, add an hidden field |
| 830 | if ($this->form_type == FormRepository::REGISTRATION_TYPE && $this->is_field_required($atts)) { |
| 831 | $value = apply_filters('ppress_custom_required_field', $this->human_readable_field_key($key), $key); |
| 832 | $html .= "<input name='required-fields[$key]' type='hidden' value='$value'>"; |
| 833 | } |
| 834 | |
| 835 | if ($is_multiple) { |
| 836 | $limit = absint($atts['limit'] ?? 0); |
| 837 | $html .= $this->select2_js_script($key, $limit); |
| 838 | } |
| 839 | |
| 840 | return $html; |
| 841 | } |
| 842 | |
| 843 | public function radio_buttons_field($atts) |
| 844 | { |
| 845 | if (empty($atts)) $atts = []; |
| 846 | |
| 847 | if (empty($atts['key'])) { |
| 848 | return esc_html__('Field key is missing', 'wp-user-avatar'); |
| 849 | } |
| 850 | |
| 851 | $key = ppress_sanitize_key($atts['key']); |
| 852 | |
| 853 | if ( ! isset($atts['options']) || empty($atts['options'])) { |
| 854 | return esc_html__('No radio choice found.', 'wp-user-avatar'); |
| 855 | } |
| 856 | |
| 857 | $attributes = $this->field_attributes('ignore_value', $this->valid_field_atts(ppress_normalize_attributes($atts))); |
| 858 | |
| 859 | $option_values = explode(',', $atts['options']); |
| 860 | |
| 861 | $_POST = $this->GET_POST(); |
| 862 | |
| 863 | $html = '<div class="pp-radios-container">'; |
| 864 | |
| 865 | foreach ($option_values as $value) { |
| 866 | $value = esc_attr(trim($value)); |
| 867 | |
| 868 | $checked = @checked($_POST[$key], $value, false); |
| 869 | |
| 870 | if ($this->form_type == FormRepository::EDIT_PROFILE_TYPE) { |
| 871 | $db_data = isset($atts['value']) ? esc_attr($atts['value']) : ($this->current_user->$key ?? ''); |
| 872 | $checked = @checked( |
| 873 | isset($_POST[$key]) && ! empty($_POST[$key]) ? $_POST[$key] : $db_data, |
| 874 | $value, |
| 875 | false |
| 876 | ); |
| 877 | } |
| 878 | |
| 879 | $backward_compat_class = ''; |
| 880 | if ($this->form_type == FormRepository::REGISTRATION_TYPE) { |
| 881 | $backward_compat_class = ' profilepress-reg-label'; |
| 882 | } |
| 883 | |
| 884 | $html .= '<div class="pp-radio-wrap">'; |
| 885 | $html .= "<input type='radio' name=\"$key\" value=\"$value\" id=\"$value\" $checked $attributes>"; |
| 886 | $html .= "<label class=\"pp-form-label{$backward_compat_class}\" for=\"$value\">$value</label>"; |
| 887 | $html .= '</div>'; |
| 888 | } |
| 889 | |
| 890 | $html .= '</div>'; |
| 891 | |
| 892 | if ($this->form_type == FormRepository::REGISTRATION_TYPE && $this->is_field_required($atts)) { |
| 893 | $value = apply_filters('ppress_custom_required_field', $this->human_readable_field_key($key), $key); |
| 894 | $html .= "<input name='required-fields[$key]' type='hidden' value='$value'>"; |
| 895 | } |
| 896 | |
| 897 | return $html; |
| 898 | } |
| 899 | |
| 900 | public function checkbox_list_field($atts) |
| 901 | { |
| 902 | if (empty($atts)) $atts = []; |
| 903 | |
| 904 | if (empty($atts['key'])) return esc_html__('Field key is missing', 'wp-user-avatar'); |
| 905 | |
| 906 | $key = ppress_sanitize_key($atts['key']); |
| 907 | |
| 908 | if ( ! isset($atts['options']) || empty($atts['options'])) { |
| 909 | return esc_html__('No checkbox choice found.', 'wp-user-avatar'); |
| 910 | } |
| 911 | |
| 912 | $atts['required'] = 'false'; |
| 913 | |
| 914 | $attributes = $this->field_attributes('ignore_value', $this->valid_field_atts(ppress_normalize_attributes($atts))); |
| 915 | |
| 916 | $checkbox_tag_key = "{$key}[]"; |
| 917 | |
| 918 | $option_values = explode(',', $atts['options']); |
| 919 | |
| 920 | $html = '<div class="pp-checkboxes-container">'; |
| 921 | |
| 922 | $_POST = $this->GET_POST(); |
| 923 | |
| 924 | foreach ($option_values as $value) { |
| 925 | |
| 926 | $value = esc_attr(trim($value)); |
| 927 | |
| 928 | $checked = isset($_POST[$key]) && is_array($_POST[$key]) && in_array($value, $_POST[$key]) ? 'checked="checked"' : checked($_POST[$key] ?? '', $value, false); |
| 929 | |
| 930 | if ($this->form_type == FormRepository::EDIT_PROFILE_TYPE) { |
| 931 | $checked = ''; |
| 932 | if (isset($_POST[$key]) && is_array($_POST[$key]) && in_array($value, $_POST[$key])) { |
| 933 | $checked = 'checked="checked"'; |
| 934 | } elseif ( ! isset($_POST[$key]) && isset($this->current_user->$key) && is_array($this->current_user->$key) && in_array($value, $this->current_user->$key)) { |
| 935 | $checked = 'checked="checked"'; |
| 936 | } elseif ( ! isset($_POST[$key]) && isset($this->current_user->$key) && ! is_array($this->current_user->$key) && $value == $this->current_user->$key) { |
| 937 | $checked = 'checked="checked"'; |
| 938 | } |
| 939 | } |
| 940 | |
| 941 | $html .= '<div class="pp-checkbox-wrap pp-multi-checkbox">'; |
| 942 | $html .= "<input type='checkbox' name=\"$checkbox_tag_key\" value=\"$value\" id=\"$value\" $attributes $checked>"; |
| 943 | $html .= "<label class='pp-form-label' for=\"$value\">$value</label>"; |
| 944 | $html .= '</div>'; |
| 945 | } |
| 946 | |
| 947 | $html .= '</div>'; |
| 948 | |
| 949 | if ($this->form_type == FormRepository::REGISTRATION_TYPE && $this->is_field_required($atts)) { |
| 950 | $value = apply_filters('ppress_custom_required_field', $this->human_readable_field_key($key), $key); |
| 951 | $html .= "<input name='required-fields[$key]' type='hidden' value='$value'>"; |
| 952 | } |
| 953 | |
| 954 | return $html; |
| 955 | } |
| 956 | |
| 957 | public function single_checkbox_field($atts) |
| 958 | { |
| 959 | if (empty($atts)) $atts = []; |
| 960 | |
| 961 | $_POST = $this->GET_POST(); |
| 962 | |
| 963 | unset($atts['placeholder']); |
| 964 | |
| 965 | $attributes = $this->field_attributes('ignore_value', $this->valid_field_atts(ppress_normalize_attributes($atts))); |
| 966 | |
| 967 | if (empty($atts['key'])) return esc_html__('Field key is missing', 'wp-user-avatar'); |
| 968 | |
| 969 | $key = ppress_sanitize_key($atts['key']); |
| 970 | |
| 971 | $html = '<div class="pp-checkbox-wrap pp-single-checkbox">'; |
| 972 | $field_label = isset($atts['checkbox_text']) ? wp_kses_post(html_entity_decode($atts['checkbox_text'])) : ''; |
| 973 | |
| 974 | // remove all onXYZ attributes |
| 975 | $field_label = preg_replace('/(on[\S]+=)/', '', $field_label); |
| 976 | |
| 977 | // checked for checkbox |
| 978 | $checked = checked(ppressPOST_var($key, ppress_var($atts, 'checked_state')), 'true', false); |
| 979 | |
| 980 | if ($this->form_type == FormRepository::EDIT_PROFILE_TYPE) { |
| 981 | $db_data = isset($atts['value']) ? sanitize_text_field($atts['value']) : ($this->current_user->$key ?? ''); |
| 982 | $db_data = ('1' == $db_data) ? 'true' : $db_data; |
| 983 | |
| 984 | $checked = checked( |
| 985 | ! empty($_POST[$key]) ? $_POST[$key] : $db_data, |
| 986 | 'true', |
| 987 | false |
| 988 | ); |
| 989 | } |
| 990 | |
| 991 | $html .= "<input type='hidden' name=\"$key\" value=\"false\" style='display: none'>"; |
| 992 | $html .= "<input type='checkbox' name=\"$key\" value=\"true\" id=\"$key\" $checked $attributes>"; |
| 993 | $html .= "<label class='pp-form-label' for=\"$key\">$field_label</label>"; |
| 994 | $html .= '</div>'; |
| 995 | |
| 996 | if ($this->form_type == FormRepository::REGISTRATION_TYPE && $this->is_field_required($atts)) { |
| 997 | $value = apply_filters('ppress_custom_required_field', $this->human_readable_field_key($key), $key); |
| 998 | $html .= "<input name='required-fields[$key]' type='hidden' value='$value'>"; |
| 999 | } |
| 1000 | |
| 1001 | return $html; |
| 1002 | } |
| 1003 | |
| 1004 | /** |
| 1005 | * @param $atts |
| 1006 | * |
| 1007 | * @return string |
| 1008 | */ |
| 1009 | public function custom_profile_field($atts) |
| 1010 | { |
| 1011 | if (empty($atts)) $atts = []; |
| 1012 | |
| 1013 | $_POST = $this->GET_POST(); |
| 1014 | |
| 1015 | $atts = ppress_normalize_attributes($atts); |
| 1016 | |
| 1017 | $key = ppress_sanitize_key($atts['key']); |
| 1018 | |
| 1019 | if (empty($key)) return esc_html__('Field key is missing', 'wp-user-avatar'); |
| 1020 | |
| 1021 | $type = PROFILEPRESS_sql::get_field_type($key); |
| 1022 | |
| 1023 | $standard_billing_fields = CheckoutFields::standard_billing_fields(); |
| 1024 | |
| 1025 | if (in_array($key, array_keys($standard_billing_fields))) { |
| 1026 | |
| 1027 | $type = $standard_billing_fields[$key]['field_type']; |
| 1028 | |
| 1029 | if ($key == CheckoutFields::BILLING_STATE) { |
| 1030 | $country = get_user_meta($this->current_user->ID, CheckoutFields::BILLING_COUNTRY, true); |
| 1031 | if ( ! empty($atts['billing_country'])) { |
| 1032 | $country = sanitize_text_field($atts['billing_country']); |
| 1033 | } |
| 1034 | |
| 1035 | if ( ! empty($country) && ! empty($states = ppress_array_of_world_states($country))) { |
| 1036 | $type = 'select'; |
| 1037 | $atts['key_value_options'] = $states; |
| 1038 | } |
| 1039 | } |
| 1040 | } |
| 1041 | |
| 1042 | $type = ! empty($atts['type']) ? $atts['type'] : $type; |
| 1043 | |
| 1044 | if (empty($type)) $type = 'text'; |
| 1045 | |
| 1046 | $html = esc_html__('custom field not defined', 'wp-user-avatar'); |
| 1047 | |
| 1048 | if ($type == 'select') { |
| 1049 | $atts['options'] = $atts['options'] ?? PROFILEPRESS_sql::get_field_option_values($key); |
| 1050 | $atts['is_multiple'] = ppress_is_select_field_multi_selectable($key) ? '1' : ''; |
| 1051 | $html = $this->select_dropdown_field($atts); |
| 1052 | } |
| 1053 | |
| 1054 | if ($type == 'radio') { |
| 1055 | $atts['key'] = $key; |
| 1056 | $atts['options'] = PROFILEPRESS_sql::get_field_option_values($key); |
| 1057 | $html = $this->radio_buttons_field($atts); |
| 1058 | } |
| 1059 | |
| 1060 | if ($type == 'agreeable') { |
| 1061 | $atts['key'] = $key; |
| 1062 | $atts['checkbox_text'] = wp_kses_post(html_entity_decode(PROFILEPRESS_sql::get_field_label($key))); |
| 1063 | $html = $this->single_checkbox_field($atts); |
| 1064 | } |
| 1065 | |
| 1066 | if ($type == 'checkbox') { |
| 1067 | $atts['key'] = $key; |
| 1068 | $atts['options'] = PROFILEPRESS_sql::get_field_option_values($key); |
| 1069 | |
| 1070 | $html = $this->checkbox_list_field($atts); |
| 1071 | } |
| 1072 | |
| 1073 | if ($type == 'textarea') { |
| 1074 | $html = $this->textarea_field($atts); |
| 1075 | } |
| 1076 | |
| 1077 | if ($type == 'country') { |
| 1078 | $html = $this->country_field($atts); |
| 1079 | } |
| 1080 | |
| 1081 | if ($type == 'date') { |
| 1082 | $atts['options'] = PROFILEPRESS_sql::get_field_option_values($key); |
| 1083 | $html = $this->date_field($atts); |
| 1084 | } |
| 1085 | |
| 1086 | if ('file' == $type) { |
| 1087 | |
| 1088 | $attributes = $this->field_attributes($key, $this->valid_field_atts($atts)); |
| 1089 | |
| 1090 | $html = ''; |
| 1091 | |
| 1092 | if ('edit_profile' == $this->form_name) { |
| 1093 | |
| 1094 | $user_upload_data = get_user_meta($this->current_user->ID, 'pp_uploaded_files', true); |
| 1095 | // if the user uploads isn't empty and there exist a file with the custom field key. |
| 1096 | if ( ! empty($user_upload_data) && isset($user_upload_data[$key]) && ($filename = $user_upload_data[$key])) { |
| 1097 | $link = PPRESS_FILE_UPLOAD_URL . $filename; |
| 1098 | $html .= "<div class='ppress-user-upload'><a href='$link'>$filename</a></div>"; |
| 1099 | $attributes = str_replace('required="required"', '', $attributes); |
| 1100 | } |
| 1101 | |
| 1102 | $html = apply_filters('ppress_edit_profile_hide_file', $html); |
| 1103 | } |
| 1104 | |
| 1105 | $html .= "<input name='" . esc_attr($key) . "' type='file' $attributes>"; |
| 1106 | // if field is required, add an hidden field |
| 1107 | if ($this->form_type == FormRepository::REGISTRATION_TYPE && $this->is_field_required($atts)) { |
| 1108 | $html .= "<input name='required-" . esc_attr($key) . "' type='hidden' value='true' style='display:none'>"; |
| 1109 | } |
| 1110 | } |
| 1111 | |
| 1112 | if (in_array($type, ['text', 'password', 'email', 'tel', 'number', 'hidden'])) { |
| 1113 | $atts['type'] = $type; |
| 1114 | $html = $this->textbox_field($atts); |
| 1115 | } |
| 1116 | |
| 1117 | return apply_filters('ppress_' . $this->form_name . '_cpf_field', $html, $atts); |
| 1118 | } |
| 1119 | |
| 1120 | /** |
| 1121 | * Callback function for submit button |
| 1122 | * |
| 1123 | * @param $atts |
| 1124 | * |
| 1125 | * @return string |
| 1126 | */ |
| 1127 | public function submit($atts) |
| 1128 | { |
| 1129 | if (empty($atts)) $atts = []; |
| 1130 | |
| 1131 | $field_name = isset($atts['name']) ? esc_attr($atts['name']) : $this->tag_name . '_submit'; |
| 1132 | $value = esc_html__('Sign Up', 'wp-user-avatar'); |
| 1133 | |
| 1134 | if ($this->form_type == FormRepository::EDIT_PROFILE_TYPE) { |
| 1135 | $value = esc_html__('Save Changes', 'wp-user-avatar'); |
| 1136 | } |
| 1137 | |
| 1138 | $atts = apply_filters('ppress_' . $this->form_name . '_submit_field_atts', $this->valid_field_atts(ppress_normalize_attributes($atts))); |
| 1139 | $atts['value'] = isset($atts['value']) ? esc_attr($atts['value']) : $value; |
| 1140 | |
| 1141 | $form_type = $this->form_type; |
| 1142 | $form_id = $GLOBALS['pp_registration_form_id'] ?? 0; |
| 1143 | if ($form_type == FormRepository::EDIT_PROFILE_TYPE) { |
| 1144 | $form_id = isset($GLOBALS['pp_edit_profile_form_id']) ? esc_attr($GLOBALS['pp_edit_profile_form_id']) : 0; |
| 1145 | } |
| 1146 | |
| 1147 | if (isset($GLOBALS['pp_melange_form_id'])) { |
| 1148 | $form_id = $GLOBALS['pp_melange_form_id']; |
| 1149 | $form_type = FormRepository::MELANGE_TYPE; |
| 1150 | } |
| 1151 | |
| 1152 | $processing_label = ! empty($atts['processing_label']) ? esc_attr($atts['processing_label']) : FormRepository::get_processing_label($form_id, $form_type); |
| 1153 | |
| 1154 | $attributes = $this->field_attributes($field_name, $atts); |
| 1155 | |
| 1156 | $html = sprintf( |
| 1157 | '<input data-pp-submit-label="%1$s" data-pp-processing-label="%2$s" name="%3$s" type="submit" %4$s>', |
| 1158 | $atts['value'], |
| 1159 | esc_attr($processing_label), |
| 1160 | $field_name, |
| 1161 | $attributes |
| 1162 | ); |
| 1163 | |
| 1164 | $html .= ppress_nonce_field(); |
| 1165 | |
| 1166 | return apply_filters('ppress_' . $this->form_name . '_submit_field', $html, $atts); |
| 1167 | } |
| 1168 | |
| 1169 | public function select2_js_script($key, $limit = 0) |
| 1170 | { |
| 1171 | $limit = absint($limit); |
| 1172 | |
| 1173 | return <<<SCRIPT |
| 1174 | <script type='text/javascript'> |
| 1175 | jQuery(function() { |
| 1176 | var selector = jQuery('select[name^="$key"].ppress-select2'); |
| 1177 | selector.select2({width: '100%', maximumSelectionLength: $limit}); |
| 1178 | }); |
| 1179 | </script> |
| 1180 | SCRIPT; |
| 1181 | } |
| 1182 | |
| 1183 | /** |
| 1184 | * Remove a user avatar |
| 1185 | * |
| 1186 | * @param $atts |
| 1187 | * |
| 1188 | * @return string |
| 1189 | */ |
| 1190 | public function remove_user_avatar($atts) |
| 1191 | { |
| 1192 | if (empty($atts)) $atts = []; |
| 1193 | |
| 1194 | $other_atts_html = ppress_other_field_atts($atts); |
| 1195 | |
| 1196 | $atts = shortcode_atts([ |
| 1197 | 'class' => '', |
| 1198 | 'id' => '', |
| 1199 | 'title' => '', |
| 1200 | 'label' => esc_html__('Delete Avatar', 'wp-user-avatar'), |
| 1201 | ], $atts |
| 1202 | ); |
| 1203 | |
| 1204 | $atts = apply_filters('ppress_edit_profile_remove_avatar_button_atts', $atts); |
| 1205 | |
| 1206 | $class = 'class="pp-del-profile-avatar ' . esc_attr($atts['class']) . '"'; |
| 1207 | $label = ! empty($atts['label']) ? esc_attr($atts['label']) : null; |
| 1208 | $id = ! empty($atts['id']) ? 'id="' . esc_attr($atts['id']) . '"' : null; |
| 1209 | $title = 'title="' . esc_attr($atts['title']) . '"'; |
| 1210 | |
| 1211 | // ensure a profile avatar for the user is available before the remove button gets displayed |
| 1212 | if (UserAvatar::user_has_pp_avatar($this->current_user->ID)) { |
| 1213 | $button = "<button type=\"submit\" name=\"eup_remove_avatar\" value=\"removed\" $class $id $title $other_atts_html>$label</button>"; |
| 1214 | |
| 1215 | return apply_filters('ppress_edit_profile_remove_avatar_button', $button, $atts); |
| 1216 | } |
| 1217 | } |
| 1218 | |
| 1219 | /** |
| 1220 | * Remove a user cover photo |
| 1221 | * |
| 1222 | * @param $atts |
| 1223 | * |
| 1224 | * @return string |
| 1225 | */ |
| 1226 | public function remove_cover_image($atts) |
| 1227 | { |
| 1228 | if (empty($atts)) $atts = []; |
| 1229 | |
| 1230 | $other_atts_html = ppress_other_field_atts($atts); |
| 1231 | |
| 1232 | $atts = shortcode_atts([ |
| 1233 | 'class' => '', |
| 1234 | 'id' => '', |
| 1235 | 'title' => '', |
| 1236 | 'label' => esc_html__('Delete Cover Photo', 'wp-user-avatar'), |
| 1237 | ], $atts |
| 1238 | ); |
| 1239 | |
| 1240 | $atts = apply_filters('ppress_edit_profile_remove_cover_image_button_atts', $atts); |
| 1241 | |
| 1242 | $class = 'class="pp-del-cover-image ' . esc_attr($atts['class']) . '"'; |
| 1243 | $label = ! empty($atts['label']) ? esc_attr($atts['label']) : null; |
| 1244 | $id = ! empty($atts['id']) ? 'id="' . esc_attr($atts['id']) . '"' : null; |
| 1245 | $title = 'title="' . esc_attr($atts['title']) . '"'; |
| 1246 | |
| 1247 | if (ppress_user_has_cover_image($this->current_user->ID)) { |
| 1248 | $button = "<button type=\"submit\" name=\"eup_remove_cover_image\" value=\"removed\" $class $id $title $other_atts_html>$label</button>"; |
| 1249 | |
| 1250 | return apply_filters('ppress_edit_profile_remove_cover_image_button', $button, $atts); |
| 1251 | } |
| 1252 | } |
| 1253 | } |