Completed
Push — develop ( 4188c8...4421e3 )
by Marco
01:14
created

wpml_widget_before()   C

Complexity

Conditions 8
Paths 7

Size

Total Lines 26
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 9
nc 7
nop 2
dl 0
loc 26
rs 5.3846
c 0
b 0
f 0
1
<?php
2
/**
3
 * Black Studio TinyMCE Widget - Compatibility with other plugins
4
 *
5
 * @package Black_Studio_TinyMCE_Widget
6
 */
7
8
// Exit if accessed directly.
9
if ( ! defined( 'ABSPATH' ) ) {
10
	exit;
11
}
12
13
if ( ! class_exists( 'Black_Studio_TinyMCE_Compatibility_Plugins' ) ) {
14
15
	/**
16
	 * Class that provides compatibility code with other plugins
17
	 *
18
	 * @package Black_Studio_TinyMCE_Widget
19
	 * @since 2.0.0
20
	 */
21
	final class Black_Studio_TinyMCE_Compatibility_Plugins {
22
23
		/**
24
		 * The single instance of the class
25
		 *
26
		 * @var object
27
		 * @since 2.0.0
28
		 */
29
		protected static $_instance = null;
30
31
		/**
32
		 * Flag to keep track of removed WPML filter on widget title
33
		 *
34
		 * @var boolean
35
		 * @since 2.6.1
36
		 */
37
		private $wpml_removed_widget_title_filter = false;
38
39
		/**
40
		 * Flag to keep track of removed WPML filter on widget text
41
		 *
42
		 * @var boolean
43
		 * @since 2.6.1
44
		 */
45
		private $wpml_removed_widget_text_filter = false;
46
47
		/**
48
		 * Return the single class instance
49
		 *
50
		 * @param string[] $plugins Array of plugins.
51
		 * @return object
52
		 * @since 2.0.0
53
		 */
54
		public static function instance( $plugins = array() ) {
55
			if ( is_null( self::$_instance ) ) {
56
				self::$_instance = new self( $plugins );
57
			}
58
			return self::$_instance;
59
		}
60
61
		/**
62
		 * Class constructor
63
		 *
64
		 * @param string[] $plugins Array of plugins.
65
		 * @since 2.0.0
66
		 */
67
		protected function __construct( $plugins ) {
68
			foreach ( $plugins as $plugin ) {
69
				if ( is_callable( array( $this, $plugin ), false ) ) {
70
					$this->$plugin();
71
				}
72
			}
73
			if ( ! function_exists( 'is_plugin_active' ) ) {
74
				include_once ABSPATH . 'wp-admin/includes/plugin.php';
75
			}
76
		}
77
78
		/**
79
		 * Prevent the class from being cloned
80
		 *
81
		 * @return void
82
		 * @since 2.0.0
83
		 */
84
		protected function __clone() {
85
			_doing_it_wrong( __FUNCTION__, esc_html__( 'Cheatin&#8217; uh?' ), '2.0' );
86
		}
87
88
		/**
89
		 * Compatibility with WPML
90
		 *
91
		 * @uses add_filter()
92
		 *
93
		 * @return void
94
		 * @since 2.0.0
95
		 */
96
		public function wpml() {
97
			add_action( 'plugins_loaded', array( $this, 'wpml_init', 20 ) );
98
			add_action( 'black_studio_tinymce_before_widget', array( $this, 'wpml_widget_before' ), 10, 2 );
99
			add_action( 'black_studio_tinymce_after_widget', array( $this, 'wpml_widget_after' ), 10, 2 );
100
			add_filter( 'black_studio_tinymce_widget_update', array( $this, 'wpml_widget_update' ), 10, 2 );
101
			add_action( 'black_studio_tinymce_before_editor', array( $this, 'wpml_check_deprecated_translations' ), 5, 2 );
102
			add_filter( 'widget_text', array( $this, 'wpml_widget_text' ), 2, 3 );
103
		}
104
105
		/**
106
		 * Helper function to get WPML version
107
		 *
108
		 * @uses get_plugin_data()
109
		 *
110
		 * @return string
111
		 * @since 2.6.0
112
		 */
113
		public function wpml_get_version() {
114
			$wpml_data = get_plugin_data( WP_PLUGIN_DIR . '/sitepress-multilingual-cms/sitepress.php', false, false );
115
			return $wpml_data['Version'];
116
		}
117
118
		/**
119
		 * Initialize compatibility with WPML and WPML Widgets plugins
120
		 *
121
		 * @uses is_plugin_active()
122
		 * @uses has_action()
123
		 * @uses remove_action()
124
		 *
125
		 * @return void
126
		 * @since 2.3.1
127
		 */
128
		public function wpml_init() {
129
			if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) && is_plugin_active( 'wpml-widgets/wpml-widgets.php' ) ) {
130
				if ( false !== has_action( 'update_option_widget_black-studio-tinymce', 'icl_st_update_widget_title_actions' ) ) {
131
					remove_action( 'update_option_widget_black-studio-tinymce', 'icl_st_update_widget_title_actions', 5 );
132
				}
133
			}
134
		}
135
136
		/**
137
		 * Disable WPML String translation native behavior
138
		 *
139
		 * @uses remove_filter()
140
		 *
141
		 * @param mixed[] $args     Array of arguments.
142
		 * @param mixed[] $instance Widget instance.
143
		 * @return void
144
		 * @since 2.3.0
145
		 */
146
		public function wpml_widget_before( $args, $instance ) {
147
			if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) ) {
148
				/*
149
				Avoid native WPML string translation of widget titles
150
				for widgets inserted in pages built with Page Builder (SiteOrigin panels)
151
				and also when WPML Widgets is active and for WPML versions from 3.8.0 on
152
				*/
153
				if ( false !== has_filter( 'widget_title', 'icl_sw_filters_widget_title' ) ) {
154
					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 ) {
155
						remove_filter( 'widget_title', 'icl_sw_filters_widget_title', 0 );
156
						$this->wpml_removed_widget_title_filter = true;
157
					}
158
				}
159
160
				/*
161
				Avoid native WPML string translation of widget texts (for all widgets)
162
				Note: Black Studio TinyMCE Widget already supports WPML string translation,
163
				so this is needed to prevent duplicate translations
164
				*/
165
				if ( false !== has_filter( 'widget_text', 'icl_sw_filters_widget_text' ) ) {
166
					remove_filter( 'widget_text', 'icl_sw_filters_widget_text', 0 );
167
					$this->wpml_removed_widget_text_filter = true;
168
				}
169
			}
170
171
		}
172
173
		/**
174
		 * Re-Enable WPML String translation native behavior
175
		 *
176
		 * @uses add_filter()
177
		 *
178
		 * @param mixed[] $args     Array of arguments.
179
		 * @param mixed[] $instance Widget instance.
180
		 * @return void
181
		 * @since 2.3.0
182
		 */
183
		public function wpml_widget_after( $args, $instance ) {
0 ignored issues
show
Unused Code introduced by Marco Chiesi
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...
Unused Code introduced by Marco Chiesi
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...
184
			if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) ) {
185
				// Restore widget title's native WPML string translation filter if it was removed.
186
				if ( $this->wpml_removed_widget_title_filter ) {
187
					if ( false === has_filter( 'widget_title', 'icl_sw_filters_widget_title' ) && function_exists( 'icl_sw_filters_widget_title' ) ) {
188
						add_filter( 'widget_title', 'icl_sw_filters_widget_title', 0 );
189
						$this->wpml_removed_widget_title_filter = false;
190
					}
191
				}
192
				// Restore widget text's native WPML string translation filter if it was removed.
193
				if ( $this->wpml_removed_widget_text_filter ) {
194
					if ( false === has_filter( 'widget_text', 'icl_sw_filters_widget_text' ) && function_exists( 'icl_sw_filters_widget_text' ) ) {
195
						add_filter( 'widget_text', 'icl_sw_filters_widget_text', 0 );
196
						$this->wpml_removed_widget_text_filter = false;
197
					}
198
				}
199
			}
200
		}
201
202
		/**
203
		 * Add widget text to WPML String translation
204
		 *
205
		 * @uses is_plugin_active()
206
		 * @uses icl_register_string() Part of WPML
207
		 *
208
		 * @param mixed[] $instance Array of arguments.
209
		 * @param object  $widget   Widget instance.
210
		 * @return mixed[]
211
		 * @since 2.0.0
212
		 */
213
		public function wpml_widget_update( $instance, $widget ) {
214
			if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) &&
215
				version_compare( $this->wpml_get_version(), '3.8.0' ) < 0 &&
216
				! is_plugin_active( 'wpml-widgets/wpml-widgets.php' )
217
			) {
218
				if ( function_exists( 'icl_register_string' ) && ! empty( $widget->number ) ) {
219
					// Avoid translation of Page Builder (SiteOrigin panels) and WP Page Widget widgets.
220
					if ( ! isset( $instance['panels_info'] ) && ! isset( $instance['wp_page_widget'] ) ) {
221
						icl_register_string( 'Widgets', 'widget body - ' . $widget->id_base . '-' . $widget->number, $instance['text'] );
222
					}
223
				}
224
			}
