CYS - Move the `ai/products` endpoint to woocommerce admin API (#50396)
* 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 * CYS - Move the `ai/products` endpoint to woocommerce admin API * Add changefile(s) from automation for the following project(s): woocommerce * 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 * Fix merge issues * Add strict types * Start extending from AIEndpoint --------- Co-authored-by: github-actions <github-actions@github.com>
This commit is contained in:
parent
0a60c3c5ec
commit
ae6f7837b2
|
@ -220,7 +220,7 @@ const resetPatternsAndProducts = () => async () => {
|
|||
method: 'DELETE',
|
||||
} ),
|
||||
apiFetch( {
|
||||
path: '/wc/private/ai/products',
|
||||
path: '/wc-admin/ai/products',
|
||||
method: 'DELETE',
|
||||
} ),
|
||||
] );
|
||||
|
@ -278,7 +278,7 @@ export const updateStorePatterns = async (
|
|||
additional_errors?: unknown[];
|
||||
} >( [
|
||||
apiFetch( {
|
||||
path: '/wc/private/ai/products',
|
||||
path: '/wc-admin/ai/products',
|
||||
method: 'POST',
|
||||
data: {
|
||||
business_description:
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
Significance: minor
|
||||
Type: dev
|
||||
|
||||
CYS - Move the "ai/products" endpoint to woocommerce admin API.
|
|
@ -0,0 +1,128 @@
|
|||
<?php
|
||||
|
||||
declare( strict_types = 1 );
|
||||
|
||||
namespace Automattic\WooCommerce\Admin\API\AI;
|
||||
|
||||
use Automattic\WooCommerce\Blocks\AI\Connection;
|
||||
use Automattic\WooCommerce\Blocks\AIContent\UpdateProducts;
|
||||
use WP_Error;
|
||||
use WP_REST_Request;
|
||||
use WP_REST_Response;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Product controller
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
class Products extends AIEndpoint {
|
||||
/**
|
||||
* Endpoint.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $endpoint = 'products';
|
||||
|
||||
/**
|
||||
* Register routes.
|
||||
*/
|
||||
public function register_routes() {
|
||||
$this->register(
|
||||
array(
|
||||
array(
|
||||
'methods' => \WP_REST_Server::CREATABLE,
|
||||
'callback' => array( $this, 'generate_products_content' ),
|
||||
'permission_callback' => array( Middleware::class, 'is_authorized' ),
|
||||
'args' => array(
|
||||
'business_description' => array(
|
||||
'description' => __( 'The business description for a given store.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
),
|
||||
'images' => array(
|
||||
'description' => __( 'The images for a given store.', 'woocommerce' ),
|
||||
'type' => 'object',
|
||||
),
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => \WP_REST_Server::DELETABLE,
|
||||
'callback' => array( $this, 'delete_products' ),
|
||||
'permission_callback' => array( Middleware::class, 'is_authorized' ),
|
||||
),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate the content for the products.
|
||||
*
|
||||
* @param WP_REST_Request $request Request object.
|
||||
*
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function generate_products_content( WP_REST_Request $request ) {
|
||||
$allow_ai_connection = get_option( 'woocommerce_blocks_allow_ai_connection' );
|
||||
|
||||
if ( ! $allow_ai_connection ) {
|
||||
return rest_ensure_response(
|
||||
new WP_Error(
|
||||
'ai_connection_not_allowed',
|
||||
__( 'AI content generation is not allowed on this store. Update your store settings if you wish to enable this feature.', 'woocommerce' )
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
$business_description = sanitize_text_field( wp_unslash( $request['business_description'] ) );
|
||||
|
||||
if ( empty( $business_description ) ) {
|
||||
$business_description = get_option( 'woo_ai_describe_store_description' );
|
||||
}
|
||||
|
||||
$ai_connection = new Connection();
|
||||
|
||||
$site_id = $ai_connection->get_site_id();
|
||||
|
||||
if ( is_wp_error( $site_id ) ) {
|
||||
return $site_id;
|
||||
}
|
||||
|
||||
$token = $ai_connection->get_jwt_token( $site_id );
|
||||
|
||||
if ( is_wp_error( $token ) ) {
|
||||
return $token;
|
||||
}
|
||||
|
||||
$images = $request['images'];
|
||||
|
||||
$populate_products = ( new UpdateProducts() )->generate_content( $ai_connection, $token, $images, $business_description );
|
||||
|
||||
if ( is_wp_error( $populate_products ) ) {
|
||||
return $populate_products;
|
||||
}
|
||||
|
||||
if ( ! isset( $populate_products['product_content'] ) ) {
|
||||
return new WP_Error( 'product_content_not_found', __( 'Product content not found.', 'woocommerce' ) );
|
||||
}
|
||||
|
||||
$product_content = $populate_products['product_content'];
|
||||
|
||||
$item = array(
|
||||
'ai_content_generated' => true,
|
||||
'product_content' => $product_content,
|
||||
);
|
||||
|
||||
return rest_ensure_response( $item );
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove products generated by AI.
|
||||
*
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function delete_products() {
|
||||
( new UpdateProducts() )->reset_products_content();
|
||||
return rest_ensure_response( array( 'removed' => true ) );
|
||||
}
|
||||
}
|
|
@ -92,6 +92,7 @@ class Init {
|
|||
'Automattic\WooCommerce\Admin\API\AI\Images',
|
||||
'Automattic\WooCommerce\Admin\API\AI\Patterns',
|
||||
'Automattic\WooCommerce\Admin\API\AI\Product',
|
||||
'Automattic\WooCommerce\Admin\API\AI\Products',
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,50 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Automattic\WooCommerce\StoreApi\Routes\V1\AI;
|
||||
|
||||
use Automattic\WooCommerce\StoreApi\Exceptions\RouteException;
|
||||
|
||||
/**
|
||||
* Middleware class.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
class Middleware {
|
||||
|
||||
|
||||
/**
|
||||
* Ensure that the user is allowed to make this request.
|
||||
*
|
||||
* @throws RouteException If the user is not allowed to make this request.
|
||||
* @return boolean
|
||||
*/
|
||||
public static function is_authorized() {
|
||||
try {
|
||||
if ( ! current_user_can( 'manage_options' ) ) {
|
||||
throw new RouteException( 'woocommerce_rest_invalid_user', __( 'You are not allowed to make this request. Please make sure you are logged in.', 'woocommerce' ), 403 );
|
||||
}
|
||||
} catch ( RouteException $error ) {
|
||||
return new \WP_Error(
|
||||
$error->getErrorCode(),
|
||||
$error->getMessage(),
|
||||
array( 'status' => $error->getCode() )
|
||||
);
|
||||
}
|
||||
|
||||
$allow_ai_connection = get_option( 'woocommerce_blocks_allow_ai_connection' );
|
||||
|
||||
if ( ! $allow_ai_connection ) {
|
||||
try {
|
||||
throw new RouteException( 'ai_connection_not_allowed', __( 'AI content generation is not allowed on this store. Update your store settings if you wish to enable this feature.', 'woocommerce' ), 403 );
|
||||
} catch ( RouteException $error ) {
|
||||
return new \WP_Error(
|
||||
$error->getErrorCode(),
|
||||
$error->getMessage(),
|
||||
array( 'status' => $error->getCode() )
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -1,153 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Automattic\WooCommerce\StoreApi\Routes\V1\AI;
|
||||
|
||||
use Automattic\WooCommerce\Blocks\AI\Connection;
|
||||
use Automattic\WooCommerce\Blocks\AIContent\UpdateProducts;
|
||||
use Automattic\WooCommerce\StoreApi\Routes\V1\AbstractRoute;
|
||||
|
||||
/**
|
||||
* Products class.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
class Products extends AbstractRoute {
|
||||
/**
|
||||
* The route identifier.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const IDENTIFIER = 'ai/products';
|
||||
|
||||
/**
|
||||
* The schema item identifier.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const SCHEMA_TYPE = 'ai/products';
|
||||
|
||||
/**
|
||||
* 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/products';
|
||||
}
|
||||
|
||||
/**
|
||||
* 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' => [
|
||||
'business_description' => [
|
||||
'description' => __( 'The business description for a given store.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
],
|
||||
'images' => [
|
||||
'description' => __( 'The images for a given store.', 'woocommerce' ),
|
||||
'type' => 'object',
|
||||
],
|
||||
],
|
||||
],
|
||||
[
|
||||
'methods' => \WP_REST_Server::DELETABLE,
|
||||
'callback' => [ $this, 'get_response' ],
|
||||
'permission_callback' => [ Middleware::class, 'is_authorized' ],
|
||||
],
|
||||
'schema' => [ $this->schema, 'get_public_item_schema' ],
|
||||
'allow_batch' => [ 'v1' => true ],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate the content for the products.
|
||||
*
|
||||
* @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 ) {
|
||||
$allow_ai_connection = get_option( 'woocommerce_blocks_allow_ai_connection' );
|
||||
|
||||
if ( ! $allow_ai_connection ) {
|
||||
return rest_ensure_response(
|
||||
$this->error_to_response(
|
||||
new \WP_Error(
|
||||
'ai_connection_not_allowed',
|
||||
__( 'AI content generation is not allowed on this store. Update your store settings if you wish to enable this feature.', 'woocommerce' )
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
$business_description = sanitize_text_field( wp_unslash( $request['business_description'] ) );
|
||||
|
||||
if ( empty( $business_description ) ) {
|
||||
$business_description = get_option( 'woo_ai_describe_store_description' );
|
||||
}
|
||||
|
||||
$ai_connection = new Connection();
|
||||
|
||||
$site_id = $ai_connection->get_site_id();
|
||||
|
||||
if ( is_wp_error( $site_id ) ) {
|
||||
return $this->error_to_response( $site_id );
|
||||
}
|
||||
|
||||
$token = $ai_connection->get_jwt_token( $site_id );
|
||||
|
||||
if ( is_wp_error( $token ) ) {
|
||||
return $this->error_to_response( $token );
|
||||
}
|
||||
|
||||
$images = $request['images'];
|
||||
|
||||
$populate_products = ( new UpdateProducts() )->generate_content( $ai_connection, $token, $images, $business_description );
|
||||
|
||||
if ( is_wp_error( $populate_products ) ) {
|
||||
return $this->error_to_response( $populate_products );
|
||||
}
|
||||
|
||||
if ( ! isset( $populate_products['product_content'] ) ) {
|
||||
return $this->error_to_response( new \WP_Error( 'product_content_not_found', __( 'Product content not found.', 'woocommerce' ) ) );
|
||||
}
|
||||
|
||||
$product_content = $populate_products['product_content'];
|
||||
|
||||
$item = array(
|
||||
'ai_content_generated' => true,
|
||||
'product_content' => $product_content,
|
||||
);
|
||||
|
||||
return rest_ensure_response( $item );
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove products generated by AI.
|
||||
*
|
||||
* @param \WP_REST_Request $request Request object.
|
||||
*
|
||||
* @return bool|string|\WP_Error|\WP_REST_Response
|
||||
*/
|
||||
protected function get_route_delete_response( \WP_REST_Request $request ) {
|
||||
( new UpdateProducts() )->reset_products_content();
|
||||
return rest_ensure_response( array( 'removed' => true ) );
|
||||
}
|
||||
}
|
|
@ -66,10 +66,8 @@ class RoutesController {
|
|||
Routes\V1\ProductsById::IDENTIFIER => Routes\V1\ProductsById::class,
|
||||
Routes\V1\ProductsBySlug::IDENTIFIER => Routes\V1\ProductsBySlug::class,
|
||||
],
|
||||
// @todo Migrate internal AI routes to WooCommerce Core codebase.
|
||||
'private' => [
|
||||
Routes\V1\AI\Products::IDENTIFIER => Routes\V1\AI\Products::class,
|
||||
Routes\V1\Patterns::IDENTIFIER => Routes\V1\Patterns::class,
|
||||
Routes\V1\Patterns::IDENTIFIER => Routes\V1\Patterns::class,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
|
|
@ -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\ProductsSchema::IDENTIFIER => Schemas\V1\AI\ProductsSchema::class,
|
||||
Schemas\V1\PatternsSchema::IDENTIFIER => Schemas\V1\PatternsSchema::class,
|
||||
],
|
||||
];
|
||||
|
|
|
@ -1,48 +0,0 @@
|
|||
<?php
|
||||
namespace Automattic\WooCommerce\StoreApi\Schemas\V1\AI;
|
||||
|
||||
use Automattic\WooCommerce\StoreApi\Schemas\V1\AbstractSchema;
|
||||
|
||||
/**
|
||||
* ProductsSchema class.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
class ProductsSchema extends AbstractSchema {
|
||||
/**
|
||||
* The schema item name.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $title = 'ai/products';
|
||||
|
||||
/**
|
||||
* The schema item identifier.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const IDENTIFIER = 'ai/products';
|
||||
|
||||
/**
|
||||
* Products schema properties.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_properties() {
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Products response.
|
||||
*
|
||||
* @param array $item Item to get response for.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_item_response( $item ) {
|
||||
return [
|
||||
'ai_content_generated' => $item['ai_content_generated'],
|
||||
'product_content' => $item['product_content'],
|
||||
];
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue