/wordpress/wp-content/plugins/advanced-custom-fields/core/fields/select.php

https://bitbucket.org/gfelizola/pacaembu-institucional · PHP · 362 lines · 232 code · 61 blank · 69 comment · 25 complexity · 7dc5cf5778d3cd3e2147ca1f649acc21 MD5 · raw file

  1. <?php
  2. class acf_Select extends acf_Field
  3. {
  4. /*--------------------------------------------------------------------------------------
  5. *
  6. * Constructor
  7. *
  8. * @author Elliot Condon
  9. * @since 1.0.0
  10. * @updated 2.2.0
  11. *
  12. *-------------------------------------------------------------------------------------*/
  13. function __construct($parent)
  14. {
  15. parent::__construct($parent);
  16. $this->name = 'select';
  17. $this->title = __("Select",'acf');
  18. // filters (for all fields with choices)
  19. add_filter('acf_save_field-select', array($this, 'acf_save_field'));
  20. add_filter('acf_save_field-checkbox', array($this, 'acf_save_field'));
  21. add_filter('acf_save_field-radio', array($this, 'acf_save_field'));
  22. }
  23. /*--------------------------------------------------------------------------------------
  24. *
  25. * create_field
  26. *
  27. * @author Elliot Condon
  28. * @since 2.0.5
  29. * @updated 2.2.0
  30. *
  31. *-------------------------------------------------------------------------------------*/
  32. function create_field($field)
  33. {
  34. // vars
  35. $defaults = array(
  36. 'value' => array(),
  37. 'multiple' => 0,
  38. 'allow_null' => 0,
  39. 'choices' => array(),
  40. 'optgroup' => 0,
  41. );
  42. $field = array_merge($defaults, $field);
  43. // no choices
  44. if(empty($field['choices']))
  45. {
  46. echo '<p>' . __("No choices to choose from",'acf') . '</p>';
  47. return false;
  48. }
  49. // multiple select
  50. $multiple = '';
  51. if( $field['multiple'] )
  52. {
  53. // create a hidden field to allow for no selections
  54. echo '<input type="hidden" name="' . $field['name'] . '" />';
  55. $multiple = ' multiple="multiple" size="5" ';
  56. $field['name'] .= '[]';
  57. }
  58. // html
  59. echo '<select id="' . $field['id'] . '" class="' . $field['class'] . '" name="' . $field['name'] . '" ' . $multiple . ' >';
  60. // null
  61. if( $field['allow_null'] )
  62. {
  63. echo '<option value="null"> - Select - </option>';
  64. }
  65. // loop through values and add them as options
  66. foreach($field['choices'] as $key => $value)
  67. {
  68. if($field['optgroup'])
  69. {
  70. // this select is grouped with optgroup
  71. if($key != '') echo '<optgroup label="'.$key.'">';
  72. if($value)
  73. {
  74. foreach($value as $id => $label)
  75. {
  76. $selected = '';
  77. if(is_array($field['value']) && in_array($id, $field['value']))
  78. {
  79. // 2. If the value is an array (multiple select), loop through values and check if it is selected
  80. $selected = 'selected="selected"';
  81. }
  82. else
  83. {
  84. // 3. this is not a multiple select, just check normaly
  85. if($id == $field['value'])
  86. {
  87. $selected = 'selected="selected"';
  88. }
  89. }
  90. echo '<option value="'.$id.'" '.$selected.'>'.$label.'</option>';
  91. }
  92. }
  93. if($key != '') echo '</optgroup>';
  94. }
  95. else
  96. {
  97. $selected = '';
  98. if(is_array($field['value']) && in_array($key, $field['value']))
  99. {
  100. // 2. If the value is an array (multiple select), loop through values and check if it is selected
  101. $selected = 'selected="selected"';
  102. }
  103. else
  104. {
  105. // 3. this is not a multiple select, just check normaly
  106. if($key == $field['value'])
  107. {
  108. $selected = 'selected="selected"';
  109. }
  110. }
  111. echo '<option value="'.$key.'" '.$selected.'>'.$value.'</option>';
  112. }
  113. }
  114. echo '</select>';
  115. }
  116. /*--------------------------------------------------------------------------------------
  117. *
  118. * create_options
  119. *
  120. * @author Elliot Condon
  121. * @since 2.0.6
  122. * @updated 2.2.0
  123. *
  124. *-------------------------------------------------------------------------------------*/
  125. function create_options($key, $field)
  126. {
  127. // vars
  128. $defaults = array(
  129. 'multiple' => 0,
  130. 'allow_null' => 0,
  131. 'default_value' => '',
  132. 'choices' => '',
  133. );
  134. $field = array_merge($defaults, $field);
  135. // implode selects so they work in a textarea
  136. if(isset($field['choices']) && is_array($field['choices']))
  137. {
  138. foreach($field['choices'] as $choice_key => $choice_val)
  139. {
  140. $field['choices'][$choice_key] = $choice_key.' : '.$choice_val;
  141. }
  142. $field['choices'] = implode("\n", $field['choices']);
  143. }
  144. else
  145. {
  146. $field['choices'] = "";
  147. }
  148. ?>
  149. <tr class="field_option field_option_<?php echo $this->name; ?>">
  150. <td class="label">
  151. <label for=""><?php _e("Choices",'acf'); ?></label>
  152. <p class="description"><?php _e("Enter your choices one per line",'acf'); ?><br />
  153. <br />
  154. <?php _e("Red",'acf'); ?><br />
  155. <?php _e("Blue",'acf'); ?><br />
  156. <br />
  157. <?php _e("red : Red",'acf'); ?><br />
  158. <?php _e("blue : Blue",'acf'); ?><br />
  159. </p>
  160. </td>
  161. <td>
  162. <?php
  163. do_action('acf/create_field', array(
  164. 'type' => 'textarea',
  165. 'class' => 'textarea field_option-choices',
  166. 'name' => 'fields['.$key.'][choices]',
  167. 'value' => $field['choices'],
  168. ));
  169. ?>
  170. </td>
  171. </tr>
  172. <tr class="field_option field_option_<?php echo $this->name; ?>">
  173. <td class="label">
  174. <label><?php _e("Default Value",'acf'); ?></label>
  175. </td>
  176. <td>
  177. <?php
  178. do_action('acf/create_field', array(
  179. 'type' => 'text',
  180. 'name' => 'fields['.$key.'][default_value]',
  181. 'value' => $field['default_value'],
  182. ));
  183. ?>
  184. </td>
  185. </tr>
  186. <tr class="field_option field_option_<?php echo $this->name; ?>">
  187. <td class="label">
  188. <label><?php _e("Allow Null?",'acf'); ?></label>
  189. </td>
  190. <td>
  191. <?php
  192. do_action('acf/create_field', array(
  193. 'type' => 'radio',
  194. 'name' => 'fields['.$key.'][allow_null]',
  195. 'value' => $field['allow_null'],
  196. 'choices' => array(
  197. 1 => __("Yes",'acf'),
  198. 0 => __("No",'acf'),
  199. ),
  200. 'layout' => 'horizontal',
  201. ));
  202. ?>
  203. </td>
  204. </tr>
  205. <tr class="field_option field_option_<?php echo $this->name; ?>">
  206. <td class="label">
  207. <label><?php _e("Select multiple values?",'acf'); ?></label>
  208. </td>
  209. <td>
  210. <?php
  211. do_action('acf/create_field', array(
  212. 'type' => 'radio',
  213. 'name' => 'fields['.$key.'][multiple]',
  214. 'value' => $field['multiple'],
  215. 'choices' => array(
  216. 1 => __("Yes",'acf'),
  217. 0 => __("No",'acf'),
  218. ),
  219. 'layout' => 'horizontal',
  220. ));
  221. ?>
  222. </td>
  223. </tr>
  224. <?php
  225. }
  226. /*--------------------------------------------------------------------------------------
  227. *
  228. * pre_save_field
  229. * - called just before saving the field to the database.
  230. *
  231. * @author Elliot Condon
  232. * @since 2.2.0
  233. *
  234. *-------------------------------------------------------------------------------------*/
  235. function acf_save_field( $field )
  236. {
  237. // vars
  238. $defaults = array(
  239. 'choices' => '',
  240. );
  241. $field = array_merge($defaults, $field);
  242. // check if is array. Normal back end edit posts a textarea, but a user might use update_field from the front end
  243. if( is_array( $field['choices'] ))
  244. {
  245. return $field;
  246. }
  247. // vars
  248. $new_choices = array();
  249. // explode choices from each line
  250. if( $field['choices'] )
  251. {
  252. // stripslashes ("")
  253. $field['choices'] = stripslashes_deep($field['choices']);
  254. if(strpos($field['choices'], "\n") !== false)
  255. {
  256. // found multiple lines, explode it
  257. $field['choices'] = explode("\n", $field['choices']);
  258. }
  259. else
  260. {
  261. // no multiple lines!
  262. $field['choices'] = array($field['choices']);
  263. }
  264. // key => value
  265. foreach($field['choices'] as $choice)
  266. {
  267. if(strpos($choice, ' : ') !== false)
  268. {
  269. $choice = explode(' : ', $choice);
  270. $new_choices[trim($choice[0])] = trim($choice[1]);
  271. }
  272. else
  273. {
  274. $new_choices[trim($choice)] = trim($choice);
  275. }
  276. }
  277. }
  278. // update choices
  279. $field['choices'] = $new_choices;
  280. // return updated field
  281. return $field;
  282. }
  283. /*--------------------------------------------------------------------------------------
  284. *
  285. * get_value_for_api
  286. *
  287. * @author Elliot Condon
  288. * @since 3.1.2
  289. *
  290. *-------------------------------------------------------------------------------------*/
  291. function get_value_for_api($post_id, $field)
  292. {
  293. $value = parent::get_value($post_id, $field);
  294. if($value == 'null')
  295. {
  296. $value = false;
  297. }
  298. return $value;
  299. }
  300. }
  301. ?>