Completed
Push — develop ( 09af60...eeb504 )
by Marco
01:23
created

includes/class-compatibility-plugins.php (2 issues)

parameters are used.

Unused Code Minor

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
// Exit if accessed directly
4
if ( ! defined( 'ABSPATH' ) ) {
5
	exit;
6
}
7
8
/**
9
 * Class that provides compatibility code with other plugins
10
 *
11
 * @package Black_Studio_TinyMCE_Widget
12
 * @since 2.0.0
13
 */
14
15
if ( ! class_exists( 'Black_Studio_TinyMCE_Compatibility_Plugins' ) ) {
16
17
	final class Black_Studio_TinyMCE_Compatibility_Plugins {
18
19
		/**
20
		 * The single instance of the class
21
		 *
22
		 * @var object
23
		 * @since 2.0.0
24
		 */
25
		protected static $_instance = null;
26
27
		/**
28
		 * Flag to keep track of removed WPML filter on widget title
29
		 *
30
		 * @var boolean
31
		 * @since 2.6.1
32
		 */
33
		private $wpml_removed_widget_title_filter = false;
34
35
		/**
36
		 * Flag to keep track of removed WPML filter on widget text
37
		 *
38
		 * @var boolean
39
		 * @since 2.6.1
40
		 */
41
		private $wpml_removed_widget_text_filter = false;
42
43
		/**
44
		 * Return the single class instance
45
		 *
46
		 * @param string[] $plugins
47
		 * @return object
48
		 * @since 2.0.0
49
		 */
50
		public static function instance( $plugins = array() ) {
51
			if ( is_null( self::$_instance ) ) {
52
				self::$_instance = new self( $plugins );
53
			}
54
			return self::$_instance;
55
		}
56
57
		/**
58
		 * Class constructor
59
		 *
60
		 * @param string[] $plugins
61
		 * @since 2.0.0
62
		 */
63
		protected function __construct( $plugins ) {
64
			foreach ( $plugins as $plugin ) {
65
				if ( is_callable( array( $this, $plugin ), false ) ) {
66
					$this->$plugin();
67
				}
68
			}
69
			if ( ! function_exists( 'is_plugin_active' ) ) {
70
				include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
71
			}
72
		}
73
74
		/**
75
		 * Prevent the class from being cloned
76
		 *
77
		 * @return void
78
		 * @since 2.0.0
79
		 */
80
		protected function __clone() {
81
			_doing_it_wrong( __FUNCTION__, __( 'Cheatin&#8217; uh?' ), '2.0' );
82
		}
83
84
		/**
85
		 * Compatibility with WPML
86
		 *
87
		 * @uses add_filter()
88
		 *
89
		 * @return void
90
		 * @since 2.0.0
91
		 */
92
		public function wpml() {
93
			add_action( 'plugins_loaded', array( $this, 'wpml_init', 20 ) );
94
			add_action( 'black_studio_tinymce_before_widget', array( $this, 'wpml_widget_before' ), 10, 2 );
95
			add_action( 'black_studio_tinymce_after_widget', array( $this, 'wpml_widget_after' ), 10, 2 );
96
			add_filter( 'black_studio_tinymce_widget_update', array( $this, 'wpml_widget_update' ), 10, 2 );
97
			add_action( 'black_studio_tinymce_before_editor', array( $this, 'wpml_check_deprecated_translations' ), 5, 2 );
98
			add_filter( 'widget_text', array( $this, 'wpml_widget_text' ), 2, 3 );
99
		}
100
101
		/**
102
		 * Helper function to get WPML version
103
		 *
104
		 * @uses get_plugin_data()
105
		 *
106
		 * @return string
107
		 * @since 2.6.0
108
		 */
109
		public function wpml_get_version() {
110
			$wpml_data = get_plugin_data( WP_PLUGIN_DIR . '/sitepress-multilingual-cms/sitepress.php', false, false );
111
			return $wpml_data['Version'];
112
		}
113
114
		/**
115
		 * Initialize compatibility with WPML and WPML Widgets plugins
116
		 *
117
		 * @uses is_plugin_active()
118
		 * @uses has_action()
119
		 * @uses remove_action()
120
		 *
121
		 * @return void
122
		 * @since 2.3.1
123
		 */
124
		public function wpml_init() {
125
			if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) && is_plugin_active( 'wpml-widgets/wpml-widgets.php' ) ) {
126
				if ( false !== has_action( 'update_option_widget_black-studio-tinymce', 'icl_st_update_widget_title_actions' ) ) {
127
					remove_action( 'update_option_widget_black-studio-tinymce', 'icl_st_update_widget_title_actions', 5 );
128
				}
129
			}
130
		}
