class-acf-field-accordion.php
5 months ago
class-acf-field-button-group.php
5 months ago
class-acf-field-checkbox.php
2 months ago
class-acf-field-color_picker.php
5 months ago
class-acf-field-date_picker.php
5 months ago
class-acf-field-date_time_picker.php
5 months ago
class-acf-field-email.php
5 months ago
class-acf-field-file.php
5 months ago
class-acf-field-google-map.php
5 months ago
class-acf-field-group.php
5 months ago
class-acf-field-icon_picker.php
6 months ago
class-acf-field-image.php
1 month ago
class-acf-field-link.php
5 months ago
class-acf-field-message.php
6 months ago
class-acf-field-number.php
5 months ago
class-acf-field-oembed.php
2 months ago
class-acf-field-output.php
6 months ago
class-acf-field-page_link.php
1 month ago
class-acf-field-password.php
5 months ago
class-acf-field-post_object.php
1 month ago
class-acf-field-radio.php
2 months ago
class-acf-field-range.php
5 months ago
class-acf-field-relationship.php
1 month ago
class-acf-field-select.php
2 months ago
class-acf-field-separator.php
6 months ago
class-acf-field-tab.php
6 months ago
class-acf-field-taxonomy.php
2 months ago
class-acf-field-text.php
5 months ago
class-acf-field-textarea.php
5 months ago
class-acf-field-time_picker.php
5 months ago
class-acf-field-true_false.php
5 months ago
class-acf-field-url.php
5 months ago
class-acf-field-user.php
2 months ago
class-acf-field-wysiwyg.php
5 months ago
class-acf-field.php
5 months ago
index.php
2 years ago
class-acf-field-date_picker.php
375 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package ACF |
| 4 | * @author WP Engine |
| 5 | * |
| 6 | * © 2026 Advanced Custom Fields (ACF®). All rights reserved. |
| 7 | * "ACF" is a trademark of WP Engine. |
| 8 | * Licensed under the GNU General Public License v2 or later. |
| 9 | * https://www.gnu.org/licenses/gpl-2.0.html |
| 10 | */ |
| 11 | |
| 12 | if ( ! class_exists( 'acf_field_date_picker' ) ) : |
| 13 | |
| 14 | class acf_field_date_picker extends acf_field { |
| 15 | |
| 16 | |
| 17 | /** |
| 18 | * This function will setup the field type data |
| 19 | * |
| 20 | * @type function |
| 21 | * @date 5/03/2014 |
| 22 | * @since 5.0.0 |
| 23 | * |
| 24 | * @param n/a |
| 25 | * @return n/a |
| 26 | */ |
| 27 | function initialize() { |
| 28 | |
| 29 | // vars |
| 30 | $this->name = 'date_picker'; |
| 31 | $this->label = __( 'Date Picker', 'acf' ); |
| 32 | $this->category = 'advanced'; |
| 33 | $this->description = __( 'An interactive UI for picking a date. The date return format can be customized using the field settings.', 'acf' ); |
| 34 | $this->preview_image = acf_get_url() . '/assets/images/field-type-previews/field-preview-date-picker.png'; |
| 35 | $this->doc_url = acf_add_url_utm_tags( 'https://www.advancedcustomfields.com/resources/date-picker/', 'docs', 'field-type-selection' ); |
| 36 | $this->defaults = array( |
| 37 | 'display_format' => 'd/m/Y', |
| 38 | 'return_format' => 'd/m/Y', |
| 39 | 'first_day' => 1, |
| 40 | 'default_to_current_date' => 0, |
| 41 | ); |
| 42 | } |
| 43 | |
| 44 | |
| 45 | /** |
| 46 | * description |
| 47 | * |
| 48 | * @type function |
| 49 | * @date 16/12/2015 |
| 50 | * @since 5.3.2 |
| 51 | * |
| 52 | * @param $post_id (int) |
| 53 | * @return $post_id (int) |
| 54 | */ |
| 55 | function input_admin_enqueue_scripts() { |
| 56 | |
| 57 | // bail early if no enqueue |
| 58 | if ( ! acf_get_setting( 'enqueue_datepicker' ) ) { |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | // localize |
| 63 | global $wp_locale; |
| 64 | acf_localize_data( |
| 65 | array( |
| 66 | 'datePickerL10n' => array( |
| 67 | 'closeText' => _x( 'Done', 'Date Picker JS closeText', 'acf' ), |
| 68 | 'currentText' => _x( 'Today', 'Date Picker JS currentText', 'acf' ), |
| 69 | 'nextText' => _x( 'Next', 'Date Picker JS nextText', 'acf' ), |
| 70 | 'prevText' => _x( 'Prev', 'Date Picker JS prevText', 'acf' ), |
| 71 | 'weekHeader' => _x( 'Wk', 'Date Picker JS weekHeader', 'acf' ), |
| 72 | 'monthNames' => array_values( $wp_locale->month ), |
| 73 | 'monthNamesShort' => array_values( $wp_locale->month_abbrev ), |
| 74 | 'dayNames' => array_values( $wp_locale->weekday ), |
| 75 | 'dayNamesMin' => array_values( $wp_locale->weekday_initial ), |
| 76 | 'dayNamesShort' => array_values( $wp_locale->weekday_abbrev ), |
| 77 | ), |
| 78 | ) |
| 79 | ); |
| 80 | |
| 81 | // script |
| 82 | wp_enqueue_script( 'jquery-ui-datepicker' ); |
| 83 | |
| 84 | // style |
| 85 | wp_enqueue_style( 'acf-datepicker', acf_get_url( 'assets/inc/datepicker/jquery-ui.min.css' ), array(), '1.11.4' ); |
| 86 | } |
| 87 | |
| 88 | |
| 89 | /** |
| 90 | * Create the HTML interface for your field |
| 91 | * |
| 92 | * @param $field - an array holding all the field's data |
| 93 | * |
| 94 | * @type action |
| 95 | * @since 3.6 |
| 96 | * @date 23/01/13 |
| 97 | */ |
| 98 | function render_field( $field ) { |
| 99 | |
| 100 | // vars |
| 101 | $hidden_value = ''; |
| 102 | $display_value = ''; |
| 103 | |
| 104 | // format value |
| 105 | if ( $field['value'] ) { |
| 106 | $hidden_value = acf_format_date( $field['value'], 'Ymd' ); |
| 107 | $display_value = acf_format_date( $field['value'], $field['display_format'] ); |
| 108 | } |
| 109 | |
| 110 | // elements |
| 111 | $div = array( |
| 112 | 'class' => 'acf-date-picker acf-input-wrap', |
| 113 | 'data-date_format' => acf_convert_date_to_js( $field['display_format'] ), |
| 114 | 'data-first_day' => $field['first_day'], |
| 115 | ); |
| 116 | $hidden_input = array( |
| 117 | 'id' => $field['id'], |
| 118 | 'name' => $field['name'], |
| 119 | 'value' => $hidden_value, |
| 120 | ); |
| 121 | $text_input = array( |
| 122 | 'class' => $field['class'] . ' input', |
| 123 | 'value' => $display_value, |
| 124 | 'data-default-to-today' => $field['default_to_current_date'], |
| 125 | ); |
| 126 | |
| 127 | // special attributes |
| 128 | foreach ( array( 'readonly', 'disabled' ) as $k ) { |
| 129 | if ( ! empty( $field[ $k ] ) ) { |
| 130 | $hidden_input[ $k ] = $k; |
| 131 | $text_input[ $k ] = $k; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | // save_format - compatibility with ACF < 5.0.0 |
| 136 | if ( ! empty( $field['save_format'] ) ) { |
| 137 | |
| 138 | // add custom JS save format |
| 139 | $div['data-save_format'] = $field['save_format']; |
| 140 | |
| 141 | // revert hidden input value to raw DB value |
| 142 | $hidden_input['value'] = $field['value']; |
| 143 | |
| 144 | // remove formatted value (will do this via JS) |
| 145 | $text_input['value'] = ''; |
| 146 | } |
| 147 | |
| 148 | // html |
| 149 | ?> |
| 150 | <div <?php echo acf_esc_attrs( $div ); ?>> |
| 151 | <?php acf_hidden_input( $hidden_input ); ?> |
| 152 | <?php acf_text_input( $text_input ); ?> |
| 153 | </div> |
| 154 | <?php |
| 155 | } |
| 156 | |
| 157 | |
| 158 | /** |
| 159 | * Create extra options for your field. This is rendered when editing a field. |
| 160 | * The value of $field['name'] can be used (like bellow) to save extra data to the $field |
| 161 | * |
| 162 | * @type action |
| 163 | * @since 3.6 |
| 164 | * @date 23/01/13 |
| 165 | * |
| 166 | * @param $field - an array holding all the field's data |
| 167 | */ |
| 168 | function render_field_settings( $field ) { |
| 169 | global $wp_locale; |
| 170 | |
| 171 | $d_m_Y = date_i18n( 'd/m/Y' ); |
| 172 | $m_d_Y = date_i18n( 'm/d/Y' ); |
| 173 | $F_j_Y = date_i18n( 'F j, Y' ); |
| 174 | $Ymd = date_i18n( 'Ymd' ); |
| 175 | |
| 176 | echo '<div class="acf-field-settings-split">'; |
| 177 | |
| 178 | acf_render_field_setting( |
| 179 | $field, |
| 180 | array( |
| 181 | 'label' => __( 'Display Format', 'acf' ), |
| 182 | 'hint' => __( 'The format displayed when editing a post', 'acf' ), |
| 183 | 'type' => 'radio', |
| 184 | 'name' => 'display_format', |
| 185 | 'other_choice' => 1, |
| 186 | 'choices' => array( |
| 187 | 'd/m/Y' => '<span>' . $d_m_Y . '</span><code>d/m/Y</code>', |
| 188 | 'm/d/Y' => '<span>' . $m_d_Y . '</span><code>m/d/Y</code>', |
| 189 | 'F j, Y' => '<span>' . $F_j_Y . '</span><code>F j, Y</code>', |
| 190 | 'other' => '<span>' . __( 'Custom:', 'acf' ) . '</span>', |
| 191 | ), |
| 192 | ) |
| 193 | ); |
| 194 | |
| 195 | // save_format - compatibility with ACF < 5.0.0 |
| 196 | if ( ! empty( $field['save_format'] ) ) { |
| 197 | acf_render_field_setting( |
| 198 | $field, |
| 199 | array( |
| 200 | 'label' => __( 'Save Format', 'acf' ), |
| 201 | 'hint' => __( 'The format used when saving a value', 'acf' ), |
| 202 | 'type' => 'text', |
| 203 | 'name' => 'save_format', |
| 204 | // 'readonly' => 1 // this setting was not readonly in v4 |
| 205 | ) |
| 206 | ); |
| 207 | } else { |
| 208 | acf_render_field_setting( |
| 209 | $field, |
| 210 | array( |
| 211 | 'label' => __( 'Return Format', 'acf' ), |
| 212 | 'hint' => __( 'The format returned via template functions', 'acf' ), |
| 213 | 'type' => 'radio', |
| 214 | 'name' => 'return_format', |
| 215 | 'other_choice' => 1, |
| 216 | 'choices' => array( |
| 217 | 'd/m/Y' => '<span>' . $d_m_Y . '</span><code>d/m/Y</code>', |
| 218 | 'm/d/Y' => '<span>' . $m_d_Y . '</span><code>m/d/Y</code>', |
| 219 | 'F j, Y' => '<span>' . $F_j_Y . '</span><code>F j, Y</code>', |
| 220 | 'Ymd' => '<span>' . $Ymd . '</span><code>Ymd</code>', |
| 221 | 'other' => '<span>' . __( 'Custom:', 'acf' ) . '</span>', |
| 222 | ), |
| 223 | ) |
| 224 | ); |
| 225 | } |
| 226 | |
| 227 | echo '</div>'; |
| 228 | |
| 229 | acf_render_field_setting( |
| 230 | $field, |
| 231 | array( |
| 232 | 'label' => __( 'Week Starts On', 'acf' ), |
| 233 | 'instructions' => '', |
| 234 | 'type' => 'select', |
| 235 | 'name' => 'first_day', |
| 236 | 'choices' => array_values( $wp_locale->weekday ), |
| 237 | ) |
| 238 | ); |
| 239 | |
| 240 | acf_render_field_setting( |
| 241 | $field, |
| 242 | array( |
| 243 | 'label' => __( 'Default to the current date', 'acf' ), |
| 244 | 'instructions' => __( 'Use the current date as the default value for this field.', 'acf' ), |
| 245 | 'type' => 'true_false', |
| 246 | 'name' => 'default_to_current_date', |
| 247 | 'ui' => 1, |
| 248 | ) |
| 249 | ); |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * This filter is appied to the $value after it is loaded from the db and before it is returned to the template |
| 254 | * |
| 255 | * @type filter |
| 256 | * @since 3.6 |
| 257 | * @date 23/01/13 |
| 258 | * |
| 259 | * @param $value (mixed) the value which was loaded from the database |
| 260 | * @param $post_id (mixed) the post_id from which the value was loaded |
| 261 | * @param $field (array) the field array holding all the field options |
| 262 | * |
| 263 | * @return $value (mixed) the modified value |
| 264 | */ |
| 265 | function format_value( $value, $post_id, $field ) { |
| 266 | |
| 267 | // save_format - compatibility with ACF < 5.0.0 |
| 268 | if ( ! empty( $field['save_format'] ) ) { |
| 269 | return $value; |
| 270 | } |
| 271 | |
| 272 | // return |
| 273 | return acf_format_date( $value, $field['return_format'] ); |
| 274 | } |
| 275 | |
| 276 | |
| 277 | /** |
| 278 | * This filter is applied to the $field after it is loaded from the database |
| 279 | * and ensures the return and display values are set. |
| 280 | * |
| 281 | * @type filter |
| 282 | * @since 5.11.0 |
| 283 | * @date 28/09/21 |
| 284 | * |
| 285 | * @param array $field The field array holding all the field options. |
| 286 | * @return array |
| 287 | */ |
| 288 | public function load_field( $field ) { |
| 289 | if ( empty( $field['display_format'] ) ) { |
| 290 | $field['display_format'] = $this->defaults['display_format']; |
| 291 | } |
| 292 | |
| 293 | if ( empty( $field['return_format'] ) ) { |
| 294 | $field['return_format'] = $this->defaults['return_format']; |
| 295 | } |
| 296 | |
| 297 | return $field; |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * Return the schema array for the REST API. |
| 302 | * |
| 303 | * @param array $field The field array |
| 304 | * @return array |
| 305 | */ |
| 306 | public function get_rest_schema( array $field ) { |
| 307 | return array( |
| 308 | 'type' => array( 'string', 'null' ), |
| 309 | 'description' => 'A `Ymd` formatted date string.', |
| 310 | 'required' => ! empty( $field['required'] ), |
| 311 | ); |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * Apply basic formatting to prepare the value for default REST output. |
| 316 | * |
| 317 | * @param mixed $value |
| 318 | * @param string|integer $post_id |
| 319 | * @param array $field |
| 320 | * @return mixed |
| 321 | */ |
| 322 | public function format_value_for_rest( $value, $post_id, array $field ) { |
| 323 | if ( ! $value ) { |
| 324 | return null; |
| 325 | } |
| 326 | |
| 327 | return (string) $value; |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * Returns an array of JSON-LD Property output types that are supported by this field type. |
| 332 | * |
| 333 | * @since 6.8 |
| 334 | * |
| 335 | * @return string[] |
| 336 | */ |
| 337 | public function get_jsonld_output_types(): array { |
| 338 | return array( 'Date' ); |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * Formats the field value for JSON-LD output. |
| 343 | * |
| 344 | * Converts the stored Ymd format to ISO 8601 date format. |
| 345 | * |
| 346 | * @since 6.8.0 |
| 347 | * |
| 348 | * @param mixed $value The value of the field. |
| 349 | * @param integer|string $post_id The ID of the post. |
| 350 | * @param array $field The field array. |
| 351 | * @return string|null ISO 8601 formatted date or null. |
| 352 | */ |
| 353 | public function format_value_for_jsonld( $value, $post_id, $field ) { |
| 354 | if ( empty( $value ) || ! is_string( $value ) ) { |
| 355 | return null; |
| 356 | } |
| 357 | |
| 358 | // ACF stores date_picker internally as 'Ymd' (e.g., 20231225). |
| 359 | $date = \DateTime::createFromFormat( 'Ymd', $value ); |
| 360 | if ( ! $date ) { |
| 361 | return null; |
| 362 | } |
| 363 | |
| 364 | // Return ISO 8601 date format (YYYY-MM-DD). |
| 365 | return $date->format( 'Y-m-d' ); |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | |
| 370 | // initialize |
| 371 | acf_register_field_type( 'acf_field_date_picker' ); |
| 372 | endif; // class_exists check |
| 373 | |
| 374 | ?> |
| 375 |