CYS - Move the `ai/patterns` endpoint to woocommerce admin API (#50372)
* 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 * Add changefile(s) from automation for the following project(s): woocommerce-beta-tester, woocommerce * Fix duplicated entries * Use AI endpoint base class * Remove unnecessary change * Add strict types * Remove unnecessary return type --------- Co-authored-by: github-actions <github-actions@github.com>
This commit is contained in:
parent
8387b0a842
commit
4e76cb11be
|
@ -216,7 +216,7 @@ const resetPatternsAndProducts = () => async () => {
|
|||
|
||||
return Promise.all( [
|
||||
apiFetch( {
|
||||
path: '/wc/private/ai/patterns',
|
||||
path: '/wc-admin/ai/patterns',
|
||||
method: 'DELETE',
|
||||
} ),
|
||||
apiFetch( {
|
||||
|
@ -287,7 +287,7 @@ export const updateStorePatterns = async (
|
|||
},
|
||||
} ),
|
||||
apiFetch( {
|
||||
path: '/wc/private/ai/patterns',
|
||||
path: '/wc-admin/ai/patterns',
|
||||
method: 'POST',
|
||||
data: {
|
||||
business_description:
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
Significance: minor
|
||||
Type: dev
|
||||
|
||||
CYS - Move the "ai/patterns" endpoint to woocommerce admin API.
|
|
@ -229,7 +229,7 @@ export function* resetCustomizeYourStore() {
|
|||
} );
|
||||
|
||||
yield apiFetch( {
|
||||
path: '/wc/private/ai/patterns',
|
||||
path: '/wc-admin/ai/patterns',
|
||||
method: 'DELETE',
|
||||
} );
|
||||
} );
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
Significance: minor
|
||||
Type: dev
|
||||
|
||||
CYS - Move the "ai/patterns" endpoint to woocommerce admin API.
|
|
@ -0,0 +1,98 @@
|
|||
<?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\UpdatePatterns;
|
||||
use WP_Error;
|
||||
use WP_REST_Request;
|
||||
use WP_REST_Response;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Patterns controller
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
class Patterns extends AIEndpoint {
|
||||
/**
|
||||
* Endpoint.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $endpoint = 'patterns';
|
||||
|
||||
/**
|
||||
* Register routes.
|
||||
*/
|
||||
public function register_routes() {
|
||||
$this->register(
|
||||
array(
|
||||
array(
|
||||
'methods' => \WP_REST_Server::CREATABLE,
|
||||
'callback' => array( $this, 'update_patterns' ),
|
||||
'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_patterns' ),
|
||||
'permission_callback' => array( Middleware::class, 'is_authorized' ),
|
||||
),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update patterns with the content and images powered by AI.
|
||||
*
|
||||
* @param WP_REST_Request $request Request object.
|
||||
*
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function update_patterns( WP_REST_Request $request ) {
|
||||
$business_description = sanitize_text_field( wp_unslash( $request['business_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 );
|
||||
|
||||
$images = $request['images'];
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove patterns generated by AI.
|
||||
*
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function delete_patterns() {
|
||||
PatternsHelper::delete_patterns_ai_data_post();
|
||||
return rest_ensure_response( array( 'removed' => true ) );
|
||||
}
|
||||
}
|
|
@ -45,7 +45,6 @@ class StoreTitle extends AIEndpoint {
|
|||
*/
|
||||
protected $endpoint = 'store-title';
|
||||
|
||||
|
||||
/**
|
||||
* Register routes.
|
||||
*/
|
||||
|
|
|
@ -90,6 +90,7 @@ class Init {
|
|||
'Automattic\WooCommerce\Admin\API\AI\BusinessDescription',
|
||||
'Automattic\WooCommerce\Admin\API\AI\StoreInfo',
|
||||
'Automattic\WooCommerce\Admin\API\AI\Images',
|
||||
'Automattic\WooCommerce\Admin\API\AI\Patterns',
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,122 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Automattic\WooCommerce\StoreApi\Routes\V1\AI;
|
||||
|
||||
use Automattic\WooCommerce\Blocks\AI\Connection;
|
||||
use Automattic\WooCommerce\Blocks\AIContent\PatternsHelper;
|
||||
use Automattic\WooCommerce\Blocks\AIContent\UpdatePatterns;
|
||||
use Automattic\WooCommerce\StoreApi\Routes\V1\AbstractRoute;
|
||||
use WP_Error;
|
||||
|
||||
/**
|
||||
* Patterns class.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
class Patterns extends AbstractRoute {
|
||||
/**
|
||||
* The route identifier.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const IDENTIFIER = 'ai/patterns';
|
||||
|
||||
/**
|
||||
* The schema item identifier.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const SCHEMA_TYPE = 'ai/patterns';
|
||||
|
||||
/**
|
||||
* 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/patterns';
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 ],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Update patterns with the content and images powered by AI.
|
||||
*
|
||||
* @param \WP_REST_Request $request Request object.
|
||||
*
|
||||
* @return WP_Error|\WP_HTTP_Response|\WP_REST_Response
|
||||
*/
|
||||
protected function get_route_post_response( \WP_REST_Request $request ) {
|
||||
$business_description = sanitize_text_field( wp_unslash( $request['business_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 );
|
||||
|
||||
$images = $request['images'];
|
||||
|
||||
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 $this->error_to_response( $e );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove patterns 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 ) {
|
||||
PatternsHelper::delete_patterns_ai_data_post();
|
||||
return rest_ensure_response( array( 'removed' => true ) );
|
||||
}
|
||||
}
|
|
@ -68,7 +68,6 @@ class RoutesController {
|
|||
],
|
||||
// @todo Migrate internal AI routes to WooCommerce Core codebase.
|
||||
'private' => [
|
||||
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,
|
||||
|
|
|
@ -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\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\PatternsSchema::IDENTIFIER => Schemas\V1\PatternsSchema::class,
|
||||
|
|
|
@ -1,47 +0,0 @@
|
|||
<?php
|
||||
namespace Automattic\WooCommerce\StoreApi\Schemas\V1\AI;
|
||||
|
||||
use Automattic\WooCommerce\StoreApi\Schemas\V1\AbstractSchema;
|
||||
|
||||
/**
|
||||
* PatternsSchema class.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
class PatternsSchema extends AbstractSchema {
|
||||
/**
|
||||
* The schema item name.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $title = 'ai/patterns';
|
||||
|
||||
/**
|
||||
* The schema item identifier.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const IDENTIFIER = 'ai/patterns';
|
||||
|
||||
/**
|
||||
* Patterns schema properties.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_properties() {
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Patterns response.
|
||||
*
|
||||
* @param array $item Item to get response for.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_item_response( $item ) {
|
||||
return [
|
||||
'ai_content_generated' => true,
|
||||
];
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue