/plugins/advanced-custom-fields/core/fields/select.php

https://bitbucket.org/iwtechru/rivoinvest.com · PHP · 357 lines · 211 code · 61 blank · 85 comment · 20 complexity · beae51f98162f9d8f1ccc8f9c5080e6f MD5 · raw file

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