MslsPlugin::init()   B
last analyzed

Complexity

Conditions 9
Paths 13

Size

Total Lines 62

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
nc 13
nop 0
dl 0
loc 62
rs 7.2735
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
	 * 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
				$href = 'https://wordpress.org/support/article/create-a-network/';
94
				$msg  = sprintf( __( '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="%s">this post</a> if you don\'t know the meaning.', 'multisite-language-switcher' ), $href );
95
96
				self::message_handler( $msg );
97
			} );
98
		}
99
100
		return $obj;
101
	}
102
103
	/**
104
	 * Gets MslsOutput object
105
	 *
106
	 * @return MslsOutput
107
	 */
108
	public static function get_output() {
109
		static $obj = null;
110
111
		if ( is_null( $obj ) ) {
112
			$obj = MslsOutput::init();
113
		}
114
115
		return $obj;
116
	}
117
118
	/**
119
	 * @param $wp_admin_bar
120
	 */
121
	public static function update_adminbar( \WP_Admin_Bar $wp_admin_bar ) {
122
		$blog_collection = MslsBlogCollection::instance();
123
		foreach ( $blog_collection->get_plugin_active_blogs() as $blog ) {
124
			$title = '<div class="blavatar"></div>' . $blog->get_title();
125
126
			$wp_admin_bar->add_node( [ 'id' => 'blog-' . $blog->userblog_id, 'title' => $title ] );
127
		}
128
129
		$blog = $blog_collection->get_current_blog();
130
		if ( is_object( $blog ) && method_exists( $blog, 'get_title' ) ) {
131
			$wp_admin_bar->add_node( [ 'id' => 'site-name', 'title' => $blog->get_title() ] );
132
		}
133
	}
134
135
	/**
136
	 * Callback for action wp_head
137
	 */
138
	public static function print_alternate_links() {
139
		echo self::get_output()->get_alternate_links(), PHP_EOL;
140
	}
141
142
	/**
143
	 * Filter for the_content()
144
	 *
145
	 * @param string $content
146
	 *
147
	 * @return string
148
	 */
149
	function content_filter( $content ) {
150
		if ( ! is_front_page() && is_singular() ) {
151
			$options = $this->options;
152
153
			if ( $options->is_content_filter() ) {
154
				$content .= $this->filter_string();
155
			}
156
		}
157
158
		return $content;
159
	}
160
161
	/**
162
	 * Create filterstring for msls_content_filter()
163
	 *
164
	 * @param string $pref
165
	 * @param string $post
166
	 *
167
	 * @return string
168
	 */
169
	function filter_string( $pref = '<p id="msls">', $post = '</p>' ) {
170
		$obj    = MslsOutput::init();
171
		$links  = $obj->get( 1, true, true );
172
		$output = __( 'This post is also available in %s.', 'multisite-language-switcher' );
173
174
		if ( has_filter( 'msls_filter_string' ) ) {
175
			/**
176
			 * Overrides the string for the output of the translation hint
177
			 *
178
			 * @param string $output
179
			 * @param array $links
180
			 *
181
			 * @since 1.0
182
			 */
183
			$output = apply_filters( 'msls_filter_string', $output, $links );
184
		} else {
185
			$output = '';
186
187
			if ( count( $links ) > 1 ) {
188
				$last   = array_pop( $links );
189
				$output = sprintf(
190
					$output,
191
					sprintf(
192
						__( '%s and %s', 'multisite-language-switcher' ),
193
						implode( ', ', $links ),
194
						$last
195
					)
196
				);
197
			} elseif ( 1 == count( $links ) ) {
198
				$output = sprintf(
199
					$output,
200
					$links[0]
201
				);
202
			}
203
		}
204
205
		return ! empty( $output ) ? $pref . $output . $post : '';
206
	}
207
208
	/**
209
	 * Register block and shortcode.
210
	 * @return bool
211
	 */
