class-acf-field-accordion.php
1 year ago
class-acf-field-button-group.php
1 year ago
class-acf-field-checkbox.php
1 year ago
class-acf-field-color_picker.php
1 year ago
class-acf-field-date_picker.php
1 year ago
class-acf-field-date_time_picker.php
1 year ago
class-acf-field-email.php
1 year ago
class-acf-field-file.php
1 year ago
class-acf-field-google-map.php
1 year ago
class-acf-field-group.php
1 year ago
class-acf-field-icon_picker.php
1 year ago
class-acf-field-image.php
1 year ago
class-acf-field-link.php
1 year ago
class-acf-field-message.php
1 year ago
class-acf-field-number.php
1 year ago
class-acf-field-oembed.php
1 year ago
class-acf-field-output.php
1 year ago
class-acf-field-page_link.php
1 year ago
class-acf-field-password.php
1 year ago
class-acf-field-post_object.php
1 year ago
class-acf-field-radio.php
1 year ago
class-acf-field-range.php
1 year ago
class-acf-field-relationship.php
1 year ago
class-acf-field-select.php
1 year ago
class-acf-field-separator.php
1 year ago
class-acf-field-tab.php
1 year ago
class-acf-field-taxonomy.php
1 year ago
class-acf-field-text.php
1 year ago
class-acf-field-textarea.php
1 year ago
class-acf-field-time_picker.php
1 year ago
class-acf-field-true_false.php
1 year ago
class-acf-field-url.php
1 year ago
class-acf-field-user.php
1 year ago
class-acf-field-wysiwyg.php
1 year ago
class-acf-field.php
1 year ago
index.php
1 year ago
class-acf-field-date_picker.php
315 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 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', 'acf' ); |
| 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.', 'acf' ); |
| 25 | $this->preview_image = acf_get_url() . '/assets/images/field-type-previews/field-preview-date-picker.png'; |
| 26 | $this->doc_url = 'https://www.advancedcustomfields.com/resources/date-picker/'; |
| 27 | $this->defaults = array( |
| 28 | 'display_format' => 'd/m/Y', |
| 29 | 'return_format' => 'd/m/Y', |
| 30 | 'first_day' => 1, |
| 31 | ); |
| 32 | } |
| 33 | |
| 34 | |
| 35 | /** |
| 36 | * description |
| 37 | * |
| 38 | * @type function |
| 39 | * @date 16/12/2015 |
| 40 | * @since 5.3.2 |
| 41 | * |
| 42 | * @param $post_id (int) |
| 43 | * @return $post_id (int) |
| 44 | */ |
| 45 | function input_admin_enqueue_scripts() { |
| 46 | |
| 47 | // bail early if no enqueue |
| 48 | if ( ! acf_get_setting( 'enqueue_datepicker' ) ) { |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | // localize |
| 53 | global $wp_locale; |
| 54 | acf_localize_data( |
| 55 | array( |
| 56 | 'datePickerL10n' => array( |
| 57 | 'closeText' => _x( 'Done', 'Date Picker JS closeText', 'acf' ), |
| 58 | 'currentText' => _x( 'Today', 'Date Picker JS currentText', 'acf' ), |
| 59 | 'nextText' => _x( 'Next', 'Date Picker JS nextText', 'acf' ), |
| 60 | 'prevText' => _x( 'Prev', 'Date Picker JS prevText', 'acf' ), |
| 61 | 'weekHeader' => _x( 'Wk', 'Date Picker JS weekHeader', 'acf' ), |
| 62 | 'monthNames' => array_values( $wp_locale->month ), |
| 63 | 'monthNamesShort' => array_values( $wp_locale->month_abbrev ), |
| 64 | 'dayNames' => array_values( $wp_locale->weekday ), |
| 65 | 'dayNamesMin' => array_values( $wp_locale->weekday_initial ), |
| 66 | 'dayNamesShort' => array_values( $wp_locale->weekday_abbrev ), |
| 67 | ), |
| 68 | ) |
| 69 | ); |
| 70 | |
| 71 | // script |
| 72 | wp_enqueue_script( 'jquery-ui-datepicker' ); |
| 73 | |
| 74 | // style |
| 75 | wp_enqueue_style( 'acf-datepicker', acf_get_url( 'assets/inc/datepicker/jquery-ui.min.css' ), array(), '1.11.4' ); |
| 76 | } |
| 77 | |
| 78 | |
| 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 | |
| 90 | // vars |
| 91 | $hidden_value = ''; |
| 92 | $display_value = ''; |
| 93 | |
| 94 | // format value |
| 95 | if ( $field['value'] ) { |
| 96 | $hidden_value = acf_format_date( $field['value'], 'Ymd' ); |
| 97 | $display_value = acf_format_date( $field['value'], $field['display_format'] ); |
| 98 | } |
| 99 | |
| 100 | // elements |
| 101 | $div = array( |
| 102 | 'class' => 'acf-date-picker acf-input-wrap', |
| 103 | 'data-date_format' => acf_convert_date_to_js( $field['display_format'] ), |
| 104 | 'data-first_day' => $field['first_day'], |
| 105 | ); |
| 106 | $hidden_input = array( |
| 107 | 'id' => $field['id'], |
| 108 | 'name' => $field['name'], |
| 109 | 'value' => $hidden_value, |
| 110 | ); |
| 111 | $text_input = array( |
| 112 | 'class' => $field['class'] . ' input', |
| 113 | 'value' => $display_value, |
| 114 | ); |
| 115 | |
| 116 | // special attributes |
| 117 | foreach ( array( 'readonly', 'disabled' ) as $k ) { |
| 118 | if ( ! empty( $field[ $k ] ) ) { |
| 119 | $hidden_input[ $k ] = $k; |
| 120 | $text_input[ $k ] = $k; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | // save_format - compatibility with ACF < 5.0.0 |
| 125 | if ( ! empty( $field['save_format'] ) ) { |
| 126 | |
| 127 | // add custom JS save format |
| 128 | $div['data-save_format'] = $field['save_format']; |
| 129 | |
| 130 | // revert hidden input value to raw DB value |
| 131 | $hidden_input['value'] = $field['value']; |
| 132 | |
| 133 | // remove formatted value (will do this via JS) |
| 134 | $text_input['value'] = ''; |
| 135 | } |
| 136 | |
| 137 | // html |
| 138 | ?> |
| 139 | <div <?php echo acf_esc_attrs( $div ); ?>> |
| 140 | <?php acf_hidden_input( $hidden_input ); ?> |
| 141 | <?php acf_text_input( $text_input ); ?> |
| 142 | </div> |
| 143 | <?php |
| 144 | } |
| 145 | |
| 146 | |
| 147 | /** |
| 148 | * Create extra options for your field. This is rendered when editing a field. |
| 149 | * The value of $field['name'] can be used (like bellow) to save extra data to the $field |
| 150 | * |
| 151 | * @type action |
| 152 | * @since 3.6 |
| 153 | * @date 23/01/13 |
| 154 | * |
| 155 | * @param $field - an array holding all the field's data |
| 156 | */ |
| 157 | function render_field_settings( $field ) { |
| 158 | global $wp_locale; |
| 159 | |
| 160 | $d_m_Y = date_i18n( 'd/m/Y' ); |
| 161 | $m_d_Y = date_i18n( 'm/d/Y' ); |
| 162 | $F_j_Y = date_i18n( 'F j, Y' ); |
| 163 | $Ymd = date_i18n( 'Ymd' ); |
| 164 | |
| 165 | echo '<div class="acf-field-settings-split">'; |
| 166 | |
| 167 | acf_render_field_setting( |
| 168 | $field, |
| 169 | array( |
| 170 | 'label' => __( 'Display Format', 'acf' ), |
| 171 | 'hint' => __( 'The format displayed when editing a post', 'acf' ), |
| 172 | 'type' => 'radio', |
| 173 | 'name' => 'display_format', |
| 174 | 'other_choice' => 1, |
| 175 | 'choices' => array( |
| 176 | 'd/m/Y' => '<span>' . $d_m_Y . '</span><code>d/m/Y</code>', |
| 177 | 'm/d/Y' => '<span>' . $m_d_Y . '</span><code>m/d/Y</code>', |
| 178 | 'F j, Y' => '<span>' . $F_j_Y . '</span><code>F j, Y</code>', |
| 179 | 'other' => '<span>' . __( 'Custom:', 'acf' ) . '</span>', |
| 180 | ), |
| 181 | ) |
| 182 | ); |
| 183 | |
| 184 | // save_format - compatibility with ACF < 5.0.0 |
| 185 | if ( ! empty( $field['save_format'] ) ) { |
| 186 | acf_render_field_setting( |
| 187 | $field, |
| 188 | array( |
| 189 | 'label' => __( 'Save Format', 'acf' ), |
| 190 | 'hint' => __( 'The format used when saving a value', 'acf' ), |
| 191 | 'type' => 'text', |
| 192 | 'name' => 'save_format', |
| 193 | // 'readonly' => 1 // this setting was not readonly in v4 |
| 194 | ) |
| 195 | ); |
| 196 | } else { |
| 197 | acf_render_field_setting( |
| 198 | $field, |
| 199 | array( |
| 200 | 'label' => __( 'Return Format', 'acf' ), |
| 201 | 'hint' => __( 'The format returned via template functions', 'acf' ), |
| 202 | 'type' => 'radio', |
| 203 | 'name' => 'return_format', |
| 204 | 'other_choice' => 1, |
| 205 | 'choices' => array( |
| 206 | 'd/m/Y' => '<span>' . $d_m_Y . '</span><code>d/m/Y</code>', |
| 207 | 'm/d/Y' => '<span>' . $m_d_Y . '</span><code>m/d/Y</code>', |
| 208 | 'F j, Y' => '<span>' . $F_j_Y . '</span><code>F j, Y</code>', |
| 209 | 'Ymd' => '<span>' . $Ymd . '</span><code>Ymd</code>', |
| 210 | 'other' => '<span>' . __( 'Custom:', 'acf' ) . '</span>', |
| 211 | ), |
| 212 | ) |
| 213 | ); |
| 214 | } |
| 215 | |
| 216 | echo '</div>'; |
| 217 | |
| 218 | acf_render_field_setting( |
| 219 | $field, |
| 220 | array( |
| 221 | 'label' => __( 'Week Starts On', 'acf' ), |
| 222 | 'instructions' => '', |
| 223 | 'type' => 'select', |
| 224 | 'name' => 'first_day', |
| 225 | 'choices' => array_values( $wp_locale->weekday ), |
| 226 | ) |
| 227 | ); |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * This filter is appied to the $value after it is loaded from the db and before it is returned to the template |
| 232 | * |
| 233 | * @type filter |
| 234 | * @since 3.6 |
| 235 | * @date 23/01/13 |
| 236 | * |
| 237 | * @param $value (mixed) the value which was loaded from the database |
| 238 | * @param $post_id (mixed) the post_id from which the value was loaded |
| 239 | * @param $field (array) the field array holding all the field options |
| 240 | * |
| 241 | * @return $value (mixed) the modified value |
| 242 | */ |
| 243 | function format_value( $value, $post_id, $field ) { |
| 244 | |
| 245 | // save_format - compatibility with ACF < 5.0.0 |
| 246 | if ( ! empty( $field['save_format'] ) ) { |
| 247 | return $value; |
| 248 | } |
| 249 | |
| 250 | // return |
| 251 | return acf_format_date( $value, $field['return_format'] ); |
| 252 | } |
| 253 | |
| 254 | |
| 255 | /** |
| 256 | * This filter is applied to the $field after it is loaded from the database |
| 257 | * and ensures the return and display values are set. |
| 258 | * |
| 259 | * @type filter |
| 260 | * @since 5.11.0 |
| 261 | * @date 28/09/21 |
| 262 | * |
| 263 | * @param array $field The field array holding all the field options. |
| 264 | * @return array |
| 265 | */ |
| 266 | public function load_field( $field ) { |
| 267 | if ( empty( $field['display_format'] ) ) { |
| 268 | $field['display_format'] = $this->defaults['display_format']; |
| 269 | } |
| 270 | |
| 271 | if ( empty( $field['return_format'] ) ) { |
| 272 | $field['return_format'] = $this->defaults['return_format']; |
| 273 | } |
| 274 | |
| 275 | return $field; |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * Return the schema array for the REST API. |
| 280 | * |
| 281 | * @param array $field The field array |
| 282 | * @return array |
| 283 | */ |
| 284 | public function get_rest_schema( array $field ) { |
| 285 | return array( |
| 286 | 'type' => array( 'string', 'null' ), |
| 287 | 'description' => 'A `Ymd` formatted date string.', |
| 288 | 'required' => ! empty( $field['required'] ), |
| 289 | ); |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Apply basic formatting to prepare the value for default REST output. |
| 294 | * |
| 295 | * @param mixed $value |
| 296 | * @param string|integer $post_id |
| 297 | * @param array $field |
| 298 | * @return mixed |
| 299 | */ |
| 300 | public function format_value_for_rest( $value, $post_id, array $field ) { |
| 301 | if ( ! $value ) { |
| 302 | return null; |
| 303 | } |
| 304 | |
| 305 | return (string) $value; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | |
| 310 | // initialize |
| 311 | acf_register_field_type( 'acf_field_date_picker' ); |
| 312 | endif; // class_exists check |
| 313 | |
| 314 | ?> |
| 315 |