Complex classes like Admin 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.
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 Admin, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | final class Admin { |
||
| 26 | |||
| 27 | /** |
||
| 28 | * The single instance of the class |
||
| 29 | * |
||
| 30 | * @var object |
||
| 31 | * @since 2.0.0 |
||
| 32 | */ |
||
| 33 | protected static $_instance = null; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Array containing the plugin links |
||
| 37 | * |
||
| 38 | * @var array |
||
| 39 | * @since 2.0.0 |
||
| 40 | */ |
||
| 41 | protected $links; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Return the single class instance |
||
| 45 | * |
||
| 46 | * @return object |
||
| 47 | * @since 2.0.0 |
||
| 48 | */ |
||
| 49 | public static function instance() { |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Class constructor |
||
| 58 | * |
||
| 59 | * @uses add_action() |
||
| 60 | * @uses add_filter() |
||
| 61 | * @uses get_option() |
||
| 62 | * @uses get_bloginfo() |
||
| 63 | * |
||
| 64 | * @global object $wp_embed |
||
| 65 | * @since 2.0.0 |
||
| 66 | */ |
||
| 67 | protected function __construct() { |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Prevent the class from being cloned |
||
| 76 | * |
||
| 77 | * @return void |
||
| 78 | * @since 2.0.0 |
||
| 79 | */ |
||
| 80 | protected function __clone() { |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Load language files |
||
| 86 | * |
||
| 87 | * @uses load_plugin_textdomain() |
||
| 88 | * |
||
| 89 | * @return void |
||
| 90 | * @since 2.0.0 |
||
| 91 | */ |
||
| 92 | public function load_textdomain() { |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Checks if the plugin admin code should be loaded |
||
| 98 | * |
||
| 99 | * @uses apply_filters() |
||
| 100 | * |
||
| 101 | * @global string $pagenow |
||
| 102 | * @return boolean |
||
| 103 | * @since 2.0.0 |
||
| 104 | */ |
||
| 105 | public function enabled() { |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Add actions and filters (only in widgets admin page) |
||
| 113 | * |
||
| 114 | * @uses add_action() |
||
| 115 | * @uses add_filter() |
||
| 116 | * @uses do_action() |
||
| 117 | * |
||
| 118 | * @return void |
||
| 119 | * @since 2.0.0 |
||
| 120 | */ |
||
| 121 | public function admin_init() { |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Instantiate tinyMCE editor |
||
| 147 | * |
||
| 148 | * @uses add_thickbox() |
||
| 149 | * @uses wp_enqueue_media() |
||
| 150 | * |
||
| 151 | * @return void |
||
| 152 | * @since 2.0.0 |
||
| 153 | */ |
||
| 154 | public function enqueue_media() { |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Enqueue styles |
||
| 165 | * |
||
| 166 | * @uses wp_enqueue_style() |
||
| 167 | * |
||
| 168 | * @return void |
||
| 169 | * @since 2.0.0 |
||
| 170 | */ |
||
| 171 | public function admin_print_styles() { |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Helper function to enqueue style |
||
| 179 | * |
||
| 180 | * @uses apply_filters() |
||
| 181 | * @uses wp_enqueue_style() |
||
| 182 | * @uses plugins_url() |
||
| 183 | * @uses SCRIPT_DEBUG |
||
| 184 | * |
||
| 185 | * @return void |
||
| 186 | * @since 2.0.0 |
||
| 187 | */ |
||
| 188 | public function enqueue_style() { |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Enqueue header scripts |
||
| 202 | * |
||
| 203 | * @uses wp_enqueue_script() |
||
| 204 | * @uses do_action() |
||
| 205 | * |
||
| 206 | * @return void |
||
| 207 | * @since 2.0.0 |
||
| 208 | */ |
||
| 209 | public function admin_print_scripts() { |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Helper function to enqueue script |
||
| 220 | * |
||
| 221 | * @uses apply_filters() |
||
| 222 | * @uses wp_enqueue_script() |
||
| 223 | * @uses plugins_url() |
||
| 224 | * @uses SCRIPT_DEBUG |
||
| 225 | * |
||
| 226 | * @return void |
||
| 227 | * @since 2.0.0 |
||
| 228 | */ |
||
| 229 | public function enqueue_script() { |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Helper function to enqueue localized script |
||
| 244 | * |
||
| 245 | * @uses apply_filters() |
||
| 246 | * @uses wp_localize_script() |
||
| 247 | * |
||
| 248 | * @return void |
||
| 249 | * @since 2.0.0 |
||
| 250 | */ |
||
| 251 | public function localize_script() { |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Enqueue footer scripts |
||
| 268 | * |
||
| 269 | * @return void |
||
| 270 | * @since 2.0.0 |
||
| 271 | */ |
||
| 272 | public function admin_print_footer_scripts() { |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Output the visual editor |
||
| 278 | * |
||
| 279 | * @uses wp_editor() |
||
| 280 | * |
||
| 281 | * @param string $text Text inside the editor. |
||
| 282 | * @param string $editor_id Editor instance ID. |
||
| 283 | * @param string $name Editor instance name. |
||
| 284 | * @param string $type Editor instance type. |
||
| 285 | * @return void |
||
| 286 | * @since 2.0.0 |
||
| 287 | */ |
||
| 288 | public function editor( $text, $editor_id, $name = '', $type = 'visual' ) { |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Remove editor content filters for multiple editor instances |
||
| 297 | * Workaround for WordPress Core bug #28403 https://core.trac.wordpress.org/ticket/28403 |
||
| 298 | * |
||
| 299 | * @uses remove_filter |
||
| 300 | * |
||
| 301 | * @return void |
||
| 302 | * @since 2.1.7 |
||
| 303 | */ |
||
| 304 | public function fix_the_editor_content_filter() { |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Setup editor instance for event handling |
||
| 311 | * |
||
| 312 | * @uses SCRIPT_DEBUG |
||
| 313 | * |
||
| 314 | * @return void |
||
| 315 | * @since 2.2.1 |
||
| 316 | */ |
||
| 317 | public function wp_tiny_mce_init() { |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Set editor settings |
||
| 325 | * |
||
| 326 | * @param mixed[] $settings Array of settings. |
||
| 327 | * @param string $editor_id Editor instance ID. |
||
| 328 | * @return mixed[] |
||
| 329 | * @since 2.0.0 |
||
| 330 | */ |
||
| 331 | public function editor_settings( $settings, $editor_id ) { |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Initialize plugin links |
||
| 347 | * |
||
| 348 | * @return void |
||
| 349 | * @since 2.0.0 |
||
| 350 | */ |
||
| 351 | public function init_links() { |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Display plugin links |
||
| 370 | * |
||
| 371 | * @return void |
||
| 372 | * @since 2.0.0 |
||
| 373 | */ |
||
| 374 | public function display_links() { |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Show row meta on the plugin screen |
||
| 390 | * |
||
| 391 | * @uses esc_html() |
||
| 392 | * @uses esc_url() |
||
| 393 | * |
||
| 394 | * @param string[] $links Array of links. |
||
| 395 | * @param string $file Plugin's filename. |
||
| 396 | * @return string[] |
||
| 397 | * @since 2.0.0 |
||
| 398 | */ |
||
| 399 | public function plugin_row_meta( $links, $file ) { |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Fix for rtl languages |
||
| 410 | * |
||
| 411 | * @param mixed[] $settings Array of settings. |
||
| 412 | * @return mixed[] |
||
| 413 | * @since 2.1.0 |
||
| 414 | */ |
||
| 415 | public function tinymce_fix_rtl( $settings ) { |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Apply TinyMCE default fullscreen |
||
| 425 | * |
||
| 426 | * @param mixed[] $settings Array of settings. |
||
| 427 | * @param string|null $editor_id Editor ID. |
||
| 428 | * @return mixed[] |
||
| 429 | * @since 2.1.2 |
||
| 430 | */ |
||
| 431 | public function tinymce_fullscreen( $settings, $editor_id = null ) { |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Disable Quicktags default fullscreen |
||
| 445 | * |
||
| 446 | * @param mixed[] $settings Array of settings. |
||
| 447 | * @param string $editor_id Editor ID. |
||
| 448 | * @return mixed[] |
||
| 449 | * @since 2.1.2 |
||
| 450 | */ |
||
| 451 | public function quicktags_fullscreen( $settings, $editor_id ) { |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Show admin notice when visual editor is disabled in current user's profile settings |
||
| 460 | * |
||
| 461 | * @uses get_user_meta() |
||
| 462 | * @uses get_current_user_id() |
||
| 463 | * |
||
| 464 | * @return void |
||
| 465 | * @since 2.4.0 |
||
| 466 | */ |
||
| 467 | public function visual_editor_disabled_notice() { |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Store dismission of the "Visual Editor disabled" notice for the current user |
||
| 483 | * |
||
| 484 | * @uses add_user_meta() |
||
| 485 | * @uses get_current_user_id() |
||
| 486 | * |
||
| 487 | * @return void |
||
| 488 | * @since 2.4.0 |
||
| 489 | */ |
||
| 490 | public function visual_editor_disabled_dismiss_notice() { |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Register a private custom post type to be used for link embed previews |
||
| 498 | * |
||
| 499 | * @uses register_post_type() |
||
| 500 | * |
||
| 501 | * @return void |
||
| 502 | * @since 3.0.0 |
||
| 503 | */ |
||
| 504 | public function register_dummy_post_type() { |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Get dummy post ID for link embed previews |
||
| 522 | * |
||
| 523 | * @uses WP_Query() |
||
| 524 | * @uses wp_insert_post() |
||
| 525 | * @uses update_option() |
||
| 526 | * @uses get_option() |
||
| 527 | * |
||
| 528 | * @return int |
||
| 529 | * @since 3.0.0 |
||
| 530 | */ |
||
| 531 | public function get_dummy_post_id() { |
||
| 540 | |||
| 541 | } // END class Black_Studio_TinyMCE_Admin |
||
| 542 | |||
| 544 |