225
			return $instance;
226
		}
227
228
		/**
229
		 * Translate widget text
230
		 *
231
		 * @uses is_plugin_active()
232
		 * @uses icl_t() Part of WPML
233
		 * @uses icl_st_is_registered_string() Part of WPML
234
		 *
235
		 * @param string       $text     Widget text.
236
		 * @param mixed[]|null $instance Widget instance.
237
		 * @param object|null  $widget   Widget object.
238
		 * @return string
239
		 * @since 2.0.0
240
		 */
241
		public function wpml_widget_text( $text, $instance = null, $widget = null ) {
242
			if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) && ! is_plugin_active( 'wpml-widgets/wpml-widgets.php' ) ) {
243
				if ( bstw()->check_widget( $widget ) && ! empty( $instance ) ) {
244
					if ( function_exists( 'icl_t' ) && function_exists( 'icl_st_is_registered_string' ) ) {
245
						// Avoid translation of Page Builder (SiteOrigin panels) and WP Page Widget widgets.
246
						if ( ! isset( $instance['panels_info'] ) && ! isset( $instance['wp_page_widget'] ) ) {
247
							if ( icl_st_is_registered_string( 'Widgets', 'widget body - ' . $widget->id_base . '-' . $widget->number ) ) {
248
								$text = icl_t( 'Widgets', 'widget body - ' . $widget->id_base . '-' . $widget->number, $text );
249
							}
250
						}
251
					}
252
				}
253
			}
254
			return $text;
255
		}
256
257
		/**
258
		 * Check for existing deprecated translations (made with WPML String Translations plugin) and display warning
259
		 *
260
		 * @uses is_plugin_active()
261
		 * @uses icl_st_is_registered_string() Part of WPML
262
		 * @uses admin_url()
263
		 *
264
		 * @param mixed[]|null $instance Widget instance.
265
		 * @param object|null  $widget   Widget object.
266
		 * @return void
267
		 * @since 2.6.0
268
		 */
269
		public function wpml_check_deprecated_translations( $instance, $widget ) {
270
			if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) && version_compare( $this->wpml_get_version(), '3.8.0' ) >= 0 ) {
271
				if ( function_exists( 'icl_st_is_registered_string' ) ) {
272
					if ( icl_st_is_registered_string( 'Widgets', 'widget body - ' . $widget->id_base . '-' . $widget->number ) ) {
273
						$wpml_st_url = admin_url( 'admin.php?page=wpml-string-translation%2Fmenu%2Fstring-translation.php&context=Widgets' );
274
						echo '<div class="notice notice-warning inline"><p>';
275
						/* translators: Warning displayed when deprecated translations of the current widget are detected */
276
						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 ) );
0 ignored issues
show
introduced by Marco Chiesi
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'sprintf'
Loading history...
277
						echo '</p></div>';
278
					}
279
				}
280
			}
281
		}
282
283
		/**
284
		 * Compatibility for WP Page Widget plugin
285
		 *
286
		 * @uses add_action()
287
		 *
288
		 * @return void
289
		 * @since 2.0.0
290
		 */
291
		public function wp_page_widget() {
292
			add_action( 'init', array( $this, 'wp_page_widget_init' ), 0 );
293
		}
294
295
		/**
296
		 * Initialize compatibility for WP Page Widget plugin (only for WordPress 3.3+)
297
		 *
298
		 * @uses add_filter()
299
		 * @uses add_action()
300
		 * @uses is_plugin_active()
301
		 * @uses get_bloginfo()
302
		 *
303
		 * @return void
304
		 * @since 2.0.0
305
		 */
306
		public function wp_page_widget_init() {
307
			if ( is_admin() && is_plugin_active( 'wp-page-widget/wp-page-widgets.php' ) && version_compare( get_bloginfo( 'version' ), '3.3', '>=' ) ) {
308
				add_filter( 'black_studio_tinymce_enable_pages', array( $this, 'wp_page_widget_enable_pages' ) );
309
				add_action( 'admin_print_scripts', array( $this, 'wp_page_widget_enqueue_script' ) );
310
				add_filter( 'black_studio_tinymce_widget_update', array( $this, 'wp_page_widget_add_data' ), 10, 2 );
311
			}
312
		}
313
314
		/**
315
		 * Enable filter for WP Page Widget plugin
316
		 *
317
		 * @param string[] $pages Array of pages.
318
		 * @return string[]
319
		 * @since 2.0.0
320
		 */
