Completed
Push — fix/local_script_translation_p... ( f509ec...67e374 )
by
unknown
18:29 queued 11:00
created

functions.global.php ➔ jetpack_get_future_removed_version()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 30
rs 9.44
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is meant to be the home for any generic & reusable functions
4
 * that can be accessed anywhere within Jetpack.
5
 *
6
 * This file is loaded whether or not Jetpack is active.
7
 *
8
 * Please namespace with jetpack_
9
 *
10
 * @package Jetpack
11
 */
12
13
use Automattic\Jetpack\Connection\Client;
14
use Automattic\Jetpack\Redirect;
15
use Automattic\Jetpack\Device_Detection;
16
17
/**
18
 * Disable direct access.
19
 */
20
if ( ! defined( 'ABSPATH' ) ) {
21
	exit;
22
}
23
24
/**
25
 * Hook into Core's _deprecated_function
26
 * Add more details about when a deprecated function will be removed.
27
 *
28
 * @since 8.8.0
29
 *
30
 * @param string $function    The function that was called.
31
 * @param string $replacement Optional. The function that should have been called. Default null.
32
 * @param string $version     The version of Jetpack that deprecated the function.
33
 */
34 View Code Duplication
function jetpack_deprecated_function( $function, $replacement, $version ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
35
	// Bail early for non-Jetpack deprecations.
36
	if ( 0 !== strpos( $version, 'jetpack-' ) ) {
37
		return;
38
	}
39
40
	// Look for when a function will be removed based on when it was deprecated.
41
	$removed_version = jetpack_get_future_removed_version( $version );
42
43
	// If we could find a version, let's log a message about when removal will happen.
44
	if (
45
		! empty( $removed_version )
46
		&& ( defined( 'WP_DEBUG' ) && WP_DEBUG )
47
		/** This filter is documented in core/src/wp-includes/functions.php */
48
		&& apply_filters( 'deprecated_function_trigger_error', true )
49
	) {
50
		error_log( // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
51
			sprintf(
52
				/* Translators: 1. Function name. 2. Jetpack version number. */
53
				__( 'The %1$s function will be removed from the Jetpack plugin in version %2$s.', 'jetpack' ),
54
				$function,
55
				$removed_version
56
			)
57
		);
58
59
	}
60
}
61
add_action( 'deprecated_function_run', 'jetpack_deprecated_function', 10, 3 );
62
63
/**
64
 * Hook into Core's _deprecated_file
65
 * Add more details about when a deprecated file will be removed.
66
 *
67
 * @since 8.8.0
68
 *
69
 * @param string $file        The file that was called.
70
 * @param string $replacement The file that should have been included based on ABSPATH.
71
 * @param string $version     The version of WordPress that deprecated the file.
72
 * @param string $message     A message regarding the change.
73
 */
74 View Code Duplication
function jetpack_deprecated_file( $file, $replacement, $version, $message ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75
	// Bail early for non-Jetpack deprecations.
76
	if ( 0 !== strpos( $version, 'jetpack-' ) ) {
77
		return;
78
	}
79
80
	// Look for when a file will be removed based on when it was deprecated.
81
	$removed_version = jetpack_get_future_removed_version( $version );
82
83
	// If we could find a version, let's log a message about when removal will happen.
84
	if (
85
		! empty( $removed_version )
86
		&& ( defined( 'WP_DEBUG' ) && WP_DEBUG )
87
		/** This filter is documented in core/src/wp-includes/functions.php */
88
		&& apply_filters( 'deprecated_file_trigger_error', true )
89
	) {
90
		error_log( // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
91
			sprintf(
92
				/* Translators: 1. File name. 2. Jetpack version number. */
93
				__( 'The %1$s file will be removed from the Jetpack plugin in version %2$s.', 'jetpack' ),
94
				$file,
95
				$removed_version
96
			)
97
		);
98
99
	}
100
}
101
add_action( 'deprecated_file_included', 'jetpack_deprecated_file', 10, 4 );
102
103
/**
104
 * Get the major version number of Jetpack 6 months after provided version.
105
 * Useful to indicate when a deprecated function will be removed from Jetpack.
106
 *
107
 * @since 8.8.0
108
 *
109
 * @param string $version The version of WordPress that deprecated the function.
110
 *
111
 * @return bool|float Return a Jetpack Major version number, or false.
112
 */
