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-time_picker.php
229 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! class_exists( 'acf_field_time_picker' ) ) : |
| 4 | |
| 5 | class acf_field_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 = 'time_picker'; |
| 22 | $this->label = __( 'Time Picker', 'secure-custom-fields' ); |
| 23 | $this->category = 'advanced'; |
| 24 | $this->description = __( 'An interactive UI for picking a time. The time 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-time.png'; |
| 26 | $this->doc_url = 'https://developer.wordpress.org/secure-custom-fields/features/fields/time-picker/'; |
| 27 | $this->tutorial_url = 'https://developer.wordpress.org/secure-custom-fields/features/fields/time-picker/time-picker-tutorial/'; |
| 28 | $this->defaults = array( |
| 29 | 'display_format' => 'g:i a', |
| 30 | 'return_format' => 'g:i a', |
| 31 | ); |
| 32 | } |
| 33 | |
| 34 | |
| 35 | /** |
| 36 | * Create the HTML interface for your field |
| 37 | * |
| 38 | * @param $field - an array holding all the field's data |
| 39 | * |
| 40 | * @type action |
| 41 | * @since ACF 3.6 |
| 42 | * @date 23/01/13 |
| 43 | */ |
| 44 | function render_field( $field ) { |
| 45 | |
| 46 | // Set value. |
| 47 | $display_value = ''; |
| 48 | |
| 49 | if ( $field['value'] ) { |
| 50 | $display_value = acf_format_date( $field['value'], $field['display_format'] ); |
| 51 | } |
| 52 | |
| 53 | // Elements. |
| 54 | $div = array( |
| 55 | 'class' => 'acf-time-picker acf-input-wrap', |
| 56 | 'data-time_format' => acf_convert_time_to_js( $field['display_format'] ), |
| 57 | ); |
| 58 | $hidden_input = array( |
| 59 | 'id' => $field['id'], |
| 60 | 'class' => 'input-alt', |
| 61 | 'type' => 'hidden', |
| 62 | 'name' => $field['name'], |
| 63 | 'value' => $field['value'], |
| 64 | ); |
| 65 | $text_input = array( |
| 66 | 'class' => $field['class'] . ' input', |
| 67 | 'type' => 'text', |
| 68 | 'value' => $display_value, |
| 69 | ); |
| 70 | foreach ( array( 'readonly', 'disabled' ) as $k ) { |
| 71 | if ( ! empty( $field[ $k ] ) ) { |
| 72 | $hidden_input[ $k ] = $k; |
| 73 | $text_input[ $k ] = $k; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // Output. |
| 78 | ?> |
| 79 | <div <?php echo acf_esc_attrs( $div ); ?>> |
| 80 | <?php acf_hidden_input( $hidden_input ); ?> |
| 81 | <?php acf_text_input( $text_input ); ?> |
| 82 | </div> |
| 83 | <?php |
| 84 | } |
| 85 | |
| 86 | |
| 87 | /** |
| 88 | * Create extra options for your field. This is rendered when editing a field. |
| 89 | * The value of $field['name'] can be used (like bellow) to save extra data to the $field |
| 90 | * |
| 91 | * @type action |
| 92 | * @since ACF 3.6 |
| 93 | * @date 23/01/13 |
| 94 | * |
| 95 | * @param $field - an array holding all the field's data |
| 96 | */ |
| 97 | function render_field_settings( $field ) { |
| 98 | $g_i_a = date_i18n( 'g:i a' ); |
| 99 | $H_i_s = date_i18n( 'H:i:s' ); |
| 100 | |
| 101 | echo '<div class="acf-field-settings-split">'; |
| 102 | |
| 103 | acf_render_field_setting( |
| 104 | $field, |
| 105 | array( |
| 106 | 'label' => __( 'Display Format', 'secure-custom-fields' ), |
| 107 | 'hint' => __( 'The format displayed when editing a post', 'secure-custom-fields' ), |
| 108 | 'type' => 'radio', |
| 109 | 'name' => 'display_format', |
| 110 | 'other_choice' => 1, |
| 111 | 'choices' => array( |
| 112 | 'g:i a' => '<span>' . $g_i_a . '</span><code>g:i a</code>', |
| 113 | 'H:i:s' => '<span>' . $H_i_s . '</span><code>H:i:s</code>', |
| 114 | 'other' => '<span>' . __( 'Custom:', 'secure-custom-fields' ) . '</span>', |
| 115 | ), |
| 116 | ) |
| 117 | ); |
| 118 | |
| 119 | acf_render_field_setting( |
| 120 | $field, |
| 121 | array( |
| 122 | 'label' => __( 'Return Format', 'secure-custom-fields' ), |
| 123 | 'hint' => __( 'The format returned via template functions', 'secure-custom-fields' ), |
| 124 | 'type' => 'radio', |
| 125 | 'name' => 'return_format', |
| 126 | 'other_choice' => 1, |
| 127 | 'choices' => array( |
| 128 | 'g:i a' => '<span>' . $g_i_a . '</span><code>g:i a</code>', |
| 129 | 'H:i:s' => '<span>' . $H_i_s . '</span><code>H:i:s</code>', |
| 130 | 'other' => '<span>' . __( 'Custom:', 'secure-custom-fields' ) . '</span>', |
| 131 | ), |
| 132 | ) |
| 133 | ); |
| 134 | |
| 135 | echo '</div>'; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * This filter is applied to the $value after it is loaded from the db and before it is returned to the template |
| 140 | * |
| 141 | * @type filter |
| 142 | * @since ACF 3.6 |
| 143 | * @date 23/01/13 |
| 144 | * |
| 145 | * @param $value (mixed) the value which was loaded from the database |
| 146 | * @param $post_id (mixed) the post_id from which the value was loaded |
| 147 | * @param $field (array) the field array holding all the field options |
| 148 | * @return $value (mixed) the modified value |
| 149 | */ |
| 150 | public function format_value( $value, $post_id, $field ) { |
| 151 | return acf_format_date( $value, $field['return_format'] ); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * This filter is applied to the $field after it is loaded from the database |
| 156 | * and ensures the return and display values are set. |
| 157 | * |
| 158 | * @type filter |
| 159 | * @since ACF 5.11.0 |
| 160 | * |
| 161 | * @param array $field The field array holding all the field options. |
| 162 | * @return array |
| 163 | */ |
| 164 | public function load_field( $field ) { |
| 165 | if ( empty( $field['display_format'] ) ) { |
| 166 | $field['display_format'] = $this->defaults['display_format']; |
| 167 | } |
| 168 | |
| 169 | if ( empty( $field['return_format'] ) ) { |
| 170 | $field['return_format'] = $this->defaults['return_format']; |
| 171 | } |
| 172 | |
| 173 | return $field; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Return the schema array for the REST API. |
| 178 | * |
| 179 | * @param array $field The field array. |
| 180 | * @return array |
| 181 | */ |
| 182 | public function get_rest_schema( array $field ) { |
| 183 | return array( |
| 184 | 'type' => array( 'string', 'null' ), |
| 185 | 'description' => 'A `H:i:s` formatted time string.', |
| 186 | 'required' => ! empty( $field['required'] ), |
| 187 | ); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Returns an array of JSON-LD Property output types that are supported by this field type. |
| 192 | * |
| 193 | * @since 6.8 |
| 194 | * |
| 195 | * @return string[] |
| 196 | */ |
| 197 | public function get_jsonld_output_types(): array { |
| 198 | return array( 'Time' ); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Formats the field value for JSON-LD output. |
| 203 | * |
| 204 | * Returns the stored H:i:s format which is already ISO 8601 compliant. |
| 205 | * |
| 206 | * @since 6.8.0 |
| 207 | * |
| 208 | * @param mixed $value The value of the field. |
| 209 | * @param integer|string $post_id The ID of the post. |
| 210 | * @param array $field The field array. |
| 211 | * @return string|null ISO 8601 formatted time or null. |
| 212 | */ |
| 213 | public function format_value_for_jsonld( $value, $post_id, $field ) { |
| 214 | if ( empty( $value ) || ! is_string( $value ) ) { |
| 215 | return null; |
| 216 | } |
| 217 | |
| 218 | // ACF stores time_picker internally as 'H:i:s' which is ISO 8601 compliant. |
| 219 | return $value; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | |
| 224 | // initialize |
| 225 | acf_register_field_type( 'acf_field_time_picker' ); |
| 226 | endif; // class_exists check |
| 227 | |
| 228 | ?> |
| 229 |