321
		public function wp_page_widget_enable_pages( $pages ) {
322
			$pages[] = 'post-new.php';
323
			$pages[] = 'post.php';
324
			if ( isset( $_GET['action'] ) && 'edit' === $_GET['action'] ) {
325
				$pages[] = 'edit-tags.php';
326
			}
327
			if ( isset( $_GET['page'] ) && in_array( $_GET['page'], array( 'pw-front-page', 'pw-search-page' ), true ) ) {
328
				$pages[] = 'admin.php';
329
			}
330
			return $pages;
331
		}
332
333
		/**
334
		 * Add WP Page Widget marker
335
		 *
336
		 * @param mixed[] $instance Widget instance.
337
		 * @param object  $widget   Widget object.
338
		 * @return mixed[]
339
		 * @since 2.5.0
340
		 */
341
		public function wp_page_widget_add_data( $instance, $widget ) {
342
			if ( bstw()->check_widget( $widget ) && ! empty( $instance ) ) {
343
				if ( isset( $_POST['action'] ) && 'pw-save-widget' === $_POST['action'] ) {
344
					$instance['wp_page_widget'] = true;
345
				}
346
			}
347
			return $instance;
348
		}
349
350
		/**
351
		 * Enqueue script for WP Page Widget plugin
352
		 *
353
		 * @uses apply_filters()
354
		 * @uses wp_enqueue_script()
355
		 * @uses plugins_url()
356
		 * @uses SCRIPT_DEBUG
357
		 *
358
		 * @return void
359
		 * @since 2.0.0
360
		 */
361
		public function wp_page_widget_enqueue_script() {
362
			$main_script   = apply_filters( 'black-studio-tinymce-widget-script', 'black-studio-tinymce-widget' );
363
			$compat_script = 'wp-page-widget';
364
			$suffix        = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
365
			wp_enqueue_script(
366
				$compat_script,
367
				plugins_url( 'js/' . $compat_script . $suffix . '.js', dirname( __FILE__ ) ),
368
				array( 'jquery', 'editor', 'quicktags', $main_script ),
369
				bstw()->get_version(),
370
				true
371
			);
372
		}
373
374
		/**
375
		 * Compatibility with Page Builder (SiteOrigin Panels)
376
		 *
377
		 * @uses add_action()
378
		 *
379
		 * @return void
380
		 * @since 2.0.0
381
		 */
382
		public function siteorigin_panels() {
383
			add_action( 'admin_init', array( $this, 'siteorigin_panels_disable_compat' ), 7 );
384
			add_action( 'admin_init', array( $this, 'siteorigin_panels_admin_init' ) );
385
		}
386
387
		/**
388
		 * Initialize compatibility for Page Builder (SiteOrigin Panels)
389
		 *
390
		 * @uses add_filter()
391
		 * @uses add_action()
392
		 * @uses remove_filter()
393
		 * @uses add_action()
394
		 * @uses is_plugin_active()
395
		 *
396
		 * @return void
397
		 * @since 2.0.0
398
		 */
399
		public function siteorigin_panels_admin_init() {
400
			if ( is_admin() && is_plugin_active( 'siteorigin-panels/siteorigin-panels.php' ) ) {
401
				add_filter( 'siteorigin_panels_widget_object', array( $this, 'siteorigin_panels_widget_object' ), 10 );
402
				add_filter( 'black_studio_tinymce_container_selectors', array( $this, 'siteorigin_panels_container_selectors' ) );
403
				add_filter( 'black_studio_tinymce_activate_events', array( $this, 'siteorigin_panels_activate_events' ) );
404
				add_filter( 'black_studio_tinymce_deactivate_events', array( $this, 'siteorigin_panels_deactivate_events' ) );
405
				add_filter( 'black_studio_tinymce_enable_pages', array( $this, 'siteorigin_panels_enable_pages' ) );
406
				add_filter( 'black_studio_tinymce_widget_additional_fields', array( $this, 'siteorigin_panels_additional_fields' ) );
407
				remove_filter( 'widget_text', array( bstw()->text_filters(), 'wpautop' ), 8 );
408
			}
409
		}
410
411
		/**
412
		 * Remove widget number to prevent translation when using Page Builder (SiteOrigin Panels) + WPML String Translation
413
		 *
414
		 * @param object $widget Widget object.
415
		 * @return object
416
		 * @since 2.0.0
417
		 */
418
		public function siteorigin_panels_widget_object( $widget ) {
419
			if ( isset( $widget->id_base ) && 'black-studio-tinymce' === $widget->id_base ) {
420
				$widget->number = '';
421
			}
422
			return $widget;
423
		}
