Completed
Push — develop ( 56a932...e33754 )
by Marco
01:25
created

Wpml::restore_text_translation()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 6
c 0
b 0
f 0
nc 4
nop 0
dl 0
loc 10
rs 8.8571
1
<?php
2
/**
3
 * Black Studio TinyMCE Widget - Compatibility with WPML plugin(s)
4
 *
5
 * @package Black_Studio_TinyMCE_Widget
6
 */
7
8
namespace Black_Studio_TinyMCE_Widget\Compatibility\Plugin;
9
10
// Exit if accessed directly.
11
if ( ! defined( 'ABSPATH' ) ) {
12
	exit;
13
}
14
15
if ( ! class_exists( 'Black_Studio_TinyMCE_Widget\\Compatibility\\Plugin\\Wpml', false ) ) {
16
	/**
17
	 * Class that provides compatibility with WPML plugin(s)
18
	 *
19
	 * @package Black_Studio_TinyMCE_Widget
20
	 * @since 3.0.0
21
	 */
22
	final class Wpml {
23
24
		/**
25
		 * The single instance of the class
26
		 *
27
		 * @var object
28
		 * @since 3.0.0
29
		 */
30
		protected static $_instance = null;
31
32
		/**
33
		 * Flag to keep track of removed WPML filter on widget title
34
		 *
35
		 * @var boolean
36
		 * @since 3.0.0
37
		 */
38
		private $removed_widget_title_filter = false;
39
40
		/**
41
		 * Flag to keep track of removed WPML filter on widget text
42
		 *
43
		 * @var boolean
44
		 * @since 3.0.0
45
		 */
46
		private $removed_widget_text_filter = false;
47
48
		/**
49
		 * Return the single class instance
50
		 *
51
		 * @return object
52
		 * @since 3.0.0
53
		 */
54
		public static function instance() {
55
			if ( is_null( self::$_instance ) ) {
56
				self::$_instance = new self();
57
			}
58
			return self::$_instance;
59
		}
60
61
		/**
62
		 * Class constructor
63
		 *
64
		 * @uses add_action()
65
		 * @uses add_filter()
66
		 *
67
		 * @since 3.0.0
68
		 */
69
		protected function __construct() {
70
			add_action( 'init', array( $this, 'init' ) );
71
			add_action( 'black_studio_tinymce_before_widget', array( $this, 'disable_title_translation' ), 10, 2 );
72
			add_action( 'black_studio_tinymce_before_widget', array( $this, 'disable_text_translation' ), 10, 2 );
73
			add_action( 'black_studio_tinymce_after_widget', array( $this, 'restore_title_translation' ) );
74
			add_action( 'black_studio_tinymce_after_widget', array( $this, 'restore_text_translation' ) );
75
			add_filter( 'black_studio_tinymce_widget_update', array( $this, 'widget_update' ), 10, 2 );
76
			add_action( 'black_studio_tinymce_before_editor', array( $this, 'check_deprecated_translations' ), 5, 2 );
77
			add_filter( 'widget_text', array( $this, 'widget_text' ), 2, 3 );
78
			if ( ! function_exists( 'is_plugin_active' ) ) {
79
				include_once ABSPATH . 'wp-admin/includes/plugin.php';
80
			}
81
		}
82
83
		/**
84
		 * Prevent the class from being cloned
85
		 *
86
		 * @return void
87
		 * @since 3.0.0
88
		 */
89
		protected function __clone() {
90
			_doing_it_wrong( __FUNCTION__, esc_html__( 'Cheatin&#8217; uh?' ), '3.0' );
91
		}
92
93
		/**
94
		 * Helper function to get WPML version
95
		 *
96
		 * @uses get_plugin_data()
97
		 *
98
		 * @return string
99
		 * @since 3.0.0
100
		 */
101
		public function get_version() {
102
			$plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/sitepress-multilingual-cms/sitepress.php', false, false );
103
			return $plugin_data['Version'];
104
		}
105
106
		/**
107
		 * Initialize compatibility with WPML and WPML Widgets plugins
108
		 *
109
		 * @uses is_plugin_active()
110
		 * @uses has_action()
111
		 * @uses remove_action()
112
		 *
113
		 * @return void
114
		 * @since 3.0.0
115
		 */
116
		public function init() {
117
			if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) && is_plugin_active( 'wpml-widgets/wpml-widgets.php' ) ) {
118
				if ( false !== has_action( 'update_option_widget_black-studio-tinymce', 'icl_st_update_widget_title_actions' ) ) {
119
					remove_action( 'update_option_widget_black-studio-tinymce', 'icl_st_update_widget_title_actions', 5 );
120
				}
121
			}
122
		}
