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 <github-actions@github.com>
This commit is contained in:
Alba Rincón 2024-08-20 09:54:57 +02:00 committed by GitHub
parent ace3169c0a
commit c06190d529
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 93 additions and 137 deletions

View File

@ -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',
} );

View File

@ -0,0 +1,4 @@
Significance: minor
Type: dev
CYS - Move the "ai/store-info" endpoint to woocommerce admin API

View File

@ -0,0 +1,81 @@
<?php
declare( strict_types = 1 );
namespace Automattic\WooCommerce\Admin\API\AI;
use Automattic\WooCommerce\Blocks\AI\Connection;
use Automattic\WooCommerce\Blocks\AIContent\PatternsHelper;
use Automattic\WooCommerce\Blocks\AIContent\UpdateProducts;
use WP_Error;
use WP_REST_Request;
use WP_REST_Response;
defined( 'ABSPATH' ) || exit;
/**
* Store Info controller
*
* @internal
*/
class StoreInfo extends AIEndpoint {
/**
* Endpoint.
*
* @var string
*/
protected $endpoint = 'store-info';
/**
* Register routes.
*/
public function register_routes() {
$this->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,
);
}
}

View File

@ -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',
);
}

View File

@ -1,94 +0,0 @@
<?php
namespace Automattic\WooCommerce\StoreApi\Routes\V1\AI;
use Automattic\WooCommerce\Blocks\AIContent\PatternsHelper;
use Automattic\WooCommerce\Blocks\AIContent\UpdateProducts;
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 self::get_path_regex();
}
/**
* Get the path of this rest route.
*
* @return string
*/
public static function get_path_regex() {
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 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,
)
);
}
}

View File

@ -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,
],
];
}

View File

@ -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,
],
];

View File

@ -1,34 +0,0 @@
<?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 [];
}
}