Completed
Push — develop ( 85c2ba...5fea9a )
by Marco
04:56
created

Wpml::widget_before()   C

Complexity

Conditions 8
Paths 7

Size

Total Lines 26
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 9
nc 7
nop 2
dl 0
loc 26
rs 5.3846
c 0
b 0
f 0
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, 'widget_before' ), 10, 2 );
72
			add_action( 'black_studio_tinymce_after_widget', array( $this, 'widget_after' ) );
73
			add_filter( 'black_studio_tinymce_widget_update', array( $this, 'widget_update' ), 10, 2 );
74
			add_action( 'black_studio_tinymce_before_editor', array( $this, 'check_deprecated_translations' ), 5, 2 );
75
			add_filter( 'widget_text', array( $this, 'widget_text' ), 2, 3 );
76
			if ( ! function_exists( 'is_plugin_active' ) ) {
77
				include_once ABSPATH . 'wp-admin/includes/plugin.php';
78
			}
79
		}
80
81
		/**
82
		 * Prevent the class from being cloned
83
		 *
84
		 * @return void
85
		 * @since 3.0.0
86
		 */
87
		protected function __clone() {
88
			_doing_it_wrong( __FUNCTION__, esc_html__( 'Cheatin&#8217; uh?' ), '3.0' );
89
		}
90
91
		/**
92
		 * Helper function to get WPML version
93
		 *
94
		 * @uses get_plugin_data()
95
		 *
96
		 * @return string
97
		 * @since 3.0.0
98
		 */
99
		public function get_version() {
100
			$plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/sitepress-multilingual-cms/sitepress.php', false, false );
101
			return $plugin_data['Version'];
102
		}
103
104
		/**
105
		 * Initialize compatibility with WPML and WPML Widgets plugins
106
		 *
107
		 * @uses is_plugin_active()
108
		 * @uses has_action()
109
		 * @uses remove_action()
110
		 *
111
		 * @return void
112
		 * @since 3.0.0
113
		 */
114
		public function init() {
115
			if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) && is_plugin_active( 'wpml-widgets/wpml-widgets.php' ) ) {
116
				if ( false !== has_action( 'update_option_widget_black-studio-tinymce', 'icl_st_update_widget_title_actions' ) ) {
117
					remove_action( 'update_option_widget_black-studio-tinymce', 'icl_st_update_widget_title_actions', 5 );
118
				}
119
			}
120
		}
121
122
		/**
123
		 * Disable WPML String translation native behavior
124
		 *
125
		 * @uses is_plugin_active()
126
		 * @uses has_filter()
127
		 * @uses remove_filter()
128
		 *
129
		 * @param mixed[] $args     Array of arguments.
130
		 * @param mixed[] $instance Widget instance.
131
		 * @return void
132
		 * @since 3.0.0
133
		 */
134
		public function widget_before( $args, $instance ) {
135
			if ( is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) ) {
136
				/*
137
				Avoid native WPML string translation of widget titles
138
				for widgets inserted in pages built with Page Builder (SiteOrigin panels)
139
				and also when WPML Widgets is active and for WPML versions from 3.8.0 on
140
				*/
141
				if ( false !== has_filter( 'widget_title', 'icl_sw_filters_widget_title' ) ) {
142
					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 ) {
143
						remove_filter( 'widget_title', 'icl_sw_filters_widget_title', 0 );
144
						$this->removed_widget_title_filter = true;
145
					}
146
				}
147
148
				/*
149
				Avoid native WPML string translation of widget texts (for all widgets)
150
				Note: Black Studio TinyMCE Widget already supports WPML string translation,
151
				so this is needed to prevent duplicate translations
152
				*/
153
				if ( false !== has_filter( 'widget_text', 'icl_sw_filters_widget_text' ) ) {
154
					remove_filter( 'widget_text', 'icl_sw_filters_widget_text', 0 );
155
					$this->removed_widget_text_filter = true;
156
				}
157
			}
158
159
		}
160
161
		/**
162
		 * Re-Enable WPML String translation native behavior
163
		 *
164
		 * @uses add_filter()
165
		 * @uses has_filter()
166
		 *
167
		 * @return void
168
		 * @since 3.0.0
169
		 */
170
		public function widget_after() {
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->removed_widget_text_filter = false;
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>' );
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'sprintf'
Loading history...
264
						echo '</p></div>';
265
					}
266
				}
267
			}
268
		}
269
270
	} // END class
271
272
} // END class_exists
273