123
124
		/**
125
		 * Avoid native WPML string translation of widget titles
126
		 * for widgets inserted in pages built with Page Builder (SiteOrigin panels)
127
		 * and also when WPML Widgets is active and for WPML versions from 3.8.0 on
128
		 *
129
		 * @uses is_plugin_active()
130
		 * @uses has_filter()
131
		 * @uses remove_filter()
132
		 *
133
		 * @param mixed[] $args     Array of arguments.
134
		 * @param mixed[] $instance Widget instance.
135
		 * @return void
136
		 * @since 3.0.0
137
		 */
138
		public function disable_title_translation( $args, $instance ) {
139
			if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) ) {
140
				if ( false !== has_filter( 'widget_title', 'icl_sw_filters_widget_title' ) ) {
141
					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 ) {
142
						remove_filter( 'widget_title', 'icl_sw_filters_widget_title', 0 );
143
						$this->removed_widget_title_filter = true;
144
					}
145
				}
146
			}
147
		}
148
149
		/**
150
		 * Avoid native WPML string translation of widget texts (for all widgets)
151
		 * Note: Black Studio TinyMCE Widget already supports WPML string translation,
152
		 * so this is needed to prevent duplicate translations
153
		 *
154
		 * @uses is_plugin_active()
155
		 * @uses has_filter()
156
		 * @uses remove_filter()
157
		 *
158
		 * @param mixed[] $args     Array of arguments.
159
		 * @param mixed[] $instance Widget instance.
160
		 * @return void
161
		 * @since 3.0.0
162
		 */
163
		public function disable_text_translation( $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...
164
			if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) ) {
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->removed_widget_text_filter = true;
168
				}
169
			}
170
		}
171
172
		/**
173
		 * Restore widget title's native WPML string translation filter if it was removed
174
		 *
175
		 * @uses add_filter()
176
		 * @uses has_filter()
177
		 *
178
		 * @return void
179
		 * @since 3.0.0
180
		 */
181
		public function restore_title_translation() {
182
			if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) ) {
183
				if ( $this->removed_widget_title_filter ) {
184
					if ( false === has_filter( 'widget_title', 'icl_sw_filters_widget_title' ) && function_exists( 'icl_sw_filters_widget_title' ) ) {
185
						add_filter( 'widget_title', 'icl_sw_filters_widget_title', 0 );
186
						$this->removed_widget_title_filter = false;
187
					}
188
				}
189
			}
190
		}
191
192
		/**
193
		 * Restore widget text's native WPML string translation filter if it was removed
194
		 *
195
		 * @uses add_filter()
196
		 * @uses has_filter()
197
		 *
198
		 * @return void
199
		 * @since 3.0.0
200
		 */
201
		public function restore_text_translation() {
202
			if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) ) {
203
				if ( $this->removed_widget_text_filter ) {
204
					if ( false === has_filter( 'widget_text', 'icl_sw_filters_widget_text' ) && function_exists( 'icl_sw_filters_widget_text' ) ) {
205
						add_filter( 'widget_text', 'icl_sw_filters_widget_text', 0 );
206
						$this->removed_widget_text_filter = false;
207
					}
208
				}
209
			}
210
		}
