Complex classes like ZT_Debug_Bar_Cron 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 ZT_Debug_Bar_Cron, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class ZT_Debug_Bar_Cron extends Debug_Bar_Panel { |
||
28 | |||
29 | const DBCRON_STYLES_VERSION = '1.0'; |
||
30 | |||
31 | const DBCRON_NAME = 'debug-bar-cron'; |
||
32 | |||
33 | /** |
||
34 | * Holds all of the cron events. |
||
35 | * |
||
36 | * @var array |
||
37 | */ |
||
38 | private $_crons; |
||
39 | |||
40 | /** |
||
41 | * Holds only the cron events initiated by WP core. |
||
42 | * |
||
43 | * @var array |
||
44 | */ |
||
45 | private $_core_crons; |
||
46 | |||
47 | /** |
||
48 | * Holds the cron events created by plugins or themes. |
||
49 | * |
||
50 | * @var array |
||
51 | */ |
||
52 | private $_user_crons; |
||
53 | |||
54 | /** |
||
55 | * Total number of cron events. |
||
56 | * |
||
57 | * @var int |
||
58 | */ |
||
59 | private $_total_crons = 0; |
||
60 | |||
61 | /** |
||
62 | * Total number of cron events created by plugins and themes. |
||
63 | * |
||
64 | * @var int |
||
65 | */ |
||
66 | private $_total_user_crons = 0; |
||
67 | |||
68 | /** |
||
69 | * Total number of WP core cron events. |
||
70 | * |
||
71 | * @var int |
||
72 | */ |
||
73 | private $_total_core_crons = 0; |
||
74 | |||
75 | /** |
||
76 | * Whether cron is being executed or not. |
||
77 | * |
||
78 | * @var string |
||
79 | */ |
||
80 | private $_doing_cron = 'No'; |
||
81 | |||
82 | /** |
||
83 | * Lists all crons that are defined in WP Core. |
||
84 | * |
||
85 | * @var array |
||
86 | * |
||
87 | * @internal To find all, search WP trunk for `wp_schedule_(single_)?event`. |
||
88 | */ |
||
89 | private $core_cron_hooks = array( |
||
90 | 'do_pings', |
||
91 | 'importer_scheduled_cleanup', // WP 3.1+. |
||
92 | 'publish_future_post', |
||
93 | 'update_network_counts', // WP 3.1+. |
||
94 | 'upgrader_scheduled_cleanup', // WP 3.3+. |
||
95 | 'wp_maybe_auto_update', // WP 3.7+. |
||
96 | 'wp_scheduled_auto_draft_delete', // WP 3.4+. |
||
97 | 'wp_scheduled_delete', // WP 2.9+. |
||
98 | 'wp_split_shared_term_batch', // WP 4.3+. |
||
99 | 'wp_update_plugins', |
||
100 | 'wp_update_themes', |
||
101 | 'wp_version_check', |
||
102 | ); |
||
103 | |||
104 | |||
105 | |||
106 | /** |
||
107 | * Give the panel a title and set the enqueues. |
||
108 | * |
||
109 | * @return void |
||
110 | */ |
||
111 | public function init() { |
||
117 | |||
118 | |||
119 | /** |
||
120 | * Enqueue styles. |
||
121 | * |
||
122 | * @return void |
||
123 | */ |
||
124 | public function enqueue_scripts_styles() { |
||
134 | |||
135 | |||
136 | /** |
||
137 | * Show the menu item in Debug Bar. |
||
138 | * |
||
139 | * @return void |
||
140 | */ |
||
141 | public function prerender() { |
||
144 | |||
145 | |||
146 | /** |
||
147 | * Show the contents of the page. |
||
148 | * |
||
149 | * @return void |
||
150 | */ |
||
151 | public function render() { |
||
203 | |||
204 | |||
205 | /** |
||
206 | * Gets all of the cron jobs. |
||
207 | * |
||
208 | * @return array|null Array of crons. |
||
209 | */ |
||
210 | private function get_crons() { |
||
223 | |||
224 | |||
225 | /** |
||
226 | * Sort and count crons. |
||
227 | * |
||
228 | * This function sorts the cron jobs into core crons, and custom crons. It also tallies |
||
229 | * a total count for the crons as this number is otherwise tough to get. |
||
230 | */ |
||
231 | private function sort_count_crons() { |
||
246 | |||
247 | |||
248 | /** |
||
249 | * Displays the events in an easy to read table. |
||
250 | * |
||
251 | * @param array $events Array of events. |
||
252 | * @param string $no_events_msg Message to display if there are no events. |
||
253 | */ |
||
254 | private function display_events( $events, $no_events_msg ) { |
||
343 | |||
344 | |||
345 | /** |
||
346 | * Count the number of argument sets for a cron time. |
||
347 | * |
||
348 | * @param array $hook_array Array of hooks with argument sets. |
||
349 | * |
||
350 | * @return int |
||
351 | */ |
||
352 | private function get_arg_set_count( $hook_array ) { |
||
359 | |||
360 | |||
361 | /** |
||
362 | * Create a HTML attribute string for an event row. |
||
363 | * |
||
364 | * @param int $time Unix timestamp. |
||
365 | * @param string $hook Action hook for the cron job. |
||
366 | * |
||
367 | * @return string |
||
368 | */ |
||
369 | private function get_event_row_attributes( $time, $hook ) { |
||
391 | |||
392 | |||
393 | /** |
||
394 | * Displays the the event schedule name for recurring events or else 'single event'. |
||
395 | * |
||
396 | * @param array $info Event info array. |
||
397 | */ |
||
398 | private function display_event_schedule( $info ) { |
||
405 | |||
406 | |||
407 | /** |
||
408 | * Displays the event interval in seconds, minutes and hours. |
||
409 | * |
||
410 | * @param array $info Event info array. |
||
411 | */ |
||
412 | private function display_event_intervals( $info ) { |
||
426 | |||
427 | |||
428 | /** |
||
429 | * Displays the cron arguments in a readable format. |
||
430 | * |
||
431 | * @param mixed $args Cron argument(s). |
||
432 | * |
||
433 | * @return void |
||
434 | */ |
||
435 | private function display_event_cron_arguments( $args ) { |
||
445 | |||
446 | |||
447 | /** |
||
448 | * Displays all of the schedules defined. |
||
449 | * |
||
450 | * @return void |
||
451 | */ |
||
452 | private function display_schedules() { |
||
485 | |||
486 | /** |
||
487 | * Sorting method for cron scheldules. Order by schedules interval. |
||
488 | * |
||
489 | * @param array $a First element of comparison pair. |
||
490 | * @param array $b Second element of comparison pair. |
||
491 | * |
||
492 | * @return int Return 1 if $a argument 'interval' greater then $b argument 'interval', 0 if both intervals equivalent and -1 otherwise. |
||
493 | */ |
||
494 | function schedules_sorting( $a, $b ) { |
||
501 | |||
502 | /** |
||
503 | * Verify if a given timestamp is in the past or the future. |
||
504 | * |
||
505 | * @param int $time Unix timestamp. |
||
506 | * |
||
507 | * @return bool True if the time has passed, false otherwise. |
||
508 | */ |
||
509 | private function is_time_in_past( $time ) { |
||
512 | |||
513 | |||
514 | /** |
||
515 | * Transform a time in seconds to minutes rounded to 2 decimals. |
||
516 | * |
||
517 | * @param int $time Unix timestamp. |
||
518 | * |
||
519 | * @return int|float |
||
520 | */ |
||
521 | private function get_minutes( $time ) { |
||
524 | |||
525 | |||
526 | /** |
||
527 | * Transform a time in seconds to hours rounded to 2 decimals. |
||
528 | * |
||
529 | * @param int $time Unix timestamp. |
||
530 | * |
||
531 | * @return int|float |
||
532 | */ |
||
533 | private function get_hours( $time ) { |
||
536 | |||
537 | |||
538 | /** |
||
539 | * Compares time with current time and adds ' ago' if current time is greater than event time. |
||
540 | * |
||
541 | * @param string $human_time Human readable time difference. |
||
542 | * @param int $time Unix time of event. |
||
543 | * |
||
544 | * @return string |
||
545 | */ |
||
546 | private function display_past_time( $human_time, $time ) { |
||
554 | |||
555 | |||
556 | /** |
||
557 | * Load the pretty output class & set the recursion limit. |
||
558 | */ |
||
559 | private function load_debug_bar_pretty_output() { |
||
569 | |||
570 | |||
571 | /** |
||
572 | * Print any type of variable with colour coding and a variable type indication. |
||
573 | * |
||
574 | * @param mixed $variable The variable to print. |
||
575 | */ |
||
576 | private function print_pretty_output( $variable ) { |
||
585 | |||
586 | |||
587 | /** |
||
588 | * Unset recursion depth limit for the pretty output class. |
||
589 | * |
||
590 | * @internal Method available since DBPO v1.4. |
||
591 | */ |
||
592 | private function reset_debug_bar_pretty_output() { |
||
597 | } // End of class ZT_Debug_Bar_Cron. |
||
598 | |||
600 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.