class-acf-field-date_time_picker.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. if ( ! class_exists( 'acf_field_date_and_time_picker' ) ) :
  3. class acf_field_date_and_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 = 'date_time_picker';
  19. $this->label = __( 'Date Time Picker', 'acf' );
  20. $this->category = 'jquery';
  21. $this->defaults = array(
  22. 'display_format' => 'd/m/Y g:i a',
  23. 'return_format' => 'd/m/Y g:i a',
  24. 'first_day' => 1,
  25. );
  26. }
  27. /*
  28. * input_admin_enqueue_scripts
  29. *
  30. * description
  31. *
  32. * @type function
  33. * @date 16/12/2015
  34. * @since 5.3.2
  35. *
  36. * @param $post_id (int)
  37. * @return $post_id (int)
  38. */
  39. function input_admin_enqueue_scripts() {
  40. // bail early if no enqueue
  41. if ( ! acf_get_setting( 'enqueue_datetimepicker' ) ) {
  42. return;
  43. }
  44. // vars
  45. $version = '1.6.1';
  46. // script
  47. wp_enqueue_script( 'acf-timepicker', acf_get_url( 'assets/inc/timepicker/jquery-ui-timepicker-addon.min.js' ), array( 'jquery-ui-datepicker' ), $version );
  48. // style
  49. wp_enqueue_style( 'acf-timepicker', acf_get_url( 'assets/inc/timepicker/jquery-ui-timepicker-addon.min.css' ), '', $version );
  50. // localize
  51. acf_localize_data(
  52. array(
  53. 'dateTimePickerL10n' => array(
  54. 'timeOnlyTitle' => _x( 'Choose Time', 'Date Time Picker JS timeOnlyTitle', 'acf' ),
  55. 'timeText' => _x( 'Time', 'Date Time Picker JS timeText', 'acf' ),
  56. 'hourText' => _x( 'Hour', 'Date Time Picker JS hourText', 'acf' ),
  57. 'minuteText' => _x( 'Minute', 'Date Time Picker JS minuteText', 'acf' ),
  58. 'secondText' => _x( 'Second', 'Date Time Picker JS secondText', 'acf' ),
  59. 'millisecText' => _x( 'Millisecond', 'Date Time Picker JS millisecText', 'acf' ),
  60. 'microsecText' => _x( 'Microsecond', 'Date Time Picker JS microsecText', 'acf' ),
  61. 'timezoneText' => _x( 'Time Zone', 'Date Time Picker JS timezoneText', 'acf' ),
  62. 'currentText' => _x( 'Now', 'Date Time Picker JS currentText', 'acf' ),
  63. 'closeText' => _x( 'Done', 'Date Time Picker JS closeText', 'acf' ),
  64. 'selectText' => _x( 'Select', 'Date Time Picker JS selectText', 'acf' ),
  65. 'amNames' => array(
  66. _x( 'AM', 'Date Time Picker JS amText', 'acf' ),
  67. _x( 'A', 'Date Time Picker JS amTextShort', 'acf' ),
  68. ),
  69. 'pmNames' => array(
  70. _x( 'PM', 'Date Time Picker JS pmText', 'acf' ),
  71. _x( 'P', 'Date Time Picker JS pmTextShort', 'acf' ),
  72. ),
  73. ),
  74. )
  75. );
  76. }
  77. /*
  78. * render_field()
  79. *
  80. * Create the HTML interface for your field
  81. *
  82. * @param $field - an array holding all the field's data
  83. *
  84. * @type action
  85. * @since 3.6
  86. * @date 23/01/13
  87. */
  88. function render_field( $field ) {
  89. // Set value.
  90. $hidden_value = '';
  91. $display_value = '';
  92. if ( $field['value'] ) {
  93. $hidden_value = acf_format_date( $field['value'], 'Y-m-d H:i:s' );
  94. $display_value = acf_format_date( $field['value'], $field['display_format'] );
  95. }
  96. // Convert "display_format" setting to individual date and time formats.
  97. $formats = acf_split_date_time( $field['display_format'] );
  98. // Elements.
  99. $div = array(
  100. 'class' => 'acf-date-time-picker acf-input-wrap',
  101. 'data-date_format' => acf_convert_date_to_js( $formats['date'] ),
  102. 'data-time_format' => acf_convert_time_to_js( $formats['time'] ),
  103. 'data-first_day' => $field['first_day'],
  104. );
  105. $hidden_input = array(
  106. 'id' => $field['id'],
  107. 'class' => 'input-alt',
  108. 'name' => $field['name'],
  109. 'value' => $hidden_value,
  110. );
  111. $text_input = array(
  112. 'class' => $field['class'] . ' input',
  113. 'value' => $display_value,
  114. );
  115. foreach ( array( 'readonly', 'disabled' ) as $k ) {
  116. if ( ! empty( $field[ $k ] ) ) {
  117. $hidden_input[ $k ] = $k;
  118. $text_input[ $k ] = $k;
  119. }
  120. }
  121. // Output.
  122. ?>
  123. <div <?php echo acf_esc_attrs( $div ); ?>>
  124. <?php acf_hidden_input( $hidden_input ); ?>
  125. <?php acf_text_input( $text_input ); ?>
  126. </div>
  127. <?php
  128. }
  129. /*
  130. * render_field_settings()
  131. *
  132. * Create extra options for your field. This is rendered when editing a field.
  133. * The value of $field['name'] can be used (like bellow) to save extra data to the $field
  134. *
  135. * @type action
  136. * @since 3.6
  137. * @date 23/01/13
  138. *
  139. * @param $field - an array holding all the field's data
  140. */
  141. function render_field_settings( $field ) {
  142. global $wp_locale;
  143. $d_m_Y = date_i18n( 'd/m/Y g:i a' );
  144. $m_d_Y = date_i18n( 'm/d/Y g:i a' );
  145. $F_j_Y = date_i18n( 'F j, Y g:i a' );
  146. $Ymd = date_i18n( 'Y-m-d H:i:s' );
  147. echo '<div class="acf-field-settings-split">';
  148. acf_render_field_setting(
  149. $field,
  150. array(
  151. 'label' => __( 'Display Format', 'acf' ),
  152. 'hint' => __( 'The format displayed when editing a post', 'acf' ),
  153. 'type' => 'radio',
  154. 'name' => 'display_format',
  155. 'other_choice' => 1,
  156. 'choices' => array(
  157. 'd/m/Y g:i a' => '<span>' . $d_m_Y . '</span><code>d/m/Y g:i a</code>',
  158. 'm/d/Y g:i a' => '<span>' . $m_d_Y . '</span><code>m/d/Y g:i a</code>',
  159. 'F j, Y g:i a' => '<span>' . $F_j_Y . '</span><code>F j, Y g:i a</code>',
  160. 'Y-m-d H:i:s' => '<span>' . $Ymd . '</span><code>Y-m-d H:i:s</code>',
  161. 'other' => '<span>' . __( 'Custom:', 'acf' ) . '</span>',
  162. ),
  163. )
  164. );
  165. acf_render_field_setting(
  166. $field,
  167. array(
  168. 'label' => __( 'Return Format', 'acf' ),
  169. 'hint' => __( 'The format returned via template functions', 'acf' ),
  170. 'type' => 'radio',
  171. 'name' => 'return_format',
  172. 'other_choice' => 1,
  173. 'choices' => array(
  174. 'd/m/Y g:i a' => '<span>' . $d_m_Y . '</span><code>d/m/Y g:i a</code>',
  175. 'm/d/Y g:i a' => '<span>' . $m_d_Y . '</span><code>m/d/Y g:i a</code>',
  176. 'F j, Y g:i a' => '<span>' . $F_j_Y . '</span><code>F j, Y g:i a</code>',
  177. 'Y-m-d H:i:s' => '<span>' . $Ymd . '</span><code>Y-m-d H:i:s</code>',
  178. 'other' => '<span>' . __( 'Custom:', 'acf' ) . '</span>',
  179. ),
  180. )
  181. );
  182. echo '</div>';
  183. acf_render_field_setting(
  184. $field,
  185. array(
  186. 'label' => __( 'Week Starts On', 'acf' ),
  187. 'instructions' => '',
  188. 'type' => 'select',
  189. 'name' => 'first_day',
  190. 'choices' => array_values( $wp_locale->weekday ),
  191. )
  192. );
  193. }
  194. /*
  195. * format_value()
  196. *
  197. * This filter is appied to the $value after it is loaded from the db and before it is returned to the template
  198. *
  199. * @type filter
  200. * @since 3.6
  201. * @date 23/01/13
  202. *
  203. * @param $value (mixed) the value which was loaded from the database
  204. * @param $post_id (mixed) the $post_id from which the value was loaded
  205. * @param $field (array) the field array holding all the field options
  206. *
  207. * @return $value (mixed) the modified value
  208. */
  209. function format_value( $value, $post_id, $field ) {
  210. return acf_format_date( $value, $field['return_format'] );
  211. }
  212. /**
  213. * This filter is applied to the $field after it is loaded from the database
  214. * and ensures the return and display values are set.
  215. *
  216. * @type filter
  217. * @since 5.11.0
  218. * @date 28/09/21
  219. *
  220. * @param array $field The field array holding all the field options.
  221. *
  222. * @return array
  223. */
  224. function load_field( $field ) {
  225. if ( empty( $field['display_format'] ) ) {
  226. $field['display_format'] = $this->defaults['display_format'];
  227. }
  228. if ( empty( $field['return_format'] ) ) {
  229. $field['return_format'] = $this->defaults['return_format'];
  230. }
  231. return $field;
  232. }
  233. /**
  234. * Return the schema array for the REST API.
  235. *
  236. * @param array $field
  237. * @return array
  238. */
  239. public function get_rest_schema( array $field ) {
  240. return array(
  241. 'type' => array( 'string', 'null' ),
  242. 'description' => 'A `Y-m-d H:i:s` formatted date string.',
  243. 'required' => ! empty( $field['required'] ),
  244. );
  245. }
  246. }
  247. // initialize
  248. acf_register_field_type( 'acf_field_date_and_time_picker' );
  249. endif; // class_exists check
  250. ?>