PluginProbe ʕ •ᴥ•ʔ
Paid Membership Plugin, Ecommerce, User Registration Form, Login Form, User Profile & Restrict Content – ProfilePress / 4.0.3
Paid Membership Plugin, Ecommerce, User Registration Form, Login Form, User Profile & Restrict Content – ProfilePress v4.0.3
4.17.2 4.17.1 4.17.0 4.16.19 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 years ago ProfileCustomFields.php 4 years ago index.php 4 years ago
ProfileCustomFields.php
222 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 $billing_fields = CheckoutFields::standard_billing_fields();
55
56 echo '<h3>' . apply_filters('ppress_billing_details_wp_profile_header', esc_html__('Billing Address (ProfilePress)', 'profilepress-pro')) . '</h3>';
57
58 echo '<table class="form-table">';
59
60 foreach ($billing_fields as $field_key => $field) {
61
62 $field_data = PROFILEPRESS_sql::get_profile_custom_field_by_key($field_key);
63
64 $label_name = $field['label'];
65 $field_type = $field['field_type'];
66 $options = ppress_var($field_data, 'options', []);
67 $description = ppress_var($field_data, 'description', '');
68
69 // skip woocommerce core billing / shipping fields added to wordpress profile admin page.
70 if (in_array($field_key, ppress_woocommerce_billing_shipping_fields())) continue;
71
72 if (in_array($field_key, $this->core_user_fields())) continue;
73
74 if ($field_key == CheckoutFields::BILLING_STATE) {
75
76 $billing_country = get_user_meta($user->ID, CheckoutFields::BILLING_COUNTRY, true);
77 $billing_country_states = ppress_array_of_world_states($billing_country);
78
79 if ( ! empty($billing_country_states)) {
80 $field_type = 'select';
81 $options = ['' => '&mdash;&mdash;&mdash;&mdash;'] + $billing_country_states;
82 }
83 }
84
85
86 $this->parse_custom_field($user, $label_name, $field_key, $field_type, $options, $description);
87 }
88 echo '</table>';
89 }
90
91
92 /**
93 * Update user profile info.
94 *
95 * @param int $user_id
96 */
97 public function save_profile_update($user_id)
98 {
99 $custom_fields = ppress_custom_fields_key_value_pair(true);
100
101 if ( ! $custom_fields || empty($custom_fields)) return;
102
103 foreach ($custom_fields as $field_key => $field) {
104
105 $field_data = isset($_POST[$field_key]) ? $_POST[$field_key] : '';
106
107 $field_value = is_array($field_data) ? array_map('sanitize_textarea_field', $field_data) : sanitize_textarea_field($field_data);
108
109 update_user_meta($user_id, $field_key, $field_value);
110
111 do_action('ppress_after_custom_field_update', $field_key, $field_value, $user_id);
112 }
113
114 // update file uploads
115 $uploads = FileUploader::init();
116 $upload_errors = '';
117
118 foreach ($uploads as $field_key => $uploaded_filename_or_wp_error) {
119 if (is_wp_error($uploads[$field_key])) {
120 $upload_errors .= $uploads[$field_key]->get_error_message() . '<br/>';
121 // save the error in a global state
122 $this->upload_errors = $upload_errors;
123 }
124 }
125
126 if (empty($upload_errors)) {
127 // we get the old array of stored file for the user
128 $old = get_user_meta($user_id, 'pp_uploaded_files', true);
129 $old = ! empty($old) ? $old : array();
130
131 // we loop through the array of newly uploaded files and remove any file (unsetting the file array key)
132 // that isn't be updated i.e if the field is left empty, unsetting it prevent update_user_meta
133 // fom overriding it.
134 // we then merge the old and new uploads before saving the data to user meta table.
135 foreach ($uploads as $key => $value) {
136 if (is_null($value) || empty($value)) {
137 unset($uploads[$key]);
138 }
139 }
140
141 update_user_meta($user_id, 'pp_uploaded_files', array_merge($old, $uploads));
142 }
143 }
144
145 /**
146 * Output generated files upload errors.
147 *
148 * @param \WP_Error $errors
149 * @param string $update
150 * @param \WP_User $user
151 */
152 public function file_upload_errors($errors, $update, $user)
153 {
154 if (empty($this->upload_errors)) return;
155
156 $errors->add('file_upload_err', $this->upload_errors);
157 }
158
159 public function js_scripts()
160 {
161 $screen = get_current_screen();
162
163 if ( ! isset($screen->id) || $screen->id != 'profile') {
164 return;
165 }
166 ?>
167
168 <script type="text/html" id="tmpl-ppress-profile-state-input">
169 <input type="text" name="ppress_billing_state" id="ppress_billing_state" value="" class="regular-text">
170 </script>
171
172 <script type="text/html" id="tmpl-ppress-profile-state-select">
173 <select id="ppress_billing_state" name="ppress_billing_state">
174 <option value="">&mdash;&mdash;&mdash;</option>
175 <# jQuery.each(data.options, function(index, value) { #>
176 <option value="{{index}}">{{value}}</option>
177 <# }); #>
178 </select>
179 </script>
180
181 <script type="text/javascript">
182 (function ($) {
183 $(function () {
184
185 var ppress_countries_states = <?php echo wp_json_encode(array_filter(ppress_array_of_world_states())); ?>,
186 country_state_select_tmpl = wp.template('ppress-profile-state-select'),
187 country_state_input_tmpl = wp.template('ppress-profile-state-input');
188
189 $(document).on('change', 'select[name=ppress_billing_country]', function () {
190
191 var val = $(this).val(), field;
192
193 if (val in ppress_countries_states) {
194
195 field = country_state_select_tmpl({
196 options: ppress_countries_states[val]
197 });
198
199 } else {
200
201 field = country_state_input_tmpl();
202 }
203
204 $('#ppress_billing_state').replaceWith(field);
205 });
206 })
207 })(jQuery);
208 </script>
209 <?php
210 }
211
212 public static function get_instance()
213 {
214 static $instance = null;
215
216 if (is_null($instance)) {
217 $instance = new self();
218 }
219
220 return $instance;
221 }
222 }