From b26d81d466e149ca584fa852f74b79b75f5d8e72 Mon Sep 17 00:00:00 2001 From: champsupertramp Date: Wed, 25 Nov 2015 14:19:51 +0800 Subject: [PATCH 1/5] Fixed conditional form fields --- assets/js/um-functions.js | 51 +++++++++++++++++++++++++++++--------- core/um-actions-form.php | 2 ++ core/um-fields.php | 1 + core/um-filters-fields.php | 40 ++++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+), 12 deletions(-) diff --git a/assets/js/um-functions.js b/assets/js/um-functions.js index 5254d767..55cf3d3c 100644 --- a/assets/js/um-functions.js +++ b/assets/js/um-functions.js @@ -4,7 +4,8 @@ var live_value; function um_conditional(){ jQuery('.um-field.um-is-conditional').each(function(){ - + //console.log('-----'); + var found = 0; for (var i = 0; i < 5; i++) { var action0 = jQuery(this).data('cond-'+i+'-action'); @@ -12,73 +13,91 @@ function um_conditional(){ var operator0 = jQuery(this).data('cond-'+i+'-operator'); var value0 = jQuery(this).data('cond-'+i+'-value'); - if ( action0 == 'show' && field0 == live_field ) { + if ( action0 == 'show' && field0 == live_field && typeof value0 !== 'undefined' ) { + //console.log( 'show',i,'conditional='+value0, 'option='+live_value ); + if ( operator0 == 'empty' ) { - if ( !live_value || live_value == '' ) { + if ( !live_value || live_value == '' || found > 0 ) { jQuery(this).fadeIn(); + found++; } else { jQuery(this).hide(); + } } if ( operator0 == 'not empty' ) { - if ( live_value && live_value != '' ) { + if ( live_value && live_value != '' || found > 0 ) { jQuery(this).fadeIn(); + found++; } else { jQuery(this).hide(); + } } if ( operator0 == 'equals to' ) { - if ( value0 == live_value ) { + if ( value0 == live_value || found > 0 ) { jQuery(this).fadeIn(); + found++; } else { jQuery(this).hide(); + } } if ( operator0 == 'not equals' ) { - if ( jQuery.isNumeric( value0 ) && parseInt( live_value ) != parseInt( value0 ) && live_value ) { + if ( jQuery.isNumeric( value0 ) && parseInt( live_value ) != parseInt( value0 ) && live_value || found > 0 ) { jQuery(this).fadeIn(); - } else if ( !jQuery.isNumeric( value0 ) && value0 != live_value ) { + found++; + } else if ( !jQuery.isNumeric( value0 ) && value0 != live_value || found > 0 ) { jQuery(this).fadeIn(); + found++; } else { jQuery(this).hide(); + } } if ( operator0 == 'greater than' ) { - if ( jQuery.isNumeric( value0 ) && parseInt( live_value ) > parseInt( value0 ) ) { + if ( jQuery.isNumeric( value0 ) && parseInt( live_value ) > parseInt( value0 ) || found > 0) { jQuery(this).fadeIn(); + found++; } else { jQuery(this).hide(); + } } if ( operator0 == 'less than' ) { - if ( jQuery.isNumeric( value0 ) && parseInt( live_value ) < parseInt( value0 ) && live_value ) { + if ( jQuery.isNumeric( value0 ) && parseInt( live_value ) < parseInt( value0 ) && live_value || found > 0) { jQuery(this).fadeIn(); + found++; } else { jQuery(this).hide(); + } } if ( operator0 == 'contains' ) { - if ( live_value && live_value.indexOf( value0 ) >= 0 ) { + if ( live_value && live_value.indexOf( value0 ) >= 0 || found > 0 ) { jQuery(this).fadeIn(); + found++; } else { jQuery(this).hide(); + } } } - if ( action0 == 'hide' && field0 == live_field ) { + if ( action0 == 'hide' && field0 == live_field && typeof value0 !== 'undefined' ) { if ( operator0 == 'empty' ) { if ( !live_value || live_value == '' ) { jQuery(this).hide(); + found++; } else { jQuery(this).fadeIn(); } @@ -87,6 +106,7 @@ function um_conditional(){ if ( operator0 == 'not empty' ) { if ( live_value && live_value != '' ) { jQuery(this).hide(); + found++; } else { jQuery(this).fadeIn(); } @@ -95,6 +115,7 @@ function um_conditional(){ if ( operator0 == 'equals to' ) { if ( value0 == live_value ) { jQuery(this).hide(); + found++; } else { jQuery(this).fadeIn(); } @@ -103,8 +124,10 @@ function um_conditional(){ if ( operator0 == 'not equals' ) { if ( jQuery.isNumeric( value0 ) && parseInt( live_value ) != parseInt( value0 ) && live_value ) { jQuery(this).hide(); + found++; } else if ( !jQuery.isNumeric( value0 ) && value0 != live_value ) { jQuery(this).hide(); + found++; } else { jQuery(this).fadeIn(); } @@ -113,6 +136,7 @@ function um_conditional(){ if ( operator0 == 'greater than' ) { if ( jQuery.isNumeric( value0 ) && parseInt( live_value ) > parseInt( value0 ) ) { jQuery(this).hide(); + found++; } else { jQuery(this).fadeIn(); } @@ -121,6 +145,7 @@ function um_conditional(){ if ( operator0 == 'less than' ) { if ( jQuery.isNumeric( value0 ) && parseInt( live_value ) < parseInt( value0 ) && live_value ) { jQuery(this).hide(); + found++; } else { jQuery(this).fadeIn(); } @@ -129,11 +154,13 @@ function um_conditional(){ if ( operator0 == 'contains' ) { if ( live_value && live_value.indexOf( value0 ) >= 0 ) { jQuery(this).hide(); + found++; } else { jQuery(this).fadeIn(); } } - + //console.log( 'hide',i,value0, live_value ); + } } diff --git a/core/um-actions-form.php b/core/um-actions-form.php index 9e85f04a..151dcf6b 100644 --- a/core/um-actions-form.php +++ b/core/um-actions-form.php @@ -155,6 +155,8 @@ foreach( $fields as $key => $array ) { + $array = apply_filters('um_get_custom_field_array', $array, $fields ); + if ( isset( $array['type'] ) && $array['type'] == 'checkbox' && isset( $array['required'] ) && $array['required'] == 1 && !isset( $args[$key] ) ) { $ultimatemember->form->add_error($key, sprintf(__('%s is required.','ultimatemember'), $array['title'] ) ); } diff --git a/core/um-fields.php b/core/um-fields.php index 96fd50d8..0db89032 100644 --- a/core/um-fields.php +++ b/core/um-fields.php @@ -581,6 +581,7 @@ class UM_Fields { if ( isset( $array['conditions'] ) && is_array( $array['conditions'] ) && !$this->viewing ) { $array['conditional'] = ''; + foreach( $array['conditions'] as $cond_id => $cond ) { $array['conditional'] .= ' data-cond-'.$cond_id.'-action="'. $cond[0] . '" data-cond-'.$cond_id.'-field="'. $cond[1] . '" data-cond-'.$cond_id.'-operator="'. $cond[2] . '" data-cond-'.$cond_id.'-value="'. $cond[3] . '"'; } diff --git a/core/um-filters-fields.php b/core/um-filters-fields.php index d8cd3e26..1fc6be75 100644 --- a/core/um-filters-fields.php +++ b/core/um-filters-fields.php @@ -261,4 +261,44 @@ return $array; + } + + /*** + *** @validate conditional logic + ***/ + add_filter('um_get_custom_field_array', 'um_get_custom_field_array',99,2); + + function um_get_custom_field_array( $array, $fields ){ + + if( isset( $array['conditions'] ) ){ + $found = 0; + for( $a = 0; $a < count( $array['conditions'] ); $a++ ){ + if( isset( $array['conditional_value'] ) || isset( $array['conditional_value'.$a] ) ){ + + if( isset( $array['conditions'] ) && ! empty( $array['conditions'] ) ){ + + $arr_conditions = array(); + + foreach ($array['conditions'] as $key => $value) { + $metakey = $fields[ $value[1] ]['metakey'] ; + $arr_conditions[ $metakey ] = $_POST[ $metakey ]; + } + + foreach ($array['conditions'] as $key => $value) { + $metakey = $fields[ $value[1] ]['metakey'] ; + $arr_conditions[ $metakey ] = $_POST[ $metakey ]; + if( isset( $_POST[ $metakey ] ) && isset( $array['conditional_value'] ) && $_POST[ $metakey ] !== $array['conditional_value'] ){ + $array['required'] = 0; + } + if( isset( $_POST[ $metakey ] ) && isset( $array['conditional_value'.$a] ) && $_POST[ $metakey ] !== $array['conditional_value'.$a] ){ + $array['required'] = 0; + } + } + + } + } + } + } + + return $array; } \ No newline at end of file From 94aa894dba9871afcf389ee5ac065851c77f3059 Mon Sep 17 00:00:00 2001 From: champsupertramp Date: Wed, 25 Nov 2015 14:22:43 +0800 Subject: [PATCH 2/5] Remove duplicate user IDs --- core/um-members.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/core/um-members.php b/core/um-members.php index bc7a8ffc..84ba5b61 100644 --- a/core/um-members.php +++ b/core/um-members.php @@ -136,14 +136,13 @@ class UM_Members { $query_args = array(); $query_args = apply_filters( 'um_prepare_user_query_args', $query_args, $args ); - $users = new WP_User_Query( $query_args ); // number of profiles for mobile if ( $ultimatemember->mobile->isMobile() && isset( $profiles_per_page_mobile ) ) $profiles_per_page = $profiles_per_page_mobile; - $array['users'] = $users->results; + $array['users'] = array_unique( $users->results ); $array['total_users'] = (isset( $max_users ) && $max_users && $max_users <= $users->total_users ) ? $max_users : $users->total_users; From 1ec02e11a92a347a8bacb8ff26f6bb77c9ebbafd Mon Sep 17 00:00:00 2001 From: champsupertramp Date: Wed, 25 Nov 2015 19:27:41 +0800 Subject: [PATCH 3/5] Fixed default wp page restriction function --- core/um-actions-wpadmin.php | 113 ++++++++++++++++++------------------ 1 file changed, 58 insertions(+), 55 deletions(-) diff --git a/core/um-actions-wpadmin.php b/core/um-actions-wpadmin.php index 9bcf91d1..afc3a00c 100644 --- a/core/um-actions-wpadmin.php +++ b/core/um-actions-wpadmin.php @@ -6,80 +6,83 @@ add_action('init','um_block_wpadmin_for_guests'); function um_block_wpadmin_for_guests() { global $pagenow; - + + + if ( isset( $_REQUEST['um_panic_key'] ) && $_REQUEST['um_panic_key'] == um_get_option('panic_key') ) { exit( wp_redirect( add_query_arg('_verified_key', $_REQUEST['um_panic_key'], wp_login_url() ) ) ); } if ( !isset( $_REQUEST['_verified_key'] ) || $_REQUEST['_verified_key'] != um_get_option('panic_key') ) { - - // Logout screen - if ( isset( $pagenow ) && $pagenow == 'wp-login.php' && is_user_logged_in() && isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'logout' ) { - $redirect = um_get_core_page('logout'); - if ( isset( $_REQUEST['redirect_to'] ) && !empty( $_REQUEST['redirect_to'] ) ) { - $redirect = add_query_arg( 'redirect_to', $_REQUEST['redirect_to'], $redirect ); - } - exit( wp_redirect( $redirect ) ); - } - - // Login screen - if ( isset( $pagenow ) && $pagenow == 'wp-login.php' && !is_user_logged_in() && !isset( $_REQUEST['action'] ) ) { + // Logout screen + if ( isset( $pagenow ) && $pagenow == 'wp-login.php' && is_user_logged_in() && isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'logout' ) { + $redirect = um_get_core_page('logout'); - $allowed = um_get_option('wpadmin_login'); - $allowed = apply_filters('um_whitelisted_wpadmin_access', $allowed ); - - if ( !$allowed ) { - - $act = um_get_option('wpadmin_login_redirect'); - $custom_url = um_get_option('wpadmin_login_redirect_url'); - - if ( $act == 'um_login_page' || !$custom_url ) { - $redirect = um_get_core_page('login'); - } else { - $redirect = $custom_url; + if ( isset( $_REQUEST['redirect_to'] ) && !empty( $_REQUEST['redirect_to'] ) ) { + $redirect = add_query_arg( 'redirect_to', $_REQUEST['redirect_to'], $redirect ); } exit( wp_redirect( $redirect ) ); } - } + + // Login screen + if ( isset( $pagenow ) && $pagenow == 'wp-login.php' && !is_user_logged_in() && !isset( $_REQUEST['action'] ) ) { - // Register screen - if ( isset( $pagenow ) && $pagenow == 'wp-login.php' && !is_user_logged_in() && isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'register' ) { - - $allowed = um_get_option('wpadmin_register'); - $allowed = apply_filters('um_whitelisted_wpadmin_access', $allowed ); - - if ( !$allowed ) { + $allowed = um_get_option('wpadmin_login'); + $allowed = apply_filters('um_whitelisted_wpadmin_access', $allowed ); - $act = um_get_option('wpadmin_register_redirect'); - $custom_url = um_get_option('wpadmin_register_redirect_url'); - - if ( $act == 'um_register_page' || !$custom_url ) { - $redirect = um_get_core_page('register'); - } else { - $redirect = $custom_url; + if ( !$allowed ) { + + $act = um_get_option('wpadmin_login_redirect'); + $custom_url = um_get_option('wpadmin_login_redirect_url'); + + if ( $act == 'um_login_page' || !$custom_url ) { + $redirect = um_get_core_page('login'); + } else { + $redirect = $custom_url; + } + exit( wp_redirect( $redirect ) ); + } + } + + // Register screen + if ( isset( $pagenow ) && $pagenow == 'wp-login.php' && !is_user_logged_in() && isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'register' ) { + + $allowed = um_get_option('wpadmin_register'); + $allowed = apply_filters('um_whitelisted_wpadmin_access', $allowed ); + + if ( !$allowed ) { + + $act = um_get_option('wpadmin_register_redirect'); + $custom_url = um_get_option('wpadmin_register_redirect_url'); + + if ( $act == 'um_register_page' || !$custom_url ) { + $redirect = um_get_core_page('register'); + } else { + $redirect = $custom_url; + } + exit( wp_redirect( $redirect ) ); } - exit( wp_redirect( $redirect ) ); } - } - // Lost password page - if ( isset( $pagenow ) && $pagenow == 'wp-login.php' && isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'lostpassword' ) { - exit( wp_redirect( um_get_core_page('password-reset') ) ); - } - - // Prevention for logged in user - if ( isset( $pagenow ) && $pagenow == 'wp-login.php' && is_user_logged_in() ) { + // Lost password page + if ( isset( $pagenow ) && $pagenow == 'wp-login.php' && isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'lostpassword' ) { + exit( wp_redirect( um_get_core_page('password-reset') ) ); + } + + // Prevention for logged in user + if ( isset( $pagenow ) && $pagenow == 'wp-login.php' && is_user_logged_in() && isset( $_REQUEST['action'] ) && $_REQUEST['action'] != 'postpass' ) { + + if ( !um_user('can_access_wpadmin') ) { + exit( wp_redirect( home_url() ) ); + } else { + exit( wp_redirect( admin_url() ) ); + } - if ( !um_user('can_access_wpadmin') ) { - exit( wp_redirect( home_url() ) ); - } else { - exit( wp_redirect( admin_url() ) ); } } - - } + } /*** From 727e45f58d38264b0f1e2559fb384fd4630b5658 Mon Sep 17 00:00:00 2001 From: champsupertramp Date: Wed, 25 Nov 2015 20:37:42 +0800 Subject: [PATCH 4/5] Fixed category/post restrictions redirection --- core/um-actions-access.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/core/um-actions-access.php b/core/um-actions-access.php index 502c61d7..74c595c0 100644 --- a/core/um-actions-access.php +++ b/core/um-actions-access.php @@ -63,11 +63,14 @@ add_action('um_access_category_settings','um_access_category_settings'); function um_access_category_settings() { global $post, $wp_query, $ultimatemember; - if ( is_single() && get_the_category() ) { + if ( is_single() || get_the_category() ) { + + $categories = get_the_category(); foreach( $categories as $cat ) { $term_id = $cat->term_id; $opt = get_option("category_$term_id"); + if ( isset( $opt['_um_accessible'] ) ) { switch( $opt['_um_accessible'] ) { @@ -88,8 +91,8 @@ case 2: - if ( !is_user_logged_in() ) - $ultimatemember->access->redirect_handler = ( isset( $opt['_um_redirect'] ) ) ? $opt['_um_redirect'] : um_get_core_page('login'); + if ( ! is_user_logged_in() ) + $ultimatemember->access->redirect_handler = ( isset( $opt['_um_redirect'] ) && ! empty( $opt['_um_redirect'] ) ) ? $opt['_um_redirect'] : um_get_core_page('login'); if ( is_user_logged_in() && isset( $opt['_um_roles'] ) && !empty( $opt['_um_roles'] ) ){ if ( !in_array( um_user('role'), $opt['_um_roles'] ) ) { From f6ef053dda7e940f0fa00a2aa610fc5482df70ef Mon Sep 17 00:00:00 2001 From: champsupertramp Date: Wed, 25 Nov 2015 20:38:20 +0800 Subject: [PATCH 5/5] Fixed multiple redirection in page restriction --- core/um-access.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core/um-access.php b/core/um-access.php index 1c0c8823..edada558 100644 --- a/core/um-access.php +++ b/core/um-access.php @@ -25,9 +25,10 @@ class UM_Access { do_action('um_access_post_settings'); - if ( $this->redirect_handler && !$this->allow_access ) { + if ( $this->redirect_handler && !$this->allow_access && ! um_is_core_page('login') ) { // login page add protected page automatically + if ( strstr( $this->redirect_handler, um_get_core_page('login') ) ){ $curr = $ultimatemember->permalinks->get_current_url(); $this->redirect_handler = esc_url( add_query_arg('redirect_to', $curr, $this->redirect_handler) );