211
212
		/**
213
		 * Add widget text to WPML String translation
214
		 *
215
		 * @uses is_plugin_active()
216
		 * @uses icl_register_string() Part of WPML
217
		 *
218
		 * @param mixed[] $instance Array of arguments.
219
		 * @param object  $widget   Widget instance.
220
		 * @return mixed[]
221
		 * @since 3.0.0
222
		 */
223
		public function widget_update( $instance, $widget ) {
224
			if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) &&
225
				version_compare( $this->get_version(), '3.8.0' ) < 0 &&
226
				! is_plugin_active( 'wpml-widgets/wpml-widgets.php' )
227
			) {
228
				if ( function_exists( 'icl_register_string' ) && ! empty( $widget->number ) ) {
229
					// Avoid translation of Page Builder (SiteOrigin panels) and WP Page Widget widgets.
230
					if ( ! isset( $instance['panels_info'] ) && ! isset( $instance['wp_page_widget'] ) ) {
231
						icl_register_string( 'Widgets', 'widget body - ' . $widget->id_base . '-' . $widget->number, $instance['text'] );
232
					}
233
				}
234
			}
235
			return $instance;
236
		}
237
238
		/**
239
		 * Translate widget text
240
		 *
241
		 * @uses is_plugin_active()
242
		 * @uses icl_t() Part of WPML
243
		 * @uses icl_st_is_registered_string() Part of WPML
244
		 *
245
		 * @param string       $text     Widget text.
246
		 * @param mixed[]|null $instance Widget instance.
247
		 * @param object|null  $widget   Widget object.
248
		 * @return string
249
		 * @since 3.0.0
250
		 */
251
		public function widget_text( $text, $instance = null, $widget = null ) {
252
			if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) && ! is_plugin_active( 'wpml-widgets/wpml-widgets.php' ) ) {
253
				if ( bstw()->check_widget( $widget ) && ! empty( $instance ) ) {
254
					if ( function_exists( 'icl_t' ) && function_exists( 'icl_st_is_registered_string' ) ) {
255
						// Avoid translation of Page Builder (SiteOrigin panels) and WP Page Widget widgets.
256
						if ( ! isset( $instance['panels_info'] ) && ! isset( $instance['wp_page_widget'] ) ) {
257
							if ( icl_st_is_registered_string( 'Widgets', 'widget body - ' . $widget->id_base . '-' . $widget->number ) ) {
258
								$text = icl_t( 'Widgets', 'widget body - ' . $widget->id_base . '-' . $widget->number, $text );
259
							}
260
						}
261
					}
262
				}
263
			}
264
			return $text;
265
		}
266
267
		/**
268
		 * Check for existing deprecated translations (made with WPML String Translations plugin) and display warning
269
		 *
270
		 * @uses is_plugin_active()
271
		 * @uses icl_st_is_registered_string() Part of WPML
272
		 * @uses admin_url()
273
		 *
274
		 * @param mixed[]|null $instance Widget instance.
275
		 * @param object|null  $widget   Widget object.
276
		 * @return void
277
		 * @since 2.6.0
278
		 */
279
		public function check_deprecated_translations( $instance, $widget ) {
280
			if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) && version_compare( $this->get_version(), '3.8.0' ) >= 0 ) {
281
				if ( function_exists( 'icl_st_is_registered_string' ) ) {
282
					if ( icl_st_is_registered_string( 'Widgets', 'widget body - ' . $widget->id_base . '-' . $widget->number ) ) {
283
						$wpml_st_url = admin_url( 'admin.php?page=wpml-string-translation%2Fmenu%2Fstring-translation.php&context=Widgets' );
284
						echo '<div class="notice notice-warning inline"><p>';
285
						/* translators: Warning displayed when deprecated translations of the current widget are detected */
286
						$warning = __( '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' );
287
						echo wp_kses_post( sprintf( esc_html( $warning ), '<a href="' . esc_url( $wpml_st_url ) . '">WPML String Translation</a>' ) );
288
						echo '</p></div>';
289
					}
290
				}
291
			}
292
		}
293
294
	} // END class
295
296
} // END class_exists
297