Complex classes like PodsField_DateTime often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PodsField_DateTime, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
6 | class PodsField_DateTime extends PodsField { |
||
7 | |||
8 | /** |
||
9 | * {@inheritdoc} |
||
10 | */ |
||
11 | public static $group = 'Date / Time'; |
||
12 | |||
13 | /** |
||
14 | * {@inheritdoc} |
||
15 | */ |
||
16 | public static $type = 'datetime'; |
||
17 | |||
18 | /** |
||
19 | * {@inheritdoc} |
||
20 | */ |
||
21 | public static $label = 'Date / Time'; |
||
22 | |||
23 | /** |
||
24 | * {@inheritdoc} |
||
25 | */ |
||
26 | public static $prepare = '%s'; |
||
27 | |||
28 | /** |
||
29 | * Storage format. |
||
30 | * |
||
31 | * @var string |
||
32 | * @since 2.7 |
||
33 | */ |
||
34 | public static $storage_format = 'Y-m-d H:i:s'; |
||
35 | |||
36 | /** |
||
37 | * The default empty value (database) |
||
38 | * |
||
39 | * @var string |
||
40 | * @since 2.7 |
||
41 | */ |
||
42 | public static $empty_value = '0000-00-00 00:00:00'; |
||
43 | |||
44 | /** |
||
45 | * {@inheritdoc} |
||
46 | */ |
||
47 | public function setup() { |
||
48 | |||
49 | static::$label = __( 'Date / Time', 'pods' ); |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * {@inheritdoc} |
||
54 | */ |
||
55 | public function options() { |
||
56 | |||
57 | $options = array( |
||
58 | static::$type . '_repeatable' => array( |
||
59 | 'label' => __( 'Repeatable Field', 'pods' ), |
||
60 | 'default' => 0, |
||
61 | 'type' => 'boolean', |
||
62 | 'help' => __( 'Making a field repeatable will add controls next to the field which allows users to Add/Remove/Reorder additional values. These values are saved in the database as an array, so searching and filtering by them may require further adjustments".', 'pods' ), |
||
63 | 'boolean_yes_label' => '', |
||
64 | 'dependency' => true, |
||
65 | 'developer_mode' => true, |
||
66 | ), |
||
67 | static::$type . '_type' => array( |
||
68 | 'label' => __( 'Date Format Type', 'pods' ), |
||
69 | 'default' => 'format', |
||
70 | // Backwards compatibility |
||
71 | 'type' => 'pick', |
||
72 | 'help' => __( 'WordPress Default is the format used in Settings, General under "Date Format".', 'pods' ) . '<br>' . __( 'Predefined Format will allow you to select from a list of commonly used date formats.', 'pods' ) . '<br>' . __( 'Custom will allow you to enter your own using PHP Date/Time Strings.', 'pods' ), |
||
73 | 'data' => array( |
||
74 | 'wp' => __( 'WordPress default', 'pods' ) . ': ' . date_i18n( get_option( 'date_format' ) ), |
||
75 | 'format' => __( 'Predefined format', 'pods' ), |
||
76 | 'custom' => __( 'Custom format', 'pods' ), |
||
77 | ), |
||
78 | 'dependency' => true, |
||
79 | ), |
||
80 | static::$type . '_format_custom' => array( |
||
81 | 'label' => __( 'Date format for display', 'pods' ), |
||
82 | 'depends-on' => array( static::$type . '_type' => 'custom' ), |
||
83 | 'default' => '', |
||
84 | 'type' => 'text', |
||
85 | 'help' => sprintf( |
||
86 | '<a href="http://php.net/manual/function.date.php" target="_blank">%s</a>', |
||
87 | esc_html__( 'PHP date documentation', 'pods' ) |
||
88 | ), |
||
89 | ), |
||
90 | static::$type . '_format_custom_js' => array( |
||
91 | 'label' => __( 'Date format for input', 'pods' ), |
||
92 | 'depends-on' => array( static::$type . '_type' => 'custom' ), |
||
93 | 'default' => '', |
||
94 | 'type' => 'text', |
||
95 | 'help' => sprintf( |
||
96 | '<a href="https://api.jqueryui.com/datepicker/" target="_blank">%1$s</a><br />%2$s', |
||
97 | esc_html__( 'jQuery UI datepicker documentation', 'pods' ), |
||
98 | esc_html__( 'Leave empty to auto-generate from PHP format.', 'pods' ) |
||
99 | ), |
||
100 | ), |
||
101 | static::$type . '_format' => array( |
||
102 | 'label' => __( 'Date Format', 'pods' ), |
||
103 | 'depends-on' => array( static::$type . '_type' => 'format' ), |
||
104 | 'default' => 'mdy', |
||
105 | 'type' => 'pick', |
||
106 | 'data' => array( |
||
107 | 'mdy' => date_i18n( 'm/d/Y' ), |
||
108 | 'mdy_dash' => date_i18n( 'm-d-Y' ), |
||
109 | 'mdy_dot' => date_i18n( 'm.d.Y' ), |
||
110 | 'ymd_slash' => date_i18n( 'Y/m/d' ), |
||
111 | 'ymd_dash' => date_i18n( 'Y-m-d' ), |
||
112 | 'ymd_dot' => date_i18n( 'Y.m.d' ), |
||
113 | 'fjy' => date_i18n( 'F j, Y' ), |
||
114 | 'fjsy' => date_i18n( 'F jS, Y' ), |
||
115 | 'c' => date_i18n( 'c' ), |
||
116 | ), |
||
117 | 'dependency' => true, |
||
118 | ), |
||
119 | static::$type . '_time_type' => array( |
||
120 | 'label' => __( 'Time Format Type', 'pods' ), |
||
121 | 'excludes-on' => array( static::$type . '_format' => 'c' ), |
||
122 | 'default' => '12', |
||
123 | // Backwards compatibility |
||
124 | 'type' => 'pick', |
||
125 | 'help' => __( 'WordPress Default is the format used in Settings, General under "Time Format".', 'pods' ) . '<br>' . __( '12/24 hour will allow you to select from a list of commonly used time formats.', 'pods' ) . '<br>' . __( 'Custom will allow you to enter your own using PHP Date/Time Strings.', 'pods' ), |
||
126 | 'data' => array( |
||
127 | 'wp' => __( 'WordPress default', 'pods' ) . ': ' . date_i18n( get_option( 'time_format' ) ), |
||
128 | '12' => __( '12 hour', 'pods' ), |
||
129 | '24' => __( '24 hour', 'pods' ), |
||
130 | 'custom' => __( 'Custom', 'pods' ), |
||
131 | ), |
||
132 | 'dependency' => true, |
||
133 | ), |
||
134 | static::$type . '_time_format_custom' => array( |
||
135 | 'label' => __( 'Time format', 'pods' ), |
||
136 | 'depends-on' => array( static::$type . '_time_type' => 'custom' ), |
||
137 | 'excludes-on' => array( static::$type . '_format' => 'c' ), |
||
138 | 'default' => '', |
||
139 | 'type' => 'text', |
||
140 | 'help' => '<a href="http://php.net/manual/function.date.php" target="_blank">' . __( 'PHP date documentation', 'pods' ) . '</a>', |
||
141 | ), |
||
142 | static::$type . '_time_format_custom_js' => array( |
||
143 | 'label' => __( 'Time format field input', 'pods' ), |
||
144 | 'depends-on' => array( static::$type . '_time_type' => 'custom' ), |
||
145 | 'excludes-on' => array( static::$type . '_format' => 'c' ), |
||
146 | 'default' => '', |
||
147 | 'type' => 'text', |
||
148 | 'help' => sprintf( |
||
149 | '<a href="http://trentrichardson.com/examples/timepicker/#tp-formatting" target="_blank">%1$s</a><br />%2$s', |
||
150 | esc_html__( 'jQuery UI timepicker documentation', 'pods' ), |
||
151 | esc_html__( 'Leave empty to auto-generate from PHP format.', 'pods' ) |
||
152 | ), |
||
153 | ), |
||
154 | static::$type . '_time_format' => array( |
||
155 | 'label' => __( 'Time Format', 'pods' ), |
||
156 | 'depends-on' => array( static::$type . '_time_type' => '12' ), |
||
157 | 'excludes-on' => array( static::$type . '_format' => 'c' ), |
||
158 | 'default' => 'h_mma', |
||
159 | 'type' => 'pick', |
||
160 | 'data' => array( |
||
161 | 'h_mm_A' => date_i18n( 'g:i A' ), |
||
162 | 'h_mm_ss_A' => date_i18n( 'g:i:s A' ), |
||
163 | 'hh_mm_A' => date_i18n( 'h:i A' ), |
||
164 | 'hh_mm_ss_A' => date_i18n( 'h:i:s A' ), |
||
165 | 'h_mma' => date_i18n( 'g:ia' ), |
||
166 | 'hh_mma' => date_i18n( 'h:ia' ), |
||
167 | 'h_mm' => date_i18n( 'g:i' ), |
||
168 | 'h_mm_ss' => date_i18n( 'g:i:s' ), |
||
169 | 'hh_mm' => date_i18n( 'h:i' ), |
||
170 | 'hh_mm_ss' => date_i18n( 'h:i:s' ), |
||
171 | ), |
||
172 | ), |
||
173 | static::$type . '_time_format_24' => array( |
||
174 | 'label' => __( 'Time Format', 'pods' ), |
||
175 | 'depends-on' => array( static::$type . '_time_type' => '24' ), |
||
176 | 'excludes-on' => array( static::$type . '_format' => 'c' ), |
||
177 | 'default' => 'hh_mm', |
||
178 | 'type' => 'pick', |
||
179 | 'data' => array( |
||
180 | 'hh_mm' => date_i18n( 'H:i' ), |
||
181 | 'hh_mm_ss' => date_i18n( 'H:i:s' ), |
||
182 | ), |
||
183 | ), |
||
184 | static::$type . '_allow_empty' => array( |
||
185 | 'label' => __( 'Allow empty value?', 'pods' ), |
||
186 | 'default' => 1, |
||
187 | 'type' => 'boolean', |
||
188 | ), |
||
189 | static::$type . '_html5' => array( |
||
190 | 'label' => __( 'Enable HTML5 Input Field?', 'pods' ), |
||
191 | 'default' => apply_filters( 'pods_form_ui_field_html5', 0, static::$type ), |
||
192 | 'type' => 'boolean', |
||
193 | ), |
||
194 | ); |
||
195 | |||
196 | // Check if PHP DateTime::createFromFormat exists for additional supported formats |
||
197 | if ( method_exists( 'DateTime', 'createFromFormat' ) || apply_filters( 'pods_form_ui_field_datetime_custom_formatter', false ) ) { |
||
198 | $options[ static::$type . '_format' ]['data'] = array_merge( |
||
199 | $options[ static::$type . '_format' ]['data'], array( |
||
200 | 'dmy' => date_i18n( 'd/m/Y' ), |
||
201 | 'dmy_dash' => date_i18n( 'd-m-Y' ), |
||
202 | 'dmy_dot' => date_i18n( 'd.m.Y' ), |
||
203 | 'dMy' => date_i18n( 'd/M/Y' ), |
||
204 | 'dMy_dash' => date_i18n( 'd-M-Y' ), |
||
205 | ) |
||
206 | ); |
||
207 | } |
||
208 | |||
209 | $options[ static::$type . '_format' ]['data'] = apply_filters( 'pods_form_ui_field_date_format_options', $options[ static::$type . '_format' ]['data'] ); |
||
210 | $options[ static::$type . '_format' ]['default'] = apply_filters( 'pods_form_ui_field_date_format_default', $options[ static::$type . '_format' ]['default'] ); |
||
211 | |||
212 | $options[ static::$type . '_time_type' ]['default'] = apply_filters( 'pods_form_ui_field_time_format_type_default', $options[ static::$type . '_time_type' ]['default'] ); |
||
213 | $options[ static::$type . '_time_format' ]['data'] = apply_filters( 'pods_form_ui_field_time_format_options', $options[ static::$type . '_time_format' ]['data'] ); |
||
214 | $options[ static::$type . '_time_format' ]['default'] = apply_filters( 'pods_form_ui_field_time_format_default', $options[ static::$type . '_time_format' ]['default'] ); |
||
215 | $options[ static::$type . '_time_format_24' ]['data'] = apply_filters( 'pods_form_ui_field_time_format_24_options', $options[ static::$type . '_time_format_24' ]['data'] ); |
||
216 | $options[ static::$type . '_time_format_24' ]['default'] = apply_filters( 'pods_form_ui_field_time_format_24_default', $options[ static::$type . '_time_format_24' ]['default'] ); |
||
217 | |||
218 | return $options; |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * {@inheritdoc} |
||
223 | */ |
||
224 | public function schema( $options = null ) { |
||
230 | |||
231 | /** |
||
232 | * {@inheritdoc} |
||
233 | */ |
||
234 | public function is_empty( $value = null ) { |
||
247 | |||
248 | /** |
||
249 | * {@inheritdoc} |
||
250 | */ |
||
251 | public function display( $value = null, $name = null, $options = null, $pod = null, $id = null ) { |
||
257 | |||
258 | /** |
||
259 | * {@inheritdoc} |
||
260 | */ |
||
261 | public function input( $name, $value = null, $options = null, $pod = null, $id = null ) { |
||
291 | |||
292 | /** |
||
293 | * {@inheritdoc} |
||
294 | */ |
||
295 | public function validate( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) { |
||
328 | |||
329 | /** |
||
330 | * {@inheritdoc} |
||
331 | */ |
||
332 | public function pre_save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) { |
||
333 | |||
334 | $js = true; |
||
335 | if ( 'custom' !== pods_v( static::$type . '_type', $options, 'format' ) ) { |
||
336 | $js = false; |
||
337 | } |
||
338 | $format = $this->format_datetime( $options, $js ); |
||
339 | if ( $js ) { |
||
340 | $format = $this->convert_format( $format, array( 'source' => 'jquery_ui' ) ); |
||
341 | } |
||
342 | |||
343 | if ( ! empty( $value ) && ( 0 === (int) pods_v( static::$type . '_allow_empty', $options, 1 ) || ! in_array( |
||
344 | $value, array( |
||
345 | '0000-00-00', |
||
346 | '0000-00-00 00:00:00', |
||
347 | '00:00:00', |
||
348 | ), true |
||
349 | ) ) ) { |
||
350 | $value = $this->convert_date( $value, static::$storage_format, $format ); |
||
351 | } elseif ( 1 === (int) pods_v( static::$type . '_allow_empty', $options, 1 ) ) { |
||
352 | $value = static::$empty_value; |
||
353 | } else { |
||
354 | $value = date_i18n( static::$storage_format ); |
||
355 | } |
||
356 | |||
357 | return $value; |
||
358 | } |
||
359 | |||
360 | /** |
||
361 | * {@inheritdoc} |
||
362 | */ |
||
363 | public function ui( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) { |
||
379 | |||
380 | /** |
||
381 | * Convert value to the correct format for display. |
||
382 | * |
||
383 | * @param string $value Field value. |
||
384 | * @param array $options Field options. |
||
385 | * @param bool $js Return formatted from jQuery UI format? (only for custom formats). |
||
386 | * |
||
387 | * @return string |
||
388 | * @since 2.7 |
||
389 | */ |
||
390 | public function format_value_display( $value, $options, $js = false ) { |
||
422 | |||
423 | /** |
||
424 | * Build date and/or time format string based on options |
||
425 | * |
||
426 | * @since 2.7 |
||
427 | * |
||
428 | * @param array $options Field options. |
||
429 | * @param bool $js Whether to return format for jQuery UI. |
||
430 | * |
||
431 | * @return string |
||
432 | */ |
||
433 | public function format_datetime( $options, $js = false ) { |
||
445 | |||
446 | /** |
||
447 | * Build date format string based on options |
||
448 | * |
||
449 | * @since 2.7 |
||
450 | * |
||
451 | * @param array $options Field options. |
||
452 | * @param bool $js Whether to return format for jQuery UI. |
||
453 | * |
||
454 | * @return string |
||
455 | */ |
||
456 | public function format_date( $options, $js = false ) { |
||
484 | |||
485 | /** |
||
486 | * Build time format string based on options |
||
487 | * |
||
488 | * @since 2.7 |
||
489 | * |
||
490 | * @param array $options Field options. |
||
491 | * @param bool $js Whether to return format for jQuery UI. |
||
492 | * |
||
493 | * @return string |
||
494 | */ |
||
495 | public function format_time( $options, $js = false ) { |
||
535 | |||
536 | /** |
||
537 | * Get the date formats. |
||
538 | * |
||
539 | * @since 2.7 |
||
540 | * |
||
541 | * @param bool $js Whether to return format for jQuery UI. |
||
542 | * |
||
543 | * @return array |
||
544 | */ |
||
545 | public function get_date_formats( $js = false ) { |
||
575 | |||
576 | /** |
||
577 | * Get the time formats. |
||
578 | * |
||
579 | * @since 2.7 |
||
580 | * |
||
581 | * @param bool $js Whether to return format for jQuery UI. |
||
582 | * |
||
583 | * @return array |
||
584 | */ |
||
585 | public function get_time_formats( $js = false ) { |
||
611 | |||
612 | /** |
||
613 | * Get the time formats. |
||
614 | * |
||
615 | * @since 2.7 |
||
616 | * |
||
617 | * @param bool $js Whether to return format for jQuery UI. |
||
618 | * |
||
619 | * @return array |
||
620 | */ |
||
621 | public function get_time_formats_24( $js = false ) { |
||
639 | |||
640 | /** |
||
641 | * PHP backwards compatibility for createFromFormat. |
||
642 | * |
||
643 | * @param string $format Format string. |
||
644 | * @param string $date Defaults to time() if empty. |
||
645 | * @param boolean $return_timestamp Whether to return the strtotime() or createFromFormat result or not. |
||
646 | * |
||
647 | * @return DateTime|null|int|false |
||
648 | */ |
||
649 | public function createFromFormat( $format, $date, $return_timestamp = false ) { |
||
697 | |||
698 | /** |
||
699 | * Convert a date from one format to another. |
||
700 | * |
||
701 | * @param string $value Field value. |
||
702 | * @param string $new_format New format string. |
||
703 | * @param string $original_format Original format string (if known). |
||
704 | * @param boolean $return_timestamp Whether to return the strtotime() or createFromFormat result or not. |
||
705 | * |
||
706 | * @return string|int|boolean|DateTime |
||
707 | */ |
||
708 | public function convert_date( $value, $new_format, $original_format = '', $return_timestamp = false ) { |
||
737 | |||
738 | /** |
||
739 | * Matches each symbol of PHP date format standard with jQuery equivalent codeword. |
||
740 | * |
||
741 | * @link http://stackoverflow.com/questions/16702398/convert-a-php-date-format-to-a-jqueryui-datepicker-date-format |
||
742 | * @link https://api.jqueryui.com/datepicker/ |
||
743 | * @link http://trentrichardson.com/examples/timepicker/ |
||
744 | * |
||
745 | * @since 2.7 |
||
746 | * |
||
747 | * @param string $source_format Source format string. |
||
748 | * @param array $args Format arguments. |
||
749 | * |
||
750 | * @return string |
||
751 | */ |
||
752 | public function convert_format( $source_format, $args = array() ) { |
||
862 | |||
863 | /** |
||
864 | * Enqueue the i18n files for jquery date/timepicker |
||
865 | * |
||
866 | * @since 2.7 |
||
867 | */ |
||
868 | public function enqueue_jquery_ui_i18n() { |
||
914 | } |
||
915 |
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.