暂无描述

class-acf-field-true_false.php 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <?php
  2. if ( ! class_exists( 'acf_field_true_false' ) ) :
  3. class acf_field_true_false 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 = 'true_false';
  19. $this->label = __( 'True / False', 'acf' );
  20. $this->category = 'choice';
  21. $this->defaults = array(
  22. 'default_value' => 0,
  23. 'message' => '',
  24. 'ui' => 0,
  25. 'ui_on_text' => '',
  26. 'ui_off_text' => '',
  27. );
  28. }
  29. /*
  30. * render_field()
  31. *
  32. * Create the HTML interface for your field
  33. *
  34. * @param $field - an array holding all the field's data
  35. *
  36. * @type action
  37. * @since 3.6
  38. * @date 23/01/13
  39. */
  40. function render_field( $field ) {
  41. // vars
  42. $input = array(
  43. 'type' => 'checkbox',
  44. 'id' => $field['id'],
  45. 'name' => $field['name'],
  46. 'value' => '1',
  47. 'class' => $field['class'],
  48. 'autocomplete' => 'off',
  49. );
  50. $hidden = array(
  51. 'name' => $field['name'],
  52. 'value' => 0,
  53. );
  54. $active = $field['value'] ? true : false;
  55. $switch = '';
  56. // checked
  57. if ( $active ) {
  58. $input['checked'] = 'checked';
  59. }
  60. // ui
  61. if ( $field['ui'] ) {
  62. // vars
  63. if ( $field['ui_on_text'] === '' ) {
  64. $field['ui_on_text'] = __( 'Yes', 'acf' );
  65. }
  66. if ( $field['ui_off_text'] === '' ) {
  67. $field['ui_off_text'] = __( 'No', 'acf' );
  68. }
  69. // update input
  70. $input['class'] .= ' acf-switch-input';
  71. // $input['style'] = 'display:none;';
  72. $switch .= '<div class="acf-switch' . ( $active ? ' -on' : '' ) . '">';
  73. $switch .= '<span class="acf-switch-on">' . $field['ui_on_text'] . '</span>';
  74. $switch .= '<span class="acf-switch-off">' . $field['ui_off_text'] . '</span>';
  75. $switch .= '<div class="acf-switch-slider"></div>';
  76. $switch .= '</div>';
  77. }
  78. ?>
  79. <div class="acf-true-false">
  80. <?php acf_hidden_input( $hidden ); ?>
  81. <label>
  82. <input <?php echo acf_esc_attr( $input ); ?>/>
  83. <?php
  84. if ( $switch ) {
  85. echo acf_esc_html( $switch );}
  86. ?>
  87. <?php
  88. if ( $field['message'] ) :
  89. ?>
  90. <span class="message"><?php echo acf_esc_html( $field['message'] ); ?></span><?php endif; ?>
  91. </label>
  92. </div>
  93. <?php
  94. }
  95. /*
  96. * render_field_settings()
  97. *
  98. * Create extra options for your field. This is rendered when editing a field.
  99. * The value of $field['name'] can be used (like bellow) to save extra data to the $field
  100. *
  101. * @type action
  102. * @since 3.6
  103. * @date 23/01/13
  104. *
  105. * @param $field - an array holding all the field's data
  106. */
  107. function render_field_settings( $field ) {
  108. // message
  109. acf_render_field_setting(
  110. $field,
  111. array(
  112. 'label' => __( 'Message', 'acf' ),
  113. 'instructions' => __( 'Displays text alongside the checkbox', 'acf' ),
  114. 'type' => 'text',
  115. 'name' => 'message',
  116. )
  117. );
  118. // default_value
  119. acf_render_field_setting(
  120. $field,
  121. array(
  122. 'label' => __( 'Default Value', 'acf' ),
  123. 'instructions' => '',
  124. 'type' => 'true_false',
  125. 'name' => 'default_value',
  126. )
  127. );
  128. // ui
  129. acf_render_field_setting(
  130. $field,
  131. array(
  132. 'label' => __( 'Stylised UI', 'acf' ),
  133. 'instructions' => '',
  134. 'type' => 'true_false',
  135. 'name' => 'ui',
  136. 'ui' => 1,
  137. 'class' => 'acf-field-object-true-false-ui',
  138. )
  139. );
  140. // on_text
  141. acf_render_field_setting(
  142. $field,
  143. array(
  144. 'label' => __( 'On Text', 'acf' ),
  145. 'instructions' => __( 'Text shown when active', 'acf' ),
  146. 'type' => 'text',
  147. 'name' => 'ui_on_text',
  148. 'placeholder' => __( 'Yes', 'acf' ),
  149. 'conditions' => array(
  150. 'field' => 'ui',
  151. 'operator' => '==',
  152. 'value' => 1,
  153. ),
  154. )
  155. );
  156. // on_text
  157. acf_render_field_setting(
  158. $field,
  159. array(
  160. 'label' => __( 'Off Text', 'acf' ),
  161. 'instructions' => __( 'Text shown when inactive', 'acf' ),
  162. 'type' => 'text',
  163. 'name' => 'ui_off_text',
  164. 'placeholder' => __( 'No', 'acf' ),
  165. 'conditions' => array(
  166. 'field' => 'ui',
  167. 'operator' => '==',
  168. 'value' => 1,
  169. ),
  170. )
  171. );
  172. }
  173. /*
  174. * format_value()
  175. *
  176. * This filter is appied to the $value after it is loaded from the db and before it is returned to the template
  177. *
  178. * @type filter
  179. * @since 3.6
  180. * @date 23/01/13
  181. *
  182. * @param $value (mixed) the value which was loaded from the database
  183. * @param $post_id (mixed) the $post_id from which the value was loaded
  184. * @param $field (array) the field array holding all the field options
  185. *
  186. * @return $value (mixed) the modified value
  187. */
  188. function format_value( $value, $post_id, $field ) {
  189. return empty( $value ) ? false : true;
  190. }
  191. /*
  192. * validate_value
  193. *
  194. * description
  195. *
  196. * @type function
  197. * @date 11/02/2014
  198. * @since 5.0.0
  199. *
  200. * @param $post_id (int)
  201. * @return $post_id (int)
  202. */
  203. function validate_value( $valid, $value, $field, $input ) {
  204. // bail early if not required
  205. if ( ! $field['required'] ) {
  206. return $valid;
  207. }
  208. // value may be '0'
  209. if ( ! $value ) {
  210. return false;
  211. }
  212. // return
  213. return $valid;
  214. }
  215. /*
  216. * translate_field
  217. *
  218. * This function will translate field settings
  219. *
  220. * @type function
  221. * @date 8/03/2016
  222. * @since 5.3.2
  223. *
  224. * @param $field (array)
  225. * @return $field
  226. */
  227. function translate_field( $field ) {
  228. // translate
  229. $field['message'] = acf_translate( $field['message'] );
  230. $field['ui_on_text'] = acf_translate( $field['ui_on_text'] );
  231. $field['ui_off_text'] = acf_translate( $field['ui_off_text'] );
  232. // return
  233. return $field;
  234. }
  235. /**
  236. * Return the schema array for the REST API.
  237. *
  238. * @param array $field
  239. * @return array
  240. */
  241. public function get_rest_schema( array $field ) {
  242. $schema = array(
  243. 'type' => array( 'boolean', 'null' ),
  244. 'required' => ! empty( $field['required'] ),
  245. );
  246. if ( isset( $field['default_value'] ) && '' !== $field['default_value'] ) {
  247. $schema['default'] = (bool) $field['default_value'];
  248. }
  249. return $schema;
  250. }
  251. /**
  252. * Apply basic formatting to prepare the value for default REST output.
  253. *
  254. * @param mixed $value
  255. * @param string|int $post_id
  256. * @param array $field
  257. * @return mixed
  258. */
  259. public function format_value_for_rest( $value, $post_id, array $field ) {
  260. return (bool) $value;
  261. }
  262. }
  263. // initialize
  264. acf_register_field_type( 'acf_field_true_false' );
  265. endif; // class_exists check
  266. ?>