131
132
		/**
133
		 * Disable WPML String translation native behavior
134
		 *
135
		 * @uses remove_filter()
136
		 *
137
		 * @param mixed[] $args
138
		 * @param mixed[] $instance
139
		 * @return void
140
		 * @since 2.3.0
141
		 */
142
		public function wpml_widget_before( $args, $instance ) {
143
			if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) ) {
144
				// Avoid native WPML string translation of widget titles
145
				// for widgets inserted in pages built with Page Builder (SiteOrigin panels)
146
				// and also when WPML Widgets is active and for WPML versions from 3.8.0 on
147
				if ( false !== has_filter( 'widget_title', 'icl_sw_filters_widget_title' ) ) {
148
					if ( isset( $instance['panels_info'] ) || isset( $instance['wp_page_widget'] ) || is_plugin_active( 'wpml-widgets/wpml-widgets.php' ) || version_compare( $this->wpml_get_version(), '3.8.0' ) >= 0 ) {
149
						remove_filter( 'widget_title', 'icl_sw_filters_widget_title', 0 );
150
						$this->wpml_removed_widget_title_filter = true;
151
					}
152
				}
153
				// Avoid native WPML string translation of widget texts (for all widgets)
154
				// Note: Black Studio TinyMCE Widget already supports WPML string translation,
155
				// so this is needed to prevent duplicate translations
156
				if ( false !== has_filter( 'widget_text', 'icl_sw_filters_widget_text' ) ) {
157
					remove_filter( 'widget_text', 'icl_sw_filters_widget_text', 0 );
158
					$this->wpml_removed_widget_text_filter = true;
159
				}
160
			}
161
162
		}
163
164
		/**
165
		 * Re-Enable WPML String translation native behavior
166
		 *
167
		 * @uses add_filter()
168
		 *
169
		 * @param mixed[] $args
170
		 * @param mixed[] $instance
171
		 * @return void
172
		 * @since 2.3.0
173
		 */
174
		public function wpml_widget_after( $args, $instance ) {
0 ignored issues
show
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The parameter $instance is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
175
			if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) ) {
176
				// Restore widget title's native WPML string translation filter if it was removed
177
				if ( $this->wpml_removed_widget_title_filter ) {
178
					if ( false === has_filter( 'widget_title', 'icl_sw_filters_widget_title' ) && function_exists( 'icl_sw_filters_widget_title' ) ) {
179
						add_filter( 'widget_title', 'icl_sw_filters_widget_title', 0 );
180
						$this->wpml_removed_widget_title_filter = false;
181
					}
182
				}
183
				// Restore widget text's native WPML string translation filter if it was removed
184
				if ( $this->wpml_removed_widget_text_filter ) {
185
					if ( false === has_filter( 'widget_text', 'icl_sw_filters_widget_text' ) && function_exists( 'icl_sw_filters_widget_text' ) ) {
186
						add_filter( 'widget_text', 'icl_sw_filters_widget_text', 0 );
187
						$this->wpml_removed_widget_text_filter = false;
188
					}
189
				}
190
			}
191
		}
192
193
		/**
194
		 * Add widget text to WPML String translation
195
		 *
196
		 * @uses is_plugin_active()
197
		 * @uses icl_register_string() Part of WPML
198
		 *
199
		 * @param mixed[] $instance
200
		 * @param object $widget
201
		 * @return mixed[]
202
		 * @since 2.0.0
203
		 */
204
		public function wpml_widget_update( $instance, $widget ) {
205
			if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) &&
206
				 version_compare( $this->wpml_get_version(), '3.8.0' ) < 0 &&
207
				 ! is_plugin_active( 'wpml-widgets/wpml-widgets.php' )
208
			) {
209
				if ( function_exists( 'icl_register_string' ) && ! empty( $widget->number ) ) {
210
					// Avoid translation of Page Builder (SiteOrigin panels) and WP Page Widget widgets
211
					if ( ! isset( $instance['panels_info'] ) && ! isset( $instance['wp_page_widget'] ) ) {
212
						icl_register_string( 'Widgets', 'widget body - ' . $widget->id_base . '-' . $widget->number, $instance['text'] );
213
					}
214
				}
215
			}
216
			return $instance;
217
		}
