Completed
Push — master ( ea28d3...1760c1 )
by Dennis
04:45
created

MslsPlugin::init()   C

Complexity

Conditions 8
Paths 7

Size

Total Lines 52
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 33
nc 7
nop 0
dl 0
loc 52
rs 6.8493
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
class MslsPlugin {
16
17
	/**
18
	 * @var MslsOptions
19
	 */
20
	protected $options;
21
22
	/**
23
	 * @param MslsOptions $options
24
	 */
25
	public function __construct( MslsOptions $options ) {
26
		$this->options = $options;
27
	}
28
29
	/**
30
	 * Factory
31
	 *
32
	 * @codeCoverageIgnore
33
	 *
34
	 * @return MslsPlugin
35
	 */
36
	public static function init() {
37
		$options = MslsOptions::instance();
38
		$obj     = new self( $options );
39
40
		add_action( 'plugins_loaded', [ $obj, 'init_i18n_support' ] );
41
42
		register_activation_hook( MSLS_PLUGIN__FILE__, [ $obj, 'activate' ] );
43
44
		if ( function_exists( 'is_multisite' ) && is_multisite() ) {
45
			add_action( 'widgets_init', [ $obj, 'init_widget' ] );
46
			add_filter( 'the_content', [ $obj, 'content_filter' ] );
47
48
			add_filter( 'msls_get_output', [ $obj, 'get_output' ] );
49
50
			if ( is_admin() ) {
51
				add_action( 'admin_menu', [ $obj, 'admin_menu' ] );
52
53
				add_action( 'admin_menu', [ MslsAdmin::class, 'init' ] );
54
				add_action( 'load-post.php', [ MslsMetaBox::class, 'init' ] );
55
				add_action( 'load-post-new.php', [ MslsMetaBox::class, 'init' ] );
56
				add_action( 'load-edit.php', [ MslsCustomColumn::class, 'init' ] );
57
				add_action( 'load-edit.php', [ MslsCustomFilter::class, 'init' ] );
58
59
				add_action( 'load-edit-tags.php', [ MslsCustomColumnTaxonomy::class, 'init' ] );
60
				add_action( 'load-edit-tags.php', [ MslsPostTag::class, 'init' ] );
61
62
				if ( filter_has_var( INPUT_POST, 'action' ) ) {
63
					$action = filter_input( INPUT_POST, 'action', FILTER_SANITIZE_STRING );
64
65
					if ( 'add-tag' == $action ) {
66
						add_action( 'admin_init', [ MslsPostTag::class, 'init' ] );
67
					} elseif ( 'inline-save' == $action ) {
68
						add_action( 'admin_init', [ MslsCustomColumn::class, 'init' ] );
69
					} elseif ( 'inline-save-tax' == $action ) {
70
						add_action( 'admin_init', [ MslsCustomColumnTaxonomy::class, 'init' ] );
71
					}
72
				}
73
74
				add_action( 'wp_ajax_suggest_posts', [ MslsMetaBox::class, 'suggest' ] );
75
				add_action( 'wp_ajax_suggest_terms', [ MslsPostTag::class, 'suggest' ] );
76
			}
77
		}
78
		else {
79
			add_action( 'admin_notices', function () {
80
				self::message_handler(
81
					__( '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' )
82
				);
83
			} );
84
		}
85
86
		return $obj;
87
	}
88
89
	/**
90
	 * @return MslsOutput
91
	 */
92
	public function get_output() {
93
		static $obj = null;
94
95
		if ( is_null( $obj ) ) {
96
			$obj = MslsOutput::init();
97
		}
98
99
		return $obj;
100
	}
101
102
	/**
103
	 * Filter for the_content()
104
	 *
105
	 * @package Msls
106
	 * @uses MslsOptions
107
	 * @param string $content
108
	 * @return string
109
	 */
110
	function content_filter( $content ) {
111
		if ( ! is_front_page() && is_singular() ) {
112
			$options = $this->options;
113
114
			if ( $options->is_content_filter() ) {
115
				$content .= $this->filter_string();
116
			}
117
		}
118
119
		return $content;
120
	}
121
122
	/**
123
	 * Create filterstring for msls_content_filter()
124
	 *
125
	 * @package Msls
126
	 * @uses MslsOutput
127
	 * @param string $pref
128
	 * @param string $post
129
	 * @return string
130
	 */
131
	function filter_string( $pref = '<p id="msls">', $post = '</p>' ) {
132
		$obj    = MslsOutput::init();
133
		$links  = $obj->get( 1, true, true );
134
		$output = __( 'This post is also available in %s.', 'multisite-language-switcher' );
135
136
		if ( has_filter( 'msls_filter_string' ) ) {
137
			/**
138
			 * Overrides the string for the output of the translation hint
139
			 * @since 1.0
140
			 * @param string $output
141
			 * @param array $links
142
			 */
143
			$output = apply_filters( 'msls_filter_string', $output, $links );
144
		}
145
		else {
146
			$output = '';
147
148
			if ( count( $links ) > 1 ) {
149
				$last   = array_pop( $links );
150
				$output = sprintf(
151
					$output,
152
					sprintf(
153
						__( '%s and %s', 'multisite-language-switcher' ),
154
						implode( ', ', $links ),
155
						$last
156
					)
157
				);
158
			}
159
			elseif ( 1 == count( $links ) ) {
160
				$output = sprintf(
161
					$output,
162
					$links[0]
163
				);
164
			}
165
		}
166
167
		return ! empty( $output ) ? $pref . $output . $post : '';
168
	}
169
170
	/**
171
	 * Loads styles and some js if needed
172
	 *
173
	 * The methiod returns true if JS is loaded or false if not
174
 	 *
175
	 * @return boolean
176
	 */
177
	public function admin_menu() {
178
		$postfix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' 	: '.min';
179
180
		wp_enqueue_style(
181
			'msls-styles',
182
			plugins_url( 'css/msls.css', MSLS_PLUGIN__FILE__ ),
183
			[],
184
			MSLS_PLUGIN_VERSION
185
		);
186
187
		if ( $this->options->activate_autocomplete ) {
188
			wp_enqueue_script(
189
				'msls-autocomplete',
190
				plugins_url( "js/msls{$postfix}.js", MSLS_PLUGIN__FILE__ ),
191
				array( 'jquery-ui-autocomplete' ),
192
				MSLS_PLUGIN_VERSION
193
			);
194
195
			return true;
196
		}
197
198
		return false;
199
	}
200
201
	/**
202
	 * Register widget
203
	 *
204
	 * The widget will only be registered if the current blog is not
205
	 * excluded in the configuration of the plugin.
206
	 * @return boolean
207
	 */
208
	public function init_widget() {
209
		if ( ! $this->options->is_excluded() ) {
210
			register_widget( MslsWidget::class );
211
212
			return true;
213
		}
214
215
		return false;
216
	}
217
218
	/**
219
	 * Load textdomain
220
	 *
221
	 * The method should be executed always on init because we have some
222
	 * translatable string in the frontend too.
223
	 *
224
	 * @return boolean
225
	 */
226
	public function init_i18n_support() {
227
		return load_plugin_textdomain(
228
			'multisite-language-switcher',
229
			false,
230
			dirname( MSLS_PLUGIN_PATH ) . '/languages/'
231
		);
232
	}
233
234
	/**
235
	 * Message handler
236
	 *
237
	 * Prints a message box to the screen.
238
	 * @param string $message
239
	 * @param string $css_class
240
	 * @return boolean
241
	 */
242
	public static function message_handler( $message, $css_class = 'error' ) {
243
		if ( ! empty( $message ) ) {
244
			printf(
245
				'<div id="msls-warning" class="%s"><p>%s</p></div>',
246
				$css_class,
247
				$message
248
			);
249
			return true;
250
		}
251
252
		return false;
253
	}
254
255
	/**
256
	 * Activate plugin
257
	 */
258
	public function activate(){
259
		register_uninstall_hook( MSLS_PLUGIN__FILE__, [ $this, 'uninstall' ] );
260
	}
261
262
	/**
263
	 * Uninstall plugin
264
	 *
265
	 * The plugin data in all blogs of the current network will be
266
	 * deleted after the uninstall procedure.
267
	 *
268
	 * @return boolean
269
	 */
270
	public function uninstall() {
271
		/**
272
		 * We want to be sure that the user has not deactivated the
273
		 * multisite because we need to use switch_to_blog and
274
		 * restore_current_blog
275
		 */
276
		if ( function_exists( 'is_multisite' ) && is_multisite() ) {
277
			$cache = MslsSqlCacher::init( __CLASS__ )->set_params( __METHOD__ );
278
279
			$blogs = $cache->get_results(
280
				$cache->prepare(
281
					"SELECT blog_id FROM {$cache->blogs} WHERE blog_id != %d AND site_id = %d",
282
					$cache->blogid,
283
					$cache->siteid
284
				)
285
			);
286
287
			foreach ( $blogs as $blog ) {
288
				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...
289
				self::cleanup();
290
				restore_current_blog();
291
			}
292
		}
293
294
		return self::cleanup();
295
	}
296
297
	/**
298
	 * Cleanup the options
299
	 *
300
	 * Removes all values of the current blogs which are stored in the
301
	 * options-table and returns true if it was successful.
302
	 *
303
	 * @return boolean
304
	 */
305
	public static function cleanup() {
306
		if ( delete_option( 'msls' ) ) {
307
			$cache = MslsSqlCacher::init( __CLASS__ )->set_params( __METHOD__ );
308
			$sql   = $cache->prepare(
309
				"DELETE FROM {$cache->options} WHERE option_name LIKE %s",
310
				'msls_%'
311
			);
312
313
			return (bool) $cache->query( $sql );
314
		}
315
316
		return false;
317
	}
318
319
	/**
320
	 * Get specific vars from $_POST and $_GET in a safe way
321
	 * @param array $list
322
	 * @return array
323
	 */
324
	public static function get_superglobals( array $list ) {
325
		$arr = [];
326
327
		foreach ( $list as $var ) {
328
			$arr[ $var ] = '';
329
330
			if ( filter_has_var( INPUT_POST, $var ) ) {
331
				$arr[ $var ] = filter_input( INPUT_POST, $var );
332
			}
333
			elseif ( filter_has_var( INPUT_GET, $var ) ) {
334
				$arr[ $var ] = filter_input( INPUT_GET, $var );
335
			}
336
		}
337
338
		return $arr;
339
	}
340
341
}
342