Complex classes like MslsPlugin 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 MslsPlugin, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class MslsPlugin { |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Injected MslsOptions object |
||
| 19 | * |
||
| 20 | * @var MslsOptions |
||
| 21 | */ |
||
| 22 | protected $options; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * MslsPlugin constructor. |
||
| 26 | * |
||
| 27 | * @param MslsOptions $options |
||
| 28 | */ |
||
| 29 | public function __construct( MslsOptions $options ) { |
||
| 30 | $this->options = $options; |
||
| 31 | } |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Factory |
||
| 35 | * |
||
| 36 | * @codeCoverageIgnore |
||
| 37 | * |
||
| 38 | * @return MslsPlugin |
||
| 39 | */ |
||
| 40 | public static function init() { |
||
| 41 | $options = MslsOptions::instance(); |
||
| 42 | $obj = new self( $options ); |
||
| 43 | |||
| 44 | add_action( 'plugins_loaded', [ $obj, 'init_i18n_support' ] ); |
||
| 45 | |||
| 46 | register_activation_hook( self::file(), [ $obj, 'activate' ] ); |
||
| 47 | |||
| 48 | if ( function_exists( 'is_multisite' ) && is_multisite() ) { |
||
| 49 | add_action( 'wp_head', [ __CLASS__, 'print_alternate_links' ] ); |
||
| 50 | add_filter( 'msls_get_output', [ __CLASS__, 'get_output' ] ); |
||
| 51 | |||
| 52 | add_action( 'widgets_init', [ $obj, 'init_widget' ] ); |
||
| 53 | add_filter( 'the_content', [ $obj, 'content_filter' ] ); |
||
| 54 | |||
| 55 | if ( function_exists( 'register_block_type' ) ) { |
||
| 56 | add_action( 'init', [ $obj, 'block_init' ] ); |
||
| 57 | } |
||
| 58 | |||
| 59 | \lloc\Msls\ContentImport\Service::instance()->register(); |
||
| 60 | |||
| 61 | if ( is_admin() ) { |
||
| 62 | add_action( 'admin_menu', [ $obj, 'admin_menu' ] ); |
||
| 63 | |||
| 64 | add_action( 'admin_menu', [ MslsAdmin::class, 'init' ] ); |
||
| 65 | add_action( 'load-post.php', [ MslsMetaBox::class, 'init' ] ); |
||
| 66 | add_action( 'load-post-new.php', [ MslsMetaBox::class, 'init' ] ); |
||
| 67 | add_action( 'load-edit.php', [ MslsCustomColumn::class, 'init' ] ); |
||
| 68 | add_action( 'load-edit.php', [ MslsCustomFilter::class, 'init' ] ); |
||
| 69 | |||
| 70 | add_action( 'load-edit-tags.php', [ MslsCustomColumnTaxonomy::class, 'init' ] ); |
||
| 71 | add_action( 'load-edit-tags.php', [ MslsPostTag::class, 'init' ] ); |
||
| 72 | |||
| 73 | if ( filter_has_var( INPUT_POST, 'action' ) ) { |
||
| 74 | $action = filter_input( INPUT_POST, 'action', FILTER_SANITIZE_STRING ); |
||
| 75 | |||
| 76 | if ( 'add-tag' === $action ) { |
||
| 77 | add_action( 'admin_init', [ MslsPostTag::class, 'init' ] ); |
||
| 78 | } elseif ( 'inline-save' === $action ) { |
||
| 79 | add_action( 'admin_init', [ MslsCustomColumn::class, 'init' ] ); |
||
| 80 | } elseif ( 'inline-save-tax' === $action ) { |
||
| 81 | add_action( 'admin_init', [ MslsCustomColumnTaxonomy::class, 'init' ] ); |
||
| 82 | } |
||
| 83 | } |
||
| 84 | |||
| 85 | add_action( 'wp_ajax_suggest_posts', [ MslsMetaBox::class, 'suggest' ] ); |
||
| 86 | add_action( 'wp_ajax_suggest_terms', [ MslsPostTag::class, 'suggest' ] ); |
||
| 87 | } |
||
| 88 | } else { |
||
| 89 | add_action( 'admin_notices', function () { |
||
| 90 | self::message_handler( |
||
| 91 | __( 'The Multisite Language Switcher needs the activation of the multisite-feature for working properly. Please read <a onclick="window.open(this.href); return false;" href="http://codex.wordpress.org/Create_A_Network">this post</a> if you don\'t know the meaning.', 'multisite-language-switcher' ) |
||
| 92 | ); |
||
| 93 | } ); |
||
| 94 | } |
||
| 95 | |||
| 96 | return $obj; |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Gets MslsOutput object |
||
| 101 | * |
||
| 102 | * @return MslsOutput |
||
| 103 | */ |
||
| 104 | public static function get_output() { |
||
| 113 | |||
| 114 | |||
| 115 | /** |
||
| 116 | * Callback for action wp_head |
||
| 117 | */ |
||
| 118 | public static function print_alternate_links() { |
||
| 119 | echo self::get_output()->get_alternate_links(), PHP_EOL; |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Filter for the_content() |
||
| 124 | * |
||
| 125 | * @param string $content |
||
| 126 | * |
||
| 127 | * @return string |
||
| 128 | */ |
||
| 129 | function content_filter( $content ) { |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Create filterstring for msls_content_filter() |
||
| 143 | * |
||
| 144 | * @param string $pref |
||
| 145 | * @param string $post |
||
| 146 | * |
||
| 147 | * @return string |
||
| 148 | */ |
||
| 149 | function filter_string( $pref = '<p id="msls">', $post = '</p>' ) { |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Register block and shortcode. |
||
| 190 | */ |
||
| 191 | public function block_init() { |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Loads styles and some js if needed |
||
| 218 | * |
||
| 219 | * The method returns true if JS is loaded or false if not |
||
| 220 | * |
||
| 221 | * @return boolean |
||
| 222 | */ |
||
| 223 | public function admin_menu() { |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Wrapper for plugins_url |
||
| 241 | * |
||
| 242 | * @param string $path |
||
| 243 | * |
||
| 244 | * @return string |
||
| 245 | */ |
||
| 246 | public static function plugins_url( string $path ): string { |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Wrapper for plugin_dir_path |
||
| 252 | * |
||
| 253 | * @param string $path |
||
| 254 | * |
||
| 255 | * @return string |
||
| 256 | */ |
||
| 257 | public static function plugin_dir_path( string $path ): string { |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @param string $path |
||
| 263 | * |
||
| 264 | * @return string |
||
| 265 | */ |
||
| 266 | public static function dirname( string $path ): string { |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @return string |
||
| 272 | */ |
||
| 273 | public static function file(): string { |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @return string |
||
| 279 | */ |
||
| 280 | public static function path(): string { |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Register widget |
||
| 286 | * |
||
| 287 | * The widget will only be registered if the current blog is not |
||
| 288 | * excluded in the configuration of the plugin. |
||
| 289 | * @return boolean |
||
| 290 | */ |
||
| 291 | public function init_widget() { |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Render widget output |
||
| 303 | * |
||
| 304 | * @return string |
||
| 305 | */ |
||
| 306 | public function block_render() { |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Load textdomain |
||
| 320 | * |
||
| 321 | * The method should be executed always on init because we have some |
||
| 322 | * translatable string in the frontend too. |
||
| 323 | * |
||
| 324 | * @return boolean |
||
| 325 | */ |
||
| 326 | public function init_i18n_support() { |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Message handler |
||
| 332 | * |
||
| 333 | * Prints a message box to the screen. |
||
| 334 | * |
||
| 335 | * @param string $message |
||
| 336 | * @param string $css_class |
||
| 337 | * |
||
| 338 | * @return boolean |
||
| 339 | */ |
||
| 340 | public static function message_handler( $message, $css_class = 'error' ) { |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Activate plugin |
||
| 352 | */ |
||
| 353 | public static function activate() { |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Uninstall plugin |
||
| 359 | * |
||
| 360 | * The plugin data in all blogs of the current network will be |
||
| 361 | * deleted after the uninstall procedure. |
||
| 362 | * |
||
| 363 | * @return boolean |
||
| 364 | */ |
||
| 365 | public static function uninstall() { |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Cleanup the options |
||
| 394 | * |
||
| 395 | * Removes all values of the current blogs which are stored in the |
||
| 396 | * options-table and returns true if it was successful. |
||
| 397 | * |
||
| 398 | * @return boolean |
||
| 399 | */ |
||
| 400 | public static function cleanup() { |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Get specific vars from $_POST and $_GET in a safe way |
||
| 416 | * |
||
| 417 | * @param array $list |
||
| 418 | * |
||
| 419 | * @return array |
||
| 420 | */ |
||
| 421 | public function get_superglobals( array $list ) { |
||
| 436 | |||
| 437 | } |
||
| 438 |