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 <github-actions@github.com>
This commit is contained in:
Luigi Teschio 2023-12-20 15:41:04 +01:00 committed by GitHub
parent 2f2e573f48
commit 24d0fed8e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 136 additions and 0 deletions

View File

@ -479,6 +479,17 @@ const resetPatternsAndProducts = () => async () => {
woocommerce_blocks_allow_ai_connection: 'yes', 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( [ return Promise.all( [
apiFetch( { apiFetch( {
path: '/wc/private/ai/patterns', path: '/wc/private/ai/patterns',

View File

@ -0,0 +1,4 @@
Significance: minor
Type: enhancement
CYS - Reset products and pattern when the site doesn't have AI generated content.

View File

@ -0,0 +1,85 @@
<?php
namespace Automattic\WooCommerce\StoreApi\Routes\V1\AI;
use Automattic\WooCommerce\Blocks\Patterns\PatternsHelper;
use Automattic\WooCommerce\Blocks\Patterns\ProductUpdater;
use Automattic\WooCommerce\StoreApi\Routes\V1\AbstractRoute;
/**
* StoreInfo class.
*
* @internal
*/
class StoreInfo extends AbstractRoute {
/**
* The route identifier.
*
* @var string
*/
const IDENTIFIER = 'ai/store-info';
/**
* The schema item identifier.
*
* @var string
*/
const SCHEMA_TYPE = 'ai/store-info';
/**
* Get the path of this REST route.
*
* @return string
*/
public function get_path() {
return '/ai/store-info';
}
/**
* Get method arguments for this REST route.
*
* @return array An array of endpoints.
*/
public function get_args() {
return [
[
'methods' => \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,
)
);
}
}

View File

@ -67,6 +67,7 @@ class RoutesController {
Routes\V1\AI\Product::IDENTIFIER => Routes\V1\AI\Product::class, Routes\V1\AI\Product::IDENTIFIER => Routes\V1\AI\Product::class,
Routes\V1\AI\Products::IDENTIFIER => Routes\V1\AI\Products::class, Routes\V1\AI\Products::IDENTIFIER => Routes\V1\AI\Products::class,
Routes\V1\AI\BusinessDescription::IDENTIFIER => Routes\V1\AI\BusinessDescription::class, Routes\V1\AI\BusinessDescription::IDENTIFIER => Routes\V1\AI\BusinessDescription::class,
Routes\V1\AI\StoreInfo::IDENTIFIER => Routes\V1\AI\StoreInfo::class,
], ],
]; ];
} }

View File

@ -60,6 +60,7 @@ class SchemaController {
Schemas\V1\AI\ProductSchema::IDENTIFIER => Schemas\V1\AI\ProductSchema::class, Schemas\V1\AI\ProductSchema::IDENTIFIER => Schemas\V1\AI\ProductSchema::class,
Schemas\V1\AI\ProductsSchema::IDENTIFIER => Schemas\V1\AI\ProductsSchema::class, Schemas\V1\AI\ProductsSchema::IDENTIFIER => Schemas\V1\AI\ProductsSchema::class,
Schemas\V1\AI\BusinessDescriptionSchema::IDENTIFIER => Schemas\V1\AI\BusinessDescriptionSchema::class, Schemas\V1\AI\BusinessDescriptionSchema::IDENTIFIER => Schemas\V1\AI\BusinessDescriptionSchema::class,
Schemas\V1\AI\StoreInfoSchema::IDENTIFIER => Schemas\V1\AI\StoreInfoSchema::class,
], ],
]; ];
} }

View File

@ -0,0 +1,34 @@
<?php
namespace Automattic\WooCommerce\StoreApi\Schemas\V1\AI;
use Automattic\WooCommerce\StoreApi\Schemas\V1\AbstractSchema;
/**
* StoreInfoSchema class.
*
* @internal
*/
class StoreInfoSchema extends AbstractSchema {
/**
* The schema item name.
*
* @var string
*/
protected $title = 'ai/store-info';
/**
* The schema item identifier.
*
* @var string
*/
const IDENTIFIER = 'ai/store-info';
/**
* Store Info schema properties.
*
* @return array
*/
public function get_properties() {
return [];
}
}