Completed
Push — master ( 042809...cf2d3e )
by Dennis
01:16
created

MslsPlugin::dirname()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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