113
function jetpack_get_future_removed_version( $version ) {
114
	/*
115
	 * Extract the version number from a deprecation notice.
116
	 * (let's only keep the first decimal, e.g. 8.8 and not 8.8.0)
117
	 */
118
	preg_match( '#(([0-9]+\.([0-9]+))(?:\.[0-9]+)*)#', $version, $matches );
119
120
	if ( isset( $matches[2], $matches[3] ) ) {
121
		$deprecated_version = (float) $matches[2];
122
		$deprecated_minor   = (float) $matches[3];
123
124
		/*
125
		 * If the detected minor version number
126
		 * (e.g. "7" in "8.7")
127
		 * is higher than 9, we know the version number is malformed.
128
		 * Jetpack does not use semver yet.
129
		 * Bail.
130
		 */
131
		if ( 10 <= $deprecated_minor ) {
132
			return false;
133
		}
134
135
		// We'll remove the function from the code 6 months later, thus 6 major versions later.
136
		$removed_version = $deprecated_version + 0.6;
137
138
		return (float) $removed_version;
139
	}
140
141
	return false;
142
}
143
144
/**
145
 * Set the admin language, based on user language.
146
 *
147
 * @since 4.5.0
148
 * @deprecated 6.6.0 Use Core function instead.
149
 *
150
 * @return string
151
 */
152
function jetpack_get_user_locale() {
153
	_deprecated_function( __FUNCTION__, 'jetpack-6.6.0', 'get_user_locale' );
154
	return get_user_locale();
155
}
156
157
/**
158
 * Determine if this site is an Atomic site or not looking first at the 'at_options' option.
159
 * As a fallback, check for presence of wpcomsh plugin to determine if a current site has undergone AT.
160
 *
161
 * @since 4.8.1
162
 *
163
 * @return bool
164
 */
165
function jetpack_is_atomic_site() {
166
	$at_options = get_option( 'at_options', array() );
167
	return ! empty( $at_options ) || defined( 'WPCOMSH__PLUGIN_FILE' );
168
}
169
170
/**
171
 * Register post type for migration.
172
 *
173
 * @since 5.2
174
 */
175
function jetpack_register_migration_post_type() {
176
	register_post_type(
177
		'jetpack_migration',
178
		array(
179
			'supports'     => array(),
180
			'taxonomies'   => array(),
181
			'hierarchical' => false,
182
			'public'       => false,
183
			'has_archive'  => false,
184
			'can_export'   => true,
185
		)
186
	);
187
}
188
189
/**
190
 * Stores migration data in the database.
191
 *
192
 * @since 5.2
193
 *
194
 * @param string $option_name  Option name.
195
 * @param bool   $option_value Option value.
196
 *
197
 * @return int|WP_Error
198
 */
199
function jetpack_store_migration_data( $option_name, $option_value ) {
200
	jetpack_register_migration_post_type();
201
202
	$insert = array(
203
		'post_title'            => $option_name,
204
		'post_content_filtered' => $option_value,
205
		'post_type'             => 'jetpack_migration',
206
		'post_date'             => gmdate( 'Y-m-d H:i:s', time() ),
207
	);
208
209
	$post = get_page_by_title( $option_name, 'OBJECT', 'jetpack_migration' );
210
211
	if ( null !== $post ) {
212
		$insert['ID'] = $post->ID;
213
	}
214
215
	return wp_insert_post( $insert, true );
216
}
217
218
/**
219
 * Retrieves legacy image widget data.
220
 *
221
 * @since 5.2
222
 *
223
 * @param string $option_name Option name.
224
 *
225
 * @return mixed|null
226
 */
227
function jetpack_get_migration_data( $option_name ) {
228
	$post = get_page_by_title( $option_name, 'OBJECT', 'jetpack_migration' );
229
230
	return null !== $post ? maybe_unserialize( $post->post_content_filtered ) : null;
231
}
232
233
/**
234
 * Prints a TOS blurb used throughout the connection prompts.
235
 *
236
 * @since 5.3
237
 *
238
 * @echo string
239
 */
240
function jetpack_render_tos_blurb() {
241
	printf(
242
		wp_kses(
243
			/* Translators: placeholders are links. */
244
			__( 'By clicking the <strong>Set up Jetpack</strong> button, you agree to our <a href="%1$s" target="_blank" rel="noopener noreferrer">Terms of Service</a> and to <a href="%2$s" target="_blank" rel="noopener noreferrer">share details</a> with WordPress.com.', 'jetpack' ),
245
			array(
246
				'a'      => array(
247
					'href'   => array(),
248
					'target' => array(),
249
					'rel'    => array(),
250
				),
251
				'strong' => true,
252
			)
253
		),
254
		esc_url( Redirect::get_url( 'wpcom-tos' ) ),
255
		esc_url( Redirect::get_url( 'jetpack-support-what-data-does-jetpack-sync' ) )
256
	);
257
}
258
259
/**
260
 * Intervene upgrade process so Jetpack themes are downloaded with credentials.
261
 *
262
 * @since 5.3
263
 *
264
 * @param bool   $preempt Whether to preempt an HTTP request's return value. Default false.
265
 * @param array  $r       HTTP request arguments.
266
 * @param string $url     The request URL.
267
 *
268
 * @return array|bool|WP_Error
269
 */
270
function jetpack_theme_update( $preempt, $r, $url ) {
271
	if ( false !== stripos( $url, JETPACK__WPCOM_JSON_API_HOST . '/rest/v1/themes/download' ) ) {
272
		$file = $r['filename'];
273
		if ( ! $file ) {
274
			return new WP_Error( 'problem_creating_theme_file', esc_html__( 'Problem creating file for theme download', 'jetpack' ) );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'problem_creating_theme_file'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
275
		}
276
		$theme = pathinfo( wp_parse_url( $url, PHP_URL_PATH ), PATHINFO_FILENAME );
0 ignored issues
show
Unused Code introduced by
The call to wp_parse_url() has too many arguments starting with PHP_URL_PATH.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
277
278
		// Remove filter to avoid endless loop since wpcom_json_api_request_as_blog uses this too.
279
		remove_filter( 'pre_http_request', 'jetpack_theme_update' );
280
		$result = Client::wpcom_json_api_request_as_blog(
281
			"themes/download/$theme.zip",
282
			'1.1',
283
			array(
284
				'stream'   => true,
285
				'filename' => $file,
286
			)
287
		);
288
289
		if ( 200 !== wp_remote_retrieve_response_code( $result ) ) {
290
			return new WP_Error( 'problem_fetching_theme', esc_html__( 'Problem downloading theme', 'jetpack' ) );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'problem_fetching_theme'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
291
		}
292
		return $result;
293
	}
294
	return $preempt;
295
}
296
297
/**
298
 * Add the filter when a upgrade is going to be downloaded.
299
 *
300
 * @since 5.3
301
 *
302
 * @param bool $reply Whether to bail without returning the package. Default false.
303
 *
304
 * @return bool
305
 */
306
function jetpack_upgrader_pre_download( $reply ) {
307
	add_filter( 'pre_http_request', 'jetpack_theme_update', 10, 3 );
308
	return $reply;
309
}
310
311
add_filter( 'upgrader_pre_download', 'jetpack_upgrader_pre_download' );
312
313
314
/**
315
 * Wraps data in a way so that we can distinguish between objects and array and also prevent object recursion.
316
 *
317
 * @since 6.1.0
318
 *
319
 * @param array|obj $any        Source data to be cleaned up.
320
 * @param array     $seen_nodes Built array of nodes.
321
 *
322
 * @return array
323
 */
324 View Code Duplication
function jetpack_json_wrap( &$any, $seen_nodes = array() ) {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
325
	if ( is_object( $any ) ) {
326
		$input        = get_object_vars( $any );
327
		$input['__o'] = 1;
328
	} else {
329
		$input = &$any;
330
	}
331
332
	if ( is_array( $input ) ) {
333
		$seen_nodes[] = &$any;
334
335
		$return = array();
336
337
		foreach ( $input as $k => &$v ) {
338
			if ( ( is_array( $v ) || is_object( $v ) ) ) {
339
				if ( in_array( $v, $seen_nodes, true ) ) {
340
					continue;
341
				}
342
				$return[ $k ] = jetpack_json_wrap( $v, $seen_nodes );
0 ignored issues
show
Bug introduced by
It seems like $v can also be of type object; however, jetpack_json_wrap() does only seem to accept array|object<obj>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
343
			} else {
344
				$return[ $k ] = $v;
345
			}
346
		}
347
348
		return $return;
349
	}
350
351
	return $any;
352
}
353
354
/**
355
 * Checks if the mime_content_type function is available and return it if so.
356
 *
357
 * The function mime_content_type is enabled by default in PHP, but can be disabled. We attempt to
358
 * enforce this via composer.json, but that won't be checked in majority of cases where
359
 * this would be happening.
360
 *
361
 * @since 7.8.0
362
 *
363
 * @param string $file File location.
364
 *
365
 * @return string|false MIME type or false if functionality is not available.
366
 */
