Ticket #47991: 47991.patch
| File 47991.patch, 12.2 KB (added by , 7 years ago) |
|---|
-
wp-includes/Requests/Session.php
22 22 * Base URL for requests 23 23 * 24 24 * URLs will be made absolute using this as the base 25 * 25 26 * @var string|null 26 27 */ 27 28 public $url = null; … … 28 29 29 30 /** 30 31 * Base headers for requests 32 * 31 33 * @var array 32 34 */ 33 35 public $headers = array(); … … 58 60 /** 59 61 * Create a new session 60 62 * 61 * @param string|null $url Base URL for requests 62 * @param array $headers Default headers for requests63 * @param array $data Default data for requests64 * @param array $options Default options for requests63 * @param string|null $url Base URL for requests. 64 * @param array $headers Default headers for requests. 65 * @param array $data Default data for requests. 66 * @param array $options Default options for requests. 65 67 */ 66 public function __construct( $url = null, $headers = array(), $data = array(), $options = array()) {67 $this->url = $url;68 public function __construct( $url = null, $headers = array(), $data = array(), $options = array() ) { 69 $this->url = $url; 68 70 $this->headers = $headers; 69 $this->data = $data;71 $this->data = $data; 70 72 $this->options = $options; 71 73 72 if ( empty($this->options['cookies'])) {74 if ( empty( $this->options['cookies'] ) ) { 73 75 $this->options['cookies'] = new Requests_Cookie_Jar(); 74 76 } 75 77 } … … 77 79 /** 78 80 * Get a property's value 79 81 * 80 * @param string $key Property key 82 * @param string $key Property key. 81 83 * @return mixed|null Property value, null if none found 82 84 */ 83 public function __get( $key) {84 if ( isset($this->options[$key])) {85 return $this->options[ $key];85 public function __get( $key ) { 86 if ( isset( $this->options[ $key ] ) ) { 87 return $this->options[ $key ]; 86 88 } 87 89 88 90 return null; … … 91 93 /** 92 94 * Set a property's value 93 95 * 94 * @param string $key Property key 95 * @param mixed $value Property value96 * @param string $key Property key. 97 * @param mixed $value Property value. 96 98 */ 97 public function __set( $key, $value) {98 $this->options[ $key] = $value;99 public function __set( $key, $value ) { 100 $this->options[ $key ] = $value; 99 101 } 100 102 101 103 /** 102 104 * Remove a property's value 103 105 * 104 * @param string $key Property key 106 * @param string $key Property key. 105 107 */ 106 public function __isset( $key) {107 return isset( $this->options[$key]);108 public function __isset( $key ) { 109 return isset( $this->options[ $key ] ); 108 110 } 109 111 110 112 /** 111 113 * Remove a property's value 112 114 * 113 * @param string $key Property key 115 * @param string $key Property key. 114 116 */ 115 public function __unset( $key) {116 if ( isset($this->options[$key])) {117 unset( $this->options[$key]);117 public function __unset( $key ) { 118 if ( isset( $this->options[ $key ] ) ) { 119 unset( $this->options[ $key ] ); 118 120 } 119 121 } 120 122 121 /**#@+ 123 /** 124 * Send a GET request 125 * 122 126 * @see request() 123 * @param string $url124 * @param array $headers125 * @param array $options127 * @param string $url URL to request. 128 * @param array $headers Extra headers to send with the request. 129 * @param array $options Options for the request (see {@see Requests::request}). 126 130 * @return Requests_Response 127 131 */ 128 /** 129 * Send a GET request 130 */ 131 public function get($url, $headers = array(), $options = array()) { 132 return $this->request($url, $headers, null, Requests::GET, $options); 132 public function get( $url, $headers = array(), $options = array() ) { 133 return $this->request( $url, $headers, null, Requests::GET, $options ); 133 134 } 134 135 135 136 /** 136 137 * Send a HEAD request 138 * 139 * @param string $url URL to request. 140 * @param array $headers Extra headers to send with the request. 141 * @param array $options Options for the request (see {@see Requests::request}). 142 * @return Requests_Response 137 143 */ 138 public function head( $url, $headers = array(), $options = array()) {139 return $this->request( $url, $headers, null, Requests::HEAD, $options);144 public function head( $url, $headers = array(), $options = array() ) { 145 return $this->request( $url, $headers, null, Requests::HEAD, $options ); 140 146 } 141 147 142 148 /** 143 149 * Send a DELETE request 150 * 151 * @param string $url URL to request. 152 * @param array $headers Extra headers to send with the request. 153 * @param array $options Options for the request (see {@see Requests::request}). 154 * @return Requests_Response 144 155 */ 145 public function delete( $url, $headers = array(), $options = array()) {146 return $this->request( $url, $headers, null, Requests::DELETE, $options);156 public function delete( $url, $headers = array(), $options = array() ) { 157 return $this->request( $url, $headers, null, Requests::DELETE, $options ); 147 158 } 148 /**#@-*/149 159 150 /**#@+ 160 /** 161 * Send a POST request 162 * 151 163 * @see request() 152 * @param string $url153 * @param array $headers154 * @param array $data155 * @param array $options164 * @param string $url URL to request. 165 * @param array $headers Extra headers to send with the request. 166 * @param array|null $data Data to send either as a query string for GET/HEAD requests, or in the body for POST requests. 167 * @param array $options Options for the request (see {@see Requests::request}). 156 168 * @return Requests_Response 157 169 */ 158 /** 159 * Send a POST request 160 */ 161 public function post($url, $headers = array(), $data = array(), $options = array()) { 162 return $this->request($url, $headers, $data, Requests::POST, $options); 170 public function post( $url, $headers = array(), $data = array(), $options = array() ) { 171 return $this->request( $url, $headers, $data, Requests::POST, $options ); 163 172 } 164 173 165 174 /** 166 175 * Send a PUT request 176 * 177 * @param string $url URL to request. 178 * @param array $headers Extra headers to send with the request. 179 * @param array|null $data Data to send either as a query string for GET/HEAD requests, or in the body for POST requests. 180 * @param array $options Options for the request (see {@see Requests::request}). 181 * @return Requests_Response 167 182 */ 168 public function put( $url, $headers = array(), $data = array(), $options = array()) {169 return $this->request( $url, $headers, $data, Requests::PUT, $options);183 public function put( $url, $headers = array(), $data = array(), $options = array() ) { 184 return $this->request( $url, $headers, $data, Requests::PUT, $options ); 170 185 } 171 186 172 187 /** … … 176 191 * specification recommends that should send an ETag 177 192 * 178 193 * @link https://tools.ietf.org/html/rfc5789 194 * 195 * @param string $url URL to request. 196 * @param array $headers Extra headers to send with the request. 197 * @param array|null $data Data to send either as a query string for GET/HEAD requests, or in the body for POST requests. 198 * @param array $options Options for the request (see {@see Requests::request}). 199 * @return Requests_Response 179 200 */ 180 public function patch( $url, $headers, $data = array(), $options = array()) {181 return $this->request( $url, $headers, $data, Requests::PATCH, $options);201 public function patch( $url, $headers, $data = array(), $options = array() ) { 202 return $this->request( $url, $headers, $data, Requests::PATCH, $options ); 182 203 } 183 /**#@-*/184 204 185 205 /** 186 206 * Main interface for HTTP requests … … 190 210 * 191 211 * @see Requests::request() 192 212 * 193 * @throws Requests_Exception On invalid URLs (`nonhttp`) 213 * @throws Requests_Exception On invalid URLs (`nonhttp`). 194 214 * 195 * @param string $url URL to request196 * @param array $headers Extra headers to send with the request197 * @param array|null $data Data to send either as a query string for GET/HEAD requests, or in the body for POST requests198 * @param string $type HTTP request type (use Requests constants)199 * @param array $options Options for the request (see {@see Requests::request})215 * @param string $url URL to request. 216 * @param array $headers Extra headers to send with the request. 217 * @param array|null $data Data to send either as a query string for GET/HEAD requests, or in the body for POST requests. 218 * @param string $type HTTP request type (use Requests constants). 219 * @param array $options Options for the request (see {@see Requests::request}). 200 220 * @return Requests_Response 201 221 */ 202 public function request( $url, $headers = array(), $data = array(), $type = Requests::GET, $options = array()) {203 $request = $this->merge_request( compact('url', 'headers', 'data', 'options'));222 public function request( $url, $headers = array(), $data = array(), $type = Requests::GET, $options = array() ) { 223 $request = $this->merge_request( compact( 'url', 'headers', 'data', 'options' ) ); 204 224 205 return Requests::request( $request['url'], $request['headers'], $request['data'], $type, $request['options']);225 return Requests::request( $request['url'], $request['headers'], $request['data'], $type, $request['options'] ); 206 226 } 207 227 208 228 /** … … 210 230 * 211 231 * @see Requests::request_multiple() 212 232 * 213 * @param array $requests Requests data (see {@see Requests::request_multiple})214 * @param array $options Global and default options (see {@see Requests::request})215 * @return array Responses (either Requests_Response or a Requests_Exception object) 233 * @param array $requests Requests data (see {@see Requests::request_multiple}). 234 * @param array $options Global and default options (see {@see Requests::request}). 235 * @return array Responses (either Requests_Response or a Requests_Exception object). 216 236 */ 217 public function request_multiple( $requests, $options = array()) {218 foreach ( $requests as $key => $request) {219 $requests[ $key] = $this->merge_request($request, false);237 public function request_multiple( $requests, $options = array() ) { 238 foreach ( $requests as $key => $request ) { 239 $requests[ $key ] = $this->merge_request( $request, false ); 220 240 } 221 241 222 $options = array_merge( $this->options, $options);242 $options = array_merge( $this->options, $options ); 223 243 224 // Disallow forcing the type, as that's a per request setting 225 unset( $options['type']);244 // Disallow forcing the type, as that's a per request setting. 245 unset( $options['type'] ); 226 246 227 return Requests::request_multiple( $requests, $options);247 return Requests::request_multiple( $requests, $options ); 228 248 } 229 249 230 250 /** 231 251 * Merge a request's data with the default data 232 252 * 233 * @param array $request Request data (same form as {@see request_multiple})234 * @param boolean $merge_options Should we merge options as well? 235 * @return array Request data253 * @param array $request Request data (same form as {@see request_multiple}). 254 * @param boolean $merge_options Should we merge options as well?. 255 * @return array Request data 236 256 */ 237 protected function merge_request( $request, $merge_options = true) {238 if ( $this->url !== null) {239 $request['url'] = Requests_IRI::absolutize( $this->url, $request['url']);257 protected function merge_request( $request, $merge_options = true ) { 258 if ( null !== $this->url ) { 259 $request['url'] = Requests_IRI::absolutize( $this->url, $request['url'] ); 240 260 $request['url'] = $request['url']->uri; 241 261 } 242 262 243 if ( empty($request['headers'])) {263 if ( empty( $request['headers'] ) ) { 244 264 $request['headers'] = array(); 245 265 } 246 $request['headers'] = array_merge( $this->headers, $request['headers']);266 $request['headers'] = array_merge( $this->headers, $request['headers'] ); 247 267 248 if ( empty($request['data'])) {249 if ( is_array($this->data)) {268 if ( empty( $request['data'] ) ) { 269 if ( is_array( $this->data ) ) { 250 270 $request['data'] = $this->data; 251 271 } 272 } elseif ( is_array( $request['data'] ) && is_array( $this->data ) ) { 273 $request['data'] = array_merge( $this->data, $request['data'] ); 252 274 } 253 elseif (is_array($request['data']) && is_array($this->data)) {254 $request['data'] = array_merge($this->data, $request['data']);255 }256 275 257 if ( $merge_options !== false) {258 $request['options'] = array_merge( $this->options, $request['options']);276 if ( false !== $merge_options ) { 277 $request['options'] = array_merge( $this->options, $request['options'] ); 259 278 260 // Disallow forcing the type, as that's a per request setting 261 unset( $request['options']['type']);279 // Disallow forcing the type, as that's a per request setting. 280 unset( $request['options']['type'] ); 262 281 } 263 282 264 283 return $request;