class-acf-field-true_false.php 6.9 KB

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. acf_render_field_setting(
  109. $field,
  110. array(
  111. 'label' => __( 'Message', 'acf' ),
  112. 'instructions' => __( 'Displays text alongside the checkbox', 'acf' ),
  113. 'type' => 'text',
  114. 'name' => 'message',
  115. )
  116. );
  117. acf_render_field_setting(
  118. $field,
  119. array(
  120. 'label' => __( 'Default Value', 'acf' ),
  121. 'instructions' => '',
  122. 'type' => 'true_false',
  123. 'name' => 'default_value',
  124. )
  125. );
  126. }
  127. /**
  128. * Renders the field settings used in the "Presentation" tab.
  129. *
  130. * @since 6.0
  131. *
  132. * @param array $field The field settings array.
  133. * @return void
  134. */
  135. function render_field_presentation_settings( $field ) {
  136. acf_render_field_setting(
  137. $field,
  138. array(
  139. 'label' => __( 'On Text', 'acf' ),
  140. 'instructions' => __( 'Text shown when active', 'acf' ),
  141. 'type' => 'text',
  142. 'name' => 'ui_on_text',
  143. 'placeholder' => __( 'Yes', 'acf' ),
  144. 'conditions' => array(
  145. 'field' => 'ui',
  146. 'operator' => '==',
  147. 'value' => 1,
  148. ),
  149. )
  150. );
  151. acf_render_field_setting(
  152. $field,
  153. array(
  154. 'label' => __( 'Off Text', 'acf' ),
  155. 'instructions' => __( 'Text shown when inactive', 'acf' ),
  156. 'type' => 'text',
  157. 'name' => 'ui_off_text',
  158. 'placeholder' => __( 'No', 'acf' ),
  159. 'conditions' => array(
  160. 'field' => 'ui',
  161. 'operator' => '==',
  162. 'value' => 1,
  163. ),
  164. )
  165. );
  166. acf_render_field_setting(
  167. $field,
  168. array(
  169. 'label' => __( 'Stylized UI', 'acf' ),
  170. 'instructions' => __( 'Use a stylized checkbox using select2', 'acf' ),
  171. 'type' => 'true_false',
  172. 'name' => 'ui',
  173. 'ui' => 1,
  174. 'class' => 'acf-field-object-true-false-ui',
  175. )
  176. );
  177. }
  178. /*
  179. * format_value()
  180. *
  181. * This filter is appied to the $value after it is loaded from the db and before it is returned to the template
  182. *
  183. * @type filter
  184. * @since 3.6
  185. * @date 23/01/13
  186. *
  187. * @param $value (mixed) the value which was loaded from the database
  188. * @param $post_id (mixed) the $post_id from which the value was loaded
  189. * @param $field (array) the field array holding all the field options
  190. *
  191. * @return $value (mixed) the modified value
  192. */
  193. function format_value( $value, $post_id, $field ) {
  194. return empty( $value ) ? false : true;
  195. }
  196. /*
  197. * validate_value
  198. *
  199. * description
  200. *
  201. * @type function
  202. * @date 11/02/2014
  203. * @since 5.0.0
  204. *
  205. * @param $post_id (int)
  206. * @return $post_id (int)
  207. */
  208. function validate_value( $valid, $value, $field, $input ) {
  209. // bail early if not required
  210. if ( ! $field['required'] ) {
  211. return $valid;
  212. }
  213. // value may be '0'
  214. if ( ! $value ) {
  215. return false;
  216. }
  217. // return
  218. return $valid;
  219. }
  220. /*
  221. * translate_field
  222. *
  223. * This function will translate field settings
  224. *
  225. * @type function
  226. * @date 8/03/2016
  227. * @since 5.3.2
  228. *
  229. * @param $field (array)
  230. * @return $field
  231. */
  232. function translate_field( $field ) {
  233. // translate
  234. $field['message'] = acf_translate( $field['message'] );
  235. $field['ui_on_text'] = acf_translate( $field['ui_on_text'] );
  236. $field['ui_off_text'] = acf_translate( $field['ui_off_text'] );
  237. // return
  238. return $field;
  239. }
  240. /**
  241. * Return the schema array for the REST API.
  242. *
  243. * @param array $field
  244. * @return array
  245. */
  246. public function get_rest_schema( array $field ) {
  247. $schema = array(
  248. 'type' => array( 'boolean', 'null' ),
  249. 'required' => ! empty( $field['required'] ),
  250. );
  251. if ( isset( $field['default_value'] ) && '' !== $field['default_value'] ) {
  252. $schema['default'] = (bool) $field['default_value'];
  253. }
  254. return $schema;
  255. }
  256. /**
  257. * Apply basic formatting to prepare the value for default REST output.
  258. *
  259. * @param mixed $value
  260. * @param string|int $post_id
  261. * @param array $field
  262. * @return mixed
  263. */
  264. public function format_value_for_rest( $value, $post_id, array $field ) {
  265. return (bool) $value;
  266. }
  267. }
  268. // initialize
  269. acf_register_field_type( 'acf_field_true_false' );
  270. endif; // class_exists check
  271. ?>