From 06983833af296d8a7a35cb21d8d4eaab5c9b1358 Mon Sep 17 00:00:00 2001 From: Champ Camba Date: Sun, 6 Nov 2016 19:36:22 +0800 Subject: [PATCH] Adds new select/multi-select options to retrieve options from a callback --- admin/assets/js/um-admin-modal.js | 35 ++++++++++- admin/core/um-admin-actions-ajax.php | 29 ++++++++- admin/core/um-admin-metabox.php | 11 ++++ core/um-builtin.php | 6 +- core/um-fields.php | 88 +++++++++++++++++++++++++--- core/um-filters-fields.php | 48 ++++++++++++++- core/um-filters-members.php | 18 ++++++ core/um-members.php | 4 ++ core/um-short-functions.php | 2 +- 9 files changed, 226 insertions(+), 15 deletions(-) diff --git a/admin/assets/js/um-admin-modal.js b/admin/assets/js/um-admin-modal.js index 9911fb32..5f3b841b 100644 --- a/admin/assets/js/um-admin-modal.js +++ b/admin/assets/js/um-admin-modal.js @@ -328,4 +328,37 @@ jQuery(document).ready(function() { um_admin_modal_responsive(); }); -}); \ No newline at end of file + + /** + * Retrieve options from a callback function + */ + jQuery(document).on('blur',"#_custom_dropdown_options_source", function(){ + var me = jQuery(this); + var _options = jQuery('textarea[id=_options]'); + + if( me.val() != '' ){ + var um_option_callback = me.val(); + jQuery.ajax({ + url: ultimatemember_ajax_url, + type: 'POST', + data: {action: 'ultimatemember_populate_dropdown_options', um_option_callback: um_option_callback }, + complete: function(){ + + }, + success: function( response ){ + var arr_opts = []; + + for (var key in response.data ){ + arr_opts.push( response.data[ key ] ); + } + + _options.val( arr_opts.join('\n') ); + + } + }); + } + + }); + +}); // end jQuery(document).ready + diff --git a/admin/core/um-admin-actions-ajax.php b/admin/core/um-admin-actions-ajax.php index 08a965dc..6568468a 100644 --- a/admin/core/um-admin-actions-ajax.php +++ b/admin/core/um-admin-actions-ajax.php @@ -48,4 +48,31 @@ if(is_array($output)){ print_r($output); }else{ echo $output; } die; - } \ No newline at end of file + } + + /** + * Retrieves dropdown/multi-select options from a callback function + */ + add_action('wp_ajax_nopriv_ultimatemember_populate_dropdown_options', 'ultimatemember_populate_dropdown_options'); + add_action('wp_ajax_ultimatemember_populate_dropdown_options', 'ultimatemember_populate_dropdown_options'); + function ultimatemember_populate_dropdown_options(){ + + $arr_options = array(); + + $um_callback_func = $_POST['um_option_callback']; + if( empty( $um_callback_func ) ){ + $arr_options['status'] = 'empty'; + $arr_options['function_name'] = $um_callback_func; + $arr_options['function_exists'] = function_exists( $um_callback_func ); + } + + $arr_options['data'] = array(); + + if( function_exists( $um_callback_func ) ){ + $arr_options['data'] = call_user_func( $um_callback_func ); + } + + wp_send_json( $arr_options ); + } + + \ No newline at end of file diff --git a/admin/core/um-admin-metabox.php b/admin/core/um-admin-metabox.php index 0ea5482a..2e0cc98d 100644 --- a/admin/core/um-admin-metabox.php +++ b/admin/core/um-admin-metabox.php @@ -1505,6 +1505,17 @@ class UM_Admin_Metabox { + +

+ +

