Adds new select/multi-select options to retrieve options from a callback

This commit is contained in:
Champ Camba
2016-11-06 19:36:22 +08:00
parent 71ef64623d
commit 06983833af
9 changed files with 226 additions and 15 deletions
+34 -1
View File
@@ -328,4 +328,37 @@ jQuery(document).ready(function() {
um_admin_modal_responsive();
});
});
/**
* 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
+28 -1
View File
@@ -48,4 +48,31 @@
if(is_array($output)){ print_r($output); }else{ echo $output; } die;
}
}
/**
* 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 );
}
+11
View File
@@ -1505,6 +1505,17 @@ class UM_Admin_Metabox {
<?php
break;
case '_custom_dropdown_options_source':
?>
<p><label for="_placeholder">Options Callback<?php $this->tooltip('Add a callback source to retrieve options.'); ?></label>
<input type="text" name="_custom_dropdown_options_source" id="_custom_dropdown_options_source" value="<?php echo htmlspecialchars($this->edit_mode_value, ENT_QUOTES); ?>" />
</p>
<?php
break;
}
+3 -3
View File
@@ -182,11 +182,11 @@ class UM_Builtin {
'select' => 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(
+80 -8
View File
@@ -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 .= '<option value=""></option>';
$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 .= '<option value=""></option>';
@@ -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 .= '<div class="um-field-area">';
+47 -1
View File
@@ -383,4 +383,50 @@
}
return $value;
}
}
/**
* 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;
}
+18
View File
@@ -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;
}
+4
View File
@@ -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 ) {
+1 -1
View File
@@ -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 );