Completed
Push — develop ( cffcc3...85c2ba )
by Marco
03:09
created

...ck-studio-tinymce-compatibility-plugin-wpml.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Black Studio TinyMCE Widget - Compatibility with WPML plugin(s)
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_Plugin_Wpml' ) ) {
14
	/**
15
	 * Class that provides compatibility with WPML plugin(s)
16
	 *
17
	 * @package Black_Studio_TinyMCE_Widget
18
	 * @since 3.0.0
19
	 */
20
	final class Black_Studio_TinyMCE_Compatibility_Plugin_Wpml {
21
22
		/**
23
		 * The single instance of the class
24
		 *
25
		 * @var object
26
		 * @since 3.0.0
27
		 */
28
		protected static $_instance = null;
29
30
		/**
31
		 * Flag to keep track of removed WPML filter on widget title
32
		 *
33
		 * @var boolean
34
		 * @since 3.0.0
35
		 */
36
		private $removed_widget_title_filter = false;
37
38
		/**
39
		 * Flag to keep track of removed WPML filter on widget text
40
		 *
41
		 * @var boolean
42
		 * @since 3.0.0
43
		 */
44
		private $removed_widget_text_filter = false;
45
46
		/**
47
		 * Return the single class instance
48
		 *
49
		 * @return object
50
		 * @since 3.0.0
51
		 */
52
		public static function instance() {
53
			if ( is_null( self::$_instance ) ) {
54
				self::$_instance = new self();
55
			}
56
			return self::$_instance;
57
		}
58
59
		/**
60
		 * Class constructor
61
		 *
62
		 * @uses add_action()
63
		 * @uses add_filter()
64
		 *
65
		 * @since 3.0.0
66
		 */
67
		protected function __construct() {
68
			add_action( 'init', array( $this, 'init' ) );
69
			add_action( 'black_studio_tinymce_before_widget', array( $this, 'widget_before' ), 10, 2 );
70
			add_action( 'black_studio_tinymce_after_widget', array( $this, 'widget_after' ), 10, 2 );
71
			add_filter( 'black_studio_tinymce_widget_update', array( $this, 'widget_update' ), 10, 2 );
72
			add_action( 'black_studio_tinymce_before_editor', array( $this, 'check_deprecated_translations' ), 5, 2 );
73
			add_filter( 'widget_text', array( $this, 'widget_text' ), 2, 3 );
74
			if ( ! function_exists( 'is_plugin_active' ) ) {
75
				include_once ABSPATH . 'wp-admin/includes/plugin.php';
76
			}
77
		}
78
79
		/**
80
		 * Prevent the class from being cloned
81
		 *
82
		 * @return void
83
		 * @since 3.0.0
84
		 */
85
		protected function __clone() {
86
			_doing_it_wrong( __FUNCTION__, esc_html__( 'Cheatin&#8217; uh?' ), '3.0' );
87
		}
88
89
		/**
90
		 * Helper function to get WPML version
91
		 *
92
		 * @uses get_plugin_data()
93
		 *
94
		 * @return string
95
		 * @since 3.0.0
96
		 */
97
		public function get_version() {
98
			$plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/sitepress-multilingual-cms/sitepress.php', false, false );
99
			return $plugin_data['Version'];
100
		}
101
102
		/**
103
		 * Initialize compatibility with WPML and WPML Widgets plugins
104
		 *
105
		 * @uses is_plugin_active()
106
		 * @uses has_action()
107
		 * @uses remove_action()
108
		 *
109
		 * @return void
110
		 * @since 3.0.0
111
		 */
112
		public function init() {
113
			if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) && is_plugin_active( 'wpml-widgets/wpml-widgets.php' ) ) {
114
				if ( false !== has_action( 'update_option_widget_black-studio-tinymce', 'icl_st_update_widget_title_actions' ) ) {
115
					remove_action( 'update_option_widget_black-studio-tinymce', 'icl_st_update_widget_title_actions', 5 );
116
				}
117
			}
118
		}
119
120
		/**
121
		 * Disable WPML String translation native behavior
122
		 *
123
		 * @uses is_plugin_active()
124
		 * @uses has_filter()
125
		 * @uses remove_filter()
126
		 *
127
		 * @param mixed[] $args     Array of arguments.
128
		 * @param mixed[] $instance Widget instance.
129
		 * @return void
130
		 * @since 3.0.0
131
		 */
132
		public function widget_before( $args, $instance ) {
133
			if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) ) {
134
				/*
135
				Avoid native WPML string translation of widget titles
136
				for widgets inserted in pages built with Page Builder (SiteOrigin panels)
137
				and also when WPML Widgets is active and for WPML versions from 3.8.0 on
138
				*/
139
				if ( false !== has_filter( 'widget_title', 'icl_sw_filters_widget_title' ) ) {
140
					if ( isset( $instance['panels_info'] ) || isset( $instance['wp_page_widget'] ) || is_plugin_active( 'wpml-widgets/wpml-widgets.php' ) || version_compare( $this->get_version(), '3.8.0' ) >= 0 ) {
141
						remove_filter( 'widget_title', 'icl_sw_filters_widget_title', 0 );
142
						$this->removed_widget_title_filter = true;
143
					}
144
				}
145
146
				/*
147
				Avoid native WPML string translation of widget texts (for all widgets)
148
				Note: Black Studio TinyMCE Widget already supports WPML string translation,
149
				so this is needed to prevent duplicate translations
150
				*/
151
				if ( false !== has_filter( 'widget_text', 'icl_sw_filters_widget_text' ) ) {
152
					remove_filter( 'widget_text', 'icl_sw_filters_widget_text', 0 );
153
					$this->removed_widget_text_filter = true;
154
				}
155
			}
156
157
		}
158
159
		/**
160
		 * Re-Enable WPML String translation native behavior
161
		 *
162
		 * @uses add_filter()
163
		 * @uses has_filter()
164
		 *
165
		 * @param mixed[] $args     Array of arguments.
166
		 * @param mixed[] $instance Widget instance.
167
		 * @return void
168
		 * @since 3.0.0
169
		 */
170
		public function widget_after( $args, $instance ) {
171
			if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) ) {
172
				// Restore widget title's native WPML string translation filter if it was removed.
173
				if ( $this->removed_widget_title_filter ) {
174
					if ( false === has_filter( 'widget_title', 'icl_sw_filters_widget_title' ) && function_exists( 'icl_sw_filters_widget_title' ) ) {
175
						add_filter( 'widget_title', 'icl_sw_filters_widget_title', 0 );
176
						$this->removed_widget_title_filter = false;
177
					}
178
				}
179
				// Restore widget text's native WPML string translation filter if it was removed.
180
				if ( $this->removed_widget_text_filter ) {
181
					if ( false === has_filter( 'widget_text', 'icl_sw_filters_widget_text' ) && function_exists( 'icl_sw_filters_widget_text' ) ) {
182
						add_filter( 'widget_text', 'icl_sw_filters_widget_text', 0 );
183
						$this->wpml_removed_widget_text_filter = false;
0 ignored issues
show
The property wpml_removed_widget_text_filter does not seem to exist. Did you mean removed_widget_text_filter?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
184
					}
185
				}
186
			}
187
		}
188
189
		/**
190
		 * Add widget text to WPML String translation
191
		 *
192
		 * @uses is_plugin_active()
193
		 * @uses icl_register_string() Part of WPML
194
		 *
195
		 * @param mixed[] $instance Array of arguments.
196
		 * @param object  $widget   Widget instance.
197
		 * @return mixed[]
198
		 * @since 3.0.0
199
		 */
200
		public function widget_update( $instance, $widget ) {
201
			if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) &&
202
				version_compare( $this->get_version(), '3.8.0' ) < 0 &&
