SettingsPages
4 weeks ago
ProfileCustomFields.php
3 years ago
UserRolesEdit.php
10 months ago
index.php
4 years ago
ProfileCustomFields.php
224 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePress\Core\Admin; |
| 4 | |
| 5 | use ProfilePress\Core\Classes\FileUploader; |
| 6 | use ProfilePress\Core\Classes\PROFILEPRESS_sql; |
| 7 | use ProfilePress\Core\Classes\WPProfileFieldParserTrait; |
| 8 | use ProfilePress\Core\Membership\CheckoutFields; |
| 9 | use ProfilePress\Core\ShortcodeParser\Builder\FieldsShortcodeCallback; |
| 10 | |
| 11 | class ProfileCustomFields |
| 12 | { |
| 13 | use WPProfileFieldParserTrait; |
| 14 | |
| 15 | static private $instance; |
| 16 | |
| 17 | public $upload_errors; |
| 18 | |
| 19 | /** |
| 20 | * add the extra field and update to DB |
| 21 | */ |
| 22 | public function __construct() |
| 23 | { |
| 24 | add_action('show_user_profile', array($this, 'display_billing_details_fields'), 1); |
| 25 | add_action('edit_user_profile', array($this, 'display_billing_details_fields'), 1); |
| 26 | |
| 27 | add_action('personal_options_update', array($this, 'save_profile_update')); |
| 28 | add_action('edit_user_profile_update', array($this, 'save_profile_update')); |
| 29 | |
| 30 | add_action('user_profile_update_errors', array($this, 'file_upload_errors'), 10, 3); |
| 31 | |
| 32 | add_action('user_edit_form_tag', array($this, 'add_form_enctype')); |
| 33 | |
| 34 | add_action('admin_footer', [$this, 'js_scripts']); |
| 35 | } |
| 36 | |
| 37 | public function date_field_picker($field_key) |
| 38 | { |
| 39 | echo sprintf('<script>jQuery(function ($) {$("#%1$s").flatpickr(%2$s);});</script>', $field_key, json_encode( |
| 40 | FieldsShortcodeCallback::date_picker_config($field_key) |
| 41 | )); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Add multipart/form-data to wordpress profile admin page |
| 46 | */ |
| 47 | public function edit_form_type() |
| 48 | { |
| 49 | echo ' enctype="multipart/form-data"'; |
| 50 | } |
| 51 | |
| 52 | public function display_billing_details_fields($user) |
| 53 | { |
| 54 | if (apply_filters('ppress_display_billing_details_fields', true)) { |
| 55 | |
| 56 | $billing_fields = CheckoutFields::standard_billing_fields(); |
| 57 | |
| 58 | echo '<h3>' . apply_filters('ppress_billing_details_wp_profile_header', esc_html__('Billing Address (ProfilePress)', 'wp-user-avatar')) . '</h3>'; |
| 59 | |
| 60 | echo '<table class="form-table">'; |
| 61 | |
| 62 | foreach ($billing_fields as $field_key => $field) { |
| 63 | |
| 64 | $field_data = PROFILEPRESS_sql::get_profile_custom_field_by_key($field_key); |
| 65 | |
| 66 | $label_name = $field['label']; |
| 67 | $field_type = $field['field_type']; |
| 68 | $options = ppress_var($field_data, 'options', []); |
| 69 | $description = ppress_var($field_data, 'description', ''); |
| 70 | |
| 71 | // skip woocommerce core billing / shipping fields added to wordpress profile admin page. |
| 72 | if (in_array($field_key, ppress_woocommerce_billing_shipping_fields())) continue; |
| 73 | |
| 74 | if (in_array($field_key, $this->core_user_fields())) continue; |
| 75 | |
| 76 | if ($field_key == CheckoutFields::BILLING_STATE) { |
| 77 | |
| 78 | $billing_country = get_user_meta($user->ID, CheckoutFields::BILLING_COUNTRY, true); |
| 79 | $billing_country_states = empty($billing_country) ? [] : ppress_array_of_world_states($billing_country); |
| 80 | |
| 81 | if ( ! empty($billing_country_states)) { |
| 82 | $field_type = 'select'; |
| 83 | $options = ['' => '————'] + $billing_country_states; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | $this->parse_custom_field($user, $label_name, $field_key, $field_type, $options, $description); |
| 88 | } |
| 89 | echo '</table>'; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | |
| 94 | /** |
| 95 | * Update user profile info. |
| 96 | * |
| 97 | * @param int $user_id |
| 98 | */ |
| 99 | public function save_profile_update($user_id) |
| 100 | { |
| 101 | $custom_fields = ppress_custom_fields_key_value_pair(true); |
| 102 | |
| 103 | if ( ! $custom_fields || empty($custom_fields)) return; |
| 104 | |
| 105 | foreach ($custom_fields as $field_key => $field) { |
| 106 | |
| 107 | $field_data = isset($_POST[$field_key]) ? $_POST[$field_key] : ''; |
| 108 | |
| 109 | $field_value = is_array($field_data) ? array_map('sanitize_textarea_field', $field_data) : sanitize_textarea_field($field_data); |
| 110 | |
| 111 | update_user_meta($user_id, $field_key, $field_value); |
| 112 | |
| 113 | do_action('ppress_after_custom_field_update', $field_key, $field_value, $user_id); |
| 114 | } |
| 115 | |
| 116 | // update file uploads |
| 117 | $uploads = FileUploader::init(); |
| 118 | $upload_errors = ''; |
| 119 | |
| 120 | foreach ($uploads as $field_key => $uploaded_filename_or_wp_error) { |
| 121 | if (is_wp_error($uploads[$field_key])) { |
| 122 | $upload_errors .= $uploads[$field_key]->get_error_message() . '<br/>'; |
| 123 | // save the error in a global state |
| 124 | $this->upload_errors = $upload_errors; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | if (empty($upload_errors)) { |
| 129 | // we get the old array of stored file for the user |
| 130 | $old = get_user_meta($user_id, 'pp_uploaded_files', true); |
| 131 | $old = ! empty($old) ? $old : array(); |
| 132 | |
| 133 | // we loop through the array of newly uploaded files and remove any file (unsetting the file array key) |
| 134 | // that isn't be updated i.e if the field is left empty, unsetting it prevent update_user_meta |
| 135 | // fom overriding it. |
| 136 | // we then merge the old and new uploads before saving the data to user meta table. |
| 137 | foreach ($uploads as $key => $value) { |
| 138 | if (is_null($value) || empty($value)) { |
| 139 | unset($uploads[$key]); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | update_user_meta($user_id, 'pp_uploaded_files', array_merge($old, $uploads)); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Output generated files upload errors. |
| 149 | * |
| 150 | * @param \WP_Error $errors |
| 151 | * @param string $update |
| 152 | * @param \WP_User $user |
| 153 | */ |
| 154 | public function file_upload_errors($errors, $update, $user) |
| 155 | { |
| 156 | if (empty($this->upload_errors)) return; |
| 157 | |
| 158 | $errors->add('file_upload_err', $this->upload_errors); |
| 159 | } |
| 160 | |
| 161 | public function js_scripts() |
| 162 | { |
| 163 | $screen = get_current_screen(); |
| 164 | |
| 165 | if ( ! isset($screen->id) || $screen->id != 'profile') { |
| 166 | return; |
| 167 | } |
| 168 | ?> |
| 169 | |
| 170 | <script type="text/html" id="tmpl-ppress-profile-state-input"> |
| 171 | <input type="text" name="ppress_billing_state" id="ppress_billing_state" value="" class="regular-text"> |
| 172 | </script> |
| 173 | |
| 174 | <script type="text/html" id="tmpl-ppress-profile-state-select"> |
| 175 | <select id="ppress_billing_state" name="ppress_billing_state"> |
| 176 | <option value="">———</option> |
| 177 | <# jQuery.each(data.options, function(index, value) { #> |
| 178 | <option value="{{index}}">{{value}}</option> |
| 179 | <# }); #> |
| 180 | </select> |
| 181 | </script> |
| 182 | |
| 183 | <script type="text/javascript"> |
| 184 | (function ($) { |
| 185 | $(function () { |
| 186 | |
| 187 | var ppress_countries_states = <?php echo wp_json_encode(array_filter(ppress_array_of_world_states())); ?>, |
| 188 | country_state_select_tmpl = wp.template('ppress-profile-state-select'), |
| 189 | country_state_input_tmpl = wp.template('ppress-profile-state-input'); |
| 190 | |
| 191 | $(document).on('change', 'select[name=ppress_billing_country]', function () { |
| 192 | |
| 193 | var val = $(this).val(), field; |
| 194 | |
| 195 | if (val in ppress_countries_states) { |
| 196 | |
| 197 | field = country_state_select_tmpl({ |
| 198 | options: ppress_countries_states[val] |
| 199 | }); |
| 200 | |
| 201 | } else { |
| 202 | |
| 203 | field = country_state_input_tmpl(); |
| 204 | } |
| 205 | |
| 206 | $('#ppress_billing_state').replaceWith(field); |
| 207 | }); |
| 208 | }) |
| 209 | })(jQuery); |
| 210 | </script> |
| 211 | <?php |
| 212 | } |
| 213 | |
| 214 | public static function get_instance() |
| 215 | { |
| 216 | static $instance = null; |
| 217 | |
| 218 | if (is_null($instance)) { |
| 219 | $instance = new self(); |
| 220 | } |
| 221 | |
| 222 | return $instance; |
| 223 | } |
| 224 | } |