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