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