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 |
||
16 | class MslsPlugin { |
||
17 | |||
18 | /** |
||
19 | * @var MslsOptions |
||
20 | */ |
||
21 | protected $options; |
||
22 | |||
23 | /** |
||
24 | * MslsPlugin constructor. |
||
25 | * |
||
26 | * @param MslsOptions $options |
||
27 | */ |
||
28 | public function __construct( MslsOptions $options ) { |
||
29 | $this->options = $options; |
||
30 | } |
||
31 | |||
32 | /** |
||
33 | * Factory |
||
34 | * |
||
35 | * @codeCoverageIgnore |
||
36 | * |
||
37 | * @return MslsPlugin |
||
38 | */ |
||
39 | public static function init() { |
||
40 | $options = MslsOptions::instance(); |
||
41 | $obj = new self( $options ); |
||
42 | |||
43 | add_action( 'plugins_loaded', [ $obj, 'init_i18n_support' ] ); |
||
44 | |||
45 | register_activation_hook( MSLS_PLUGIN__FILE__, [ $obj, 'activate' ] ); |
||
46 | |||
47 | if ( function_exists( 'is_multisite' ) && is_multisite() ) { |
||
48 | add_action( 'widgets_init', [ $obj, 'init_widget' ] ); |
||
49 | add_filter( 'the_content', [ $obj, 'content_filter' ] ); |
||
50 | add_action( 'wp_head', [ $obj, 'print_alternate_links' ] ); |
||
51 | |||
52 | if ( function_exists( 'register_block_type' ) ) { |
||
53 | add_action( 'init', [ $obj, 'block_init' ] ); |
||
54 | } |
||
55 | |||
56 | add_filter( 'msls_get_output', [ $obj, 'get_output' ] ); |
||
57 | |||
58 | \lloc\Msls\ContentImport\Service::instance()->register(); |
||
59 | |||
60 | if ( is_admin() ) { |
||
61 | add_action( 'admin_menu', [ $obj, 'admin_menu' ] ); |
||
62 | |||
63 | add_action( 'admin_menu', [ MslsAdmin::class, 'init' ] ); |
||
64 | add_action( 'load-post.php', [ MslsMetaBox::class, 'init' ] ); |
||
65 | add_action( 'load-post-new.php', [ MslsMetaBox::class, 'init' ] ); |
||
66 | add_action( 'load-edit.php', [ MslsCustomColumn::class, 'init' ] ); |
||
67 | add_action( 'load-edit.php', [ MslsCustomFilter::class, 'init' ] ); |
||
68 | |||
69 | add_action( 'load-edit-tags.php', [ MslsCustomColumnTaxonomy::class, 'init' ] ); |
||
70 | add_action( 'load-edit-tags.php', [ MslsPostTag::class, 'init' ] ); |
||
71 | |||
72 | if ( filter_has_var( INPUT_POST, 'action' ) ) { |
||
73 | $action = filter_input( INPUT_POST, 'action', FILTER_SANITIZE_STRING ); |
||
74 | |||
75 | if ( 'add-tag' === $action ) { |
||
76 | add_action( 'admin_init', [ MslsPostTag::class, 'init' ] ); |
||
77 | } elseif ( 'inline-save' === $action ) { |
||
78 | add_action( 'admin_init', [ MslsCustomColumn::class, 'init' ] ); |
||
79 | } elseif ( 'inline-save-tax' === $action ) { |
||
80 | add_action( 'admin_init', [ MslsCustomColumnTaxonomy::class, 'init' ] ); |
||
81 | } |
||
82 | } |
||
83 | |||
84 | add_action( 'wp_ajax_suggest_posts', [ MslsMetaBox::class, 'suggest' ] ); |
||
85 | add_action( 'wp_ajax_suggest_terms', [ MslsPostTag::class, 'suggest' ] ); |
||
86 | } |
||
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 function get_output() { |
||
105 | static $obj = null; |
||
106 | |||
107 | if ( is_null( $obj ) ) { |
||
108 | $obj = MslsOutput::init(); |
||
109 | } |
||
110 | |||
111 | return $obj; |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * Callback for action wp_head |
||
116 | */ |
||
117 | public function print_alternate_links() { |
||
118 | echo $this->get_output()->get_alternate_links(), PHP_EOL; |
||
|
|||
119 | } |
||
120 | |||
121 | /** |
||
122 | * Filter for the_content() |
||
123 | * |
||
124 | * @param string $content |
||
125 | * @return string |
||
126 | */ |
||
127 | function content_filter( $content ) { |
||
138 | |||
139 | /** |
||
140 | * Create filterstring for msls_content_filter() |
||
141 | * |
||
142 | * @param string $pref |
||
143 | * @param string $post |
||
144 | * @return string |
||
145 | */ |
||
146 | function filter_string( $pref = '<p id="msls">', $post = '</p>' ) { |
||
184 | |||
185 | /** |
||
186 | * Register block and shortcode. |
||
187 | */ |
||
188 | public function block_init() { |
||
212 | |||
213 | /** |
||
214 | * Loads styles and some js if needed |
||
215 | * |
||
216 | * The methiod returns true if JS is loaded or false if not |
||
217 | * |
||
218 | * @return boolean |
||
219 | */ |
||
220 | public function admin_menu() { |
||
243 | |||
244 | /** |
||
245 | * Register widget |
||
246 | * |
||
247 | * The widget will only be registered if the current blog is not |
||
248 | * excluded in the configuration of the plugin. |
||
249 | * @return boolean |
||
250 | */ |
||
251 | public function init_widget() { |
||
260 | |||
261 | /** |
||
262 | * Render widget output |
||
263 | * |
||
264 | * @return string |
||
265 | */ |
||
266 | public function block_render() { |
||
273 | |||
274 | /** |
||
275 | * Load textdomain |
||
276 | * |
||
277 | * The method should be executed always on init because we have some |
||
278 | * translatable string in the frontend too. |
||
279 | * |
||
280 | * @return boolean |
||
281 | */ |
||
282 | public function init_i18n_support() { |
||
289 | |||
290 | /** |
||
291 | * Message handler |
||
292 | * |
||
293 | * Prints a message box to the screen. |
||
294 | * @param string $message |
||
295 | * @param string $css_class |
||
296 | * @return boolean |
||
297 | */ |
||
298 | public static function message_handler( $message, $css_class = 'error' ) { |
||
311 | |||
312 | /** |
||
313 | * Activate plugin |
||
314 | */ |
||
315 | public static function activate(){ |
||
318 | |||
319 | /** |
||
320 | * Uninstall plugin |
||
321 | * |
||
322 | * The plugin data in all blogs of the current network will be |
||
323 | * deleted after the uninstall procedure. |
||
324 | * |
||
325 | * @return boolean |
||
326 | */ |
||
327 | public static function uninstall() { |
||
353 | |||
354 | /** |
||
355 | * Cleanup the options |
||
356 | * |
||
357 | * Removes all values of the current blogs which are stored in the |
||
358 | * options-table and returns true if it was successful. |
||
359 | * |
||
360 | * @return boolean |
||
361 | */ |
||
362 | public static function cleanup() { |
||
375 | |||
376 | /** |
||
377 | * Get specific vars from $_POST and $_GET in a safe way |
||
378 | * @param array $list |
||
379 | * @return array |
||
380 | */ |
||
381 | public static function get_superglobals( array $list ) { |
||
397 | |||
398 | } |
||
399 |