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

[Will not release] User fields php UI #3210

Draft
wants to merge 9 commits into
base: dev
Choose a base branch
from
16 changes: 12 additions & 4 deletions adminpages/member-edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,19 @@ function pmpro_member_edit_get_panels() {
// Add user fields panels.
$user_id = PMPro_Member_Edit_Panel::get_user()->ID;
if ( $user_id ) {
$profile_user_fields = pmpro_get_user_fields_for_profile( $user_id, true );
if ( ! empty( $profile_user_fields ) ) {
foreach ( $profile_user_fields as $group_name => $user_fields ) {
$panels[] = new PMPro_Member_Edit_Panel_User_Fields( $group_name );
foreach( PMPro_Field_Group::get_all() as $group ) {
$fields_to_display = $group->get_fields_to_display(
array(
'scope' => 'profile',
'user_id' => $user_id,
)
);

if ( empty( $fields_to_display ) ) {
continue;
}

$panels[] = new PMPro_Member_Edit_Panel_User_Fields( $group->name );
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
<?php

class PMPro_Member_Edit_Panel_User_Fields extends PMPro_Member_Edit_Panel {
/**
* @var PMPro_Field_Group The field group .
* @since TBD
*/
private $field_group;

/**
* Set up the panel.
*/
public function __construct( $field_group_name ) {
$this->field_group = PMPro_Field_Group::get( $field_group_name );
$this->slug = 'user-fields-' . sanitize_title( $field_group_name );
$this->title = $field_group_name;
$this->title = $this->field_group->label;
$this->submit_text = current_user_can( 'edit_users' ) ? __( 'Update Member', 'paid-memberships-pro' ) : '';
}

Expand All @@ -15,9 +22,8 @@ public function __construct( $field_group_name ) {
*/
protected function display_panel_contents() {
// Print the group description.
$field_group = pmpro_get_field_group_by_name( $this->title );
if ( ! empty( $field_group->description ) ) {
echo wp_kses_post( $field_group->description );
if ( ! empty( $this->field_group->description ) ) {
echo wp_kses_post( $this->field_group->description );
}

// Check if this is a checkout field location and show a message about custom code.
Expand All @@ -31,21 +37,22 @@ protected function display_panel_contents() {
'before_submit_button',
'just_profile'
);
if ( in_array( $this->title, $checkout_field_locations ) ) {
if ( in_array( $this->field_group->name, $checkout_field_locations ) ) {
esc_html_e( 'These user fields were added via custom code to hook into the following location:', 'paid-memberships-pro' );
echo ' <code>' . esc_html( $this->title ) . '</code>';
echo ' <code>' . esc_html( $this->field_group->name ) . '</code>';
}

// Print the fields.
$profile_user_fields = pmpro_get_user_fields_for_profile( self::get_user()->ID, true );
?>
<table class="form-table">
<?php
foreach( $profile_user_fields[$this->title] as $field ) {
if ( pmpro_is_field( $field ) ) {
$field->displayInProfile( self::get_user()->ID ); // Field will be readonly if cannot edit users.
}
}
$this->field_group->display(
array(
'markup' => 'table',
'show_group_label' => false,
'user_id' => self::get_user()->ID,
)
);
?>
</table>
<?php
Expand All @@ -55,7 +62,11 @@ protected function display_panel_contents() {
* Save panel data.
*/
public function save() {
$saved = ( pmpro_save_user_fields_in_profile( self::get_user()->ID ) !== false ); // Function returns false on failed, null on saved. Will check edit_users cap in function.
$saved = $this->field_group->save_fields(
array(
'user_id' => self::get_user()->ID,
)
);

// Show success message.
if ( $saved ) {
Expand Down
43 changes: 43 additions & 0 deletions adminpages/user-fields/delete-field.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

// Get the field to delete
if ( isset( $_REQUEST['delete_name'] ) ) {
$delete_name = sanitize_text_field( $_REQUEST['delete_name'] );

// Get the current settings.
$current_settings = pmpro_get_user_fields_settings();

// Remove the field from the settings.
$new_settings = array();
$deleted = false;
foreach ( $current_settings as $group_setting ) {
$new_fields = array();
foreach ( $group_setting->fields as $field_setting ) {
if ( $field_setting->name === $delete_name ) {
$deleted = true;
} else {
$new_fields[] = $field_setting;
}
}
$group_setting->fields = $new_fields;
$new_settings[] = $group_setting;
}

if ( $deleted ) {
// Save the new settings.
update_option( 'pmpro_user_fields_settings', $new_settings );

// Show a success message.
pmpro_setMessage( __( 'Field deleted.', 'paid-memberships-pro' ), 'success' );

// Redirect with javascript.
?>
<script>
window.location.href = '?page=pmpro-userfields&success_message=<?php echo urlencode( __( 'Field deleted.', 'paid-memberships-pro' ) ); ?>';
</script>
<?php
exit;
} else {
pmpro_setMessage( __( 'Field not found.', 'paid-memberships-pro' ), -1 );
}
}
38 changes: 38 additions & 0 deletions adminpages/user-fields/delete-group.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

// Get the group to delete
if ( isset( $_REQUEST['delete_name'] ) ) {
$delete_name = sanitize_text_field( $_REQUEST['delete_name'] );

// Get the current settings.
$current_settings = pmpro_get_user_fields_settings();

// Remove the group from the settings.
$new_settings = array();
$deleted = false;
foreach ( $current_settings as $group_setting ) {
if ( $group_setting->name === $delete_name ) {
$deleted = true;
} else {
$new_settings[] = $group_setting;
}
}

if ( $deleted ) {
// Save the new settings.
update_option( 'pmpro_user_fields_settings', $new_settings );

// Show a success message.
pmpro_setMessage( __( 'Group deleted.', 'paid-memberships-pro' ), 'success' );

// Redirect with javascript.
?>
<script>
window.location.href = '?page=pmpro-userfields&success_message=<?php echo urlencode( __( 'Group deleted.', 'paid-memberships-pro' ) ); ?>';
</script>
<?php
exit;
} else {
pmpro_setMessage( __( 'Group not found.', 'paid-memberships-pro' ), -1 );
}
}
Loading