Wpml::widget_text()   B
last analyzed

Complexity

Conditions 10
Paths 6

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
nc 6
nop 3
dl 0
loc 15
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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' ) );
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
		 * @return void
159
		 * @since 3.0.0
160
		 */
161
		public function disable_text_translation() {
162
			if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) ) {
163
				if ( false !== has_filter( 'widget_text', 'icl_sw_filters_widget_text' ) ) {
164
					remove_filter( 'widget_text', 'icl_sw_filters_widget_text', 0 );
165
					$this->removed_widget_text_filter = true;
166
				}
167
			}
168
		}
169
170
		/**
171
		 * Restore widget title's native WPML string translation filter if it was removed
172
		 *
173
		 * @uses add_filter()
174
		 * @uses has_filter()
175
		 *
176
		 * @return void
177
		 * @since 3.0.0
178
		 */
179
		public function restore_title_translation() {
180
			if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) ) {
181
				if ( $this->removed_widget_title_filter ) {
182
					if ( false === has_filter( 'widget_title', 'icl_sw_filters_widget_title' ) && function_exists( 'icl_sw_filters_widget_title' ) ) {
183
						add_filter( 'widget_title', 'icl_sw_filters_widget_title', 0 );
184
						$this->removed_widget_title_filter = false;
185
					}
186
				}
187
			}
188
		}
189
190
		/**
191
		 * Restore widget text's native WPML string translation filter if it was removed
192
		 *
193
		 * @uses add_filter()
194
		 * @uses has_filter()
195
		 *
196
		 * @return void
197
		 * @since 3.0.0
198
		 */
199
		public function restore_text_translation() {
200
			if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) ) {
201
				if ( $this->removed_widget_text_filter ) {
202
					if ( false === has_filter( 'widget_text', 'icl_sw_filters_widget_text' ) && function_exists( 'icl_sw_filters_widget_text' ) ) {
203
						add_filter( 'widget_text', 'icl_sw_filters_widget_text', 0 );
204
						$this->removed_widget_text_filter = false;
205
					}
206
				}
207
			}
208
		}
209
210
		/**
211
		 * Add widget text to WPML String translation
212
		 *
213
		 * @uses is_plugin_active()
214
		 * @uses icl_register_string() Part of WPML
215
		 *
216
		 * @param mixed[] $instance Array of arguments.
217
		 * @param object  $widget   Widget instance.
218
		 * @return mixed[]
219
		 * @since 3.0.0
220
		 */
221
		public function widget_update( $instance, $widget ) {
222
			if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) &&
223
				version_compare( $this->get_version(), '3.8.0' ) < 0 &&
224
				! is_plugin_active( 'wpml-widgets/wpml-widgets.php' )
225
			) {
226
				if ( function_exists( 'icl_register_string' ) && ! empty( $widget->number ) ) {
227
					// Avoid translation of Page Builder (SiteOrigin panels) and WP Page Widget widgets.
228
					if ( ! isset( $instance['panels_info'] ) && ! isset( $instance['wp_page_widget'] ) ) {
229
						icl_register_string( 'Widgets', 'widget body - ' . $widget->id_base . '-' . $widget->number, $instance['text'] );
230
					}
231
				}
232
			}
233
			return $instance;
234
		}
235
236
		/**
237
		 * Translate widget text
238
		 *
239
		 * @uses is_plugin_active()
240
		 * @uses icl_t() Part of WPML
241
		 * @uses icl_st_is_registered_string() Part of WPML
242
		 *
243
		 * @param string       $text     Widget text.
244
		 * @param mixed[]|null $instance Widget instance.
245
		 * @param object|null  $widget   Widget object.
246
		 * @return string
247
		 * @since 3.0.0
248
		 */
249
		public function widget_text( $text, $instance = null, $widget = null ) {
250
			if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) && ! is_plugin_active( 'wpml-widgets/wpml-widgets.php' ) ) {
251
				if ( bstw()->check_widget( $widget ) && ! empty( $instance ) ) {
252
					if ( function_exists( 'icl_t' ) && function_exists( 'icl_st_is_registered_string' ) ) {
253
						// Avoid translation of Page Builder (SiteOrigin panels) and WP Page Widget widgets.
254
						if ( ! isset( $instance['panels_info'] ) && ! isset( $instance['wp_page_widget'] ) ) {
255
							if ( icl_st_is_registered_string( 'Widgets', 'widget body - ' . $widget->id_base . '-' . $widget->number ) ) {
256
								$text = icl_t( 'Widgets', 'widget body - ' . $widget->id_base . '-' . $widget->number, $text );
257
							}
258
						}
259
					}
260
				}
261
			}
262
			return $text;
263
		}
264
265
		/**
266
		 * Check for existing deprecated translations (made with WPML String Translations plugin) and display warning
267
		 *
268
		 * @uses is_plugin_active()
269
		 * @uses icl_st_is_registered_string() Part of WPML
270
		 * @uses admin_url()
271
		 *
272
		 * @param mixed[]|null $instance Widget instance.
273
		 * @param object|null  $widget   Widget object.
274
		 * @return void
275
		 * @since 2.6.0
276
		 */
277
		public function check_deprecated_translations( $instance, $widget ) {
278
			if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) && version_compare( $this->get_version(), '3.8.0' ) >= 0 ) {
279
				if ( function_exists( 'icl_st_is_registered_string' ) ) {
280
					if ( icl_st_is_registered_string( 'Widgets', 'widget body - ' . $widget->id_base . '-' . $widget->number ) ) {
281
						$wpml_st_url = admin_url( 'admin.php?page=wpml-string-translation%2Fmenu%2Fstring-translation.php&context=Widgets' );
282
						echo '<div class="notice notice-warning inline"><p>';
283
						/* translators: Warning displayed when deprecated translations of the current widget are detected */
284
						$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' );
285
						echo wp_kses_post( sprintf( esc_html( $warning ), '<a href="' . esc_url( $wpml_st_url ) . '">WPML String Translation</a>' ) );
286
						echo '</p></div>';
287
					}
288
				}
289
			}
290
		}
291
292
	} // END class
293
294
} // END class_exists
295