From 79c17fdac6607c54098614aae8e5e6248b5651cb Mon Sep 17 00:00:00 2001 From: pgmatev Date: Wed, 8 Jul 2020 16:41:24 +0300 Subject: Validation works; Problem with saving --- wp-content/plugins/dx-users/class.cm.php | 42 +++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 12 deletions(-) (limited to 'wp-content/plugins/dx-users/class.cm.php') diff --git a/wp-content/plugins/dx-users/class.cm.php b/wp-content/plugins/dx-users/class.cm.php index 23d07e4..dae01a8 100644 --- a/wp-content/plugins/dx-users/class.cm.php +++ b/wp-content/plugins/dx-users/class.cm.php @@ -2,13 +2,14 @@ class Users { function init() { // add_filter( 'user_contactmethods', array( $this, 'add_custom_box_user' ), 10, 1 ); - add_action( 'show_user_profile', 'renderProfileFields' ); - add_action( 'edit_user_profile', 'renderProfileFields' ); + add_action( 'show_user_profile', array( $this, 'render_profile_fields' ) ); + add_action( 'edit_user_profile', array( $this, 'render_profile_fields' ) ); // validate profile fields - add_action( 'user_profile_update_errors', 'validateProfileFields', 10, 3 ); + add_action( 'user_profile_update_errors', array( $this, 'validate_profile_phone' ), 10, 3 ); + add_action( 'user_profile_update_errors', array( $this, 'validate_profile_address' ), 10, 3 ); // save profile fields - add_action( 'edit_user_profile_update', 'saveProfileFields' ); - add_action( 'personal_options_update', 'saveProfileFields' ); + // add_action( 'edit_user_profile_update', array( $this, 'save_profile_fields' ), 10 ); + // add_action( 'personal_options_update', array( $this, 'save_profile_fields' ) ); } // function add_custom_box_user($contact_methods) { // $new_methods = array( @@ -30,7 +31,7 @@ class Users { // return $contact_methods; // } - function renderProfileFields( WP_User $user ) { ?> + function render_profile_fields( $user ) { ?>

More Information

@@ -51,18 +52,35 @@ class Users { 10 && !empty( $_POST['phone'] ) ) $errors->add( 'phone', "ERROR: The maximum phone length is 10 characters." ); + + if( !empty( $errors ) ) + return $errors; + else { + add_action( 'edit_user_profile_update', array( $this, 'save_profile_fields' ) ); + add_action( 'personal_options_update', array( $this, 'save_profile_fields' ) ); + } - if ( !empty( $_POST['address'] ) && strlen( $_POST['address'] ) > 255 && !empty( $_POST['address'] ) ) - $errors->add( 'address', "ERROR: The maximum address length is 255 characters." ); //Unsure what address validation to use - - return $errors; } - function saveProfileFields( $id ) { + function validate_profile_address( &$errors, $update, &$user ) { + // validate input fields + if ( !empty( $_POST['address'] ) && strlen( $_POST['address'] ) > 255 && !empty( $_POST['address'] ) ) + $errors->add( 'address', "ERROR: The maximum address length is 255 characters." ); //test validation; TO DO define proper validation + + if( !empty( $errors ) ) + return $errors; + else { + add_action( 'edit_user_profile_update', array( $this, 'save_profile_fields' ) ); + add_action( 'personal_options_update', array( $this, 'save_profile_fields' ) ); + } + + } + + function save_profile_fields( $id ) { // save input fields values if ( !current_user_can( 'edit_user', $id ) ) return false; -- cgit v1.3.1-26-gb76a