PluginProbe
Elementor Website Builder – more than just a page builder / 3.2.4
Elementor Website Builder – more than just a page builder v3.2.4
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / modules / dev-tools / deprecation.php

deprecation.php in Elementor Website Builder – more than just a page builder 3.2.4, at modules/dev-tools/deprecation.php

353 lines 8.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor\Modules\DevTools;
3
4 if ( ! defined( 'ABSPATH' ) ) {
5 exit; // Exit if accessed directly.
6 }
7
8 class Deprecation {
9 const SOFT_VERSIONS_COUNT = 4;
10 const HARD_VERSIONS_COUNT = 8;
11
12 private $current_version = null;
13 private $soft_deprecated_notices = [];
14
15 public function __construct( $current_version ) {
16 $this->current_version = $current_version;
17 }
18
19 public function get_settings() {
20 return [
21 'soft_notices' => $this->soft_deprecated_notices,
22 ];
23 }
24
25 /**
26 * Get total of major.
27 *
28 * Since `get_total_major` cannot determine how much really versions between 2.9.0 and 3.3.0 if there is 2.10.0 version for example,
29 * versions with major2 more then 9 will be added to total.
30 *
31 * @since 3.1.0
32 *
33 * @param array $parsed_version
34 *
35 * @return int
36 */
37 public function get_total_major( $parsed_version ) {
38 $major1 = $parsed_version['major1'];
39 $major2 = $parsed_version['major2'];
40 $major2 = $major2 > 9 ? 9 : $major2;
41 $minor = 0;
42
43 $total = intval( "{$major1}{$major2}{$minor}" );
44
45 if ( $total > 99 ) {
46 $total = $total / 10;
47 } else {
48 $total = intval( $total / 10 );
49 }
50
51 if ( $parsed_version['major2'] > 9 ) {
52 $total += $parsed_version['major2'] - 9;
53 }
54
55 return $total;
56 }
57
58 /**
59 * Get next version.
60 *
61 * @since 3.1.0
62 *
63 * @param string $version
64 * @param int $count
65 *
66 * @return string|false
67 */
68 public function get_next_version( $version, $count = 1 ) {
69 $version = $this->parse_version( $version );
70
71 if ( ! $version ) {
72 return false;
73 }
74
75 $version['total'] = $this->get_total_major( $version ) + $count;
76
77 $total = $version['total'];
78
79 if ( $total > 9 ) {
80 $version['major1'] = intval( $total / 10 );
81 $version['major2'] = $total % 10;
82 } else {
83 $version['major1'] = 0;
84 $version['major2'] = $total;
85 }
86
87 $version['minor'] = 0;
88
89 return $this->implode_version( $version );
90 }
91
92 /**
93 * Implode parsed version to string version.
94 *
95 * @since 3.1.0
96 *
97 * @param array $parsed_version
98 *
99 * @return string
100 */
101 public function implode_version( $parsed_version ) {
102 $major1 = $parsed_version['major1'];
103 $major2 = $parsed_version['major2'];
104 $minor = $parsed_version['minor'];
105
106 return "{$major1}.{$major2}.{$minor}";
107 }
108
109 /**
110 * Parse to an informative array.
111 *
112 * @since 3.1.0
113 *
114 * @param string $version
115 *
116 * @return array|false
117 */
118 public function parse_version( $version ) {
119 $version_explode = explode( '.', $version );
120 $version_explode_count = count( $version_explode );
121
122 if ( $version_explode_count < 3 || $version_explode_count > 4 ) {
123 trigger_error( 'Invalid Semantic Version string provided' );
124
125 return false;
126 }
127
128 list( $major1, $major2, $minor ) = $version_explode;
129
130 $result = [
131 'major1' => intval( $major1 ),
132 'major2' => intval( $major2 ),
133 'minor' => intval( $minor ),
134 ];
135
136 if ( $version_explode_count > 3 ) {
137 $result['build'] = $version_explode[3];
138 }
139
140 return $result;
141 }
142
143 /**
144 * Compare two versions, result is equal to diff of major versions.
145 * Notice: If you want to compare between 2.9.0 and 3.3.0, and there is also a 2.10.0 version, you cannot get the right comparison
146 * Since $this->deprecation->get_total_major cannot determine how much really versions between 2.9.0 and 3.3.0.
147 *
148 * @since 3.1.0
149 *
150 * @param {string} $version1
151 * @param {string} $version2
152 *
153 * @return int|false
154 */
155 public function compare_version( $version1, $version2 ) {
156 $version1 = self::parse_version( $version1 );
157 $version2 = self::parse_version( $version2 );
158
159 if ( $version1 && $version2 ) {
160 $versions = [ &$version1, &$version2 ];
161
162 foreach ( $versions as &$version ) {
163 $version['total'] = self::get_total_major( $version );
164 }
165
166 return $version1['total'] - $version2['total'];
167 }
168
169 return false;
170 }
171
172 /**
173 * Check Deprecation
174 *
175 * Checks whether the given entity is valid. If valid, this method checks whether the deprecation
176 * should be soft (browser console notice) or hard (use WordPress' native deprecation methods).
177 *
178 * @since 3.1.0
179 *
180 * @param string $entity - The Deprecated entity (the function/hook itself)
181 * @param string $version
182 * @param string $replacement Optional
183 * @param string $base_version Optional. Default is `null`
184 *
185 * @return bool|void
186 * @throws \Exception
187 */
188 private function check_deprecation( $entity, $version, $replacement, $base_version = null ) {
189 if ( null === $base_version ) {
190 $base_version = $this->current_version;
191 }
192
193 $diff = $this->compare_version( $base_version, $version );
194
195 if ( false === $diff ) {
196 throw new \Exception( 'Invalid deprecation diff' );
197 }
198
199 $print_deprecated = false;
200
201 if ( defined( 'WP_DEBUG' ) && WP_DEBUG && $diff <= self::SOFT_VERSIONS_COUNT ) {
202 // Soft deprecated.
203 if ( ! isset( $this->soft_deprecated_notices[ $entity ] ) ) {
204 $this->soft_deprecated_notices[ $entity ] = [
205 $version,
206 $replacement,
207 ];
208 }
209
210 if ( defined( 'ELEMENTOR_DEBUG' ) && ELEMENTOR_DEBUG ) {
211 $print_deprecated = true;
212 }
213 } else {
214 // Hard deprecated.
215 $print_deprecated = true;
216 }
217
218 return $print_deprecated;
219 }
220
221 /**
222 * Deprecated Function
223 *
224 * Handles the deprecation process for functions.
225 *
226 * @since 3.1.0
227 *
228 * @param string $function
229 * @param string $version
230 * @param string $replacement Optional. Default is ''
231 * @param string $base_version Optional. Default is `null`
232 * @throws \Exception
233 */
234 public function deprecated_function( $function, $version, $replacement = '', $base_version = null ) {
235 $print_deprecated = $this->check_deprecation( $function, $version, $replacement, $base_version );
236
237 if ( $print_deprecated ) {
238 _deprecated_function( $function, $version, $replacement );
239 }
240 }
241
242 /**
243 * Deprecated Hook
244 *
245 * Handles the deprecation process for hooks.
246 *
247 * @since 3.1.0
248 *
249 * @param string $hook
250 * @param string $version
251 * @param string $replacement Optional. Default is ''
252 * @param string $base_version Optional. Default is `null`
253 * @throws \Exception
254 */
255 public function deprecated_hook( $hook, $version, $replacement = '', $base_version = null ) {
256 $print_deprecated = $this->check_deprecation( $hook, $version, $replacement, $base_version );
257
258 if ( $print_deprecated ) {
259 _deprecated_hook( $hook, $version, $replacement );
260 }
261 }
262
263 /**
264 * Deprecated Argument
265 *
266 * Handles the deprecation process for function arguments.
267 *
268 * @since 3.1.0
269 *
270 * @param string $argument
271 * @param string $version
272 * @param string $replacement
273 * @param string $message
274 * @throws \Exception
275 */
276 public function deprecated_argument( $argument, $version, $replacement = '', $message = '' ) {
277 $print_deprecated = $this->check_deprecation( $argument, $version, $replacement );
278
279 if ( $print_deprecated ) {
280 $message = empty( $message ) ? '' : ' ' . $message;
281 $error_message_args = [ $argument, $version ];
282
283 if ( $replacement ) {
284 /* translators: 1: Function argument, 2: Elementor version number, 3: Replacement argument name. */
285 $translation_string = __( 'The %1$s argument is <strong>deprecated</strong> since version %2$s! Use %3$s instead.', 'elementor' );
286 $error_message_args[] = $replacement;
287 } else {
288 /* translators: 1: Function argument, 2: Elementor version number. */
289 $translation_string = __( 'The %1$s argument is <strong>deprecated</strong> since version %2$s!', 'elementor' );
290 }
291
292 trigger_error(
293 vsprintf(
294 $translation_string,
295 $error_message_args
296 ) . $message,
297 E_USER_DEPRECATED
298 );
299 }
300 }
301
302 /**
303 * Do Deprecated Action
304 *
305 * A method used to run deprecated actions through Elementor's deprecation process.
306 *
307 * @since 3.1.0
308 *
309 * @param string $hook
310 * @param array $args
311 * @param string $version
312 * @param string $replacement
313 * @param null|string $base_version
314 *
315 * @throws \Exception
316 */
317 public function do_deprecated_action( $hook, $args, $version, $replacement = '', $base_version = null ) {
318 if ( ! has_action( $hook ) ) {
319 return;
320 }
321
322 $this->deprecated_hook( $hook, $version, $replacement, $base_version );
323
324 do_action_ref_array( $hook, $args );
325 }
326
327 /**
328 * Apply Deprecated Filter
329 *
330 * A method used to run deprecated filters through Elementor's deprecation process.
331 *
332 * @since 3.2.0
333 *
334 * @param string $hook
335 * @param array $args
336 * @param string $version
337 * @param string $replacement
338 * @param null|string $base_version
339 *
340 * @return mixed
341 * @throws \Exception
342 */
343 public function apply_deprecated_filter( $hook, $args, $version, $replacement = '', $base_version = null ) {
344 if ( ! has_action( $hook ) ) {
345 return;
346 }
347
348 $this->deprecated_hook( $hook, $version, $replacement, $base_version );
349
350 return apply_filters_ref_array( $hook, $args );
351 }
352 }
353