Completed
Push — fix/bump-to-4.8 ( d258b8 )
by
unknown
07:21 queued 40s
created

functions.global.php (1 issue)

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
 * 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
 * Please write docblocks
10
 */
11
12
/**
13
 * Disable direct access.
14
 */
15
if ( ! defined( 'ABSPATH' ) ) {
16
	exit;
17
}
18
19
/**
20
 * Set the admin language, based on user language.
21
 *
22
 * @since 4.5.0
23
 * @deprecated 6.6.0 Use Core function instead.
24
 *
25
 * @return string
26
 */
27
function jetpack_get_user_locale() {
28
	_deprecated_function( __FUNCTION__, 'jetpack-6.6.0', 'get_user_locale' );
29
	$locale = get_locale();
30
31
	if ( function_exists( 'get_user_locale' ) ) {
32
		$locale = get_user_locale();
33
	}
34
35
	return $locale;
36
}
37
38
/**
39
 * Determine if this site is an Atomic site or not looking first at the 'at_options' option.
40
 * As a fallback, check for presence of wpcomsh plugin to determine if a current site has undergone AT.
41
 *
42
 * @since 4.8.1
43
 *
44
 * @return bool
45
 */
46
function jetpack_is_atomic_site() {
47
	$at_options = get_option( 'at_options', array() );
48
	return ! empty( $at_options ) || defined( 'WPCOMSH__PLUGIN_FILE' );
49
}
50
51
/**
52
 * Register post type for migration.
53
 *
54
 * @since 5.2
55
 */
56
function jetpack_register_migration_post_type() {
57
	register_post_type( 'jetpack_migration', array(
58
		'supports'     => array(),
59
		'taxonomies'   => array(),
60
		'hierarchical' => false,
61
		'public'       => false,
62
		'has_archive'  => false,
63
		'can_export'   => true,
64
	) );
65
}
66
67
/**
68
 * Stores migration data in the database.
69
 *
70
 * @since 5.2
71
 *
72
 * @param string $option_name
73
 * @param bool $option_value
74
 *
75
 * @return int|WP_Error
76
 */
77
function jetpack_store_migration_data( $option_name, $option_value ) {
78
	jetpack_register_migration_post_type();
79
80
	$insert = array(
81
		'post_title' => $option_name,
82
		'post_content_filtered' => $option_value,
83
		'post_type' => 'jetpack_migration',
84
		'post_date' => date( 'Y-m-d H:i:s', time() ),
85
	);
86
87
	$post = get_page_by_title( $option_name, 'OBJECT', 'jetpack_migration' );
88
89
	if ( null !== $post ) {
90
		$insert['ID'] = $post->ID;
91
	}
92
93
	return wp_insert_post( $insert, true );
94
}
95
96
/**
97
 * Retrieves legacy image widget data.
98
 *
99
 * @since 5.2
100
 *
101
 * @param string $option_name
102
 *
103
 * @return mixed|null
104
 */
105
function jetpack_get_migration_data( $option_name ) {
106
	$post = get_page_by_title( $option_name, 'OBJECT', 'jetpack_migration' );
107
108
	return null !== $post ? maybe_unserialize( $post->post_content_filtered ) : null;
109
}
110
111
/**
112
 * Prints a TOS blurb used throughout the connection prompts.
113
 *
114
 * @since 5.3
115
 *
116
 * @return string
117
 */
118
function jetpack_render_tos_blurb() {
119
	printf(
120
		__( 'By clicking the <strong>Set up Jetpack</strong> button, you agree to our <a href="%s" target="_blank">Terms of Service</a> and to <a href="%s" target="_blank">share details</a> with WordPress.com.', 'jetpack' ),
121
		'https://wordpress.com/tos',
122
		'https://jetpack.com/support/what-data-does-jetpack-sync'
123
	);
124
}
125
126
/**
127
 * Intervene upgrade process so Jetpack themes are downloaded with credentials.
128
 *
129
 * @since 5.3
130
 *
131
 * @param bool   $preempt Whether to preempt an HTTP request's return value. Default false.
132
 * @param array  $r       HTTP request arguments.
133
 * @param string $url     The request URL.
134
 *
135
 * @return array|bool|WP_Error
136
 */
137
function jetpack_theme_update( $preempt, $r, $url ) {
138
	if ( false !== stripos( $url, JETPACK__WPCOM_JSON_API_HOST . '/rest/v1/themes/download' ) ) {
139
		$file = $r['filename'];
140
		if ( ! $file ) {
141
			return new WP_Error( 'problem_creating_theme_file', esc_html__( 'Problem creating file for theme download', 'jetpack' ) );
142
		}
143
		$theme = pathinfo( parse_url( $url, PHP_URL_PATH ), PATHINFO_FILENAME );
144
145
		// Remove filter to avoid endless loop since wpcom_json_api_request_as_blog uses this too.
146
		remove_filter( 'pre_http_request', 'jetpack_theme_update' );
147
		$result = Jetpack_Client::wpcom_json_api_request_as_blog(
148
			"themes/download/$theme.zip", '1.1', array( 'stream' => true, 'filename' => $file )
149
		);
150
151
		if ( 200 !== wp_remote_retrieve_response_code( $result ) ) {
152
			return new WP_Error( 'problem_fetching_theme', esc_html__( 'Problem downloading theme', 'jetpack' ) );
153
		}
154
		return $result;
155
	}
156
	return $preempt;
157
}
158
159
/**
160
 * Add the filter when a upgrade is going to be downloaded.
161
 *
162
 * @since 5.3
163
 *
164
 * @param bool $reply Whether to bail without returning the package. Default false.
165
 *
166
 * @return bool
167
 */
168
function jetpack_upgrader_pre_download( $reply ) {
169
	add_filter( 'pre_http_request', 'jetpack_theme_update', 10, 3 );
170
	return $reply;
171
}
172
173
add_filter( 'upgrader_pre_download', 'jetpack_upgrader_pre_download' );
174
175
176
/**
177
 * Wraps data in a way so that we can distinguish between objects and array and also prevent object recursion.
178
 *
179
 * @since 6.1.0
180
 *
181
 * @param $any
182
 * @param array $seen_nodes
183
 *
184
 * @return array
185
 */
186 View Code Duplication
function jetpack_json_wrap( &$any, $seen_nodes = array() ) {
0 ignored issues
show
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...
187
	if ( is_object( $any ) ) {
188
		$input = get_object_vars( $any );
189
		$input['__o'] = 1;
190
	} else {
191
		$input = &$any;
192
	}
193
194
	if ( is_array( $input ) ) {
195
		$seen_nodes[] = &$any;
196
197
		$return = array();
198
199
		foreach ( $input as $k => &$v ) {
200
			if ( ( is_array( $v ) || is_object( $v ) ) ) {
201
				if ( in_array( $v, $seen_nodes, true ) ) {
202
					continue;
203
				}
204
				$return[ $k ] = jetpack_json_wrap( $v, $seen_nodes );
205
			} else {
206
				$return[ $k ] = $v;
207
			}
208
		}
209
210
		return $return;
211
	}
212
213
	return $any;
214
}
215