Conditions | 19 |
Paths | 848 |
Total Lines | 141 |
Code Lines | 84 |
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 | 'sslverify' => true, |
||
24 | ); |
||
25 | |||
26 | $args = wp_parse_args( $args, $defaults ); |
||
27 | |||
28 | $args['blog_id'] = (int) $args['blog_id']; |
||
29 | |||
30 | if ( 'header' != $args['auth_location'] ) { |
||
31 | $args['auth_location'] = 'query_string'; |
||
32 | } |
||
33 | |||
34 | $token = Jetpack_Data::get_access_token( $args['user_id'] ); |
||
|
|||
35 | if ( !$token ) { |
||
36 | return new Jetpack_Error( 'missing_token' ); |
||
37 | } |
||
38 | |||
39 | $method = strtoupper( $args['method'] ); |
||
40 | |||
41 | $timeout = intval( $args['timeout'] ); |
||
42 | |||
43 | $redirection = $args['redirection']; |
||
44 | $stream = $args['stream']; |
||
45 | $filename = $args['filename']; |
||
46 | $sslverify = $args['sslverify']; |
||
47 | |||
48 | $request = compact( 'method', 'body', 'timeout', 'redirection', 'stream', 'filename', 'sslverify' ); |
||
49 | |||
50 | @list( $token_key, $secret ) = explode( '.', $token->secret ); |
||
51 | if ( empty( $token ) || empty( $secret ) ) { |
||
52 | return new Jetpack_Error( 'malformed_token' ); |
||
53 | } |
||
54 | |||
55 | $token_key = sprintf( '%s:%d:%d', $token_key, JETPACK__API_VERSION, $token->external_user_id ); |
||
56 | |||
57 | require_once JETPACK__PLUGIN_DIR . 'class.jetpack-signature.php'; |
||
58 | |||
59 | $time_diff = (int) Jetpack_Options::get_option( 'time_diff' ); |
||
60 | $jetpack_signature = new Jetpack_Signature( $token->secret, $time_diff ); |
||
61 | |||
62 | $timestamp = time() + $time_diff; |
||
63 | |||
64 | if( function_exists( 'wp_generate_password' ) ) { |
||
65 | $nonce = wp_generate_password( 10, false ); |
||
66 | } else { |
||
67 | $nonce = substr( sha1( rand( 0, 1000000 ) ), 0, 10); |
||
68 | } |
||
69 | |||
70 | // Kind of annoying. Maybe refactor Jetpack_Signature to handle body-hashing |
||
71 | if ( is_null( $body ) ) { |
||
72 | $body_hash = ''; |
||
73 | |||
74 | } else { |
||
75 | // Allow arrays to be used in passing data. |
||
76 | $body_to_hash = $body; |
||
77 | |||
78 | if ( is_array( $body ) ) { |
||
79 | // We cast this to a new variable, because the array form of $body needs to be |
||
80 | // maintained so it can be passed into the request later on in the code. |
||
81 | if ( count( $body ) > 0 ) { |
||
82 | $body_to_hash = json_encode( self::_stringify_data( $body ) ); |
||
83 | } else { |
||
84 | $body_to_hash = ''; |
||
85 | } |
||
86 | } |
||
87 | |||
88 | if ( ! is_string( $body_to_hash ) ) { |
||
89 | return new Jetpack_Error( 'invalid_body', 'Body is malformed.' ); |
||
90 | } |
||
91 | |||
92 | $body_hash = jetpack_sha1_base64( $body_to_hash ); |
||
93 | } |
||
94 | |||
95 | $auth = array( |
||
96 | 'token' => $token_key, |
||
97 | 'timestamp' => $timestamp, |
||
98 | 'nonce' => $nonce, |
||
99 | 'body-hash' => $body_hash, |
||
100 | ); |
||
101 | |||
102 | if ( false !== strpos( $args['url'], 'xmlrpc.php' ) ) { |
||
103 | $url_args = array( |
||
104 | 'for' => 'jetpack', |
||
105 | 'wpcom_blog_id' => Jetpack_Options::get_option( 'id' ), |
||
106 | ); |
||
107 | } else { |
||
108 | $url_args = array(); |
||
109 | } |
||
110 | |||
111 | if ( 'header' != $args['auth_location'] ) { |
||
112 | $url_args += $auth; |
||
113 | } |
||
114 | |||
115 | $url = add_query_arg( urlencode_deep( $url_args ), $args['url'] ); |
||
116 | $url = Jetpack::fix_url_for_bad_hosts( $url ); |
||
117 | |||
118 | $signature = $jetpack_signature->sign_request( $token_key, $timestamp, $nonce, $body_hash, $method, $url, $body, false ); |
||
119 | |||
120 | if ( !$signature || is_wp_error( $signature ) ) { |
||
121 | return $signature; |
||
122 | } |
||
123 | |||
124 | // Send an Authorization header so various caches/proxies do the right thing |
||
125 | $auth['signature'] = $signature; |
||
126 | $auth['version'] = JETPACK__VERSION; |
||
127 | $header_pieces = array(); |
||
128 | foreach ( $auth as $key => $value ) { |
||
129 | $header_pieces[] = sprintf( '%s="%s"', $key, $value ); |
||
130 | } |
||
131 | $request['headers'] = array_merge( $args['headers'], array( |
||
132 | 'Authorization' => "X_JETPACK " . join( ' ', $header_pieces ), |
||
133 | ) ); |
||
134 | |||
135 | $host = parse_url( $url, PHP_URL_HOST ); |
||
136 | |||
137 | // If we have a JETPACK__WPCOM_JSON_API_HOST_HEADER set, then let's use |
||
138 | // that, otherwise, let's fallback to the standard. |
||
139 | if ( defined( 'JETPACK__WPCOM_JSON_API_HOST_HEADER' ) && JETPACK__WPCOM_JSON_API_HOST_HEADER ) { |
||
140 | $request['headers']['Host'] = JETPACK__WPCOM_JSON_API_HOST_HEADER; |
||
141 | |||
142 | } elseif ( $host === JETPACK__WPCOM_JSON_API_HOST ) { |
||
143 | $request['headers']['Host'] = 'public-api.wordpress.com'; |
||
144 | } |
||
145 | |||
146 | if ( 'header' != $args['auth_location'] ) { |
||
147 | $url = add_query_arg( 'signature', urlencode( $signature ), $url ); |
||
148 | } |
||
149 | |||
150 | return Jetpack_Client::_wp_remote_request( $url, $request ); |
||
151 | } |
||
152 | |||
350 |
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: