name = 'select'; $this->label = _x( 'Select', 'noun', 'acf' ); $this->category = 'choice'; $this->description = __( 'A dropdown list with a selection of choices that you specify.', 'acf' ); $this->preview_image = acf_get_url() . '/assets/images/field-type-previews/field-preview-select.png'; $this->doc_url = acf_add_url_utm_tags( 'https://www.advancedcustomfields.com/resources/select/', 'docs', 'field-type-selection' ); $this->defaults = array( 'multiple' => 0, 'allow_null' => 0, 'choices' => array(), 'default_value' => '', 'ui' => 0, 'ajax' => 0, 'placeholder' => '', 'return_format' => 'value', ); // ajax 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' ) ); } /* * input_admin_enqueue_scripts * * description * * @type function * @date 16/12/2015 * @since 5.3.2 * * @param $post_id (int) * @return $post_id (int) */ function input_admin_enqueue_scripts() { // bail early if no enqueue if ( ! acf_get_setting( 'enqueue_select2' ) ) { return; } // globals global $wp_scripts, $wp_styles; // vars $min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; $major = acf_get_setting( 'select2_version' ); $version = ''; $script = ''; $style = ''; // attempt to find 3rd party Select2 version // - avoid including v3 CSS when v4 JS is already enququed if ( isset( $wp_scripts->registered['select2'] ) ) { $major = (int) $wp_scripts->registered['select2']->ver; } // v4 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" ); // v3 } 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' ); } // enqueue wp_enqueue_script( 'select2', $script, array( 'jquery' ), $version ); wp_enqueue_style( 'select2', $style, '', $version ); // localize 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' ), ), ) ); } /* * ajax_query * * description * * @type function * @date 24/10/13 * @since 5.0.0 * * @param $post_id (int) * @return $post_id (int) */ function ajax_query() { // validate if ( ! acf_verify_ajax() ) { die(); } // get choices $response = $this->get_ajax_query( $_POST ); // return acf_send_ajax_results( $response ); } /** * This function will return an array of data formatted for use in a select2 AJAX response * * @since 5.0.9 * * @param array $options An array of options. * @return array A select2 compatible array of options. */ public function get_ajax_query( $options = array() ) { $options = acf_parse_args( $options, array( 'post_id' => 0, 's' => '', 'field_key' => '', 'paged' => 1, ) ); $shortcut = apply_filters( 'acf/fields/select/query', array(), $options ); $shortcut = apply_filters( 'acf/fields/select/query/key=' . $options['field_key'], $shortcut, $options ); if ( ! empty( $shortcut ) ) { return $shortcut; } // load field. $field = acf_get_field( $options['field_key'] ); if ( ! $field ) { return false; } // get choices. $choices = acf_get_array( $field['choices'] ); if ( empty( $field['choices'] ) ) { return false; } $results = array(); $s = null; // search. if ( $options['s'] !== '' ) { // strip slashes (search may be integer) $s = strval( $options['s'] ); $s = wp_unslash( $s ); } foreach ( $field['choices'] as $k => $v ) { // ensure $v is a string. $v = strval( $v ); // if searching, but doesn't exist. if ( is_string( $s ) && stripos( $v, $s ) === false ) { continue; } // append results. $results[] = array( 'id' => $k, 'text' => $v, ); } $response = array( 'results' => $results, ); return $response; } /* * render_field() * * Create the HTML interface for your field * * @param $field - an array holding all the field's data * * @type action * @since 3.6 * @date 23/01/13 */ function render_field( $field ) { // convert $value = acf_get_array( $field['value'] ); $choices = acf_get_array( $field['choices'] ); // placeholder if ( empty( $field['placeholder'] ) ) { $field['placeholder'] = _x( 'Select', 'verb', 'acf' ); } // add empty value (allows '' to be selected) if ( empty( $value ) ) { $value = array( '' ); } // prepend empty choice // - only for single selects // - have tried array_merge but this causes keys to re-index if is numeric (post ID's) if ( $field['allow_null'] && ! $field['multiple'] ) { $choices = array( '' => "- {$field['placeholder']} -" ) + $choices; } // clean up choices if using ajax if ( $field['ui'] && $field['ajax'] ) { $minimal = array(); foreach ( $value as $key ) { if ( isset( $choices[ $key ] ) ) { $minimal[ $key ] = $choices[ $key ]; } } $choices = $minimal; } // vars $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']; } // multiple if ( $field['multiple'] ) { $select['multiple'] = 'multiple'; $select['size'] = 5; $select['name'] .= '[]'; // Reduce size to single line if UI. if ( $field['ui'] ) { $select['size'] = 1; } } // special atts 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 ( ! empty( $field['hide_search'] ) ) { $select['data-minimum-results-for-search'] = '-1'; } // hidden input is needed to allow validation to see