| Conditions | 9 |
| Paths | 8 |
| Total 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 |
||
| 216 | public function sideload() |
||
| 217 | { |
||
| 218 | // setup temp dir |
||
| 219 | $temp_file = wp_tempnam($this->_download_from); |
||
| 220 | |||
| 221 | if (!$temp_file) { |
||
| 222 | EE_Error::add_error( |
||
| 223 | esc_html__('Something went wrong with the upload. Unable to create a tmp file for the uploaded file on the server', 'event_espresso'), |
||
| 224 | __FILE__, |
||
| 225 | __FUNCTION__, |
||
| 226 | __LINE__ |
||
| 227 | ); |
||
| 228 | return false; |
||
| 229 | } |
||
| 230 | |||
| 231 | do_action('AHEE__EEH_Sideloader__sideload__before', $this, $temp_file); |
||
| 232 | |||
| 233 | $wp_remote_args = apply_filters('FHEE__EEH_Sideloader__sideload__wp_remote_args', array( 'timeout' => 500, 'stream' => true, 'filename' => $temp_file ), $this, $temp_file); |
||
| 234 | |||
| 235 | $response = wp_safe_remote_get($this->_download_from, $wp_remote_args); |
||
| 236 | |||
| 237 | if (is_wp_error($response) || 200 != wp_remote_retrieve_response_code($response)) { |
||
| 238 | unlink($temp_file); |
||
| 239 | if (defined('WP_DEBUG') && WP_DEBUG) { |
||
| 240 | EE_Error::add_error( |
||
| 241 | sprintf( |
||
| 242 | esc_html__('Unable to upload the file. Either the path given to download from is incorrect, or something else happened. Here is the path given: %s', 'event_espresso'), |
||
| 243 | $this->_download_from |
||
| 244 | ), |
||
| 245 | __FILE__, |
||
| 246 | __FUNCTION__, |
||
| 247 | __LINE__ |
||
| 248 | ); |
||
| 249 | } |
||
| 250 | return false; |
||
| 251 | } |
||
| 252 | |||
| 253 | // possible md5 check |
||
| 254 | $content_md5 = wp_remote_retrieve_header($response, 'content-md5'); |
||
| 255 | if ($content_md5) { |
||
| 256 | $md5_check = verify_file_md5($temp_file, $content_md5); |
||
| 257 | if (is_wp_error($md5_check)) { |
||
| 258 | unlink($temp_file); |
||
| 259 | EE_Error::add_error( |
||
| 260 | $md5_check->get_error_message(), |
||
| 261 | __FILE__, |
||
| 262 | __FUNCTION__, |
||
| 263 | __LINE__ |
||
| 264 | ); |
||
| 265 | return false; |
||
| 266 | } |
||
| 267 | } |
||
| 268 | |||
| 269 | $file = $temp_file; |
||
| 270 | |||
| 271 | // now we have the file, let's get it in the right directory with the right name. |
||
| 272 | $path = apply_filters('FHEE__EEH_Sideloader__sideload__new_path', $this->_upload_to . $this->_new_file_name, $this); |
||
| 273 | |||
| 274 | // move file in |
||
| 275 | if (false === @ rename($file, $path)) { |
||
| 276 | unlink($temp_file); |
||
| 277 | EE_Error::add_error( |
||
| 278 | sprintf( |
||
| 279 | esc_html__('Unable to move the file to new location (possible permissions errors). This is the path the class attempted to move the file to: %s', 'event_espresso'), |
||
| 280 | $path |
||
| 281 | ), |
||
| 282 | __FILE__, |
||
| 283 | __FUNCTION__, |
||
| 284 | __LINE__ |
||
| 285 | ); |
||
| 286 | return false; |
||
| 287 | } |
||
| 288 | |||
| 289 | // set permissions |
||
| 290 | $permissions = apply_filters('FHEE__EEH_Sideloader__sideload__permissions_applied', $this->_permissions, $this); |
||
| 291 | chmod($path, $permissions); |
||
| 292 | |||
| 293 | // that's it. let's allow for actions after file uploaded. |
||
| 294 | do_action('AHEE__EE_Sideloader__sideload_after', $this, $path); |
||
| 295 | |||
| 296 | // unlink tempfile |
||
| 297 | @unlink($temp_file); |
||
| 298 | return true; |
||
| 299 | } |
||
| 300 | |||
| 342 |