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 | * Give the panel a title and set the enqueues. |
||
107 | * |
||
108 | * @return void |
||
109 | */ |
||
110 | public function init() { |
||
119 | |||
120 | |||
121 | /** |
||
122 | * Enqueue styles. |
||
123 | * |
||
124 | * @return void |
||
125 | */ |
||
126 | public function enqueue_scripts_styles() { |
||
136 | |||
137 | |||
138 | /** |
||
139 | * Show the menu item in Debug Bar. |
||
140 | * |
||
141 | * @return void |
||
142 | */ |
||
143 | public function prerender() { |
||
146 | |||
147 | |||
148 | /** |
||
149 | * Show the contents of the page. |
||
150 | * |
||
151 | * @return void |
||
152 | */ |
||
153 | public function render() { |
||
205 | |||
206 | |||
207 | /** |
||
208 | * Gets all of the cron jobs. |
||
209 | * |
||
210 | * @return array|null Array of crons. |
||
211 | */ |
||
212 | private function get_crons() { |
||
225 | |||
226 | |||
227 | /** |
||
228 | * Sort and count crons. |
||
229 | * |
||
230 | * This function sorts the cron jobs into core crons, and custom crons. It also tallies |
||
231 | * a total count for the crons as this number is otherwise tough to get. |
||
232 | */ |
||
233 | private function sort_count_crons() { |
||
248 | |||
249 | |||
250 | /** |
||
251 | * Displays the events in an easy to read table. |
||
252 | * |
||
253 | * @param array $events Array of events. |
||
254 | * @param string $no_events_msg Message to display if there are no events. |
||
255 | */ |
||
256 | private function display_events( $events, $no_events_msg ) { |
||
329 | |||
330 | |||
331 | /** |
||
332 | * Count the number of argument sets for a cron time. |
||
333 | * |
||
334 | * @param array $hook_array Array of hooks with argument sets. |
||
335 | * |
||
336 | * @return int |
||
337 | */ |
||
338 | private function get_arg_set_count( $hook_array ) { |
||
345 | |||
346 | |||
347 | /** |
||
348 | * Create a HTML attribute string for an event row. |
||
349 | * |
||
350 | * @param int $time Unix timestamp. |
||
351 | * @param string $hook Action hook for the cron job. |
||
352 | * |
||
353 | * @return string |
||
354 | */ |
||
355 | private function get_event_row_attributes( $time, $hook ) { |
||
377 | |||
378 | |||
379 | /** |
||
380 | * Display the timing for the event as a date, timestamp and human readable time difference. |
||
381 | * |
||
382 | * @param int $time Timestamp. |
||
383 | * @param int $event_count Number of events running at this time. |
||
384 | */ |
||
385 | private function display_event_time( $time, $event_count ) { |
||
396 | |||
397 | |||
398 | /** |
||
399 | * Display the name of the cron job event hook. |
||
400 | * |
||
401 | * @param string $hook Hook name. |
||
402 | * @param int $arg_set_count Number of events running at this time and on this hook. |
||
403 | */ |
||
404 | private function display_event_hook( $hook, $arg_set_count ) { |
||
411 | |||
412 | |||
413 | /** |
||
414 | * Displays the the event schedule name for recurring events or else 'single event'. |
||
415 | * |
||
416 | * @param array $info Event info array. |
||
417 | */ |
||
418 | private function display_event_schedule( $info ) { |
||
425 | |||
426 | |||
427 | /** |
||
428 | * Displays the event interval in seconds, minutes and hours. |
||
429 | * |
||
430 | * @param array $info Event info array. |
||
431 | */ |
||
432 | private function display_event_intervals( $info ) { |
||
446 | |||
447 | |||
448 | /** |
||
449 | * Displays the cron arguments in a readable format. |
||
450 | * |
||
451 | * @param mixed $args Cron argument(s). |
||
452 | * |
||
453 | * @return void |
||
454 | */ |
||
455 | private function display_event_cron_arguments( $args ) { |
||
465 | |||
466 | |||
467 | /** |
||
468 | * Displays all of the schedules defined. |
||
469 | * |
||
470 | * @return void |
||
471 | */ |
||
472 | private function display_schedules() { |
||
504 | |||
505 | /** |
||
506 | * Sorting method for cron schedules. Order by schedules interval. |
||
507 | * |
||
508 | * @param array $schedule_a First element of comparison pair. |
||
509 | * @param array $schedule_b Second element of comparison pair. |
||
510 | * |
||
511 | * @return int Return 1 if $schedule_a argument 'interval' greater then $schedule_b argument 'interval', |
||
512 | * 0 if both intervals equivalent and -1 otherwise. |
||
513 | */ |
||
514 | private function schedules_sorting( $schedule_a, $schedule_b ) { |
||
521 | |||
522 | /** |
||
523 | * Verify if a given timestamp is in the past or the future. |
||
524 | * |
||
525 | * @param int $time Unix timestamp. |
||
526 | * |
||
527 | * @return bool True if the time has passed, false otherwise. |
||
528 | */ |
||
529 | private function is_time_in_past( $time ) { |
||
532 | |||
533 | |||
534 | /** |
||
535 | * Transform a time in seconds to minutes rounded to 2 decimals. |
||
536 | * |
||
537 | * @param int $time Unix timestamp. |
||
538 | * |
||
539 | * @return float |
||
540 | */ |
||
541 | private function get_minutes( $time ) { |
||
544 | |||
545 | |||
546 | /** |
||
547 | * Transform a time in seconds to hours rounded to 2 decimals. |
||
548 | * |
||
549 | * @param int $time Unix timestamp. |
||
550 | * |
||
551 | * @return float |
||
552 | */ |
||
553 | private function get_hours( $time ) { |
||
556 | |||
557 | |||
558 | /** |
||
559 | * Compares time with current time and adds ' ago' if current time is greater than event time. |
||
560 | * |
||
561 | * @param string $human_time Human readable time difference. |
||
562 | * @param int $time Unix time of event. |
||
563 | * |
||
564 | * @return string |
||
565 | */ |
||
566 | private function display_past_time( $human_time, $time ) { |
||
574 | |||
575 | |||
576 | /** |
||
577 | * Load the pretty output class & set the recursion limit. |
||
578 | */ |
||
579 | private function load_debug_bar_pretty_output() { |
||
589 | |||
590 | |||
591 | /** |
||
592 | * Print any type of variable with colour coding and a variable type indication. |
||
593 | * |
||
594 | * @param mixed $variable The variable to print. |
||
595 | */ |
||
596 | private function print_pretty_output( $variable ) { |
||
605 | |||
606 | |||
607 | /** |
||
608 | * Unset recursion depth limit for the pretty output class. |
||
609 | * |
||
610 | * @internal Method available since DBPO v1.4. |
||
611 | */ |
||
612 | private function reset_debug_bar_pretty_output() { |
||
617 | } // End of class ZT_Debug_Bar_Cron. |
||
618 | |||
620 |