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