Completed
Push — master ( ee928d...1fc1bc )
by Dennis
02:34
created

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