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 ) { |
||
| 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_filter( 'msls_get_output', [ __CLASS__, 'get_output' ] ); |
||
| 50 | |||
| 51 | add_action( 'widgets_init', [ $obj, 'init_widget' ] ); |
||
| 52 | add_filter( 'the_content', [ $obj, 'content_filter' ] ); |
||
| 53 | |||
| 54 | add_action( 'wp_head', [ __CLASS__, 'print_alternate_links' ] ); |
||
| 55 | |||
| 56 | if ( function_exists( 'register_block_type' ) ) { |
||
| 57 | add_action( 'init', [ $obj, 'block_init' ] ); |
||
| 58 | } |
||
| 59 | |||
| 60 | add_action( 'init', [ $obj, 'admin_bar_init' ] ); |
||
| 61 | |||
| 62 | \lloc\Msls\ContentImport\Service::instance()->register(); |
||
| 63 | |||
| 64 | if ( is_admin() ) { |
||
| 65 | add_action( 'admin_menu', [ $obj, 'admin_menu' ] ); |
||
| 66 | |||
| 67 | add_action( 'admin_menu', [ MslsAdmin::class, 'init' ] ); |
||
| 68 | add_action( 'load-post.php', [ MslsMetaBox::class, 'init' ] ); |
||
| 69 | add_action( 'load-post-new.php', [ MslsMetaBox::class, 'init' ] ); |
||
| 70 | add_action( 'load-edit.php', [ MslsCustomColumn::class, 'init' ] ); |
||
| 71 | add_action( 'load-edit.php', [ MslsCustomFilter::class, 'init' ] ); |
||
| 72 | |||
| 73 | add_action( 'load-edit-tags.php', [ MslsCustomColumnTaxonomy::class, 'init' ] ); |
||
| 74 | add_action( 'load-edit-tags.php', [ MslsPostTag::class, 'init' ] ); |
||
| 75 | |||
| 76 | if ( filter_has_var( INPUT_POST, 'action' ) ) { |
||
| 77 | $action = filter_input( INPUT_POST, 'action', FILTER_SANITIZE_STRING ); |
||
| 78 | |||
| 79 | if ( 'add-tag' === $action ) { |
||
| 80 | add_action( 'admin_init', [ MslsPostTag::class, 'init' ] ); |
||
| 81 | } elseif ( 'inline-save' === $action ) { |
||
| 82 | add_action( 'admin_init', [ MslsCustomColumn::class, 'init' ] ); |
||
| 83 | } elseif ( 'inline-save-tax' === $action ) { |
||
| 84 | add_action( 'admin_init', [ MslsCustomColumnTaxonomy::class, 'init' ] ); |
||
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 88 | add_action( 'wp_ajax_suggest_posts', [ MslsMetaBox::class, 'suggest' ] ); |
||
| 89 | add_action( 'wp_ajax_suggest_terms', [ MslsPostTag::class, 'suggest' ] ); |
||
| 90 | } |
||
| 91 | } else { |
||
| 92 | add_action( 'admin_notices', function () { |
||
| 93 | $href = 'https://wordpress.org/support/article/create-a-network/'; |
||
| 94 | $msg = sprintf( __( '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="%s">this post</a> if you don\'t know the meaning.', 'multisite-language-switcher' ), $href ); |
||
| 95 | |||
| 96 | self::message_handler( $msg ); |
||
| 97 | } ); |
||
| 98 | } |
||
| 99 | |||
| 100 | return $obj; |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Gets MslsOutput object |
||
| 105 | * |
||
| 106 | * @return MslsOutput |
||
| 107 | */ |
||
| 108 | public static function get_output() { |
||
| 109 | static $obj = null; |
||
| 110 | |||
| 111 | if ( is_null( $obj ) ) { |
||
| 112 | $obj = MslsOutput::init(); |
||
| 113 | } |
||
| 114 | |||
| 115 | return $obj; |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @param $wp_admin_bar |
||
| 120 | */ |
||
| 121 | public static function update_adminbar( \WP_Admin_Bar $wp_admin_bar ) { |
||
| 122 | $blog_collection = MslsBlogCollection::instance(); |
||
| 123 | foreach ( $blog_collection->get_plugin_active_blogs() as $blog ) { |
||
| 124 | $title = '<div class="blavatar"></div>' . $blog->get_title(); |
||
| 125 | |||
| 126 | $wp_admin_bar->add_node( [ 'id' => 'blog-' . $blog->userblog_id, 'title' => $title ] ); |
||
| 127 | } |
||
| 128 | |||
| 129 | $blog = $blog_collection->get_current_blog(); |
||
| 130 | if ( is_object( $blog ) && method_exists( $blog, 'get_title' ) ) { |
||
| 131 | $wp_admin_bar->add_node( [ 'id' => 'site-name', 'title' => $blog->get_title() ] ); |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Callback for action wp_head |
||
| 137 | */ |
||
| 138 | public static function print_alternate_links() { |
||
| 139 | echo self::get_output()->get_alternate_links(), PHP_EOL; |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Filter for the_content() |
||
| 144 | * |
||
| 145 | * @param string $content |
||
| 146 | * |
||
| 147 | * @return string |
||
| 148 | */ |
||
| 149 | function content_filter( $content ) { |
||
| 150 | if ( ! is_front_page() && is_singular() ) { |
||
| 151 | $options = $this->options; |
||
| 152 | |||
| 153 | if ( $options->is_content_filter() ) { |
||
| 154 | $content .= $this->filter_string(); |
||
| 155 | } |
||
| 156 | } |
||
| 157 | |||
| 158 | return $content; |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Create filterstring for msls_content_filter() |
||
| 163 | * |
||
| 164 | * @param string $pref |
||
| 165 | * @param string $post |
||
| 166 | * |
||
| 167 | * @return string |
||
| 168 | */ |
||
| 169 | function filter_string( $pref = '<p id="msls">', $post = '</p>' ) { |
||
| 170 | $obj = MslsOutput::init(); |
||
| 171 | $links = $obj->get( 1, true, true ); |
||
| 172 | $output = __( 'This post is also available in %s.', 'multisite-language-switcher' ); |
||
| 173 | |||
| 174 | if ( has_filter( 'msls_filter_string' ) ) { |
||
| 175 | /** |
||
| 176 | * Overrides the string for the output of the translation hint |
||
| 177 | * |
||
| 178 | * @param string $output |
||
| 179 | * @param array $links |
||
| 180 | * |
||
| 181 | * @since 1.0 |
||
| 182 | */ |
||
| 183 | $output = apply_filters( 'msls_filter_string', $output, $links ); |
||
| 184 | } else { |
||
| 185 | $output = ''; |
||
| 186 | |||
| 187 | if ( count( $links ) > 1 ) { |
||
| 188 | $last = array_pop( $links ); |
||
| 189 | $output = sprintf( |
||
| 190 | $output, |
||
| 191 | sprintf( |
||
| 192 | __( '%s and %s', 'multisite-language-switcher' ), |
||
| 193 | implode( ', ', $links ), |
||
| 194 | $last |
||
| 195 | ) |
||
| 196 | ); |
||
| 197 | } elseif ( 1 == count( $links ) ) { |
||
| 198 | $output = sprintf( |
||
| 199 | $output, |
||
| 200 | $links[0] |
||
| 201 | ); |
||
| 202 | } |
||
| 203 | } |
||
| 204 | |||
| 205 | return ! empty( $output ) ? $pref . $output . $post : ''; |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Register block and shortcode. |
||
| 210 | * @return bool |
||
| 211 | */ |
||
| 212 | public function block_init() { |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @return bool |
||
| 239 | */ |
||
| 240 | public function admin_bar_init() { |
||
| 241 | if ( is_admin_bar_showing() && is_super_admin() ) { |
||
| 242 | add_action( 'admin_bar_menu', [ __CLASS__, 'update_adminbar' ], 999 ); |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Loads styles and some js if needed |
||
| 252 | * |
||
| 253 | * The method returns true if JS is loaded or false if not |
||
| 254 | * |
||
| 255 | * @return boolean |
||
| 256 | */ |
||
| 257 | public function admin_menu() { |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Wrapper for plugins_url |
||
| 275 | * |
||
| 276 | * @param string $path |
||
| 277 | * |
||
| 278 | * @return string |
||
| 279 | */ |
||
| 280 | public static function plugins_url( string $path ): string { |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Wrapper for plugin_dir_path |
||
| 286 | * |
||
| 287 | * @param string $path |
||
| 288 | * |
||
| 289 | * @return string |
||
| 290 | */ |
||
| 291 | public static function plugin_dir_path( string $path ): string { |
||
| 294 | |||
| 295 | /** |
||
| 296 | * @param string $path |
||
| 297 | * |
||
| 298 | * @return string |
||
| 299 | */ |
||
| 300 | public static function dirname( string $path ): string { |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @return string |
||
| 306 | */ |
||
| 307 | public static function file(): string { |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @return string |
||
| 313 | */ |
||
| 314 | public static function path(): string { |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Register widget |
||
| 320 | * |
||
| 321 | * The widget will only be registered if the current blog is not |
||
| 322 | * excluded in the configuration of the plugin. |
||
| 323 | * @return boolean |
||
| 324 | */ |
||
| 325 | public function init_widget() { |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Render widget output |
||
| 337 | * |
||
| 338 | * @return string |
||
| 339 | */ |
||
| 340 | public function block_render() { |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Load textdomain |
||
| 354 | * |
||
| 355 | * The method should be executed always on init because we have some |
||
| 356 | * translatable string in the frontend too. |
||
| 357 | * |
||
| 358 | * @return boolean |
||
| 359 | */ |
||
| 360 | public function init_i18n_support() { |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Message handler |
||
| 366 | * |
||
| 367 | * Prints a message box to the screen. |
||
| 368 | * |
||
| 369 | * @param string $message |
||
| 370 | * @param string $css_class |
||
| 371 | * |
||
| 372 | * @return boolean |
||
| 373 | */ |
||
| 374 | public static function message_handler( $message, $css_class = 'error' ) { |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Activate plugin |
||
| 386 | */ |
||
| 387 | public static function activate() { |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Uninstall plugin |
||
| 393 | * |
||
| 394 | * The plugin data in all blogs of the current network will be |
||
| 395 | * deleted after the uninstall procedure. |
||
| 396 | * |
||
| 397 | * @return boolean |
||
| 398 | */ |
||
| 399 | public static function uninstall() { |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Cleanup the options |
||
| 428 | * |
||
| 429 | * Removes all values of the current blogs which are stored in the |
||
| 430 | * options-table and returns true if it was successful. |
||
| 431 | * |
||
| 432 | * @return boolean |
||
| 433 | */ |
||
| 434 | public static function cleanup() { |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Get specific vars from $_POST and $_GET in a safe way |
||
| 450 | * |
||
| 451 | * @param array $list |
||
| 452 | * |
||
| 453 | * @return array |
||
| 454 | */ |
||
| 455 | public function get_superglobals( array $list ) { |
||
| 470 | |||
| 471 | } |
||
| 472 |