Completed
Push — develop ( 03416e...7846c8 )
by Marco
01:28
created

wp_page_widget_add_data()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 2
nop 2
dl 0
loc 6
rs 9.4285
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' ) ) {
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'] ) || isset( $instance['wp_page_widget'] ) || 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' ) ) {
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'] ) || isset( $instance['wp_page_widget'] ) || 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' ) ) {
166
				if ( function_exists( 'icl_register_string' ) && ! empty( $widget->number ) ) {
167
					// Avoid translation of Page Builder (SiteOrigin panels) and WP Page Widget widgets
168
					if ( ! isset( $instance['panels_info'] ) && ! isset( $instance['wp_page_widget'] ) ) {
169
						icl_register_string( 'Widgets', 'widget body - ' . $widget->id_base . '-' . $widget->number, $instance['text'] );
170
					}
171
				}
172
			}
173
			return $instance;
174
		}
175
176
		/**
177
		 * Translate widget text
178
		 *
179
		 * @uses is_plugin_active()
180
		 * @uses icl_t() Part of WPML
181
		 *
182
		 * @param string $text
183
		 * @param mixed[]|null $instance
184
		 * @param object|null $widget
185
		 * @return string
186
		 * @since 2.0.0
187
		 */
188
		public function wpml_widget_text( $text, $instance = null, $widget = null ) {
189
			if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) && ! is_plugin_active( 'wpml-widgets/wpml-widgets.php' ) ) {
190
				if ( bstw()->check_widget( $widget ) && ! empty( $instance ) ) {
191
					if ( function_exists( 'icl_t' ) ) {
192
						// Avoid translation of Page Builder (SiteOrigin panels) and WP Page Widget widgets 
193
						if ( ! isset( $instance['panels_info'] ) && ! isset( $instance['wp_page_widget'] ) ) { 
194
							$text = icl_t( 'Widgets', 'widget body - ' . $widget->id_base . '-' . $widget->number, $text );
195
						}
196
					}
197
				}
198
			}
199
			return $text;
200
		}
201
202
		/**
203
		 * Compatibility for WP Page Widget plugin
204
		 *
205
		 * @uses add_action()
206
		 *
207
		 * @return void
208
		 * @since 2.0.0
209
		 */
210
		public function wp_page_widget() {
211
			add_action( 'init', array( $this, 'wp_page_widget_init' ), 0 );
212
		}
213
214
		/**
215
		 * Initialize compatibility for WP Page Widget plugin (only for WordPress 3.3+)
216
		 *
217
		 * @uses add_filter()
218
		 * @uses add_action()
219
		 * @uses is_plugin_active()
220
		 * @uses get_bloginfo()
221
		 *
222
		 * @return void
223
		 * @since 2.0.0
224
		 */
225
		public function wp_page_widget_init() {
226
			if ( is_admin() && is_plugin_active( 'wp-page-widget/wp-page-widgets.php' ) && version_compare( get_bloginfo( 'version' ), '3.3', '>=' ) ) {
227
				add_filter( 'black_studio_tinymce_enable_pages', array( $this, 'wp_page_widget_enable_pages' ) );
228
				add_action( 'admin_print_scripts', array( $this, 'wp_page_widget_enqueue_script' ) );
229
				add_filter( 'black_studio_tinymce_widget_update', array( $this, 'wp_page_widget_add_data' ), 10, 2 );
230
			}
231
		}
232
233
		/**
234
		 * Enable filter for WP Page Widget plugin
235
		 *
236
		 * @param string[] $pages
237
		 * @return string[]
238
		 * @since 2.0.0
239
		 */
240
		public function wp_page_widget_enable_pages( $pages ) {
241
			$pages[] = 'post-new.php';
242
			$pages[] = 'post.php';
243
			if ( isset( $_GET['action'] ) && 'edit' == $_GET['action'] ) {
244
				$pages[] = 'edit-tags.php';
245
			}
246
			if ( isset( $_GET['page'] ) && in_array( $_GET['page'], array( 'pw-front-page', 'pw-search-page' ) ) ) {
247
				$pages[] = 'admin.php';
248
			}
249
			return $pages;
250
		}
251
252
		/**
253
		 * Add WP Page Widget marker
254
		 *
255
		 * @param mixed[] $instance
256
		 * @param object $widget
257
		 * @return mixed[]
258
		 * @since 2.5.0
259
		 */
260
		public function wp_page_widget_add_data( $instance, $widget ) {
0 ignored issues
show
Unused Code introduced by Marco Chiesi
The parameter $widget 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...
261
			if ( isset( $_POST['action'] ) && 'pw-save-widget' == $_POST['action'] ) {
262
				$instance['wp_page_widget'] = true;
263
			}
264
			return $instance;
265
		}
266
267
		/**
268
		 * Enqueue script for WP Page Widget plugin
269
		 *
270
		 * @uses apply_filters()
271
		 * @uses wp_enqueue_script()
272
		 * @uses plugins_url()
273
		 * @uses SCRIPT_DEBUG
274
		 *
275
		 * @return void
276
		 * @since 2.0.0
277
		 */
278
		public function wp_page_widget_enqueue_script() {
279
			$main_script = apply_filters( 'black-studio-tinymce-widget-script', 'black-studio-tinymce-widget' );
280
			$compat_script = 'wp-page-widget';
281
			$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
282
			wp_enqueue_script(
283
				$compat_script,
284
				plugins_url( 'js/' . $compat_script . $suffix . '.js', dirname( __FILE__ ) ),
285
				array( 'jquery', 'editor', 'quicktags', $main_script ),
286
				bstw()->get_version(),
287
				true
288
			);
289
		}
