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 | * Load the plugin text strings. |
||
| 121 | * |
||
| 122 | * Compatible with use of the plugin in the must-use plugins directory. |
||
| 123 | * |
||
| 124 | * @param string $domain Text domain to load. |
||
| 125 | */ |
||
| 126 | protected function load_textdomain( $domain ) { |
||
| 138 | |||
| 139 | |||
| 140 | /** |
||
| 141 | * Enqueue styles. |
||
| 142 | * |
||
| 143 | * @return void |
||
| 144 | */ |
||
| 145 | public function enqueue_scripts_styles() { |
||
| 155 | |||
| 156 | |||
| 157 | /** |
||
| 158 | * Show the menu item in Debug Bar. |
||
| 159 | * |
||
| 160 | * @return void |
||
| 161 | */ |
||
| 162 | public function prerender() { |
||
| 165 | |||
| 166 | |||
| 167 | /** |
||
| 168 | * Show the contents of the page. |
||
| 169 | * |
||
| 170 | * @return void |
||
| 171 | */ |
||
| 172 | public function render() { |
||
| 224 | |||
| 225 | |||
| 226 | /** |
||
| 227 | * Gets all of the cron jobs. |
||
| 228 | * |
||
| 229 | * @return array|null Array of crons. |
||
| 230 | */ |
||
| 231 | private function get_crons() { |
||
| 244 | |||
| 245 | |||
| 246 | /** |
||
| 247 | * Sort and count crons. |
||
| 248 | * |
||
| 249 | * This function sorts the cron jobs into core crons, and custom crons. It also tallies |
||
| 250 | * a total count for the crons as this number is otherwise tough to get. |
||
| 251 | */ |
||
| 252 | private function sort_count_crons() { |
||
| 267 | |||
| 268 | |||
| 269 | /** |
||
| 270 | * Displays the events in an easy to read table. |
||
| 271 | * |
||
| 272 | * @param array $events Array of events. |
||
| 273 | * @param string $no_events_msg Message to display if there are no events. |
||
| 274 | */ |
||
| 275 | private function display_events( $events, $no_events_msg ) { |
||
| 348 | |||
| 349 | |||
| 350 | /** |
||
| 351 | * Count the number of argument sets for a cron time. |
||
| 352 | * |
||
| 353 | * @param array $hook_array Array of hooks with argument sets. |
||
| 354 | * |
||
| 355 | * @return int |
||
| 356 | */ |
||
| 357 | private function get_arg_set_count( $hook_array ) { |
||
| 364 | |||
| 365 | |||
| 366 | /** |
||
| 367 | * Create a HTML attribute string for an event row. |
||
| 368 | * |
||
| 369 | * @param int $time Unix timestamp. |
||
| 370 | * @param string $hook Action hook for the cron job. |
||
| 371 | * |
||
| 372 | * @return string |
||
| 373 | */ |
||
| 374 | private function get_event_row_attributes( $time, $hook ) { |
||
| 396 | |||
| 397 | |||
| 398 | /** |
||
| 399 | * Display the timing for the event as a date, timestamp and human readable time difference. |
||
| 400 | * |
||
| 401 | * @param int $time Timestamp. |
||
| 402 | * @param int $event_count Number of events running at this time. |
||
| 403 | */ |
||
| 404 | private function display_event_time( $time, $event_count ) { |
||
| 415 | |||
| 416 | |||
| 417 | /** |
||
| 418 | * Display the name of the cron job event hook. |
||
| 419 | * |
||
| 420 | * @param string $hook Hook name. |
||
| 421 | * @param int $arg_set_count Number of events running at this time and on this hook. |
||
| 422 | */ |
||
| 423 | private function display_event_hook( $hook, $arg_set_count ) { |
||
| 430 | |||
| 431 | |||
| 432 | /** |
||
| 433 | * Displays the the event schedule name for recurring events or else 'single event'. |
||
| 434 | * |
||
| 435 | * @param array $info Event info array. |
||
| 436 | */ |
||
| 437 | private function display_event_schedule( $info ) { |
||
| 444 | |||
| 445 | |||
| 446 | /** |
||
| 447 | * Displays the event interval in seconds, minutes and hours. |
||
| 448 | * |
||
| 449 | * @param array $info Event info array. |
||
| 450 | */ |
||
| 451 | private function display_event_intervals( $info ) { |
||
| 465 | |||
| 466 | |||
| 467 | /** |
||
| 468 | * Displays the cron arguments in a readable format. |
||
| 469 | * |
||
| 470 | * @param mixed $args Cron argument(s). |
||
| 471 | * |
||
| 472 | * @return void |
||
| 473 | */ |
||
| 474 | private function display_event_cron_arguments( $args ) { |
||
| 484 | |||
| 485 | |||
| 486 | /** |
||
| 487 | * Displays all of the schedules defined. |
||
| 488 | * |
||
| 489 | * @return void |
||
| 490 | */ |
||
| 491 | private function display_schedules() { |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Sorting method for cron scheldules. Order by schedules interval. |
||
| 527 | * |
||
| 528 | * @param array $a First element of comparison pair. |
||
| 529 | * @param array $b Second element of comparison pair. |
||
| 530 | * |
||
| 531 | * @return int Return 1 if $a argument 'interval' greater then $b argument 'interval', 0 if both intervals equivalent and -1 otherwise. |
||
| 532 | */ |
||
| 533 | private function schedules_sorting( $a, $b ) { |
||
| 540 | |||
| 541 | /** |
||
| 542 | * Verify if a given timestamp is in the past or the future. |
||
| 543 | * |
||
| 544 | * @param int $time Unix timestamp. |
||
| 545 | * |
||
| 546 | * @return bool True if the time has passed, false otherwise. |
||
| 547 | */ |
||
| 548 | private function is_time_in_past( $time ) { |
||
| 551 | |||
| 552 | |||
| 553 | /** |
||
| 554 | * Transform a time in seconds to minutes rounded to 2 decimals. |
||
| 555 | * |
||
| 556 | * @param int $time Unix timestamp. |
||
| 557 | * |
||
| 558 | * @return int|float |
||
| 559 | */ |
||
| 560 | private function get_minutes( $time ) { |
||
| 563 | |||
| 564 | |||
| 565 | /** |
||
| 566 | * Transform a time in seconds to hours rounded to 2 decimals. |
||
| 567 | * |
||
| 568 | * @param int $time Unix timestamp. |
||
| 569 | * |
||
| 570 | * @return int|float |
||
| 571 | */ |
||
| 572 | private function get_hours( $time ) { |
||
| 575 | |||
| 576 | |||
| 577 | /** |
||
| 578 | * Compares time with current time and adds ' ago' if current time is greater than event time. |
||
| 579 | * |
||
| 580 | * @param string $human_time Human readable time difference. |
||
| 581 | * @param int $time Unix time of event. |
||
| 582 | * |
||
| 583 | * @return string |
||
| 584 | */ |
||
| 585 | private function display_past_time( $human_time, $time ) { |
||
| 593 | |||
| 594 | |||
| 595 | /** |
||
| 596 | * Load the pretty output class & set the recursion limit. |
||
| 597 | */ |
||
| 598 | private function load_debug_bar_pretty_output() { |
||
| 608 | |||
| 609 | |||
| 610 | /** |
||
| 611 | * Print any type of variable with colour coding and a variable type indication. |
||
| 612 | * |
||
| 613 | * @param mixed $variable The variable to print. |
||
| 614 | */ |
||
| 615 | private function print_pretty_output( $variable ) { |
||
| 624 | |||
| 625 | |||
| 626 | /** |
||
| 627 | * Unset recursion depth limit for the pretty output class. |
||
| 628 | * |
||
| 629 | * @internal Method available since DBPO v1.4. |
||
| 630 | */ |
||
| 631 | private function reset_debug_bar_pretty_output() { |
||
| 636 | } // End of class ZT_Debug_Bar_Cron. |
||
| 637 | |||
| 639 |