218
219
		/**
220
		 * Translate widget text
221
		 *
222
		 * @uses is_plugin_active()
223
		 * @uses icl_t() Part of WPML
224
		 * @uses icl_st_is_registered_string() Part of WPML
225
		 *
226
		 * @param string $text
227
		 * @param mixed[]|null $instance
228
		 * @param object|null $widget
229
		 * @return string
230
		 * @since 2.0.0
231
		 */
232
		public function wpml_widget_text( $text, $instance = null, $widget = null ) {
233
			if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) && ! is_plugin_active( 'wpml-widgets/wpml-widgets.php' ) ) {
234
				if ( bstw()->check_widget( $widget ) && ! empty( $instance ) ) {
235
					if ( function_exists( 'icl_t' ) && function_exists( 'icl_st_is_registered_string' ) ) {
236
						// Avoid translation of Page Builder (SiteOrigin panels) and WP Page Widget widgets
237
						if ( ! isset( $instance['panels_info'] ) && ! isset( $instance['wp_page_widget'] ) ) {
238
							if ( icl_st_is_registered_string( 'Widgets', 'widget body - ' . $widget->id_base . '-' . $widget->number ) ) {
239
								$text = icl_t( 'Widgets', 'widget body - ' . $widget->id_base . '-' . $widget->number, $text );
240
							}
241
						}
242
					}
243
				}
244
			}
245
			return $text;
246
		}
247
248
		/**
249
		 * Check for existing deprecated translations (made with WPML String Translations plugin) and display warning
250
		 *
251
		 * @uses is_plugin_active()
252
		 * @uses icl_st_is_registered_string() Part of WPML
253
		 * @uses admin_url()
254
		 *
255
		 * @param mixed[]|null $instance
256
		 * @param object|null $widget
257
		 * @return void
258
		 * @since 2.6.0
259
		 */
260
		public function wpml_check_deprecated_translations( $instance, $widget ) {
261
			if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) && version_compare( $this->wpml_get_version(), '3.8.0' ) >= 0 ) {
262
				if ( function_exists( 'icl_st_is_registered_string' ) ) {
263
					if ( icl_st_is_registered_string( 'Widgets', 'widget body - ' . $widget->id_base . '-' . $widget->number ) ) {
264
						$wpml_st_url = admin_url( 'admin.php?page=wpml-string-translation%2Fmenu%2Fstring-translation.php&context=Widgets' );
265
						echo '<div class="notice notice-warning"><p>';
266
						/* translators: Warning displayed when deprecated translations of the current widget are detected */
267
						echo sprintf( __( 'WARNING: This widget has one or more translations made using WPML String Translation plugin, which is now a deprecated method of translating widgets, in favor of the "Display on language" dropdown introduced with WPML 3.8. Please migrate your existing translations by creating new widgets and selecting the language of this widget and the new ones accordingly. Finally delete the existing translations from <a href="%s">WPML String Translation interface</a>.', 'black-studio-tinymce-widget' ), esc_url( $wpml_st_url ) );
268
						echo '</p></div>';
269
					}
270
				}
271
			}
272
		}
273
274
		/**
275
		 * Compatibility for WP Page Widget plugin
276
		 *
277
		 * @uses add_action()
278
		 *
279
		 * @return void
280
		 * @since 2.0.0
281
		 */
282
		public function wp_page_widget() {
283
			add_action( 'init', array( $this, 'wp_page_widget_init' ), 0 );
284
		}
285
286
		/**
287
		 * Initialize compatibility for WP Page Widget plugin (only for WordPress 3.3+)
288
		 *
289
		 * @uses add_filter()
290
		 * @uses add_action()
291
		 * @uses is_plugin_active()
292
		 * @uses get_bloginfo()
293
		 *
294
		 * @return void
295
		 * @since 2.0.0
296
		 */
297
		public function wp_page_widget_init() {
298
			if ( is_admin() && is_plugin_active( 'wp-page-widget/wp-page-widgets.php' ) && version_compare( get_bloginfo( 'version' ), '3.3', '>=' ) ) {
299
				add_filter( 'black_studio_tinymce_enable_pages', array( $this, 'wp_page_widget_enable_pages' ) );
300
				add_action( 'admin_print_scripts', array( $this, 'wp_page_widget_enqueue_script' ) );
301
				add_filter( 'black_studio_tinymce_widget_update', array( $this, 'wp_page_widget_add_data' ), 10, 2 );
302
			}
303
		}
304
305
		/**
306
		 * Enable filter for WP Page Widget plugin
307
		 *
308
		 * @param string[] $pages
309
		 * @return string[]
310
		 * @since 2.0.0
311
		 */
