EditProfile
5 years ago
Login
5 years ago
MemberDirectory
5 years ago
PasswordReset
5 years ago
Registration
5 years ago
UserProfile
5 years ago
AbstractBuildScratch.php
5 years ago
AbstractMemberDirectoryTheme.php
5 years ago
AbstractTheme.php
5 years ago
FieldListing.php
5 years ago
MemberDirectoryListing.php
5 years ago
ProfileFieldListing.php
5 years ago
ThemeInterface.php
5 years ago
ThemesRepository.php
5 years ago
index.php
5 years ago
FieldListing.php
278 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', '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 .= $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="pp-hint-tooltip 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_recaptcha_v3($field_type) |
| 109 | { |
| 110 | return $this->is_recaptcha($field_type) && class_exists('ProfilePress\Libsodium\Recaptcha\Recaptcha') && Recaptcha::$type == 'v3'; |
| 111 | } |
| 112 | |
| 113 | public function forge() |
| 114 | { |
| 115 | $output = ''; |
| 116 | |
| 117 | foreach ($this->field_settings as $field_setting) { |
| 118 | |
| 119 | $field_type = $field_setting['fieldType']; |
| 120 | |
| 121 | if ($this->is_recaptcha($field_type) && ! EM::is_enabled(EM::RECAPTCHA)) continue; |
| 122 | |
| 123 | if (isset($this->defaults, $this->defaults[$field_type])) { |
| 124 | $field_setting = wp_parse_args($field_setting, $this->defaults[$field_type]); |
| 125 | } |
| 126 | |
| 127 | $raw_field_setting = $field_setting; |
| 128 | |
| 129 | $pp_form_field_wrap_classes = ''; |
| 130 | |
| 131 | $field_with_class = 'full'; |
| 132 | if ( ! empty($field_setting['field_width'])) { |
| 133 | $field_with_class = $field_setting['field_width']; |
| 134 | unset($field_setting['field_width']); |
| 135 | } |
| 136 | |
| 137 | $pp_form_field_wrap_classes .= ' fw-' . $field_with_class; |
| 138 | |
| 139 | if ( ! empty($field_setting['icon'])) { |
| 140 | $pp_form_field_wrap_classes .= ' field-has-icon'; |
| 141 | unset($field_setting['icon']); |
| 142 | } |
| 143 | |
| 144 | if (isset($field_setting['password_visibility_icon']) && $field_setting['password_visibility_icon'] === true) { |
| 145 | $pp_form_field_wrap_classes .= ' has-password-visibility-icon'; |
| 146 | unset($field_setting['password_visibility_icon']); |
| 147 | } |
| 148 | |
| 149 | $description_appearance = 'standard'; |
| 150 | if ( ! empty($field_setting['description_appearance'])) { |
| 151 | $description_appearance = $field_setting['description_appearance']; |
| 152 | unset($field_setting['description_appearance']); |
| 153 | } |
| 154 | $pp_form_field_wrap_classes .= ' fda-' . $description_appearance; |
| 155 | |
| 156 | $label_display = 'above'; |
| 157 | if ( ! empty($field_setting['label_display'])) { |
| 158 | $label_display = $field_setting['label_display']; |
| 159 | unset($field_setting['label_display']); |
| 160 | } |
| 161 | |
| 162 | $pp_form_field_wrap_classes .= ' fld-' . $label_display; |
| 163 | |
| 164 | if ( ! $this->is_buildscratch) { |
| 165 | $pp_form_field_wrap_classes = ''; |
| 166 | } |
| 167 | |
| 168 | if ( ! $this->is_recaptcha_v3($field_type)) { |
| 169 | $output .= sprintf('<div class="pp-form-field-wrap %s%s">', str_replace('_', '-', $field_type), $pp_form_field_wrap_classes); |
| 170 | } |
| 171 | |
| 172 | $field_setting = apply_filters("ppress_form_field_listing_setting_{$field_type}", $field_setting); |
| 173 | |
| 174 | if (strpos($field_type, 'reg-cpf') !== false) { |
| 175 | $field_type = sprintf('reg-cpf key="%s"', $field_setting['definedFieldKey']); |
| 176 | unset($field_setting['definedFieldKey']); |
| 177 | unset($field_setting['definedFieldType']); |
| 178 | } |
| 179 | |
| 180 | if (strpos($field_type, 'edit-profile-cpf') !== false) { |
| 181 | $field_type = sprintf('edit-profile-cpf key="%s"', $field_setting['definedFieldKey']); |
| 182 | unset($field_setting['definedFieldKey']); |
| 183 | unset($field_setting['definedFieldType']); |
| 184 | } |
| 185 | |
| 186 | $field_id = $field_type; |
| 187 | if (isset($field_setting['key'])) { |
| 188 | $field_id .= '_' . $field_setting['key']; |
| 189 | } |
| 190 | |
| 191 | $field_id = ppress_md5($field_id); |
| 192 | |
| 193 | $output .= $this->label_structure($raw_field_setting, $field_id); |
| 194 | |
| 195 | $field_description = isset($field_setting['description']) ? $field_setting['description'] : ''; |
| 196 | |
| 197 | unset($field_setting['label']); |
| 198 | unset($field_setting['description']); |
| 199 | unset($field_setting['fieldBarTitle']); |
| 200 | unset($field_setting['fieldTitle']); |
| 201 | unset($field_setting['fieldType']); |
| 202 | unset($field_setting['fieldIcon']); |
| 203 | unset($field_setting['sortID']); |
| 204 | |
| 205 | // add field class |
| 206 | $field_type_class = strpos($field_type, 'reg-cpf') !== false ? 'reg-cpf' : $field_type; |
| 207 | $field_type_class = strpos($field_type_class, 'edit-profile-cpf') !== false ? 'edit-profile-cpf' : $field_type_class; |
| 208 | $saved_classes = ' ' . $field_type_class; |
| 209 | if ( ! empty($field_setting['class'])) { |
| 210 | $saved_classes .= ' ' . $field_setting['class']; |
| 211 | } |
| 212 | |
| 213 | $field_setting['class'] = "pp-form-field{$saved_classes}"; |
| 214 | |
| 215 | if ($this->is_recaptcha($field_type) || $this->is_avatar($field_type) || $this->is_cover_image($field_type)) { |
| 216 | $field_setting['class'] = $saved_classes; |
| 217 | } |
| 218 | |
| 219 | $attributes = sprintf('id="%s" ', $field_id); |
| 220 | |
| 221 | foreach ($field_setting as $key => $value) { |
| 222 | // the str_replace sorcery/ritual checks if field type contains any of the strings https://stackoverflow.com/a/42311760/2648410 |
| 223 | if ($key == 'options' && str_replace(['-select-dropdown', '-checkbox-list', '-radio-buttons'], '', $field_type) != $field_type) { |
| 224 | /** @source https://stackoverflow.com/a/3997367/2648410 */ |
| 225 | $value = array_map('trim', preg_split('/\r\n|\r|\n/', $value, -1, PREG_SPLIT_NO_EMPTY)); |
| 226 | $value = implode(',', $value); |
| 227 | } |
| 228 | |
| 229 | $val = esc_attr($value); |
| 230 | //do not escape custom HTML |
| 231 | if ($key == 'custom_html') { |
| 232 | $val = addslashes($value); |
| 233 | } |
| 234 | |
| 235 | $attributes .= $key . "='" . $val . "' "; |
| 236 | } |
| 237 | |
| 238 | $output .= $this->shortcode_field_wrap_start; |
| 239 | |
| 240 | if ($this->is_avatar($field_type)) { |
| 241 | $output .= sprintf('<div class="pp-field-user-avatar-picture-wrap" style="width:%s">', $field_setting['size'] . 'px'); |
| 242 | } |
| 243 | |
| 244 | if ($this->is_cover_image($field_type)) { |
| 245 | $output .= '<div class="pp-field-user-cover-image-wrap">'; |
| 246 | } |
| 247 | |
| 248 | $output .= apply_filters("ppress_form_field_listing_$field_type", '[' . $field_type . ' ' . $attributes . ']', $raw_field_setting, $this->form_id, $this->form_type); |
| 249 | |
| 250 | if ($this->is_avatar($field_type) && UserAvatar::user_has_pp_avatar(get_current_user_id())) { |
| 251 | $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>'; |
| 252 | } |
| 253 | |
| 254 | if ($this->is_cover_image($field_type) && ppress_user_has_cover_image()) { |
| 255 | $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>'; |
| 256 | } |
| 257 | |
| 258 | if ($this->is_avatar($field_type) || $this->is_cover_image($field_type)) { |
| 259 | $output .= '</div>'; |
| 260 | } |
| 261 | |
| 262 | $output .= apply_filters('ppress_form_after_field_listing', '', $raw_field_setting, $this->form_id, $this->form_type); |
| 263 | if ( ! empty($field_description)) { |
| 264 | $output .= '<div class="pp-form-field-description">'; |
| 265 | $output .= $field_description; |
| 266 | $output .= '</div>'; |
| 267 | } |
| 268 | |
| 269 | $output .= $this->shortcode_field_wrap_end; |
| 270 | |
| 271 | if ( ! $this->is_recaptcha_v3($field_type)) { |
| 272 | $output .= '</div>'; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | return $output; |
| 277 | } |
| 278 | } |