123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743 |
- <?php
- if ( ! class_exists( 'acf_field_select' ) ) :
- class acf_field_select extends acf_field {
-
- function initialize() {
-
- $this->name = 'select';
- $this->label = _x( 'Select', 'noun', 'acf' );
- $this->category = 'choice';
- $this->defaults = array(
- 'multiple' => 0,
- 'allow_null' => 0,
- 'choices' => array(),
- 'default_value' => '',
- 'ui' => 0,
- 'ajax' => 0,
- 'placeholder' => '',
- 'return_format' => 'value',
- );
-
- add_action( 'wp_ajax_acf/fields/select/query', array( $this, 'ajax_query' ) );
- add_action( 'wp_ajax_nopriv_acf/fields/select/query', array( $this, 'ajax_query' ) );
- }
-
- function input_admin_enqueue_scripts() {
-
- if ( ! acf_get_setting( 'enqueue_select2' ) ) {
- return;
- }
-
- global $wp_scripts, $wp_styles;
-
- $min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
- $major = acf_get_setting( 'select2_version' );
- $version = '';
- $script = '';
- $style = '';
-
-
- if ( isset( $wp_scripts->registered['select2'] ) ) {
- $major = (int) $wp_scripts->registered['select2']->ver;
- }
-
- if ( $major == 4 ) {
- $version = '4.0.13';
- $script = acf_get_url( "assets/inc/select2/4/select2.full{$min}.js" );
- $style = acf_get_url( "assets/inc/select2/4/select2{$min}.css" );
-
- } else {
- $version = '3.5.2';
- $script = acf_get_url( "assets/inc/select2/3/select2{$min}.js" );
- $style = acf_get_url( 'assets/inc/select2/3/select2.css' );
- }
-
- wp_enqueue_script( 'select2', $script, array( 'jquery' ), $version );
- wp_enqueue_style( 'select2', $style, '', $version );
-
- acf_localize_data(
- array(
- 'select2L10n' => array(
- 'matches_1' => _x( 'One result is available, press enter to select it.', 'Select2 JS matches_1', 'acf' ),
- 'matches_n' => _x( '%d results are available, use up and down arrow keys to navigate.', 'Select2 JS matches_n', 'acf' ),
- 'matches_0' => _x( 'No matches found', 'Select2 JS matches_0', 'acf' ),
- 'input_too_short_1' => _x( 'Please enter 1 or more characters', 'Select2 JS input_too_short_1', 'acf' ),
- 'input_too_short_n' => _x( 'Please enter %d or more characters', 'Select2 JS input_too_short_n', 'acf' ),
- 'input_too_long_1' => _x( 'Please delete 1 character', 'Select2 JS input_too_long_1', 'acf' ),
- 'input_too_long_n' => _x( 'Please delete %d characters', 'Select2 JS input_too_long_n', 'acf' ),
- 'selection_too_long_1' => _x( 'You can only select 1 item', 'Select2 JS selection_too_long_1', 'acf' ),
- 'selection_too_long_n' => _x( 'You can only select %d items', 'Select2 JS selection_too_long_n', 'acf' ),
- 'load_more' => _x( 'Loading more results…', 'Select2 JS load_more', 'acf' ),
- 'searching' => _x( 'Searching…', 'Select2 JS searching', 'acf' ),
- 'load_fail' => _x( 'Loading failed', 'Select2 JS load_fail', 'acf' ),
- ),
- )
- );
- }
-
- function ajax_query() {
-
- if ( ! acf_verify_ajax() ) {
- die();
- }
-
- $response = $this->get_ajax_query( $_POST );
-
- acf_send_ajax_results( $response );
- }
-
- function get_ajax_query( $options = array() ) {
-
- $options = acf_parse_args(
- $options,
- array(
- 'post_id' => 0,
- 's' => '',
- 'field_key' => '',
- 'paged' => 1,
- )
- );
-
- $field = acf_get_field( $options['field_key'] );
- if ( ! $field ) {
- return false;
- }
-
- $choices = acf_get_array( $field['choices'] );
- if ( empty( $field['choices'] ) ) {
- return false;
- }
-
- $results = array();
- $s = null;
-
- if ( $options['s'] !== '' ) {
-
- $s = strval( $options['s'] );
- $s = wp_unslash( $s );
- }
-
- foreach ( $field['choices'] as $k => $v ) {
-
- $v = strval( $v );
-
- if ( is_string( $s ) && stripos( $v, $s ) === false ) {
- continue;
- }
-
- $results[] = array(
- 'id' => $k,
- 'text' => $v,
- );
- }
-
- $response = array(
- 'results' => $results,
- );
-
- return $response;
- }
-
- function render_field( $field ) {
-
- $value = acf_get_array( $field['value'] );
- $choices = acf_get_array( $field['choices'] );
-
- if ( empty( $field['placeholder'] ) ) {
- $field['placeholder'] = _x( 'Select', 'verb', 'acf' );
- }
-
- if ( empty( $value ) ) {
- $value = array( '' );
- }
-
-
-
- if ( $field['allow_null'] && ! $field['multiple'] ) {
- $choices = array( '' => "- {$field['placeholder']} -" ) + $choices;
- }
-
- if ( $field['ui'] && $field['ajax'] ) {
- $minimal = array();
- foreach ( $value as $key ) {
- if ( isset( $choices[ $key ] ) ) {
- $minimal[ $key ] = $choices[ $key ];
- }
- }
- $choices = $minimal;
- }
-
- $select = array(
- 'id' => $field['id'],
- 'class' => $field['class'],
- 'name' => $field['name'],
- 'data-ui' => $field['ui'],
- 'data-ajax' => $field['ajax'],
- 'data-multiple' => $field['multiple'],
- 'data-placeholder' => $field['placeholder'],
- 'data-allow_null' => $field['allow_null'],
- );
- if ( $field['aria-label'] ) {
- $select['aria-label'] = $field['aria-label'];
- }
-
- if ( $field['multiple'] ) {
- $select['multiple'] = 'multiple';
- $select['size'] = 5;
- $select['name'] .= '[]';
-
- if ( $field['ui'] ) {
- $select['size'] = 1;
- }
- }
-
- if ( ! empty( $field['readonly'] ) ) {
- $select['readonly'] = 'readonly';
- }
- if ( ! empty( $field['disabled'] ) ) {
- $select['disabled'] = 'disabled';
- }
- if ( ! empty( $field['ajax_action'] ) ) {
- $select['data-ajax_action'] = $field['ajax_action'];
- }
-
- if ( $field['multiple'] || $field['ui'] ) {
- acf_hidden_input(
- array(
- 'id' => $field['id'] . '-input',
- 'name' => $field['name'],
- )
- );
- }
- if ( ! empty( $field['query_nonce'] ) ) {
- $select['data-query-nonce'] = $field['query_nonce'];
- }
-
- $select['value'] = $value;
- $select['choices'] = $choices;
-
- acf_select_input( $select );
- }
-
- function render_field_settings( $field ) {
-
- $field['choices'] = acf_encode_choices( $field['choices'] );
- $field['default_value'] = acf_encode_choices( $field['default_value'], false );
-
- acf_render_field_setting(
- $field,
- array(
- 'label' => __( 'Choices', 'acf' ),
- 'instructions' => __( 'Enter each choice on a new line.', 'acf' ) . '<br />' . __( 'For more control, you may specify both a value and label like this:', 'acf' ) . '<br /><span class="acf-field-setting-example">' . __( 'red : Red', 'acf' ) . '</span>',
- 'name' => 'choices',
- 'type' => 'textarea',
- )
- );
-
- acf_render_field_setting(
- $field,
- array(
- 'label' => __( 'Default Value', 'acf' ),
- 'instructions' => __( 'Enter each default value on a new line', 'acf' ),
- 'name' => 'default_value',
- 'type' => 'textarea',
- )
- );
-
- acf_render_field_setting(
- $field,
- array(
- 'label' => __( 'Return Format', 'acf' ),
- 'instructions' => __( 'Specify the value returned', 'acf' ),
- 'type' => 'radio',
- 'name' => 'return_format',
- 'layout' => 'horizontal',
- 'choices' => array(
- 'value' => __( 'Value', 'acf' ),
- 'label' => __( 'Label', 'acf' ),
- 'array' => __( 'Both (Array)', 'acf' ),
- ),
- )
- );
- acf_render_field_setting(
- $field,
- array(
- 'label' => __( 'Select multiple values?', 'acf' ),
- 'instructions' => '',
- 'name' => 'multiple',
- 'type' => 'true_false',
- 'ui' => 1,
- )
- );
- }
-
- function render_field_validation_settings( $field ) {
- acf_render_field_setting(
- $field,
- array(
- 'label' => __( 'Allow Null?', 'acf' ),
- 'instructions' => '',
- 'name' => 'allow_null',
- 'type' => 'true_false',
- 'ui' => 1,
- )
- );
- }
-
- function render_field_presentation_settings( $field ) {
- acf_render_field_setting(
- $field,
- array(
- 'label' => __( 'Stylized UI', 'acf' ),
- 'instructions' => __( 'Use a stylized checkbox using select2', 'acf' ),
- 'name' => 'ui',
- 'type' => 'true_false',
- 'ui' => 1,
- )
- );
- acf_render_field_setting(
- $field,
- array(
- 'label' => __( 'Use AJAX to lazy load choices?', 'acf' ),
- 'instructions' => '',
- 'name' => 'ajax',
- 'type' => 'true_false',
- 'ui' => 1,
- 'conditions' => array(
- 'field' => 'ui',
- 'operator' => '==',
- 'value' => 1,
- ),
- )
- );
- }
-
- function load_value( $value, $post_id, $field ) {
-
- if ( $field['multiple'] ) {
- if ( acf_is_empty( $value ) ) {
- return array();
- }
- return acf_array( $value );
- }
-
- return acf_unarray( $value );
- }
-
- function update_field( $field ) {
-
- $field['choices'] = acf_decode_choices( $field['choices'] );
- $field['default_value'] = acf_decode_choices( $field['default_value'], true );
-
- if ( ! $field['multiple'] ) {
- $field['default_value'] = acf_unarray( $field['default_value'] );
- }
-
- return $field;
- }
-
- function update_value( $value, $post_id, $field ) {
-
- if ( empty( $value ) ) {
- return $value;
- }
-
-
- if ( is_array( $value ) ) {
- $value = array_map( 'strval', $value );
- }
-
- return $value;
- }
-
- function translate_field( $field ) {
-
- $field['choices'] = acf_translate( $field['choices'] );
-
- return $field;
- }
-
- function format_value( $value, $post_id, $field ) {
- if ( is_array( $value ) ) {
- foreach ( $value as $i => $val ) {
- $value[ $i ] = $this->format_value_single( $val, $post_id, $field );
- }
- } else {
- $value = $this->format_value_single( $value, $post_id, $field );
- }
- return $value;
- }
- function format_value_single( $value, $post_id, $field ) {
-
- if ( acf_is_empty( $value ) ) {
- return $value;
- }
-
- $label = acf_maybe_get( $field['choices'], $value, $value );
-
- if ( $field['return_format'] == 'value' ) {
-
-
- } elseif ( $field['return_format'] == 'label' ) {
- $value = $label;
-
- } elseif ( $field['return_format'] == 'array' ) {
- $value = array(
- 'value' => $value,
- 'label' => $label,
- );
- }
-
- return $value;
- }
-
- public function validate_rest_value( $valid, $value, $field ) {
-
- if ( is_null( $value ) || is_array( $value ) ) {
- return $valid;
- }
- $option_keys = array_diff(
- array_keys( $field['choices'] ),
- array_values( $field['choices'] )
- );
- $allowed = empty( $option_keys ) ? $field['choices'] : $option_keys;
- if ( ! in_array( $value, $allowed ) ) {
- $param = sprintf( '%s[%s]', $field['prefix'], $field['name'] );
- $data = array(
- 'param' => $param,
- 'value' => $value,
- );
- $error = sprintf(
- __( '%1$s is not one of %2$s', 'acf' ),
- $param,
- implode( ', ', $allowed )
- );
- return new WP_Error( 'rest_invalid_param', $error, $data );
- }
- return $valid;
- }
-
- public function get_rest_schema( array $field ) {
-
- $option_keys = array_diff(
- array_keys( $field['choices'] ),
- array_values( $field['choices'] )
- );
- $schema = array(
- 'type' => array( 'string', 'array', 'int', 'null' ),
- 'required' => ! empty( $field['required'] ),
- 'items' => array(
- 'type' => array( 'string', 'int' ),
- 'enum' => empty( $option_keys ) ? $field['choices'] : $option_keys,
- ),
- );
- if ( empty( $field['allow_null'] ) ) {
- $schema['minItems'] = 1;
- }
- if ( empty( $field['multiple'] ) ) {
- $schema['maxItems'] = 1;
- }
- if ( isset( $field['default_value'] ) && '' !== $field['default_value'] ) {
- $schema['default'] = $field['default_value'];
- }
- return $schema;
- }
- }
-
- acf_register_field_type( 'acf_field_select' );
- endif;
|