| 1 |
<?php |
| 2 |
// Exit if accessed directly |
| 3 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 4 |
|
| 5 |
/* handle field output */ |
| 6 |
function wppb_select_handler( $output, $form_location, $field, $user_id, $field_check_errors, $request_data ){ |
| 7 |
if ( $field['field'] == 'Select' ){ |
| 8 |
$item_title = apply_filters( 'wppb_'.$form_location.'_select_custom_field_'.$field['id'].'_item_title', wppb_icl_t( 'plugin profile-builder-pro', 'custom_field_'.$field['id'].'_title_translation', $field['field-title'], true ) ); |
| 9 |
$item_description = wppb_icl_t( 'plugin profile-builder-pro', 'custom_field_'.$field['id'].'_description_translation', $field['description'], true ); |
| 10 |
$item_option_labels = wppb_icl_t( 'plugin profile-builder-pro', 'custom_field_'.$field['id'].'_labels_translation', $field['labels'], true ); |
| 11 |
|
| 12 |
$select_labels = apply_filters( 'wppb_select_labels_array', explode( ',', $item_option_labels ), $field, $form_location, $user_id, $request_data ); |
| 13 |
$select_values = apply_filters( 'wppb_select_options_array', explode( ',', $field['options'] ), $field, $form_location, $user_id, $request_data ); |
| 14 |
|
| 15 |
$extra_attr = apply_filters( 'wppb_extra_attribute', '', $field, $form_location ); |
| 16 |
|
| 17 |
if( $form_location != 'register' ) |
| 18 |
$input_value = ( ( wppb_user_meta_exists ( $user_id, $field['meta-name'] ) != null ) ? stripslashes( get_user_meta( $user_id, $field['meta-name'], true ) ) : $field['default-option'] ); |
| 19 |
else |
| 20 |
$input_value = ( isset( $field['default-option'] ) ? trim( $field['default-option'] ) : '' ); |
| 21 |
|
| 22 |
$input_value = ( isset( $request_data[wppb_handle_meta_name( $field['meta-name'] )] ) ? stripslashes( trim( $request_data[wppb_handle_meta_name( $field['meta-name'] )] ) ) : $input_value ); |
| 23 |
$input_value = apply_filters( 'wppb_form_select_field_value', $input_value, $field, $form_location ); |
| 24 |
|
| 25 |
if ( $form_location != 'back_end' ){ |
| 26 |
$error_mark = ( ( $field['required'] == 'Yes' ) ? '<span class="wppb-required" title="'.wppb_required_field_error($field["field-title"]).'">*</span>' : '' ); |
| 27 |
|
| 28 |
if ( array_key_exists( $field['id'], $field_check_errors ) ) |
| 29 |
$error_mark = '<img src="'.WPPB_PLUGIN_URL.'assets/images/pencil_delete.png" title="'.wppb_required_field_error($field["field-title"]).'"/>'; |
| 30 |
|
| 31 |
$output = ' |
| 32 |
<label for="'.$field['meta-name'].'">'.$item_title.$error_mark.'</label> |
| 33 |
<select name="'.$field['meta-name'].'" id="'.$field['meta-name'].'" class="custom_field_select" '. $extra_attr .'>'; |
| 34 |
|
| 35 |
$extra_select_option = apply_filters( 'wppb_extra_select_option', '', $field, $item_title ); |
| 36 |
if( ! empty( $extra_select_option ) ) { |
| 37 |
$output .= $extra_select_option; |
| 38 |
} |
| 39 |
|
| 40 |
foreach( $select_values as $key => $value){ |
| 41 |
$output .= '<option value="'.esc_attr( trim( $value ) ).'" class="custom_field_select_option '. apply_filters( 'wppb_fields_extra_css_class', '', $field ) .'" '; |
| 42 |
|
| 43 |
if ( $input_value === trim( $value ) ) |
| 44 |
$output .= ' selected'; |
| 45 |
|
| 46 |
$output .= '>'.( ( !isset( $select_labels[$key] ) || !$select_labels[$key] ) ? trim( $select_values[$key] ) : trim( $select_labels[$key] ) ).'</option>'; |
| 47 |
} |
| 48 |
|
| 49 |
$output .= ' |
| 50 |
</select>'; |
| 51 |
if( !empty( $item_description ) ) |
| 52 |
$output .= '<span class="wppb-description-delimiter">'.$item_description.'</span>'; |
| 53 |
|
| 54 |
}else{ |
| 55 |
$item_title = ( ( $field['required'] == 'Yes' ) ? $item_title .' <span class="description">('. __( 'required', 'profile-builder' ) .')</span>' : $item_title ); |
| 56 |
$output = ' |
| 57 |
<table class="form-table"> |
| 58 |
<tr> |
| 59 |
<th><label for="'.$field['meta-name'].'">'.$item_title.'</label></th> |
| 60 |
<td> |
| 61 |
<select name="'.$field['meta-name'].'" class="custom_field_select" id="'.$field['meta-name'].'" '. $extra_attr .'>'; |
| 62 |
|
| 63 |
foreach( $select_values as $key => $value){ |
| 64 |
$output .= '<option value="'.esc_attr( trim( $value ) ).'" class="custom_field_select_option" '; |
| 65 |
|
| 66 |
if ( $input_value === trim( $value ) ) |
| 67 |
$output .= ' selected'; |
| 68 |
|
| 69 |
$output .= '>'.( ( !isset( $select_labels[$key] ) || !$select_labels[$key] ) ? trim( $select_values[$key] ) : trim( $select_labels[$key] ) ).'</option>'; |
| 70 |
} |
| 71 |
|
| 72 |
$output .= '</select> |
| 73 |
<span class="description">'.$item_description.'</span> |
| 74 |
</td> |
| 75 |
</tr> |
| 76 |
</table>'; |
| 77 |
} |
| 78 |
|
| 79 |
return apply_filters( 'wppb_'.$form_location.'_select_custom_field_'.$field['id'], $output, $form_location, $field, $user_id, $field_check_errors, $request_data, $input_value ); |
| 80 |
} |
| 81 |
} |
| 82 |
add_filter( 'wppb_output_form_field_select', 'wppb_select_handler', 10, 6 ); |
| 83 |
add_filter( 'wppb_admin_output_form_field_select', 'wppb_select_handler', 10, 6 ); |
| 84 |
|
| 85 |
|
| 86 |
/* handle field save */ |
| 87 |
function wppb_save_select_value( $field, $user_id, $request_data, $form_location ){ |
| 88 |
if( $field['field'] == 'Select' ){ |
| 89 |
if ( isset( $request_data[wppb_handle_meta_name( $field['meta-name'] )] ) ) |
| 90 |
update_user_meta( $user_id, $field['meta-name'], sanitize_text_field( $request_data[wppb_handle_meta_name( $field['meta-name'] )] ) ); |
| 91 |
} |
| 92 |
} |
| 93 |
add_action( 'wppb_save_form_field', 'wppb_save_select_value', 10, 4 ); |
| 94 |
add_action( 'wppb_backend_save_form_field', 'wppb_save_select_value', 10, 4 ); |
| 95 |
|
| 96 |
|
| 97 |
/* handle field validation */ |
| 98 |
function wppb_check_select_value( $message, $field, $request_data, $form_location ){ |
| 99 |
if( $field['field'] == 'Select' ){ |
| 100 |
if( $field['required'] == 'Yes' ){ |
| 101 |
if ( ( isset( $request_data[wppb_handle_meta_name( $field['meta-name'] )] ) && ( trim( $request_data[wppb_handle_meta_name( $field['meta-name'] )] ) == '' ) ) || !isset( $request_data[wppb_handle_meta_name( $field['meta-name'] )] ) ){ |
| 102 |
return wppb_required_field_error($field["field-title"]); |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
return $message; |
| 108 |
} |
| 109 |
add_filter( 'wppb_check_form_field_select', 'wppb_check_select_value', 10, 4 ); |