+ + array( 'name' => 'Dropdown', 'col1' => array('_title','_metakey','_help','_default','_options','_visibility'), - 'col2' => array('_label','_placeholder','_public','_roles'), + 'col2' => array('_label','_placeholder','_public','_roles','_custom_dropdown_options_source','_parent_dropdown_relationship'), 'col3' => array('_required','_editable','_icon'), 'validate' => array( '_title' => array( - 'mode' => 'required', + 'mode' => 'required', 'error' => __('You must provide a title','ultimatemember') ), '_metakey' => array( @@ -202,7 +202,7 @@ class UM_Builtin { 'multiselect' => array( 'name' => 'Multi-Select', 'col1' => array('_title','_metakey','_help','_default','_options','_visibility'), - 'col2' => array('_label','_placeholder','_public','_roles','_min_selections','_max_selections'), + 'col2' => array('_label','_placeholder','_public','_roles','_min_selections','_max_selections','_custom_dropdown_options_source','_parent_dropdown_relationship'), 'col3' => array('_required','_editable','_icon'), 'validate' => array( '_title' => array( diff --git a/core/um-fields.php b/core/um-fields.php index c9024c44..d15fd3a3 100644 --- a/core/um-fields.php +++ b/core/um-fields.php @@ -368,27 +368,27 @@ class UM_Fields { global $ultimatemember; - if ( isset($_SESSION) && isset($_SESSION['um_social_profile'][$key]) && isset( $this->set_mode ) && $this->set_mode == 'register' ) - return $_SESSION['um_social_profile'][$key]; + if ( isset( $_SESSION ) && isset( $_SESSION['um_social_profile'][ $key ] ) && isset( $this->set_mode ) && $this->set_mode == 'register' ) + return $_SESSION['um_social_profile'][ $key ]; - $type = (isset($data['type']))?$data['type']:''; + $type = ( isset( $data['type'] ) ) ? $data['type'] : ''; // preview in backend if ( isset( $ultimatemember->user->preview ) && $ultimatemember->user->preview ) { $submitted = um_user('submitted'); - if ( isset( $submitted[$key] ) && !empty( $submitted[$key] ) ) { - return $submitted[$key]; + if ( isset( $submitted[ $key ] ) && ! empty( $submitted[ $key ] ) ) { + return $submitted[ $key ]; } else { return 'Undefined'; } } // normal state - if ( isset($ultimatemember->form->post_form[$key]) ) { + if ( isset( $ultimatemember->form->post_form[ $key ] ) ) { if ( strstr( $key, 'user_pass' ) && $this->set_mode != 'password' ) return ''; - return $ultimatemember->form->post_form[$key]; + return $ultimatemember->form->post_form[ $key ]; } else if ( um_user( $key ) && $this->editing == true ) { @@ -396,7 +396,7 @@ class UM_Fields { return apply_filters( "um_edit_{$key}_field_value", um_user( $key ), $key ); - } else if ( ( um_user( $key ) || isset($data['show_anyway']) ) && $this->viewing == true ) { + } else if ( ( um_user( $key ) || isset( $data['show_anyway'] ) ) && $this->viewing == true ) { $value = um_filtered_value( $key, $data ); return $value; @@ -556,6 +556,67 @@ class UM_Fields { return ''; } + /** + * Get selected option from a callback function + */ + function get_option_value_from_callback( $value, $data, $type ){ + + + if( in_array( $type , array('select','multiselect') ) && isset( $data['custom_dropdown_options_source'] ) && ! empty( $data['custom_dropdown_options_source'] ) ){ + + if( function_exists( $data['custom_dropdown_options_source'] ) ){ + + $arr_options = call_user_func( $data['custom_dropdown_options_source'] ); + + if( $type == 'select' ){ + return isset( $arr_options[ $value ] ) ? $arr_options[ $value ]: '' ; + } + + if( $type == 'multiselect' ){ + + if( is_array( $value ) ){ + $values = $value; + }else{ + $values = explode(', ', $value ); + } + + $arr_paired_options = array(); + + foreach ( $values as $option ) { + $arr_paired_options[] = $arr_options[ $option ]; + } + + return implode( ', ' , $arr_paired_options ); + } + + } + + + } + + return $value; + } + + /** + * Get select options from a callback function + */ + function get_options_from_callback( $data, $type ){ + + + if( in_array( $type , array('select','multiselect') ) && isset( $data['custom_dropdown_options_source'] ) && ! empty( $data['custom_dropdown_options_source'] ) ){ + + if( function_exists( $data['custom_dropdown_options_source'] ) ){ + + $arr_options = call_user_func( $data['custom_dropdown_options_source'] ); + + } + + + } + + return $arr_options; + } + /*** *** @Get Field Type ***/ @@ -1653,6 +1714,11 @@ class UM_Fields { $output .= ''; $field_value = ''; + + // switch options pair for custom options from a callback function + if( isset( $data['custom_dropdown_options_source'] ) && ! empty( $data['custom_dropdown_options_source'] ) ){ + $options_pair = true; + } // add options foreach($options as $k => $v) { @@ -1743,6 +1809,11 @@ class UM_Fields { $options = apply_filters("um_multiselect_options_{$key}", $options ); $options = apply_filters("um_multiselect_options_{$data['type']}", $options, $data ); } + + // switch options pair for custom options from a callback function + if( isset( $data['custom_dropdown_options_source'] ) && ! empty( $data['custom_dropdown_options_source'] ) ){ + $use_keyword = true; + } // add an empty option! $output .= ''; @@ -2279,6 +2350,7 @@ class UM_Fields { $res = stripslashes( $res ); } + $res = apply_filters("um_view_field", $res, $data, $type ); $res = apply_filters("um_view_field_value_{$type}", $res, $data ); $output .= '
'; diff --git a/core/um-filters-fields.php b/core/um-filters-fields.php index 0879bc0f..9ed8051c 100644 --- a/core/um-filters-fields.php +++ b/core/um-filters-fields.php @@ -383,4 +383,50 @@ } return $value; - } \ No newline at end of file + } + + + /** + * Returns dropdown/multi-select options from a callback function + * @param $options array + * @param $data array + * @return $options array + * @uses hook filter: um_select_dropdown_dynamic_options, um_multiselect_options + */ + add_filter('um_select_dropdown_dynamic_options','um_select_dropdown_dynamic_callback_options', 10, 2); + add_filter('um_multiselect_options','um_select_dropdown_dynamic_callback_options', 10, 2); + function um_select_dropdown_dynamic_callback_options( $options, $data ){ + + if( isset( $data['custom_dropdown_options_source'] ) && ! empty( $data['custom_dropdown_options_source'] ) ){ + + if( function_exists( $data['custom_dropdown_options_source'] ) ){ + $options = call_user_func( $data['custom_dropdown_options_source'] ); + } + } + + return $options; + } + + /** + * + * Pair dropdown/multi-select options from a callback function + * @param $value string + * @param $type string + * @param $data array + * @return $value string + * @uses hook filter: um_view_field + */ + add_filter('um_profile_field_filter_hook__','um_select_dropdown_callback_view_field', 10, 3); + function um_select_dropdown_callback_view_field( $value, $data, $type ){ + global $ultimatemember; + + if( in_array( $type , array('select','multiselect') ) && isset( $data['custom_dropdown_options_source'] ) && ! empty( $data['custom_dropdown_options_source'] ) ){ + + return $ultimatemember->fields->get_option_value_from_callback( $value, $data, $type ); + + + } + + return $value; + } + diff --git a/core/um-filters-members.php b/core/um-filters-members.php index 4f30affd..36ea9467 100644 --- a/core/um-filters-members.php +++ b/core/um-filters-members.php @@ -335,3 +335,21 @@ return $result; } + /** + * Retrieves search filter options from a callback + * @param $atts array + * @return $atts array + */ + add_filter('um_search_select_fields','um_search_select_fields'); + function um_search_select_fields( $atts ){ + + global $ultimatemember; + + if( isset( $atts['custom_dropdown_options_source'] ) && ! empty( $atts['custom_dropdown_options_source'] ) ){ + $atts['custom'] = true; + $atts['options'] = $ultimatemember->fields->get_options_from_callback( $atts, $atts['type'] ); + } + + return $atts; + } + diff --git a/core/um-members.php b/core/um-members.php index 778b9d14..c4392c3f 100644 --- a/core/um-members.php +++ b/core/um-members.php @@ -84,6 +84,10 @@ class UM_Members { // filter all search fields $attrs = apply_filters( 'um_search_fields', $attrs ); + + if( $type == 'select' ){ + $attrs = apply_filters( 'um_search_select_fields', $attrs ); + } switch( $type ) { diff --git a/core/um-short-functions.php b/core/um-short-functions.php index 414c0270..87d36e68 100644 --- a/core/um-short-functions.php +++ b/core/um-short-functions.php @@ -471,7 +471,7 @@ function um_user_ip() { $type = ( isset($data['type']) ) ? $data['type'] : ''; - $value = apply_filters("um_profile_field_filter_hook__", $value, $data ); + $value = apply_filters("um_profile_field_filter_hook__", $value, $data, $type ); $value = apply_filters("um_profile_field_filter_hook__{$key}", $value, $data ); $value = apply_filters("um_profile_field_filter_hook__{$type}", $value, $data );