312
		public function wp_page_widget_enable_pages( $pages ) {
313
			$pages[] = 'post-new.php';
314
			$pages[] = 'post.php';
315
			if ( isset( $_GET['action'] ) && 'edit' == $_GET['action'] ) {
316
				$pages[] = 'edit-tags.php';
317
			}
318
			if ( isset( $_GET['page'] ) && in_array( $_GET['page'], array( 'pw-front-page', 'pw-search-page' ) ) ) {
319
				$pages[] = 'admin.php';
320
			}
321
			return $pages;
322
		}
323
324
		/**
325
		 * Add WP Page Widget marker
326
		 *
327
		 * @param mixed[] $instance
328
		 * @param object $widget
329
		 * @return mixed[]
330
		 * @since 2.5.0
331
		 */
332
		public function wp_page_widget_add_data( $instance, $widget ) {
333
			if ( bstw()->check_widget( $widget ) && ! empty( $instance ) ) {
334
				if ( isset( $_POST['action'] ) && 'pw-save-widget' == $_POST['action'] ) {
335
					$instance['wp_page_widget'] = true;
336
				}
337
			}
338
			return $instance;
339
		}
340
341
		/**
342
		 * Enqueue script for WP Page Widget plugin
343
		 *
344
		 * @uses apply_filters()
345
		 * @uses wp_enqueue_script()
346
		 * @uses plugins_url()
347
		 * @uses SCRIPT_DEBUG
348
		 *
349
		 * @return void
350
		 * @since 2.0.0
351
		 */
352
		public function wp_page_widget_enqueue_script() {
353
			$main_script = apply_filters( 'black-studio-tinymce-widget-script', 'black-studio-tinymce-widget' );
354
			$compat_script = 'wp-page-widget';
355
			$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
356
			wp_enqueue_script(
357
				$compat_script,
358
				plugins_url( 'js/' . $compat_script . $suffix . '.js', dirname( __FILE__ ) ),
359
				array( 'jquery', 'editor', 'quicktags', $main_script ),
360
				bstw()->get_version(),
361
				true
362
			);
363
		}
364
365
		/**
366
		 * Compatibility with Page Builder (SiteOrigin Panels)
367
		 *
368
		 * @uses add_action()
369
		 *
370
		 * @return void
371
		 * @since 2.0.0
372
		 */
373
		public function siteorigin_panels() {
374
			add_action( 'admin_init', array( $this, 'siteorigin_panels_disable_compat' ), 7 );
375
			add_action( 'admin_init', array( $this, 'siteorigin_panels_admin_init' ) );
376
		}
377
378
		/**
379
		 * Initialize compatibility for Page Builder (SiteOrigin Panels)
380
		 *
381
		 * @uses add_filter()
382
		 * @uses add_action()
383
		 * @uses remove_filter()
384
		 * @uses add_action()
385
		 * @uses is_plugin_active()
386
		 *
387
		 * @return void
388
		 * @since 2.0.0
389
		 */
390
		public function siteorigin_panels_admin_init() {
391
			if ( is_admin() && is_plugin_active( 'siteorigin-panels/siteorigin-panels.php' ) ) {
392
				add_filter( 'siteorigin_panels_widget_object', array( $this, 'siteorigin_panels_widget_object' ), 10 );
393
				add_filter( 'black_studio_tinymce_container_selectors', array( $this, 'siteorigin_panels_container_selectors' ) );
394
				add_filter( 'black_studio_tinymce_activate_events', array( $this, 'siteorigin_panels_activate_events' ) );
395
				add_filter( 'black_studio_tinymce_deactivate_events', array( $this, 'siteorigin_panels_deactivate_events' ) );
396
				add_filter( 'black_studio_tinymce_enable_pages', array( $this, 'siteorigin_panels_enable_pages' ) );
397
				add_filter( 'black_studio_tinymce_widget_additional_fields', array( $this, 'siteorigin_panels_additional_fields' ) );
398
				remove_filter( 'widget_text', array( bstw()->text_filters(), 'wpautop' ), 8 );
399
			}
400
		}
401
402
		/**
403
		 * Remove widget number to prevent translation when using Page Builder (SiteOrigin Panels) + WPML String Translation
404
		 *
405
		 * @param object $widget
406
		 * @return object
407
		 * @since 2.0.0
408
		 */
409
		public function siteorigin_panels_widget_object( $widget ) {
410
			if ( isset( $widget->id_base ) && 'black-studio-tinymce' == $widget->id_base ) {
411
				$widget->number = '';
412
			}
413
			return $widget;
414
		}
