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 .= '