From c06190d5297b76438c475f7309a695c4bb6d51e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alba=20Rinc=C3=B3n?= Date: Tue, 20 Aug 2024 09:54:57 +0200 Subject: [PATCH] CYS - Move the `ai/store-info` endpoint to woocommerce admin API (#50363) * CYS - Move the ai/store-title endpoint to woocommerce admin api * Add middleware and callback * Add changefile(s) from automation for the following project(s): woocommerce * Fix lint error * CYS - Move the ai/business-description endpoint to woocommerce admin API * CYS - Move the ai/store-info endpoint to woocommerce admin API * Update endpoint * Add changefile(s) from automation for the following project(s): woocommerce * Use constant and normalize site title values * Add base AI Endpoint class * Fix lint error * Use the endpoint base class * Revert change * Add changefile(s) from automation for the following project(s): woocommerce * Use ai endpoint class * Add strict types * Fix lint error * Add strict type --------- Co-authored-by: github-actions --- .../design-with-ai/services.ts | 4 +- .../50363-48150-move-store-info-endpoint | 4 + .../src/Admin/API/AI/StoreInfo.php | 81 ++++++++++++++++ plugins/woocommerce/src/Admin/API/Init.php | 1 + .../src/StoreApi/Routes/V1/AI/StoreInfo.php | 94 ------------------- .../src/StoreApi/RoutesController.php | 11 +-- .../src/StoreApi/SchemaController.php | 1 - .../Schemas/V1/AI/StoreInfoSchema.php | 34 ------- 8 files changed, 93 insertions(+), 137 deletions(-) create mode 100644 plugins/woocommerce/changelog/50363-48150-move-store-info-endpoint create mode 100644 plugins/woocommerce/src/Admin/API/AI/StoreInfo.php delete mode 100644 plugins/woocommerce/src/StoreApi/Routes/V1/AI/StoreInfo.php delete mode 100644 plugins/woocommerce/src/StoreApi/Schemas/V1/AI/StoreInfoSchema.php 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 8d43f47ae59..b669a5d0238 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 @@ -206,7 +206,7 @@ const resetPatternsAndProducts = () => async () => { const response = await apiFetch< { is_ai_generated: boolean; } >( { - path: '/wc/private/ai/store-info', + path: '/wc-admin/ai/store-info', method: 'GET', } ); @@ -250,7 +250,7 @@ export const updateStorePatterns = async ( const { is_ai_generated } = await apiFetch< { is_ai_generated: boolean; } >( { - path: '/wc/private/ai/store-info', + path: '/wc-admin/ai/store-info', method: 'GET', } ); diff --git a/plugins/woocommerce/changelog/50363-48150-move-store-info-endpoint b/plugins/woocommerce/changelog/50363-48150-move-store-info-endpoint new file mode 100644 index 00000000000..6325391cf7f --- /dev/null +++ b/plugins/woocommerce/changelog/50363-48150-move-store-info-endpoint @@ -0,0 +1,4 @@ +Significance: minor +Type: dev + +CYS - Move the "ai/store-info" endpoint to woocommerce admin API \ No newline at end of file diff --git a/plugins/woocommerce/src/Admin/API/AI/StoreInfo.php b/plugins/woocommerce/src/Admin/API/AI/StoreInfo.php new file mode 100644 index 00000000000..0fb0fb3ab8c --- /dev/null +++ b/plugins/woocommerce/src/Admin/API/AI/StoreInfo.php @@ -0,0 +1,81 @@ +register( + array( + array( + 'methods' => \WP_REST_Server::READABLE, + 'callback' => array( $this, 'get_response' ), + 'permission_callback' => array( Middleware::class, 'is_authorized' ), + ), + 'schema' => array( $this, 'get_schema' ), + ) + ); + } + + /** + * Update the store title powered by AI. + * + * @return WP_Error|WP_REST_Response + */ + public function get_response() { + $product_updater = new UpdateProducts(); + $patterns = PatternsHelper::get_patterns_ai_data_post(); + + $products = $product_updater->fetch_product_ids( 'dummy' ); + + if ( empty( $products ) && ! isset( $patterns ) ) { + return rest_ensure_response( + array( + 'is_ai_generated' => false, + ) + ); + } + + return rest_ensure_response( + array( + 'is_ai_generated' => true, + ) + ); + } + + /** + * Get the Business Description response. + * + * @return array + */ + public function get_schema() { + return array( + 'ai_content_generated' => true, + ); + } +} diff --git a/plugins/woocommerce/src/Admin/API/Init.php b/plugins/woocommerce/src/Admin/API/Init.php index 4750b156c41..afafc423a04 100644 --- a/plugins/woocommerce/src/Admin/API/Init.php +++ b/plugins/woocommerce/src/Admin/API/Init.php @@ -88,6 +88,7 @@ class Init { 'Automattic\WooCommerce\Admin\API\ShippingPartnerSuggestions', 'Automattic\WooCommerce\Admin\API\AI\StoreTitle', 'Automattic\WooCommerce\Admin\API\AI\BusinessDescription', + 'Automattic\WooCommerce\Admin\API\AI\StoreInfo', ); } diff --git a/plugins/woocommerce/src/StoreApi/Routes/V1/AI/StoreInfo.php b/plugins/woocommerce/src/StoreApi/Routes/V1/AI/StoreInfo.php deleted file mode 100644 index c3b28d61676..00000000000 --- a/plugins/woocommerce/src/StoreApi/Routes/V1/AI/StoreInfo.php +++ /dev/null @@ -1,94 +0,0 @@ - \WP_REST_Server::READABLE, - 'callback' => [ $this, 'get_response' ], - 'permission_callback' => [ Middleware::class, 'is_authorized' ], - ], - 'schema' => [ $this->schema, 'get_public_item_schema' ], - 'allow_batch' => [ 'v1' => true ], - ]; - } - - /** - * Update the store title powered by AI. - * - * @param \WP_REST_Request $request Request object. - * - * @return bool|string|\WP_Error|\WP_REST_Response - */ - protected function get_route_response( \WP_REST_Request $request ) { - $product_updater = new UpdateProducts(); - $patterns = PatternsHelper::get_patterns_ai_data_post(); - - $products = $product_updater->fetch_product_ids( 'dummy' ); - - if ( empty( $products ) && ! isset( $patterns ) ) { - return rest_ensure_response( - array( - 'is_ai_generated' => false, - ) - ); - } - - return rest_ensure_response( - array( - 'is_ai_generated' => true, - ) - ); - - } - - -} diff --git a/plugins/woocommerce/src/StoreApi/RoutesController.php b/plugins/woocommerce/src/StoreApi/RoutesController.php index 1e4b902a4f5..3b9b5bbe5cf 100644 --- a/plugins/woocommerce/src/StoreApi/RoutesController.php +++ b/plugins/woocommerce/src/StoreApi/RoutesController.php @@ -68,12 +68,11 @@ 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, - Routes\V1\AI\StoreInfo::IDENTIFIER => Routes\V1\AI\StoreInfo::class, - Routes\V1\Patterns::IDENTIFIER => Routes\V1\Patterns::class, + 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, + 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 170a151105b..907c60000db 100644 --- a/plugins/woocommerce/src/StoreApi/SchemaController.php +++ b/plugins/woocommerce/src/StoreApi/SchemaController.php @@ -58,7 +58,6 @@ class SchemaController { 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, - Schemas\V1\AI\StoreInfoSchema::IDENTIFIER => Schemas\V1\AI\StoreInfoSchema::class, Schemas\V1\PatternsSchema::IDENTIFIER => Schemas\V1\PatternsSchema::class, ], ]; diff --git a/plugins/woocommerce/src/StoreApi/Schemas/V1/AI/StoreInfoSchema.php b/plugins/woocommerce/src/StoreApi/Schemas/V1/AI/StoreInfoSchema.php deleted file mode 100644 index 251e702d9a9..00000000000 --- a/plugins/woocommerce/src/StoreApi/Schemas/V1/AI/StoreInfoSchema.php +++ /dev/null @@ -1,34 +0,0 @@ -