290
291
		/**
292
		 * Compatibility with Page Builder (SiteOrigin Panels)
293
		 *
294
		 * @uses add_action()
295
		 *
296
		 * @return void
297
		 * @since 2.0.0
298
		 */
299
		public function siteorigin_panels() {
300
			add_action( 'admin_init', array( $this, 'siteorigin_panels_disable_compat' ), 7 );
301
			add_action( 'admin_init', array( $this, 'siteorigin_panels_admin_init' ) );
302
		}
303
304
		/**
305
		 * Initialize compatibility for Page Builder (SiteOrigin Panels)
306
		 *
307
		 * @uses add_filter()
308
		 * @uses add_action()
309
		 * @uses remove_filter()
310
		 * @uses add_action()
311
		 * @uses is_plugin_active()
312
		 *
313
		 * @return void
314
		 * @since 2.0.0
315
		 */
316
		public function siteorigin_panels_admin_init() {
317
			if ( is_admin() && is_plugin_active( 'siteorigin-panels/siteorigin-panels.php' ) ) {
318
				add_filter( 'siteorigin_panels_widget_object', array( $this, 'siteorigin_panels_widget_object' ), 10 );
319
				add_filter( 'black_studio_tinymce_container_selectors', array( $this, 'siteorigin_panels_container_selectors' ) );
320
				add_filter( 'black_studio_tinymce_activate_events', array( $this, 'siteorigin_panels_activate_events' ) );
321
				add_filter( 'black_studio_tinymce_deactivate_events', array( $this, 'siteorigin_panels_deactivate_events' ) );
322
				add_filter( 'black_studio_tinymce_enable_pages', array( $this, 'siteorigin_panels_enable_pages' ) );
323
				remove_filter( 'widget_text', array( bstw()->text_filters(), 'wpautop' ), 8 );
324
			}
325
		}
326
327
		/**
328
		 * Remove widget number to prevent translation when using Page Builder (SiteOrigin Panels) + WPML String Translation
329
		 *
330
		 * @param object $widget
331
		 * @return object
332
		 * @since 2.0.0
333
		 */
334
		public function siteorigin_panels_widget_object( $widget ) {
335
			if ( isset( $widget->id_base ) && 'black-studio-tinymce' == $widget->id_base ) {
336
				$widget->number = '';
337
			}
338
			return $widget;
339
		}
340
341
		/**
342
		 * Add selector for widget detection for Page Builder (SiteOrigin Panels)
343
		 *
344
		 * @param string[] $selectors
345
		 * @return string[]
346
		 * @since 2.0.0
347
		 */
348
		public function siteorigin_panels_container_selectors( $selectors ) {
349
			$selectors[] = 'div.panel-dialog';
350
			return $selectors;
351
		}
352
353
		/**
354
		 * Add activate events for Page Builder (SiteOrigin Panels)
355
		 *
356
		 * @param string[] $events
357
		 * @return string[]
358
		 * @since 2.0.0
359
		 */
360
		public function siteorigin_panels_activate_events( $events ) {
361
			$events[] = 'panelsopen';
362
			return $events;
363
		}
364
365
		/**
366
		 * Add deactivate events for Page Builder (SiteOrigin Panels)
367
		 *
368
		 * @param string[] $events
369
		 * @return string[]
370
		 * @since 2.0.0
371
		 */
372
		public function siteorigin_panels_deactivate_events( $events ) {
373
			$events[] = 'panelsdone';
374
			return $events;
375
		}
376
377
		/**
378
		 * Add pages filter to enable editor for Page Builder (SiteOrigin Panels)
379
		 *
380
		 * @param string[] $pages
381
		 * @return string[]
382
		 * @since 2.0.0
383
		 */
384
		public function siteorigin_panels_enable_pages( $pages ) {
385
			$pages[] = 'post-new.php';
386
			$pages[] = 'post.php';
387
			if ( isset( $_GET['page'] ) && 'so_panels_home_page' == $_GET['page'] ) {
388
				$pages[] = 'themes.php';
389
			}
390
			return $pages;
391
		}
392
393
		/**
394
		 * Disable old compatibility code provided by Page Builder (SiteOrigin Panels)
395
		 *
396
		 * @return void
397
		 * @since 2.0.0
398
		 */
399
		public function siteorigin_panels_disable_compat( ) {
400
			remove_action( 'admin_init', 'siteorigin_panels_black_studio_tinymce_admin_init' );
401
			remove_action( 'admin_enqueue_scripts', 'siteorigin_panels_black_studio_tinymce_admin_enqueue', 15 );
402
		}
403
404
		/**
405
		 * Compatibility with Jetpack After the deadline
406
		 *
407
		 * @uses add_action()
408
		 *
409
		 * @return void
410
		 * @since 2.0.0
411
		 */
412
		public function jetpack_after_the_deadline() {
413
			add_action( 'black_studio_tinymce_load', array( $this, 'jetpack_after_the_deadline_load' ) );
414
		}
415
416
		/**
417
		 * Load Jetpack After the deadline scripts
418
		 *
419
		 * @uses add_filter()
420
		 *
421
		 * @return void
422
		 * @since 2.0.0
423
		 */
424
		public function jetpack_after_the_deadline_load() {
425
			add_filter( 'atd_load_scripts', '__return_true' );
426
		}
427
428
		/**
429
		 * Compatibility for Elementor plugin
430
		 *
431
		 * @uses add_filter()
432
		 *
433
		 * @return void
434
		 * @since 2.5.0
435
		 */
436
		public function elementor() {
437
			if ( is_admin() && isset( $_GET['action'] ) && 'elementor' == $_GET['action'] ) {
438
				add_filter( 'black_studio_tinymce_enable', '__return_false', 100 );
439
			}
440
		}
441
442
	} // END class Black_Studio_TinyMCE_Compatibility_Plugins
443
444
} // END class_exists check
445