424
425
		/**
426
		 * Add selector for widget detection for Page Builder (SiteOrigin Panels)
427
		 *
428
		 * @param string[] $selectors Array of selectors.
429
		 * @return string[]
430
		 * @since 2.0.0
431
		 */
432
		public function siteorigin_panels_container_selectors( $selectors ) {
433
			$selectors[] = 'div.panel-dialog';
434
			return $selectors;
435
		}
436
437
		/**
438
		 * Add activate events for Page Builder (SiteOrigin Panels)
439
		 *
440
		 * @param string[] $events Array of events.
441
		 * @return string[]
442
		 * @since 2.0.0
443
		 */
444
		public function siteorigin_panels_activate_events( $events ) {
445
			$events[] = 'panelsopen';
446
			return $events;
447
		}
448
449
		/**
450
		 * Add deactivate events for Page Builder (SiteOrigin Panels)
451
		 *
452
		 * @param string[] $events Array of events.
453
		 * @return string[]
454
		 * @since 2.0.0
455
		 */
456
		public function siteorigin_panels_deactivate_events( $events ) {
457
			$events[] = 'panelsdone';
458
			return $events;
459
		}
460
461
		/**
462
		 * Add pages filter to enable editor for Page Builder (SiteOrigin Panels)
463
		 *
464
		 * @param string[] $pages Array of pages.
465
		 * @return string[]
466
		 * @since 2.0.0
467
		 */
468
		public function siteorigin_panels_enable_pages( $pages ) {
469
			$pages[] = 'post-new.php';
470
			$pages[] = 'post.php';
471
			if ( isset( $_GET['page'] ) && 'so_panels_home_page' === $_GET['page'] ) {
472
				$pages[] = 'themes.php';
473
			}
474
			return $pages;
475
		}
476
477
		/**
478
		 * Add widget field for Page Builder (SiteOrigin Panels)
479
		 *
480
		 * @param string[] $fields Array of fields.
481
		 * @return string[]
482
		 * @since 2.6.0
483
		 */
484
		public function siteorigin_panels_additional_fields( $fields ) {
485
			$fields[] = 'panels_info';
486
			return $fields;
487
		}
488
489
		/**
490
		 * Disable old compatibility code provided by Page Builder (SiteOrigin Panels)
491
		 *
492
		 * @return void
493
		 * @since 2.0.0
494
		 */
495
		public function siteorigin_panels_disable_compat() {
496
			remove_action( 'admin_init', 'siteorigin_panels_black_studio_tinymce_admin_init' );
497
			remove_action( 'admin_enqueue_scripts', 'siteorigin_panels_black_studio_tinymce_admin_enqueue', 15 );
498
		}
499
500
		/**
501
		 * Compatibility with Jetpack After the deadline
502
		 *
503
		 * @uses add_action()
504
		 *
505
		 * @return void
506
		 * @since 2.0.0
507
		 */
508
		public function jetpack_after_the_deadline() {
509
			add_action( 'black_studio_tinymce_load', array( $this, 'jetpack_after_the_deadline_load' ) );
510
		}
511
512
		/**
513
		 * Load Jetpack After the deadline scripts
514
		 *
515
		 * @uses add_filter()
516
		 *
517
		 * @return void
518
		 * @since 2.0.0
519
		 */
520
		public function jetpack_after_the_deadline_load() {
521
			add_filter( 'atd_load_scripts', '__return_true' );
522
		}
523
524
		/**
525
		 * Compatibility for Elementor plugin
526
		 *
527
		 * @uses add_filter()
528
		 *
529
		 * @return void
530
		 * @since 2.5.0
531
		 */
532
		public function elementor() {
533
			if ( is_admin() && isset( $_GET['action'] ) && 'elementor' === $_GET['action'] ) {
534
				add_filter( 'black_studio_tinymce_enable', '__return_false', 100 );
535
				add_action( 'widgets_init', array( $this, 'elementor_unregister_widget' ), 20 );
536
			}
537
		}
538
539
		/**
540
		 * Unregister Widget for Elementor plugin
541
		 *
542
		 * @uses unregister_widget()
543
		 *
544
		 * @return void
545
		 * @since 2.5.1
546
		 */
547
		public function elementor_unregister_widget() {
548
			unregister_widget( 'WP_Widget_Black_Studio_TinyMCE' );
549
		}
550
551
	} // END class Black_Studio_TinyMCE_Compatibility_Plugins
552
553
} // END class_exists check
554