class-acf-field-date_picker.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <?php
  2. if ( ! class_exists( 'acf_field_date_picker' ) ) :
  3. class acf_field_date_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_picker';
  19. $this->label = __( 'Date Picker', 'acf' );
  20. $this->category = 'jquery';
  21. $this->defaults = array(
  22. 'display_format' => 'd/m/Y',
  23. 'return_format' => 'd/m/Y',
  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_datepicker' ) ) {
  42. return;
  43. }
  44. // localize
  45. global $wp_locale;
  46. acf_localize_data(
  47. array(
  48. 'datePickerL10n' => array(
  49. 'closeText' => _x( 'Done', 'Date Picker JS closeText', 'acf' ),
  50. 'currentText' => _x( 'Today', 'Date Picker JS currentText', 'acf' ),
  51. 'nextText' => _x( 'Next', 'Date Picker JS nextText', 'acf' ),
  52. 'prevText' => _x( 'Prev', 'Date Picker JS prevText', 'acf' ),
  53. 'weekHeader' => _x( 'Wk', 'Date Picker JS weekHeader', 'acf' ),
  54. 'monthNames' => array_values( $wp_locale->month ),
  55. 'monthNamesShort' => array_values( $wp_locale->month_abbrev ),
  56. 'dayNames' => array_values( $wp_locale->weekday ),
  57. 'dayNamesMin' => array_values( $wp_locale->weekday_initial ),
  58. 'dayNamesShort' => array_values( $wp_locale->weekday_abbrev ),
  59. ),
  60. )
  61. );
  62. // script
  63. wp_enqueue_script( 'jquery-ui-datepicker' );
  64. // style
  65. wp_enqueue_style( 'acf-datepicker', acf_get_url( 'assets/inc/datepicker/jquery-ui.min.css' ), array(), '1.11.4' );
  66. }
  67. /*
  68. * render_field()
  69. *
  70. * Create the HTML interface for your field
  71. *
  72. * @param $field - an array holding all the field's data
  73. *
  74. * @type action
  75. * @since 3.6
  76. * @date 23/01/13
  77. */
  78. function render_field( $field ) {
  79. // vars
  80. $hidden_value = '';
  81. $display_value = '';
  82. // format value
  83. if ( $field['value'] ) {
  84. $hidden_value = acf_format_date( $field['value'], 'Ymd' );
  85. $display_value = acf_format_date( $field['value'], $field['display_format'] );
  86. }
  87. // elements
  88. $div = array(
  89. 'class' => 'acf-date-picker acf-input-wrap',
  90. 'data-date_format' => acf_convert_date_to_js( $field['display_format'] ),
  91. 'data-first_day' => $field['first_day'],
  92. );
  93. $hidden_input = array(
  94. 'id' => $field['id'],
  95. 'name' => $field['name'],
  96. 'value' => $hidden_value,
  97. );
  98. $text_input = array(
  99. 'class' => $field['class'] . ' input',
  100. 'value' => $display_value,
  101. );
  102. // special attributes
  103. foreach ( array( 'readonly', 'disabled' ) as $k ) {
  104. if ( ! empty( $field[ $k ] ) ) {
  105. $hidden_input[ $k ] = $k;
  106. $text_input[ $k ] = $k;
  107. }
  108. }
  109. // save_format - compatibility with ACF < 5.0.0
  110. if ( ! empty( $field['save_format'] ) ) {
  111. // add custom JS save format
  112. $div['data-save_format'] = $field['save_format'];
  113. // revert hidden input value to raw DB value
  114. $hidden_input['value'] = $field['value'];
  115. // remove formatted value (will do this via JS)
  116. $text_input['value'] = '';
  117. }
  118. // html
  119. ?>
  120. <div <?php echo acf_esc_attrs( $div ); ?>>
  121. <?php acf_hidden_input( $hidden_input ); ?>
  122. <?php acf_text_input( $text_input ); ?>
  123. </div>
  124. <?php
  125. }
  126. /*
  127. * render_field_settings()
  128. *
  129. * Create extra options for your field. This is rendered when editing a field.
  130. * The value of $field['name'] can be used (like bellow) to save extra data to the $field
  131. *
  132. * @type action
  133. * @since 3.6
  134. * @date 23/01/13
  135. *
  136. * @param $field - an array holding all the field's data
  137. */
  138. function render_field_settings( $field ) {
  139. global $wp_locale;
  140. $d_m_Y = date_i18n( 'd/m/Y' );
  141. $m_d_Y = date_i18n( 'm/d/Y' );
  142. $F_j_Y = date_i18n( 'F j, Y' );
  143. $Ymd = date_i18n( 'Ymd' );
  144. echo '<div class="acf-field-settings-split">';
  145. acf_render_field_setting(
  146. $field,
  147. array(
  148. 'label' => __( 'Display Format', 'acf' ),
  149. 'hint' => __( 'The format displayed when editing a post', 'acf' ),
  150. 'type' => 'radio',
  151. 'name' => 'display_format',
  152. 'other_choice' => 1,
  153. 'choices' => array(
  154. 'd/m/Y' => '<span>' . $d_m_Y . '</span><code>d/m/Y</code>',
  155. 'm/d/Y' => '<span>' . $m_d_Y . '</span><code>m/d/Y</code>',
  156. 'F j, Y' => '<span>' . $F_j_Y . '</span><code>F j, Y</code>',
  157. 'other' => '<span>' . __( 'Custom:', 'acf' ) . '</span>',
  158. ),
  159. )
  160. );
  161. // save_format - compatibility with ACF < 5.0.0
  162. if ( ! empty( $field['save_format'] ) ) {
  163. acf_render_field_setting(
  164. $field,
  165. array(
  166. 'label' => __( 'Save Format', 'acf' ),
  167. 'hint' => __( 'The format used when saving a value', 'acf' ),
  168. 'type' => 'text',
  169. 'name' => 'save_format',
  170. // 'readonly' => 1 // this setting was not readonly in v4
  171. )
  172. );
  173. } else {
  174. acf_render_field_setting(
  175. $field,
  176. array(
  177. 'label' => __( 'Return Format', 'acf' ),
  178. 'hint' => __( 'The format returned via template functions', 'acf' ),
  179. 'type' => 'radio',
  180. 'name' => 'return_format',
  181. 'other_choice' => 1,
  182. 'choices' => array(
  183. 'd/m/Y' => '<span>' . $d_m_Y . '</span><code>d/m/Y</code>',
  184. 'm/d/Y' => '<span>' . $m_d_Y . '</span><code>m/d/Y</code>',
  185. 'F j, Y' => '<span>' . $F_j_Y . '</span><code>F j, Y</code>',
  186. 'Ymd' => '<span>' . $Ymd . '</span><code>Ymd</code>',
  187. 'other' => '<span>' . __( 'Custom:', 'acf' ) . '</span>',
  188. ),
  189. )
  190. );
  191. }
  192. echo '</div>';
  193. acf_render_field_setting(
  194. $field,
  195. array(
  196. 'label' => __( 'Week Starts On', 'acf' ),
  197. 'instructions' => '',
  198. 'type' => 'select',
  199. 'name' => 'first_day',
  200. 'choices' => array_values( $wp_locale->weekday ),
  201. )
  202. );
  203. }
  204. /*
  205. * format_value()
  206. *
  207. * This filter is appied to the $value after it is loaded from the db and before it is returned to the template
  208. *
  209. * @type filter
  210. * @since 3.6
  211. * @date 23/01/13
  212. *
  213. * @param $value (mixed) the value which was loaded from the database
  214. * @param $post_id (mixed) the $post_id from which the value was loaded
  215. * @param $field (array) the field array holding all the field options
  216. *
  217. * @return $value (mixed) the modified value
  218. */
  219. function format_value( $value, $post_id, $field ) {
  220. // save_format - compatibility with ACF < 5.0.0
  221. if ( ! empty( $field['save_format'] ) ) {
  222. return $value;
  223. }
  224. // return
  225. return acf_format_date( $value, $field['return_format'] );
  226. }
  227. /**
  228. * This filter is applied to the $field after it is loaded from the database
  229. * and ensures the return and display values are set.
  230. *
  231. * @type filter
  232. * @since 5.11.0
  233. * @date 28/09/21
  234. *
  235. * @param array $field The field array holding all the field options.
  236. *
  237. * @return array
  238. */
  239. function load_field( $field ) {
  240. if ( empty( $field['display_format'] ) ) {
  241. $field['display_format'] = $this->defaults['display_format'];
  242. }
  243. if ( empty( $field['return_format'] ) ) {
  244. $field['return_format'] = $this->defaults['return_format'];
  245. }
  246. return $field;
  247. }
  248. /**
  249. * Return the schema array for the REST API.
  250. *
  251. * @param array $field
  252. * @return array
  253. */
  254. public function get_rest_schema( array $field ) {
  255. return array(
  256. 'type' => array( 'string', 'null' ),
  257. 'description' => 'A `Ymd` formatted date string.',
  258. 'required' => ! empty( $field['required'] ),
  259. );
  260. }
  261. /**
  262. * Apply basic formatting to prepare the value for default REST output.
  263. *
  264. * @param mixed $value
  265. * @param string|int $post_id
  266. * @param array $field
  267. * @return mixed
  268. */
  269. public function format_value_for_rest( $value, $post_id, array $field ) {
  270. if ( ! $value ) {
  271. return null;
  272. }
  273. return (string) $value;
  274. }
  275. }
  276. // initialize
  277. acf_register_field_type( 'acf_field_date_picker' );
  278. endif; // class_exists check
  279. ?>