Completed
Push — master ( ece6f0...261834 )
by Dennis
01:48
created

MslsPlugin::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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