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_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 | self::message_handler( |
||
94 | __( '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' ) |
||
95 | ); |
||
96 | } ); |
||
97 | } |
||
98 | |||
99 | return $obj; |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * Gets MslsOutput object |
||
104 | * |
||
105 | * @return MslsOutput |
||
106 | */ |
||
107 | public static function get_output() { |
||
116 | |||
117 | /** |
||
118 | * @param $wp_admin_bar |
||
119 | */ |
||
120 | public static function update_adminbar( \WP_Admin_Bar $wp_admin_bar ) { |
||
121 | $blog_collection = MslsBlogCollection::instance(); |
||
122 | foreach ( $blog_collection->get_plugin_active_blogs() as $blog ) { |
||
123 | $title = '<div class="blavatar"></div>' . $blog->get_title(); |
||
124 | |||
125 | $wp_admin_bar->add_node( [ 'id' => 'blog-' . $blog->userblog_id, 'title' => $title ] ); |
||
126 | } |
||
127 | |||
128 | $blog = $blog_collection->get_current_blog(); |
||
129 | if ( is_object( $blog ) && method_exists( $blog, 'get_title' ) ) { |
||
130 | $wp_admin_bar->add_node( [ 'id' => 'site-name', 'title' => $blog->get_title() ] ); |
||
131 | } |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Callback for action wp_head |
||
136 | */ |
||
137 | public static function print_alternate_links() { |
||
140 | |||
141 | /** |
||
142 | * Filter for the_content() |
||
143 | * |
||
144 | * @param string $content |
||
145 | * |
||
146 | * @return string |
||
147 | */ |
||
148 | function content_filter( $content ) { |
||
159 | |||
160 | /** |
||
161 | * Create filterstring for msls_content_filter() |
||
162 | * |
||
163 | * @param string $pref |
||
164 | * @param string $post |
||
165 | * |
||
166 | * @return string |
||
167 | */ |
||
168 | function filter_string( $pref = '<p id="msls">', $post = '</p>' ) { |
||
206 | |||
207 | /** |
||
208 | * Register block and shortcode. |
||
209 | * @return bool |
||
210 | */ |
||
211 | public function block_init() { |
||
235 | |||
236 | /** |
||
237 | * @return bool |
||
238 | */ |
||
239 | public function admin_bar_init() { |
||
248 | |||
249 | /** |
||
250 | * Loads styles and some js if needed |
||
251 | * |
||
252 | * The method returns true if JS is loaded or false if not |
||
253 | * |
||
254 | * @return boolean |
||
255 | */ |
||
256 | public function admin_menu() { |
||
271 | |||
272 | /** |
||
273 | * Wrapper for plugins_url |
||
274 | * |
||
275 | * @param string $path |
||
276 | * |
||
277 | * @return string |
||
278 | */ |
||
279 | public static function plugins_url( string $path ): string { |
||
282 | |||
283 | /** |
||
284 | * Wrapper for plugin_dir_path |
||
285 | * |
||
286 | * @param string $path |
||
287 | * |
||
288 | * @return string |
||
289 | */ |
||
290 | public static function plugin_dir_path( string $path ): string { |
||
293 | |||
294 | /** |
||
295 | * @param string $path |
||
296 | * |
||
297 | * @return string |
||
298 | */ |
||
299 | public static function dirname( string $path ): string { |
||
302 | |||
303 | /** |
||
304 | * @return string |
||
305 | */ |
||
306 | public static function file(): string { |
||
309 | |||
310 | /** |
||
311 | * @return string |
||
312 | */ |
||
313 | public static function path(): string { |
||
316 | |||
317 | /** |
||
318 | * Register widget |
||
319 | * |
||
320 | * The widget will only be registered if the current blog is not |
||
321 | * excluded in the configuration of the plugin. |
||
322 | * @return boolean |
||
323 | */ |
||
324 | public function init_widget() { |
||
333 | |||
334 | /** |
||
335 | * Render widget output |
||
336 | * |
||
337 | * @return string |
||
338 | */ |
||
339 | public function block_render() { |
||
350 | |||
351 | /** |
||
352 | * Load textdomain |
||
353 | * |
||
354 | * The method should be executed always on init because we have some |
||
355 | * translatable string in the frontend too. |
||
356 | * |
||
357 | * @return boolean |
||
358 | */ |
||
359 | public function init_i18n_support() { |
||
362 | |||
363 | /** |
||
364 | * Message handler |
||
365 | * |
||
366 | * Prints a message box to the screen. |
||
367 | * |
||
368 | * @param string $message |
||
369 | * @param string $css_class |
||
370 | * |
||
371 | * @return boolean |
||
372 | */ |
||
373 | public static function message_handler( $message, $css_class = 'error' ) { |
||
382 | |||
383 | /** |
||
384 | * Activate plugin |
||
385 | */ |
||
386 | public static function activate() { |
||
389 | |||
390 | /** |
||
391 | * Uninstall plugin |
||
392 | * |
||
393 | * The plugin data in all blogs of the current network will be |
||
394 | * deleted after the uninstall procedure. |
||
395 | * |
||
396 | * @return boolean |
||
397 | */ |
||
398 | public static function uninstall() { |
||
424 | |||
425 | /** |
||
426 | * Cleanup the options |
||
427 | * |
||
428 | * Removes all values of the current blogs which are stored in the |
||
429 | * options-table and returns true if it was successful. |
||
430 | * |
||
431 | * @return boolean |
||
432 | */ |
||
433 | public static function cleanup() { |
||
446 | |||
447 | /** |
||
448 | * Get specific vars from $_POST and $_GET in a safe way |
||
449 | * |
||
450 | * @param array $list |
||
451 | * |
||
452 | * @return array |
||
453 | */ |
||
454 | public function get_superglobals( array $list ) { |
||
469 | |||
470 | } |
||
471 |