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