EditProfile
5 years ago
Login
3 years ago
MemberDirectory
7 months ago
PasswordReset
5 years ago
Registration
3 years ago
UserProfile
1 year ago
AbstractBuildScratch.php
7 months ago
AbstractMemberDirectoryTheme.php
4 months ago
AbstractTheme.php
11 months ago
FieldListing.php
7 months ago
MemberDirectoryListing.php
2 months ago
MemberDirectoryTrait.php
1 year ago
ProfileFieldListing.php
2 months ago
ThemeInterface.php
5 years ago
ThemesRepository.php
1 year ago
index.php
5 years ago
FieldListing.php
286 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePress\Core\Themes\DragDrop; |
| 4 | |
| 5 | use ProfilePress\Core\Classes\ExtensionManager as EM; |
| 6 | use ProfilePress\Core\Classes\FormRepository as FR; |
| 7 | use ProfilePress\Core\Classes\UserAvatar; |
| 8 | use ProfilePress\Libsodium\Recaptcha\Recaptcha; |
| 9 | |
| 10 | class FieldListing |
| 11 | { |
| 12 | private $form_id; |
| 13 | private $form_type; |
| 14 | private $is_buildscratch; |
| 15 | |
| 16 | private $field_settings = []; |
| 17 | |
| 18 | private $defaults; |
| 19 | |
| 20 | private $shortcode_field_wrap_start = ''; |
| 21 | |
| 22 | private $shortcode_field_wrap_end = ''; |
| 23 | |
| 24 | public function __construct($form_id, $form_type, $is_buildscratch = false) |
| 25 | { |
| 26 | $this->form_id = $form_id; |
| 27 | $this->form_type = $form_type; |
| 28 | $this->is_buildscratch = $is_buildscratch; |
| 29 | |
| 30 | $field_settings = FR::get_form_meta($form_id, $form_type, FR::FORM_BUILDER_FIELDS_SETTINGS); |
| 31 | if ( ! empty($field_settings)) { |
| 32 | $this->field_settings = json_decode($field_settings, true); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | public function shortcode_field_wrap_start($wrapper = '') |
| 37 | { |
| 38 | $this->shortcode_field_wrap_start = $wrapper; |
| 39 | |
| 40 | return $this; |
| 41 | } |
| 42 | |
| 43 | public function shortcode_field_wrap_end($wrapper = '') |
| 44 | { |
| 45 | $this->shortcode_field_wrap_end = $wrapper; |
| 46 | |
| 47 | return $this; |
| 48 | } |
| 49 | |
| 50 | public function defaults($defaults) |
| 51 | { |
| 52 | $this->defaults = $defaults; |
| 53 | |
| 54 | return $this; |
| 55 | } |
| 56 | |
| 57 | protected function is_field_required($field_setting) |
| 58 | { |
| 59 | $field_type = $field_setting['fieldType']; |
| 60 | |
| 61 | // the str_replace sorcery checks if field type contains any of the strings https://stackoverflow.com/a/42311760/2648410 |
| 62 | // we are using edit-profile-password and reg-password because we have other password custom fields that ends with -password |
| 63 | if (str_replace(['-username', 'reg-password', 'login-password', 'edit-profile-password', '-email'], '', $field_type) != $field_type) return true; |
| 64 | |
| 65 | return isset($field_setting['required']) && ($field_setting['required'] === true || $field_setting['required'] == 'true' || $field_setting['required'] == '1'); |
| 66 | } |
| 67 | |
| 68 | protected function label_structure($field_setting, $field_id) |
| 69 | { |
| 70 | $field_type = $field_setting['fieldType']; |
| 71 | |
| 72 | if ( ! in_array($field_type, ['login-remember']) && strpos($field_type, 'agreeable') === false && ! empty($field_setting['label'])) { |
| 73 | $output = sprintf('<div class="pp-form-label-wrap"><label for="%s" class="pp-form-label">', $field_id); |
| 74 | $output .= wp_kses_post($field_setting['label']); |
| 75 | |
| 76 | if ($this->is_field_required($field_setting)) { |
| 77 | $output .= ' <span class="pp-form-required-label">*</span>'; |
| 78 | } |
| 79 | |
| 80 | if (isset($field_setting['description_appearance']) && $field_setting['description_appearance'] == 'tooltip' && ! empty($field_setting['description'])) { |
| 81 | $output .= sprintf( |
| 82 | ' <span class="ppress-hint-tooltip ppress-hint-wrap hint--top hint--medium hint--bounce" aria-label="%s"><i class="pp-form-material-icons">help</i></span>', |
| 83 | sanitize_text_field($field_setting['description']) |
| 84 | ); |
| 85 | } |
| 86 | |
| 87 | $output .= sprintf('</label></div>'); |
| 88 | |
| 89 | return $output; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | public function is_avatar($field_type) |
| 94 | { |
| 95 | return $field_type == 'pp-user-avatar'; |
| 96 | } |
| 97 | |
| 98 | public function is_cover_image($field_type) |
| 99 | { |
| 100 | return $field_type == 'pp-user-cover-image'; |
| 101 | } |
| 102 | |
| 103 | public function is_recaptcha($field_type) |
| 104 | { |
| 105 | return $field_type == 'pp-recaptcha'; |
| 106 | } |
| 107 | |
| 108 | public function is_cloudflare_turnstile($field_type) |
| 109 | { |
| 110 | return $field_type == 'pp-turnstile'; |
| 111 | } |
| 112 | |
| 113 | public function is_recaptcha_v3($field_type) |
| 114 | { |
| 115 | return $this->is_recaptcha($field_type) && class_exists('ProfilePress\Libsodium\Recaptcha\Recaptcha') && Recaptcha::$type == 'v3'; |
| 116 | } |
| 117 | |
| 118 | public function forge() |
| 119 | { |
| 120 | $output = ''; |
| 121 | |
| 122 | foreach ($this->field_settings as $field_setting) { |
| 123 | |
| 124 | $field_type = $field_setting['fieldType']; |
| 125 | |
| 126 | if ($this->is_recaptcha($field_type) && ! EM::is_enabled(EM::RECAPTCHA)) continue; |
| 127 | |
| 128 | if ($this->is_cloudflare_turnstile($field_type) && ! EM::is_enabled(EM::TURNSTILE)) continue; |
| 129 | |
| 130 | if (isset($this->defaults, $this->defaults[$field_type])) { |
| 131 | $field_setting = wp_parse_args($field_setting, $this->defaults[$field_type]); |
| 132 | } |
| 133 | |
| 134 | $raw_field_setting = $field_setting; |
| 135 | |
| 136 | $pp_form_field_wrap_classes = ''; |
| 137 | |
| 138 | $field_with_class = 'full'; |
| 139 | if ( ! empty($field_setting['field_width'])) { |
| 140 | $field_with_class = $field_setting['field_width']; |
| 141 | unset($field_setting['field_width']); |
| 142 | } |
| 143 | |
| 144 | $pp_form_field_wrap_classes .= ' fw-' . esc_attr($field_with_class); |
| 145 | |
| 146 | if ( ! empty($field_setting['icon'])) { |
| 147 | $pp_form_field_wrap_classes .= ' field-has-icon'; |
| 148 | unset($field_setting['icon']); |
| 149 | } |
| 150 | |
| 151 | if (isset($field_setting['password_visibility_icon']) && $field_setting['password_visibility_icon'] === true) { |
| 152 | $pp_form_field_wrap_classes .= ' has-password-visibility-icon'; |
| 153 | unset($field_setting['password_visibility_icon']); |
| 154 | } |
| 155 | |
| 156 | $description_appearance = 'standard'; |
| 157 | if ( ! empty($field_setting['description_appearance'])) { |
| 158 | $description_appearance = $field_setting['description_appearance']; |
| 159 | unset($field_setting['description_appearance']); |
| 160 | } |
| 161 | $pp_form_field_wrap_classes .= ' fda-' . esc_attr($description_appearance); |
| 162 | |
| 163 | $label_display = 'above'; |
| 164 | if ( ! empty($field_setting['label_display'])) { |
| 165 | $label_display = $field_setting['label_display']; |
| 166 | unset($field_setting['label_display']); |
| 167 | } |
| 168 | |
| 169 | $pp_form_field_wrap_classes .= ' fld-' . $label_display; |
| 170 | |
| 171 | if ( ! $this->is_buildscratch) { |
| 172 | $pp_form_field_wrap_classes = ''; |
| 173 | } |
| 174 | |
| 175 | if ( ! $this->is_recaptcha_v3($field_type)) { |
| 176 | $output .= sprintf('<div class="pp-form-field-wrap %s%s">', str_replace('_', '-', $field_type), $pp_form_field_wrap_classes); |
| 177 | } |
| 178 | |
| 179 | $field_setting = apply_filters("ppress_form_field_listing_setting_{$field_type}", $field_setting); |
| 180 | |
| 181 | if (strpos($field_type, 'reg-cpf') !== false) { |
| 182 | $field_type = sprintf('reg-cpf key="%s"', $field_setting['definedFieldKey']); |
| 183 | unset($field_setting['definedFieldKey']); |
| 184 | unset($field_setting['definedFieldType']); |
| 185 | } |
| 186 | |
| 187 | if (strpos($field_type, 'edit-profile-cpf') !== false) { |
| 188 | $field_type = sprintf('edit-profile-cpf key="%s"', $field_setting['definedFieldKey']); |
| 189 | unset($field_setting['definedFieldKey']); |
| 190 | unset($field_setting['definedFieldType']); |
| 191 | } |
| 192 | |
| 193 | $field_id = $field_type; |
| 194 | if (isset($field_setting['key'])) { |
| 195 | $field_id .= '_' . $field_setting['key']; |
| 196 | } |
| 197 | |
| 198 | $field_id = ppress_md5($field_id); |
| 199 | |
| 200 | $output .= $this->label_structure($raw_field_setting, $field_id); |
| 201 | |
| 202 | $field_description = $field_setting['description'] ?? ''; |
| 203 | |
| 204 | unset($field_setting['label']); |
| 205 | unset($field_setting['description']); |
| 206 | unset($field_setting['fieldBarTitle']); |
| 207 | unset($field_setting['fieldTitle']); |
| 208 | unset($field_setting['fieldType']); |
| 209 | unset($field_setting['fieldIcon']); |
| 210 | unset($field_setting['sortID']); |
| 211 | |
| 212 | // add field class |
| 213 | $field_type_class = strpos($field_type, 'reg-cpf') !== false ? 'reg-cpf' : $field_type; |
| 214 | $field_type_class = strpos($field_type_class, 'edit-profile-cpf') !== false ? 'edit-profile-cpf' : $field_type_class; |
| 215 | $saved_classes = ' ' . $field_type_class; |
| 216 | if ( ! empty($field_setting['class'])) { |
| 217 | $saved_classes .= ' ' . $field_setting['class']; |
| 218 | } |
| 219 | |
| 220 | $field_setting['class'] = "pp-form-field{$saved_classes}"; |
| 221 | |
| 222 | if ($this->is_recaptcha($field_type) || $this->is_cloudflare_turnstile($field_type) || $this->is_avatar($field_type) || $this->is_cover_image($field_type)) { |
| 223 | $field_setting['class'] = $saved_classes; |
| 224 | } |
| 225 | |
| 226 | $attributes = sprintf('id="%s" ', $field_id); |
| 227 | |
| 228 | foreach ($field_setting as $key => $value) { |
| 229 | // the str_replace sorcery/ritual checks if field type contains any of the strings https://stackoverflow.com/a/42311760/2648410 |
| 230 | if ($key == 'options' && str_replace(['-select-dropdown', '-checkbox-list', '-radio-buttons'], '', $field_type) != $field_type) { |
| 231 | /** @source https://stackoverflow.com/a/3997367/2648410 */ |
| 232 | $value = array_map('trim', preg_split('/\r\n|\r|\n/', $value, -1, PREG_SPLIT_NO_EMPTY)); |
| 233 | $value = implode(',', $value); |
| 234 | } |
| 235 | |
| 236 | $val = esc_attr($value); |
| 237 | //do not escape custom HTML |
| 238 | if ($key == 'custom_html') { |
| 239 | $val = base64_encode($value); |
| 240 | } |
| 241 | |
| 242 | $attributes .= $key . "='" . $val . "' "; |
| 243 | } |
| 244 | |
| 245 | $output .= $this->shortcode_field_wrap_start; |
| 246 | |
| 247 | if ($this->is_avatar($field_type)) { |
| 248 | $output .= sprintf('<div class="pp-field-user-avatar-picture-wrap" style="width:%s">', esc_attr($field_setting['size']) . 'px'); |
| 249 | } |
| 250 | |
| 251 | if ($this->is_cover_image($field_type)) { |
| 252 | $output .= '<div class="pp-field-user-cover-image-wrap">'; |
| 253 | } |
| 254 | |
| 255 | $output .= apply_filters("ppress_form_field_listing_$field_type", '[' . $field_type . ' ' . $attributes . ']', $raw_field_setting, $this->form_id, $this->form_type); |
| 256 | |
| 257 | if ($this->is_avatar($field_type) && UserAvatar::user_has_pp_avatar(get_current_user_id())) { |
| 258 | $output .= '<span class="pp-profile-avatar-overlay-wrap"><span class="pp-profile-avatar-overlay"><ins><i class="pp-form-material-icons pp-del-profile-avatar">delete</i></ins></span></span>'; |
| 259 | } |
| 260 | |
| 261 | if ($this->is_cover_image($field_type) && ppress_user_has_cover_image()) { |
| 262 | $output .= '<span class="pp-cover-image-overlay-wrap"><span class="pp-cover-image-overlay"><ins><i class="pp-form-material-icons pp-del-cover-image">delete</i></ins></span></span>'; |
| 263 | } |
| 264 | |
| 265 | if ($this->is_avatar($field_type) || $this->is_cover_image($field_type)) { |
| 266 | $output .= '</div>'; |
| 267 | } |
| 268 | |
| 269 | $output .= apply_filters('ppress_form_after_field_listing', '', $raw_field_setting, $this->form_id, $this->form_type); |
| 270 | if ( ! empty($field_description)) { |
| 271 | $output .= '<div class="pp-form-field-description">'; |
| 272 | $output .= wp_kses_post($field_description); |
| 273 | $output .= '</div>'; |
| 274 | } |
| 275 | |
| 276 | $output .= $this->shortcode_field_wrap_end; |
| 277 | |
| 278 | if ( ! $this->is_recaptcha_v3($field_type)) { |
| 279 | $output .= '</div>'; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | return $output; |
| 284 | } |
| 285 | } |
| 286 |