CYS - Move the `ai/business-description` endpoint to woocommerce admin API (#50359)

* 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

* 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

* Add strict types

---------

Co-authored-by: github-actions <github-actions@github.com>
This commit is contained in:
Alba Rincón 2024-08-20 09:11:20 +02:00 committed by GitHub
parent 6bac54ad82
commit 9b1d91c7d2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 90 additions and 147 deletions

View File

@ -314,7 +314,7 @@ export const updateStorePatterns = async (
await Promise.all( [ await Promise.all( [
...productContents, ...productContents,
apiFetch( { apiFetch( {
path: '/wc/private/ai/business-description', path: '/wc-admin/ai/business-description',
method: 'POST', method: 'POST',
data: { data: {
business_description: business_description:

View File

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

View File

@ -0,0 +1,84 @@
<?php
declare( strict_types = 1 );
namespace Automattic\WooCommerce\Admin\API\AI;
use WP_Error;
use WP_REST_Request;
use WP_REST_Response;
defined( 'ABSPATH' ) || exit;
/**
* Store Title controller
*
* @internal
*/
class BusinessDescription extends AIEndpoint {
/**
* Endpoint.
*
* @var string
*/
protected $endpoint = 'business-description';
/**
* Register routes.
*/
public function register_routes() {
$this->register(
array(
array(
'methods' => \WP_REST_Server::CREATABLE,
'callback' => array( $this, 'update_business_description' ),
'permission_callback' => array( Middleware::class, 'is_authorized' ),
'args' => array(
'business_description' => array(
'description' => __( 'The business description for a given store.', 'woocommerce' ),
'type' => 'string',
),
),
),
'schema' => array( $this, 'get_schema' ),
)
);
}
/**
* Update the business description.
*
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response|WP_Error Response object.
*/
public function update_business_description( $request ) {
$business_description = $request->get_param( 'business_description' );
if ( ! $business_description ) {
return new WP_Error(
'invalid_business_description',
__( 'Invalid business description.', 'woocommerce' )
);
}
update_option( 'last_business_description_with_ai_content_generated', $business_description );
return rest_ensure_response(
array(
'ai_content_generated' => true,
)
);
}
/**
* Get the Business Description response.
*
* @return array
*/
public function get_schema() {
return array(
'ai_content_generated' => true,
);
}
}

View File

@ -87,6 +87,7 @@ class Init {
'Automattic\WooCommerce\Admin\API\MobileAppMagicLink', 'Automattic\WooCommerce\Admin\API\MobileAppMagicLink',
'Automattic\WooCommerce\Admin\API\ShippingPartnerSuggestions', 'Automattic\WooCommerce\Admin\API\ShippingPartnerSuggestions',
'Automattic\WooCommerce\Admin\API\AI\StoreTitle', 'Automattic\WooCommerce\Admin\API\AI\StoreTitle',
'Automattic\WooCommerce\Admin\API\AI\BusinessDescription',
); );
} }

View File

@ -1,97 +0,0 @@
<?php
namespace Automattic\WooCommerce\StoreApi\Routes\V1\AI;
use Automattic\WooCommerce\StoreApi\Routes\V1\AbstractRoute;
/**
* BusinessDescription class.
*
* @internal
*/
class BusinessDescription extends AbstractRoute {
/**
* The route identifier.
*
* @var string
*/
const IDENTIFIER = 'ai/business-description';
/**
* The schema item identifier.
*
* @var string
*/
const SCHEMA_TYPE = 'ai/business-description';
/**
* 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/business-description';
}
/**
* 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 ],
];
}
/**
* Update the last business description.
*
* @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 = $request->get_param( 'business_description' );
if ( ! $business_description ) {
return $this->error_to_response(
new \WP_Error(
'invalid_business_description',
__( 'Invalid business description.', 'woocommerce' )
)
);
}
update_option( 'last_business_description_with_ai_content_generated', $business_description );
return rest_ensure_response(
array(
'ai_content_generated' => true,
)
);
}
}

View File

@ -72,7 +72,6 @@ class RoutesController {
Routes\V1\AI\Patterns::IDENTIFIER => Routes\V1\AI\Patterns::class, Routes\V1\AI\Patterns::IDENTIFIER => Routes\V1\AI\Patterns::class,
Routes\V1\AI\Product::IDENTIFIER => Routes\V1\AI\Product::class, Routes\V1\AI\Product::IDENTIFIER => Routes\V1\AI\Product::class,
Routes\V1\AI\Products::IDENTIFIER => Routes\V1\AI\Products::class, Routes\V1\AI\Products::IDENTIFIER => Routes\V1\AI\Products::class,
Routes\V1\AI\BusinessDescription::IDENTIFIER => Routes\V1\AI\BusinessDescription::class,
Routes\V1\AI\StoreInfo::IDENTIFIER => Routes\V1\AI\StoreInfo::class, Routes\V1\AI\StoreInfo::IDENTIFIER => Routes\V1\AI\StoreInfo::class,
Routes\V1\Patterns::IDENTIFIER => Routes\V1\Patterns::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\PatternsSchema::IDENTIFIER => Schemas\V1\AI\PatternsSchema::class,
Schemas\V1\AI\ProductSchema::IDENTIFIER => Schemas\V1\AI\ProductSchema::class, Schemas\V1\AI\ProductSchema::IDENTIFIER => Schemas\V1\AI\ProductSchema::class,
Schemas\V1\AI\ProductsSchema::IDENTIFIER => Schemas\V1\AI\ProductsSchema::class, Schemas\V1\AI\ProductsSchema::IDENTIFIER => Schemas\V1\AI\ProductsSchema::class,
Schemas\V1\AI\BusinessDescriptionSchema::IDENTIFIER => Schemas\V1\AI\BusinessDescriptionSchema::class,
Schemas\V1\AI\StoreInfoSchema::IDENTIFIER => Schemas\V1\AI\StoreInfoSchema::class, Schemas\V1\AI\StoreInfoSchema::IDENTIFIER => Schemas\V1\AI\StoreInfoSchema::class,
Schemas\V1\PatternsSchema::IDENTIFIER => Schemas\V1\PatternsSchema::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;
/**
* BusinessDescriptionSchema class.
*
* @internal
*/
class BusinessDescriptionSchema extends AbstractSchema {
/**
* The schema item name.
*
* @var string
*/
protected $title = 'ai/business-description';
/**
* The schema item identifier.
*
* @var string
*/
const IDENTIFIER = 'ai/business-description';
/**
* Business Description schema properties.
*
* @return array
*/
public function get_properties() {
return [];
}
/**
* Get the Business Description response.
*
* @param array $item Item to get response for.
*
* @return array
*/
public function get_item_response( $item ) {
return [
'ai_content_generated' => true,
];
}
}