This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 221
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix pattern route performance (#11535)
* fix pattern route performance * update namespace * improve middleware * improve ProductSchema * improve error handling * update identifier * fix middleware * update description * use schema to return the response * Break down the generate_content method and create the new fetch_dummy_products_to_update method for handling the fetch of dummy products to be updated. * Ensure the Product endpoint relies on the fetch_dummy_products_to_update method for fetching dummy products to avoid code repetition and add safety checks and handle errors in case certain properties are not available. * Add error handling for the Products endpoint. * Remove memory limit increase and update docblocks. * re-add set_time_limit --------- Co-authored-by: Patricia Hillebrandt <[email protected]>
- Loading branch information
Showing
14 changed files
with
746 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
namespace Automattic\WooCommerce\StoreApi\Routes\V1\AI; | ||
|
||
use Automattic\WooCommerce\Blocks\Patterns\ProductUpdater; | ||
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 '/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.', 'woo-gutenberg-products-block' ), | ||
'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.', 'woo-gutenberg-products-block' ) | ||
) | ||
); | ||
} | ||
|
||
update_option( 'last_business_description_with_ai_content_generated', $business_description ); | ||
|
||
return rest_ensure_response( | ||
array( | ||
'ai_content_generated' => true, | ||
) | ||
); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
<?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 '/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.', 'woo-gutenberg-products-block' ), | ||
'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 ) ) { | ||
return $this->error_to_response( $images ); | ||
} | ||
|
||
return rest_ensure_response( | ||
$this->prepare_item_for_response( | ||
[ | ||
'ai_content_generated' => true, | ||
'images' => $images, | ||
], | ||
$request | ||
) | ||
); | ||
} | ||
} |
Oops, something went wrong.