| 1 |
<?php |
| 2 |
// Exit if accessed directly |
| 3 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 4 |
|
| 5 |
/* |
| 6 |
* Handle field output. |
| 7 |
* |
| 8 |
* @param string $output Contains the HTML to display the select2 field. |
| 9 |
* @param string $form_location |
| 10 |
* @param array $field |
| 11 |
* @param integer $user_id |
| 12 |
* @param array $field_check_errors |
| 13 |
* @param array $request_data |
| 14 |
* @return string Filtered output of the HTML to display the select2 field. |
| 15 |
*/ |
| 16 |
function wppb_select2_display_handler($output, $form_location, $field, $user_id, $field_check_errors, $request_data) |
| 17 |
{ |
| 18 |
if ($field['field'] == 'Select2') { |
| 19 |
wp_enqueue_script('wppb_sl2_lib_js', WPPB_PLUGIN_URL . 'assets/js/select2/select2.min.js', array('jquery')); |
| 20 |
wp_enqueue_style('wppb_sl2_lib_css', WPPB_PLUGIN_URL . 'assets/css/select2/select2.min.css'); |
| 21 |
|
| 22 |
wp_enqueue_script( 'wppb_sl2_js', WPPB_PLUGIN_URL.'front-end/default-fields/select2/select2.js', array('jquery'), PROFILE_BUILDER_VERSION, true ); |
| 23 |
wp_enqueue_style( 'wppb_sl2_css', WPPB_PLUGIN_URL.'front-end/default-fields/select2/select2.css', false, PROFILE_BUILDER_VERSION ); |
| 24 |
|
| 25 |
|
| 26 |
$field['labels'] = apply_filters('wppb_select2_labels', $field['labels'], $form_location, $field, $user_id, $field_check_errors, $request_data); |
| 27 |
$field['options'] = apply_filters('wppb_select2_options', $field['options'], $form_location, $field, $user_id, $field_check_errors, $request_data); |
| 28 |
$arguments = apply_filters('wppb_select2_arguments', array(), $form_location, $field); |
| 29 |
|
| 30 |
$item_title = apply_filters('wppb_' . $form_location . '_select2_custom_field_' . $field['id'] . '_item_title', wppb_icl_t('plugin profile-builder-pro', 'custom_field_' . $field['id'] . '_title_translation', $field['field-title'], true)); |
| 31 |
$item_description = wppb_icl_t('plugin profile-builder-pro', 'custom_field_' . $field['id'] . '_description_translation', $field['description'], true); |
| 32 |
$item_option_labels = wppb_icl_t('plugin profile-builder-pro', 'custom_field_' . $field['id'] . '_labels_translation', $field['labels'], true); |
| 33 |
|
| 34 |
$select2_labels = apply_filters('wppb_select2_labels_array', explode(',', $item_option_labels)); |
| 35 |
$select2_values = apply_filters('wppb_select2_options_array', explode(',', $field['options'])); |
| 36 |
|
| 37 |
$extra_attr = apply_filters('wppb_extra_attribute', '', $field, $form_location); |
| 38 |
|
| 39 |
if ($form_location != 'register') |
| 40 |
$input_value = ((wppb_user_meta_exists($user_id, $field['meta-name']) != null) ? esc_attr(stripslashes(get_user_meta($user_id, $field['meta-name'], true))) : $field['default-option']); |
| 41 |
else |
| 42 |
$input_value = (!empty($field['default-option']) ? esc_attr(trim($field['default-option'])) : ''); |
| 43 |
|
| 44 |
$input_value = (isset($request_data[wppb_handle_meta_name($field['meta-name'])]) ? esc_attr(stripslashes(trim($request_data[wppb_handle_meta_name($field['meta-name'])]))) : $input_value); |
| 45 |
|
| 46 |
$input_value = html_entity_decode( htmlspecialchars_decode( $input_value, ENT_QUOTES ), ENT_QUOTES ); |
| 47 |
$input_value = apply_filters( 'wppb_form_select2_field_value', $input_value, $field, $form_location ); |
| 48 |
|
| 49 |
if ($form_location != 'back_end') { |
| 50 |
$error_mark = (($field['required'] == 'Yes') ? '<span class="wppb-required" title="' . wppb_required_field_error($field["field-title"]) . '">*</span>' : ''); |
| 51 |
|
| 52 |
if (array_key_exists($field['id'], $field_check_errors)) |
| 53 |
$error_mark = '<img src="' . WPPB_PLUGIN_URL . 'assets/images/pencil_delete.png" title="' . wppb_required_field_error($field["field-title"]) . '"/>'; |
| 54 |
|
| 55 |
$output = ' |
| 56 |
<label for="' . esc_attr($field['meta-name']) . '">' . $item_title . $error_mark . '</label> |
| 57 |
<select name="' . esc_attr($field['meta-name']) . '" id="' . esc_attr($field['meta-name']) . '" class="custom_field_select2" ' . $extra_attr . ' data-wppb-select2-arguments=\'' . json_encode($arguments) . '\'>'; |
| 58 |
|
| 59 |
$extra_select_option = apply_filters('wppb_extra_select_option', '', $field, $item_title); |
| 60 |
if (!empty($extra_select_option)) { |
| 61 |
$output .= $extra_select_option; |
| 62 |
} |
| 63 |
|
| 64 |
foreach ($select2_values as $key => $value) { |
| 65 |
$output .= '<option value="' . esc_attr(trim($value)) . '" class="custom_field_select2_option ' . apply_filters('wppb_fields_extra_css_class', '', $field) . '" '; |
| 66 |
|
| 67 |
if ($input_value === trim($value)) |
| 68 |
$output .= ' selected'; |
| 69 |
|
| 70 |
$output .= '>' . ((!isset($select2_labels[$key]) || !$select2_labels[$key]) ? esc_attr(trim($select2_values[$key])) : esc_attr(trim($select2_labels[$key]))) . '</option>'; |
| 71 |
} |
| 72 |
|
| 73 |
$output .= ' |
| 74 |
</select>'; |
| 75 |
if (!empty($item_description)) |
| 76 |
$output .= '<span class="wppb-description-delimiter">' . $item_description . '</span>'; |
| 77 |
|
| 78 |
} else { |
| 79 |
$item_title = (($field['required'] == 'Yes') ? $item_title . ' <span class="description">(' . __('required', 'profile-builder') . ')</span>' : $item_title); |
| 80 |
$output = ' |
| 81 |
<table class="form-table"> |
| 82 |
<tr> |
| 83 |
<th><label for="' . esc_attr($field['meta-name']) . '">' . $item_title . '</label></th> |
| 84 |
<td> |
| 85 |
<select name="' . esc_attr($field['meta-name']) . '" class="custom_field_select2 wppb-select2" id="' . esc_attr($field['meta-name']) . '" ' . $extra_attr . ' data-wppb-select2-arguments=\'' . json_encode($arguments) . '\'>'; |
| 86 |
|
| 87 |
foreach ($select2_values as $key => $value) { |
| 88 |
$output .= '<option value="' . esc_attr(trim($value)) . '" class="custom_field_select2_option" '; |
| 89 |
|
| 90 |
if ($input_value === trim($value)) |
| 91 |
$output .= ' selected'; |
| 92 |
|
| 93 |
$output .= '>' . ((!isset($select2_labels[$key]) || !$select2_labels[$key]) ? esc_attr(trim($select2_values[$key])) : esc_attr(trim($select2_labels[$key]))) . '</option>'; |
| 94 |
} |
| 95 |
|
| 96 |
$output .= '</select> |
| 97 |
<span class="description">' . $item_description . '</span> |
| 98 |
</td> |
| 99 |
</tr> |
| 100 |
</table>'; |
| 101 |
} |
| 102 |
|
| 103 |
return apply_filters('wppb_' . $form_location . '_select2_custom_field_' . $field['id'], $output, $form_location, $field, $user_id, $field_check_errors, $request_data, $input_value); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
add_filter('wppb_output_form_field_select2', 'wppb_select2_display_handler', 10, 6); |
| 108 |
add_filter('wppb_admin_output_form_field_select2', 'wppb_select2_display_handler', 10, 6); |
| 109 |
|
| 110 |
/* |
| 111 |
* Handle field save. |
| 112 |
* |
| 113 |
* @since 1.0.0 |
| 114 |
* |
| 115 |
* @param array $field |
| 116 |
* @param integer $user_id |
| 117 |
* @param array $request_data |
| 118 |
* @param string $form_location |
| 119 |
*/ |
| 120 |
function wppb_select2_save_value($field, $user_id, $request_data, $form_location) |
| 121 |
{ |
| 122 |
if ($field['field'] == 'Select2') { |
| 123 |
if (isset($request_data[wppb_handle_meta_name($field['meta-name'])])) |
| 124 |
update_user_meta($user_id, sanitize_text_field($field['meta-name']), sanitize_text_field($request_data[wppb_handle_meta_name($field['meta-name'])])); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
add_action('wppb_save_form_field', 'wppb_select2_save_value', 10, 4); |
| 129 |
add_action('wppb_backend_save_form_field', 'wppb_select2_save_value', 10, 4); |
| 130 |
|
| 131 |
/* |
| 132 |
* Handle field validation. |
| 133 |
* |
| 134 |
* @since 1.0.0 |
| 135 |
* |
| 136 |
* @param string $message |
| 137 |
* @param array $field |
| 138 |
* @param array $request_data |
| 139 |
* @param $form_location |
| 140 |
* @return string Message to display on field validation |
| 141 |
*/ |
| 142 |
function wppb_select2_check_value($message, $field, $request_data, $form_location) |
| 143 |
{ |
| 144 |
if ($field['field'] == 'Select2') { |
| 145 |
if ($field['required'] == 'Yes') { |
| 146 |
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'])])) { |
| 147 |
return wppb_required_field_error($field["field-title"]); |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
return $message; |
| 153 |
} |
| 154 |
|
| 155 |
add_filter('wppb_check_form_field_select2', 'wppb_select2_check_value', 10, 4); |
| 156 |
|
| 157 |
|
| 158 |
|