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
|
|
|
add_action( 'admin_bar_menu', [ __CLASS__, 'update_adminbar' ], 999 ); |
51
|
|
|
|
52
|
|
|
add_action( 'widgets_init', [ $obj, 'init_widget' ] ); |
53
|
|
|
add_filter( 'the_content', [ $obj, 'content_filter' ] ); |
54
|
|
|
|
55
|
|
|
if( is_super_admin() || is_admin_bar_showing() ) { |
56
|
|
|
add_action( 'wp_head', [ __CLASS__, 'print_alternate_links' ] ); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if ( function_exists( 'register_block_type' ) ) { |
60
|
|
|
add_action( 'init', [ $obj, 'block_init' ] ); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
\lloc\Msls\ContentImport\Service::instance()->register(); |
64
|
|
|
|
65
|
|
|
if ( is_admin() ) { |
66
|
|
|
add_action( 'admin_menu', [ $obj, 'admin_menu' ] ); |
67
|
|
|
|
68
|
|
|
add_action( 'admin_menu', [ MslsAdmin::class, 'init' ] ); |
69
|
|
|
add_action( 'load-post.php', [ MslsMetaBox::class, 'init' ] ); |
70
|
|
|
add_action( 'load-post-new.php', [ MslsMetaBox::class, 'init' ] ); |
71
|
|
|
add_action( 'load-edit.php', [ MslsCustomColumn::class, 'init' ] ); |
72
|
|
|
add_action( 'load-edit.php', [ MslsCustomFilter::class, 'init' ] ); |
73
|
|
|
|
74
|
|
|
add_action( 'load-edit-tags.php', [ MslsCustomColumnTaxonomy::class, 'init' ] ); |
75
|
|
|
add_action( 'load-edit-tags.php', [ MslsPostTag::class, 'init' ] ); |
76
|
|
|
|
77
|
|
|
if ( filter_has_var( INPUT_POST, 'action' ) ) { |
78
|
|
|
$action = filter_input( INPUT_POST, 'action', FILTER_SANITIZE_STRING ); |
79
|
|
|
|
80
|
|
|
if ( 'add-tag' === $action ) { |
81
|
|
|
add_action( 'admin_init', [ MslsPostTag::class, 'init' ] ); |
82
|
|
|
} elseif ( 'inline-save' === $action ) { |
83
|
|
|
add_action( 'admin_init', [ MslsCustomColumn::class, 'init' ] ); |
84
|
|
|
} elseif ( 'inline-save-tax' === $action ) { |
85
|
|
|
add_action( 'admin_init', [ MslsCustomColumnTaxonomy::class, 'init' ] ); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
add_action( 'wp_ajax_suggest_posts', [ MslsMetaBox::class, 'suggest' ] ); |
90
|
|
|
add_action( 'wp_ajax_suggest_terms', [ MslsPostTag::class, 'suggest' ] ); |
91
|
|
|
} |
92
|
|
|
} else { |
93
|
|
|
add_action( 'admin_notices', function () { |
94
|
|
|
self::message_handler( |
95
|
|
|
__( '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' ) |
96
|
|
|
); |
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
|
|
|
*/ |
211
|
|
|
public function block_init() { |
212
|
|
|
if ( ! $this->options->is_excluded() ) { |
213
|
|
|
$handle = 'msls-widget-block'; |
214
|
|
|
$callback = [ $this, 'block_render' ]; |
215
|
|
|
|
216
|
|
|
wp_register_script( |
217
|
|
|
$handle, |
218
|
|
|
self::plugins_url( 'js/msls-widget-block.js' ), |
219
|
|
|
[ 'wp-blocks', 'wp-element', 'wp-components', 'wp-editor' ] |
220
|
|
|
); |
221
|
|
|
|
222
|
|
|
register_block_type( 'lloc/msls-widget-block', [ |
223
|
|
|
'attributes' => [ 'title' => [ 'type' => 'string' ] ], |
224
|
|
|
'editor_script' => $handle, |
225
|
|
|
'render_callback' => $callback, |
226
|
|
|
] ); |
227
|
|
|
|
228
|
|
|
add_shortcode( 'sc_msls_widget', $callback ); |
229
|
|
|
|
230
|
|
|
return true; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
return false; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Loads styles and some js if needed |
238
|
|
|
* |
239
|
|
|
* The method returns true if JS is loaded or false if not |
240
|
|
|
* |
241
|
|
|
* @return boolean |
242
|
|
|
*/ |
243
|
|
|
public function admin_menu() { |
244
|
|
|
$ver = defined( 'MSLS_PLUGIN_VERSION' ) ? constant( 'MSLS_PLUGIN_VERSION' ) : false; |
245
|
|
|
$postfix = defined( 'SCRIPT_DEBUG' ) && constant( 'SCRIPT_DEBUG' ) ? '' : '.min'; |
246
|
|
|
|
247
|
|
|
wp_enqueue_style( 'msls-styles', self::plugins_url( 'css/msls.css' ), [], $ver ); |
248
|
|
|
wp_enqueue_style( 'msls-flags', self::plugins_url( 'css-flags/css/flag-icon.min.css' ), [], $ver ); |
249
|
|
|
|
250
|
|
|
if ( $this->options->activate_autocomplete ) { |
251
|
|
|
wp_enqueue_script( 'msls-autocomplete', self::plugins_url( "js/msls{$postfix}.js" ), [ 'jquery-ui-autocomplete' ], $ver ); |
252
|
|
|
|
253
|
|
|
return true; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
return false; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Wrapper for plugins_url |
261
|
|
|
* |
262
|
|
|
* @param string $path |
263
|
|
|
* |
264
|
|
|
* @return string |
265
|
|
|
*/ |
266
|
|
|
public static function plugins_url( string $path ): string { |
267
|
|
|
return plugins_url( $path, self::file() ); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Wrapper for plugin_dir_path |
272
|
|
|
* |
273
|
|
|
* @param string $path |
274
|
|
|
* |
275
|
|
|
* @return string |
276
|
|
|
*/ |
277
|
|
|
public static function plugin_dir_path( string $path ): string { |
278
|
|
|
return plugin_dir_path( self::file() ) . $path; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* @param string $path |
283
|
|
|
* |
284
|
|
|
* @return string |
285
|
|
|
*/ |
286
|
|
|
public static function dirname( string $path ): string { |
287
|
|
|
return dirname( self::path() ) . $path; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* @return string |
292
|
|
|
*/ |
293
|
|
|
public static function file(): string { |
294
|
|
|
return defined( 'MSLS_PLUGIN__FILE__' ) ? constant( 'MSLS_PLUGIN__FILE__' ) : ''; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* @return string |
299
|
|
|
*/ |
300
|
|
|
public static function path(): string { |
301
|
|
|
return defined( 'MSLS_PLUGIN_PATH' ) ? constant( 'MSLS_PLUGIN_PATH' ) : ''; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Register widget |
306
|
|
|
* |
307
|
|
|
* The widget will only be registered if the current blog is not |
308
|
|
|
* excluded in the configuration of the plugin. |
309
|
|
|
* @return boolean |
310
|
|
|
*/ |
311
|
|
|
public function init_widget() { |
312
|
|
|
if ( ! $this->options->is_excluded() ) { |
313
|
|
|
register_widget( MslsWidget::class ); |
314
|
|
|
|
315
|
|
|
return true; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
return false; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* Render widget output |
323
|
|
|
* |
324
|
|
|
* @return string |
325
|
|
|
*/ |
326
|
|
|
public function block_render() { |
327
|
|
|
if ( ! $this->init_widget() ) { |
328
|
|
|
return ''; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
ob_start(); |
332
|
|
|
the_widget( MslsWidget::class ); |
333
|
|
|
$output = ob_get_clean(); |
334
|
|
|
|
335
|
|
|
return $output; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* Load textdomain |
340
|
|
|
* |
341
|
|
|
* The method should be executed always on init because we have some |
342
|
|
|
* translatable string in the frontend too. |
343
|
|
|
* |
344
|
|
|
* @return boolean |
345
|
|
|
*/ |
346
|
|
|
public function init_i18n_support() { |
347
|
|
|
return load_plugin_textdomain( 'multisite-language-switcher', false, self::dirname( '/languages/' ) ); |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* Message handler |
352
|
|
|
* |
353
|
|
|
* Prints a message box to the screen. |
354
|
|
|
* |
355
|
|
|
* @param string $message |
356
|
|
|
* @param string $css_class |
357
|
|
|
* |
358
|
|
|
* @return boolean |
359
|
|
|
*/ |
360
|
|
|
public static function message_handler( $message, $css_class = 'error' ) { |
361
|
|
|
if ( ! empty( $message ) ) { |
362
|
|
|
printf( '<div id="msls-warning" class="%s"><p>%s</p></div>', $css_class, $message ); |
363
|
|
|
|
364
|
|
|
return true; |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
return false; |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
/** |
371
|
|
|
* Activate plugin |
372
|
|
|
*/ |
373
|
|
|
public static function activate() { |
374
|
|
|
register_uninstall_hook( self::file(), [ __CLASS__, 'uninstall' ] ); |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* Uninstall plugin |
379
|
|
|
* |
380
|
|
|
* The plugin data in all blogs of the current network will be |
381
|
|
|
* deleted after the uninstall procedure. |
382
|
|
|
* |
383
|
|
|
* @return boolean |
384
|
|
|
*/ |
385
|
|
|
public static function uninstall() { |
386
|
|
|
/** |
387
|
|
|
* We want to be sure that the user has not deactivated the |
388
|
|
|
* multisite because we need to use switch_to_blog and |
389
|
|
|
* restore_current_blog |
390
|
|
|
*/ |
391
|
|
|
if ( function_exists( 'is_multisite' ) && is_multisite() ) { |
392
|
|
|
$cache = MslsSqlCacher::init( __CLASS__ )->set_params( __METHOD__ ); |
393
|
|
|
|
394
|
|
|
$blogs = $cache->get_results( |
395
|
|
|
$cache->prepare( |
396
|
|
|
"SELECT blog_id FROM {$cache->blogs} WHERE blog_id != %d AND site_id = %d", |
397
|
|
|
$cache->blogid, |
398
|
|
|
$cache->siteid |
399
|
|
|
) |
400
|
|
|
); |
401
|
|
|
|
402
|
|
|
foreach ( $blogs as $blog ) { |
403
|
|
|
switch_to_blog( $blog->blog_id ); |
404
|
|
|
self::cleanup(); |
405
|
|
|
restore_current_blog(); |
406
|
|
|
} |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
return self::cleanup(); |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
/** |
413
|
|
|
* Cleanup the options |
414
|
|
|
* |
415
|
|
|
* Removes all values of the current blogs which are stored in the |
416
|
|
|
* options-table and returns true if it was successful. |
417
|
|
|
* |
418
|
|
|
* @return boolean |
419
|
|
|
*/ |
420
|
|
|
public static function cleanup() { |
421
|
|
|
if ( delete_option( 'msls' ) ) { |
422
|
|
|
$cache = MslsSqlCacher::init( __CLASS__ )->set_params( __METHOD__ ); |
423
|
|
|
$sql = $cache->prepare( |
424
|
|
|
"DELETE FROM {$cache->options} WHERE option_name LIKE %s", |
425
|
|
|
'msls_%' |
426
|
|
|
); |
427
|
|
|
|
428
|
|
|
return (bool) $cache->query( $sql ); |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
return false; |
432
|
|
|
} |
433
|
|
|
|
434
|
|
|
/** |
435
|
|
|
* Get specific vars from $_POST and $_GET in a safe way |
436
|
|
|
* |
437
|
|
|
* @param array $list |
438
|
|
|
* |
439
|
|
|
* @return array |
440
|
|
|
*/ |
441
|
|
|
public function get_superglobals( array $list ) { |
442
|
|
|
$arr = []; |
443
|
|
|
|
444
|
|
|
foreach ( $list as $var ) { |
445
|
|
|
$arr[ $var ] = ''; |
446
|
|
|
|
447
|
|
|
if ( filter_has_var( INPUT_POST, $var ) ) { |
448
|
|
|
$arr[ $var ] = filter_input( INPUT_POST, $var ); |
449
|
|
|
} elseif ( filter_has_var( INPUT_GET, $var ) ) { |
450
|
|
|
$arr[ $var ] = filter_input( INPUT_GET, $var ); |
451
|
|
|
} |
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
return $arr; |
455
|
|
|
} |
456
|
|
|
|
457
|
|
|
} |
458
|
|
|
|