Completed
Push — develop ( 3994fc...e73ae0 )
by Marco
01:05
created

elementor()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 3
nc 2
nop 0
dl 0
loc 5
rs 9.2
c 0
b 0
f 0
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
		 * Return the single class instance
29
		 *
30
		 * @param string[] $plugins
31
		 * @return object
32
		 * @since 2.0.0
33
		 */
34
		public static function instance( $plugins = array() ) {
35
			if ( is_null( self::$_instance ) ) {
36
				self::$_instance = new self( $plugins );
37
			}
38
			return self::$_instance;
39
		}
40
41
		/**
42
		 * Class constructor
43
		 *
44
		 * @param string[] $plugins
45
		 * @since 2.0.0
46
		 */
47
		protected function __construct( $plugins ) {
48
			foreach ( $plugins as $plugin ) {
49
				if ( is_callable( array( $this, $plugin ), false ) ) {
50
					$this->$plugin();
51
				}
52
			}
53
			if ( ! function_exists( 'is_plugin_active' ) ) {
54
				include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
55
			}
56
		}
57
58
		/**
59
		 * Prevent the class from being cloned
60
		 *
61
		 * @return void
62
		 * @since 2.0.0
63
		 */
64
		protected function __clone() {
65
			_doing_it_wrong( __FUNCTION__, __( 'Cheatin&#8217; uh?' ), '2.0' );
66
		}
67
68
		/**
69
		 * Compatibility with WPML
70
		 *
71
		 * @uses add_filter()
72
		 *
73
		 * @return void
74
		 * @since 2.0.0
75
		 */
76
		public function wpml() {
77
			add_action( 'init', array( $this, 'wpml_init' ) );
78
			add_action( 'black_studio_tinymce_before_widget', array( $this, 'wpml_widget_before' ), 10, 2 );
79
			add_action( 'black_studio_tinymce_after_widget', array( $this, 'wpml_widget_after' ), 10, 2 );
80
			add_filter( 'black_studio_tinymce_widget_update', array( $this, 'wpml_widget_update' ), 10, 2 );
81
			add_filter( 'widget_text', array( $this, 'wpml_widget_text' ), 2, 3 );
82
		}
83
84
		/**
85
		 * Initialize compatibility with WPML and WPML Widgets plugins
86
		 *
87
		 * @uses is_plugin_active()
88
		 * @uses has_action()
89
		 * @uses remove_action()
90
		 *
91
		 * @return void
92
		 * @since 2.3.1
93
		 */
94
		public function wpml_init() {
95
			if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) && is_plugin_active( 'wpml-widgets/wpml-widgets.php' ) ) {
96
				if ( false !== has_action( 'update_option_widget_black-studio-tinymce', 'icl_st_update_widget_title_actions' ) ) {
97
					remove_action( 'update_option_widget_black-studio-tinymce', 'icl_st_update_widget_title_actions', 5 );
98
				}
99
			}
100
		}
101
102
		/**
103
		 * Disable WPML String translation native behavior
104
		 *
105
		 * @uses remove_filter()
106
		 *
107
		 * @param mixed[] $args
108
		 * @param mixed[] $instance
109
		 * @return void
110
		 * @since 2.3.0
111
		 */
112
		public function wpml_widget_before( $args, $instance ) {
113
			if( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) ) {
0 ignored issues
show
introduced by Marco Chiesi
Space after opening control structure is required
Loading history...
introduced by Marco Chiesi
No space before opening parenthesis is prohibited
Loading history...
114
				// Avoid native WPML string translation of widget titles 
115
				// For widgets inserted in pages built with Page Builder (SiteOrigin panels) and also when WPML Widgets is active
116
				if ( false !== has_filter( 'widget_title', 'icl_sw_filters_widget_title' ) ) {
117
					if ( isset( $instance['panels_info'] ) || is_plugin_active( 'wpml-widgets/wpml-widgets.php' ) ) {
118
						remove_filter( 'widget_title', 'icl_sw_filters_widget_title', 0 );
119
					}
120
				}
121
				// Avoid native WPML string translation of widget texts (for all widgets) 
122
				// Black Studio TinyMCE Widget already supports WPML string translation, so this is needed to prevent duplicate translations
123
				if ( false !== has_filter( 'widget_text', 'icl_sw_filters_widget_text' ) ) {
124
					remove_filter( 'widget_text', 'icl_sw_filters_widget_text', 0 );
125
				}
126
			}
127
			
128
		}
129
130
		/**
131
		 * Re-Enable WPML String translation native behavior
132
		 *
133
		 * @uses add_filter()
134
		 *
135
		 * @param mixed[] $args
136
		 * @param mixed[] $instance
137
		 * @return void
138
		 * @since 2.3.0
139
		 */
140
		public function wpml_widget_after( $args, $instance ) {
141
			if( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) ) {
0 ignored issues
show
introduced by Marco Chiesi
Space after opening control structure is required
Loading history...
introduced by Marco Chiesi
No space before opening parenthesis is prohibited
Loading history...
142
				if ( false === has_filter( 'widget_title', 'icl_sw_filters_widget_title' ) && function_exists( 'icl_sw_filters_widget_title' ) ) {
143
					if ( isset( $instance['panels_info'] ) || is_plugin_active( 'wpml-widgets/wpml-widgets.php' ) ) {
144
						add_filter( 'widget_title', 'icl_sw_filters_widget_title', 0 );
145
					}
146
				}
147
				if ( false === has_filter( 'widget_text', 'icl_sw_filters_widget_text' ) && function_exists( 'icl_sw_filters_widget_text' ) ) {
148
					add_filter( 'widget_text', 'icl_sw_filters_widget_text', 0 );
149
				}
150
			}
151
		}
152
153
		/**
154
		 * Add widget text to WPML String translation
155
		 *
156
		 * @uses is_plugin_active()
157
		 * @uses icl_register_string() Part of WPML
158
		 *
159
		 * @param mixed[] $instance
160
		 * @param object $widget
161
		 * @return mixed[]
162
		 * @since 2.0.0
163
		 */
164
		public function wpml_widget_update( $instance, $widget ) {
165
			if( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) && ! is_plugin_active( 'wpml-widgets/wpml-widgets.php' ) ) {
0 ignored issues
show
introduced by Marco Chiesi
Space after opening control structure is required
Loading history...
introduced by Marco Chiesi
No space before opening parenthesis is prohibited
Loading history...
166
				if ( function_exists( 'icl_register_string' ) && ! empty( $widget->number ) ) {
167
					if ( ! isset( $instance['panels_info'] ) ) { // Avoid translation of Page Builder (SiteOrigin panels) widgets
168
						icl_register_string( 'Widgets', 'widget body - ' . $widget->id_base . '-' . $widget->number, $instance['text'] );
169
					}
170
				}
171
			}
172
			return $instance;
173
		}
174
175
		/**
176
		 * Translate widget text
177
		 *
178
		 * @uses is_plugin_active()
179
		 * @uses icl_t() Part of WPML
180
		 *
181
		 * @param string $text
182
		 * @param mixed[]|null $instance
183
		 * @param object|null $widget
184
		 * @return string
185
		 * @since 2.0.0
186
		 */
187
		public function wpml_widget_text( $text, $instance = null, $widget = null ) {
188
			if( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) && ! is_plugin_active( 'wpml-widgets/wpml-widgets.php' ) ) {
0 ignored issues
show
introduced by Marco Chiesi
Space after opening control structure is required
Loading history...
introduced by Marco Chiesi
No space before opening parenthesis is prohibited
Loading history...
189
				if ( bstw()->check_widget( $widget ) && ! empty( $instance ) ) {
190
					if ( function_exists( 'icl_t' ) ) {
191
						// Avoid translation of Page Builder (SiteOrigin panels) widgets
192
						if ( ! isset( $instance['panels_info'] ) ) { 
193
							$text = icl_t( 'Widgets', 'widget body - ' . $widget->id_base . '-' . $widget->number, $text );
194
						}
195
					}
196
				}
197
			}
198
			return $text;
199
		}
200
201
		/**
202
		 * Compatibility for WP Page Widget plugin
203
		 *
204
		 * @uses add_action()
205
		 *
206
		 * @return void
207
		 * @since 2.0.0
208
		 */
209
		public function wp_page_widget() {
210
			add_action( 'admin_init', array( $this, 'wp_page_widget_admin_init' ) );
211
		}
212
213
		/**
214
		 * Initialize compatibility for WP Page Widget plugin (only for WordPress 3.3+)
215
		 *
216
		 * @uses add_filter()
217
		 * @uses add_action()
218
		 * @uses is_plugin_active()
219
		 * @uses get_bloginfo()
220
		 *
221
		 * @return void
222
		 * @since 2.0.0
223
		 */
224
		public function wp_page_widget_admin_init() {
225
			if ( is_admin() && is_plugin_active( 'wp-page-widget/wp-page-widgets.php' ) && version_compare( get_bloginfo( 'version' ), '3.3', '>=' ) ) {
226
				add_filter( 'black_studio_tinymce_enable_pages', array( $this, 'wp_page_widget_enable_pages' ) );
227
				add_action( 'admin_print_scripts', array( $this, 'wp_page_widget_enqueue_script' ) );
228
			}
229
		}
230
231
		/**
232
		 * Enable filter for WP Page Widget plugin
233
		 *
234
		 * @param string[] $pages
235
		 * @return string[]
236
		 * @since 2.0.0
237
		 */
238
		public function wp_page_widget_enable_pages( $pages ) {
239
			$pages[] = 'post-new.php';
240
			$pages[] = 'post.php';
241
			if ( isset( $_GET['action'] ) && 'edit' == $_GET['action'] ) {
242
				$pages[] = 'edit-tags.php';
243
			}
244
			if ( isset( $_GET['page'] ) && in_array( $_GET['page'], array( 'pw-front-page', 'pw-search-page' ) ) ) {
245
				$pages[] = 'admin.php';
246
			}
247
			return $pages;
248
		}
249
250
		/**
251
		 * Enqueue script for WP Page Widget plugin
252
		 *
253
		 * @uses apply_filters()
254
		 * @uses wp_enqueue_script()
255
		 * @uses plugins_url()
256
		 * @uses SCRIPT_DEBUG
257
		 *
258
		 * @return void
259
		 * @since 2.0.0
260
		 */
261
		public function wp_page_widget_enqueue_script() {
262
			$main_script = apply_filters( 'black-studio-tinymce-widget-script', 'black-studio-tinymce-widget' );
263
			$compat_script = 'wp-page-widget';
264
			$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
265
			wp_enqueue_script(
266
				$compat_script,
267
				plugins_url( 'js/' . $compat_script . $suffix . '.js', dirname( __FILE__ ) ),
268
				array( 'jquery', 'editor', 'quicktags', $main_script ),
269
				bstw()->get_version(),
270
				true
271
			);
272
		}
273
274
		/**
275
		 * Compatibility with Page Builder (SiteOrigin Panels)
276
		 *
277
		 * @uses add_action()
278
		 *
279
		 * @return void
280
		 * @since 2.0.0
281
		 */
282
		public function siteorigin_panels() {
283
			add_action( 'admin_init', array( $this, 'siteorigin_panels_disable_compat' ), 7 );
284
			add_action( 'admin_init', array( $this, 'siteorigin_panels_admin_init' ) );
285
		}
286
287
		/**
288
		 * Initialize compatibility for Page Builder (SiteOrigin Panels)
289
		 *
290
		 * @uses add_filter()
291
		 * @uses add_action()
292
		 * @uses remove_filter()
293
		 * @uses add_action()
294
		 * @uses is_plugin_active()
295
		 *
296
		 * @return void
297
		 * @since 2.0.0
298
		 */
299
		public function siteorigin_panels_admin_init() {
300
			if ( is_admin() && is_plugin_active( 'siteorigin-panels/siteorigin-panels.php' ) ) {
301
				add_filter( 'siteorigin_panels_widget_object', array( $this, 'siteorigin_panels_widget_object' ), 10 );
302
				add_filter( 'black_studio_tinymce_container_selectors', array( $this, 'siteorigin_panels_container_selectors' ) );
303
				add_filter( 'black_studio_tinymce_activate_events', array( $this, 'siteorigin_panels_activate_events' ) );
304
				add_filter( 'black_studio_tinymce_deactivate_events', array( $this, 'siteorigin_panels_deactivate_events' ) );
305
				add_filter( 'black_studio_tinymce_enable_pages', array( $this, 'siteorigin_panels_enable_pages' ) );
306
				remove_filter( 'widget_text', array( bstw()->text_filters(), 'wpautop' ), 8 );
307
			}
308
		}
