From d948f1d7f527ae115ad600a33c0bf0923cd150c4 Mon Sep 17 00:00:00 2001 From: denisbaranov Date: Thu, 30 May 2019 18:59:03 +0300 Subject: [PATCH 1/3] the issue with the options from custom callback function for multi-select/select type field (Telegram, May 30) --- includes/core/um-actions-profile.php | 19 +++++++++++++++++-- includes/core/um-filters-fields.php | 16 ++++++++-------- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/includes/core/um-actions-profile.php b/includes/core/um-actions-profile.php index ad3d7cf7..46d71322 100644 --- a/includes/core/um-actions-profile.php +++ b/includes/core/um-actions-profile.php @@ -215,8 +215,8 @@ function um_user_edit_profile( $args ) { */ do_action( 'um_user_before_updating_profile', $userinfo ); - if ( ! empty( $args['custom_fields'] ) ) { - $fields = unserialize( $args['custom_fields'] ); + if ( !empty( $args[ 'custom_fields' ] ) ) { + $fields = apply_filters( 'um_user_edit_profile_fields', unserialize( $args[ 'custom_fields' ] ), $args ); } // loop through fields @@ -246,6 +246,21 @@ function um_user_edit_profile( $args ) { } } + + /** + * Returns dropdown/multi-select options keys from a callback function + * @since 2019-05-30 + */ + if ( isset( $array[ 'options' ] ) && in_array( $array[ 'type' ], array( 'select', 'multiselect' ) ) ) { + if ( !empty( $array[ 'custom_dropdown_options_source' ] ) && function_exists( $array[ 'custom_dropdown_options_source' ] ) ) { + $options = call_user_func( $array[ 'custom_dropdown_options_source' ], $array[ 'options' ] ); + if( is_array( $options )){ + $array[ 'options' ] = array_keys( $options ); + } + } + } + + //validation of correct values from options in wp-admin $stripslashes = stripslashes( $args['submitted'][ $key ] ); if ( in_array( $array['type'], array( 'select' ) ) && diff --git a/includes/core/um-filters-fields.php b/includes/core/um-filters-fields.php index 66c87e88..0089f505 100644 --- a/includes/core/um-filters-fields.php +++ b/includes/core/um-filters-fields.php @@ -333,7 +333,7 @@ function um_profile_field_filter_hook__( $value, $data, $type = '' ) { if ( $data['validate'] == 'vk_url' ) $value = 'https://vk.com/' . $value; } - + if ( isset( $data['validate'] ) && $data['validate'] == 'skype' ) { $value = $value; @@ -351,15 +351,15 @@ function um_profile_field_filter_hook__( $value, $data, $type = '' ) { } if ( isset( $data['validate'] ) && $data['validate'] == 'skype' ) { - + $value = str_replace('https://','',$value ); $value = str_replace('http://','',$value ); - + $data['url_target'] = ( isset( $data['url_target'] ) ) ? $data['url_target'] : '_blank'; $value = ''.$value.''; - } - + } + if ( !is_array( $value ) ) { if ( is_email( $value ) ) $value = ''.$value.''; @@ -681,13 +681,13 @@ function um_profile_field_filter_xss_validation( $value, $data, $type = '' ) { } } } elseif ( 'select' == $type || 'radio' == $type ) { - if ( ! empty( $data['options'] ) && ! in_array( $value, $data['options'] ) ) { + if ( ! empty( $data['options'] ) && ! in_array( $value, $data['options'] ) && empty( $data[ 'custom_dropdown_options_source' ] ) ) { $value = ''; } } - } elseif ( ! empty( $value ) ) { + } elseif ( ! empty( $value ) && is_array( $value ) ) { if ( 'multiselect' == $type || 'checkbox' == $type ) { - if ( ! empty( $data['options'] ) && is_array( $value ) ) { + if ( ! empty( $data['options'] ) && empty( $data[ 'custom_dropdown_options_source' ] ) ) { $value = array_intersect( $value, $data['options'] ); } } From 4d53a629a03cf9ee188a5108a0233e9e5b6c9f35 Mon Sep 17 00:00:00 2001 From: Champ Camba Date: Mon, 3 Jun 2019 01:20:31 +0800 Subject: [PATCH 2/3] Add filter hooks to populate dropdown options alternative to callback function --- includes/core/um-actions-profile.php | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/includes/core/um-actions-profile.php b/includes/core/um-actions-profile.php index 46d71322..f99504e9 100644 --- a/includes/core/um-actions-profile.php +++ b/includes/core/um-actions-profile.php @@ -251,13 +251,19 @@ function um_user_edit_profile( $args ) { * Returns dropdown/multi-select options keys from a callback function * @since 2019-05-30 */ + $has_custom_source = apply_filters("um_has_dropdown_options_source__{$key}", false ); + if ( isset( $array[ 'options' ] ) && in_array( $array[ 'type' ], array( 'select', 'multiselect' ) ) ) { - if ( !empty( $array[ 'custom_dropdown_options_source' ] ) && function_exists( $array[ 'custom_dropdown_options_source' ] ) ) { + + if ( !empty( $array[ 'custom_dropdown_options_source' ] ) && function_exists( $array[ 'custom_dropdown_options_source' ] ) && ! $has_custom_source ) { $options = call_user_func( $array[ 'custom_dropdown_options_source' ], $array[ 'options' ] ); - if( is_array( $options )){ - $array[ 'options' ] = array_keys( $options ); + if( is_array( $options ) ){ + $array[ 'options' ] = apply_filters("um_custom_dropdown_options__{$key}", array_keys( $options ) ); } + }else{ + $array[ 'options' ] = apply_filters("um_custom_dropdown_options__{$key}", array() ); } + } @@ -265,7 +271,7 @@ function um_user_edit_profile( $args ) { $stripslashes = stripslashes( $args['submitted'][ $key ] ); if ( in_array( $array['type'], array( 'select' ) ) && ! empty( $array['options'] ) && ! empty( $stripslashes ) && - ! in_array( $stripslashes, array_map( 'trim', $array['options'] ) ) ) { + ! in_array( $stripslashes, array_map( 'trim', $array['options'] ) ) && ! $has_custom_source ) { continue; } @@ -311,6 +317,7 @@ function um_user_edit_profile( $args ) { } } + if ( isset( $args['submitted']['description'] ) ) { $to_update['description'] = $args['submitted']['description']; } From a11fd188fa06b361a60bdbfd3943f807d16aa9b8 Mon Sep 17 00:00:00 2001 From: Champ Camba Date: Sun, 9 Jun 2019 23:41:31 +0800 Subject: [PATCH 3/3] Fix displaying custom callback options value in profile view --- includes/core/class-fields.php | 60 +++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 26 deletions(-) diff --git a/includes/core/class-fields.php b/includes/core/class-fields.php index 3355583c..5bb57697 100644 --- a/includes/core/class-fields.php +++ b/includes/core/class-fields.php @@ -1068,45 +1068,53 @@ if ( ! class_exists( 'um\core\Fields' ) ) { */ function get_option_value_from_callback( $value, $data, $type ) { + if ( in_array( $type, array( 'select', 'multiselect' ) ) && ! empty( $data['custom_dropdown_options_source'] ) ) { - if ( function_exists( $data['custom_dropdown_options_source'] ) ) { + $has_custom_source = apply_filters("um_has_dropdown_options_source__{$data['metakey']}", false ); + + if( $has_custom_source ){ + + $opts = apply_filters("um_get_field__{$data['metakey']}", array() ); + $arr_options = $opts['options']; + + }else if ( function_exists( $data['custom_dropdown_options_source'] ) ) { $arr_options = call_user_func( $data['custom_dropdown_options_source'], ( ! empty( $data['parent_dropdown_relationship'] ) ? $data['parent_dropdown_relationship'] : '' ) ); + } - + if( $has_custom_source || function_exists( $data['custom_dropdown_options_source'] ) ){ if ( $type == 'select' ) { - if ( ! empty( $arr_options[ $value ] ) ) { - return $arr_options[ $value ]; - } elseif ( ! empty( $data['default'] ) && empty( $arr_options[ $value ] ) ) { - return $arr_options[ $data['default'] ]; - } else { - return ''; - } - } - - if ( $type == 'multiselect' ) { - - if ( is_array( $value ) ) { - $values = $value; - } else { - $values = explode( ', ', $value ); - } - - $arr_paired_options = array(); - - foreach ( $values as $option ) { - if ( isset( $arr_options[ $option ] ) ) { - $arr_paired_options[] = $arr_options[ $option ]; + if ( ! empty( $arr_options[ $value ] ) ) { + return $arr_options[ $value ]; + } elseif ( ! empty( $data['default'] ) && empty( $arr_options[ $value ] ) ) { + return $arr_options[ $data['default'] ]; + } else { + return ''; } } - return implode( ', ', $arr_paired_options ); - } + if ( $type == 'multiselect' ) { + if ( is_array( $value ) ) { + $values = $value; + } else { + $values = explode( ', ', $value ); + } + + $arr_paired_options = array(); + + foreach ( $values as $option ) { + if ( isset( $arr_options[ $option ] ) ) { + $arr_paired_options[] = $arr_options[ $option ]; + } + } + + return implode( ', ', $arr_paired_options ); + } }