PluginProbe ʕ •ᴥ•ʔ
Paid Membership Plugin, Ecommerce, User Registration Form, Login Form, User Profile & Restrict Content – ProfilePress / trunk
Paid Membership Plugin, Ecommerce, User Registration Form, Login Form, User Profile & Restrict Content – ProfilePress vtrunk
4.16.18 4.16.17 4.16.16 trunk 1.0 1.0.1 1.0.2 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.5a 1.1.6 1.1.7 1.2 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.4 1.4.1 1.4.2 1.5 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.7 1.7.1 1.7.2 1.8 1.8.1 1.8.10 1.8.2 1.8.3 1.8.4 1.8.5 1.8.6 1.8.7 1.8.8 1.8.9 1.9 1.9.1 1.9.10 1.9.11 1.9.12 1.9.13 1.9.2 1.9.3 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.1.9 2.2.10 2.2.11 2.2.12 2.2.13 2.2.14 2.2.15 2.2.16 2.2.2 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 3.0 3.1 3.1.1 3.1.10 3.1.11 3.1.12 3.1.13 3.1.14 3.1.15 3.1.16 3.1.17 3.1.18 3.1.19 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.10 3.2.11 3.2.12 3.2.13 3.2.14 3.2.15 3.2.16 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 3.2.8 3.2.9 4.0.0 4.0.1 4.0.2 4.0.3 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.10.0 4.10.1 4.10.2 4.10.3 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.13.3 4.13.4 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.15.0 4.15.1 4.15.10 4.15.11 4.15.12 4.15.13 4.15.14 4.15.15 4.15.16 4.15.17 4.15.18 4.15.19 4.15.2 4.15.20 4.15.20.1 4.15.21 4.15.22 4.15.23 4.15.24 4.15.25 4.15.3 4.15.4 4.15.5 4.15.6 4.15.7 4.15.8 4.15.9 4.16.0 4.16.1 4.16.10 4.16.11 4.16.12 4.16.13 4.16.14 4.16.15 4.16.2 4.16.3 4.16.4 4.16.5 4.16.6 4.16.7 4.16.8 4.16.9 4.2.0 4.3.0 4.3.1 4.3.2 4.4.0 4.4.1 4.5.0 4.5.1 4.5.2 4.5.3 4.5.4 4.5.5 4.6.0 4.7.0 4.8.0 4.9.0
wp-user-avatar / src / Admin / ProfileCustomFields.php
wp-user-avatar / src / Admin Last commit date
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 = ['' => '&mdash;&mdash;&mdash;&mdash;'] + $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="">&mdash;&mdash;&mdash;</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 }