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 9958ff36239..3e16f4683ac 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 @@ -300,7 +300,7 @@ export const updateStorePatterns = async ( const productContents = response.product_content.map( ( product, index ) => { return apiFetch( { - path: '/wc/private/ai/product', + path: '/wc-admin/ai/product', method: 'POST', data: { products_information: product, diff --git a/plugins/woocommerce/changelog/50393-48150-move-product-endpoint b/plugins/woocommerce/changelog/50393-48150-move-product-endpoint new file mode 100644 index 00000000000..3968d2b48db --- /dev/null +++ b/plugins/woocommerce/changelog/50393-48150-move-product-endpoint @@ -0,0 +1,4 @@ +Significance: minor +Type: dev + +CYS - Move the `ai/product` endpoint to woocommerce admin API. \ No newline at end of file diff --git a/plugins/woocommerce/src/Admin/API/AI/Patterns.php b/plugins/woocommerce/src/Admin/API/AI/Patterns.php index a766f2579f3..224a7afb163 100644 --- a/plugins/woocommerce/src/Admin/API/AI/Patterns.php +++ b/plugins/woocommerce/src/Admin/API/AI/Patterns.php @@ -81,8 +81,8 @@ class Patterns extends AIEndpoint { try { ( new UpdatePatterns() )->generate_content( $ai_connection, $token, $images, $business_description ); return rest_ensure_response( array( 'ai_content_generated' => true ) ); - } catch ( WP_Error $e ) { - return $e; + } catch ( \Exception $e ) { + return rest_ensure_response( array( 'ai_content_generated' => false ) ); } } diff --git a/plugins/woocommerce/src/Admin/API/AI/Product.php b/plugins/woocommerce/src/Admin/API/AI/Product.php new file mode 100644 index 00000000000..68f388cdc63 --- /dev/null +++ b/plugins/woocommerce/src/Admin/API/AI/Product.php @@ -0,0 +1,96 @@ +register( + array( + array( + 'methods' => \WP_REST_Server::CREATABLE, + 'callback' => array( $this, 'update_product' ), + 'permission_callback' => array( Middleware::class, 'is_authorized' ), + 'args' => array( + 'products_information' => array( + 'description' => __( 'Data generated by AI for updating dummy products.', 'woocommerce' ), + 'type' => 'object', + ), + 'last_product' => array( + 'description' => __( 'Whether the product being updated is the last one in the loop', 'woocommerce' ), + 'type' => 'boolean', + ), + ), + ), + ) + ); + } + + /** + * Update product with the content and images powered by AI. + * + * @param WP_REST_Request $request Request object. + * + * @return WP_REST_Response + */ + public function update_product( WP_REST_Request $request ) { + $product_information = $request['products_information'] ?? array(); + + if ( empty( $product_information ) ) { + return rest_ensure_response( + array( + self::AI_CONTENT_GENERATED => true, + ) + ); + } + + try { + $product_updater = new UpdateProducts(); + $product_updater->update_product_content( $product_information ); + } catch ( \Exception $e ) { + return rest_ensure_response( array( 'ai_content_generated' => false ) ); + } + + $last_product_to_update = $request['last_product'] ?? false; + + if ( $last_product_to_update ) { + flush_rewrite_rules(); + } + + return rest_ensure_response( + array( + self::AI_CONTENT_GENERATED => true, + ) + ); + } +} diff --git a/plugins/woocommerce/src/Admin/API/Init.php b/plugins/woocommerce/src/Admin/API/Init.php index e107ec897f9..9ea331a9ef7 100644 --- a/plugins/woocommerce/src/Admin/API/Init.php +++ b/plugins/woocommerce/src/Admin/API/Init.php @@ -91,6 +91,7 @@ class Init { 'Automattic\WooCommerce\Admin\API\AI\StoreInfo', 'Automattic\WooCommerce\Admin\API\AI\Images', 'Automattic\WooCommerce\Admin\API\AI\Patterns', + 'Automattic\WooCommerce\Admin\API\AI\Product', ); } diff --git a/plugins/woocommerce/src/StoreApi/Routes/V1/AI/Product.php b/plugins/woocommerce/src/StoreApi/Routes/V1/AI/Product.php deleted file mode 100644 index 46b76355e71..00000000000 --- a/plugins/woocommerce/src/StoreApi/Routes/V1/AI/Product.php +++ /dev/null @@ -1,107 +0,0 @@ - \WP_REST_Server::CREATABLE, - 'callback' => [ $this, 'get_response' ], - 'permission_callback' => [ Middleware::class, 'is_authorized' ], - 'args' => [ - 'products_information' => [ - 'description' => __( 'Data generated by AI for updating dummy products.', 'woocommerce' ), - 'type' => 'object', - ], - 'last_product' => [ - 'description' => __( 'Whether the product being updated is the last one in the loop', 'woocommerce' ), - 'type' => 'boolean', - ], - ], - ], - 'schema' => [ $this->schema, 'get_public_item_schema' ], - 'allow_batch' => [ 'v1' => true ], - ]; - } - - /** - * Update product with the content and image powered by AI. - * - * @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 ) { - $product_updater = new UpdateProducts(); - $product_information = $request['products_information'] ?? array(); - - if ( empty( $product_information ) ) { - return rest_ensure_response( - array( - 'ai_content_generated' => true, - ) - ); - } - - $product_updater->update_product_content( $product_information ); - - $last_product_to_update = $request['last_product'] ?? false; - - if ( $last_product_to_update ) { - flush_rewrite_rules(); - } - - return rest_ensure_response( - array( - 'ai_content_generated' => true, - ) - ); - } - -} diff --git a/plugins/woocommerce/src/StoreApi/RoutesController.php b/plugins/woocommerce/src/StoreApi/RoutesController.php index 517318964d8..1b88b70daa0 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\Product::IDENTIFIER => Routes\V1\AI\Product::class, Routes\V1\AI\Products::IDENTIFIER => Routes\V1\AI\Products::class, Routes\V1\Patterns::IDENTIFIER => Routes\V1\Patterns::class, ], diff --git a/plugins/woocommerce/src/StoreApi/SchemaController.php b/plugins/woocommerce/src/StoreApi/SchemaController.php index 3e693afce8b..252b475b584 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\ProductSchema::IDENTIFIER => Schemas\V1\AI\ProductSchema::class, Schemas\V1\AI\ProductsSchema::IDENTIFIER => Schemas\V1\AI\ProductsSchema::class, Schemas\V1\PatternsSchema::IDENTIFIER => Schemas\V1\PatternsSchema::class, ], diff --git a/plugins/woocommerce/src/StoreApi/Schemas/V1/AI/ProductSchema.php b/plugins/woocommerce/src/StoreApi/Schemas/V1/AI/ProductSchema.php deleted file mode 100644 index d2c4aa4ccd4..00000000000 --- a/plugins/woocommerce/src/StoreApi/Schemas/V1/AI/ProductSchema.php +++ /dev/null @@ -1,47 +0,0 @@ - true, - ]; - } -}