212
	public function block_init() {
213
		if ( ! $this->options->is_excluded() ) {
214
			$handle   = 'msls-widget-block';
215
			$callback = [ $this, 'block_render' ];
216
217
			wp_register_script(
218
				$handle,
219
				self::plugins_url( 'js/msls-widget-block.js' ),
220
				[ 'wp-blocks', 'wp-element', 'wp-components', 'wp-editor' ]
221
			);
222
223
			register_block_type( 'lloc/msls-widget-block', [
224
				'attributes'      => [ 'title' => [ 'type' => 'string' ] ],
225
				'editor_script'   => $handle,
226
				'render_callback' => $callback,
227
			] );
228
229
			add_shortcode( 'sc_msls_widget', $callback );
230
231
			return true;
232
		}
233
234
		return false;
235
	}
236
237
	/**
238
	 * @return bool
239
	 */
240
	public function admin_bar_init() {
241
		if ( is_admin_bar_showing() && is_super_admin() ) {
242
			add_action( 'admin_bar_menu', [ __CLASS__, 'update_adminbar' ], 999 );
243
244
			return true;
245
		}
246
247
		return false;
248
	}
249
250
	/**
251
	 * Loads styles and some js if needed
252
	 *
253
	 * The method returns true if JS is loaded or false if not
254
	 *
255
	 * @return boolean
256
	 */
257
	public function admin_menu() {
258
		$ver     = defined( 'MSLS_PLUGIN_VERSION' ) ? constant( 'MSLS_PLUGIN_VERSION' ) : false;
259
		$postfix = defined( 'SCRIPT_DEBUG' ) && constant( 'SCRIPT_DEBUG' ) ? '' : '.min';
260
261
		wp_enqueue_style( 'msls-styles', self::plugins_url( 'css/msls.css' ), [], $ver );
262
		wp_enqueue_style( 'msls-flags', self::plugins_url( 'css-flags/css/flag-icon.min.css' ), [], $ver );
263
264
		if ( $this->options->activate_autocomplete ) {
265
			wp_enqueue_script( 'msls-autocomplete', self::plugins_url( "js/msls{$postfix}.js" ), [ 'jquery-ui-autocomplete' ], $ver );
266
267
			return true;
268
		}
269
270
		return false;
271
	}
272
273
	/**
274
	 * Wrapper for plugins_url
275
	 *
276
	 * @param string $path
277
	 *
278
	 * @return string
279
	 */
280
	public static function plugins_url( string $path ): string {
281
		return plugins_url( $path, self::file() );
282
	}
283
284
	/**
285
	 * Wrapper for plugin_dir_path
286
	 *
287
	 * @param string $path
288
	 *
289
	 * @return string
290
	 */
291
	public static function plugin_dir_path( string $path ): string {
292
		return plugin_dir_path( self::file() ) . $path;
293
	}
294
295
	/**
296
	 * @param string $path
297
	 *
298
	 * @return string
299
	 */
300
	public static function dirname( string $path ): string {
301
		return dirname( self::path() ) . $path;
302
	}
303
304
	/**
305
	 * @return string
306
	 */
307
	public static function file(): string {
308
		return defined( 'MSLS_PLUGIN__FILE__' ) ? constant( 'MSLS_PLUGIN__FILE__' ) : '';
309
	}
310
311
	/**
312
	 * @return string
313
	 */
314
	public static function path(): string {
315
		return defined( 'MSLS_PLUGIN_PATH' ) ? constant( 'MSLS_PLUGIN_PATH' ) : '';
316
	}
317
318
	/**
319
	 * Register widget
320
	 *
321
	 * The widget will only be registered if the current blog is not
322
	 * excluded in the configuration of the plugin.
323
	 * @return boolean
324
	 */
325
	public function init_widget() {
326
		if ( ! $this->options->is_excluded() ) {
327
			register_widget( MslsWidget::class );
328
329
			return true;
330
		}
331
332
		return false;
333
	}
334
335
	/**
336
	 * Render widget output
337
	 *
338
	 * @return string
339
	 */
