Нет описания

class-acf-field-time_picker.php 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 acf_esc_attr_e( $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. // vars
  88. $g_i_a = date_i18n( 'g:i a' );
  89. $H_i_s = date_i18n( 'H:i:s' );
  90. // display_format
  91. acf_render_field_setting(
  92. $field,
  93. array(
  94. 'label' => __( 'Display Format', 'acf' ),
  95. 'instructions' => __( 'The format displayed when editing a post', 'acf' ),
  96. 'type' => 'radio',
  97. 'name' => 'display_format',
  98. 'other_choice' => 1,
  99. 'choices' => array(
  100. 'g:i a' => '<span>' . $g_i_a . '</span><code>g:i a</code>',
  101. 'H:i:s' => '<span>' . $H_i_s . '</span><code>H:i:s</code>',
  102. 'other' => '<span>' . __( 'Custom:', 'acf' ) . '</span>',
  103. ),
  104. )
  105. );
  106. // return_format
  107. acf_render_field_setting(
  108. $field,
  109. array(
  110. 'label' => __( 'Return Format', 'acf' ),
  111. 'instructions' => __( 'The format returned via template functions', 'acf' ),
  112. 'type' => 'radio',
  113. 'name' => 'return_format',
  114. 'other_choice' => 1,
  115. 'choices' => array(
  116. 'g:i a' => '<span>' . $g_i_a . '</span><code>g:i a</code>',
  117. 'H:i:s' => '<span>' . $H_i_s . '</span><code>H:i:s</code>',
  118. 'other' => '<span>' . __( 'Custom:', 'acf' ) . '</span>',
  119. ),
  120. )
  121. );
  122. }
  123. /*
  124. * format_value()
  125. *
  126. * This filter is appied to the $value after it is loaded from the db and before it is returned to the template
  127. *
  128. * @type filter
  129. * @since 3.6
  130. * @date 23/01/13
  131. *
  132. * @param $value (mixed) the value which was loaded from the database
  133. * @param $post_id (mixed) the $post_id from which the value was loaded
  134. * @param $field (array) the field array holding all the field options
  135. *
  136. * @return $value (mixed) the modified value
  137. */
  138. function format_value( $value, $post_id, $field ) {
  139. return acf_format_date( $value, $field['return_format'] );
  140. }
  141. /**
  142. * This filter is applied to the $field after it is loaded from the database
  143. * and ensures the return and display values are set.
  144. *
  145. * @type filter
  146. * @since 5.11.0
  147. * @date 28/09/21
  148. *
  149. * @param array $field The field array holding all the field options.
  150. *
  151. * @return array
  152. */
  153. function load_field( $field ) {
  154. if ( empty( $field['display_format'] ) ) {
  155. $field['display_format'] = $this->defaults['display_format'];
  156. }
  157. if ( empty( $field['return_format'] ) ) {
  158. $field['return_format'] = $this->defaults['return_format'];
  159. }
  160. return $field;
  161. }
  162. /**
  163. * Return the schema array for the REST API.
  164. *
  165. * @param array $field
  166. * @return array
  167. */
  168. public function get_rest_schema( array $field ) {
  169. return array(
  170. 'type' => array( 'string', 'null' ),
  171. 'description' => 'A `H:i:s` formatted time string.',
  172. 'required' => ! empty( $field['required'] ),
  173. );
  174. }
  175. }
  176. // initialize
  177. acf_register_field_type( 'acf_field_time_picker' );
  178. endif; // class_exists check
  179. ?>