Automattic /
jetpack
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 | * |
||
| 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 | View Code Duplication | 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 | |||
| 232 | /** |
||
| 233 | * Checks if the mime_content_type function is available and return it if so. |
||
| 234 | * |
||
| 235 | * The function mime_content_type is enabled by default in PHP, but can be disabled. We attempt to |
||
| 236 | * enforce this via composer.json, but that won't be checked in majority of cases where |
||
| 237 | * this would be happening. |
||
| 238 | * |
||
| 239 | * @since 7.8.0 |
||
| 240 | * |
||
| 241 | * @param string $file File location. |
||
| 242 | * |
||
| 243 | * @return string|false MIME type or false if functionality is not available. |
||
| 244 | */ |
||
| 245 | function jetpack_mime_content_type( $file ) { |
||
| 246 | if ( function_exists( 'mime_content_type' ) ) { |
||
| 247 | return mime_content_type( $file ); |
||
| 248 | } |
||
| 249 | |||
| 250 | return false; |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Checks that the mime type of the specified file is among those in a filterable list of mime types. |
||
| 255 | * |
||
| 256 | * @since 7.8.0 |
||
| 257 | * |
||
| 258 | * @param string $file Path to file to get its mime type. |
||
| 259 | * |
||
| 260 | * @return bool |
||
| 261 | */ |
||
| 262 | function jetpack_is_file_supported_for_sideloading( $file ) { |
||
| 263 | $type = jetpack_mime_content_type( $file ); |
||
| 264 | |||
| 265 | if ( ! $type ) { |
||
|
0 ignored issues
–
show
|
|||
| 266 | return false; |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Filter the list of supported mime types for media sideloading. |
||
| 271 | * |
||
| 272 | * @since 4.0.0 |
||
| 273 | * |
||
| 274 | * @module json-api |
||
| 275 | * |
||
| 276 | * @param array $supported_mime_types Array of the supported mime types for media sideloading. |
||
| 277 | */ |
||
| 278 | $supported_mime_types = apply_filters( |
||
| 279 | 'jetpack_supported_media_sideload_types', |
||
| 280 | array( |
||
| 281 | 'image/png', |
||
| 282 | 'image/jpeg', |
||
| 283 | 'image/gif', |
||
| 284 | 'image/bmp', |
||
| 285 | 'video/quicktime', |
||
| 286 | 'video/mp4', |
||
| 287 | 'video/mpeg', |
||
| 288 | 'video/ogg', |
||
| 289 | 'video/3gpp', |
||
| 290 | 'video/3gpp2', |
||
| 291 | 'video/h261', |
||
| 292 | 'video/h262', |
||
| 293 | 'video/h264', |
||
| 294 | 'video/x-msvideo', |
||
| 295 | 'video/x-ms-wmv', |
||
| 296 | 'video/x-ms-asf', |
||
| 297 | ) |
||
| 298 | ); |
||
| 299 | |||
| 300 | // If the type returned was not an array as expected, then we know we don't have a match. |
||
| 301 | if ( ! is_array( $supported_mime_types ) ) { |
||
| 302 | return false; |
||
| 303 | } |
||
| 304 | |||
| 305 | return in_array( $type, $supported_mime_types, true ); |
||
| 306 | } |
||
| 307 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: