تابع ووکامرسی wc_rest_upload_image_from_url آپلود تصویر بر اساس URL داده شده

تابع ووکامرسی wc_rest_upload_image_from_url – آپلود تصویر بر اساس URL داده شده
Syntax – سینتکس
(array|WP_Error) wc_rest_upload_image_from_url( (string) $image_url );
Parameters – پارامتر ها (1)
- 1- $image_url (string)
Returns – مقادیر بازگشتی (array|WP_Error)
Attachment data or error message.
Usage – نحوه استفاده
if ( !function_exists( 'wc_rest_upload_image_from_url' ) ) {
require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-rest-functions.php';
}
// The image url.
$image_url = '';
// NOTICE! Understand what this does before running.
$result = wc_rest_upload_image_from_url($image_url);
Defined – محل تعریف
/includes/wc-rest-functions.php
function wc_rest_upload_image_from_url( $image_url ) {
$file_name = basename( current( explode( '?', $image_url ) ) );
$parsed_url = @parse_url( $image_url );
// Check parsed URL.
if ( ! $parsed_url || ! is_array( $parsed_url ) ) {
return new WP_Error( 'woocommerce_rest_invalid_image_url', sprintf( __( 'Invalid URL %s.', woocommerce ), $image_url ), array( 'status' => 400 ) );
}
// Ensure url is valid.
$image_url = esc_url_raw( $image_url );
// Get the file.
$response = wp_safe_remote_get( $image_url, array(
'timeout' => 10,
) );
if ( is_wp_error( $response ) ) {
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 ) );
} elseif ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
return new WP_Error( 'woocommerce_rest_invalid_remote_image_url', sprintf( __( 'Error getting remote image %s.', woocommerce ), $image_url ), array( 'status' => 400 ) );
}
// Ensure we have a file name and type.
$wp_filetype = wp_check_filetype( $file_name, wc_rest_allowed_image_mime_types() );
if ( ! $wp_filetype['type'] ) {
$headers = wp_remote_retrieve_headers( $response );
if ( isset( $headers['content-disposition'] ) && strstr( $headers['content-disposition'], 'filename=' ) ) {
$disposition = end( explode( 'filename=', $headers['content-disposition'] ) );
$disposition = sanitize_file_name( $disposition );
$file_name = $disposition;
} elseif ( isset( $headers['content-type'] ) && strstr( $headers['content-type'], 'image/' ) ) {
$file_name = 'image.' . str_replace( 'image/', '', $headers['content-type'] );
}
unset( $headers );
// Recheck filetype
$wp_filetype = wp_check_filetype( $file_name, wc_rest_allowed_image_mime_types() );
if ( ! $wp_filetype['type'] ) {
return new WP_Error( 'woocommerce_rest_invalid_image_type', __( 'Invalid image type.', woocommerce ), array( 'status' => 400 ) );
}
}
// Upload the file.
$upload = wp_upload_bits( $file_name, '', wp_remote_retrieve_body( $response ) );
if ( $upload[error] ) {
return new WP_Error( 'woocommerce_rest_image_upload_error', $upload[error], array( 'status' => 400 ) );
}
// Get filesize.
$filesize = filesize( $upload['file'] );
if ( 0 == $filesize ) {
@unlink( $upload['file'] );
unset( $upload );
return new WP_Error( 'woocommerce_rest_image_upload_file_error', __( 'Zero size file downloaded.', woocommerce ), array( 'status' => 400 ) );
}
do_action( 'woocommerce_rest_api_uploaded_image_from_url', $upload, $image_url );
return $upload;
}
versions – نسخه ها
از نسخه : 2.6.0
نسخه فعلی : 3.0.6
دیگر نسخه ها : 3.0.6 , 3.0.5 , 3.0.4 , 3.0.3 , 3.0.2
ارسال نظر