PageRenderTime 26ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-content/plugins/advanced-custom-fields-pro/fields/select.php

https://gitlab.com/sihabudinahmad/asppi
PHP | 564 lines | 222 code | 172 blank | 170 comment | 25 complexity | 096c6159f64240d0b1977dc79e5be39a MD5 | raw file
  1. <?php
  2. /*
  3. * ACF Select Field Class
  4. *
  5. * All the logic for this field type
  6. *
  7. * @class acf_field_select
  8. * @extends acf_field
  9. * @package ACF
  10. * @subpackage Fields
  11. */
  12. if( ! class_exists('acf_field_select') ) :
  13. class acf_field_select extends acf_field {
  14. /*
  15. * __construct
  16. *
  17. * This function will setup the field type data
  18. *
  19. * @type function
  20. * @date 5/03/2014
  21. * @since 5.0.0
  22. *
  23. * @param n/a
  24. * @return n/a
  25. */
  26. function __construct() {
  27. // vars
  28. $this->name = 'select';
  29. $this->label = __("Select",'acf');
  30. $this->category = 'choice';
  31. $this->defaults = array(
  32. 'multiple' => 0,
  33. 'allow_null' => 0,
  34. 'choices' => array(),
  35. 'default_value' => '',
  36. 'ui' => 0,
  37. 'ajax' => 0,
  38. 'placeholder' => '',
  39. 'disabled' => 0,
  40. 'readonly' => 0,
  41. );
  42. // ajax
  43. add_action('wp_ajax_acf/fields/select/query', array($this, 'ajax_query'));
  44. add_action('wp_ajax_nopriv_acf/fields/select/query', array($this, 'ajax_query'));
  45. // do not delete!
  46. parent::__construct();
  47. }
  48. /*
  49. * query_posts
  50. *
  51. * description
  52. *
  53. * @type function
  54. * @date 24/10/13
  55. * @since 5.0.0
  56. *
  57. * @param n/a
  58. * @return n/a
  59. */
  60. function ajax_query() {
  61. // options
  62. $options = acf_parse_args( $_POST, array(
  63. 'post_id' => 0,
  64. 's' => '',
  65. 'field_key' => '',
  66. 'nonce' => '',
  67. ));
  68. // load field
  69. $field = acf_get_field( $options['field_key'] );
  70. if( !$field ) {
  71. die();
  72. }
  73. // vars
  74. $r = array();
  75. $s = false;
  76. // search
  77. if( $options['s'] !== '' ) {
  78. // search may be integer
  79. $s = strval($options['s']);
  80. // strip slashes
  81. $s = wp_unslash($s);
  82. }
  83. // loop through choices
  84. if( !empty($field['choices']) ) {
  85. foreach( $field['choices'] as $k => $v ) {
  86. // if searching, but doesn't exist
  87. if( $s !== false && stripos($v, $s) === false ) {
  88. continue;
  89. }
  90. // append
  91. $r[] = array(
  92. 'id' => $k,
  93. 'text' => strval( $v )
  94. );
  95. }
  96. }
  97. // return JSON
  98. echo json_encode( $r );
  99. die();
  100. }
  101. /*
  102. * render_field()
  103. *
  104. * Create the HTML interface for your field
  105. *
  106. * @param $field - an array holding all the field's data
  107. *
  108. * @type action
  109. * @since 3.6
  110. * @date 23/01/13
  111. */
  112. function render_field( $field ) {
  113. // convert value to array
  114. $field['value'] = acf_force_type_array($field['value']);
  115. // add empty value (allows '' to be selected)
  116. if( empty($field['value']) ){
  117. $field['value'][''] = '';
  118. }
  119. // placeholder
  120. if( empty($field['placeholder']) ) {
  121. $field['placeholder'] = __("Select",'acf');
  122. }
  123. // vars
  124. $atts = array(
  125. 'id' => $field['id'],
  126. 'class' => $field['class'],
  127. 'name' => $field['name'],
  128. 'data-ui' => $field['ui'],
  129. 'data-ajax' => $field['ajax'],
  130. 'data-multiple' => $field['multiple'],
  131. 'data-placeholder' => $field['placeholder'],
  132. 'data-allow_null' => $field['allow_null']
  133. );
  134. // ui
  135. if( $field['ui'] ) {
  136. $atts['disabled'] = 'disabled';
  137. $atts['class'] .= ' acf-hidden';
  138. }
  139. // multiple
  140. if( $field['multiple'] ) {
  141. $atts['multiple'] = 'multiple';
  142. $atts['size'] = 5;
  143. $atts['name'] .= '[]';
  144. }
  145. // special atts
  146. foreach( array( 'readonly', 'disabled' ) as $k ) {
  147. if( !empty($field[ $k ]) ) {
  148. $atts[ $k ] = $k;
  149. }
  150. }
  151. // vars
  152. $els = array();
  153. $choices = array();
  154. // loop through values and add them as options
  155. if( !empty($field['choices']) ) {
  156. foreach( $field['choices'] as $k => $v ) {
  157. if( is_array($v) ){
  158. // optgroup
  159. $els[] = array( 'type' => 'optgroup', 'label' => $k );
  160. if( !empty($v) ) {
  161. foreach( $v as $k2 => $v2 ) {
  162. $els[] = array( 'type' => 'option', 'value' => $k2, 'label' => $v2, 'selected' => in_array($k2, $field['value']) );
  163. $choices[] = $k2;
  164. }
  165. }
  166. $els[] = array( 'type' => '/optgroup' );
  167. } else {
  168. $els[] = array( 'type' => 'option', 'value' => $k, 'label' => $v, 'selected' => in_array($k, $field['value']) );
  169. $choices[] = $k;
  170. }
  171. }
  172. }
  173. // prepende orphans
  174. /*
  175. if( !empty($field['value']) ) {
  176. foreach( $field['value'] as $v ) {
  177. if( empty($v) ) {
  178. continue;
  179. }
  180. if( !in_array($v, $choices) ) {
  181. array_unshift( $els, array( 'type' => 'option', 'value' => $v, 'label' => $v, 'selected' => true ) );
  182. }
  183. }
  184. }
  185. */
  186. // hidden input
  187. if( $field['ui'] ) {
  188. // find real value based on $choices and $field['value']
  189. $real_value = array_intersect($field['value'], $choices);
  190. acf_hidden_input(array(
  191. 'type' => 'hidden',
  192. 'id' => $field['id'],
  193. 'name' => $field['name'],
  194. 'value' => implode(',', $real_value)
  195. ));
  196. } elseif( $field['multiple'] ) {
  197. acf_hidden_input(array(
  198. 'type' => 'hidden',
  199. 'name' => $field['name'],
  200. ));
  201. }
  202. // null
  203. if( $field['allow_null'] ) {
  204. array_unshift( $els, array( 'type' => 'option', 'value' => '', 'label' => '- ' . $field['placeholder'] . ' -' ) );
  205. }
  206. // html
  207. echo '<select ' . acf_esc_attr( $atts ) . '>';
  208. // construct html
  209. if( !empty($els) ) {
  210. foreach( $els as $el ) {
  211. // extract type
  212. $type = acf_extract_var($el, 'type');
  213. if( $type == 'option' ) {
  214. // get label
  215. $label = acf_extract_var($el, 'label');
  216. // validate selected
  217. if( acf_extract_var($el, 'selected') ) {
  218. $el['selected'] = 'selected';
  219. }
  220. // echo
  221. echo '<option ' . acf_esc_attr( $el ) . '>' . $label . '</option>';
  222. } else {
  223. // echo
  224. echo '<' . $type . ' ' . acf_esc_attr( $el ) . '>';
  225. }
  226. }
  227. }
  228. echo '</select>';
  229. }
  230. /*
  231. * render_field_settings()
  232. *
  233. * Create extra options for your field. This is rendered when editing a field.
  234. * The value of $field['name'] can be used (like bellow) to save extra data to the $field
  235. *
  236. * @type action
  237. * @since 3.6
  238. * @date 23/01/13
  239. *
  240. * @param $field - an array holding all the field's data
  241. */
  242. function render_field_settings( $field ) {
  243. // encode choices (convert from array)
  244. $field['choices'] = acf_encode_choices($field['choices']);
  245. $field['default_value'] = acf_encode_choices($field['default_value']);
  246. // choices
  247. acf_render_field_setting( $field, array(
  248. 'label' => __('Choices','acf'),
  249. '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'),
  250. 'type' => 'textarea',
  251. 'name' => 'choices',
  252. ));
  253. // default_value
  254. acf_render_field_setting( $field, array(
  255. 'label' => __('Default Value','acf'),
  256. 'instructions' => __('Enter each default value on a new line','acf'),
  257. 'type' => 'textarea',
  258. 'name' => 'default_value',
  259. ));
  260. // allow_null
  261. acf_render_field_setting( $field, array(
  262. 'label' => __('Allow Null?','acf'),
  263. 'instructions' => '',
  264. 'type' => 'radio',
  265. 'name' => 'allow_null',
  266. 'choices' => array(
  267. 1 => __("Yes",'acf'),
  268. 0 => __("No",'acf'),
  269. ),
  270. 'layout' => 'horizontal',
  271. ));
  272. // multiple
  273. acf_render_field_setting( $field, array(
  274. 'label' => __('Select multiple values?','acf'),
  275. 'instructions' => '',
  276. 'type' => 'radio',
  277. 'name' => 'multiple',
  278. 'choices' => array(
  279. 1 => __("Yes",'acf'),
  280. 0 => __("No",'acf'),
  281. ),
  282. 'layout' => 'horizontal',
  283. ));
  284. // ui
  285. acf_render_field_setting( $field, array(
  286. 'label' => __('Stylised UI','acf'),
  287. 'instructions' => '',
  288. 'type' => 'radio',
  289. 'name' => 'ui',
  290. 'choices' => array(
  291. 1 => __("Yes",'acf'),
  292. 0 => __("No",'acf'),
  293. ),
  294. 'layout' => 'horizontal',
  295. ));
  296. // ajax
  297. acf_render_field_setting( $field, array(
  298. 'label' => __('Use AJAX to lazy load choices?','acf'),
  299. 'instructions' => '',
  300. 'type' => 'radio',
  301. 'name' => 'ajax',
  302. 'choices' => array(
  303. 1 => __("Yes",'acf'),
  304. 0 => __("No",'acf'),
  305. ),
  306. 'layout' => 'horizontal',
  307. ));
  308. }
  309. /*
  310. * load_value()
  311. *
  312. * This filter is applied to the $value after it is loaded from the db
  313. *
  314. * @type filter
  315. * @since 3.6
  316. * @date 23/01/13
  317. *
  318. * @param $value (mixed) the value found in the database
  319. * @param $post_id (mixed) the $post_id from which the value was loaded
  320. * @param $field (array) the field array holding all the field options
  321. * @return $value
  322. */
  323. function load_value( $value, $post_id, $field ) {
  324. // ACF4 null
  325. if( $value === 'null' ) {
  326. return false;
  327. }
  328. // return
  329. return $value;
  330. }
  331. /*
  332. * update_field()
  333. *
  334. * This filter is appied to the $field before it is saved to the database
  335. *
  336. * @type filter
  337. * @since 3.6
  338. * @date 23/01/13
  339. *
  340. * @param $field - the field array holding all the field options
  341. * @param $post_id - the field group ID (post_type = acf)
  342. *
  343. * @return $field - the modified field
  344. */
  345. function update_field( $field ) {
  346. // decode choices (convert to array)
  347. $field['choices'] = acf_decode_choices($field['choices']);
  348. $field['default_value'] = acf_decode_choices($field['default_value']);
  349. // return
  350. return $field;
  351. }
  352. /*
  353. * update_value()
  354. *
  355. * This filter is appied to the $value before it is updated in the db
  356. *
  357. * @type filter
  358. * @since 3.6
  359. * @date 23/01/13
  360. *
  361. * @param $value - the value which will be saved in the database
  362. * @param $post_id - the $post_id of which the value will be saved
  363. * @param $field - the field array holding all the field options
  364. *
  365. * @return $value - the modified value
  366. */
  367. function update_value( $value, $post_id, $field ) {
  368. // validate
  369. if( empty($value) ) {
  370. return $value;
  371. }
  372. // array
  373. if( is_array($value) ) {
  374. // save value as strings, so we can clearly search for them in SQL LIKE statements
  375. $value = array_map('strval', $value);
  376. }
  377. // return
  378. return $value;
  379. }
  380. }
  381. new acf_field_select();
  382. endif;
  383. ?>