203
				! is_plugin_active( 'wpml-widgets/wpml-widgets.php' )
204
			) {
205
				if ( function_exists( 'icl_register_string' ) && ! empty( $widget->number ) ) {
206
					// Avoid translation of Page Builder (SiteOrigin panels) and WP Page Widget widgets.
207
					if ( ! isset( $instance['panels_info'] ) && ! isset( $instance['wp_page_widget'] ) ) {
208
						icl_register_string( 'Widgets', 'widget body - ' . $widget->id_base . '-' . $widget->number, $instance['text'] );
209
					}
210
				}
211
			}
212
			return $instance;
213
		}
214
215
		/**
216
		 * Translate widget text
217
		 *
218
		 * @uses is_plugin_active()
219
		 * @uses icl_t() Part of WPML
220
		 * @uses icl_st_is_registered_string() Part of WPML
221
		 *
222
		 * @param string       $text     Widget text.
223
		 * @param mixed[]|null $instance Widget instance.
224
		 * @param object|null  $widget   Widget object.
225
		 * @return string
226
		 * @since 3.0.0
227
		 */
228
		public function widget_text( $text, $instance = null, $widget = null ) {
229
			if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) && ! is_plugin_active( 'wpml-widgets/wpml-widgets.php' ) ) {
230
				if ( bstw()->check_widget( $widget ) && ! empty( $instance ) ) {
231
					if ( function_exists( 'icl_t' ) && function_exists( 'icl_st_is_registered_string' ) ) {
232
						// Avoid translation of Page Builder (SiteOrigin panels) and WP Page Widget widgets.
233
						if ( ! isset( $instance['panels_info'] ) && ! isset( $instance['wp_page_widget'] ) ) {
234
							if ( icl_st_is_registered_string( 'Widgets', 'widget body - ' . $widget->id_base . '-' . $widget->number ) ) {
235
								$text = icl_t( 'Widgets', 'widget body - ' . $widget->id_base . '-' . $widget->number, $text );
236
							}
237
						}
238
					}
239
				}
240
			}
241
			return $text;
242
		}
243
244
		/**
245
		 * Check for existing deprecated translations (made with WPML String Translations plugin) and display warning
246
		 *
247
		 * @uses is_plugin_active()
248
		 * @uses icl_st_is_registered_string() Part of WPML
249
		 * @uses admin_url()
250
		 *
251
		 * @param mixed[]|null $instance Widget instance.
252
		 * @param object|null  $widget   Widget object.
253
		 * @return void
254
		 * @since 2.6.0
255
		 */
256
		public function check_deprecated_translations( $instance, $widget ) {
257
			if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) && version_compare( $this->get_version(), '3.8.0' ) >= 0 ) {
258
				if ( function_exists( 'icl_st_is_registered_string' ) ) {
259
					if ( icl_st_is_registered_string( 'Widgets', 'widget body - ' . $widget->id_base . '-' . $widget->number ) ) {
260
						$wpml_st_url = admin_url( 'admin.php?page=wpml-string-translation%2Fmenu%2Fstring-translation.php&context=Widgets' );
261
						echo '<div class="notice notice-warning inline"><p>';
262
						/* translators: Warning displayed when deprecated translations of the current widget are detected */
263
						echo sprintf( esc_html__( 'WARNING: This widget has one or more translations made using WPML String Translation, which is now a deprecated translation method. Starting from WPML 3.8 you should replicate this widget for each language and set the "Display on language" dropdown accordingly. Finally, you should delete the previously existing translations from %s.', 'black-studio-tinymce-widget' ), '<a href="' . esc_url( $wpml_st_url ) . '">WPML String Translation</a>' );
264
						echo '</p></div>';
265
					}
266
				}
267
			}
268
		}
269
270
	} // END class Black_Studio_TinyMCE_Compatibility_Plugin_Wpml
271
272
} // END class_exists check
273