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
3 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
3 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
3 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_time_picker.php
328 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! class_exists( 'acf_field_date_and_time_picker' ) ) : |
| 4 | |
| 5 | class acf_field_date_and_time_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_time_picker'; |
| 22 | $this->label = __( 'Date Time Picker', 'secure-custom-fields' ); |
| 23 | $this->category = 'advanced'; |
| 24 | $this->description = __( 'An interactive UI for picking a date and time. 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-time.png'; |
| 26 | $this->doc_url = 'https://developer.wordpress.org/secure-custom-fields/features/fields/date-time-picker/'; |
| 27 | $this->tutorial_url = 'https://developer.wordpress.org/secure-custom-fields/features/fields/date-time-picker/date-time-picker-tutorial/'; |
| 28 | $this->defaults = array( |
| 29 | 'display_format' => 'd/m/Y g:i a', |
| 30 | 'return_format' => 'd/m/Y g:i a', |
| 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_datetimepicker' ) ) { |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | // vars |
| 55 | $version = '1.6.1'; |
| 56 | |
| 57 | // script |
| 58 | wp_enqueue_script( 'acf-timepicker', acf_get_url( 'assets/inc/timepicker/jquery-ui-timepicker-addon.min.js' ), array( 'jquery-ui-datepicker' ), $version ); |
| 59 | |
| 60 | // style |
| 61 | wp_enqueue_style( 'acf-timepicker', acf_get_url( 'assets/inc/timepicker/jquery-ui-timepicker-addon.min.css' ), '', $version ); |
| 62 | |
| 63 | // localize |
| 64 | acf_localize_data( |
| 65 | array( |
| 66 | 'dateTimePickerL10n' => array( |
| 67 | 'timeOnlyTitle' => _x( 'Choose Time', 'Date Time Picker JS timeOnlyTitle', 'secure-custom-fields' ), |
| 68 | 'timeText' => _x( 'Time', 'Date Time Picker JS timeText', 'secure-custom-fields' ), |
| 69 | 'hourText' => _x( 'Hour', 'Date Time Picker JS hourText', 'secure-custom-fields' ), |
| 70 | 'minuteText' => _x( 'Minute', 'Date Time Picker JS minuteText', 'secure-custom-fields' ), |
| 71 | 'secondText' => _x( 'Second', 'Date Time Picker JS secondText', 'secure-custom-fields' ), |
| 72 | 'millisecText' => _x( 'Millisecond', 'Date Time Picker JS millisecText', 'secure-custom-fields' ), |
| 73 | 'microsecText' => _x( 'Microsecond', 'Date Time Picker JS microsecText', 'secure-custom-fields' ), |
| 74 | 'timezoneText' => _x( 'Time Zone', 'Date Time Picker JS timezoneText', 'secure-custom-fields' ), |
| 75 | 'currentText' => _x( 'Now', 'Date Time Picker JS currentText', 'secure-custom-fields' ), |
| 76 | 'closeText' => _x( 'Done', 'Date Time Picker JS closeText', 'secure-custom-fields' ), |
| 77 | 'selectText' => _x( 'Select', 'Date Time Picker JS selectText', 'secure-custom-fields' ), |
| 78 | 'amNames' => array( |
| 79 | _x( 'AM', 'Date Time Picker JS amText', 'secure-custom-fields' ), |
| 80 | _x( 'A', 'Date Time Picker JS amTextShort', 'secure-custom-fields' ), |
| 81 | ), |
| 82 | 'pmNames' => array( |
| 83 | _x( 'PM', 'Date Time Picker JS pmText', 'secure-custom-fields' ), |
| 84 | _x( 'P', 'Date Time Picker JS pmTextShort', 'secure-custom-fields' ), |
| 85 | ), |
| 86 | ), |
| 87 | ) |
| 88 | ); |
| 89 | } |
| 90 | |
| 91 | |
| 92 | /** |
| 93 | * Create the HTML interface for your field |
| 94 | * |
| 95 | * @param $field - an array holding all the field's data |
| 96 | * |
| 97 | * @type action |
| 98 | * @since ACF 3.6 |
| 99 | * @date 23/01/13 |
| 100 | */ |
| 101 | function render_field( $field ) { |
| 102 | |
| 103 | // Set value. |
| 104 | $hidden_value = ''; |
| 105 | $display_value = ''; |
| 106 | |
| 107 | if ( $field['value'] ) { |
| 108 | $hidden_value = acf_format_date( $field['value'], 'Y-m-d H:i:s' ); |
| 109 | $display_value = acf_format_date( $field['value'], $field['display_format'] ); |
| 110 | } |
| 111 | |
| 112 | // Convert "display_format" setting to individual date and time formats. |
| 113 | $formats = acf_split_date_time( $field['display_format'] ); |
| 114 | |
| 115 | // Elements. |
| 116 | $div = array( |
| 117 | 'class' => 'acf-date-time-picker acf-input-wrap', |
| 118 | 'data-date_format' => acf_convert_date_to_js( $formats['date'] ), |
| 119 | 'data-time_format' => acf_convert_time_to_js( $formats['time'] ), |
| 120 | 'data-first_day' => $field['first_day'], |
| 121 | ); |
| 122 | $hidden_input = array( |
| 123 | 'id' => $field['id'], |
| 124 | 'class' => 'input-alt', |
| 125 | 'name' => $field['name'], |
| 126 | 'value' => $hidden_value, |
| 127 | ); |
| 128 | $text_input = array( |
| 129 | 'class' => $field['class'] . ' input', |
| 130 | 'value' => $display_value, |
| 131 | 'data-default-to-today' => $field['default_to_current_date'], |
| 132 | ); |
| 133 | foreach ( array( 'readonly', 'disabled' ) as $k ) { |
| 134 | if ( ! empty( $field[ $k ] ) ) { |
| 135 | $hidden_input[ $k ] = $k; |
| 136 | $text_input[ $k ] = $k; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | // Output. |
| 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 g:i a' ); |
| 164 | $m_d_Y = date_i18n( 'm/d/Y g:i a' ); |
| 165 | $F_j_Y = date_i18n( 'F j, Y g:i a' ); |
| 166 | $Ymd = date_i18n( 'Y-m-d H:i:s' ); |
| 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 g:i a' => '<span>' . $d_m_Y . '</span><code>d/m/Y g:i a</code>', |
| 180 | 'm/d/Y g:i a' => '<span>' . $m_d_Y . '</span><code>m/d/Y g:i a</code>', |
| 181 | 'F j, Y g:i a' => '<span>' . $F_j_Y . '</span><code>F j, Y g:i a</code>', |
| 182 | 'Y-m-d H:i:s' => '<span>' . $Ymd . '</span><code>Y-m-d H:i:s</code>', |
| 183 | 'other' => '<span>' . __( 'Custom:', 'secure-custom-fields' ) . '</span>', |
| 184 | ), |
| 185 | ) |
| 186 | ); |
| 187 | |
| 188 | acf_render_field_setting( |
| 189 | $field, |
| 190 | array( |
| 191 | 'label' => __( 'Return Format', 'secure-custom-fields' ), |
| 192 | 'hint' => __( 'The format returned via template functions', 'secure-custom-fields' ), |
| 193 | 'type' => 'radio', |
| 194 | 'name' => 'return_format', |
| 195 | 'other_choice' => 1, |
| 196 | 'choices' => array( |
| 197 | 'd/m/Y g:i a' => '<span>' . $d_m_Y . '</span><code>d/m/Y g:i a</code>', |
| 198 | 'm/d/Y g:i a' => '<span>' . $m_d_Y . '</span><code>m/d/Y g:i a</code>', |
| 199 | 'F j, Y g:i a' => '<span>' . $F_j_Y . '</span><code>F j, Y g:i a</code>', |
| 200 | 'Y-m-d H:i:s' => '<span>' . $Ymd . '</span><code>Y-m-d H:i:s</code>', |
| 201 | 'other' => '<span>' . __( 'Custom:', 'secure-custom-fields' ) . '</span>', |
| 202 | ), |
| 203 | ) |
| 204 | ); |
| 205 | |
| 206 | echo '</div>'; |
| 207 | |
| 208 | acf_render_field_setting( |
| 209 | $field, |
| 210 | array( |
| 211 | 'label' => __( 'Week Starts On', 'secure-custom-fields' ), |
| 212 | 'instructions' => '', |
| 213 | 'type' => 'select', |
| 214 | 'name' => 'first_day', |
| 215 | 'choices' => array_values( $wp_locale->weekday ), |
| 216 | ) |
| 217 | ); |
| 218 | |
| 219 | acf_render_field_setting( |
| 220 | $field, |
| 221 | array( |
| 222 | 'label' => __( 'Default to the current date', 'secure-custom-fields' ), |
| 223 | 'instructions' => __( 'Use the current date as the default value for this field.', 'secure-custom-fields' ), |
| 224 | 'type' => 'true_false', |
| 225 | 'name' => 'default_to_current_date', |
| 226 | 'ui' => 1, |
| 227 | ) |
| 228 | ); |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * This filter is applied to the $value after it is loaded from the db and before it is returned to the template |
| 233 | * |
| 234 | * @type filter |
| 235 | * @since ACF 3.6 |
| 236 | * |
| 237 | * @param mixed $value The value which was loaded from the database |
| 238 | * @param mixed $post_id The post_id from which the value was loaded |
| 239 | * @param array $field The field array holding all the field options |
| 240 | * @return mixed $value The modified value |
| 241 | */ |
| 242 | public function format_value( $value, $post_id, $field ) { |
| 243 | return acf_format_date( $value, $field['return_format'] ); |
| 244 | } |
| 245 | |
| 246 | |
| 247 | /** |
| 248 | * This filter is applied to the $field after it is loaded from the database |
| 249 | * and ensures the return and display values are set. |
| 250 | * |
| 251 | * @type filter |
| 252 | * @since ACF 5.11.0 |
| 253 | * |
| 254 | * @param array $field The field array holding all the field options. |
| 255 | * @return array |
| 256 | */ |
| 257 | public function load_field( $field ) { |
| 258 | if ( empty( $field['display_format'] ) ) { |
| 259 | $field['display_format'] = $this->defaults['display_format']; |
| 260 | } |
| 261 | |
| 262 | if ( empty( $field['return_format'] ) ) { |
| 263 | $field['return_format'] = $this->defaults['return_format']; |
| 264 | } |
| 265 | |
| 266 | return $field; |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Return the schema array for the REST API. |
| 271 | * |
| 272 | * @param array $field |
| 273 | * @return array |
| 274 | */ |
| 275 | public function get_rest_schema( array $field ) { |
| 276 | return array( |
| 277 | 'type' => array( 'string', 'null' ), |
| 278 | 'description' => 'A `Y-m-d H:i:s` formatted date string.', |
| 279 | 'required' => ! empty( $field['required'] ), |
| 280 | ); |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Returns an array of JSON-LD Property output types that are supported by this field type. |
| 285 | * |
| 286 | * @since 6.8 |
| 287 | * |
| 288 | * @return string[] |
| 289 | */ |
| 290 | public function get_jsonld_output_types(): array { |
| 291 | return array( 'DateTime' ); |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * Formats the field value for JSON-LD output. |
| 296 | * |
| 297 | * Converts the stored Y-m-d H:i:s format to ISO 8601 datetime format. |
| 298 | * |
| 299 | * @since 6.8.0 |
| 300 | * |
| 301 | * @param mixed $value The value of the field. |
| 302 | * @param integer|string $post_id The ID of the post. |
| 303 | * @param array $field The field array. |
| 304 | * @return string|null ISO 8601 formatted datetime or null. |
| 305 | */ |
| 306 | public function format_value_for_jsonld( $value, $post_id, $field ) { |
| 307 | if ( empty( $value ) || ! is_string( $value ) ) { |
| 308 | return null; |
| 309 | } |
| 310 | |
| 311 | // ACF stores date_time_picker internally as 'Y-m-d H:i:s'. |
| 312 | // Use WordPress site timezone so the ISO 8601 offset is correct. |
| 313 | $date = \DateTime::createFromFormat( 'Y-m-d H:i:s', $value, wp_timezone() ); |
| 314 | if ( ! $date ) { |
| 315 | return null; |
| 316 | } |
| 317 | |
| 318 | // Return ISO 8601 datetime format with timezone. |
| 319 | return $date->format( 'c' ); |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | // initialize |
| 324 | acf_register_field_type( 'acf_field_date_and_time_picker' ); |
| 325 | endif; // class_exists check |
| 326 | |
| 327 | ?> |
| 328 |