Completed
Pull Request — master (#111)
by Luca
02:07
created

MslsPlugin   B

Complexity

Total Complexity 40

Size/Duplication

Total Lines 329
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 329
rs 8.2608
c 0
b 0
f 0
wmc 40
lcom 1
cbo 4

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A get_output() 0 9 2
A content_filter() 0 11 4
B filter_string() 0 38 5
B admin_menu() 0 23 4
A init_widget() 0 9 2
A init_i18n_support() 0 7 1
A message_handler() 0 12 2
A activate() 0 3 1
B uninstall() 0 26 4
A cleanup() 0 13 2
A get_superglobals() 0 16 4
B init() 0 54 8

How to fix   Complexity   

Complex Class

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
2
/**
3
 * MslsPlugin
4
 * @author Dennis Ploetner <[email protected]>
5
 * @since 0.9.8
6
 */
7
8
namespace lloc\Msls;
9
10
/**
11
 * Provides functionalities for general hooks and activation/deactivation
12
 *
13
 * @package Msls
14
 */
15
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 ) {
27
		$this->options = $options;
28
	}
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() {
96
		static $obj = null;
97
98
		if ( is_null( $obj ) ) {
99
			$obj = MslsOutput::init();
100
		}
101
102
		return $obj;
103
	}
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 ) {
114
		if ( ! is_front_page() && is_singular() ) {
115
			$options = $this->options;
116
117
			if ( $options->is_content_filter() ) {
118
				$content .= $this->filter_string();
119
			}
120
		}
121
122
		return $content;
123
	}
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>' ) {
135
		$obj    = MslsOutput::init();
136
		$links  = $obj->get( 1, true, true );
137
		$output = __( 'This post is also available in %s.', 'multisite-language-switcher' );
138
139
		if ( has_filter( 'msls_filter_string' ) ) {
140
			/**
141
			 * Overrides the string for the output of the translation hint
142
			 * @since 1.0
143
			 * @param string $output
144
			 * @param array $links
145
			 */
146
			$output = apply_filters( 'msls_filter_string', $output, $links );
147
		}
148
		else {
149
			$output = '';
150
151
			if ( count( $links ) > 1 ) {
152
				$last   = array_pop( $links );
153
				$output = sprintf(
154
					$output,
155
					sprintf(
156
						__( '%s and %s', 'multisite-language-switcher' ),
157
						implode( ', ', $links ),
158
						$last
159
					)
160
				);
161
			}
162
			elseif ( 1 == count( $links ) ) {
163
				$output = sprintf(
164
					$output,
165
					$links[0]
166
				);
167
			}
168
		}
169
170
		return ! empty( $output ) ? $pref . $output . $post : '';
171
	}
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() {
181
		$postfix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' 	: '.min';
182
183
		wp_enqueue_style(
184
			'msls-styles',
185
			plugins_url( 'css/msls.css', MSLS_PLUGIN__FILE__ ),
186
			[],
187
			MSLS_PLUGIN_VERSION
188
		);
189
190
		if ( $this->options->activate_autocomplete ) {
191
			wp_enqueue_script(
192
				'msls-autocomplete',
193
				plugins_url( "js/msls{$postfix}.js", MSLS_PLUGIN__FILE__ ),
194
				array( 'jquery-ui-autocomplete' ),
195
				MSLS_PLUGIN_VERSION
196
			);
197
198
			return true;
199
		}
200
201
		return false;
202
	}
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() {
212
		if ( ! $this->options->is_excluded() ) {
213
			register_widget( MslsWidget::class );
214
215
			return true;
216
		}
217
218
		return false;
219
	}
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() {
230
		return load_plugin_textdomain(
231
			'multisite-language-switcher',
232
			false,
233
			dirname( MSLS_PLUGIN_PATH ) . '/languages/'
234
		);
235
	}
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' ) {
246
		if ( ! empty( $message ) ) {
247
			printf(
248
				'<div id="msls-warning" class="%s"><p>%s</p></div>',
249
				$css_class,
250
				$message
251
			);
252
			return true;
253
		}
254
255
		return false;
256
	}
257
258
	/**
259
	 * Activate plugin
260
	 */
261
	public function activate(){
262
		register_uninstall_hook( MSLS_PLUGIN__FILE__, [ $this, 'uninstall' ] );
263
	}
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() {
274
		/**
275
		 * We want to be sure that the user has not deactivated the
276
		 * multisite because we need to use switch_to_blog and
277
		 * restore_current_blog
278
		 */
279
		if ( function_exists( 'is_multisite' ) && is_multisite() ) {
280
			$cache = MslsSqlCacher::init( __CLASS__ )->set_params( __METHOD__ );
281
282
			$blogs = $cache->get_results(
283
				$cache->prepare(
284
					"SELECT blog_id FROM {$cache->blogs} WHERE blog_id != %d AND site_id = %d",
285
					$cache->blogid,
286
					$cache->siteid
287
				)
288
			);
289
290
			foreach ( $blogs as $blog ) {
291
				switch_to_blog( $blog->blog_id );
0 ignored issues
show
introduced by
switch_to_blog is not something you should ever need to do in a VIP theme context. Instead use an API (XML-RPC, REST) to interact with other sites if needed.
Loading history...
292
				self::cleanup();
293
				restore_current_blog();
294
			}
295
		}
296
297
		return self::cleanup();
298
	}
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() {
309
		if ( delete_option( 'msls' ) ) {
310
			$cache = MslsSqlCacher::init( __CLASS__ )->set_params( __METHOD__ );
311
			$sql   = $cache->prepare(
312
				"DELETE FROM {$cache->options} WHERE option_name LIKE %s",
313
				'msls_%'
314
			);
315
316
			return (bool) $cache->query( $sql );
317
		}
318
319
		return false;
320
	}
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 ) {
328
		$arr = [];
329
330
		foreach ( $list as $var ) {
331
			$arr[ $var ] = '';
332
333
			if ( filter_has_var( INPUT_POST, $var ) ) {
334
				$arr[ $var ] = filter_input( INPUT_POST, $var );
335
			}
336
			elseif ( filter_has_var( INPUT_GET, $var ) ) {
337
				$arr[ $var ] = filter_input( INPUT_GET, $var );
338
			}
339
		}
340
341
		return $arr;
342
	}
343
344
}
345