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

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

Severity

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