class-acf-field-time_picker.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. if ( ! class_exists( 'acf_field_time_picker' ) ) :
  3. class acf_field_time_picker 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 = 'time_picker';
  19. $this->label = __( 'Time Picker', 'acf' );
  20. $this->category = 'jquery';
  21. $this->defaults = array(
  22. 'display_format' => 'g:i a',
  23. 'return_format' => 'g:i a',
  24. );
  25. }
  26. /*
  27. * render_field()
  28. *
  29. * Create the HTML interface for your field
  30. *
  31. * @param $field - an array holding all the field's data
  32. *
  33. * @type action
  34. * @since 3.6
  35. * @date 23/01/13
  36. */
  37. function render_field( $field ) {
  38. // Set value.
  39. $display_value = '';
  40. if ( $field['value'] ) {
  41. $display_value = acf_format_date( $field['value'], $field['display_format'] );
  42. }
  43. // Elements.
  44. $div = array(
  45. 'class' => 'acf-time-picker acf-input-wrap',
  46. 'data-time_format' => acf_convert_time_to_js( $field['display_format'] ),
  47. );
  48. $hidden_input = array(
  49. 'id' => $field['id'],
  50. 'class' => 'input-alt',
  51. 'type' => 'hidden',
  52. 'name' => $field['name'],
  53. 'value' => $field['value'],
  54. );
  55. $text_input = array(
  56. 'class' => $field['class'] . ' input',
  57. 'type' => 'text',
  58. 'value' => $display_value,
  59. );
  60. foreach ( array( 'readonly', 'disabled' ) as $k ) {
  61. if ( ! empty( $field[ $k ] ) ) {
  62. $hidden_input[ $k ] = $k;
  63. $text_input[ $k ] = $k;
  64. }
  65. }
  66. // Output.
  67. ?>
  68. <div <?php echo acf_esc_attrs( $div ); ?>>
  69. <?php acf_hidden_input( $hidden_input ); ?>
  70. <?php acf_text_input( $text_input ); ?>
  71. </div>
  72. <?php
  73. }
  74. /*
  75. * render_field_settings()
  76. *
  77. * Create extra options for your field. This is rendered when editing a field.
  78. * The value of $field['name'] can be used (like bellow) to save extra data to the $field
  79. *
  80. * @type action
  81. * @since 3.6
  82. * @date 23/01/13
  83. *
  84. * @param $field - an array holding all the field's data
  85. */
  86. function render_field_settings( $field ) {
  87. $g_i_a = date_i18n( 'g:i a' );
  88. $H_i_s = date_i18n( 'H:i:s' );
  89. echo '<div class="acf-field-settings-split">';
  90. acf_render_field_setting(
  91. $field,
  92. array(
  93. 'label' => __( 'Display Format', 'acf' ),
  94. 'hint' => __( 'The format displayed when editing a post', 'acf' ),
  95. 'type' => 'radio',
  96. 'name' => 'display_format',
  97. 'other_choice' => 1,
  98. 'choices' => array(
  99. 'g:i a' => '<span>' . $g_i_a . '</span><code>g:i a</code>',
  100. 'H:i:s' => '<span>' . $H_i_s . '</span><code>H:i:s</code>',
  101. 'other' => '<span>' . __( 'Custom:', 'acf' ) . '</span>',
  102. ),
  103. )
  104. );
  105. acf_render_field_setting(
  106. $field,
  107. array(
  108. 'label' => __( 'Return Format', 'acf' ),
  109. 'hint' => __( 'The format returned via template functions', 'acf' ),
  110. 'type' => 'radio',
  111. 'name' => 'return_format',
  112. 'other_choice' => 1,
  113. 'choices' => array(
  114. 'g:i a' => '<span>' . $g_i_a . '</span><code>g:i a</code>',
  115. 'H:i:s' => '<span>' . $H_i_s . '</span><code>H:i:s</code>',
  116. 'other' => '<span>' . __( 'Custom:', 'acf' ) . '</span>',
  117. ),
  118. )
  119. );
  120. echo '</div>';
  121. }
  122. /*
  123. * format_value()
  124. *
  125. * This filter is appied to the $value after it is loaded from the db and before it is returned to the template
  126. *
  127. * @type filter
  128. * @since 3.6
  129. * @date 23/01/13
  130. *
  131. * @param $value (mixed) the value which was loaded from the database
  132. * @param $post_id (mixed) the $post_id from which the value was loaded
  133. * @param $field (array) the field array holding all the field options
  134. *
  135. * @return $value (mixed) the modified value
  136. */
  137. function format_value( $value, $post_id, $field ) {
  138. return acf_format_date( $value, $field['return_format'] );
  139. }
  140. /**
  141. * This filter is applied to the $field after it is loaded from the database
  142. * and ensures the return and display values are set.
  143. *
  144. * @type filter
  145. * @since 5.11.0
  146. * @date 28/09/21
  147. *
  148. * @param array $field The field array holding all the field options.
  149. *
  150. * @return array
  151. */
  152. function load_field( $field ) {
  153. if ( empty( $field['display_format'] ) ) {
  154. $field['display_format'] = $this->defaults['display_format'];
  155. }
  156. if ( empty( $field['return_format'] ) ) {
  157. $field['return_format'] = $this->defaults['return_format'];
  158. }
  159. return $field;
  160. }
  161. /**
  162. * Return the schema array for the REST API.
  163. *
  164. * @param array $field
  165. * @return array
  166. */
  167. public function get_rest_schema( array $field ) {
  168. return array(
  169. 'type' => array( 'string', 'null' ),
  170. 'description' => 'A `H:i:s` formatted time string.',
  171. 'required' => ! empty( $field['required'] ),
  172. );
  173. }
  174. }
  175. // initialize
  176. acf_register_field_type( 'acf_field_time_picker' );
  177. endif; // class_exists check
  178. ?>