diff --git a/plugins/woocommerce-admin/client/customize-store/design-with-ai/services.ts b/plugins/woocommerce-admin/client/customize-store/design-with-ai/services.ts index b669a5d0238..978876e66d9 100644 --- a/plugins/woocommerce-admin/client/customize-store/design-with-ai/services.ts +++ b/plugins/woocommerce-admin/client/customize-store/design-with-ai/services.ts @@ -239,7 +239,7 @@ export const updateStorePatterns = async ( ai_content_generated: boolean; images: { images: Array< unknown >; search_term: string }; } >( { - path: '/wc/private/ai/images', + path: '/wc-admin/ai/images', method: 'POST', data: { business_description: diff --git a/plugins/woocommerce/changelog/50365-48150-move-images-endpoint b/plugins/woocommerce/changelog/50365-48150-move-images-endpoint new file mode 100644 index 00000000000..cceb3d51caf --- /dev/null +++ b/plugins/woocommerce/changelog/50365-48150-move-images-endpoint @@ -0,0 +1,4 @@ +Significance: minor +Type: dev + +CYS - Move the `ai/images` endpoint to woocommerce admin API \ No newline at end of file diff --git a/plugins/woocommerce/src/Admin/API/AI/AIEndpoint.php b/plugins/woocommerce/src/Admin/API/AI/AIEndpoint.php index ff177b2fa93..12ea82b0fc5 100644 --- a/plugins/woocommerce/src/Admin/API/AI/AIEndpoint.php +++ b/plugins/woocommerce/src/Admin/API/AI/AIEndpoint.php @@ -51,5 +51,7 @@ abstract class AIEndpoint { * * @return array */ - abstract public function get_schema(); + public function get_schema() { + return array(); + } } diff --git a/plugins/woocommerce/src/Admin/API/AI/Images.php b/plugins/woocommerce/src/Admin/API/AI/Images.php new file mode 100644 index 00000000000..5acd60e2030 --- /dev/null +++ b/plugins/woocommerce/src/Admin/API/AI/Images.php @@ -0,0 +1,108 @@ +register( + array( + array( + 'methods' => \WP_REST_Server::CREATABLE, + 'callback' => array( $this, 'generate_images' ), + 'permission_callback' => array( Middleware::class, 'is_authorized' ), + 'args' => array( + 'business_description' => array( + 'description' => __( 'The business description for a given store.', 'woocommerce' ), + 'type' => 'string', + ), + ), + ), + ) + ); + } + + /** + * Generate Images from Pexels + * + * @param WP_REST_Request $request Request object. + * + * @return WP_Error|WP_REST_Response + */ + public function generate_images( WP_REST_Request $request ) { + + $business_description = sanitize_text_field( wp_unslash( $request['business_description'] ) ); + + if ( empty( $business_description ) ) { + $business_description = get_option( 'woo_ai_describe_store_description' ); + } + + $last_business_description = get_option( 'last_business_description_with_ai_content_generated' ); + + if ( $last_business_description === $business_description ) { + return rest_ensure_response( + $this->prepare_item_for_response( + array( + 'ai_content_generated' => true, + 'images' => array(), + ), + $request + ) + ); + } + + $ai_connection = new Connection(); + + $site_id = $ai_connection->get_site_id(); + + if ( is_wp_error( $site_id ) ) { + return $site_id; + } + + $token = $ai_connection->get_jwt_token( $site_id ); + + if ( is_wp_error( $token ) ) { + return $token; + } + + $images = ( new Pexels() )->get_images( $ai_connection, $token, $business_description ); + + if ( is_wp_error( $images ) ) { + $images = array( + 'images' => array(), + 'search_term' => '', + ); + } + + return rest_ensure_response( + array( + 'ai_content_generated' => true, + 'images' => $images, + ) + ); + } +} diff --git a/plugins/woocommerce/src/Admin/API/Init.php b/plugins/woocommerce/src/Admin/API/Init.php index afafc423a04..9c53054e54a 100644 --- a/plugins/woocommerce/src/Admin/API/Init.php +++ b/plugins/woocommerce/src/Admin/API/Init.php @@ -89,6 +89,7 @@ class Init { 'Automattic\WooCommerce\Admin\API\AI\StoreTitle', 'Automattic\WooCommerce\Admin\API\AI\BusinessDescription', 'Automattic\WooCommerce\Admin\API\AI\StoreInfo', + 'Automattic\WooCommerce\Admin\API\AI\Images', ); } diff --git a/plugins/woocommerce/src/StoreApi/Routes/V1/AI/Images.php b/plugins/woocommerce/src/StoreApi/Routes/V1/AI/Images.php deleted file mode 100644 index 51cf7d2abc1..00000000000 --- a/plugins/woocommerce/src/StoreApi/Routes/V1/AI/Images.php +++ /dev/null @@ -1,130 +0,0 @@ - \WP_REST_Server::CREATABLE, - 'callback' => [ $this, 'get_response' ], - 'permission_callback' => [ Middleware::class, 'is_authorized' ], - 'args' => [ - 'business_description' => [ - 'description' => __( 'The business description for a given store.', 'woocommerce' ), - 'type' => 'string', - ], - ], - ], - 'schema' => [ $this->schema, 'get_public_item_schema' ], - 'allow_batch' => [ 'v1' => true ], - ]; - } - - /** - * Generate Images from Pexels - * - * @param \WP_REST_Request $request Request object. - * - * @return bool|string|\WP_Error|\WP_REST_Response - */ - protected function get_route_post_response( \WP_REST_Request $request ) { - - $business_description = sanitize_text_field( wp_unslash( $request['business_description'] ) ); - - if ( empty( $business_description ) ) { - $business_description = get_option( 'woo_ai_describe_store_description' ); - } - - $last_business_description = get_option( 'last_business_description_with_ai_content_generated' ); - - if ( $last_business_description === $business_description ) { - return rest_ensure_response( - $this->prepare_item_for_response( - [ - 'ai_content_generated' => true, - 'images' => array(), - ], - $request - ) - ); - } - - $ai_connection = new Connection(); - - $site_id = $ai_connection->get_site_id(); - - if ( is_wp_error( $site_id ) ) { - return $this->error_to_response( $site_id ); - } - - $token = $ai_connection->get_jwt_token( $site_id ); - - if ( is_wp_error( $token ) ) { - return $this->error_to_response( $token ); - } - - $images = ( new Pexels() )->get_images( $ai_connection, $token, $business_description ); - - if ( is_wp_error( $images ) ) { - $images = array( - 'images' => array(), - 'search_term' => '', - ); - } - - return rest_ensure_response( - $this->prepare_item_for_response( - [ - 'ai_content_generated' => true, - 'images' => $images, - ], - $request - ) - ); - } -} diff --git a/plugins/woocommerce/src/StoreApi/RoutesController.php b/plugins/woocommerce/src/StoreApi/RoutesController.php index 3b9b5bbe5cf..efe2d07dd4e 100644 --- a/plugins/woocommerce/src/StoreApi/RoutesController.php +++ b/plugins/woocommerce/src/StoreApi/RoutesController.php @@ -68,7 +68,6 @@ class RoutesController { ], // @todo Migrate internal AI routes to WooCommerce Core codebase. 'private' => [ - Routes\V1\AI\Images::IDENTIFIER => Routes\V1\AI\Images::class, Routes\V1\AI\Patterns::IDENTIFIER => Routes\V1\AI\Patterns::class, Routes\V1\AI\Product::IDENTIFIER => Routes\V1\AI\Product::class, Routes\V1\AI\Products::IDENTIFIER => Routes\V1\AI\Products::class, diff --git a/plugins/woocommerce/src/StoreApi/SchemaController.php b/plugins/woocommerce/src/StoreApi/SchemaController.php index 907c60000db..eb8351a4e8d 100644 --- a/plugins/woocommerce/src/StoreApi/SchemaController.php +++ b/plugins/woocommerce/src/StoreApi/SchemaController.php @@ -54,7 +54,6 @@ class SchemaController { Schemas\V1\ProductCategorySchema::IDENTIFIER => Schemas\V1\ProductCategorySchema::class, Schemas\V1\ProductCollectionDataSchema::IDENTIFIER => Schemas\V1\ProductCollectionDataSchema::class, Schemas\V1\ProductReviewSchema::IDENTIFIER => Schemas\V1\ProductReviewSchema::class, - Schemas\V1\AI\ImagesSchema::IDENTIFIER => Schemas\V1\AI\ImagesSchema::class, Schemas\V1\AI\PatternsSchema::IDENTIFIER => Schemas\V1\AI\PatternsSchema::class, Schemas\V1\AI\ProductSchema::IDENTIFIER => Schemas\V1\AI\ProductSchema::class, Schemas\V1\AI\ProductsSchema::IDENTIFIER => Schemas\V1\AI\ProductsSchema::class, diff --git a/plugins/woocommerce/src/StoreApi/Schemas/V1/AI/ImagesSchema.php b/plugins/woocommerce/src/StoreApi/Schemas/V1/AI/ImagesSchema.php deleted file mode 100644 index 317e4c028f6..00000000000 --- a/plugins/woocommerce/src/StoreApi/Schemas/V1/AI/ImagesSchema.php +++ /dev/null @@ -1,45 +0,0 @@ -