| 1 |
<?php if ( ! defined( 'ABSPATH' ) ) { |
| 2 |
die; } // Cannot access directly. |
| 3 |
/** |
| 4 |
* |
| 5 |
* Field: select |
| 6 |
* |
| 7 |
* @since 1.0.0 |
| 8 |
* @version 1.0.0 |
| 9 |
*/ |
| 10 |
if ( ! class_exists( 'ADMINIFY_Field_select' ) ) { |
| 11 |
class ADMINIFY_Field_select extends ADMINIFY_Fields { |
| 12 |
|
| 13 |
public function __construct( $field, $value = '', $unique = '', $where = '', $parent = '' ) { |
| 14 |
parent::__construct( $field, $value, $unique, $where, $parent ); |
| 15 |
} |
| 16 |
|
| 17 |
public function render() { |
| 18 |
$args = wp_parse_args( |
| 19 |
$this->field, |
| 20 |
[ |
| 21 |
'placeholder' => '', |
| 22 |
'chosen' => false, |
| 23 |
'multiple' => false, |
| 24 |
'sortable' => false, |
| 25 |
'ajax' => false, |
| 26 |
'settings' => [], |
| 27 |
'query_args' => [], |
| 28 |
] |
| 29 |
); |
| 30 |
|
| 31 |
$this->value = ( is_array( $this->value ) ) ? $this->value : array_filter( (array) $this->value ); |
| 32 |
|
| 33 |
echo wp_kses_post( $this->field_before() ); |
| 34 |
|
| 35 |
if ( isset( $this->field['options'] ) ) { |
| 36 |
if ( ! empty( $args['ajax'] ) ) { |
| 37 |
$args['settings']['data']['type'] = $args['options']; |
| 38 |
$args['settings']['data']['nonce'] = wp_create_nonce( 'adminify_chosen_ajax_nonce' ); |
| 39 |
if ( ! empty( $args['query_args'] ) ) { |
| 40 |
$args['settings']['data']['query_args'] = $args['query_args']; |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
$chosen_rtl = ( is_rtl() ) ? ' chosen-rtl' : ''; |
| 45 |
$multiple_name = ( $args['multiple'] ) ? '[]' : ''; |
| 46 |
$multiple_attr = ( $args['multiple'] ) ? ' multiple="multiple"' : ''; |
| 47 |
$chosen_sortable = ( $args['chosen'] && $args['sortable'] ) ? ' adminify-chosen-sortable' : ''; |
| 48 |
$chosen_ajax = ( $args['chosen'] && $args['ajax'] ) ? ' adminify-chosen-ajax' : ''; |
| 49 |
$placeholder_attr = ( $args['chosen'] && $args['placeholder'] ) ? ' data-placeholder="' . $args['placeholder'] . '"' : ''; |
| 50 |
$field_class = ( $args['chosen'] ) ? ' class="adminify-chosen' . $chosen_rtl . $chosen_sortable . $chosen_ajax . '"' : ''; |
| 51 |
$field_name = $this->field_name( $multiple_name ); |
| 52 |
$field_attr = $this->field_attributes(); |
| 53 |
$maybe_options = $this->field['options']; |
| 54 |
$chosen_data_attr = ( $args['chosen'] && ! empty( $args['settings'] ) ) ? ' data-chosen-settings="' . json_encode( $args['settings'] ) . '"' : ''; |
| 55 |
|
| 56 |
if ( is_string( $maybe_options ) && ! empty( $args['chosen'] ) && ! empty( $args['ajax'] ) ) { |
| 57 |
$options = $this->field_wp_query_data_title( $maybe_options, $this->value ); |
| 58 |
} elseif ( is_string( $maybe_options ) ) { |
| 59 |
$options = $this->field_data( $maybe_options, false, $args['query_args'] ); |
| 60 |
} else { |
| 61 |
$options = $maybe_options; |
| 62 |
} |
| 63 |
|
| 64 |
if ( ( is_array( $options ) && ! empty( $options ) ) || ( ! empty( $args['chosen'] ) && ! empty( $args['ajax'] ) ) ) { |
| 65 |
if ( ! empty( $args['chosen'] ) && ! empty( $args['multiple'] ) ) { |
| 66 |
echo '<select name="' . esc_attr( $field_name ) . '" class="adminify-hide-select hidden"' . wp_kses_post( $multiple_attr . $field_attr ) . '>'; |
| 67 |
foreach ( $this->value as $option_key ) { |
| 68 |
echo '<option value="' . esc_attr( $option_key ) . '" selected>' . wp_kses_post( $option_key ) . '</option>'; |
| 69 |
} |
| 70 |
echo '</select>'; |
| 71 |
|
| 72 |
$field_name = '_pseudo'; |
| 73 |
$field_attr = ''; |
| 74 |
} |
| 75 |
|
| 76 |
// These attributes has been serialized above. |
| 77 |
echo '<select name="' . esc_attr( $field_name ) . '"' . wp_kses_post( $field_class . $multiple_attr . $placeholder_attr . $field_attr . $chosen_data_attr ) . '>'; |
| 78 |
|
| 79 |
if ( $args['placeholder'] && empty( $args['multiple'] ) ) { |
| 80 |
if ( ! empty( $args['chosen'] ) ) { |
| 81 |
echo '<option value=""></option>'; |
| 82 |
} else { |
| 83 |
echo '<option value="">' . esc_attr( $args['placeholder'] ) . '</option>'; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
foreach ( $options as $option_key => $option ) { |
| 88 |
if ( is_array( $option ) && ! empty( $option ) ) { |
| 89 |
echo '<optgroup label="' . esc_attr( $option_key ) . '">'; |
| 90 |
|
| 91 |
foreach ( $option as $sub_key => $sub_value ) { |
| 92 |
$selected = ( in_array( $sub_key, $this->value ) ) ? ' selected' : ''; |
| 93 |
echo '<option value="' . esc_attr( $sub_key ) . '" ' . esc_attr( $selected ) . '>' . esc_attr( $sub_value ) . '</option>'; |
| 94 |
} |
| 95 |
|
| 96 |
echo '</optgroup>'; |
| 97 |
} else { |
| 98 |
$selected = ( in_array( $option_key, $this->value ) ) ? ' selected' : ''; |
| 99 |
echo '<option value="' . esc_attr( $option_key ) . '" ' . esc_attr( $selected ) . '>' . esc_attr( $option ) . '</option>'; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
echo '</select>'; |
| 104 |
} else { |
| 105 |
if ( ! empty( $this->field['empty_message'] ) ) { |
| 106 |
echo esc_attr( $this->field['empty_message'] ); |
| 107 |
} else { |
| 108 |
echo esc_html__( 'No data available.', 'adminify' ); |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
echo wp_kses_post( $this->field_after() ); |
| 114 |
} |
| 115 |
|
| 116 |
public function enqueue() { |
| 117 |
if ( ! wp_script_is( 'jquery-ui-sortable' ) ) { |
| 118 |
wp_enqueue_script( 'jquery-ui-sortable' ); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
} |
| 123 |
} |
| 124 |
|