0 ) {
$key = $new_order[ $row ];
$key = substr( $key, 15 );
if ( $key ) {
for ( $x = 0; $x < count( $wpmem_old_fields ); $x++ ) {
if ( $wpmem_old_fields[ $x ][2] == $key ) {
$wpmem_new_fields[ $row - 1 ] = $wpmem_old_fields[ $x ];
}
}
}
}
}
update_option( 'wpmembers_fields', $wpmem_new_fields );
die(); // This is required to return a proper result.
}
/**
* Updates fields.
*
* @since 2.8
*
* @param string $action The field update action (update_fields|add|edit)
* @global string $add_field_err_msg The add field error message
* @return string $did_update The fields update message
*
* @todo apply some additional form validation to the add/update process
*/
function wpmem_update_fields( $action ) {
// Get the current fields.
$wpmem_fields = get_option( 'wpmembers_fields' );
$wpmem_ut_fields = get_option( 'wpmembers_utfields' );
if ( $action == 'update_fields' ) {
// Check nonce.
check_admin_referer( 'wpmem-update-fields' );
// @todo - need some additional form validation here
// Update user table fields.
$arr = ( isset( $_POST['ut_fields'] ) ) ? $_POST['ut_fields'] : '';
update_option( 'wpmembers_utfields', $arr );
// Rebuild the array, don't touch user_email - it's always mandatory.
$nrow = 0;
foreach ( $wpmem_fields as $field ) {
$meta_key = $field[2];
// Check to see if the field is checked for deletion, and if not, add it to the new array.
$delete_field = "del_" . $meta_key;
$delete_field = ( isset( $_POST[ $delete_field ] ) ) ? $_POST[ $delete_field ] : false;
if ( $delete_field != "delete" ) {
for ( $i = 0; $i < 4; $i++ ) {
$wpmem_newfields[ $nrow ][ $i ] = $field[ $i ];
}
$wpmem_newfields[ $nrow ][0] = $nrow + 1;
$display_field = $meta_key . "_display";
$require_field = $meta_key . "_required";
$checked_field = $meta_key . "_checked";
if ( $field[2] != 'user_email' ){
$wpmem_newfields[ $nrow ][4] = ( isset( $_POST[ $display_field ] ) ) ? 'y' : '';
$wpmem_newfields[ $nrow ][5] = ( isset( $_POST[ $require_field ] ) ) ? 'y' : '';
} else {
$wpmem_newfields[ $nrow ][4] = 'y';
$wpmem_newfields[ $nrow ][5] = 'y';
}
$wpmem_newfields[ $nrow ][6] = $field[6];
$wpmem_newfields[ $nrow ][7] = ( isset( $field[7] ) ) ? $field[7] : '';
if ( $field[3] == 'checkbox' ) {
if ( isset( $_POST[ $checked_field ] ) && $_POST[ $checked_field ] == 'y' ) {
$wpmem_newfields[ $nrow ][8] = 'y';
} else {
$wpmem_newfields[ $nrow ][8] = 'n';
}
}
$nrow = $nrow + 1;
}
}
update_option( 'wpmembers_fields', $wpmem_newfields );
$did_update = __( 'WP-Members fields were updated', 'wp-members' );
} elseif ( $action == 'add_field' || 'edit_field' ) {
// Check nonce.
check_admin_referer( 'wpmem-add-fields' );
global $add_field_err_msg;
$add_field_err_msg = false;
// Error check that field label and option name are included and unique.
$add_field_err_msg = ( $_POST['add_name'] == '' ) ? __( 'Field Label is required for adding a new field. Nothing was updated.', 'wp-members' ) : $add_field_err_msg;
$add_field_err_msg = ( $_POST['add_option'] == '' ) ? __( 'Option Name is required for adding a new field. Nothing was updated.', 'wp-members' ) : $add_field_err_msg;
$add_field_err_msg = ( !preg_match("/^[A-Za-z0-9_]*$/", $_POST['add_option'] ) ) ? __( 'Option Name must contain only letters, numbers, and underscores', 'wp-members' ) : $add_field_err_msg;
// Check for duplicate field names.
$chk_fields = array();
foreach ( $wpmem_fields as $field ) {
$chk_fields[] = $field[2];
}
$add_field_err_msg = ( in_array( $_POST['add_option'], $chk_fields ) ) ? __( 'A field with that option name already exists', 'wp-members' ) : $add_field_err_msg;
// Error check for reserved terms.
$reserved_terms = wpmem_wp_reserved_terms();
$submitted_meta = $_POST['add_option'];
if ( in_array( strtolower( $submitted_meta ), $reserved_terms ) ) {
$add_field_err_msg = sprintf( __( 'Sorry, "%s" is a reserved term. Field was not added.', 'wp-members' ), $submitted_term );
}
// Error check option name for spaces and replace with underscores.
$us_option = preg_replace( "/ /", '_', $submitted_meta );
$arr = array();
$type = $_POST['add_type'];
$arr[0] = ( $action == 'add_field' ) ? ( count( $wpmem_fields ) ) + 2 : false;
$arr[1] = stripslashes( $_POST['add_name'] );
$arr[2] = $us_option;
$arr[3] = $type;
$arr[4] = ( isset( $_POST['add_display'] ) ) ? $_POST['add_display'] : 'n';
$arr[5] = ( isset( $_POST['add_required'] ) ) ? $_POST['add_required'] : 'n';
$arr[6] = ( $us_option == 'user_nicename' || $us_option == 'display_name' || $us_option == 'nickname' ) ? 'y' : 'n';
if ( $type == 'checkbox' ) {
$add_field_err_msg = ( ! $_POST['add_checked_value'] ) ? __( 'Checked value is required for checkboxes. Nothing was updated.', 'wp-members' ) : $add_field_err_msg;
$arr[7] = ( isset( $_POST['add_checked_value'] ) ) ? $_POST['add_checked_value'] : false;
$arr[8] = ( isset( $_POST['add_checked_default'] ) ) ? $_POST['add_checked_default'] : 'n';
}
if ( $type == 'select'
|| $type == 'multiselect'
|| $type == 'radio'
|| $type == 'multicheckbox'
) {
// Get the values.
$str = stripslashes( $_POST['add_dropdown_value'] );
// Remove linebreaks.
$str = trim( str_replace( array("\r", "\r\n", "\n"), '', $str ) );
// Create array.
if ( ! function_exists( 'str_getcsv' ) ) {
$arr[7] = explode( ',', $str );
} else {
$arr[7] = str_getcsv( $str, ',', '"' );
}
// If multiselect or multicheckbox, set delimiter.
if ( 'multiselect' == $type || 'multicheckbox' == $type ) {
$arr[8] = ( isset( $_POST['add_delimiter_value'] ) ) ? $_POST['add_delimiter_value'] : '|';
}
}
if ( $type == 'file' || $type == 'image' ) {
$arr[7] = stripslashes( $_POST['add_file_value'] );
}
if ( $_POST['add_type'] == 'hidden' ) {
$add_field_err_msg = ( ! $_POST['add_hidden_value'] ) ? __( 'A value is required for hidden fields. Nothing was updated.', 'wp-members' ) : $add_field_err_msg;
$arr[7] = ( isset( $_POST['add_hidden_value'] ) ) ? stripslashes( $_POST['add_hidden_value'] ) : '';
}
if ( $action == 'add_field' ) {
if ( ! $add_field_err_msg ) {
array_push( $wpmem_fields, $arr );
update_option( 'wpmembers_fields', $wpmem_fields );
$did_update = $_POST['add_name'] . ' ' . __( 'field was added', 'wp-members' );
} else {
$did_update = $add_field_err_msg;
}
} else {
for ( $row = 0; $row < count( $wpmem_fields ); $row++ ) {
if ( $wpmem_fields[ $row ][2] == $_GET['edit'] ) {
$arr[0] = $wpmem_fields[ $row ][0];
//$x = ( $arr[3] == 'checkbox' ) ? 8 : ( ( $arr[3] == 'select' || $arr[3] == 'file' ) ? 7 : 6 );
for ( $r = 0; $r < count( $arr ); $r++ ) {
$wpmem_fields[ $row ][ $r ] = $arr[ $r ];
}
}
}
update_option( 'wpmembers_fields', $wpmem_fields );
$did_update = $_POST['add_name'] . ' ' . __( 'field was updated', 'wp-members' );
}
}
if ( WPMEM_DEBUG == true && isset( $arr ) ) { echo ""; print_r($arr); echo "
"; }
global $wpmem;
$wpmem->load_fields();
return $did_update;
}
/**
* Function to write the field edit link.
*
* @since 2.8
*
* @param string $field_id The option name of the field to be edited
*/
function wpmem_fields_edit_link( $field_id ) {
return '' . __( 'Edit' ) . '';
}
/**
* Function to dispay the add/edit field form.
*
* @since 2.8
*
* @param string $mode The mode for the function (edit|add)
* @param array|null $wpmem_fields the array of fields
* @param string|null $field the field being edited
*/
function wpmem_a_field_edit( $mode, $wpmem_fields = null, $meta_key = null ) {
global $wpmem;
if ( $mode == 'edit' ) {
$fields = wpmem_fields();
$field = $fields[ $meta_key ];
}
$form_action = ( $mode == 'edit' ) ? 'editfieldform' : 'addfieldform';
?>