Created api functions to upload images
This commit is contained in:
parent
d4d91d0fe9
commit
bf286dfe96
|
@ -109,8 +109,8 @@ class WC_REST_Coupons_Controller extends WC_REST_Posts_Controller {
|
||||||
'id' => $coupon->id,
|
'id' => $coupon->id,
|
||||||
'code' => $coupon->code,
|
'code' => $coupon->code,
|
||||||
'type' => $coupon->type,
|
'type' => $coupon->type,
|
||||||
'created_at' => wc_api_prepare_date_response( $post->post_date_gmt ),
|
'created_at' => wc_rest_api_prepare_date_response( $post->post_date_gmt ),
|
||||||
'updated_at' => wc_api_prepare_date_response( $post->post_modified_gmt ),
|
'updated_at' => wc_rest_api_prepare_date_response( $post->post_modified_gmt ),
|
||||||
'amount' => wc_format_decimal( $coupon->coupon_amount, 2 ),
|
'amount' => wc_format_decimal( $coupon->coupon_amount, 2 ),
|
||||||
'individual_use' => ( 'yes' === $coupon->individual_use ),
|
'individual_use' => ( 'yes' === $coupon->individual_use ),
|
||||||
'product_ids' => array_map( 'absint', (array) $coupon->product_ids ),
|
'product_ids' => array_map( 'absint', (array) $coupon->product_ids ),
|
||||||
|
@ -119,7 +119,7 @@ class WC_REST_Coupons_Controller extends WC_REST_Posts_Controller {
|
||||||
'usage_limit_per_user' => ( ! empty( $coupon->usage_limit_per_user ) ) ? $coupon->usage_limit_per_user : null,
|
'usage_limit_per_user' => ( ! empty( $coupon->usage_limit_per_user ) ) ? $coupon->usage_limit_per_user : null,
|
||||||
'limit_usage_to_x_items' => (int) $coupon->limit_usage_to_x_items,
|
'limit_usage_to_x_items' => (int) $coupon->limit_usage_to_x_items,
|
||||||
'usage_count' => (int) $coupon->usage_count,
|
'usage_count' => (int) $coupon->usage_count,
|
||||||
'expiry_date' => ( ! empty( $coupon->expiry_date ) ) ? wc_api_prepare_date_response( $coupon->expiry_date ) : null,
|
'expiry_date' => ( ! empty( $coupon->expiry_date ) ) ? wc_rest_api_prepare_date_response( $coupon->expiry_date ) : null,
|
||||||
'enable_free_shipping' => $coupon->enable_free_shipping(),
|
'enable_free_shipping' => $coupon->enable_free_shipping(),
|
||||||
'product_category_ids' => array_map( 'absint', (array) $coupon->product_categories ),
|
'product_category_ids' => array_map( 'absint', (array) $coupon->product_categories ),
|
||||||
'exclude_product_category_ids' => array_map( 'absint', (array) $coupon->exclude_product_categories ),
|
'exclude_product_category_ids' => array_map( 'absint', (array) $coupon->exclude_product_categories ),
|
||||||
|
|
|
@ -485,15 +485,15 @@ class WC_REST_Customers_Controller extends WP_REST_Controller {
|
||||||
|
|
||||||
$data = array(
|
$data = array(
|
||||||
'id' => $customer->ID,
|
'id' => $customer->ID,
|
||||||
'created_at' => wc_api_prepare_date_response( $customer->user_registered ),
|
'created_at' => wc_rest_api_prepare_date_response( $customer->user_registered ),
|
||||||
'updated_at' => $customer->last_update ? wc_api_prepare_date_response( date( 'Y-m-d H:i:s', $customer->last_update ) ) : null,
|
'updated_at' => $customer->last_update ? wc_rest_api_prepare_date_response( date( 'Y-m-d H:i:s', $customer->last_update ) ) : null,
|
||||||
'email' => $customer->user_email,
|
'email' => $customer->user_email,
|
||||||
'first_name' => $customer->first_name,
|
'first_name' => $customer->first_name,
|
||||||
'last_name' => $customer->last_name,
|
'last_name' => $customer->last_name,
|
||||||
'username' => $customer->user_login,
|
'username' => $customer->user_login,
|
||||||
'last_order' => array(
|
'last_order' => array(
|
||||||
'id' => is_object( $last_order ) ? $last_order->id : null,
|
'id' => is_object( $last_order ) ? $last_order->id : null,
|
||||||
'date' => is_object( $last_order ) ? wc_api_prepare_date_response( $last_order->post->post_date_gmt ) : null
|
'date' => is_object( $last_order ) ? wc_rest_api_prepare_date_response( $last_order->post->post_date_gmt ) : null
|
||||||
),
|
),
|
||||||
'orders_count' => wc_get_customer_order_count( $customer->ID ),
|
'orders_count' => wc_get_customer_order_count( $customer->ID ),
|
||||||
'total_spent' => wc_format_decimal( wc_get_customer_total_spent( $customer->ID ), 2 ),
|
'total_spent' => wc_format_decimal( wc_get_customer_total_spent( $customer->ID ), 2 ),
|
||||||
|
|
|
@ -20,11 +20,12 @@ if ( ! defined( 'ABSPATH' ) ) {
|
||||||
* Requered WP 4.4 or later.
|
* Requered WP 4.4 or later.
|
||||||
* See https://developer.wordpress.org/reference/functions/mysql_to_rfc3339/
|
* See https://developer.wordpress.org/reference/functions/mysql_to_rfc3339/
|
||||||
*
|
*
|
||||||
|
* @since 2.6.0
|
||||||
* @param string $date_gmt
|
* @param string $date_gmt
|
||||||
* @param string|null $date
|
* @param string|null $date
|
||||||
* @return string|null ISO8601/RFC3339 formatted datetime.
|
* @return string|null ISO8601/RFC3339 formatted datetime.
|
||||||
*/
|
*/
|
||||||
function wc_api_prepare_date_response( $date_gmt, $date = null ) {
|
function wc_rest_api_prepare_date_response( $date_gmt, $date = null ) {
|
||||||
// Check if mysql_to_rfc3339 exists first!
|
// Check if mysql_to_rfc3339 exists first!
|
||||||
if ( ! function_exists( 'mysql_to_rfc3339' ) ) {
|
if ( ! function_exists( 'mysql_to_rfc3339' ) ) {
|
||||||
return null;
|
return null;
|
||||||
|
@ -43,3 +44,105 @@ function wc_api_prepare_date_response( $date_gmt, $date = null ) {
|
||||||
// Return the formatted datetime.
|
// Return the formatted datetime.
|
||||||
return mysql_to_rfc3339( $date_gmt );
|
return mysql_to_rfc3339( $date_gmt );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Upload image from URL.
|
||||||
|
*
|
||||||
|
* @since 2.6.0
|
||||||
|
* @param string $image_url
|
||||||
|
* @return array|WP_Error Attachment data or error message.
|
||||||
|
*/
|
||||||
|
function wc_rest_api_upload_image_from_url( $image_url ) {
|
||||||
|
$file_name = basename( current( explode( '?', $image_url ) ) );
|
||||||
|
$wp_filetype = wp_check_filetype( $file_name, null );
|
||||||
|
$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 = str_replace( ' ', '%20', $image_url );
|
||||||
|
|
||||||
|
// Get the file.
|
||||||
|
$response = wp_safe_remote_get( $image_url, array(
|
||||||
|
'timeout' => 10
|
||||||
|
) );
|
||||||
|
|
||||||
|
if ( is_wp_error( $response ) || 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.
|
||||||
|
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 );
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set uploaded image as attachment.
|
||||||
|
*
|
||||||
|
* @since 2.6.0
|
||||||
|
* @param array $upload Upload information from wp_upload_bits.
|
||||||
|
* @param int $id Post ID. Default to 0.
|
||||||
|
* @return int Attachment ID
|
||||||
|
*/
|
||||||
|
function wc_rest_api_set_uploaded_image_as_attachment( $upload, $id = 0 ) {
|
||||||
|
$info = wp_check_filetype( $upload['file'] );
|
||||||
|
$title = '';
|
||||||
|
$content = '';
|
||||||
|
|
||||||
|
if ( $image_meta = @wp_read_image_metadata( $upload['file'] ) ) {
|
||||||
|
if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) ) {
|
||||||
|
$title = $image_meta['title'];
|
||||||
|
}
|
||||||
|
if ( trim( $image_meta['caption'] ) ) {
|
||||||
|
$content = $image_meta['caption'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$attachment = array(
|
||||||
|
'post_mime_type' => $info['type'],
|
||||||
|
'guid' => $upload['url'],
|
||||||
|
'post_parent' => $id,
|
||||||
|
'post_title' => $title,
|
||||||
|
'post_content' => $content
|
||||||
|
);
|
||||||
|
|
||||||
|
$attachment_id = wp_insert_attachment( $attachment, $upload['file'], $id );
|
||||||
|
if ( ! is_wp_error( $attachment_id ) ) {
|
||||||
|
wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $upload['file'] ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
return $attachment_id;
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue