Completed
Push — develop ( 5b1286...34711c )
by Marco
01:15
created

wpml_get_version()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 4
rs 10
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( 'plugins_loaded', array( $this, 'wpml_init', 20 ) );
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_action( 'black_studio_tinymce_before_editor', array( $this, 'wpml_check_deprecated_translations' ), 5, 2 );
82
			add_filter( 'widget_text', array( $this, 'wpml_widget_text' ), 2, 3 );
83
		}
84
85
		/**
86
		 * Helper function to get WPML version
87
		 * 
88
		 * @uses get_plugin_data()
89
		 * 
90
		 * @return string
91
		 * @since 2.5.2
92
		 */
93
		public function wpml_get_version() {
94
			$wpml_data = get_plugin_data( WP_PLUGIN_DIR . '/sitepress-multilingual-cms/sitepress.php', false, false );
95
			return $wpml_data['Version'];
96
		}
97
		
98
		/**
99
		 * Initialize compatibility with WPML and WPML Widgets plugins
100
		 *
101
		 * @uses is_plugin_active()
102
		 * @uses has_action()
103
		 * @uses remove_action()
104
		 *
105
		 * @return void
106
		 * @since 2.3.1
107
		 */
108
		public function wpml_init() {
109
			if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) && is_plugin_active( 'wpml-widgets/wpml-widgets.php' ) ) {
110
				if ( false !== has_action( 'update_option_widget_black-studio-tinymce', 'icl_st_update_widget_title_actions' ) ) {
111
					remove_action( 'update_option_widget_black-studio-tinymce', 'icl_st_update_widget_title_actions', 5 );
112
				}
113
			}
114
		}
115
116
		/**
117
		 * Disable WPML String translation native behavior
118
		 *
119
		 * @uses remove_filter()
120
		 *
121
		 * @param mixed[] $args
122
		 * @param mixed[] $instance
123
		 * @return void
124
		 * @since 2.3.0
125
		 */
126
		public function wpml_widget_before( $args, $instance ) {
127
			if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) ) {
128
				// Avoid native WPML string translation of widget titles 
129
				// For widgets inserted in pages built with Page Builder (SiteOrigin panels) and also when WPML Widgets is active
130
				if ( false !== has_filter( 'widget_title', 'icl_sw_filters_widget_title' ) ) {
131
					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 ) {
132
						remove_filter( 'widget_title', 'icl_sw_filters_widget_title', 0 );
133
					}
134
				}
135
				// Avoid native WPML string translation of widget texts (for all widgets) 
136
				// Black Studio TinyMCE Widget already supports WPML string translation, so this is needed to prevent duplicate translations
137
				if ( false !== has_filter( 'widget_text', 'icl_sw_filters_widget_text' ) ) {
138
					remove_filter( 'widget_text', 'icl_sw_filters_widget_text', 0 );
139
				}
140
			}
141
			
142
		}
143
144
		/**
145
		 * Re-Enable WPML String translation native behavior
146
		 *
147
		 * @uses add_filter()
148
		 *
149
		 * @param mixed[] $args
150
		 * @param mixed[] $instance
151
		 * @return void
152
		 * @since 2.3.0
153
		 */
154
		public function wpml_widget_after( $args, $instance ) {
155
			if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) ) {
156
				if ( false === has_filter( 'widget_title', 'icl_sw_filters_widget_title' ) && function_exists( 'icl_sw_filters_widget_title' ) ) {
157
					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 ) {
158
						add_filter( 'widget_title', 'icl_sw_filters_widget_title', 0 );
159
					}
160
				}
161
				if ( false === has_filter( 'widget_text', 'icl_sw_filters_widget_text' ) && function_exists( 'icl_sw_filters_widget_text' )  || version_compare( $this->wpml_get_version(), '3.8.0' ) >= 0 ) {
162
					add_filter( 'widget_text', 'icl_sw_filters_widget_text', 0 );
163
				}
164
			}
165
		}
166
167
		/**
168
		 * Add widget text to WPML String translation
169
		 *
170
		 * @uses is_plugin_active()
171
		 * @uses icl_register_string() Part of WPML
172
		 *
173
		 * @param mixed[] $instance
174
		 * @param object $widget
175
		 * @return mixed[]
176
		 * @since 2.0.0
177
		 */
178
		public function wpml_widget_update( $instance, $widget ) {
179
			if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) &&
180
				 version_compare( $this->wpml_get_version(), '3.8.0' ) < 0 &&
181
				 ! is_plugin_active( 'wpml-widgets/wpml-widgets.php' )
182
			) {
183
				if ( function_exists( 'icl_register_string' ) && ! empty( $widget->number ) ) {
184
					// Avoid translation of Page Builder (SiteOrigin panels) and WP Page Widget widgets
185
					if ( ! isset( $instance['panels_info'] ) && ! isset( $instance['wp_page_widget'] ) ) {
186
						icl_register_string( 'Widgets', 'widget body - ' . $widget->id_base . '-' . $widget->number, $instance['text'] );
187
					}
188
				}
189
			}
190
			return $instance;
191
		}
192
193
		/**
194
		 * Translate widget text
195
		 *
196
		 * @uses is_plugin_active()
197
		 * @uses icl_t() Part of WPML
198
		 * @uses icl_st_is_registered_string() Part of WPML
199
		 *
200
		 * @param string $text
201
		 * @param mixed[]|null $instance
202
		 * @param object|null $widget
203
		 * @return string
204
		 * @since 2.0.0
205
		 */
206
		public function wpml_widget_text( $text, $instance = null, $widget = null ) {
207
			if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) && ! is_plugin_active( 'wpml-widgets/wpml-widgets.php' ) ) {
208
				if ( bstw()->check_widget( $widget ) && ! empty( $instance ) ) {
209
					if ( function_exists( 'icl_t' ) && function_exists( 'icl_st_is_registered_string' ) ) {
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
							if ( icl_st_is_registered_string( 'Widgets', 'widget body - ' . $widget->id_base . '-' . $widget->number ) ) {
213
								$text = icl_t( 'Widgets', 'widget body - ' . $widget->id_base . '-' . $widget->number, $text );
214
							}
215
						}
216
					}
217
				}
218
			}
219
			return $text;
220
		}
221
		
222
		/**
223
		 * Check for existing deprecated translations (made with WPML String Translations plugin) and display warning
224
		 *
225
		 * @uses is_plugin_active()
226
		 * @uses icl_st_is_registered_string() Part of WPML
227
		 * @uses admin_url()
228
		 *
229
		 * @param mixed[]|null $instance
230
		 * @param object|null $widget
231
		 * @return void
232
		 * @since 2.5.2
233
		 */
234
		public function wpml_check_deprecated_translations( $instance, $widget ) {
235
			if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) && version_compare( $this->wpml_get_version(), '3.8.0' ) >= 0 ) {
236
				if ( function_exists( 'icl_st_is_registered_string' ) ) {
237
					if ( icl_st_is_registered_string( 'Widgets', 'widget body - ' . $widget->id_base . '-' . $widget->number ) ) {
238
						$wpml_st_url = admin_url( 'admin.php?page=wpml-string-translation%2Fmenu%2Fstring-translation.php&context=Widgets' );
239
						echo '<div class="notice notice-warning"><p>';
240
						/* translators: Warning displayed when deprecated translations of the current widget are detected */
241
						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' ), $wpml_st_url );
0 ignored issues
show
introduced by Marco Chiesi
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'sprintf'
Loading history...
242
						echo '</p></div>';
243
					}
244
				}
245
			}
246
		}
247
248
		/**
249
		 * Compatibility for WP Page Widget plugin
250
		 *
251
		 * @uses add_action()
252
		 *
253
		 * @return void
254
		 * @since 2.0.0
255
		 */
256
		public function wp_page_widget() {
257
			add_action( 'init', array( $this, 'wp_page_widget_init' ), 0 );
258
		}
259
260
		/**
261
		 * Initialize compatibility for WP Page Widget plugin (only for WordPress 3.3+)
262
		 *
263
		 * @uses add_filter()
264
		 * @uses add_action()
265
		 * @uses is_plugin_active()
266
		 * @uses get_bloginfo()
267
		 *
268
		 * @return void
269
		 * @since 2.0.0
270
		 */
271
		public function wp_page_widget_init() {
272
			if ( is_admin() && is_plugin_active( 'wp-page-widget/wp-page-widgets.php' ) && version_compare( get_bloginfo( 'version' ), '3.3', '>=' ) ) {
273
				add_filter( 'black_studio_tinymce_enable_pages', array( $this, 'wp_page_widget_enable_pages' ) );
274
				add_action( 'admin_print_scripts', array( $this, 'wp_page_widget_enqueue_script' ) );
275
				add_filter( 'black_studio_tinymce_widget_update', array( $this, 'wp_page_widget_add_data' ), 10, 2 );
276
			}
277
		}
278
279
		/**
280
		 * Enable filter for WP Page Widget plugin
281
		 *
282
		 * @param string[] $pages
283
		 * @return string[]
284
		 * @since 2.0.0
285
		 */
286
		public function wp_page_widget_enable_pages( $pages ) {
287
			$pages[] = 'post-new.php';
288
			$pages[] = 'post.php';
289
			if ( isset( $_GET['action'] ) && 'edit' == $_GET['action'] ) {
290
				$pages[] = 'edit-tags.php';
291
			}
292
			if ( isset( $_GET['page'] ) && in_array( $_GET['page'], array( 'pw-front-page', 'pw-search-page' ) ) ) {
293
				$pages[] = 'admin.php';
294
			}
295
			return $pages;
296
		}
297
298
		/**
299
		 * Add WP Page Widget marker
300
		 *
301
		 * @param mixed[] $instance
302
		 * @param object $widget
303
		 * @return mixed[]
304
		 * @since 2.5.0
305
		 */
306
		public function wp_page_widget_add_data( $instance, $widget ) {
307
			if ( bstw()->check_widget( $widget ) && ! empty( $instance ) ) {
308
				if ( isset( $_POST['action'] ) && 'pw-save-widget' == $_POST['action'] ) {
309
					$instance['wp_page_widget'] = true;
310
				}
311
			}
312
			return $instance;
313
		}
314
315
		/**
316
		 * Enqueue script for WP Page Widget plugin
317
		 *
318
		 * @uses apply_filters()
319
		 * @uses wp_enqueue_script()
320
		 * @uses plugins_url()
321
		 * @uses SCRIPT_DEBUG
322
		 *
323
		 * @return void
324
		 * @since 2.0.0
325
		 */
326
		public function wp_page_widget_enqueue_script() {
327
			$main_script = apply_filters( 'black-studio-tinymce-widget-script', 'black-studio-tinymce-widget' );
328
			$compat_script = 'wp-page-widget';
329
			$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
330
			wp_enqueue_script(
331
				$compat_script,
332
				plugins_url( 'js/' . $compat_script . $suffix . '.js', dirname( __FILE__ ) ),
333
				array( 'jquery', 'editor', 'quicktags', $main_script ),
334
				bstw()->get_version(),
335
				true
336
			);
337
		}
338
339
		/**
340
		 * Compatibility with Page Builder (SiteOrigin Panels)
341
		 *
342
		 * @uses add_action()
343
		 *
344
		 * @return void
345
		 * @since 2.0.0
346
		 */
347
		public function siteorigin_panels() {
348
			add_action( 'admin_init', array( $this, 'siteorigin_panels_disable_compat' ), 7 );
349
			add_action( 'admin_init', array( $this, 'siteorigin_panels_admin_init' ) );
350
		}
351
352
		/**
353
		 * Initialize compatibility for Page Builder (SiteOrigin Panels)
354
		 *
355
		 * @uses add_filter()
356
		 * @uses add_action()
357
		 * @uses remove_filter()
358
		 * @uses add_action()
359
		 * @uses is_plugin_active()
360
		 *
361
		 * @return void
362
		 * @since 2.0.0
363
		 */
364
		public function siteorigin_panels_admin_init() {
365
			if ( is_admin() && is_plugin_active( 'siteorigin-panels/siteorigin-panels.php' ) ) {
366
				add_filter( 'siteorigin_panels_widget_object', array( $this, 'siteorigin_panels_widget_object' ), 10 );
367
				add_filter( 'black_studio_tinymce_container_selectors', array( $this, 'siteorigin_panels_container_selectors' ) );
368
				add_filter( 'black_studio_tinymce_activate_events', array( $this, 'siteorigin_panels_activate_events' ) );
369
				add_filter( 'black_studio_tinymce_deactivate_events', array( $this, 'siteorigin_panels_deactivate_events' ) );
370
				add_filter( 'black_studio_tinymce_enable_pages', array( $this, 'siteorigin_panels_enable_pages' ) );
371
				add_filter( 'black_studio_tinymce_widget_additional_fields', array( $this, 'siteorigin_panels_additional_fields' ) );
372
				remove_filter( 'widget_text', array( bstw()->text_filters(), 'wpautop' ), 8 );
373
			}
374
		}
375
376
		/**
377
		 * Remove widget number to prevent translation when using Page Builder (SiteOrigin Panels) + WPML String Translation
378
		 *
379
		 * @param object $widget
380
		 * @return object
381
		 * @since 2.0.0
382
		 */
383
		public function siteorigin_panels_widget_object( $widget ) {
384
			if ( isset( $widget->id_base ) && 'black-studio-tinymce' == $widget->id_base ) {
385
				$widget->number = '';
386
			}
387
			return $widget;
388
		}
389
390
		/**
391
		 * Add selector for widget detection for Page Builder (SiteOrigin Panels)
392
		 *
393
		 * @param string[] $selectors
394
		 * @return string[]
395
		 * @since 2.0.0
396
		 */
397
		public function siteorigin_panels_container_selectors( $selectors ) {
398
			$selectors[] = 'div.panel-dialog';
399
			return $selectors;
400
		}
401
402
		/**
403
		 * Add activate events for Page Builder (SiteOrigin Panels)
404
		 *
405
		 * @param string[] $events
406
		 * @return string[]
407
		 * @since 2.0.0
408
		 */
409
		public function siteorigin_panels_activate_events( $events ) {
410
			$events[] = 'panelsopen';
411
			return $events;
412
		}
413
414
		/**
415
		 * Add deactivate events for Page Builder (SiteOrigin Panels)
416
		 *
417
		 * @param string[] $events
418
		 * @return string[]
419
		 * @since 2.0.0
420
		 */
421
		public function siteorigin_panels_deactivate_events( $events ) {
422
			$events[] = 'panelsdone';
423
			return $events;
424
		}
425
426
		/**
427
		 * Add pages filter to enable editor for Page Builder (SiteOrigin Panels)
428
		 *
429
		 * @param string[] $pages
430
		 * @return string[]
431
		 * @since 2.0.0
432
		 */
433
		public function siteorigin_panels_enable_pages( $pages ) {
434
			$pages[] = 'post-new.php';
435
			$pages[] = 'post.php';
436
			if ( isset( $_GET['page'] ) && 'so_panels_home_page' == $_GET['page'] ) {
437
				$pages[] = 'themes.php';
438
			}
439
			return $pages;
440
		}
441
442
		/**
443
		 * Add widget field for Page Builder (SiteOrigin Panels)
444
		 *
445
		 * @param mixed[] $instance
0 ignored issues
show
Bug introduced by Marco Chiesi
There is no parameter named $instance. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
446
		 * @param object $widget
0 ignored issues
show
Bug introduced by Marco Chiesi
There is no parameter named $widget. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
447
		 * @return mixed[]
0 ignored issues
show
Documentation introduced by Marco Chiesi
Consider making the return type a bit more specific; maybe use string[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
448
		 * @since 2.5.2
449
		 */
450
		public function siteorigin_panels_additional_fields( $fields ) {
451
			$fields[] = 'panels_info';
452
			return $fields;
453
		}
454
455
		/**
456
		 * Disable old compatibility code provided by Page Builder (SiteOrigin Panels)
457
		 *
458
		 * @return void
459
		 * @since 2.0.0
460
		 */
461
		public function siteorigin_panels_disable_compat( ) {
462
			remove_action( 'admin_init', 'siteorigin_panels_black_studio_tinymce_admin_init' );
463
			remove_action( 'admin_enqueue_scripts', 'siteorigin_panels_black_studio_tinymce_admin_enqueue', 15 );
464
		}
465
466
		/**
467
		 * Compatibility with Jetpack After the deadline
468
		 *
469
		 * @uses add_action()
470
		 *
471
		 * @return void
472
		 * @since 2.0.0
473
		 */
474
		public function jetpack_after_the_deadline() {
475
			add_action( 'black_studio_tinymce_load', array( $this, 'jetpack_after_the_deadline_load' ) );
476
		}
477
478
		/**
479
		 * Load Jetpack After the deadline scripts
480
		 *
481
		 * @uses add_filter()
482
		 *
483
		 * @return void
484
		 * @since 2.0.0
485
		 */
486
		public function jetpack_after_the_deadline_load() {
487
			add_filter( 'atd_load_scripts', '__return_true' );
488
		}
489
490
		/**
491
		 * Compatibility for Elementor plugin
492
		 *
493
		 * @uses add_filter()
494
		 *
495
		 * @return void
496
		 * @since 2.5.0
497
		 */
498
		public function elementor() {
499
			if ( is_admin() && isset( $_GET['action'] ) && 'elementor' == $_GET['action'] ) {
500
				add_filter( 'black_studio_tinymce_enable', '__return_false', 100 );
501
				add_action( 'widgets_init', array( $this, 'elementor_unregister_widget' ), 20 );
502
			}
503
		}
504
		
505
		/**
506
		 * Unregister Widget for Elementor plugin
507
		 *
508
		 * @uses unregister_widget()
509
		 *
510
		 * @return void
511
		 * @since 2.5.1
512
		 */
513
		public function elementor_unregister_widget() {
514
			unregister_widget( 'WP_Widget_Black_Studio_TinyMCE' );
515
		}
516
517
	} // END class Black_Studio_TinyMCE_Compatibility_Plugins
518
519
} // END class_exists check
520