CYS - Move the `ai/product` endpoint to woocommerce admin API (#50393)

* 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

* CYS - Move the ai/images endpoint to woocommerce admin API

* CYS - Move the `ai/patterns` endpoint to woocommerce admin API

* CYS - Move the `ai/product` endpoint to woocommerce admin API

* Add changefile(s) from automation for the following project(s): woocommerce

* Remove import

* Add changefile(s) from automation for the following project(s): woocommerce-beta-tester, woocommerce

* Add changefile(s) from automation for the following project(s): woocommerce

* Add strict types

* Extract AI_CONTENT_GENERATED constant

* Move instance creating to where it's used

* Add try/catch and fix the on Patterns

* Use the base AIEndpoint class

* Fix endpoint

* Update comment return type

* Fix comments

---------

Co-authored-by: github-actions <github-actions@github.com>
This commit is contained in:
Alba Rincón 2024-08-21 15:34:12 +02:00 committed by GitHub
parent 944d9233ed
commit 796854770c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 104 additions and 159 deletions

View File

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

View File

@ -0,0 +1,4 @@
Significance: minor
Type: dev
CYS - Move the `ai/product` endpoint to woocommerce admin API.

View File

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

View File

@ -0,0 +1,96 @@
<?php
declare( strict_types = 1 );
namespace Automattic\WooCommerce\Admin\API\AI;
use Automattic\WooCommerce\Blocks\AIContent\UpdateProducts;
use WP_Error;
use WP_REST_Request;
use WP_REST_Response;
defined( 'ABSPATH' ) || exit;
/**
* Product controller
*
* @internal
*/
class Product extends AIEndpoint {
/**
* The endpoint response option name.
*
* @var string
*/
const AI_CONTENT_GENERATED = 'ai_content_generated';
/**
* Endpoint.
*
* @var string
*/
protected $endpoint = 'product';
/**
* Register routes.
*/
public function register_routes() {
$this->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,
)
);
}
}

View File

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

View File

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

View File

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

View File

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

View File

@ -1,47 +0,0 @@
<?php
namespace Automattic\WooCommerce\StoreApi\Schemas\V1\AI;
use Automattic\WooCommerce\StoreApi\Schemas\V1\AbstractSchema;
/**
* ProductSchema class.
*
* @internal
*/
class ProductSchema extends AbstractSchema {
/**
* The schema item name.
*
* @var string
*/
protected $title = 'ai/product';
/**
* The schema item identifier.
*
* @var string
*/
const IDENTIFIER = 'ai/product';
/**
* Patterns schema properties.
*
* @return array
*/
public function get_properties() {
return [];
}
/**
* Get the Product response.
*
* @param array $item Item to get response for.
*
* @return array
*/
public function get_item_response( $item ) {
return [
'ai_content_generated' => true,
];
}
}