123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 |
- <?php
- if ( ! class_exists( 'acf_field_date_picker' ) ) :
- class acf_field_date_picker extends acf_field {
-
- function initialize() {
-
- $this->name = 'date_picker';
- $this->label = __( 'Date Picker', 'acf' );
- $this->category = 'jquery';
- $this->defaults = array(
- 'display_format' => 'd/m/Y',
- 'return_format' => 'd/m/Y',
- 'first_day' => 1,
- );
- }
-
- function input_admin_enqueue_scripts() {
-
- if ( ! acf_get_setting( 'enqueue_datepicker' ) ) {
- return;
- }
-
- global $wp_locale;
- acf_localize_data(
- array(
- 'datePickerL10n' => array(
- 'closeText' => _x( 'Done', 'Date Picker JS closeText', 'acf' ),
- 'currentText' => _x( 'Today', 'Date Picker JS currentText', 'acf' ),
- 'nextText' => _x( 'Next', 'Date Picker JS nextText', 'acf' ),
- 'prevText' => _x( 'Prev', 'Date Picker JS prevText', 'acf' ),
- 'weekHeader' => _x( 'Wk', 'Date Picker JS weekHeader', 'acf' ),
- 'monthNames' => array_values( $wp_locale->month ),
- 'monthNamesShort' => array_values( $wp_locale->month_abbrev ),
- 'dayNames' => array_values( $wp_locale->weekday ),
- 'dayNamesMin' => array_values( $wp_locale->weekday_initial ),
- 'dayNamesShort' => array_values( $wp_locale->weekday_abbrev ),
- ),
- )
- );
-
- wp_enqueue_script( 'jquery-ui-datepicker' );
-
- wp_enqueue_style( 'acf-datepicker', acf_get_url( 'assets/inc/datepicker/jquery-ui.min.css' ), array(), '1.11.4' );
- }
-
- function render_field( $field ) {
-
- $hidden_value = '';
- $display_value = '';
-
- if ( $field['value'] ) {
- $hidden_value = acf_format_date( $field['value'], 'Ymd' );
- $display_value = acf_format_date( $field['value'], $field['display_format'] );
- }
-
- $div = array(
- 'class' => 'acf-date-picker acf-input-wrap',
- 'data-date_format' => acf_convert_date_to_js( $field['display_format'] ),
- 'data-first_day' => $field['first_day'],
- );
- $hidden_input = array(
- 'id' => $field['id'],
- 'name' => $field['name'],
- 'value' => $hidden_value,
- );
- $text_input = array(
- 'class' => $field['class'] . ' input',
- 'value' => $display_value,
- );
-
- foreach ( array( 'readonly', 'disabled' ) as $k ) {
- if ( ! empty( $field[ $k ] ) ) {
- $hidden_input[ $k ] = $k;
- $text_input[ $k ] = $k;
- }
- }
-
- if ( ! empty( $field['save_format'] ) ) {
-
- $div['data-save_format'] = $field['save_format'];
-
- $hidden_input['value'] = $field['value'];
-
- $text_input['value'] = '';
- }
-
- ?>
- <div <?php echo acf_esc_attrs( $div ); ?>>
- <?php acf_hidden_input( $hidden_input ); ?>
- <?php acf_text_input( $text_input ); ?>
- </div>
- <?php
- }
-
- function render_field_settings( $field ) {
- global $wp_locale;
- $d_m_Y = date_i18n( 'd/m/Y' );
- $m_d_Y = date_i18n( 'm/d/Y' );
- $F_j_Y = date_i18n( 'F j, Y' );
- $Ymd = date_i18n( 'Ymd' );
- echo '<div class="acf-field-settings-split">';
- acf_render_field_setting(
- $field,
- array(
- 'label' => __( 'Display Format', 'acf' ),
- 'hint' => __( 'The format displayed when editing a post', 'acf' ),
- 'type' => 'radio',
- 'name' => 'display_format',
- 'other_choice' => 1,
- 'choices' => array(
- 'd/m/Y' => '<span>' . $d_m_Y . '</span><code>d/m/Y</code>',
- 'm/d/Y' => '<span>' . $m_d_Y . '</span><code>m/d/Y</code>',
- 'F j, Y' => '<span>' . $F_j_Y . '</span><code>F j, Y</code>',
- 'other' => '<span>' . __( 'Custom:', 'acf' ) . '</span>',
- ),
- )
- );
-
- if ( ! empty( $field['save_format'] ) ) {
- acf_render_field_setting(
- $field,
- array(
- 'label' => __( 'Save Format', 'acf' ),
- 'hint' => __( 'The format used when saving a value', 'acf' ),
- 'type' => 'text',
- 'name' => 'save_format',
-
- )
- );
- } else {
- acf_render_field_setting(
- $field,
- array(
- 'label' => __( 'Return Format', 'acf' ),
- 'hint' => __( 'The format returned via template functions', 'acf' ),
- 'type' => 'radio',
- 'name' => 'return_format',
- 'other_choice' => 1,
- 'choices' => array(
- 'd/m/Y' => '<span>' . $d_m_Y . '</span><code>d/m/Y</code>',
- 'm/d/Y' => '<span>' . $m_d_Y . '</span><code>m/d/Y</code>',
- 'F j, Y' => '<span>' . $F_j_Y . '</span><code>F j, Y</code>',
- 'Ymd' => '<span>' . $Ymd . '</span><code>Ymd</code>',
- 'other' => '<span>' . __( 'Custom:', 'acf' ) . '</span>',
- ),
- )
- );
- }
- echo '</div>';
- acf_render_field_setting(
- $field,
- array(
- 'label' => __( 'Week Starts On', 'acf' ),
- 'instructions' => '',
- 'type' => 'select',
- 'name' => 'first_day',
- 'choices' => array_values( $wp_locale->weekday ),
- )
- );
- }
-
- function format_value( $value, $post_id, $field ) {
-
- if ( ! empty( $field['save_format'] ) ) {
- return $value;
- }
-
- return acf_format_date( $value, $field['return_format'] );
- }
-
- function load_field( $field ) {
- if ( empty( $field['display_format'] ) ) {
- $field['display_format'] = $this->defaults['display_format'];
- }
- if ( empty( $field['return_format'] ) ) {
- $field['return_format'] = $this->defaults['return_format'];
- }
- return $field;
- }
-
- public function get_rest_schema( array $field ) {
- return array(
- 'type' => array( 'string', 'null' ),
- 'description' => 'A `Ymd` formatted date string.',
- 'required' => ! empty( $field['required'] ),
- );
- }
-
- public function format_value_for_rest( $value, $post_id, array $field ) {
- if ( ! $value ) {
- return null;
- }
- return (string) $value;
- }
- }
-
- acf_register_field_type( 'acf_field_date_picker' );
- endif;
- ?>
|