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 | * @param MslsOptions $options |
||
25 | */ |
||
26 | public function __construct( MslsOptions $options ) { |
||
29 | |||
30 | /** |
||
31 | * Factory |
||
32 | * |
||
33 | * @codeCoverageIgnore |
||
34 | * |
||
35 | * @return MslsPlugin |
||
36 | */ |
||
37 | public static function init() { |
||
38 | $options = MslsOptions::instance(); |
||
39 | $obj = new self( $options ); |
||
40 | |||
41 | add_action( 'plugins_loaded', [ $obj, 'init_i18n_support' ] ); |
||
42 | |||
43 | register_activation_hook( MSLS_PLUGIN__FILE__, [ $obj, 'activate' ] ); |
||
44 | |||
45 | if ( function_exists( 'is_multisite' ) && is_multisite() ) { |
||
46 | add_action( 'widgets_init', [ $obj, 'init_widget' ] ); |
||
47 | add_filter( 'the_content', [ $obj, 'content_filter' ] ); |
||
48 | |||
49 | add_filter( 'msls_get_output', [ $obj, 'get_output' ] ); |
||
50 | |||
51 | \lloc\Msls\ContentImport\Service::instance()->register(); |
||
52 | |||
53 | if ( is_admin() ) { |
||
54 | add_action( 'admin_menu', [ $obj, 'admin_menu' ] ); |
||
55 | |||
56 | add_action( 'admin_menu', [ MslsAdmin::class, 'init' ] ); |
||
57 | add_action( 'load-post.php', [ MslsMetaBox::class, 'init' ] ); |
||
58 | add_action( 'load-post-new.php', [ MslsMetaBox::class, 'init' ] ); |
||
59 | add_action( 'load-edit.php', [ MslsCustomColumn::class, 'init' ] ); |
||
60 | add_action( 'load-edit.php', [ MslsCustomFilter::class, 'init' ] ); |
||
61 | |||
62 | add_action( 'load-edit-tags.php', [ MslsCustomColumnTaxonomy::class, 'init' ] ); |
||
63 | add_action( 'load-edit-tags.php', [ MslsPostTag::class, 'init' ] ); |
||
64 | |||
65 | if ( filter_has_var( INPUT_POST, 'action' ) ) { |
||
66 | $action = filter_input( INPUT_POST, 'action', FILTER_SANITIZE_STRING ); |
||
67 | |||
68 | if ( 'add-tag' === $action ) { |
||
69 | add_action( 'admin_init', [ MslsPostTag::class, 'init' ] ); |
||
70 | } elseif ( 'inline-save' === $action ) { |
||
71 | add_action( 'admin_init', [ MslsCustomColumn::class, 'init' ] ); |
||
72 | } elseif ( 'inline-save-tax' === $action ) { |
||
73 | add_action( 'admin_init', [ MslsCustomColumnTaxonomy::class, 'init' ] ); |
||
74 | } |
||
75 | } |
||
76 | |||
77 | add_action( 'wp_ajax_suggest_posts', [ MslsMetaBox::class, 'suggest' ] ); |
||
78 | add_action( 'wp_ajax_suggest_terms', [ MslsPostTag::class, 'suggest' ] ); |
||
79 | } |
||
80 | } |
||
81 | else { |
||
82 | add_action( 'admin_notices', function () { |
||
83 | self::message_handler( |
||
84 | __( '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' ) |
||
85 | ); |
||
86 | } ); |
||
87 | } |
||
88 | |||
89 | return $obj; |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * @return MslsOutput |
||
94 | */ |
||
95 | public function get_output() { |
||
104 | |||
105 | /** |
||
106 | * Filter for the_content() |
||
107 | * |
||
108 | * @package Msls |
||
109 | * @uses MslsOptions |
||
110 | * @param string $content |
||
111 | * @return string |
||
112 | */ |
||
113 | function content_filter( $content ) { |
||
124 | |||
125 | /** |
||
126 | * Create filterstring for msls_content_filter() |
||
127 | * |
||
128 | * @package Msls |
||
129 | * @uses MslsOutput |
||
130 | * @param string $pref |
||
131 | * @param string $post |
||
132 | * @return string |
||
133 | */ |
||
134 | function filter_string( $pref = '<p id="msls">', $post = '</p>' ) { |
||
172 | |||
173 | /** |
||
174 | * Loads styles and some js if needed |
||
175 | * |
||
176 | * The methiod returns true if JS is loaded or false if not |
||
177 | * |
||
178 | * @return boolean |
||
179 | */ |
||
180 | public function admin_menu() { |
||
203 | |||
204 | /** |
||
205 | * Register widget |
||
206 | * |
||
207 | * The widget will only be registered if the current blog is not |
||
208 | * excluded in the configuration of the plugin. |
||
209 | * @return boolean |
||
210 | */ |
||
211 | public function init_widget() { |
||
220 | |||
221 | /** |
||
222 | * Load textdomain |
||
223 | * |
||
224 | * The method should be executed always on init because we have some |
||
225 | * translatable string in the frontend too. |
||
226 | * |
||
227 | * @return boolean |
||
228 | */ |
||
229 | public function init_i18n_support() { |
||
236 | |||
237 | /** |
||
238 | * Message handler |
||
239 | * |
||
240 | * Prints a message box to the screen. |
||
241 | * @param string $message |
||
242 | * @param string $css_class |
||
243 | * @return boolean |
||
244 | */ |
||
245 | public static function message_handler( $message, $css_class = 'error' ) { |
||
257 | |||
258 | /** |
||
259 | * Activate plugin |
||
260 | */ |
||
261 | public function activate(){ |
||
264 | |||
265 | /** |
||
266 | * Uninstall plugin |
||
267 | * |
||
268 | * The plugin data in all blogs of the current network will be |
||
269 | * deleted after the uninstall procedure. |
||
270 | * |
||
271 | * @return boolean |
||
272 | */ |
||
273 | public function uninstall() { |
||
299 | |||
300 | /** |
||
301 | * Cleanup the options |
||
302 | * |
||
303 | * Removes all values of the current blogs which are stored in the |
||
304 | * options-table and returns true if it was successful. |
||
305 | * |
||
306 | * @return boolean |
||
307 | */ |
||
308 | public static function cleanup() { |
||
321 | |||
322 | /** |
||
323 | * Get specific vars from $_POST and $_GET in a safe way |
||
324 | * @param array $list |
||
325 | * @return array |
||
326 | */ |
||
327 | public static function get_superglobals( array $list ) { |
||
343 | |||
344 | } |
||
345 |