340
	public function block_render() {
341
		if ( ! $this->init_widget() ) {
342
			return '';
343
		}
344
345
		ob_start();
346
		the_widget( MslsWidget::class );
347
		$output = ob_get_clean();
348
349
		return $output;
350
	}
351
352
	/**
353
	 * Load textdomain
354
	 *
355
	 * The method should be executed always on init because we have some
356
	 * translatable string in the frontend too.
357
	 *
358
	 * @return boolean
359
	 */
360
	public function init_i18n_support() {
361
		return load_plugin_textdomain( 'multisite-language-switcher', false, self::dirname( '/languages/' ) );
362
	}
363
364
	/**
365
	 * Message handler
366
	 *
367
	 * Prints a message box to the screen.
368
	 *
369
	 * @param string $message
370
	 * @param string $css_class
371
	 *
372
	 * @return boolean
373
	 */
374
	public static function message_handler( $message, $css_class = 'error' ) {
375
		if ( ! empty( $message ) ) {
376
			printf( '<div id="msls-warning" class="%s"><p>%s</p></div>', $css_class, $message );
377
378
			return true;
379
		}
380
381
		return false;
382
	}
383
384
	/**
385
	 * Activate plugin
386
	 */
387
	public static function activate() {
388
		register_uninstall_hook( self::file(), [ __CLASS__, 'uninstall' ] );
389
	}
390
391
	/**
392
	 * Uninstall plugin
393
	 *
394
	 * The plugin data in all blogs of the current network will be
395
	 * deleted after the uninstall procedure.
396
	 *
397
	 * @return boolean
398
	 */
399
	public static function uninstall() {
400
		/**
401
		 * We want to be sure that the user has not deactivated the
402
		 * multisite because we need to use switch_to_blog and
403
		 * restore_current_blog
404
		 */
405
		if ( function_exists( 'is_multisite' ) && is_multisite() ) {
406
			$cache = MslsSqlCacher::init( __CLASS__ )->set_params( __METHOD__ );
407
408
			$blogs = $cache->get_results(
409
				$cache->prepare(
410
					"SELECT blog_id FROM {$cache->blogs} WHERE blog_id != %d AND site_id = %d",
411
					$cache->blogid,
412
					$cache->siteid
413
				)
414
			);
415
416
			foreach ( $blogs as $blog ) {
417
				switch_to_blog( $blog->blog_id );
418
				self::cleanup();
419
				restore_current_blog();
420
			}
421
		}
422
423
		return self::cleanup();
424
	}
425
426
	/**
427
	 * Cleanup the options
428
	 *
429
	 * Removes all values of the current blogs which are stored in the
430
	 * options-table and returns true if it was successful.
431
	 *
432
	 * @return boolean
433
	 */
434
	public static function cleanup() {
435
		if ( delete_option( 'msls' ) ) {
436
			$cache = MslsSqlCacher::init( __CLASS__ )->set_params( __METHOD__ );
437
			$sql   = $cache->prepare(
438
				"DELETE FROM {$cache->options} WHERE option_name LIKE %s",
439
				'msls_%'
440
			);
441
442
			return (bool) $cache->query( $sql );
443
		}
444
445
		return false;
446
	}
447
448
	/**
449
	 * Get specific vars from $_POST and $_GET in a safe way
450
	 *
451
	 * @param array $list
452
	 *
453
	 * @return array
454
	 */
455
	public function get_superglobals( array $list ) {
456
		$arr = [];
457
458
		foreach ( $list as $var ) {
459
			$arr[ $var ] = '';
460
461
			if ( filter_has_var( INPUT_POST, $var ) ) {
462
				$arr[ $var ] = filter_input( INPUT_POST, $var );
463
			} elseif ( filter_has_var( INPUT_GET, $var ) ) {
464
				$arr[ $var ] = filter_input( INPUT_GET, $var );
465
			}
466
		}
467
468
		return $arr;
469
	}
470
471
}
472