| 1 |
<?php |
| 2 |
/** |
| 3 |
* The framework select fields file. |
| 4 |
* |
| 5 |
* @package Smart_Post_Show |
| 6 |
* @subpackage Smart_Post_Show/admin |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
die; |
| 11 |
} // Cannot access directly. |
| 12 |
|
| 13 |
if ( ! class_exists( 'SP_PC_Field_select' ) ) { |
| 14 |
/** |
| 15 |
* SP_PC_Field_select |
| 16 |
*/ |
| 17 |
class SP_PC_Field_select extends SP_PC_Fields { |
| 18 |
|
| 19 |
/** |
| 20 |
* The select field constructor. |
| 21 |
* |
| 22 |
* @param string $field The field type. |
| 23 |
* @param string $value The field value. |
| 24 |
* @param string $unique The unique ID. |
| 25 |
* @param string $where The place to show the field. |
| 26 |
* @param string $parent If it has any parent. |
| 27 |
*/ |
| 28 |
public function __construct( $field, $value = '', $unique = '', $where = '', $parent = '' ) { |
| 29 |
parent::__construct( $field, $value, $unique, $where, $parent ); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* The render function. |
| 34 |
* |
| 35 |
* @return void |
| 36 |
*/ |
| 37 |
public function render() { |
| 38 |
|
| 39 |
$args = wp_parse_args( |
| 40 |
$this->field, |
| 41 |
array( |
| 42 |
'placeholder' => '', |
| 43 |
'chosen' => false, |
| 44 |
'multiple' => false, |
| 45 |
'sortable' => false, |
| 46 |
'ajax' => false, |
| 47 |
'settings' => array(), |
| 48 |
'query_args' => array(), |
| 49 |
) |
| 50 |
); |
| 51 |
|
| 52 |
$this->value = ( is_array( $this->value ) ) ? $this->value : array_filter( (array) $this->value ); |
| 53 |
|
| 54 |
echo wp_kses_post( $this->field_before() ); |
| 55 |
|
| 56 |
if ( isset( $this->field['options'] ) ) { |
| 57 |
|
| 58 |
$multiple_type = isset( $this->field['multiple_type'] ) ? $this->field['multiple_type'] : false; |
| 59 |
|
| 60 |
if ( ! empty( $args['ajax'] ) ) { |
| 61 |
$args['settings']['data']['type'] = $args['options']; |
| 62 |
$args['settings']['data']['nonce'] = wp_create_nonce( 'spf_chosen_ajax_nonce' ); |
| 63 |
if ( ! empty( $args['query_args'] ) ) { |
| 64 |
$args['settings']['data']['query_args'] = $args['query_args']; |
| 65 |
} |
| 66 |
} |
| 67 |
$chosen_rtl = ( is_rtl() ) ? ' chosen-rtl' : ''; |
| 68 |
$multiple_name = ( $args['multiple'] ) ? '[]' : ''; |
| 69 |
$multiple_attr = ( $args['multiple'] ) ? ' multiple="multiple"' : ''; |
| 70 |
$chosen_sortable = ( $args['chosen'] && $args['sortable'] ) ? ' spf-chosen-sortable' : ''; |
| 71 |
$chosen_attr = ( $args['chosen'] ) ? ' class="spf-chosen' . $chosen_rtl . '"' : ''; |
| 72 |
$chosen_ajax = ( $args['chosen'] && $args['ajax'] ) ? ' spf-chosen-ajax' : ''; |
| 73 |
$placeholder_attr = ( $args['chosen'] && $args['placeholder'] ) ? ' data-placeholder="' . $args['placeholder'] . '"' : ''; |
| 74 |
$field_class = ( $args['chosen'] ) ? ' class="spf-chosen' . $chosen_rtl . $chosen_sortable . $chosen_ajax . '"' : ''; |
| 75 |
$field_name = $this->field_name( $multiple_name ); |
| 76 |
$field_attr = $this->field_attributes(); |
| 77 |
$maybe_options = $this->field['options']; |
| 78 |
$field_unique = $this->unique; |
| 79 |
$chosen_data_attr = ( $args['chosen'] && ! empty( $args['settings'] ) ) ? ' data-chosen-settings="' . esc_attr( json_encode( $args['settings'] ) ) . '"' : ''; |
| 80 |
if ( is_string( $maybe_options ) && ! empty( $args['chosen'] ) && ! empty( $args['ajax'] ) ) { |
| 81 |
$options = $this->field_wp_query_data_title( $maybe_options, $this->value ); |
| 82 |
} elseif ( is_string( $maybe_options ) ) { |
| 83 |
$options = $this->field_data( $maybe_options, false, $args['query_args'], $field_unique ); |
| 84 |
} else { |
| 85 |
$options = $maybe_options; |
| 86 |
} |
| 87 |
if ( ( is_array( $options ) && ! empty( $options ) ) || ( ! empty( $args['chosen'] ) && ! empty( $args['ajax'] ) ) ) { |
| 88 |
if ( ! empty( $args['chosen'] ) && ! empty( $args['multiple'] ) ) { |
| 89 |
echo '<select name="' . esc_attr( $field_name ) . '" class="spf-hidden-select spf-hidden"' . wp_kses_post( $multiple_attr . $field_attr ) . '>'; |
| 90 |
foreach ( $this->value as $option_key ) { |
| 91 |
echo '<option value="' . esc_attr( $option_key ) . '" selected>' . esc_html( $option_key ) . '</option>'; |
| 92 |
} |
| 93 |
echo '</select>'; |
| 94 |
$field_name = '_pseudo'; |
| 95 |
$field_attr = ''; |
| 96 |
} |
| 97 |
echo '<select name="' . esc_attr( $field_name ) . '"' . wp_kses_post( $field_class . $multiple_attr . $placeholder_attr . $field_attr . $chosen_data_attr ) . '>'; |
| 98 |
if ( $args['placeholder'] && empty( $args['multiple'] ) ) { |
| 99 |
if ( ! empty( $args['chosen'] ) ) { |
| 100 |
echo '<option value=""></option>'; |
| 101 |
} else { |
| 102 |
echo '<option value="">' . esc_html( $args['placeholder'] ) . '</option>'; |
| 103 |
} |
| 104 |
} |
| 105 |
if ( $multiple_type ) { |
| 106 |
$options = array_merge( |
| 107 |
$options, |
| 108 |
array( |
| 109 |
'multiple_post_type' => __( 'Multiple Post Types (Pro)', 'post-carousel' ), |
| 110 |
) |
| 111 |
); |
| 112 |
} |
| 113 |
$selected_value = ''; |
| 114 |
foreach ( $options as $option_key => $option ) { |
| 115 |
if ( is_array( $option ) && ! empty( $option ) ) { |
| 116 |
echo '<optgroup label="' . esc_attr( $option_key ) . '">'; |
| 117 |
foreach ( $option as $sub_key => $sub_value ) { |
| 118 |
$selected = ( in_array( $sub_key, $this->value ) ) ? ' selected' : ''; |
| 119 |
echo '<option value="' . esc_attr( $sub_key ) . '" ' . esc_attr( $selected ) . '>' . wp_kses_post( $sub_value ) . '</option>'; |
| 120 |
} |
| 121 |
echo '</optgroup>'; |
| 122 |
} else { |
| 123 |
$selected = ( in_array( $option_key, $this->value ) ) ? ' selected' : ''; |
| 124 |
if ( in_array( $option_key, $this->value ) ) { |
| 125 |
$selected_value = $option_key; |
| 126 |
} |
| 127 |
echo '<option value="' . esc_attr( $option_key ) . '" ' . esc_attr( $selected ) . '>' . wp_kses_post( $option ) . '</option>'; |
| 128 |
} |
| 129 |
} |
| 130 |
echo '</select>'; |
| 131 |
if ( isset( $args['preview'] ) && $args['preview'] ) { |
| 132 |
echo '<img src="' . esc_url( SP_PC_URL ) . '/admin/views/sp-framework/assets/img/nav-position/' . esc_attr( $selected_value ) . '.svg " class="icon_preview">'; |
| 133 |
echo '<div class="pcp-pro-notice"></div>'; |
| 134 |
} |
| 135 |
} else { |
| 136 |
echo ( esc_html( ! empty( $this->field['empty_message'] ) ) ) ? esc_html( $this->field['empty_message'] ) : esc_html__( 'No data provided for this option type.', 'post-carousel' ); |
| 137 |
} |
| 138 |
} |
| 139 |
echo wp_kses_post( $this->field_after() ); |
| 140 |
} |
| 141 |
/** |
| 142 |
* Enqueue UI Sortable. |
| 143 |
* |
| 144 |
* @return void |
| 145 |
*/ |
| 146 |
public function enqueue() { |
| 147 |
if ( ! wp_script_is( 'jquery-ui-sortable' ) ) { |
| 148 |
wp_enqueue_script( 'jquery-ui-sortable' ); |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
|