wc_rest_upload_image_from_url

Upload image from URL.

Description

(array|WP_Error) wc_rest_upload_image_from_url( (string) $image_url ); 

Returns (array|WP_Error)

Attachment data or error message.

Parameters (1)

0. $image_url (string)
The image url.

Usage

  1. if ( !function_exists( 'wc_rest_upload_image_from_url' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-rest-functions.php'; 
  3.  
  4. // The image url. 
  5. $image_url = ''; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = wc_rest_upload_image_from_url($image_url); 
  9.  

Defined (1)

The function is defined in the following location(s).

/includes/wc-rest-functions.php  
  1. function wc_rest_upload_image_from_url( $image_url ) { 
  2. $file_name = basename( current( explode( '?', $image_url ) ) ); 
  3. $parsed_url = @parse_url( $image_url ); 
  4.  
  5. // Check parsed URL. 
  6. if ( ! $parsed_url || ! is_array( $parsed_url ) ) { 
  7. return new WP_Error( 'woocommerce_rest_invalid_image_url', sprintf( __( 'Invalid URL %s.', woocommerce ), $image_url ), array( 'status' => 400 ) ); 
  8.  
  9. // Ensure url is valid. 
  10. $image_url = esc_url_raw( $image_url ); 
  11.  
  12. // Get the file. 
  13. $response = wp_safe_remote_get( $image_url, array( 
  14. 'timeout' => 10,  
  15. ) ); 
  16.  
  17. if ( is_wp_error( $response ) ) { 
  18. return new WP_Error( 'woocommerce_rest_invalid_remote_image_url', sprintf( __( 'Error getting remote image %s.', woocommerce ), $image_url ) . ' ' . sprintf( __( 'Error: %s.', woocommerce ), $response->get_error_message() ), array( 'status' => 400 ) ); 
  19. } elseif ( 200 !== wp_remote_retrieve_response_code( $response ) ) { 
  20. return new WP_Error( 'woocommerce_rest_invalid_remote_image_url', sprintf( __( 'Error getting remote image %s.', woocommerce ), $image_url ), array( 'status' => 400 ) ); 
  21.  
  22. // Ensure we have a file name and type. 
  23. $wp_filetype = wp_check_filetype( $file_name, wc_rest_allowed_image_mime_types() ); 
  24.  
  25. if ( ! $wp_filetype['type'] ) { 
  26. $headers = wp_remote_retrieve_headers( $response ); 
  27. if ( isset( $headers['content-disposition'] ) && strstr( $headers['content-disposition'], 'filename=' ) ) { 
  28. $disposition = end( explode( 'filename=', $headers['content-disposition'] ) ); 
  29. $disposition = sanitize_file_name( $disposition ); 
  30. $file_name = $disposition; 
  31. } elseif ( isset( $headers['content-type'] ) && strstr( $headers['content-type'], 'image/' ) ) { 
  32. $file_name = 'image.' . str_replace( 'image/', '', $headers['content-type'] ); 
  33. unset( $headers ); 
  34.  
  35. // Recheck filetype 
  36. $wp_filetype = wp_check_filetype( $file_name, wc_rest_allowed_image_mime_types() ); 
  37.  
  38. if ( ! $wp_filetype['type'] ) { 
  39. return new WP_Error( 'woocommerce_rest_invalid_image_type', __( 'Invalid image type.', woocommerce ), array( 'status' => 400 ) ); 
  40.  
  41. // Upload the file. 
  42. $upload = wp_upload_bits( $file_name, '', wp_remote_retrieve_body( $response ) ); 
  43.  
  44. if ( $upload[error] ) { 
  45. return new WP_Error( 'woocommerce_rest_image_upload_error', $upload[error], array( 'status' => 400 ) ); 
  46.  
  47. // Get filesize. 
  48. $filesize = filesize( $upload['file'] ); 
  49.  
  50. if ( 0 == $filesize ) { 
  51. @unlink( $upload['file'] ); 
  52. unset( $upload ); 
  53.  
  54. return new WP_Error( 'woocommerce_rest_image_upload_file_error', __( 'Zero size file downloaded.', woocommerce ), array( 'status' => 400 ) ); 
  55.  
  56. do_action( 'woocommerce_rest_api_uploaded_image_from_url', $upload, $image_url ); 
  57.  
  58. return $upload;