Bez popisu

class-acf-field-select.php 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. <?php
  2. if ( ! class_exists( 'acf_field_select' ) ) :
  3. class acf_field_select extends acf_field {
  4. /*
  5. * __construct
  6. *
  7. * This function will setup the field type data
  8. *
  9. * @type function
  10. * @date 5/03/2014
  11. * @since 5.0.0
  12. *
  13. * @param n/a
  14. * @return n/a
  15. */
  16. function initialize() {
  17. // vars
  18. $this->name = 'select';
  19. $this->label = _x( 'Select', 'noun', 'acf' );
  20. $this->category = 'choice';
  21. $this->defaults = array(
  22. 'multiple' => 0,
  23. 'allow_null' => 0,
  24. 'choices' => array(),
  25. 'default_value' => '',
  26. 'ui' => 0,
  27. 'ajax' => 0,
  28. 'placeholder' => '',
  29. 'return_format' => 'value',
  30. );
  31. // ajax
  32. add_action( 'wp_ajax_acf/fields/select/query', array( $this, 'ajax_query' ) );
  33. add_action( 'wp_ajax_nopriv_acf/fields/select/query', array( $this, 'ajax_query' ) );
  34. }
  35. /*
  36. * input_admin_enqueue_scripts
  37. *
  38. * description
  39. *
  40. * @type function
  41. * @date 16/12/2015
  42. * @since 5.3.2
  43. *
  44. * @param $post_id (int)
  45. * @return $post_id (int)
  46. */
  47. function input_admin_enqueue_scripts() {
  48. // bail ealry if no enqueue
  49. if ( ! acf_get_setting( 'enqueue_select2' ) ) {
  50. return;
  51. }
  52. // globals
  53. global $wp_scripts, $wp_styles;
  54. // vars
  55. $min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
  56. $major = acf_get_setting( 'select2_version' );
  57. $version = '';
  58. $script = '';
  59. $style = '';
  60. // attempt to find 3rd party Select2 version
  61. // - avoid including v3 CSS when v4 JS is already enququed
  62. if ( isset( $wp_scripts->registered['select2'] ) ) {
  63. $major = (int) $wp_scripts->registered['select2']->ver;
  64. }
  65. // v4
  66. if ( $major == 4 ) {
  67. $version = '4.0';
  68. $script = acf_get_url( "assets/inc/select2/4/select2.full{$min}.js" );
  69. $style = acf_get_url( "assets/inc/select2/4/select2{$min}.css" );
  70. // v3
  71. } else {
  72. $version = '3.5.2';
  73. $script = acf_get_url( "assets/inc/select2/3/select2{$min}.js" );
  74. $style = acf_get_url( 'assets/inc/select2/3/select2.css' );
  75. }
  76. // enqueue
  77. wp_enqueue_script( 'select2', $script, array( 'jquery' ), $version );
  78. wp_enqueue_style( 'select2', $style, '', $version );
  79. // localize
  80. acf_localize_data(
  81. array(
  82. 'select2L10n' => array(
  83. 'matches_1' => _x( 'One result is available, press enter to select it.', 'Select2 JS matches_1', 'acf' ),
  84. 'matches_n' => _x( '%d results are available, use up and down arrow keys to navigate.', 'Select2 JS matches_n', 'acf' ),
  85. 'matches_0' => _x( 'No matches found', 'Select2 JS matches_0', 'acf' ),
  86. 'input_too_short_1' => _x( 'Please enter 1 or more characters', 'Select2 JS input_too_short_1', 'acf' ),
  87. 'input_too_short_n' => _x( 'Please enter %d or more characters', 'Select2 JS input_too_short_n', 'acf' ),
  88. 'input_too_long_1' => _x( 'Please delete 1 character', 'Select2 JS input_too_long_1', 'acf' ),
  89. 'input_too_long_n' => _x( 'Please delete %d characters', 'Select2 JS input_too_long_n', 'acf' ),
  90. 'selection_too_long_1' => _x( 'You can only select 1 item', 'Select2 JS selection_too_long_1', 'acf' ),
  91. 'selection_too_long_n' => _x( 'You can only select %d items', 'Select2 JS selection_too_long_n', 'acf' ),
  92. 'load_more' => _x( 'Loading more results&hellip;', 'Select2 JS load_more', 'acf' ),
  93. 'searching' => _x( 'Searching&hellip;', 'Select2 JS searching', 'acf' ),
  94. 'load_fail' => _x( 'Loading failed', 'Select2 JS load_fail', 'acf' ),
  95. ),
  96. )
  97. );
  98. }
  99. /*
  100. * ajax_query
  101. *
  102. * description
  103. *
  104. * @type function
  105. * @date 24/10/13
  106. * @since 5.0.0
  107. *
  108. * @param $post_id (int)
  109. * @return $post_id (int)
  110. */
  111. function ajax_query() {
  112. // validate
  113. if ( ! acf_verify_ajax() ) {
  114. die();
  115. }
  116. // get choices
  117. $response = $this->get_ajax_query( $_POST );
  118. // return
  119. acf_send_ajax_results( $response );
  120. }
  121. /*
  122. * get_ajax_query
  123. *
  124. * This function will return an array of data formatted for use in a select2 AJAX response
  125. *
  126. * @type function
  127. * @date 15/10/2014
  128. * @since 5.0.9
  129. *
  130. * @param $options (array)
  131. * @return (array)
  132. */
  133. function get_ajax_query( $options = array() ) {
  134. // defaults
  135. $options = acf_parse_args(
  136. $options,
  137. array(
  138. 'post_id' => 0,
  139. 's' => '',
  140. 'field_key' => '',
  141. 'paged' => 1,
  142. )
  143. );
  144. // load field
  145. $field = acf_get_field( $options['field_key'] );
  146. if ( ! $field ) {
  147. return false;
  148. }
  149. // get choices
  150. $choices = acf_get_array( $field['choices'] );
  151. if ( empty( $field['choices'] ) ) {
  152. return false;
  153. }
  154. // vars
  155. $results = array();
  156. $s = null;
  157. // search
  158. if ( $options['s'] !== '' ) {
  159. // strip slashes (search may be integer)
  160. $s = strval( $options['s'] );
  161. $s = wp_unslash( $s );
  162. }
  163. // loop
  164. foreach ( $field['choices'] as $k => $v ) {
  165. // ensure $v is a string
  166. $v = strval( $v );
  167. // if searching, but doesn't exist
  168. if ( is_string( $s ) && stripos( $v, $s ) === false ) {
  169. continue;
  170. }
  171. // append
  172. $results[] = array(
  173. 'id' => $k,
  174. 'text' => $v,
  175. );
  176. }
  177. // vars
  178. $response = array(
  179. 'results' => $results,
  180. );
  181. // return
  182. return $response;
  183. }
  184. /*
  185. * render_field()
  186. *
  187. * Create the HTML interface for your field
  188. *
  189. * @param $field - an array holding all the field's data
  190. *
  191. * @type action
  192. * @since 3.6
  193. * @date 23/01/13
  194. */
  195. function render_field( $field ) {
  196. // convert
  197. $value = acf_get_array( $field['value'] );
  198. $choices = acf_get_array( $field['choices'] );
  199. // placeholder
  200. if ( empty( $field['placeholder'] ) ) {
  201. $field['placeholder'] = _x( 'Select', 'verb', 'acf' );
  202. }
  203. // add empty value (allows '' to be selected)
  204. if ( empty( $value ) ) {
  205. $value = array( '' );
  206. }
  207. // prepend empty choice
  208. // - only for single selects
  209. // - have tried array_merge but this causes keys to re-index if is numeric (post ID's)
  210. if ( $field['allow_null'] && ! $field['multiple'] ) {
  211. $choices = array( '' => "- {$field['placeholder']} -" ) + $choices;
  212. }
  213. // clean up choices if using ajax
  214. if ( $field['ui'] && $field['ajax'] ) {
  215. $minimal = array();
  216. foreach ( $value as $key ) {
  217. if ( isset( $choices[ $key ] ) ) {
  218. $minimal[ $key ] = $choices[ $key ];
  219. }
  220. }
  221. $choices = $minimal;
  222. }
  223. // vars
  224. $select = array(
  225. 'id' => $field['id'],
  226. 'class' => $field['class'],
  227. 'name' => $field['name'],
  228. 'data-ui' => $field['ui'],
  229. 'data-ajax' => $field['ajax'],
  230. 'data-multiple' => $field['multiple'],
  231. 'data-placeholder' => $field['placeholder'],
  232. 'data-allow_null' => $field['allow_null'],
  233. );
  234. // multiple
  235. if ( $field['multiple'] ) {
  236. $select['multiple'] = 'multiple';
  237. $select['size'] = 5;
  238. $select['name'] .= '[]';
  239. // Reduce size to single line if UI.
  240. if ( $field['ui'] ) {
  241. $select['size'] = 1;
  242. }
  243. }
  244. // special atts
  245. if ( ! empty( $field['readonly'] ) ) {
  246. $select['readonly'] = 'readonly';
  247. }
  248. if ( ! empty( $field['disabled'] ) ) {
  249. $select['disabled'] = 'disabled';
  250. }
  251. if ( ! empty( $field['ajax_action'] ) ) {
  252. $select['data-ajax_action'] = $field['ajax_action'];
  253. }
  254. // hidden input is needed to allow validation to see <select> element with no selected value
  255. if ( $field['multiple'] || $field['ui'] ) {
  256. acf_hidden_input(
  257. array(
  258. 'id' => $field['id'] . '-input',
  259. 'name' => $field['name'],
  260. )
  261. );
  262. }
  263. if ( ! empty( $field['query_nonce'] ) ) {
  264. $select['data-query-nonce'] = $field['query_nonce'];
  265. }
  266. // append
  267. $select['value'] = $value;
  268. $select['choices'] = $choices;
  269. // render
  270. acf_select_input( $select );
  271. }
  272. /*
  273. * render_field_settings()
  274. *
  275. * Create extra options for your field. This is rendered when editing a field.
  276. * The value of $field['name'] can be used (like bellow) to save extra data to the $field
  277. *
  278. * @type action
  279. * @since 3.6
  280. * @date 23/01/13
  281. *
  282. * @param $field - an array holding all the field's data
  283. */
  284. function render_field_settings( $field ) {
  285. // encode choices (convert from array)
  286. $field['choices'] = acf_encode_choices( $field['choices'] );
  287. $field['default_value'] = acf_encode_choices( $field['default_value'], false );
  288. // choices
  289. acf_render_field_setting(
  290. $field,
  291. array(
  292. 'label' => __( 'Choices', 'acf' ),
  293. 'instructions' => __( 'Enter each choice on a new line.', 'acf' ) . '<br /><br />' . __( 'For more control, you may specify both a value and label like this:', 'acf' ) . '<br /><br />' . __( 'red : Red', 'acf' ),
  294. 'name' => 'choices',
  295. 'type' => 'textarea',
  296. )
  297. );
  298. // default_value
  299. acf_render_field_setting(
  300. $field,
  301. array(
  302. 'label' => __( 'Default Value', 'acf' ),
  303. 'instructions' => __( 'Enter each default value on a new line', 'acf' ),
  304. 'name' => 'default_value',
  305. 'type' => 'textarea',
  306. )
  307. );
  308. // allow_null
  309. acf_render_field_setting(
  310. $field,
  311. array(
  312. 'label' => __( 'Allow Null?', 'acf' ),
  313. 'instructions' => '',
  314. 'name' => 'allow_null',
  315. 'type' => 'true_false',
  316. 'ui' => 1,
  317. )
  318. );
  319. // multiple
  320. acf_render_field_setting(
  321. $field,
  322. array(
  323. 'label' => __( 'Select multiple values?', 'acf' ),
  324. 'instructions' => '',
  325. 'name' => 'multiple',
  326. 'type' => 'true_false',
  327. 'ui' => 1,
  328. )
  329. );
  330. // ui
  331. acf_render_field_setting(
  332. $field,
  333. array(
  334. 'label' => __( 'Stylised UI', 'acf' ),
  335. 'instructions' => '',
  336. 'name' => 'ui',
  337. 'type' => 'true_false',
  338. 'ui' => 1,
  339. )
  340. );
  341. // ajax
  342. acf_render_field_setting(
  343. $field,
  344. array(
  345. 'label' => __( 'Use AJAX to lazy load choices?', 'acf' ),
  346. 'instructions' => '',
  347. 'name' => 'ajax',
  348. 'type' => 'true_false',
  349. 'ui' => 1,
  350. 'conditions' => array(
  351. 'field' => 'ui',
  352. 'operator' => '==',
  353. 'value' => 1,
  354. ),
  355. )
  356. );
  357. // return_format
  358. acf_render_field_setting(
  359. $field,
  360. array(
  361. 'label' => __( 'Return Format', 'acf' ),
  362. 'instructions' => __( 'Specify the value returned', 'acf' ),
  363. 'type' => 'select',
  364. 'name' => 'return_format',
  365. 'choices' => array(
  366. 'value' => __( 'Value', 'acf' ),
  367. 'label' => __( 'Label', 'acf' ),
  368. 'array' => __( 'Both (Array)', 'acf' ),
  369. ),
  370. )
  371. );
  372. }
  373. /*
  374. * load_value()
  375. *
  376. * This filter is applied to the $value after it is loaded from the db
  377. *
  378. * @type filter
  379. * @since 3.6
  380. * @date 23/01/13
  381. *
  382. * @param $value (mixed) the value found in the database
  383. * @param $post_id (mixed) the $post_id from which the value was loaded
  384. * @param $field (array) the field array holding all the field options
  385. * @return $value
  386. */
  387. function load_value( $value, $post_id, $field ) {
  388. // Return an array when field is set for multiple.
  389. if ( $field['multiple'] ) {
  390. if ( acf_is_empty( $value ) ) {
  391. return array();
  392. }
  393. return acf_array( $value );
  394. }
  395. // Otherwise, return a single value.
  396. return acf_unarray( $value );
  397. }
  398. /*
  399. * update_field()
  400. *
  401. * This filter is appied to the $field before it is saved to the database
  402. *
  403. * @type filter
  404. * @since 3.6
  405. * @date 23/01/13
  406. *
  407. * @param $field - the field array holding all the field options
  408. * @param $post_id - the field group ID (post_type = acf)
  409. *
  410. * @return $field - the modified field
  411. */
  412. function update_field( $field ) {
  413. // decode choices (convert to array)
  414. $field['choices'] = acf_decode_choices( $field['choices'] );
  415. $field['default_value'] = acf_decode_choices( $field['default_value'], true );
  416. // Convert back to string for single selects.
  417. if ( ! $field['multiple'] ) {
  418. $field['default_value'] = acf_unarray( $field['default_value'] );
  419. }
  420. // return
  421. return $field;
  422. }
  423. /*
  424. * update_value()
  425. *
  426. * This filter is appied to the $value before it is updated in the db
  427. *
  428. * @type filter
  429. * @since 3.6
  430. * @date 23/01/13
  431. *
  432. * @param $value - the value which will be saved in the database
  433. * @param $post_id - the $post_id of which the value will be saved
  434. * @param $field - the field array holding all the field options
  435. *
  436. * @return $value - the modified value
  437. */
  438. function update_value( $value, $post_id, $field ) {
  439. // Bail early if no value.
  440. if ( empty( $value ) ) {
  441. return $value;
  442. }
  443. // Format array of values.
  444. // - Parse each value as string for SQL LIKE queries.
  445. if ( is_array( $value ) ) {
  446. $value = array_map( 'strval', $value );
  447. }
  448. // return
  449. return $value;
  450. }
  451. /*
  452. * translate_field
  453. *
  454. * This function will translate field settings
  455. *
  456. * @type function
  457. * @date 8/03/2016
  458. * @since 5.3.2
  459. *
  460. * @param $field (array)
  461. * @return $field
  462. */
  463. function translate_field( $field ) {
  464. // translate
  465. $field['choices'] = acf_translate( $field['choices'] );
  466. // return
  467. return $field;
  468. }
  469. /*
  470. * format_value()
  471. *
  472. * This filter is appied to the $value after it is loaded from the db and before it is returned to the template
  473. *
  474. * @type filter
  475. * @since 3.6
  476. * @date 23/01/13
  477. *
  478. * @param $value (mixed) the value which was loaded from the database
  479. * @param $post_id (mixed) the $post_id from which the value was loaded
  480. * @param $field (array) the field array holding all the field options
  481. *
  482. * @return $value (mixed) the modified value
  483. */
  484. function format_value( $value, $post_id, $field ) {
  485. if ( is_array( $value ) ) {
  486. foreach ( $value as $i => $val ) {
  487. $value[ $i ] = $this->format_value_single( $val, $post_id, $field );
  488. }
  489. } else {
  490. $value = $this->format_value_single( $value, $post_id, $field );
  491. }
  492. return $value;
  493. }
  494. function format_value_single( $value, $post_id, $field ) {
  495. // bail ealry if is empty
  496. if ( acf_is_empty( $value ) ) {
  497. return $value;
  498. }
  499. // vars
  500. $label = acf_maybe_get( $field['choices'], $value, $value );
  501. // value
  502. if ( $field['return_format'] == 'value' ) {
  503. // do nothing
  504. // label
  505. } elseif ( $field['return_format'] == 'label' ) {
  506. $value = $label;
  507. // array
  508. } elseif ( $field['return_format'] == 'array' ) {
  509. $value = array(
  510. 'value' => $value,
  511. 'label' => $label,
  512. );
  513. }
  514. // return
  515. return $value;
  516. }
  517. /**
  518. * Validates select fields updated via the REST API.
  519. *
  520. * @param bool $valid
  521. * @param int $value
  522. * @param array $field
  523. *
  524. * @return bool|WP_Error
  525. */
  526. public function validate_rest_value( $valid, $value, $field ) {
  527. // rest_validate_request_arg() handles the other types, we just worry about strings.
  528. if ( is_null( $value ) || is_array( $value ) ) {
  529. return $valid;
  530. }
  531. $option_keys = array_diff(
  532. array_keys( $field['choices'] ),
  533. array_values( $field['choices'] )
  534. );
  535. $allowed = empty( $option_keys ) ? $field['choices'] : $option_keys;
  536. if ( ! in_array( $value, $allowed ) ) {
  537. $param = sprintf( '%s[%s]', $field['prefix'], $field['name'] );
  538. $data = array(
  539. 'param' => $param,
  540. 'value' => $value,
  541. );
  542. $error = sprintf(
  543. __( '%1$s is not one of %2$s', 'acf' ),
  544. $param,
  545. implode( ', ', $allowed )
  546. );
  547. return new WP_Error( 'rest_invalid_param', $error, $data );
  548. }
  549. return $valid;
  550. }
  551. /**
  552. * Return the schema array for the REST API.
  553. *
  554. * @param array $field
  555. * @return array
  556. */
  557. public function get_rest_schema( array $field ) {
  558. /**
  559. * If a user has defined keys for the select options,
  560. * we should use the keys for the available options to POST to,
  561. * since they are what is displayed in GET requests.
  562. */
  563. $option_keys = array_diff(
  564. array_keys( $field['choices'] ),
  565. array_values( $field['choices'] )
  566. );
  567. $schema = array(
  568. 'type' => array( 'string', 'array', 'null' ),
  569. 'required' => ! empty( $field['required'] ),
  570. 'items' => array(
  571. 'type' => array( 'string' ),
  572. 'enum' => empty( $option_keys ) ? $field['choices'] : $option_keys,
  573. ),
  574. );
  575. if ( empty( $field['allow_null'] ) ) {
  576. $schema['minItems'] = 1;
  577. }
  578. if ( empty( $field['multiple'] ) ) {
  579. $schema['maxItems'] = 1;
  580. }
  581. if ( isset( $field['default_value'] ) && '' !== $field['default_value'] ) {
  582. $schema['default'] = $field['default_value'];
  583. }
  584. return $schema;
  585. }
  586. }
  587. // initialize
  588. acf_register_field_type( 'acf_field_select' );
  589. endif; // class_exists check