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