From 24d0fed8e5d46b388ac504205630fc115708c10f Mon Sep 17 00:00:00 2001 From: Luigi Teschio Date: Wed, 20 Dec 2023 15:41:04 +0100 Subject: [PATCH] CYS - reset products and pattern when the site doesn't have AI generated content (#42970) * CYS - reset products and pattern when the site doesn't have AI generated content * Add changefile(s) from automation for the following project(s): woocommerce * fix lint --------- Co-authored-by: github-actions --- .../design-with-ai/services.ts | 11 +++ ...the-store-already-has-ai-generated-content | 4 + .../src/StoreApi/Routes/V1/AI/StoreInfo.php | 85 +++++++++++++++++++ .../src/StoreApi/RoutesController.php | 1 + .../src/StoreApi/SchemaController.php | 1 + .../Schemas/V1/AI/StoreInfoSchema.php | 34 ++++++++ 6 files changed, 136 insertions(+) create mode 100644 plugins/woocommerce/changelog/42970-42906-cys-make-sure-patterns-are-not-reset-to-default-if-ai-is-down-and-the-store-already-has-ai-generated-content create mode 100644 plugins/woocommerce/src/StoreApi/Routes/V1/AI/StoreInfo.php create 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 b23e0b9f5df..4f97178f547 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 @@ -479,6 +479,17 @@ const resetPatternsAndProducts = () => async () => { woocommerce_blocks_allow_ai_connection: 'yes', } ); + const response = await apiFetch< { + is_ai_generated: boolean; + } >( { + path: '/wc/private/ai/store-info', + method: 'GET', + } ); + + if ( response.is_ai_generated ) { + return; + } + return Promise.all( [ apiFetch( { path: '/wc/private/ai/patterns', diff --git a/plugins/woocommerce/changelog/42970-42906-cys-make-sure-patterns-are-not-reset-to-default-if-ai-is-down-and-the-store-already-has-ai-generated-content b/plugins/woocommerce/changelog/42970-42906-cys-make-sure-patterns-are-not-reset-to-default-if-ai-is-down-and-the-store-already-has-ai-generated-content new file mode 100644 index 00000000000..4a86e55359d --- /dev/null +++ b/plugins/woocommerce/changelog/42970-42906-cys-make-sure-patterns-are-not-reset-to-default-if-ai-is-down-and-the-store-already-has-ai-generated-content @@ -0,0 +1,4 @@ +Significance: minor +Type: enhancement + +CYS - Reset products and pattern when the site doesn't have AI generated content. \ No newline at end of file diff --git a/plugins/woocommerce/src/StoreApi/Routes/V1/AI/StoreInfo.php b/plugins/woocommerce/src/StoreApi/Routes/V1/AI/StoreInfo.php new file mode 100644 index 00000000000..27c66476d95 --- /dev/null +++ b/plugins/woocommerce/src/StoreApi/Routes/V1/AI/StoreInfo.php @@ -0,0 +1,85 @@ + \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 ProductUpdater(); + $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 de734454396..1fa94717619 100644 --- a/plugins/woocommerce/src/StoreApi/RoutesController.php +++ b/plugins/woocommerce/src/StoreApi/RoutesController.php @@ -67,6 +67,7 @@ class RoutesController { Routes\V1\AI\Product::IDENTIFIER => Routes\V1\AI\Product::class, Routes\V1\AI\Products::IDENTIFIER => Routes\V1\AI\Products::class, Routes\V1\AI\BusinessDescription::IDENTIFIER => Routes\V1\AI\BusinessDescription::class, + Routes\V1\AI\StoreInfo::IDENTIFIER => Routes\V1\AI\StoreInfo::class, ], ]; } diff --git a/plugins/woocommerce/src/StoreApi/SchemaController.php b/plugins/woocommerce/src/StoreApi/SchemaController.php index f836f7ce4e6..1f14d6b80d7 100644 --- a/plugins/woocommerce/src/StoreApi/SchemaController.php +++ b/plugins/woocommerce/src/StoreApi/SchemaController.php @@ -60,6 +60,7 @@ class SchemaController { Schemas\V1\AI\ProductSchema::IDENTIFIER => Schemas\V1\AI\ProductSchema::class, Schemas\V1\AI\ProductsSchema::IDENTIFIER => Schemas\V1\AI\ProductsSchema::class, Schemas\V1\AI\BusinessDescriptionSchema::IDENTIFIER => Schemas\V1\AI\BusinessDescriptionSchema::class, + Schemas\V1\AI\StoreInfoSchema::IDENTIFIER => Schemas\V1\AI\StoreInfoSchema::class, ], ]; } diff --git a/plugins/woocommerce/src/StoreApi/Schemas/V1/AI/StoreInfoSchema.php b/plugins/woocommerce/src/StoreApi/Schemas/V1/AI/StoreInfoSchema.php new file mode 100644 index 00000000000..251e702d9a9 --- /dev/null +++ b/plugins/woocommerce/src/StoreApi/Schemas/V1/AI/StoreInfoSchema.php @@ -0,0 +1,34 @@ +