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