415
416
		/**
417
		 * Add selector for widget detection for Page Builder (SiteOrigin Panels)
418
		 *
419
		 * @param string[] $selectors
420
		 * @return string[]
421
		 * @since 2.0.0
422
		 */
423
		public function siteorigin_panels_container_selectors( $selectors ) {
424
			$selectors[] = 'div.panel-dialog';
425
			return $selectors;
426
		}
427
428
		/**
429
		 * Add activate events for Page Builder (SiteOrigin Panels)
430
		 *
431
		 * @param string[] $events
432
		 * @return string[]
433
		 * @since 2.0.0
434
		 */
435
		public function siteorigin_panels_activate_events( $events ) {
436
			$events[] = 'panelsopen';
437
			return $events;
438
		}
439
440
		/**
441
		 * Add deactivate events for Page Builder (SiteOrigin Panels)
442
		 *
443
		 * @param string[] $events
444
		 * @return string[]
445
		 * @since 2.0.0
446
		 */
447
		public function siteorigin_panels_deactivate_events( $events ) {
448
			$events[] = 'panelsdone';
449
			return $events;
450
		}
451
452
		/**
453
		 * Add pages filter to enable editor for Page Builder (SiteOrigin Panels)
454
		 *
455
		 * @param string[] $pages
456
		 * @return string[]
457
		 * @since 2.0.0
458
		 */
459
		public function siteorigin_panels_enable_pages( $pages ) {
460
			$pages[] = 'post-new.php';
461
			$pages[] = 'post.php';
462
			if ( isset( $_GET['page'] ) && 'so_panels_home_page' == $_GET['page'] ) {
463
				$pages[] = 'themes.php';
464
			}
465
			return $pages;
466
		}
467
468
		/**
469
		 * Add widget field for Page Builder (SiteOrigin Panels)
470
		 *
471
		 * @param string[] fields
472
		 * @return string[]
473
		 * @since 2.6.0
474
		 */
475
		public function siteorigin_panels_additional_fields( $fields ) {
476
			$fields[] = 'panels_info';
477
			return $fields;
478
		}
479
480
		/**
481
		 * Disable old compatibility code provided by Page Builder (SiteOrigin Panels)
482
		 *
483
		 * @return void
484
		 * @since 2.0.0
485
		 */
486
		public function siteorigin_panels_disable_compat( ) {
487
			remove_action( 'admin_init', 'siteorigin_panels_black_studio_tinymce_admin_init' );
488
			remove_action( 'admin_enqueue_scripts', 'siteorigin_panels_black_studio_tinymce_admin_enqueue', 15 );
489
		}
490
491
		/**
492
		 * Compatibility with Jetpack After the deadline
493
		 *
494
		 * @uses add_action()
495
		 *
496
		 * @return void
497
		 * @since 2.0.0
498
		 */
499
		public function jetpack_after_the_deadline() {
500
			add_action( 'black_studio_tinymce_load', array( $this, 'jetpack_after_the_deadline_load' ) );
501
		}
502
503
		/**
504
		 * Load Jetpack After the deadline scripts
505
		 *
506
		 * @uses add_filter()
507
		 *
508
		 * @return void
509
		 * @since 2.0.0
510
		 */
511
		public function jetpack_after_the_deadline_load() {
512
			add_filter( 'atd_load_scripts', '__return_true' );
513
		}
514
515
		/**
516
		 * Compatibility for Elementor plugin
517
		 *
518
		 * @uses add_filter()
519
		 *
520
		 * @return void
521
		 * @since 2.5.0
522
		 */
523
		public function elementor() {
524
			if ( is_admin() && isset( $_GET['action'] ) && 'elementor' == $_GET['action'] ) {
525
				add_filter( 'black_studio_tinymce_enable', '__return_false', 100 );
526
				add_action( 'widgets_init', array( $this, 'elementor_unregister_widget' ), 20 );
527
			}
528
		}
529
530
		/**
531
		 * Unregister Widget for Elementor plugin
532
		 *
533
		 * @uses unregister_widget()
534
		 *
535
		 * @return void
536
		 * @since 2.5.1
537
		 */
538
		public function elementor_unregister_widget() {
539
			unregister_widget( 'WP_Widget_Black_Studio_TinyMCE' );
540
		}
541
542
	} // END class Black_Studio_TinyMCE_Compatibility_Plugins
543
544
} // END class_exists check
545