Installer
3 years ago
AdminNotices.php
1 month ago
AjaxHandler.php
1 year ago
Autologin.php
9 months ago
BlockRegistrations.php
10 months ago
BuddyPressBbPress.php
3 years ago
DisableConcurrentLogins.php
2 years ago
EditUserProfile.php
5 months ago
ExtensionManager.php
1 month ago
FileUploader.php
3 months ago
FormPreviewHandler.php
5 months ago
FormRepository.php
1 year ago
FormShortcodeDefaults.php
1 month ago
GDPR.php
3 years ago
Geolocation.php
3 years ago
GlobalSiteAccess.php
3 years ago
ImageUploader.php
1 year ago
LoginAuth.php
1 year ago
Miscellaneous.php
3 years ago
ModifyRedirectDefaultLinks.php
5 months ago
PPRESS_Session.php
1 year ago
PROFILEPRESS_sql.php
1 year ago
PasswordReset.php
1 year ago
ProfileUrlRewrite.php
4 years ago
RegistrationAuth.php
1 week ago
SendEmail.php
3 years ago
ShortcodeThemeFactory.php
5 years ago
UserAvatar.php
1 year ago
UserSignupLocationListingPage.php
3 years ago
UsernameEmailRestrictLogin.php
4 years ago
WPProfileFieldParserTrait.php
1 year ago
WelcomeEmailAfterSignup.php
1 year ago
default-email-template.php
1 year ago
index.php
3 years ago
WPProfileFieldParserTrait.php
181 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePress\Core\Classes; |
| 4 | |
| 5 | use ProfilePress\Core\ShortcodeParser\Builder\FieldsShortcodeCallback; |
| 6 | |
| 7 | trait WPProfileFieldParserTrait |
| 8 | { |
| 9 | /** |
| 10 | * Add multipart/form-data to wordpress profile admin page |
| 11 | */ |
| 12 | public function add_form_enctype() |
| 13 | { |
| 14 | echo ' enctype="multipart/form-data"'; |
| 15 | } |
| 16 | |
| 17 | protected function date_field_picker($field_key) |
| 18 | { |
| 19 | echo sprintf('<script>jQuery(function ($) {$("#%1$s").flatpickr(%2$s);});</script>', $field_key, json_encode( |
| 20 | FieldsShortcodeCallback::date_picker_config($field_key) |
| 21 | )); |
| 22 | } |
| 23 | |
| 24 | private function description_markup($description) |
| 25 | { |
| 26 | if ( ! empty($description)) { |
| 27 | printf('<p class="description">%s</p>', esc_html($description)); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | public function parse_custom_field($user, $label_name, $field_key, $field_type, $options = [], $description = '') |
| 32 | { |
| 33 | $input_fields_array = ['text', 'password', 'email', 'tel', 'number', 'hidden']; |
| 34 | ?> |
| 35 | <tr> |
| 36 | <th> |
| 37 | <label for="<?= esc_attr($field_key); ?>"><?= wp_kses_post(htmlspecialchars_decode($label_name)); ?></label> |
| 38 | </th> |
| 39 | <td> |
| 40 | <?php if (in_array(($type = $field_type), $input_fields_array)) : ?> |
| 41 | <input type="<?= esc_attr($type); ?>" name="<?= esc_attr($field_key); ?>" id="<?= esc_attr($field_key); ?>" value="<?= esc_attr(get_the_author_meta($field_key, $user->ID)); ?>" class="regular-text"/> |
| 42 | <?php $this->description_markup($description); ?> |
| 43 | <?php endif; ?> |
| 44 | |
| 45 | <?php if ($field_type == 'date') : ?> |
| 46 | <input type="text" name="<?= esc_attr($field_key); ?>" id="<?= esc_attr($field_key); ?>" value="<?= esc_attr(get_the_author_meta($field_key, $user->ID)); ?>" class="pp_datepicker regular-text"> |
| 47 | <?php $this->description_markup($description); ?> |
| 48 | <?php $this->date_field_picker($field_key); ?> |
| 49 | <?php endif; ?> |
| 50 | |
| 51 | <?php if ($field_type == 'country') : ?> |
| 52 | <?php $countries = ppress_array_of_world_countries(); ?> |
| 53 | <?php $value = esc_attr(get_the_author_meta($field_key, $user->ID)); ?> |
| 54 | <select name="<?= $field_key; ?>"> |
| 55 | <option value=""><?php esc_html__('Select a country…', 'wp-user-avatar'); ?></option> |
| 56 | <?php foreach ($countries as $ckey => $cvalue) : ?> |
| 57 | <option value="<?= esc_attr($ckey); ?>" <?php selected($value, $ckey); ?>><?= esc_attr($cvalue); ?> </option>'; |
| 58 | <?php endforeach; ?> |
| 59 | </select> |
| 60 | <?php $this->description_markup($description); ?> |
| 61 | <?php endif; ?> |
| 62 | |
| 63 | <?php if ($field_type == 'textarea') : ?> |
| 64 | <textarea rows="5" name="<?= esc_attr($field_key); ?>"><?= esc_attr(get_the_author_meta($field_key, $user->ID)); ?></textarea> |
| 65 | <?php $this->description_markup($description); ?> |
| 66 | <?php endif; ?> |
| 67 | |
| 68 | <?php if ($field_type == 'radio') : ?> |
| 69 | <?php $radio_buttons = array_map('trim', explode(',', $options)); ?> |
| 70 | <?php foreach ($radio_buttons as $radio_button) : $radio_button = esc_attr($radio_button); ?> |
| 71 | <input id="<?= esc_attr($radio_button) ?>" type="radio" name="<?= esc_attr($field_key); ?>" value="<?= $radio_button ?>" <?php checked((get_the_author_meta($field_key, $user->ID)), $radio_button); ?> /> |
| 72 | <label for="<?= esc_attr($radio_button) ?>"><?= $radio_button ?></label> |
| 73 | <br/> |
| 74 | <?php endforeach; ?> |
| 75 | <?php $this->description_markup($description); ?> |
| 76 | <?php endif; ?> |
| 77 | |
| 78 | <?php if ($field_type == 'agreeable') { |
| 79 | $cpf_saved_data = get_the_author_meta($field_key, $user->ID); |
| 80 | $cpf_saved_data = ('1' == $cpf_saved_data) ? 'true' : $cpf_saved_data; |
| 81 | |
| 82 | echo sprintf('<input type="hidden" name="%s" value="false" style="display: none">', esc_attr($field_key)); |
| 83 | echo sprintf('<input id="%1$s" type="checkbox" name="%1$s" value="true" %2$s/>', esc_attr($field_key), checked($cpf_saved_data, 'true', false)); |
| 84 | echo sprintf('<label for="%1$s">%2$s</label><br/>', esc_attr($field_key), $description); |
| 85 | } |
| 86 | |
| 87 | if ($field_type == 'checkbox') { |
| 88 | $checkbox_values = array_map('trim', explode(',', $options)); |
| 89 | $key = $field_key; |
| 90 | $checkbox_tag_key = "{$key}[]"; |
| 91 | $cpf_saved_data = get_the_author_meta($field_key, $user->ID); |
| 92 | ?> |
| 93 | <?php foreach ($checkbox_values as $checkbox_value) { |
| 94 | $checked = null; |
| 95 | // if data is for multi select dropdown |
| 96 | if (is_array($cpf_saved_data) && in_array($checkbox_value, $cpf_saved_data)) { |
| 97 | $checked = 'checked="checked"'; |
| 98 | } // if data is a single checkbox |
| 99 | elseif ( ! is_array($cpf_saved_data) && $checkbox_value == $cpf_saved_data) { |
| 100 | $checked = 'checked="checked"'; |
| 101 | } |
| 102 | echo sprintf('<input id="%1$s" type="checkbox" name="%2$s" value="%1$s" %3$s/>', esc_attr($checkbox_value), esc_attr($checkbox_tag_key), $checked); |
| 103 | echo sprintf('<label for="%1$s">%1$s</label><br/>', esc_attr($checkbox_value)); |
| 104 | } |
| 105 | $this->description_markup($description); |
| 106 | } |
| 107 | |
| 108 | if ($field_type == 'select') { |
| 109 | |
| 110 | $select_options_values = $options; |
| 111 | if ( ! is_array($options)) { |
| 112 | $select_options_values = array_map('trim', explode(',', $options)); |
| 113 | // make array values the array keys too |
| 114 | $select_options_values = array_combine($select_options_values, $select_options_values); |
| 115 | } |
| 116 | $is_multi_selectable = ppress_is_select_field_multi_selectable($field_key); |
| 117 | $select_tag_key = $is_multi_selectable ? "{$field_key}[]" : $field_key; |
| 118 | $multiple = $is_multi_selectable ? 'multiple' : null; |
| 119 | |
| 120 | $cpf_saved_data = get_the_author_meta($field_key, $user->ID); |
| 121 | |
| 122 | echo sprintf('<select id="%1$s" name="%2$s" %3$s>', esc_attr($field_key), esc_attr($select_tag_key), $multiple); |
| 123 | foreach ($select_options_values as $options_key => $options_value) { |
| 124 | $selected = null; |
| 125 | // if data is for multi select dropdown |
| 126 | if (is_array($cpf_saved_data) && in_array($options_key, $cpf_saved_data)) { |
| 127 | $selected = 'selected="selected"'; |
| 128 | } // if data is not multi select dropdown but a single selection dropdown |
| 129 | elseif ( ! $is_multi_selectable && ! is_array($cpf_saved_data) && $options_key == $cpf_saved_data) { |
| 130 | $selected = 'selected="selected"'; |
| 131 | } |
| 132 | |
| 133 | echo sprintf('<option value="%1$s" %3$s>%2$s</option>', esc_attr($options_key), esc_attr($options_value), $selected); |
| 134 | } |
| 135 | echo '</select>'; |
| 136 | |
| 137 | $this->description_markup($description); |
| 138 | |
| 139 | if ($is_multi_selectable === true) { |
| 140 | echo sprintf('<script>jQuery(function () {jQuery("#%s").select2({width: "350px"});});</script>', esc_attr($field_key)); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | if ($field_type == 'file') { |
| 145 | $user_upload_data = get_user_meta($user->ID, 'pp_uploaded_files', true); |
| 146 | // if the user uploads isn't empty and there exist a file with the custom field key. |
| 147 | if ( ! empty($user_upload_data) && isset($user_upload_data[$field_key])) { |
| 148 | $filename = $user_upload_data[$field_key]; |
| 149 | $link = PPRESS_FILE_UPLOAD_URL . $filename; |
| 150 | echo "<p><a href='$link'>$filename</a></p>"; |
| 151 | } |
| 152 | ?> |
| 153 | <input name="<?= esc_attr($field_key); ?>" type="file"> |
| 154 | <?php $this->description_markup($description); ?> |
| 155 | <?php |
| 156 | } |
| 157 | ?> |
| 158 | </td> |
| 159 | </tr> |
| 160 | <?php |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Array of core user profile. |
| 165 | * |
| 166 | * This is useful in cases where say first and last name field is added so it can be added to buddypress |
| 167 | * extended profile synced to WordPress user profile. |
| 168 | * |
| 169 | * @return array |
| 170 | */ |
| 171 | public function core_user_fields() |
| 172 | { |
| 173 | return [ |
| 174 | 'first_name', |
| 175 | 'last_name', |
| 176 | 'user_nicename', |
| 177 | 'user_url', |
| 178 | 'display_name' |
| 179 | ]; |
| 180 | } |
| 181 | } |