From 8859c48cdcdc8f6cdb5c31805bf7da33e5f1d665 Mon Sep 17 00:00:00 2001 From: nikitasinelnikov Date: Thu, 21 Nov 2019 11:41:52 +0200 Subject: [PATCH] - fixed select-type filters options; --- assets/js/um-scripts.js | 19 +++++++++--------- includes/admin/core/class-admin-builder.php | 6 ++---- includes/core/class-fields.php | 16 +++++++++------ includes/core/class-member-directory.php | 22 +++++++++++++++++---- 4 files changed, 39 insertions(+), 24 deletions(-) diff --git a/assets/js/um-scripts.js b/assets/js/um-scripts.js index c0088ee7..907bb6f6 100644 --- a/assets/js/um-scripts.js +++ b/assets/js/um-scripts.js @@ -469,7 +469,6 @@ jQuery(document).ready(function() { var me = jQuery(this); var parent_option = me.data('um-parent'); - var um_ajax_url = me.data('um-ajax-url'); var um_ajax_source = me.data('um-ajax-source'); var original_value = me.val(); @@ -480,7 +479,7 @@ jQuery(document).ready(function() { var form_id = parent.closest('form').find('input[type="hidden"][name="form_id"]').val(); var arr_key = parent.val(); - if ( parent.val() != '' && typeof um_select_options_cache[ arr_key ] != 'object' ) { + if ( arr_key != '' && typeof um_select_options_cache[ arr_key ] != 'object' ) { jQuery.ajax({ url: wp.ajax.settings.url, @@ -488,19 +487,19 @@ jQuery(document).ready(function() { data: { action: 'um_select_options', parent_option_name: parent_option, - parent_option: parent.val(), + parent_option: arr_key, child_callback: um_ajax_source, - child_name: me.attr('name'), - members_directory: me.attr('data-mebers-directory'), + child_name: me.attr('name'), + members_directory: me.attr('data-mebers-directory'), form_id: form_id, nonce: um_scripts.nonce }, success: function( data ){ - if( data.status == 'success' && parent.val() != '' ){ - um_field_populate_child_options( me, data, arr_key); + if ( data.status == 'success' && arr_key != '' ) { + um_field_populate_child_options( me, data, arr_key ); } - if( typeof data.debug !== 'undefined' ){ + if ( typeof data.debug !== 'undefined' ) { console.log( data ); } }, @@ -512,12 +511,12 @@ jQuery(document).ready(function() { } - if ( parent.val() != '' && typeof um_select_options_cache[ arr_key ] == 'object' ) { + if ( arr_key != '' && typeof um_select_options_cache[ arr_key ] == 'object' ) { var data = um_select_options_cache[ arr_key ]; um_field_populate_child_options( me, data, arr_key ); } - if ( parent.val() == '' ){ + if ( arr_key == '' ) { me.find('option[value!=""]').remove(); me.val('').trigger('change'); } diff --git a/includes/admin/core/class-admin-builder.php b/includes/admin/core/class-admin-builder.php index ae1914ea..2008ebe0 100644 --- a/includes/admin/core/class-admin-builder.php +++ b/includes/admin/core/class-admin-builder.php @@ -1150,15 +1150,13 @@ if ( ! class_exists( 'um\admin\core\Admin_Builder' ) ) { * @return boolean */ public function skip_field_validation( $skip, $post_input, $array ) { - if ( $post_input === '_options' && isset( $array['post']['_custom_dropdown_options_source'] ) ) { - $um_callback_func = $array['post']['_custom_dropdown_options_source']; - $skip = function_exists( $um_callback_func ); + $skip = function_exists( $array['post']['_custom_dropdown_options_source'] ); } return $skip; } - + /** * Retrieves dropdown/multi-select options from a callback function diff --git a/includes/core/class-fields.php b/includes/core/class-fields.php index 42e6f21d..94736d01 100644 --- a/includes/core/class-fields.php +++ b/includes/core/class-fields.php @@ -1124,11 +1124,14 @@ if ( ! class_exists( 'um\core\Fields' ) ) { $arr_options = $opts['options']; } elseif ( function_exists( $data['custom_dropdown_options_source'] ) ) { + if ( isset( $data['parent_dropdown_relationship'] ) ) { + $_POST['parent_option_name'] = $data['parent_dropdown_relationship']; + $_POST['parent_option'] = um_user( $data['parent_dropdown_relationship'] ); - $arr_options = call_user_func( - $data['custom_dropdown_options_source'], - ( ! empty( $data['parent_dropdown_relationship'] ) ? $data['parent_dropdown_relationship'] : '' ) - ); + $arr_options = call_user_func( $data['custom_dropdown_options_source'], $data['parent_dropdown_relationship'] ); + } else { + $arr_options = call_user_func( $data['custom_dropdown_options_source'] ); + } } if ( $has_custom_source || function_exists( $data['custom_dropdown_options_source'] ) ) { @@ -2783,8 +2786,9 @@ if ( ! class_exists( 'um\core\Fields' ) ) { * } * ?> */ - $ajax_source_url = apply_filters( "um_custom_dropdown_options_source_url__{$form_key}", admin_url( 'admin-ajax.php' ), $data ); - $atts_ajax .= ' data-um-ajax-url="' . esc_url( $ajax_source_url ) . '" '; + // todo maybe deprecate +// $ajax_source_url = apply_filters( "um_custom_dropdown_options_source_url__{$form_key}", admin_url( 'admin-ajax.php' ), $data ); +// $atts_ajax .= ' data-um-ajax-url="' . esc_url( $ajax_source_url ) . '" '; } diff --git a/includes/core/class-member-directory.php b/includes/core/class-member-directory.php index 4570e541..e6b71e97 100644 --- a/includes/core/class-member-directory.php +++ b/includes/core/class-member-directory.php @@ -524,11 +524,25 @@ if ( ! class_exists( 'um\core\Member_Directory' ) ) { if ( ! empty( $attrs['custom_dropdown_options_source'] ) ) { $attrs['custom'] = true; $attrs['options'] = UM()->fields()->get_options_from_callback( $attrs, $attrs['type'] ); - $custom_dropdown = ' data-um-ajax-source="' . esc_attr( $attrs['custom_dropdown_options_source'] ) . '"'; + + if ( ! empty( $attrs['parent_dropdown_relationship'] ) ) { + $custom_dropdown .= ' data-member-directory="yes"'; + $custom_dropdown .= ' data-um-parent="' . esc_attr( $attrs['parent_dropdown_relationship'] ) . '"'; + + if ( isset( $_GET[ 'filter_' . $attrs['parent_dropdown_relationship'] . '_' . $unique_hash ] ) ) { + $_POST['parent_option_name'] = $attrs['parent_dropdown_relationship']; + $_POST['parent_option'] = explode( '||', filter_input( INPUT_GET, 'filter_' . $attrs['parent_dropdown_relationship'] . '_' . $unique_hash ) ); + } + } + + $ajax_source = apply_filters( "um_custom_dropdown_options_source__{$filter}", $attrs['custom_dropdown_options_source'], $attrs ); + $custom_dropdown .= ' data-um-ajax-source="' . esc_attr( $ajax_source ) . '" '; } - if ( $attrs['metakey'] != 'role_select' ) { + if ( $attrs['metakey'] != 'role_select' && empty( $custom_dropdown ) ) { $attrs['options'] = array_intersect( $attrs['options'], $values_array ); + } elseif ( ! empty( $custom_dropdown ) ) { + $attrs['options'] = array_intersect_key( $attrs['options'], array_flip( $values_array ) ); } else { $attrs['options'] = array_intersect_key( $attrs['options'], array_flip( $values_array ) ); } @@ -545,7 +559,7 @@ if ( ! class_exists( 'um\core\Member_Directory' ) ) { asort( $attrs['options'] ); ?> -