$fields)
{
//cycle through fields
foreach($fields as $field)
{
if( ! pmpro_is_field( $field ) ) {
continue;
}
if ( ! pmpro_check_field_for_level( $field, "profile", $user_id ) ) {
continue;
}
if(!empty($field->profile) && ($field->profile === "only" || $field->profile === "only_admin")) {
continue; //wasn't shown at checkout
}
//assume no value
$value = NULL;
// Where are we getting the value from? We sanitize $value right after this.
// phpcs:disable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
if(isset($_REQUEST[$field->name]))
{
//request
$value = $_REQUEST[$field->name];
}
elseif(isset($_REQUEST[$field->name . '_checkbox']) && $field->type == 'checkbox')
{
//unchecked checkbox
$value = 0;
}
elseif(!empty($_POST[$field->name . "_checkbox"]) && in_array( $field->type, array( 'checkbox', 'checkbox_grouped', 'select2' ) ) ) //handle unchecked checkboxes
{
//unchecked checkbox
$value = array();
}
elseif(isset($_SESSION[$field->name]))
{
//file or value?
if(is_array($_SESSION[$field->name]) && isset($_SESSION[$field->name]['name']))
{
//add to files global
$_FILES[$field->name] = $_SESSION[$field->name];
//set value to name
$value = $_SESSION[$field->name]['name'];
}
else
{
//session
$value = $_SESSION[$field->name];
}
//unset
unset($_SESSION[$field->name]);
}
elseif(isset($_FILES[$field->name]))
{
//file
$value = $_FILES[$field->name]['name'];
}
// phpcs:enable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
//update user meta
if(isset($value))
{
if ( ! empty( $field->sanitize ) ) {
$value = pmpro_sanitize( $value, $field );
}
//callback?
if(!empty($field->save_function))
call_user_func( $field->save_function, $user_id, $field->name, $value, $order );
else
update_user_meta($user_id, $field->meta_key, $value);
}
}
}
}
}
add_action( 'pmpro_after_checkout', 'pmpro_after_checkout_save_fields', 10, 2 );
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
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
add_action( 'pmpro_before_send_to_gourl', 'pmpro_after_checkout_save_fields', 20, 2 ); //for the GoURL Bitcoin Gateway Add On
add_action( 'pmpro_before_send_to_payfast', 'pmpro_after_checkout_save_fields', 20, 2 ); //for the Payfast Gateway Add On
/**
* Require required fields.
*/
function pmpro_registration_checks_for_user_fields( $okay ) {
global $current_user;
//arrays to store fields that were required and missed
$required = array();
$required_labels = array();
//any fields?
global $pmpro_user_fields;
if(!empty($pmpro_user_fields))
{
//cycle through groups
foreach($pmpro_user_fields as $where => $fields)
{
//cycle through fields
foreach($fields as $field)
{
//handle arrays
$field->name = preg_replace('/\[\]$/', '', $field->name);
//if the field is not for this level, skip it
if( ! pmpro_is_field( $field ) ) {
continue;
}
if ( ! pmpro_check_field_for_level( $field ) ) {
continue;
}
if(!empty($field->profile) && ($field->profile === "only" || $field->profile === "only_admin")) {
continue; //wasn't shown at checkout
}
// If this is a file upload, check whether the file is allowed.
if ( isset( $_FILES[ $field->name ] ) && ! empty( $_FILES[$field->name]['name'] ) ) {
$upload_check = pmpro_check_upload( $field->name );
if ( is_wp_error( $upload_check ) ) {
pmpro_setMessage( $upload_check->get_error_message(), 'pmpro_error' );
return false;
}
}
if( ! $field->was_filled_if_needed() ) {
$required[] = $field->name;
$required_labels[] = $field->label;
}
}
}
}
if(!empty($required))
{
$required = array_unique($required);
//add them to error fields
global $pmpro_error_fields;
$pmpro_error_fields = array_merge((array)$pmpro_error_fields, $required);
if( count( $required ) == 1 ) {
$pmpro_msg = sprintf( __( 'The %s field is required.', 'paid-memberships-pro' ), implode(", ", $required_labels) );
$pmpro_msgt = 'pmpro_error';
} else {
$pmpro_msg = sprintf( __( 'The %s fields are required.', 'paid-memberships-pro' ), implode(", ", $required_labels) );
$pmpro_msgt = 'pmpro_error';
}
if($okay)
pmpro_setMessage($pmpro_msg, $pmpro_msgt);
return false;
}
//return whatever status was before
return $okay;
}
add_filter( 'pmpro_registration_checks', 'pmpro_registration_checks_for_user_fields' );
/**
* Sessions vars for TwoCheckout. PayPal Express was updated to store in order meta.
*
* @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.
*/
function pmpro_paypalexpress_session_vars_for_user_fields() {
global $pmpro_user_fields;
_deprecated_function( __FUNCTION__, '2.12.4', 'pmpro_after_checkout_save_fields' );
//save our added fields in session while the user goes off to PayPal
if(!empty($pmpro_user_fields))
{
//cycle through groups
foreach($pmpro_user_fields as $where => $fields)
{
//cycle through fields
foreach($fields as $field)
{
if( ! pmpro_is_field( $field ) ) {
continue;
}
if ( ! pmpro_check_field_for_level( $field ) ) {
continue;
}
if( isset( $_REQUEST[$field->name] ) ) {
$_SESSION[$field->name] = pmpro_sanitize( $_REQUEST[$field->name], $field ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
} elseif ( isset( $_FILES[$field->name] ) ) {
/*
We need to save the file somewhere and save values in $_SESSION
*/
// Make sure the file is allowed.
$upload_check = pmpro_check_upload( $field->name );
if ( is_wp_error( $upload_check ) ) {
continue;
}
// Get $file and $filetype.
$file = array_map( 'sanitize_text_field', $_FILES[ $field->name ] );
$filetype = wp_check_filetype_and_ext( $file['tmp_name'], $file['name'] );
// Make sure file was uploaded during this page load.
if ( ! is_uploaded_file( sanitize_text_field( $file['tmp_name'] ) ) ) {
continue;
}
//check for a register helper directory in wp-content
$upload_dir = wp_upload_dir();
$pmprorh_dir = $upload_dir['basedir'] . "/pmpro-register-helper/tmp/";
//create the dir and subdir if needed
if(!is_dir($pmprorh_dir))
{
wp_mkdir_p($pmprorh_dir);
}
//move file
$new_filename = $pmprorh_dir . basename( sanitize_file_name( $file['name'] ) );
move_uploaded_file( sanitize_text_field( $$file['tmp_name'] ), $new_filename );
//update location of file
$_FILES[$field->name]['tmp_name'] = $new_filename;
//save file info in session
$_SESSION[$field->name] = array_map( 'sanitize_text_field', $file );
}
}
}
}
}
/**
* Show user fields in profile.
*/
function pmpro_show_user_fields_in_profile( $user, $withlocations = false ) {
global $pmpro_user_fields;
//which fields are marked for the profile
$profile_fields = pmpro_get_user_fields_for_profile($user->ID, $withlocations);
//show the fields
if(!empty($profile_fields) && $withlocations)
{
foreach($profile_fields as $where => $fields)
{
$box = pmpro_get_field_group_by_name($where);
if ( !empty($box->label) ) {
?>
label ); ?>
description ) ) {
?>
description ); ?>
displayInProfile($user->ID);
}
?>
displayInProfile($user->ID);
}
}
?>
ID, $withlocations);
//show the fields
if ( ! empty( $profile_fields ) && $withlocations ) {
foreach( $profile_fields as $where => $fields ) {
$box = pmpro_get_field_group_by_name( $where );
// Only show on front-end if there are fields to be shown.
$show_fields = array();
foreach( $fields as $key => $field ) {
if ( pmpro_is_field( $field ) && $field->profile !== 'only_admin' && $field->profile !== 'admin' && $field->profile !== 'admins' ) {
$show_fields[] = $field;
}
}
// Bail if there are no fields to show on the front-end profile.
if ( empty( $show_fields ) ) {
continue;
}
?>
$fields)
{
//cycle through fields
foreach($fields as $field)
{
if(pmpro_is_field($field) && isset($field->addmember) && !empty($field->addmember) && ( in_array( strtolower( $field->addmember ), array( 'true', 'yes' ) ) || true == $field->addmember ) )
{
$addmember_fields[] = $field;
}
}
}
}
//show the fields
if(!empty($addmember_fields))
{
?>
ID)) {
$user_id = $user->ID;
}
if( pmpro_is_field( $field ) ) {
$field->displayInProfile($user_id);
}
}
?>
ID;
}
// Otherwise, let's use the $uid passed in.
if ( !empty( $uid ) && ( empty( $user ) || !is_object( $user ) ) ) {
$user_id = $uid;
}
// check whether the user login variable contains something useful
if (empty($user_id)) {
pmpro_setMessage( __( 'Unable to add/update user fields for this member', 'paid-memberships-pro' ), 'pmpro_error' );
return false;
}
$addmember_fields = array();
if(!empty($pmpro_user_fields))
{
//cycle through groups
foreach($pmpro_user_fields as $where => $fields)
{
//cycle through fields
foreach($fields as $field)
{
if(pmpro_is_field($field) && isset($field->addmember) && !empty($field->addmember) && ( in_array( strtolower( $field->addmember ), array( 'true', 'yes' ) ) || true == $field->addmember ) )
{
$addmember_fields[] = $field;
}
}
}
}
//save our added fields in session while the user goes off to PayPal
if(!empty($addmember_fields))
{
//cycle through fields
foreach($addmember_fields as $field)
{
if(pmpro_is_field($field) && isset($_POST[$field->name]) || isset($_FILES[$field->name]))
{
// Sanitize by default, or not. Some fields may have custom save functions/etc.
// phpcs:disable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
if ( ! empty( $field->sanitize ) && isset( $_POST[ $field->name ] ) ) {
$value = pmpro_sanitize( $_POST[ $field->name ], $field );
} elseif( isset($_POST[$field->name]) ) {
$value = $_POST[ $field->name ];
} else {
$value = $_FILES[$field->name];
}
// phpcs:enable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
//callback?
if(!empty($field->save_function))
call_user_func($field->save_function, $user_id, $field->name, $value );
else
update_user_meta($user_id, $field->meta_key, $value );
}
elseif(pmpro_is_field($field) && !empty($_POST[$field->name . "_checkbox"]) && $field->type == 'checkbox') //handle unchecked checkboxes
{
//callback?
if(!empty($field->save_function))
call_user_func($field->save_function, $user_id, $field->name, 0);
else
update_user_meta($user_id, $field->meta_key, 0);
}
elseif(!empty($_POST[$field->name . "_checkbox"]) && in_array( $field->type, array( 'checkbox', 'checkbox_grouped', 'select2' ) ) ) //handle unchecked checkboxes
{
//callback?
if(!empty($field->save_function))
call_user_func($field->save_function, $user_id, $field->name, array());
else
update_user_meta($user_id, $field->meta_key, array());
}
}
}
}
add_action( 'pmpro_add_member_added', 'pmpro_add_member_admin_save_user_fields', 10, 2 );
/**
* Get RH fields which are set to showup in the Members List CSV Export.
*/
function pmpro_get_user_fields_for_csv() {
global $pmpro_user_fields;
$csv_fields = array();
if(!empty($pmpro_user_fields))
{
//cycle through groups
foreach($pmpro_user_fields as $where => $fields)
{
//cycle through fields
foreach($fields as $field)
{
if(pmpro_is_field($field) && !empty($field->memberslistcsv) && ($field->memberslistcsv == "true"))
{
$csv_fields[] = $field;
}
}
}
}
return $csv_fields;
}
/**
* Get user fields which are marked to show in the profile.
* If a $user_id is passed in, get fields based on the user's level.
*/
function pmpro_get_user_fields_for_profile( $user_id, $withlocations = false ) {
global $pmpro_user_fields;
$profile_fields = array();
if(!empty($pmpro_user_fields))
{
//cycle through groups
foreach($pmpro_user_fields as $where => $fields)
{
//cycle through fields
foreach($fields as $field)
{
if( ! pmpro_is_field( $field ) ) {
continue;
}
if ( ! pmpro_check_field_for_level( $field, "profile", $user_id ) ) {
continue;
}
if(!empty($field->profile) && ($field->profile === "admins" || $field->profile === "admin" || $field->profile === "only_admin"))
{
if( current_user_can( 'manage_options' ) || current_user_can( 'pmpro_membership_manager' ) )
{
if($withlocations)
$profile_fields[$where][] = $field;
else
$profile_fields[] = $field;
}
}
elseif(!empty($field->profile))
{
if($withlocations)
$profile_fields[$where][] = $field;
else
$profile_fields[] = $field;
}
}
}
}
return $profile_fields;
}
/**
* Change the enctype of the edit user form in case files need to be uploaded.
*/
function pmpro_user_edit_form_tag() {
echo ' enctype="multipart/form-data"';
}
add_action( 'user_edit_form_tag', 'pmpro_user_edit_form_tag' );
/**
* Save profile fields.
*/
function pmpro_save_user_fields_in_profile( $user_id )
{
if ( !current_user_can( 'edit_user', $user_id ) )
return false;
$profile_fields = pmpro_get_user_fields_for_profile($user_id);
//save our added fields in session while the user goes off to PayPal
if(!empty($profile_fields))
{
//cycle through fields
foreach($profile_fields as $field)
{
if( ! pmpro_is_field( $field ) ) {
continue;
}
if(isset($_POST[$field->name]) || isset($_FILES[$field->name]))
{
// Sanitize by default, or not. Some fields may have custom save functions/etc.
// phpcs:disable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
if ( ! empty( $field->sanitize ) && isset( $_POST[ $field->name ] ) ) {
$value = pmpro_sanitize( $_POST[ $field->name ], $field );
} elseif( isset($_POST[$field->name]) ) {
$value = $_POST[ $field->name ];
} else {
$value = $_FILES[$field->name];
}
// phpcs:enable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
//callback?
if(!empty($field->save_function))
call_user_func($field->save_function, $user_id, $field->name, $value);
else
update_user_meta($user_id, $field->meta_key, $value);
}
elseif(!empty($_POST[$field->name . "_checkbox"]) && $field->type == 'checkbox') //handle unchecked checkboxes
{
//callback?
if(!empty($field->save_function))
call_user_func($field->save_function, $user_id, $field->name, 0);
else
update_user_meta($user_id, $field->meta_key, 0);
}
elseif(!empty($_POST[$field->name . "_checkbox"]) && in_array( $field->type, array( 'checkbox', 'checkbox_grouped', 'select2' ) ) ) //handle unchecked checkboxes
{
//callback?
if(!empty($field->save_function))
call_user_func($field->save_function, $user_id, $field->name, array());
else
update_user_meta($user_id, $field->meta_key, array());
}
}
}
}
add_action( 'personal_options_update', 'pmpro_save_user_fields_in_profile' );
add_action( 'edit_user_profile_update', 'pmpro_save_user_fields_in_profile' );
add_action( 'pmpro_personal_options_update', 'pmpro_save_user_fields_in_profile' );
/**
* Add user fields to confirmation email.
*/
function pmpro_add_user_fields_to_email( $email ) {
global $wpdb, $pmpro_user_fields, $pmpro_field_groups;
//only update admin confirmation emails
if ( ! empty( $email ) && strpos( $email->template, "checkout" ) !== false && strpos( $email->template, "admin" ) !== false ) {
//get the user_id from the email
$user_id = $wpdb->get_var( "SELECT ID FROM $wpdb->users WHERE user_email = '" . esc_sql( $email->data['user_email'] ) . "' LIMIT 1" );
$level_id = empty( $email->data['membership_id'] ) ? null : intval( $email->data['membership_id'] );
if ( ! empty( $user_id ) ) {
//add to bottom of email
if ( ! empty( $pmpro_field_groups ) ) {
$fields_content = "
" . __( 'Extra Fields:', 'paid-memberships-pro' ) . " ";
$added_field = false;
//cycle through groups
foreach( $pmpro_field_groups as $group ) {
// Get the groups name so we can grab it from the associative array.
$group_name = $group->name;
// Skip if there are no fields in this group.
if ( empty( $pmpro_user_fields[$group_name] ) ) {
continue;
}
//cycle through groups and fields associated with that group.
foreach( $pmpro_user_fields[$group_name] as $field ) {
if ( ! pmpro_is_field( $field ) ) {
continue;
}
// If the field is showing only in the profile or to admins we can skip it.
if ( ! empty( $field->profile ) && ( $field->profile === "only" || $field->profile === "only_admin" ) ) {
continue;
}
// Let's make sure the field level ID's are the same as the one they checked out for.
if ( ! empty( $field->levels ) && ( empty( $level_id) || ! in_array( $level_id, $field->levels ) ) ) {
continue;
}
$fields_content .= "- " . esc_html( $field->label ) . ": ";
$value = get_user_meta( $user_id, $field->name, true);
// Get the label value for field types that have labels.
$value = pmpro_get_label_for_user_field_value( $field->name, $value );
if ( $field->type == "file" && is_array( $value ) && ! empty( $value['fullurl'] ) ) {
$fields_content .= pmpro_sanitize( $value['fullurl'], $field );
} elseif( is_array( $value ) ) {
$fields_content .= implode(", ", pmpro_sanitize( $value, $field ) );
} else {
$fields_content .= pmpro_sanitize( $value, $field );
}
$fields_content .= " ";
$added_field = true;
}
}
$fields_content .= "
";
if ( $added_field ) {
$email->body .= $fields_content;
}
}
}
}
return $email;
}
add_filter( 'pmpro_email_filter', 'pmpro_add_user_fields_to_email', 10, 2 );
/**
* Add CSV fields to the Member's List CSV Export.
*/
function pmpro_members_list_csv_extra_columns_for_user_fields($columns)
{
$csv_cols = pmpro_get_user_fields_for_csv();
foreach($csv_cols as $key => $value)
{
$columns[$value->meta_key] = "pmpro_csv_columns_for_user_fields";
}
return $columns;
}
add_filter( 'pmpro_members_list_csv_extra_columns', 'pmpro_members_list_csv_extra_columns_for_user_fields', 10 );
/**
* Get user meta for the added CSV columns.
*/
function pmpro_csv_columns_for_user_fields( $user, $column ) {
if(!empty($user->metavalues->{$column}))
{
// check for multiple values
$value = maybe_unserialize($user->metavalues->{$column});
if(is_array($value))
$value = join(',', $value);
return $value;
}
else
{
return "";
}
}
/**
* Delete old files in wp-content/uploads/pmpro-register-helper/tmp every day.
*/
function pmpro_cron_delete_tmp() {
$upload_dir = wp_upload_dir();
$pmprorh_dir = $upload_dir['basedir'] . "/paid-memberships-pro/tmp/";
if(file_exists($pmprorh_dir) && $handle = opendir($pmprorh_dir))
{
while(false !== ($file = readdir($handle)))
{
$file = $pmprorh_dir . $file;
$filelastmodified = filemtime($file);
if(is_file($file) && (time() - $filelastmodified) > 3600)
{
unlink($file);
}
}
closedir($handle);
}
exit;
}
add_action( 'pmpro_cron_delete_tmp', 'pmpro_cron_delete_tmp' );
/**
* Get user fields from global.
* @since 2.9.3
*/
function pmpro_get_user_fields() {
global $pmpro_user_fields;
return (array)$pmpro_user_fields;
}
// Code for the user fields settings page.
/**
* Get field group HTML for settings.
*/
function pmpro_get_field_group_html( $group = null ) {
if ( ! empty( $group ) ) {
// Assume group stdClass in format we save to settings.
$group_name = $group->name;
$group_show_checkout = $group->checkout;
$group_show_profile = $group->profile;
$group_description = $group->description;
$group_levels = $group->levels;
$group_fields = $group->fields;
} else {
// Default group settings.
$group_name = '';
$group_show_checkout = 'yes';
$group_show_profile = 'yes';
$group_description = '';
$group_levels = array();
$group_fields = array();
}
// Other vars
$levels = pmpro_sort_levels_by_order( pmpro_getAllLevels( true, true ) );
// Render field group HTML.
?>
__( 'More Information', 'paid-memberships-pro' ),
'checkout' => 'yes',
'profile' => 'yes',
'description' => '',
'levels' => array(),
'fields' => array(),
)
);
$settings = get_option( 'pmpro_user_fields_settings', $default_user_fields_settings );
// TODO: Might want to validate the format the settings are in here.
return $settings;
}
/**
* Load user field settings into the fields global var.
*/
function pmpro_load_user_fields_from_settings() {
global $pmpro_user_fields, $pmpro_field_groups;
$settings_groups = pmpro_get_user_fields_settings();
foreach ( $settings_groups as $group ) {
pmpro_add_field_group( $group->name, $group->name, $group->description );
// Figure out profile value. Change 2 settings values into 1 field value.
if ( $group->checkout === 'yes' ) {
if ( $group->profile === 'yes' ) {
$group_profile = true;
} elseif ( $group->profile === 'admins' ) {
$group_profile = 'admin';
} else {
$group_profile = false;
}
} else {
if ( $group->profile === 'yes' ) {
$group_profile = 'only';
} elseif ( $group->profile === 'admins' ) {
$group_profile = 'only_admin';
} else {
// Hide from checkout AND profile? Okay, skip this group.
continue;
}
}
foreach ( $group->fields as $settings_field ) {
// Figure out field profile from settings and group profile.
if ( empty( $settings_field->profile ) || $settings_field->profile === '[Inherit Group Setting]' ) {
$profile = $group_profile;
} else {
if ( $settings_field->profile === 'yes' ) {
$profile = true;
} elseif ( $settings_field->profile === 'no' ) {
$profile = false;
} elseif ( $settings_field->profile === 'admins' ) {
$profile = 'admin';
} else {
// default to no
$profile = false;
}
}
// Figure out options.
$option_types = array( 'radio', 'select', 'select2', 'multiselect' );
if ( in_array( $settings_field->type, $option_types ) ) {
$options = array();
$settings_options = explode( "\n", $settings_field->options );
foreach( $settings_options as $settings_option ) {
if ( strpos( $settings_option, ':' ) !== false ) {
$parts = explode( ':', $settings_option );
$options[trim( $parts[0] )] = trim( $parts[1] );
} else {
$options[] = $settings_option;
}
}
} else {
$options = false;
}
// Set field levels based on group.
$levels = $group->levels;
$field = new PMPro_Field(
$settings_field->name,
$settings_field->type,
array(
'label' => $settings_field->label,
'required' => filter_var( $settings_field->required, FILTER_VALIDATE_BOOLEAN ),
'readonly' => filter_var( $settings_field->readonly, FILTER_VALIDATE_BOOLEAN ),
'profile' => $profile,
'class' => $settings_field->element_class,
'divclass' => $settings_field->wrapper_class,
'hint' => $settings_field->hint,
'options' => $options,
'levels' => $levels,
'memberslistcsv' => true,
)
);
pmpro_add_user_field( $group->name, $field );
}
}
}
add_action( 'init', 'pmpro_load_user_fields_from_settings', 1 );
/**
* Check if user is adding custom user fields with code.
*
* @since 2.9
*
* @return bool True if user is adding custom user fields with code.
*/
function pmpro_has_coded_user_fields() {
global $pmpro_user_fields, $pmprorh_registration_fields;
// Check if coded fields are being added using the PMPro Register Helper Add On active.
if ( ! empty( $pmprorh_registration_fields ) ) {
return true;
}
// Check if coded fields are being added using the PMPro Register Helper Add On inactive.
$num_db_fields = array_sum( array_map( function ($group) { return count( $group->fields ); }, pmpro_get_user_fields_settings() ) ); // Fields from UI settings page.
$num_global_fields = array_sum( array_map( 'count', $pmpro_user_fields ) ); // Total loaded fields.
return $num_global_fields > $num_db_fields;
}
/**
* Gets the label(s) for a passed user field value.
*
* @since 2.11
*
* @param string $field_name The name of the field that the value belongs to.
* @param string|array $field_value The value to get the label for.
*
* @return string|array The label(s) for the passed value. Will be same type as $field_value.
*/
function pmpro_get_label_for_user_field_value( $field_name, $field_value ) {
global $pmpro_user_fields;
foreach ( $pmpro_user_fields as $user_field_group ) { // Loop through each user field group.
foreach ( $user_field_group as $user_field ) { // Loop through each user field in the group.
// Check if this is the user field that we are displaying.
if ( $user_field->name !== $field_name ) {
continue;
}
// Make sure that we have a valid user field.
if ( ! pmpro_is_field( $user_field ) ) {
continue;
}
// Check if this is the user field that we are displaying.
if ( empty( $user_field->options ) ) {
continue;
}
// Make sure that $options is an array.
if ( ! is_array( $user_field->options ) ) {
continue;
}
// Replace meta values with their corresponding labels.
if ( is_array( $field_value ) ) {
foreach ( $field_value as $key => $value ) {
if ( isset( $user_field->options[ $value ] ) ) {
$field_value[ $key ] = $user_field->options[ $value ];
}
}
} else {
if ( isset( $user_field->options[ $field_value ] ) ) {
$field_value = $user_field->options[ $field_value ];
}
}
}
}
return $field_value;
}
/**
* Get a single field from the global $pmpro_user_fields array.
* @since 3.0
* @param string $field_name The name of the field to get.
* @return bool|object The field object if found, false otherwise.
*/
function pmpro_get_user_field( $field_name ) {
global $pmpro_user_fields;
if ( empty( $pmpro_user_fields ) ) {
return false;
}
foreach ( $pmpro_user_fields as $group ) {
foreach ( $group as $field ) {
if ( $field->name === $field_name ) {
return $field;
}
}
}
return false;
}