Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Listing coded user fields instead of showing a generic notice #2974

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 50 additions & 10 deletions adminpages/userfields.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,6 @@
*/
require_once( dirname(__FILE__) . '/admin_header.php' );

// Show warning if there are additional fields that are coded.
if ( pmpro_has_coded_user_fields() ) {
?>
<div class="notice notice-warning">
<p><?php esc_html_e( 'This website has additional user fields that are set up with code. Coded fields cannot be edited here and will show in addition to the fields set up on this page.', 'paid-memberships-pro' ); ?></p>
</div>
<?php
}

/**
* Meta boxes for User Fields admin page.
*
Expand All @@ -86,6 +77,15 @@
'memberships_page_pmpro-userfields',
'side'
);
if ( pmpro_has_coded_user_fields() ) {
add_meta_box(
'pmpro_userfields_coded_fields',
esc_html( 'Coded Fields', 'paid-memberships-pro' ),
'pmpro_userfields_coded_fields_widget',
'memberships_page_pmpro-userfields',
'side',
);
}

/**
* Meta box to show a save button and other data.
Expand All @@ -109,6 +109,47 @@ function pmpro_userfields_help_widget() { ?>
<?php
}

/**
* Meta box to show coded fields.
*
* @since TBD
*/
function pmpro_userfields_coded_fields_widget() {
global $pmpro_user_fields;

// Calculate names of UI fields.
$ui_fields_in_groups = pmpro_get_user_fields_settings();
$ui_fields = array_merge( ...array_map( function ( $group ) { return $group->fields; }, $ui_fields_in_groups ) );
$ui_field_names = array_map( function ( $field ) { return $field->name; }, $ui_fields );

// Find all fields that are not in the UI.
$coded_fields = array();
foreach( $pmpro_user_fields as $group_name => $fields ) {
foreach( $fields as $field ) {
if ( ! in_array( $field->name, $ui_field_names ) ) {
if ( empty( $coded_fields[ $group_name ] ) ) {
$coded_fields[ $group_name ] = array();
}
$coded_fields[ $group_name ][] = $field;
}
}
}

// Display the coded fields.
if ( ! empty( $coded_fields ) ) {
echo '<ul>';
foreach ( $coded_fields as $group_name => $fields ) {
echo '<li>';
echo '<strong>' . esc_html( $group_name ) . ':</strong> ';
echo '<br />';
echo esc_html( implode( ', ', array_map( function ( $field ) { return $field->label; }, $fields ) ) );
echo '</li>';
}
} else {
echo '<p>' . esc_html__( 'No coded fields found.', 'paid-memberships-pro' ) . '</p>';
}
}

?>
<hr class="wp-header-end">
<h1><?php esc_html_e( 'User Fields', 'paid-memberships-pro' ); ?></h1>
Expand Down Expand Up @@ -145,7 +186,6 @@ function pmpro_userfields_help_widget() { ?>
<input type="hidden" id="pmpro_user_fields_settings" name="pmpro_user_fields_settings" value="<?php echo esc_attr( json_encode( $user_fields_settings ) );?>" />
</form>
</div> <!-- end postbox-container-1 -->

</div> <!-- end post-body -->
</div> <!-- end poststuff -->
<script type="text/javascript">
Expand Down
7 changes: 1 addition & 6 deletions includes/fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -1623,12 +1623,7 @@ function pmpro_load_user_fields_from_settings() {
* @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;
}
global $pmpro_user_fields;

// 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.
Expand Down