Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/paid-memberships-pro/includes/fields.php

Keine Baseline-Datei – Diff nur gegen leer.
Zur Liste
1 -
1 + <?php
2 + /**
3 + * Check if a variable is a PMPro_Field.
4 + * Also checks for PMProRH_Field.
5 + */
6 + function pmpro_is_field( $var ) {
7 + if ( is_a( $var, 'PMPro_Field' ) || is_a( $var, 'PMProRH_Field' ) ) {
8 + return true;
9 + } else {
10 + return false;
11 + }
12 + }
13 +
14 + /**
15 + * Add a field to the PMPro registration fields global
16 + *
17 + * $where refers to various hooks in the PMPro checkout page and can be:
18 + * - after_username
19 + * - after_password
20 + * - after_email
21 + * - after_captcha
22 + * - checkout_boxes
23 + * - after_billing_fields
24 + * - before_submit_button
25 + * - just_profile (make sure you set the profile attr of the field to true or admins)
26 + */
27 + function pmpro_add_user_field( $where, $field ) {
28 + /**
29 + * Filter the group to add the field to.
30 + *
31 + * @since 2.9.3
32 + * @deprecated 3.4
33 + *
34 + * @param string $where The name of the group to add the field to.
35 + * @param PMPro_Field $field The field being added.
36 + */
37 + $where = apply_filters_deprecated( 'pmpro_add_user_field_where', array( $where, $field ), '3.4', 'pmpro_add_user_field' );
38 +
39 + // Get the field group.
40 + $field_group = PMPro_Field_Group::get( $where );
41 +
42 + // Add the field to the group.
43 + $field_group->add_field( $field );
44 + }
45 +
46 + /**
47 + * Add a new checkout box to the checkout_boxes section.
48 + * You can then use this as the $where parameter
49 + * to pmpro_add_user_field.
50 + *
51 + * Name must contain no spaces or special characters.
52 + */
53 + function pmpro_add_field_group( $name, $label = NULL, $description = '', $order = NULL ) {
54 + return PMPro_Field_Group::add( $name, $label, $description );
55 + }
56 +
57 + /**
58 + * Add a new User Taxonomy. You can then use this as the user_taxonomny parameter to pmpro_add_user_field.
59 + *
60 + * @param string $name The singular name for the taxonomy object.
61 + * @param string $name_plural The plural name for the taxonomy object.
62 + *
63 + */
64 + function pmpro_add_user_taxonomy( $name, $name_plural ) {
65 + global $pmpro_user_taxonomies;
66 +
67 + // Sanitize the taxonomy $name and make sure it is less than 32 characters.
68 + $safe_name = sanitize_key( $name );
69 + if ( strlen( $safe_name ) > 32 ) {
70 + $safe_name = substr( $safe_name, 0, 32 );
71 + }
72 +
73 + // Add to the global so we can keep track.
74 + $pmpro_user_taxonomies = (array) $pmpro_user_taxonomies;
75 + $pmpro_user_taxonomies[] = $safe_name;
76 +
77 + // Make sure name and plural name are less than 32 characters.
78 + if ( strlen( $name ) > 32 ) {
79 + $name = substr( $name, 0, 32 );
80 + }
81 + if ( strlen( $name_plural ) > 32 ) {
82 + $name_plural = substr( $name_plural, 0, 32 );
83 + }
84 +
85 + $pmpro_user_taxonomy_labels = array(
86 + 'name' => ucwords( $name ),
87 + 'singular_name' => ucwords( $name ),
88 + 'menu_name' => ucwords( $name_plural ),
89 + 'search_items' => sprintf( esc_html__( 'Search %s', 'paid-memberships-pro' ), ucwords( $name_plural ) ),
90 + 'popular_items' => sprintf( esc_html__( 'Popular %s', 'paid-memberships-pro' ), ucwords( $name_plural ) ),
91 + 'all_items' => sprintf( esc_html__( 'All %s', 'paid-memberships-pro' ), ucwords( $name_plural ) ),
92 + 'edit_item' => sprintf( esc_html__( 'Edit %s', 'paid-memberships-pro' ), ucwords( $name ) ),
93 + 'update_item' => sprintf( esc_html__( 'Update %s', 'paid-memberships-pro' ), ucwords( $name ) ),
94 + 'add_new_item' => sprintf( esc_html__( 'Add New %s', 'paid-memberships-pro' ), ucwords( $name ) ),
95 + 'new_item_name' => sprintf( esc_html__( 'New %s Name', 'paid-memberships-pro' ), ucwords( $name ) ),
96 + 'separate_items_with_commas' => sprintf( esc_html__( 'Separate %s with commas', 'paid-memberships-pro' ), $name_plural ),
97 + 'add_or_remove_items' => sprintf( esc_html__( 'Add or remove %s', 'paid-memberships-pro' ), $name_plural ),
98 + 'choose_from_most_used' => sprintf( esc_html__( 'Choose from the most popular %s', 'paid-memberships-pro' ), $name_plural ),
99 + );
100 +
101 + $pmpro_user_taxonomy_args = array(
102 + 'public' => false,
103 + 'labels' => $pmpro_user_taxonomy_labels,
104 + 'rewrite' => false,
105 + 'show_ui' => true,
106 + 'capabilities' => array(
107 + 'manage_terms' => 'edit_users',
108 + 'edit_terms' => 'edit_users',
109 + 'delete_terms' => 'edit_users',
110 + 'assign_terms' => 'read',
111 + ),
112 + );
113 +
114 + /**
115 + * Filter the args passed to the user taxonomy created.
116 + *
117 + * @param array $pmpro_user_taxonomy_args The arguments passed to the register_taxonomy function.
118 + * @param string $name The current taxonomy name.
119 + *
120 + */
121 + $pmpro_user_taxonomy_args = apply_filters( 'pmpro_user_taxonomy_args', $pmpro_user_taxonomy_args, $name );
122 + register_taxonomy( $safe_name, 'user', $pmpro_user_taxonomy_args );
123 +
124 + // Update the labels after the args are filtered.
125 + $pmpro_user_taxonomy_labels = $pmpro_user_taxonomy_args['labels'];
126 +
127 + /**
128 + * Add admin page for the registered user taxonomies.
129 + */
130 + add_action( 'admin_menu', function () use ( $pmpro_user_taxonomy_labels, $safe_name ) {
131 + add_users_page(
132 + esc_attr( $pmpro_user_taxonomy_labels['menu_name'] ),
133 + esc_attr( $pmpro_user_taxonomy_labels['menu_name'] ),
134 + 'edit_users',
135 + 'edit-tags.php?taxonomy=' . $safe_name
136 + );
137 + } );
138 +
139 + /**
140 + * Update parent file name to fix the selected menu issue for a user taxonomy.
141 + */
142 + add_filter( 'parent_file', function ( $parent_file ) use ( $safe_name ) {
143 + global $submenu_file;
144 + if (
145 + isset( $_GET['taxonomy'] ) &&
146 + $_GET['taxonomy'] == $safe_name &&
147 + $submenu_file == 'edit-tags.php?taxonomy=' . $safe_name
148 + ) {
149 + $parent_file = 'users.php';
150 + }
151 +
152 + return $parent_file;
153 + } );
154 + }
155 +
156 + /**
157 + * Get a field group by name.
158 + */
159 + function pmpro_get_field_group_by_name( $name ) {
160 + return PMPro_Field_Group::get( $name );
161 + }
162 +
163 + /**
164 + * Check if a user field is enabled for the current checkout level.
165 + */
166 + function pmpro_check_field_for_level( $field, $scope = 'default', $args = NULL ) {
167 + global $pmpro_level, $pmpro_checkout_level_ids;
168 + if ( ! empty( $field->levels ) ) {
169 + if ( 'profile' === $scope ) {
170 + // Expecting the args to be the user id.
171 + if ( pmpro_hasMembershipLevel( $field->levels, $args ) ) {
172 + return true;
173 + } else {
174 + return false;
175 + }
176 + } else {
177 + if ( empty( $pmpro_checkout_level_ids ) && ! empty( $pmpro_level ) && ! empty( $pmpro_level->id ) ) {
178 + $pmpro_checkout_level_ids = array( $pmpro_level->id );
179 + }
180 + if ( ! is_array( $field->levels ) ) {
181 + $field_levels = array( $field->levels );
182 + } else {
183 + $field_levels = $field->levels;
184 + }
185 + if ( ! empty( $pmpro_checkout_level_ids ) ) {
186 + // Check against $_REQUEST.
187 + return ( ! empty( array_intersect( $field_levels, $pmpro_checkout_level_ids ) ) );
188 + }
189 + return false;
190 + }
191 + }
192 +
193 + return true;
194 + }
195 +
196 + /**
197 + * Get a list of all fields that are only shown when creating a user at checkout.
198 + */
199 + function pmpro_get_user_creation_field_groups() {
200 + return array(
201 + 'after_username',
202 + 'after_password',
203 + 'after_email',
204 + );
205 + }
206 +
207 + /**
208 + * Find fields in a group and display them at checkout.
209 + * This function is only used for the following fields at checkout:
210 + * - after_username
211 + * - after_password
212 + * - after_email
213 + * - after_captcha
214 + * - checkout_boxes
215 + * - after_billing_fields
216 + * - before_submit_button
217 + * - after_tos_fields
218 + */
219 + function pmpro_display_fields_in_group( $group, $scope = 'checkout' ) {
220 + $valid_groups = array(
221 + 'after_username',
222 + 'after_password',
223 + 'after_pricing_fields',
224 + 'after_email',
225 + 'after_captcha',
226 + 'after_billing_fields',
227 + 'before_submit_button',
228 + 'after_tos_fields',
229 + );
230 + if ( ! in_array( $group, $valid_groups ) ) {
231 + _doing_it_wrong( __FUNCTION__, sprintf( esc_html__( 'The group %s should not be passed into %s. Use PMPro_Field_Group::display() instead.', 'paid-memberships-pro' ), esc_html( $group ), __FUNCTION__ ), '2.9.3' );
232 + }
233 + if ( $scope !== 'checkout' ) {
234 + _doing_it_wrong( __FUNCTION__, sprintf( esc_html__( 'The scope %s should not be passed into %s. Use PMPro_Field_Group::display() instead.', 'paid-memberships-pro' ), esc_html( $scope ), __FUNCTION__ ), '2.9.3' );
235 + }
236 +
237 + // Get the field group.
238 + $field_group = PMPro_Field_Group::get( $group );
239 + $field_group->display(
240 + array(
241 + 'markup' => 'div',
242 + 'scope' => 'checkout',
243 + 'show_group_label' => false,
244 + 'prefill_from_request' => true,
245 + 'show_required' => true,
246 + )
247 + );
248 + }
249 +
250 + /**
251 + * Cycle through extra fields. Show them at checkout.
252 + */
253 + // after_username
254 + function pmpro_checkout_after_username_fields() {
255 + pmpro_display_fields_in_group( 'after_username', 'checkout' );
256 + }
257 + add_action( 'pmpro_checkout_after_username', 'pmpro_checkout_after_username_fields' );
258 +
259 + //after_password
260 + function pmpro_checkout_after_password_fields() {
261 + pmpro_display_fields_in_group( 'after_password', 'checkout' );
262 + }
263 + add_action( 'pmpro_checkout_after_password', 'pmpro_checkout_after_password_fields' );
264 +
265 + //after_email
266 + function pmpro_checkout_after_email_fields() {
267 + pmpro_display_fields_in_group( 'after_email', 'checkout' );
268 + }
269 + add_action( 'pmpro_checkout_after_email', 'pmpro_checkout_after_email_fields' );
270 +
271 + //after captcha
272 + function pmpro_checkout_after_captcha_fields() {
273 + pmpro_display_fields_in_group( 'after_captcha', 'checkout' );
274 + }
275 + add_action( 'pmpro_checkout_before_submit_button', 'pmpro_checkout_after_captcha_fields' );
276 +
277 + //checkout boxes
278 + function pmpro_checkout_boxes_fields() {
279 + // Get all field groups.
280 + $field_groups = PMPro_Field_Group::get_all();
281 +
282 + $checkout_level = pmpro_getLevelAtCheckout();
283 + $chekcout_level_id = ! empty( $checkout_level->id ) ? (int)$checkout_level->id : NULL;
284 + if ( empty( $chekcout_level_id ) ) {
285 + return;
286 + }
287 +
288 + // Cycle through the field groups.
289 + foreach( $field_groups as $field_group_name => $field_group ) {
290 + // If this is not a checkout box, skip it.
291 + if ( in_array( $field_group_name, array( 'after_username', 'after_password', 'after_email', 'after_captcha', 'after_pricing_fields', 'after_billing_fields', 'before_submit_button', 'after_tos_fields' ) ) ) {
292 + continue;
293 + }
294 +
295 + $field_group->display(
296 + array(
297 + 'markup' => 'card',
298 + 'scope' => 'checkout',
299 + 'prefill_from_request' => true,
300 + 'show_required' => true,
301 + )
302 + );
303 + }
304 + }
305 + add_action( 'pmpro_checkout_boxes', 'pmpro_checkout_boxes_fields' );
306 +
307 + //after_pricing_fields
308 + function pmpro_checkout_after_pricing_fields() {
309 + pmpro_display_fields_in_group( 'after_pricing_fields', 'checkout' );
310 + }
311 + add_action( 'pmpro_checkout_after_pricing_fields', 'pmpro_checkout_after_pricing_fields' );
312 +
313 + //after_billing_fields
314 + function pmpro_checkout_after_billing_fields() {
315 + pmpro_display_fields_in_group( 'after_billing_fields', 'checkout' );
316 + }
317 + add_action( 'pmpro_checkout_after_billing_fields', 'pmpro_checkout_after_billing_fields');
318 +
319 + //before submit button
320 + function pmpro_checkout_before_submit_button_fields() {
321 + pmpro_display_fields_in_group( 'before_submit_button', 'checkout' );
322 + }
323 + add_action( 'pmpro_checkout_before_submit_button', 'pmpro_checkout_before_submit_button_fields');
324 +
325 + // After tos fields.
326 + function pmpro_checkout_after_tos_fields() {
327 + pmpro_display_fields_in_group( 'after_tos_fields', 'checkout' );
328 + }
329 + add_action( 'pmpro_checkout_before_submit_button', 'pmpro_checkout_after_tos_fields', 6 );
330 +
331 + /**
332 + * Update user creation fields at checkout after a user is created.
333 + *
334 + * Only runs for the after_username, after_email, and after_password field groups.
335 + *
336 + * @since 3.4
337 + *
338 + * @param int $user_id The ID of the user that was created.
339 + */
340 + function pmpro_checkout_before_user_auth_save_fields( $user_id ) {
341 + // Loop through all the field groups.
342 + $field_groups = PMPro_Field_Group::get_all();
343 + $user_creation_field_groups = pmpro_get_user_creation_field_groups();
344 + foreach($field_groups as $group_name => $group) {
345 + if ( ! in_array( $group_name, $user_creation_field_groups ) ) {
346 + continue;
347 + }
348 +
349 + // Save the fields.
350 + $group->save_fields(
351 + array(
352 + 'user_id' => $user_id,
353 + 'scope' => 'checkout',
354 + )
355 + );
356 + }
357 + }
358 + add_action( 'pmpro_checkout_before_user_auth', 'pmpro_checkout_before_user_auth_save_fields' );
359 +
360 + /**
361 + * Require required fields before creating a user at checkout.
362 + *
363 + * Only runs for the after_username, after_email, and after_password field groups.
364 + */
365 + function pmpro_checkout_user_creation_checks_user_fields( $okay ) {
366 + // Arrays to store fields that were required and missed.
367 + $required = array();
368 + $required_labels = array();
369 +
370 + // Loop through all the field groups.
371 + $field_groups = PMPro_Field_Group::get_all();
372 + $user_creation_field_groups = pmpro_get_user_creation_field_groups();
373 + foreach($field_groups as $group_name => $group) {
374 + if ( ! in_array( $group_name, $user_creation_field_groups ) ) {
375 + continue;
376 + }
377 +
378 + // Loop through all the fields in the group.
379 + $fields = $group->get_fields_to_display(
380 + array(
381 + 'scope' => 'checkout',
382 + )
383 + );
384 + foreach($fields as $field) {
385 + // If this is a file upload, check whether the file is allowed.
386 + if ( isset( $_FILES[ $field->name ] ) && ! empty( $_FILES[$field->name]['name'] ) ) {
387 + $upload_check = pmpro_check_upload( $field->name );
388 + if ( is_wp_error( $upload_check ) ) {
389 + pmpro_setMessage( $upload_check->get_error_message(), 'pmpro_error' );
390 + return false;
391 + }
392 + }
393 +
394 + // If the field was filled if needed, skip it.
395 + if ( $field->was_filled_if_needed() ) {
396 + continue;
397 + }
398 +
399 + // The field was not filled.
400 + $required[] = $field->name;
401 + $required_labels[] = $field->label;
402 + }
403 + }
404 +
405 + if(!empty($required))
406 + {
407 + $required = array_unique($required);
408 +
409 + //add them to error fields
410 + global $pmpro_error_fields;
411 + $pmpro_error_fields = array_merge((array)$pmpro_error_fields, $required);
412 +
413 + if( count( $required ) == 1 ) {
414 + $pmpro_msg = sprintf( esc_html__( 'The %s field is required.', 'paid-memberships-pro' ), implode(", ", $required_labels) );
415 + $pmpro_msgt = 'pmpro_error';
416 + } else {
417 + $pmpro_msg = sprintf( esc_html__( 'The %s fields are required.', 'paid-memberships-pro' ), implode(", ", $required_labels) );
418 + $pmpro_msgt = 'pmpro_error';
419 + }
420 +
421 + if($okay)
422 + pmpro_setMessage($pmpro_msg, $pmpro_msgt);
423 +
424 + return false;
425 + }
426 +
427 + //return whatever status was before
428 + return $okay;
429 + }
430 + add_filter( 'pmpro_checkout_user_creation_checks', 'pmpro_checkout_user_creation_checks_user_fields' );
431 +
432 + /**
433 + * Update the fields after a checkout is completed.
434 + *
435 + * Does not run for the after_username, after_email, and after_password field groups.
436 + *
437 + * @param int $user_id The ID of the user that was created.
438 + * @param object $order The order object.
439 + */
440 + function pmpro_after_checkout_save_fields( $user_id, $order ) {
441 + // Loop through all the field groups.
442 + $field_groups = PMPro_Field_Group::get_all();
443 + $user_creation_field_groups = pmpro_get_user_creation_field_groups();
444 + foreach($field_groups as $group_name => $group) {
445 + if ( in_array( $group_name, $user_creation_field_groups ) ) {
446 + continue;
447 + }
448 +
449 + // Save the fields.
450 + $group->save_fields(
451 + array(
452 + 'user_id' => $user_id,
453 + 'scope' => 'checkout',
454 + )
455 + );
456 + }
457 + }
458 + add_action( 'pmpro_after_checkout', 'pmpro_after_checkout_save_fields', 10, 2 );
459 + add_action( 'pmpro_before_send_to_paypal_standard', 'pmpro_after_checkout_save_fields', 20, 2 ); //for paypal standard we need to do this just before sending the user to paypal
460 + add_action( 'pmpro_before_send_to_twocheckout', 'pmpro_after_checkout_save_fields', 20, 2 ); //for 2checkout we need to do this just before sending the user to 2checkout
461 + add_action( 'pmpro_before_send_to_gourl', 'pmpro_after_checkout_save_fields', 20, 2 ); //for the GoURL Bitcoin Gateway Add On
462 + add_action( 'pmpro_before_send_to_payfast', 'pmpro_after_checkout_save_fields', 20, 2 ); //for the Payfast Gateway Add On
463 +
464 + /**
465 + * Require required fields before creating an order at checkout.
466 + *
467 + * Does not run for the after_username, after_email, and after_password field groups.
468 + */
469 + function pmpro_registration_checks_for_user_fields( $okay ) {
470 + // Arrays to store fields that were required and missed.
471 + $required = array();
472 + $required_labels = array();
473 +
474 + // Loop through all the field groups.
475 + $field_groups = PMPro_Field_Group::get_all();
476 + $user_creation_field_groups = pmpro_get_user_creation_field_groups();
477 + foreach($field_groups as $group_name => $group) {
478 + if ( in_array( $group_name, $user_creation_field_groups ) ) {
479 + continue;
480 + }
481 +
482 + // Loop through all the fields in the group.
483 + $fields = $group->get_fields_to_display(
484 + array(
485 + 'scope' => 'checkout',
486 + )
487 + );
488 + foreach($fields as $field) {
489 + // If this is a file upload, check whether the file is allowed.
490 + if ( isset( $_FILES[ $field->name ] ) && ! empty( $_FILES[$field->name]['name'] ) ) {
491 + $upload_check = pmpro_check_upload( $field->name );
492 + if ( is_wp_error( $upload_check ) ) {
493 + pmpro_setMessage( $upload_check->get_error_message(), 'pmpro_error' );
494 + return false;
495 + }
496 + }
497 +
498 + // If the field was filled if needed, skip it.
499 + if ( $field->was_filled_if_needed() ) {
500 + continue;
501 + }
502 +
503 + // The field was not filled.
504 + $required[] = $field->name;
505 + $required_labels[] = $field->label;
506 + }
507 + }
508 +
509 + if(!empty($required))
510 + {
511 + $required = array_unique($required);
512 +
513 + //add them to error fields
514 + global $pmpro_error_fields;
515 + $pmpro_error_fields = array_merge((array)$pmpro_error_fields, $required);
516 +
517 + if( count( $required ) == 1 ) {
518 + $pmpro_msg = sprintf( esc_html__( 'The %s field is required.', 'paid-memberships-pro' ), implode(", ", $required_labels) );
519 + $pmpro_msgt = 'pmpro_error';
520 + } else {
521 + $pmpro_msg = sprintf( esc_html__( 'The %s fields are required.', 'paid-memberships-pro' ), implode(", ", $required_labels) );
522 + $pmpro_msgt = 'pmpro_error';
523 + }
524 +
525 + if($okay)
526 + pmpro_setMessage($pmpro_msg, $pmpro_msgt);
527 +
528 + return false;
529 + }
530 +
531 + //return whatever status was before
532 + return $okay;
533 + }
534 + add_filter( 'pmpro_checkout_order_creation_checks', 'pmpro_registration_checks_for_user_fields' );
535 +
536 + /**
537 + * Sessions vars for TwoCheckout. PayPal Express was updated to store in order meta.
538 + *
539 + * @deprecated 2.12.4 Use pmpro_after_checkout_save_fields instead to save fields immediately or pmpro_save_checkout_data_to_order for delayed checkouts.
540 + */
541 + function pmpro_paypalexpress_session_vars_for_user_fields() {
542 + _deprecated_function( __FUNCTION__, '2.12.4', 'pmpro_after_checkout_save_fields' );
543 +
544 + // Loop through all the field groups.
545 + $field_groups = PMPro_Field_Group::get_all();
546 + foreach($field_groups as $group_name => $group) {
547 + // Loop through all the fields in the group.
548 + $fields = $group->get_fields();
549 + foreach($fields as $field)
550 + {
551 + if( ! pmpro_is_field( $field ) ) {
552 + continue;
553 + }
554 +
555 + if ( ! pmpro_check_field_for_level( $field ) ) {
556 + continue;
557 + }
558 +
559 + if( isset( $_REQUEST[$field->name] ) ) {
560 + $_SESSION[$field->name] = pmpro_sanitize( $_REQUEST[$field->name], $field ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
561 + } elseif ( isset( $_FILES[$field->name] ) ) {
562 + /*
563 + We need to save the file somewhere and save values in $_SESSION
564 + */
565 + // Make sure the file is allowed.
566 + $upload_check = pmpro_check_upload( $field->name );
567 + if ( is_wp_error( $upload_check ) ) {
568 + continue;
569 + }
570 +
571 + // Get $file and $filetype.
572 + $file = array_map( 'sanitize_text_field', $_FILES[ $field->name ] );
573 + $filetype = wp_check_filetype_and_ext( $file['tmp_name'], $file['name'] );
574 +
575 + // Make sure file was uploaded during this page load.
576 + if ( ! is_uploaded_file( sanitize_text_field( $file['tmp_name'] ) ) ) {
577 + continue;
578 + }
579 +
580 + //check for a register helper directory in wp-content
581 + $upload_dir = wp_upload_dir();
582 + $pmprorh_dir = $upload_dir['basedir'] . "/pmpro-register-helper/tmp/";
583 +
584 + //create the dir and subdir if needed
585 + if(!is_dir($pmprorh_dir))
586 + {
587 + wp_mkdir_p($pmprorh_dir);
588 + }
589 +
590 + //move file
591 + $new_filename = $pmprorh_dir . basename( sanitize_file_name( $file['name'] ) );
592 + move_uploaded_file( sanitize_text_field( $$file['tmp_name'] ), $new_filename );
593 +
594 + //update location of file
595 + $_FILES[$field->name]['tmp_name'] = $new_filename;
596 +
597 + //save file info in session
598 + $_SESSION[$field->name] = array_map( 'sanitize_text_field', $file );
599 + }
600 + }
601 + }
602 + }
603 +
604 + /**
605 + * Show user fields in profile.
606 + *
607 + * @deprecated 3.4
608 + */
609 + function pmpro_show_user_fields_in_profile( $user, $withlocations = false ) {
610 + _deprecated_function( __FUNCTION__, '3.4', 'pmpro_show_user_fields_in_profile_with_locations' );
611 + if ( $withlocations ) {
612 + return pmpro_show_user_fields_in_profile_with_locations( $user );
613 + }
614 + $groups = PMPro_Field_Group::get_all();
615 + foreach( $groups as $group ) {
616 + $group->display(
617 + array(
618 + 'markup' => 'table',
619 + 'scope' => 'profile',
620 + 'show_group_label' => $withlocations,
621 + 'user_id' => $user->ID,
622 + )
623 + );
624 + }
625 + }
626 +
627 + /**
628 + * Show user fields in the backend profile.
629 + */
630 + function pmpro_show_user_fields_in_profile_with_locations( $user ) {
631 + $groups = PMPro_Field_Group::get_all();
632 + foreach( $groups as $group ) {
633 + $group->display(
634 + array(
635 + 'markup' => 'table',
636 + 'scope' => 'profile',
637 + 'user_id' => $user->ID,
638 + )
639 + );
640 + }
641 + }
642 + add_action( 'show_user_profile', 'pmpro_show_user_fields_in_profile_with_locations' );
643 + add_action( 'edit_user_profile', 'pmpro_show_user_fields_in_profile_with_locations' );
644 +
645 + /**
646 + * Show Profile fields on the frontend "Member Profile Edit" page.
647 + *
648 + * @since 2.3
649 + * @deprecated 3.4
650 + */
651 + function pmpro_show_user_fields_in_frontend_profile( $user, $withlocations = false ) {
652 + _deprecated_function( __FUNCTION__, '3.4', 'pmpro_show_user_fields_in_frontend_profile_with_locations' );
653 + if ( $withlocations ) {
654 + return pmpro_show_user_fields_in_frontend_profile_with_locations( $user );
655 + }
656 +
657 + $groups = PMPro_Field_Group::get_all();
658 + foreach( $groups as $group ) {
659 + $group->display(
660 + array(
661 + 'markup' => 'div',
662 + 'scope' => 'profile',
663 + 'show_group_label' => $withlocations,
664 + 'user_id' => $user->ID,
665 + )
666 + );
667 + }
668 + }
669 +
670 + /**
671 + * Show Profile fields on the frontend "Member Profile Edit" page.
672 + *
673 + * @since 2.3
674 + */
675 + function pmpro_show_user_fields_in_frontend_profile_with_locations( $user ) {
676 + $groups = PMPro_Field_Group::get_all();
677 + foreach( $groups as $group ) {
678 + $group->display(
679 + array(
680 + 'markup' => 'div',
681 + 'scope' => 'profile',
682 + 'user_id' => $user->ID,
683 + )
684 + );
685 + }
686 + }
687 + add_action( 'pmpro_show_user_profile', 'pmpro_show_user_fields_in_frontend_profile_with_locations' );
688 +
689 + /**
690 + * Show user fields on the Add Member form
691 + * when using the Add Member Admin Add On.
692 + */
693 + // Add fields to form.
694 + function pmpro_add_member_admin_fields( $user = null, $user_id = null) {
695 + $addmember_fields = array();
696 + // Loop through all the field groups.
697 + $field_groups = PMPro_Field_Group::get_all();
698 + foreach($field_groups as $group_name => $group) {
699 + // Loop through all the fields in the group.
700 + $fields = $group->get_fields();
701 + foreach($fields as $field)
702 + {
703 + if(pmpro_is_field($field) && isset($field->addmember) && !empty($field->addmember) && ( in_array( strtolower( $field->addmember ), array( 'true', 'yes' ) ) || true == $field->addmember ) )
704 + {
705 + $addmember_fields[] = $field;
706 + }
707 + }
708 + }
709 +
710 +
711 + //show the fields
712 + if(!empty($addmember_fields)) {
713 + //cycle through groups
714 + foreach($addmember_fields as $field)
715 + {
716 + if(empty($user_id) && !empty($user) && !empty($user->ID)) {
717 + $user_id = $user->ID;
718 + }
719 +
720 + if( ! pmpro_is_field( $field ) ) {
721 + continue;
722 + }
723 +
724 + if(metadata_exists("user", $user_id, $field->meta_key))
725 + {
726 + $value = get_user_meta($user_id, $field->meta_key, true);
727 + } else {
728 + $value = "";
729 + }
730 + ?>
731 + <tr id="<?php echo esc_attr( $field->id );?>_tr">
732 + <th>
733 + <?php if ( ! empty( $field->showmainlabel ) ) { ?>
734 + <label for="<?php echo esc_attr($field->name);?>"><?php echo wp_kses_post( $field->label );?></label>
735 + <?php } ?>
736 + </th>
737 + <td>
738 + <?php
739 + if(current_user_can("edit_user", $user_id) && $field !== false)
740 + $field->display($value);
741 + else
742 + echo "<div>" . wp_kses_post( $field->displayValue($value) ) . "</div>";
743 + ?>
744 + <?php if(!empty($field->hint)) { ?>
745 + <p class="description"><?php echo wp_kses_post( $field->hint );?></p>
746 + <?php } ?>
747 + </td>
748 + </tr>
749 + <?php
750 + }
751 + }
752 + }
753 + add_action( 'pmpro_add_member_fields', 'pmpro_add_member_admin_fields', 10, 2 );
754 +
755 + /**
756 + * Save user fields on the Add Member Admin form.
757 + * Hooks into pmpro_add_member_added.
758 + * @since 2.9
759 + * @param int $uid The user ID.
760 + * @param object $user The user object.
761 + * @return void
762 + */
763 + function pmpro_add_member_admin_save_user_fields( $uid = null, $user = null ) {
764 +
765 + // Use the ID from the $user object if passed in.
766 + if ( ! empty( $user ) && is_object( $user ) ) {
767 + $user_id = $user->ID;
768 + }
769 +
770 + // Otherwise, let's use the $uid passed in.
771 + if ( !empty( $uid ) && ( empty( $user ) || !is_object( $user ) ) ) {
772 + $user_id = $uid;
773 + }
774 +
775 + // check whether the user login variable contains something useful
776 + if (empty($user_id)) {
777 +
778 + pmpro_setMessage( esc_html__( 'Unable to add/update user fields for this member', 'paid-memberships-pro' ), 'pmpro_error' );
779 +
780 + return false;
781 + }
782 +
783 + $addmember_fields = array();
784 + // Loop through all the field groups.
785 + $field_groups = PMPro_Field_Group::get_all();
786 + foreach($field_groups as $group_name => $group) {
787 + // Loop through all the fields in the group.
788 + $fields = $group->get_fields();
789 + foreach($fields as $field)
790 + {
791 + if(pmpro_is_field($field) && isset($field->addmember) && !empty($field->addmember) && ( in_array( strtolower( $field->addmember ), array( 'true', 'yes' ) ) || true == $field->addmember ) )
792 + {
793 + $addmember_fields[] = $field;
794 + }
795 + }
796 + }
797 +
798 + //save our added fields in session while the user goes off to PayPal
799 + if(!empty($addmember_fields))
800 + {
801 + //cycle through fields
802 + foreach($addmember_fields as $field)
803 + {
804 + $field->save_field_for_user( $user_id );
805 + }
806 + }
807 + }
808 + add_action( 'pmpro_add_member_added', 'pmpro_add_member_admin_save_user_fields', 10, 2 );
809 +
810 + /**
811 + * Get user fields which are set to show up in the Members List CSV Export.
812 + */
813 + function pmpro_get_user_fields_for_csv() {
814 + $csv_fields = array();
815 + // Loop through all the field groups.
816 + $field_groups = PMPro_Field_Group::get_all();
817 + foreach($field_groups as $group_name => $group) {
818 + // Loop through all the fields in the group.
819 + $fields = $group->get_fields();
820 + foreach($fields as $field)
821 + {
822 + if(pmpro_is_field($field) && !empty($field->memberslistcsv) && ($field->memberslistcsv == "true"))
823 + {
824 + $csv_fields[] = $field;
825 + }
826 + }
827 + }
828 +
829 + return $csv_fields;
830 + }
831 +
832 + /**
833 + * Get user fields which are marked to show in the profile.
834 + * If a $user_id is passed in, get fields based on the user's level.
835 + *
836 + * @deprecated 3.4 Use PMPro_Field_Group::get_fields_to_display instead.
837 + */
838 + function pmpro_get_user_fields_for_profile( $user_id, $withlocations = false ) {
839 + _deprecated_function( __FUNCTION__, '3.4', 'PMPro_Field_Group::get_fields_to_display' );
840 + $profile_fields = array();
841 + // Loop through all the field groups.
842 + $field_groups = PMPro_Field_Group::get_all();
843 + foreach($field_groups as $group_name => $group) {
844 + // Get the fields to display.
845 + $fields_to_display = $group->get_fields_to_display(
846 + array(
847 + 'scope' => 'profile',
848 + 'user_id' => $user_id,
849 + )
850 + );
851 +
852 + if ( empty( $fields_to_display ) ) {
853 + continue;
854 + }
855 +
856 + if ( $withlocations ) {
857 + $profile_fields[ $group_name ] = $fields_to_display;
858 + } else {
859 + $profile_fields = array_merge( $profile_fields, $fields_to_display );
860 + }
861 + }
862 +
863 + return $profile_fields;
864 + }
865 +
866 + /**
867 + * Change the enctype of the edit user form in case files need to be uploaded.
868 + */
869 + function pmpro_user_edit_form_tag() {
870 + echo ' enctype="multipart/form-data"';
871 + }
872 + add_action( 'user_edit_form_tag', 'pmpro_user_edit_form_tag' );
873 +
874 + /**
875 + * Save profile fields.
876 + */
877 + function pmpro_save_user_fields_in_profile( $user_id )
878 + {
879 + if ( !current_user_can( 'edit_user', $user_id ) )
880 + return false;
881 +
882 + // Loop through all the field groups.
883 + $field_groups = PMPro_Field_Group::get_all();
884 + foreach($field_groups as $group_name => $group) {
885 + // Save the fields.
886 + $group->save_fields(
887 + array(
888 + 'scope' => 'profile',
889 + 'user_id' => $user_id,
890 + )
891 + );
892 + }
893 + }
894 + add_action( 'personal_options_update', 'pmpro_save_user_fields_in_profile' );
895 + add_action( 'edit_user_profile_update', 'pmpro_save_user_fields_in_profile' );
896 + add_action( 'pmpro_personal_options_update', 'pmpro_save_user_fields_in_profile' );
897 +
898 + /**
899 + * Add user fields to confirmation email.
900 + */
901 + function pmpro_add_user_fields_to_email( $email ) {
902 + global $wpdb;
903 +
904 + //only update admin confirmation emails
905 + if ( ! empty( $email ) && strpos( $email->template, "checkout" ) !== false && strpos( $email->template, "admin" ) !== false ) {
906 + //get the user_id from the email
907 + $user_id = $wpdb->get_var( "SELECT ID FROM $wpdb->users WHERE user_email = '" . esc_sql( $email->data['user_email'] ) . "' LIMIT 1" );
908 +
909 + if ( ! empty( $user_id ) ) {
910 + //add to bottom of email
911 + $field_groups = PMPro_Field_Group::get_all();
912 + if ( ! empty( $field_groups ) ) {
913 + $fields_content = "<p>" . esc_html__( 'Extra Fields:', 'paid-memberships-pro' ) . "<br />";
914 + $added_field = false;
915 + // Loop through all the field groups.
916 + foreach( $field_groups as $group_name => $group ) {
917 + // Loop through all the fields in the group.
918 + $fields = $group->get_fields_to_display(
919 + array(
920 + 'scope' => 'checkout',
921 + 'user_id' => $user_id,
922 + )
923 + );
924 + foreach( $fields as $field ) {
925 + $fields_content .= "- " . esc_html( $field->label ) . ": ";
926 + $fields_content .= $field->displayValue( get_user_meta( $user_id, $field->name, true), false );
927 + $fields_content .= "<br />";
928 + $added_field = true;
929 + }
930 + }
931 + $fields_content .= "</p>";
932 + if ( $added_field ) {
933 + $email->body .= $fields_content;
934 + }
935 + }
936 + }
937 + }
938 +
939 + return $email;
940 + }
941 + add_filter( 'pmpro_email_filter', 'pmpro_add_user_fields_to_email', 10, 2 );
942 +
943 + /**
944 + * Add CSV fields to the Member's List CSV Export.
945 + */
946 + function pmpro_members_list_csv_extra_columns_for_user_fields($columns)
947 + {
948 + $csv_cols = pmpro_get_user_fields_for_csv();
949 + foreach($csv_cols as $key => $value)
950 + {
951 + $columns[$value->meta_key] = "pmpro_csv_columns_for_user_fields";
952 + }
953 +
954 + return $columns;
955 + }
956 + add_filter( 'pmpro_members_list_csv_extra_columns', 'pmpro_members_list_csv_extra_columns_for_user_fields', 10 );
957 +
958 + /**
959 + * Get user meta for the added CSV columns.
960 + */
961 + function pmpro_csv_columns_for_user_fields( $user, $column ) {
962 + if(!empty($user->metavalues->{$column}))
963 + {
964 + // check for multiple values
965 + $value = maybe_unserialize($user->metavalues->{$column});
966 + if(is_array($value))
967 + $value = join(',', $value);
968 +
969 + return $value;
970 + }
971 + else
972 + {
973 + return "";
974 + }
975 + }
976 +
977 + /**
978 + * Get user fields from global.
979 + * @since 2.9.3
980 + * @deprecated 3.4
981 + */
982 + function pmpro_get_user_fields() {
983 + _deprecated_function( __FUNCTION__, '3.4' );
984 +
985 + global $pmpro_user_fields;
986 +
987 + return (array)$pmpro_user_fields;
988 + }
989 +
990 + // Code for the user fields settings page.
991 + /**
992 + * Get field group HTML for settings.
993 + */
994 + function pmpro_get_field_group_html( $group = null ) {
995 + include( PMPRO_DIR . '/adminpages/user-fields/group-settings.php' );
996 + }
997 +
998 + /**
999 + * Get field HTML for settings.
1000 + */
1001 + function pmpro_get_field_html( $field = null ) {
1002 + include( PMPRO_DIR . '/adminpages/user-fields/field-settings.php' );
1003 + }
1004 +
1005 + /**
1006 + * Get user fields from options.
1007 + *
1008 + * This function will not return fields that are added through code.
1009 + */
1010 + function pmpro_get_user_fields_settings() {
1011 + $default_user_fields_settings = array(
1012 + (object) array(
1013 + 'name' => esc_html__( 'More Information', 'paid-memberships-pro' ),
1014 + 'checkout' => 'yes',
1015 + 'profile' => 'yes',
1016 + 'description' => '',
1017 + 'levels' => array(),
1018 + 'fields' => array(),
1019 + )
1020 + );
1021 +
1022 + $settings = get_option( 'pmpro_user_fields_settings', $default_user_fields_settings );
1023 +
1024 + // Make sure all expected properties are set for each group.
1025 + foreach ( $settings as $group ) {
1026 + $group->name = ! empty( $group->name ) ? $group->name : '';
1027 + $group->checkout = ! empty( $group->checkout ) ? $group->checkout : 'yes';
1028 + $group->profile = ! empty( $group->profile ) ? $group->profile : 'yes';
1029 + $group->description = ! empty( $group->description ) ? $group->description : '';
1030 + $group->levels = ! empty( $group->levels ) ? $group->levels : array();
1031 + $group->fields = ! empty( $group->fields ) ? $group->fields : array();
1032 +
1033 + // Make sure all expected properties are set for each field in the group.
1034 + foreach( $group->fields as $field ) {
1035 + $field->label = ! empty( $field->label ) ? $field->label : '';
1036 + $field->name = ! empty( $field->name ) ? $field->name : '';
1037 + $field->type = ! empty( $field->type ) ? $field->type : '';
1038 + $field->required = ! empty( $field->required ) ? $field->required : false;
1039 + $field->readonly = ! empty( $field->readonly ) ? $field->readonly : false;
1040 + $field->profile = ! empty( $field->profile ) ? $field->profile : '';
1041 + $field->wrapper_class = ! empty( $field->wrapper_class ) ? $field->wrapper_class : '';
1042 + $field->element_class = ! empty( $field->element_class ) ? $field->element_class : '';
1043 + $field->hint = ! empty( $field->hint ) ? $field->hint : '';
1044 + $field->options = ! empty( $field->options ) ? $field->options : '';
1045 + $field->default = ! empty( $field->default ) ? $field->default : '';
1046 + $field->allowed_file_types = ! empty( $field->allowed_file_types ) ? $field->allowed_file_types : '';
1047 + $field->max_file_size = ! empty( $field->max_file_size ) ? $field->max_file_size : '';
1048 + }
1049 + }
1050 +
1051 + return $settings;
1052 + }
1053 +
1054 + /**
1055 + * Load user field settings into the fields global var.
1056 + */
1057 + function pmpro_load_user_fields_from_settings() {
1058 + $settings_groups = pmpro_get_user_fields_settings();
1059 +
1060 + foreach ( $settings_groups as $group ) {
1061 + $group_obj = PMPro_Field_Group::add( $group->name, $group->name, $group->description );
1062 +
1063 + // Figure out profile value. Change 2 settings values into 1 field value.
1064 + if ( $group->checkout === 'yes' ) {
1065 + if ( $group->profile === 'yes' ) {
1066 + $group_profile = true;
1067 + } elseif ( $group->profile === 'admins' ) {
1068 + $group_profile = 'admin';
1069 + } else {
1070 + $group_profile = false;
1071 + }
1072 + } else {
1073 + if ( $group->profile === 'yes' ) {
1074 + $group_profile = 'only';
1075 + } elseif ( $group->profile === 'admins' ) {
1076 + $group_profile = 'only_admin';
1077 + } else {
1078 + // Hide from checkout AND profile? Okay, skip this group.
1079 + continue;
1080 + }
1081 + }
1082 +
1083 + foreach ( $group->fields as $settings_field ) {
1084 + // Figure out field profile from settings and group profile.
1085 + if ( empty( $settings_field->profile ) || $settings_field->profile === '[Inherit Group Setting]' ) {
1086 + $profile = $group_profile;
1087 + } else {
1088 + if ( $settings_field->profile === 'yes' ) {
1089 + $profile = true;
1090 + } elseif ( $settings_field->profile === 'no' ) {
1091 + $profile = false;
1092 + } elseif ( $settings_field->profile === 'admins' ) {
1093 + $profile = 'admin';
1094 + } else {
1095 + // default to no
1096 + $profile = false;
1097 + }
1098 + }
1099 +
1100 + // Figure out options.
1101 + $option_types = array( 'checkbox_grouped', 'radio', 'select', 'select2', 'multiselect' );
1102 + if ( in_array( $settings_field->type, $option_types ) ) {
1103 + $options = array();
1104 + $settings_options = explode( "\n", $settings_field->options );
1105 + foreach( $settings_options as $settings_option ) {
1106 + if ( strpos( $settings_option, ':' ) !== false ) {
1107 + $parts = explode( ':', $settings_option );
1108 + $options[trim( $parts[0] )] = trim( $parts[1] );
1109 + } else {
1110 + $options[] = trim( $settings_option );
1111 + }
1112 + }
1113 + } else {
1114 + $options = false;
1115 + }
1116 +
1117 + // Set field levels based on group.
1118 + $levels = $group->levels;
1119 +
1120 + $field = new PMPro_Field(
1121 + $settings_field->name,
1122 + $settings_field->type,
1123 + array(
1124 + 'label' => $settings_field->label,
1125 + 'required' => filter_var( $settings_field->required, FILTER_VALIDATE_BOOLEAN ),
1126 + 'readonly' => filter_var( $settings_field->readonly, FILTER_VALIDATE_BOOLEAN ),
1127 + 'profile' => $profile,
1128 + 'class' => $settings_field->element_class,
1129 + 'divclass' => $settings_field->wrapper_class,
1130 + 'hint' => $settings_field->hint,
1131 + 'options' => $options,
1132 + 'levels' => $levels,
1133 + 'memberslistcsv' => true,
1134 + 'allowed_file_types' => $settings_field->allowed_file_types,
1135 + 'max_file_size' => $settings_field->max_file_size,
1136 + 'default' => $settings_field->default,
1137 + )
1138 + );
1139 + $group_obj->add_field( $field );
1140 + }
1141 + }
1142 + }
1143 + add_action( 'init', 'pmpro_load_user_fields_from_settings', 1 );
1144 +
1145 + /**
1146 + * Check if user is adding custom user fields with code.
1147 + *
1148 + * @since 2.9
1149 + * @deprecated 3.4
1150 + *
1151 + * @return bool True if user is adding custom user fields with code.
1152 + */
1153 + function pmpro_has_coded_user_fields() {
1154 + _deprecated_function( __FUNCTION__, '3.4' );
1155 + global $pmprorh_registration_fields;
1156 +
1157 + // Check if coded fields are being added using the PMPro Register Helper Add On active.
1158 + if ( ! empty( $pmprorh_registration_fields ) ) {
1159 + return true;
1160 + }
1161 +
1162 + // Check if coded fields are being added using the PMPro Register Helper Add On inactive.
1163 + $num_fields_from_settings = array_sum( array_map( function ($group) { return count( $group->fields ); }, pmpro_get_user_fields_settings() ) ); // Fields from UI settings page.
1164 + $total_registered_fields = array_sum( array_map( function ($group) { return count( $group->get_fields() ); }, PMPro_Field_Group::get_all() ) ); // All registered fields.
1165 + return $total_registered_fields > $num_fields_from_settings;
1166 + }
1167 +
1168 + /**
1169 + * Gets the label(s) for a passed user field value.
1170 + *
1171 + * @since 2.11
1172 + * @deprecated 3.4 Use PMProField::displayValue instead.
1173 + *
1174 + * @param string $field_name The name of the field that the value belongs to.
1175 + * @param string|array $field_value The value to get the label for.
1176 + *
1177 + * @return string|array The label(s) for the passed value. Will be same type as $field_value.
1178 + */
1179 + function pmpro_get_label_for_user_field_value( $field_name, $field_value ) {
1180 + _deprecated_function( __FUNCTION__, '3.4', 'PMProField::displayValue' );
1181 +
1182 + // Loop through all the field groups.
1183 + $field_groups = PMPro_Field_Group::get_all();
1184 + foreach($field_groups as $group_name => $group) {
1185 + // Loop through all the fields in the group.
1186 + $fields = $group->get_fields();
1187 + foreach( $fields as $user_field ) {
1188 + // Check if this is the user field that we are displaying.
1189 + if ( $user_field->name !== $field_name ) {
1190 + continue;
1191 + }
1192 +
1193 + // Make sure that we have a valid user field.
1194 + if ( ! pmpro_is_field( $user_field ) ) {
1195 + continue;
1196 + }
1197 +
1198 + // Check if this is the user field that we are displaying.
1199 + if ( empty( $user_field->options ) ) {
1200 + continue;
1201 + }
1202 +
1203 + // Make sure that $options is an array.
1204 + if ( ! is_array( $user_field->options ) ) {
1205 + continue;
1206 + }
1207 +
1208 + // Replace meta values with their corresponding labels.
1209 + $field_value = $user_field->displayValue( $field_value, false );
1210 + }
1211 + }
1212 + return $field_value;
1213 + }
1214 +
1215 + /**
1216 + * Get a single user field.
1217 + * @since 3.0
1218 + * @deprecated 3.4
1219 + * @param string $field_name The name of the field to get.
1220 + * @return bool|object The field object if found, false otherwise.
1221 + */
1222 + function pmpro_get_user_field( $field_name ) {
1223 + _deprecated_function( __FUNCTION__, '3.4', 'PMPro_Field_Group::get_field' );
1224 + $field = PMPro_Field_Group::get_field( $field_name );
1225 + return empty( $field ) ? false : $field;
1226 + }
1227 +