| Conditions | 17 |
| Paths | 592 |
| Total Lines | 134 |
| Code Lines | 80 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 11 | public static function remote_request( $args, $body = null ) { |
||
| 12 | $defaults = array( |
||
| 13 | 'url' => '', |
||
| 14 | 'user_id' => 0, |
||
| 15 | 'blog_id' => 0, |
||
| 16 | 'auth_location' => JETPACK_CLIENT__AUTH_LOCATION, |
||
| 17 | 'method' => 'POST', |
||
| 18 | 'timeout' => 10, |
||
| 19 | 'redirection' => 0, |
||
| 20 | 'headers' => array(), |
||
| 21 | 'stream' => false, |
||
| 22 | 'filename' => null, |
||
| 23 | ); |
||
| 24 | |||
| 25 | $args = wp_parse_args( $args, $defaults ); |
||
| 26 | |||
| 27 | $args['blog_id'] = (int) $args['blog_id']; |
||
| 28 | |||
| 29 | if ( 'header' != $args['auth_location'] ) { |
||
| 30 | $args['auth_location'] = 'query_string'; |
||
| 31 | } |
||
| 32 | |||
| 33 | $token = Jetpack_Data::get_access_token( $args['user_id'] ); |
||
|
|
|||
| 34 | if ( !$token ) { |
||
| 35 | return new Jetpack_Error( 'missing_token' ); |
||
| 36 | } |
||
| 37 | |||
| 38 | $method = strtoupper( $args['method'] ); |
||
| 39 | |||
| 40 | $timeout = intval( $args['timeout'] ); |
||
| 41 | |||
| 42 | $redirection = $args['redirection']; |
||
| 43 | $stream = $args['stream']; |
||
| 44 | $filename = $args['filename']; |
||
| 45 | |||
| 46 | $request = compact( 'method', 'body', 'timeout', 'redirection', 'stream', 'filename' ); |
||
| 47 | |||
| 48 | @list( $token_key, $secret ) = explode( '.', $token->secret ); |
||
| 49 | if ( empty( $token ) || empty( $secret ) ) { |
||
| 50 | return new Jetpack_Error( 'malformed_token' ); |
||
| 51 | } |
||
| 52 | |||
| 53 | $token_key = sprintf( '%s:%d:%d', $token_key, JETPACK__API_VERSION, $token->external_user_id ); |
||
| 54 | |||
| 55 | require_once JETPACK__PLUGIN_DIR . 'class.jetpack-signature.php'; |
||
| 56 | |||
| 57 | $time_diff = (int) Jetpack_Options::get_option( 'time_diff' ); |
||
| 58 | $jetpack_signature = new Jetpack_Signature( $token->secret, $time_diff ); |
||
| 59 | |||
| 60 | $timestamp = time() + $time_diff; |
||
| 61 | |||
| 62 | if( function_exists( 'wp_generate_password' ) ) { |
||
| 63 | $nonce = wp_generate_password( 10, false ); |
||
| 64 | } else { |
||
| 65 | $nonce = substr( sha1( rand( 0, 1000000 ) ), 0, 10); |
||
| 66 | } |
||
| 67 | |||
| 68 | // Kind of annoying. Maybe refactor Jetpack_Signature to handle body-hashing |
||
| 69 | if ( is_null( $body ) ) { |
||
| 70 | $body_hash = ''; |
||
| 71 | |||
| 72 | } else { |
||
| 73 | // Allow arrays to be used in passing data. |
||
| 74 | $body_to_hash = $body; |
||
| 75 | |||
| 76 | if ( is_array( $body ) ) { |
||
| 77 | // We cast this to a new variable, because the array form of $body needs to be |
||
| 78 | // maintained so it can be passed into the request later on in the code. |
||
| 79 | if ( count( $body ) > 0 ) { |
||
| 80 | $body_to_hash = json_encode( self::_stringify_data( $body ) ); |
||
| 81 | } else { |
||
| 82 | $body_to_hash = ''; |
||
| 83 | } |
||
| 84 | } |
||
| 85 | |||
| 86 | if ( ! is_string( $body_to_hash ) ) { |
||
| 87 | return new Jetpack_Error( 'invalid_body', 'Body is malformed.' ); |
||
| 88 | } |
||
| 89 | |||
| 90 | $body_hash = jetpack_sha1_base64( $body_to_hash ); |
||
| 91 | } |
||
| 92 | |||
| 93 | $auth = array( |
||
| 94 | 'token' => $token_key, |
||
| 95 | 'timestamp' => $timestamp, |
||
| 96 | 'nonce' => $nonce, |
||
| 97 | 'body-hash' => $body_hash, |
||
| 98 | ); |
||
| 99 | |||
| 100 | if ( false !== strpos( $args['url'], 'xmlrpc.php' ) ) { |
||
| 101 | $url_args = array( |
||
| 102 | 'for' => 'jetpack', |
||
| 103 | 'wpcom_blog_id' => Jetpack_Options::get_option( 'id' ), |
||
| 104 | ); |
||
| 105 | } else { |
||
| 106 | $url_args = array(); |
||
| 107 | } |
||
| 108 | |||
| 109 | if ( 'header' != $args['auth_location'] ) { |
||
| 110 | $url_args += $auth; |
||
| 111 | } |
||
| 112 | |||
| 113 | $url = add_query_arg( urlencode_deep( $url_args ), $args['url'] ); |
||
| 114 | $url = Jetpack::fix_url_for_bad_hosts( $url ); |
||
| 115 | |||
| 116 | $signature = $jetpack_signature->sign_request( $token_key, $timestamp, $nonce, $body_hash, $method, $url, $body, false ); |
||
| 117 | |||
| 118 | if ( !$signature || is_wp_error( $signature ) ) { |
||
| 119 | return $signature; |
||
| 120 | } |
||
| 121 | |||
| 122 | // Send an Authorization header so various caches/proxies do the right thing |
||
| 123 | $auth['signature'] = $signature; |
||
| 124 | $auth['version'] = JETPACK__VERSION; |
||
| 125 | $header_pieces = array(); |
||
| 126 | foreach ( $auth as $key => $value ) { |
||
| 127 | $header_pieces[] = sprintf( '%s="%s"', $key, $value ); |
||
| 128 | } |
||
| 129 | $request['headers'] = array_merge( $args['headers'], array( |
||
| 130 | 'Authorization' => "X_JETPACK " . join( ' ', $header_pieces ), |
||
| 131 | ) ); |
||
| 132 | |||
| 133 | // Make sure we keep the host when we do JETPACK__WPCOM_JSON_API_HOST requests. |
||
| 134 | $host = parse_url( $url, PHP_URL_HOST ); |
||
| 135 | if ( $host === JETPACK__WPCOM_JSON_API_HOST ) { |
||
| 136 | $request['headers']['Host'] = 'public-api.wordpress.com'; |
||
| 137 | } |
||
| 138 | |||
| 139 | if ( 'header' != $args['auth_location'] ) { |
||
| 140 | $url = add_query_arg( 'signature', urlencode( $signature ), $url ); |
||
| 141 | } |
||
| 142 | |||
| 143 | return Jetpack_Client::_wp_remote_request( $url, $request ); |
||
| 144 | } |
||
| 145 | |||
| 342 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: