| 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 |
|
| 15 |
/** |
| 16 |
* Disable direct access. |
| 17 |
*/ |
| 18 |
if ( ! defined( 'ABSPATH' ) ) { |
| 19 |
exit; |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Set the admin language, based on user language. |
| 24 |
* |
| 25 |
* @since 4.5.0 |
| 26 |
* @deprecated 6.6.0 Use Core function instead. |
| 27 |
* |
| 28 |
* @return string |
| 29 |
*/ |
| 30 |
function jetpack_get_user_locale() { |
| 31 |
_deprecated_function( __FUNCTION__, 'jetpack-6.6.0', 'get_user_locale' ); |
| 32 |
return get_user_locale(); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Determine if this site is an Atomic site or not looking first at the 'at_options' option. |
| 37 |
* As a fallback, check for presence of wpcomsh plugin to determine if a current site has undergone AT. |
| 38 |
* |
| 39 |
* @since 4.8.1 |
| 40 |
* |
| 41 |
* @return bool |
| 42 |
*/ |
| 43 |
function jetpack_is_atomic_site() { |
| 44 |
$at_options = get_option( 'at_options', array() ); |
| 45 |
return ! empty( $at_options ) || defined( 'WPCOMSH__PLUGIN_FILE' ); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Register post type for migration. |
| 50 |
* |
| 51 |
* @since 5.2 |
| 52 |
*/ |
| 53 |
function jetpack_register_migration_post_type() { |
| 54 |
register_post_type( |
| 55 |
'jetpack_migration', |
| 56 |
array( |
| 57 |
'supports' => array(), |
| 58 |
'taxonomies' => array(), |
| 59 |
'hierarchical' => false, |
| 60 |
'public' => false, |
| 61 |
'has_archive' => false, |
| 62 |
'can_export' => true, |
| 63 |
) |
| 64 |
); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Stores migration data in the database. |
| 69 |
* |
| 70 |
* @since 5.2 |
| 71 |
* |
| 72 |
* @param string $option_name Option name. |
| 73 |
* @param bool $option_value 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 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 |
* @echo string |
| 117 |
*/ |
| 118 |
function jetpack_render_tos_blurb() { |
| 119 |
printf( |
| 120 |
wp_kses( |
| 121 |
/* Translators: placeholders are links. */ |
| 122 |
__( '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' ), |
| 123 |
array( |
| 124 |
'a' => array( |
| 125 |
'href' => array(), |
| 126 |
'target' => array(), |
| 127 |
'rel' => array(), |
| 128 |
), |
| 129 |
'strong' => true, |
| 130 |
) |
| 131 |
), |
| 132 |
'https://wordpress.com/tos', |
| 133 |
'https://jetpack.com/support/what-data-does-jetpack-sync' |
| 134 |
); |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Intervene upgrade process so Jetpack themes are downloaded with credentials. |
| 139 |
* |
| 140 |
* @since 5.3 |
| 141 |
* |
| 142 |
* @param bool $preempt Whether to preempt an HTTP request's return value. Default false. |
| 143 |
* @param array $r HTTP request arguments. |
| 144 |
* @param string $url The request URL. |
| 145 |
* |
| 146 |
* @return array|bool|WP_Error |
| 147 |
*/ |
| 148 |
function jetpack_theme_update( $preempt, $r, $url ) { |
| 149 |
if ( false !== stripos( $url, JETPACK__WPCOM_JSON_API_HOST . '/rest/v1/themes/download' ) ) { |
| 150 |
$file = $r['filename']; |
| 151 |
if ( ! $file ) { |
| 152 |
return new WP_Error( 'problem_creating_theme_file', esc_html__( 'Problem creating file for theme download', 'jetpack' ) ); |
| 153 |
} |
| 154 |
$theme = pathinfo( wp_parse_url( $url, PHP_URL_PATH ), PATHINFO_FILENAME ); |
| 155 |
|
| 156 |
// Remove filter to avoid endless loop since wpcom_json_api_request_as_blog uses this too. |
| 157 |
remove_filter( 'pre_http_request', 'jetpack_theme_update' ); |
| 158 |
$result = Client::wpcom_json_api_request_as_blog( |
| 159 |
"themes/download/$theme.zip", |
| 160 |
'1.1', |
| 161 |
array( |
| 162 |
'stream' => true, |
| 163 |
'filename' => $file, |
| 164 |
) |
| 165 |
); |
| 166 |
|
| 167 |
if ( 200 !== wp_remote_retrieve_response_code( $result ) ) { |
| 168 |
return new WP_Error( 'problem_fetching_theme', esc_html__( 'Problem downloading theme', 'jetpack' ) ); |
| 169 |
} |
| 170 |
return $result; |
| 171 |
} |
| 172 |
return $preempt; |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Add the filter when a upgrade is going to be downloaded. |
| 177 |
* |
| 178 |
* @since 5.3 |
| 179 |
* |
| 180 |
* @param bool $reply Whether to bail without returning the package. Default false. |
| 181 |
* |
| 182 |
* @return bool |
| 183 |
*/ |
| 184 |
function jetpack_upgrader_pre_download( $reply ) { |
| 185 |
add_filter( 'pre_http_request', 'jetpack_theme_update', 10, 3 ); |
| 186 |
return $reply; |
| 187 |
} |
| 188 |
|
| 189 |
add_filter( 'upgrader_pre_download', 'jetpack_upgrader_pre_download' ); |
| 190 |
|
| 191 |
|
| 192 |
/** |
| 193 |
* Wraps data in a way so that we can distinguish between objects and array and also prevent object recursion. |
| 194 |
* |
| 195 |
* @since 6.1.0 |
| 196 |
* |
| 197 |
* @param array|obj $any Source data to be cleaned up. |
| 198 |
* @param array $seen_nodes Built array of nodes. |
| 199 |
* |
| 200 |
* @return array |
| 201 |
*/ |
| 202 |
function jetpack_json_wrap( &$any, $seen_nodes = array() ) { |
| 203 |
if ( is_object( $any ) ) { |
| 204 |
$input = get_object_vars( $any ); |
| 205 |
$input['__o'] = 1; |
| 206 |
} else { |
| 207 |
$input = &$any; |
| 208 |
} |
| 209 |
|
| 210 |
if ( is_array( $input ) ) { |
| 211 |
$seen_nodes[] = &$any; |
| 212 |
|
| 213 |
$return = array(); |
| 214 |
|
| 215 |
foreach ( $input as $k => &$v ) { |
| 216 |
if ( ( is_array( $v ) || is_object( $v ) ) ) { |
| 217 |
if ( in_array( $v, $seen_nodes, true ) ) { |
| 218 |
continue; |
| 219 |
} |
| 220 |
$return[ $k ] = jetpack_json_wrap( $v, $seen_nodes ); |
| 221 |
} else { |
| 222 |
$return[ $k ] = $v; |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
return $return; |
| 227 |
} |
| 228 |
|
| 229 |
return $any; |
| 230 |
} |
| 231 |
|