309
310
		/**
311
		 * Remove widget number to prevent translation when using Page Builder (SiteOrigin Panels) + WPML String Translation
312
		 *
313
		 * @param object $the_widget
314
		 * @return object
315
		 * @since 2.0.0
316
		 */
317
		public function siteorigin_panels_widget_object( $the_widget ) {
318
			if ( isset( $the_widget->id_base ) && 'black-studio-tinymce' == $the_widget->id_base ) {
319
				$the_widget->number = '';
320
			}
321
			return $the_widget;
322
		}
323
324
		/**
325
		 * Add selector for widget detection for Page Builder (SiteOrigin Panels)
326
		 *
327
		 * @param string[] $selectors
328
		 * @return string[]
329
		 * @since 2.0.0
330
		 */
331
		public function siteorigin_panels_container_selectors( $selectors ) {
332
			$selectors[] = 'div.panel-dialog';
333
			return $selectors;
334
		}
335
336
		/**
337
		 * Add activate events for Page Builder (SiteOrigin Panels)
338
		 *
339
		 * @param string[] $events
340
		 * @return string[]
341
		 * @since 2.0.0
342
		 */
343
		public function siteorigin_panels_activate_events( $events ) {
344
			$events[] = 'panelsopen';
345
			return $events;
346
		}
347
348
		/**
349
		 * Add deactivate events for Page Builder (SiteOrigin Panels)
350
		 *
351
		 * @param string[] $events
352
		 * @return string[]
353
		 * @since 2.0.0
354
		 */
355
		public function siteorigin_panels_deactivate_events( $events ) {
356
			$events[] = 'panelsdone';
357
			return $events;
358
		}
359
360
		/**
361
		 * Add pages filter to enable editor for Page Builder (SiteOrigin Panels)
362
		 *
363
		 * @param string[] $pages
364
		 * @return string[]
365
		 * @since 2.0.0
366
		 */
367
		public function siteorigin_panels_enable_pages( $pages ) {
368
			$pages[] = 'post-new.php';
369
			$pages[] = 'post.php';
370
			if ( isset( $_GET['page'] ) && 'so_panels_home_page' == $_GET['page'] ) {
371
				$pages[] = 'themes.php';
372
			}
373
			return $pages;
374
		}
375
376
		/**
377
		 * Disable old compatibility code provided by Page Builder (SiteOrigin Panels)
378
		 *
379
		 * @return void
380
		 * @since 2.0.0
381
		 */
382
		public function siteorigin_panels_disable_compat( ) {
383
			remove_action( 'admin_init', 'siteorigin_panels_black_studio_tinymce_admin_init' );
384
			remove_action( 'admin_enqueue_scripts', 'siteorigin_panels_black_studio_tinymce_admin_enqueue', 15 );
385
		}
386
387
		/**
388
		 * Compatibility with Jetpack After the deadline
389
		 *
390
		 * @uses add_action()
391
		 *
392
		 * @return void
393
		 * @since 2.0.0
394
		 */
395
		public function jetpack_after_the_deadline() {
396
			add_action( 'black_studio_tinymce_load', array( $this, 'jetpack_after_the_deadline_load' ) );
397
		}
398
399
		/**
400
		 * Load Jetpack After the deadline scripts
401
		 *
402
		 * @uses add_filter()
403
		 *
404
		 * @return void
405
		 * @since 2.0.0
406
		 */
407
		public function jetpack_after_the_deadline_load() {
408
			add_filter( 'atd_load_scripts', '__return_true' );
409
		}
410
411
		/**
412
		 * Compatibility for Elementor plugin
413
		 *
414
		 * @uses add_filter()
415
		 *
416
		 * @return void
417
		 * @since 2.5.0
418
		 */
419
		public function elementor() {
420
			if ( is_admin() && isset( $_GET['action'] ) && 'elementor' == $_GET['action'] ) {
2 ignored issues
show
introduced by Marco Chiesi
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by Marco Chiesi
Detected usage of a non-sanitized input variable: $_GET
Loading history...
421
				add_filter( 'black_studio_tinymce_enable', '__return_false', 100 );
422
			}
423
		}
424
425
	} // END class Black_Studio_TinyMCE_Compatibility_Plugins
426
427
} // END class_exists check
428