CYS - Move the `ai/images` endpoint to woocommerce admin API (#50365)

* 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

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

* 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

* Fix lint and use ai endpoint base

* Fix lint

---------

Co-authored-by: github-actions <github-actions@github.com>
This commit is contained in:
Alba Rincón 2024-08-20 10:34:27 +02:00 committed by GitHub
parent c06190d529
commit 071af63f32
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 117 additions and 179 deletions

View File

@ -239,7 +239,7 @@ export const updateStorePatterns = async (
ai_content_generated: boolean;
images: { images: Array< unknown >; search_term: string };
} >( {
path: '/wc/private/ai/images',
path: '/wc-admin/ai/images',
method: 'POST',
data: {
business_description:

View File

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

View File

@ -51,5 +51,7 @@ abstract class AIEndpoint {
*
* @return array
*/
abstract public function get_schema();
public function get_schema() {
return array();
}
}

View File

@ -0,0 +1,108 @@
<?php
declare( strict_types = 1 );
namespace Automattic\WooCommerce\Admin\API\AI;
use Automattic\WooCommerce\Blocks\AI\Connection;
use Automattic\WooCommerce\Blocks\Images\Pexels;
use WP_Error;
use WP_REST_Request;
use WP_REST_Response;
defined( 'ABSPATH' ) || exit;
/**
* Images controller
*
* @internal
*/
class Images extends AIEndpoint {
/**
* Endpoint.
*
* @var string
*/
protected $endpoint = 'images';
/**
* Register routes.
*/
public function register_routes() {
$this->register(
array(
array(
'methods' => \WP_REST_Server::CREATABLE,
'callback' => array( $this, 'generate_images' ),
'permission_callback' => array( Middleware::class, 'is_authorized' ),
'args' => array(
'business_description' => array(
'description' => __( 'The business description for a given store.', 'woocommerce' ),
'type' => 'string',
),
),
),
)
);
}
/**
* Generate Images from Pexels
*
* @param WP_REST_Request $request Request object.
*
* @return WP_Error|WP_REST_Response
*/
public function generate_images( WP_REST_Request $request ) {
$business_description = sanitize_text_field( wp_unslash( $request['business_description'] ) );
if ( empty( $business_description ) ) {
$business_description = get_option( 'woo_ai_describe_store_description' );
}
$last_business_description = get_option( 'last_business_description_with_ai_content_generated' );
if ( $last_business_description === $business_description ) {
return rest_ensure_response(
$this->prepare_item_for_response(
array(
'ai_content_generated' => true,
'images' => array(),
),
$request
)
);
}
$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 = ( new Pexels() )->get_images( $ai_connection, $token, $business_description );
if ( is_wp_error( $images ) ) {
$images = array(
'images' => array(),
'search_term' => '',
);
}
return rest_ensure_response(
array(
'ai_content_generated' => true,
'images' => $images,
)
);
}
}

View File

@ -89,6 +89,7 @@ class Init {
'Automattic\WooCommerce\Admin\API\AI\StoreTitle',
'Automattic\WooCommerce\Admin\API\AI\BusinessDescription',
'Automattic\WooCommerce\Admin\API\AI\StoreInfo',
'Automattic\WooCommerce\Admin\API\AI\Images',
);
}

View File

@ -1,130 +0,0 @@
<?php
namespace Automattic\WooCommerce\StoreApi\Routes\V1\AI;
use Automattic\WooCommerce\Blocks\AI\Connection;
use Automattic\WooCommerce\Blocks\Images\Pexels;
use Automattic\WooCommerce\StoreApi\Routes\V1\AbstractRoute;
/**
* Patterns class.
*/
class Images extends AbstractRoute {
/**
* The route identifier.
*
* @var string
*/
const IDENTIFIER = 'ai/images';
/**
* The schema item identifier.
*
* @var string
*/
const SCHEMA_TYPE = 'ai/images';
/**
* 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/images';
}
/**
* 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',
],
],
],
'schema' => [ $this->schema, 'get_public_item_schema' ],
'allow_batch' => [ 'v1' => true ],
];
}
/**
* Generate Images from Pexels
*
* @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 ) {
$business_description = sanitize_text_field( wp_unslash( $request['business_description'] ) );
if ( empty( $business_description ) ) {
$business_description = get_option( 'woo_ai_describe_store_description' );
}
$last_business_description = get_option( 'last_business_description_with_ai_content_generated' );
if ( $last_business_description === $business_description ) {
return rest_ensure_response(
$this->prepare_item_for_response(
[
'ai_content_generated' => true,
'images' => array(),
],
$request
)
);
}
$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 = ( new Pexels() )->get_images( $ai_connection, $token, $business_description );
if ( is_wp_error( $images ) ) {
$images = array(
'images' => array(),
'search_term' => '',
);
}
return rest_ensure_response(
$this->prepare_item_for_response(
[
'ai_content_generated' => true,
'images' => $images,
],
$request
)
);
}
}

View File

@ -68,7 +68,6 @@ 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,

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\ImagesSchema::IDENTIFIER => Schemas\V1\AI\ImagesSchema::class,
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,

View File

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