Completed
Push — develop ( 7846c8...aa954e )
by Marco
01:07
created

wp_page_widget_add_data()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 5
nc 3
nop 2
dl 0
loc 8
rs 8.8571
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 ) {
261
			if ( bstw()->check_widget( $widget ) && ! empty( $instance ) ) {
262
				if ( isset( $_POST['action'] ) && 'pw-save-widget' == $_POST['action'] ) {
263
					$instance['wp_page_widget'] = true;
264
				}
265
			}
266
			return $instance;
267
		}
268
269
		/**
270
		 * Enqueue script for WP Page Widget plugin
271
		 *
272
		 * @uses apply_filters()
273
		 * @uses wp_enqueue_script()
274
		 * @uses plugins_url()
275
		 * @uses SCRIPT_DEBUG
276
		 *
277
		 * @return void
278
		 * @since 2.0.0
279
		 */
280
		public function wp_page_widget_enqueue_script() {
281
			$main_script = apply_filters( 'black-studio-tinymce-widget-script', 'black-studio-tinymce-widget' );
282
			$compat_script = 'wp-page-widget';
283
			$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
284
			wp_enqueue_script(
285
				$compat_script,
286
				plugins_url( 'js/' . $compat_script . $suffix . '.js', dirname( __FILE__ ) ),
287
				array( 'jquery', 'editor', 'quicktags', $main_script ),
288
				bstw()->get_version(),
289
				true
290
			);
291
		}
292
293
		/**
294
		 * Compatibility with Page Builder (SiteOrigin Panels)
295
		 *
296
		 * @uses add_action()
297
		 *
298
		 * @return void
299
		 * @since 2.0.0
300
		 */
301
		public function siteorigin_panels() {
302
			add_action( 'admin_init', array( $this, 'siteorigin_panels_disable_compat' ), 7 );
303
			add_action( 'admin_init', array( $this, 'siteorigin_panels_admin_init' ) );
304
		}
305
306
		/**
307
		 * Initialize compatibility for Page Builder (SiteOrigin Panels)
308
		 *
309
		 * @uses add_filter()
310
		 * @uses add_action()
311
		 * @uses remove_filter()
312
		 * @uses add_action()
313
		 * @uses is_plugin_active()
314
		 *
315
		 * @return void
316
		 * @since 2.0.0
317
		 */
318
		public function siteorigin_panels_admin_init() {
319
			if ( is_admin() && is_plugin_active( 'siteorigin-panels/siteorigin-panels.php' ) ) {
320
				add_filter( 'siteorigin_panels_widget_object', array( $this, 'siteorigin_panels_widget_object' ), 10 );
321
				add_filter( 'black_studio_tinymce_container_selectors', array( $this, 'siteorigin_panels_container_selectors' ) );
322
				add_filter( 'black_studio_tinymce_activate_events', array( $this, 'siteorigin_panels_activate_events' ) );
323
				add_filter( 'black_studio_tinymce_deactivate_events', array( $this, 'siteorigin_panels_deactivate_events' ) );
324
				add_filter( 'black_studio_tinymce_enable_pages', array( $this, 'siteorigin_panels_enable_pages' ) );
325
				remove_filter( 'widget_text', array( bstw()->text_filters(), 'wpautop' ), 8 );
326
			}
327
		}
328
329
		/**
330
		 * Remove widget number to prevent translation when using Page Builder (SiteOrigin Panels) + WPML String Translation
331
		 *
332
		 * @param object $widget
333
		 * @return object
334
		 * @since 2.0.0
335
		 */
336
		public function siteorigin_panels_widget_object( $widget ) {
337
			if ( isset( $widget->id_base ) && 'black-studio-tinymce' == $widget->id_base ) {
338
				$widget->number = '';
339
			}
340
			return $widget;
341
		}
342
343
		/**
344
		 * Add selector for widget detection for Page Builder (SiteOrigin Panels)
345
		 *
346
		 * @param string[] $selectors
347
		 * @return string[]
348
		 * @since 2.0.0
349
		 */
350
		public function siteorigin_panels_container_selectors( $selectors ) {
351
			$selectors[] = 'div.panel-dialog';
352
			return $selectors;
353
		}
354
355
		/**
356
		 * Add activate events for Page Builder (SiteOrigin Panels)
357
		 *
358
		 * @param string[] $events
359
		 * @return string[]
360
		 * @since 2.0.0
361
		 */
362
		public function siteorigin_panels_activate_events( $events ) {
363
			$events[] = 'panelsopen';
364
			return $events;
365
		}
366
367
		/**
368
		 * Add deactivate events for Page Builder (SiteOrigin Panels)
369
		 *
370
		 * @param string[] $events
371
		 * @return string[]
372
		 * @since 2.0.0
373
		 */
374
		public function siteorigin_panels_deactivate_events( $events ) {
375
			$events[] = 'panelsdone';
376
			return $events;
377
		}
378
379
		/**
380
		 * Add pages filter to enable editor for Page Builder (SiteOrigin Panels)
381
		 *
382
		 * @param string[] $pages
383
		 * @return string[]
384
		 * @since 2.0.0
385
		 */
386
		public function siteorigin_panels_enable_pages( $pages ) {
387
			$pages[] = 'post-new.php';
388
			$pages[] = 'post.php';
389
			if ( isset( $_GET['page'] ) && 'so_panels_home_page' == $_GET['page'] ) {
390
				$pages[] = 'themes.php';
391
			}
392
			return $pages;
393
		}
394
395
		/**
396
		 * Disable old compatibility code provided by Page Builder (SiteOrigin Panels)
397
		 *
398
		 * @return void
399
		 * @since 2.0.0
400
		 */
401
		public function siteorigin_panels_disable_compat( ) {
402
			remove_action( 'admin_init', 'siteorigin_panels_black_studio_tinymce_admin_init' );
403
			remove_action( 'admin_enqueue_scripts', 'siteorigin_panels_black_studio_tinymce_admin_enqueue', 15 );
404
		}
405
406
		/**
407
		 * Compatibility with Jetpack After the deadline
408
		 *
409
		 * @uses add_action()
410
		 *
411
		 * @return void
412
		 * @since 2.0.0
413
		 */
414
		public function jetpack_after_the_deadline() {
415
			add_action( 'black_studio_tinymce_load', array( $this, 'jetpack_after_the_deadline_load' ) );
416
		}
417
418
		/**
419
		 * Load Jetpack After the deadline scripts
420
		 *
421
		 * @uses add_filter()
422
		 *
423
		 * @return void
424
		 * @since 2.0.0
425
		 */
426
		public function jetpack_after_the_deadline_load() {
427
			add_filter( 'atd_load_scripts', '__return_true' );
428
		}
429
430
		/**
431
		 * Compatibility for Elementor plugin
432
		 *
433
		 * @uses add_filter()
434
		 *
435
		 * @return void
436
		 * @since 2.5.0
437
		 */
438
		public function elementor() {
439
			if ( is_admin() && isset( $_GET['action'] ) && 'elementor' == $_GET['action'] ) {
440
				add_filter( 'black_studio_tinymce_enable', '__return_false', 100 );
441
			}
442
		}
443
444
	} // END class Black_Studio_TinyMCE_Compatibility_Plugins
445
446
} // END class_exists check
447