367
function jetpack_mime_content_type( $file ) {
368
	if ( function_exists( 'mime_content_type' ) ) {
369
		return mime_content_type( $file );
370
	}
371
372
	return false;
373
}
374
375
/**
376
 * Checks that the mime type of the specified file is among those in a filterable list of mime types.
377
 *
378
 * @since 7.8.0
379
 *
380
 * @param string $file Path to file to get its mime type.
381
 *
382
 * @return bool
383
 */
384
function jetpack_is_file_supported_for_sideloading( $file ) {
385
	$type = jetpack_mime_content_type( $file );
386
387
	if ( ! $type ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $type of type string|false is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
388
		return false;
389
	}
390
391
	/**
392
	 * Filter the list of supported mime types for media sideloading.
393
	 *
394
	 * @since 4.0.0
395
	 *
396
	 * @module json-api
397
	 *
398
	 * @param array $supported_mime_types Array of the supported mime types for media sideloading.
399
	 */
400
	$supported_mime_types = apply_filters(
401
		'jetpack_supported_media_sideload_types',
402
		array(
403
			'image/png',
404
			'image/jpeg',
405
			'image/gif',
406
			'image/bmp',
407
			'video/quicktime',
408
			'video/mp4',
409
			'video/mpeg',
410
			'video/ogg',
411
			'video/3gpp',
412
			'video/3gpp2',
413
			'video/h261',
414
			'video/h262',
415
			'video/h264',
416
			'video/x-msvideo',
417
			'video/x-ms-wmv',
418
			'video/x-ms-asf',
419
		)
420
	);
421
422
	// If the type returned was not an array as expected, then we know we don't have a match.
423
	if ( ! is_array( $supported_mime_types ) ) {
424
		return false;
425
	}
426
427
	return in_array( $type, $supported_mime_types, true );
428
}
429
430
/**
431
 * Determine if the current User Agent matches the passed $kind
432
 *
433
 * @param string $kind Category of mobile device to check for.
434
 *                         Either: any, dumb, smart.
435
 * @param bool   $return_matched_agent Boolean indicating if the UA should be returned.
436
 *
437
 * @return bool|string Boolean indicating if current UA matches $kind. If
438
 *                              $return_matched_agent is true, returns the UA string
439
 */
440
function jetpack_is_mobile( $kind = 'any', $return_matched_agent = false ) {
441
442
	/**
443
	 * Filter the value of jetpack_is_mobile before it is calculated.
444
	 *
445
	 * Passing a truthy value to the filter will short-circuit determining the
446
	 * mobile type, returning the passed value instead.
447
	 *
448
	 * @since  4.2.0
449
	 *
450
	 * @param bool|string $matches Boolean if current UA matches $kind or not. If
451
	 *                             $return_matched_agent is true, should return the UA string
452
	 * @param string      $kind Category of mobile device being checked
453
	 * @param bool        $return_matched_agent Boolean indicating if the UA should be returned
454
	 */
455
	$pre = apply_filters( 'pre_jetpack_is_mobile', null, $kind, $return_matched_agent );
0 ignored issues
show
Unused Code introduced by
The call to apply_filters() has too many arguments starting with $kind.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
456
	if ( $pre ) {
457
		return $pre;
458
	}
459
460
	$return      = false;
461
	$device_info = Device_Detection::get_info();
462
463
	if ( 'any' === $kind ) {
464
		$return = $device_info['is_phone'];
465
	} elseif ( 'smart' === $kind ) {
466
		$return = $device_info['is_smartphone'];
467
	} elseif ( 'dumb' === $kind ) {
468
		$return = $device_info['is_phone'] && ! $device_info['is_smartphone'];
469
	}
470
471
	if ( $return_matched_agent && true === $return ) {
472
		$return = $device_info['is_phone_matched_ua'];
473
	}
474
475
	/**
476
	 * Filter the value of jetpack_is_mobile
477
	 *
478
	 * @since  4.2.0
479
	 *
480
	 * @param bool|string $matches Boolean if current UA matches $kind or not. If
481
	 *                             $return_matched_agent is true, should return the UA string
482
	 * @param string      $kind Category of mobile device being checked
483
	 * @param bool        $return_matched_agent Boolean indicating if the UA should be returned
484
	 */
485
	return apply_filters( 'jetpack_is_mobile', $return, $kind, $return_matched_agent );
0 ignored issues
show
Unused Code introduced by
The call to apply_filters() has too many arguments starting with $kind.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
486
}
487