CYS > Update AI-generated site title, image optimizations and updated directory structure (#43086)

* Remove the last business description check to allow repetition.

* Create the new AIContent directory and update the structure and name of the classes.

* Ensure the store title is updated with new AI-generated content if the previous title was also AI-generated.

* Return if the images result in error.

* Update the name of the option for fetching the AI generated site title.

* Introduce the new adjust_image_size method for shared usage between products and patterns.

* Increase the quality of images used in patterns

* Introduce the ContentImageProcessor class.

* Undo PatternsHelper rename

* Add changefile(s) from automation for the following project(s): woocommerce

* Address lint errors

* Ask AI to summarize the business description if the number of characters provided by the user is bigger than 150 characters.

* Rename the ContentImageProcessor class to ContentProcessor and introduce the new summarize_business_description method.

---------

Co-authored-by: github-actions <github-actions@github.com>
This commit is contained in:
Patricia Hillebrandt 2023-12-27 08:33:37 -03:00 committed by GitHub
parent 37155abb0c
commit 589617a41d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
38 changed files with 175 additions and 146 deletions

View File

@ -0,0 +1,4 @@
Significance: minor
Type: update
Ensure the site title can be updated by AI if the owner doesn't change it. Update the size of the images assigned to products and patterns for optimal performance. Allow usage of the same business description on multiple attempts of AI content generation.

View File

@ -5,7 +5,7 @@
* Categories: WooCommerce
*/
use Automattic\WooCommerce\Blocks\Patterns\PatternsHelper;
use Automattic\WooCommerce\Blocks\AIContent\PatternsHelper;
$banner_title = $content['titles'][0]['default'] ?? '';
$banner_button = $content['buttons'][0]['default'] ?? '';

View File

@ -5,7 +5,7 @@
* Categories: WooCommerce
*/
use Automattic\WooCommerce\Blocks\Patterns\PatternsHelper;
use Automattic\WooCommerce\Blocks\AIContent\PatternsHelper;
$description = $content['descriptions'][0]['default'] ?? '';
?>

View File

@ -5,7 +5,7 @@
* Categories: WooCommerce
*/
use Automattic\WooCommerce\Blocks\Patterns\PatternsHelper;
use Automattic\WooCommerce\Blocks\AIContent\PatternsHelper;
$image1 = PatternsHelper::get_image_url( $images, 0, 'assets/images/pattern-placeholders/table-wood-house-chair-floor-window.jpg' );

View File

@ -5,7 +5,7 @@
* Categories: WooCommerce
*/
use Automattic\WooCommerce\Blocks\Patterns\PatternsHelper;
use Automattic\WooCommerce\Blocks\AIContent\PatternsHelper;
$category_title = $content['titles'][0]['default'] ?? '';

View File

@ -5,7 +5,7 @@
* Categories: WooCommerce
*/
use Automattic\WooCommerce\Blocks\Patterns\PatternsHelper;
use Automattic\WooCommerce\Blocks\AIContent\PatternsHelper;
$image1 = PatternsHelper::get_image_url( $images, 0, 'assets/images/pattern-placeholders/tree-branch-plant-wood-leaf-flower.jpg' );
$image2 = PatternsHelper::get_image_url( $images, 1, 'assets/images/pattern-placeholders/watch-hand-brand-jewellery-strap-platinum.jpg' );

View File

@ -5,7 +5,7 @@
* Categories: WooCommerce
*/
use Automattic\WooCommerce\Blocks\Patterns\PatternsHelper;
use Automattic\WooCommerce\Blocks\AIContent\PatternsHelper;
$image1 = PatternsHelper::get_image_url( $images, 0, 'assets/images/pattern-placeholders/sweet-organic-lemons.png' );
$image2 = PatternsHelper::get_image_url( $images, 1, 'assets/images/pattern-placeholders/fresh-organic-tomatoes.png' );

View File

@ -5,7 +5,7 @@
* Categories: WooCommerce
*/
use Automattic\WooCommerce\Blocks\Patterns\PatternsHelper;
use Automattic\WooCommerce\Blocks\AIContent\PatternsHelper;
$main_title = $content['titles'][3]['default'] ?? '';
$first_title = $content['titles'][0]['default'] ?? '';

View File

@ -5,7 +5,7 @@
* Categories: WooCommerce
*/
use Automattic\WooCommerce\Blocks\Patterns\PatternsHelper;
use Automattic\WooCommerce\Blocks\AIContent\PatternsHelper;
$image1 = PatternsHelper::get_image_url( $images, 0, 'assets/images/pattern-placeholders/plant-white-leaf-flower-vase-green.jpg' );
$image2 = PatternsHelper::get_image_url( $images, 1, 'assets/images/pattern-placeholders/table-wood-house-chair-floor-window.jpg' );

View File

@ -5,7 +5,7 @@
* Categories: WooCommerce
*/
use Automattic\WooCommerce\Blocks\Patterns\PatternsHelper;
use Automattic\WooCommerce\Blocks\AIContent\PatternsHelper;
$hero_title = $content['titles'][0]['default'] ?? '';
?>

View File

@ -5,7 +5,7 @@
* Categories: WooCommerce
*/
use Automattic\WooCommerce\Blocks\Patterns\PatternsHelper;
use Automattic\WooCommerce\Blocks\AIContent\PatternsHelper;
$pattern_title = $content['titles'][0]['default'] ?? '';
$pattern_description = $content['descriptions'][0]['default'] ?? '';

View File

@ -4,7 +4,6 @@
* Slug: woocommerce-blocks/product-collection-4-columns
* Categories: WooCommerce
*/
use Automattic\WooCommerce\Blocks\Patterns\PatternsHelper;
$products_title = $content['titles'][0]['default'] ?? '';
?>

View File

@ -4,7 +4,6 @@
* Slug: woocommerce-blocks/product-collection-5-columns
* Categories: WooCommerce
*/
use Automattic\WooCommerce\Blocks\Patterns\PatternsHelper;
$products_title = $content['titles'][0]['default'] ?? '';
?>

View File

@ -5,7 +5,7 @@
* Categories: WooCommerce
*/
use Automattic\WooCommerce\Blocks\Patterns\PatternsHelper;
use Automattic\WooCommerce\Blocks\AIContent\PatternsHelper;
$image = PatternsHelper::get_image_url( $images, 0, 'assets/images/pattern-placeholders/beach-landscape-sea-coast-nature-person.jpg' );

View File

@ -5,8 +5,6 @@
* Categories: WooCommerce
*/
use Automattic\WooCommerce\Blocks\Patterns\PatternsHelper;
$collection_title = $content['titles'][0]['default'] ?? '';
?>

View File

@ -6,8 +6,6 @@
* Block Types: core/query/woocommerce/product-query
*/
use Automattic\WooCommerce\Blocks\Patterns\PatternsHelper;
$collection_title = $content['titles'][0]['default'] ?? '';
?>

View File

@ -5,7 +5,7 @@
* Categories: WooCommerce
*/
use Automattic\WooCommerce\Blocks\Patterns\PatternsHelper;
use Automattic\WooCommerce\Blocks\AIContent\PatternsHelper;
$image1 = PatternsHelper::get_image_url( $images, 0, 'assets/images/pattern-placeholders/technology-white-camera-photography-vintage-photographer.png' );
$image2 = PatternsHelper::get_image_url( $images, 1, 'assets/images/pattern-placeholders/leather-guitar-typewriter-red-gadget-sofa.png' );

View File

@ -6,8 +6,6 @@
* Block Types: core/query/woocommerce/product-query
*/
use Automattic\WooCommerce\Blocks\Patterns\PatternsHelper;
$first_title = $content['titles'][0]['default'] ?? '';
$first_button = $content['buttons'][0]['default'] ?? '';
?>

View File

@ -4,7 +4,6 @@
* Slug: woocommerce-blocks/featured-products-2-cols
* Categories: WooCommerce
*/
use Automattic\WooCommerce\Blocks\Patterns\PatternsHelper;
$first_title = $content['titles'][0]['default'] ?? '';
$first_description = $content['descriptions'][0]['default'] ?? '';

View File

@ -5,7 +5,7 @@
* Categories: WooCommerce
*/
use Automattic\WooCommerce\Blocks\Patterns\PatternsHelper;
use Automattic\WooCommerce\Blocks\AIContent\PatternsHelper;
$image1 = PatternsHelper::get_image_url( $images, 0, 'assets/images/pattern-placeholders/man-person-winter-photography-guy-statue.jpg' );
$image2 = PatternsHelper::get_image_url( $images, 1, 'assets/images/pattern-placeholders/pattern-fashion-clothing-outerwear-wool-scarf.png' );

View File

@ -5,8 +5,6 @@
* Categories: WooCommerce
*/
use Automattic\WooCommerce\Blocks\Patterns\PatternsHelper;
$first_title = $content['titles'][0]['default'] ?? '';
$second_title = $content['titles'][1]['default'] ?? '';
$third_title = $content['titles'][2]['default'] ?? '';

View File

@ -5,7 +5,7 @@
* Categories: WooCommerce
*/
use Automattic\WooCommerce\Blocks\Patterns\PatternsHelper;
use Automattic\WooCommerce\Blocks\AIContent\PatternsHelper;
$banner_title = $content['titles'][0]['default'] ?? '';
?>

View File

@ -5,7 +5,7 @@
* Categories: WooCommerce
*/
use Automattic\WooCommerce\Blocks\Patterns\PatternsHelper;
use Automattic\WooCommerce\Blocks\AIContent\PatternsHelper;
$image1 = PatternsHelper::get_image_url( $images, 0, 'assets/images/pattern-placeholders/drinkware-liquid-tableware-dishware-bottle-fluid.jpg' );
$image2 = PatternsHelper::get_image_url( $images, 1, 'assets/images/pattern-placeholders/watch-hand-brand-jewellery-strap-platinum.jpg' );

View File

@ -5,7 +5,7 @@
* Categories: WooCommerce
*/
use Automattic\WooCommerce\Blocks\Patterns\PatternsHelper;
use Automattic\WooCommerce\Blocks\AIContent\PatternsHelper;
$image1 = PatternsHelper::get_image_url( $images, 0, 'assets/images/pattern-placeholders/tea-leaf-meal-food-herb-produce.jpg' );
$image2 = PatternsHelper::get_image_url( $images, 1, 'assets/images/pattern-placeholders/drinkware-liquid-tableware-dishware-bottle-fluid.jpg' );

View File

@ -5,8 +5,6 @@
* Categories: WooCommerce
*/
use Automattic\WooCommerce\Blocks\Patterns\PatternsHelper;
$main_header = $content['titles'][3]['default'] ?? '';
$first_review = $content['titles'][0]['default'] ?? '';
$second_review = $content['titles'][1]['default'] ?? '';

View File

@ -5,7 +5,7 @@
* Categories: WooCommerce
*/
use Automattic\WooCommerce\Blocks\Patterns\PatternsHelper;
use Automattic\WooCommerce\Blocks\AIContent\PatternsHelper;
$testimonials_title = $content['titles'][0]['default'] ?? '';
$description = $content['descriptions'][0]['default'] ?? '';

View File

@ -0,0 +1,95 @@
<?php
namespace Automattic\WooCommerce\Blocks\AIContent;
use Automattic\WooCommerce\Blocks\AI\Connection;
use Automattic\WooCommerce\Blocks\Images\Pexels;
use WP_Error;
/**
* ContentProcessor class.
*
* Process images for content
*/
class ContentProcessor {
/**
* Summarize the business description to ensure better results are returned by AI.
*
* @param string $business_description The business description.
* @param Connection $ai_connection The AI connection.
* @param string $token The JWT token.
* @param integer $character_limit The character limit for the business description.
*
* @return mixed|WP_Error
*/
public static function summarize_business_description( $business_description, $ai_connection, $token, $character_limit = 150 ) {
if ( empty( $business_description ) ) {
return new WP_Error( 'business_description_not_found', __( 'No business description provided for generating AI content.', 'woocommerce' ) );
}
if ( strlen( $business_description ) > $character_limit ) {
$prompt = sprintf( 'You are a professional writer. Read the following business description and write a text with less than %s characters to summarize the products the business is selling: "%s". Make sure you do not add double quotes in your response. Do not add any explanations in the response', $character_limit, $business_description );
$response = $ai_connection->fetch_ai_response( $token, $prompt, 30 );
$business_description = $response['completion'] ?? $business_description;
}
return $business_description;
}
/**
* Ensure that images are provided for assignment to products and patterns.
*
* @param array|WP_Error $images The array of images.
* @param Connection $ai_connection The AI connection.
* @param string $token The JWT token.
* @param string $business_description The business description.
*
* @return array|int|mixed|string|WP_Error
*/
public static function verify_images( $images, $ai_connection, $token, $business_description ) {
if ( ! is_wp_error( $images ) && ! empty( $images['images'] ) && ! empty( $images['search_term'] ) ) {
return $images;
}
$images = ( new Pexels() )->get_images( $ai_connection, $token, $business_description );
if ( is_wp_error( $images ) ) {
return $images;
}
if ( empty( $images['images'] ) || empty( $images['search_term'] ) ) {
return new WP_Error( 'images_not_found', __( 'No images provided for generating AI content.', 'woocommerce' ) );
}
return $images;
}
/**
* Adjust the size of images for optimal performance on products and patterns.
*
* @param string $image_url The image URL.
* @param string $usage_type The usage type of the image. Either 'products' or 'patterns'.
*
* @return string
*/
public static function adjust_image_size( $image_url, $usage_type ) {
$parsed_url = wp_parse_url( $image_url );
if ( ! isset( $parsed_url['query'] ) ) {
return $image_url;
}
$width = 'products' === $usage_type ? 250 : 500;
parse_str( $parsed_url['query'], $query_params );
unset( $query_params['h'], $query_params['w'] );
$query_params['w'] = $width;
$url = $parsed_url['scheme'] . '://' . $parsed_url['host'] . $parsed_url['path'];
return add_query_arg( $query_params, $url );
}
}

View File

@ -1,15 +1,15 @@
<?php
namespace Automattic\WooCommerce\Blocks\Patterns;
namespace Automattic\WooCommerce\Blocks\AIContent;
use WP_Error;
/**
* Pattern Images Helper class.
* Patterns Helper class.
*/
class PatternsHelper {
/**
* Returns the image for the given pattern.
* Fetches the AI-selected image for the pattern or returns the default image.
*
* @param array $images The array of images.
* @param int $index The index of the image to return.
@ -17,7 +17,7 @@ class PatternsHelper {
*
* @return string The image.
*/
public static function get_image_url( array $images, int $index, string $default_image ): string {
public static function get_image_url( $images, $index, $default_image ) {
$image = filter_var( $default_image, FILTER_VALIDATE_URL )
? $default_image
: plugins_url( $default_image, dirname( __DIR__, 2 ) );

View File

@ -1,6 +1,6 @@
<?php
namespace Automattic\WooCommerce\Blocks\Patterns;
namespace Automattic\WooCommerce\Blocks\AIContent;
use Automattic\WooCommerce\Blocks\AI\Connection;
use WP_Error;
@ -8,7 +8,7 @@ use WP_Error;
/**
* Pattern Images class.
*/
class PatternUpdater {
class UpdatePatterns {
/**
* All patterns that are actively in use in the Assembler.
@ -36,40 +36,12 @@ class PatternUpdater {
* @return bool|WP_Error
*/
public function generate_content( $ai_connection, $token, $images, $business_description ) {
if ( is_wp_error( $images ) ) {
return $images;
}
if ( is_wp_error( $token ) ) {
return $token;
}
if ( ! isset( $images['images'] ) ) {
return new \WP_Error( 'images_not_found', __( 'No images provided for generating AI content.', 'woocommerce' ) );
}
$last_business_description = get_option( 'last_business_description_with_ai_content_generated' );
if ( $last_business_description === $business_description ) {
if ( is_string( $business_description ) && is_string( $last_business_description ) ) {
return true;
} else {
return new \WP_Error( 'business_description_not_found', __( 'No business description provided for generating AI content.', 'woocommerce' ) );
}
}
if ( 0 === count( $images['images'] ) ) {
$images = get_transient( 'woocommerce_ai_managed_images' );
}
if ( empty( $images['images'] ) ) {
return new WP_Error( 'no_images_found', __( 'No images found.', 'woocommerce' ) );
}
// This is required in case something interrupts the execution of the script and the endpoint is called again on retry.
set_transient( 'woocommerce_ai_managed_images', $images, 60 );
$patterns_dictionary = self::get_patterns_dictionary();
$images = ContentProcessor::verify_images( $images, $ai_connection, $token, $business_description );
$patterns_dictionary = PatternsHelper::get_patterns_dictionary();
if ( is_wp_error( $patterns_dictionary ) ) {
return $patterns_dictionary;
@ -469,7 +441,9 @@ class PatternUpdater {
continue;
}
$images[] = $selected_image['URL'];
$selected_image_url = ContentProcessor::adjust_image_size( $selected_image['URL'], 'patterns' );
$images[] = $selected_image_url;
$alts[] = $selected_image['title'];
}
@ -477,7 +451,7 @@ class PatternUpdater {
}
/**
* Returns the selected image format. Defaults to landscape.
* Returns the selected image format. Defaults to portrait.
*
* @param array $selected_image The selected image to be assigned to the pattern.
*

View File

@ -1,13 +1,13 @@
<?php
namespace Automattic\WooCommerce\Blocks\Patterns;
namespace Automattic\WooCommerce\Blocks\AIContent;
use Automattic\WooCommerce\Blocks\AI\Connection;
use WP_Error;
/**
* Pattern Images class.
*/
class ProductUpdater {
class UpdateProducts {
/**
* The dummy products.
@ -62,41 +62,20 @@ class ProductUpdater {
* @return array|WP_Error The generated content for the products. An error if the content could not be generated.
*/
public function generate_content( $ai_connection, $token, $images, $business_description ) {
if ( is_wp_error( $images ) ) {
return $images;
}
if ( is_wp_error( $token ) ) {
return $token;
}
if ( ! isset( $images['images'] ) || ! isset( $images['search_term'] ) ) {
$images = get_transient( 'woocommerce_ai_managed_images' );
}
$images = ContentProcessor::verify_images( $images, $ai_connection, $token, $business_description );
if ( ! isset( $images['images'] ) || ! isset( $images['search_term'] ) ) {
return new \WP_Error( 'images_not_found', __( 'No images provided for generating AI content.', 'woocommerce' ) );
if ( is_wp_error( $images ) ) {
return $images;
}
// This is required in case something interrupts the execution of the script and the endpoint is called again on retry.
set_transient( 'woocommerce_ai_managed_images', $images, 60 );
if ( empty( $business_description ) ) {
return new \WP_Error( 'missing_business_description', __( 'No business description provided for generating AI content.', 'woocommerce' ) );
}
$last_business_description = get_option( 'last_business_description_with_ai_content_generated' );
if ( $last_business_description === $business_description ) {
if ( is_string( $business_description ) && is_string( $last_business_description ) ) {
return array(
'product_content' => array(),
);
} else {
return new \WP_Error( 'business_description_not_found', __( 'No business description provided for generating AI content.', 'woocommerce' ) );
}
}
$dummy_products_to_update = $this->fetch_dummy_products_to_update();
if ( is_wp_error( $dummy_products_to_update ) ) {
@ -351,30 +330,6 @@ class ProductUpdater {
return $product_image_id;
}
/**
* Reduce the size of the image for the product to improve performance and
* avoid memory exhaustion errors when uploading them to the media library.
*
* @param string $image_url The image URL.
*
* @return string
*/
private function adjust_image_size_for_products( $image_url ) {
$parsed_url = wp_parse_url( $image_url );
if ( ! isset( $parsed_url['query'] ) ) {
return $image_url;
}
parse_str( $parsed_url['query'], $query_params );
unset( $query_params['h'], $query_params['w'] );
$query_params['w'] = 300;
$new_query_string = http_build_query( $query_params );
return $parsed_url['scheme'] . '://' . $parsed_url['host'] . $parsed_url['path'] . '?' . $new_query_string;
}
/**
* Assigns the default content for the products.
*
@ -390,7 +345,7 @@ class ProductUpdater {
$image_src = $ai_selected_images[ $i ]['URL'] ?? '';
if ( wc_is_valid_url( $image_src ) ) {
$image_src = $this->adjust_image_size_for_products( $ai_selected_images[ $i ]['URL'] );
$image_src = ContentProcessor::adjust_image_size( $image_src, 'products' );
}
$image_alt = $ai_selected_images[ $i ]['title'] ?? '';
@ -400,8 +355,8 @@ class ProductUpdater {
'description' => 'A product description',
'price' => 'The product price',
'image' => [
'src' => esc_url( $image_src ),
'alt' => esc_attr( $image_alt ),
'src' => $image_src,
'alt' => $image_alt,
],
'product_id' => $dummy_products_to_update[ $i ]->get_id(),
];
@ -422,8 +377,10 @@ class ProductUpdater {
* @return array|int|string|\WP_Error
*/
public function assign_ai_generated_content_to_dummy_products( $ai_connection, $token, $products_information_list, $business_description, $search_term ) {
if ( empty( $business_description ) ) {
return new \WP_Error( 'missing_store_description', __( 'The store description is required to generate content for your site.', 'woocommerce' ) );
$business_description = ContentProcessor::summarize_business_description( $business_description, $ai_connection, $token, 100 );
if ( is_wp_error( $business_description ) ) {
return $business_description;
}
$prompts = [];

View File

@ -4,9 +4,9 @@ namespace Automattic\WooCommerce\Blocks;
use Automattic\WooCommerce\Blocks\AI\Connection;
use Automattic\WooCommerce\Blocks\Images\Pexels;
use Automattic\WooCommerce\Blocks\Domain\Package;
use Automattic\WooCommerce\Blocks\Patterns\PatternsHelper;
use Automattic\WooCommerce\Blocks\Patterns\PatternUpdater;
use Automattic\WooCommerce\Blocks\Patterns\ProductUpdater;
use Automattic\WooCommerce\Blocks\AIContent\PatternsHelper;
use Automattic\WooCommerce\Blocks\AIContent\UpdatePatterns;
use Automattic\WooCommerce\Blocks\AIContent\UpdateProducts;
/**
* Registers patterns under the `./patterns/` directory and updates their content.
@ -366,13 +366,13 @@ class BlockPatterns {
return $images->get_error_message();
}
$populate_patterns = ( new PatternUpdater() )->generate_content( $ai_connection, $token, $images, $business_description );
$populate_patterns = ( new UpdatePatterns() )->generate_content( $ai_connection, $token, $images, $business_description );
if ( is_wp_error( $populate_patterns ) ) {
return $populate_patterns->get_error_message();
}
$populate_products = ( new ProductUpdater() )->generate_content( $ai_connection, $token, $images, $business_description );
$populate_products = ( new UpdateProducts() )->generate_content( $ai_connection, $token, $images, $business_description );
if ( is_wp_error( $populate_products ) ) {
return $populate_products->get_error_message();

View File

@ -3,7 +3,8 @@
namespace Automattic\WooCommerce\Blocks\Images;
use Automattic\WooCommerce\Blocks\AI\Connection;
use Automattic\WooCommerce\Blocks\Patterns\PatternUpdater;
use Automattic\WooCommerce\Blocks\AIContent\ContentProcessor;
use Automattic\WooCommerce\Blocks\AIContent\UpdatePatterns;
/**
* Pexels API client.
@ -27,6 +28,8 @@ class Pexels {
* @return array|\WP_Error Array of images, or WP_Error if the request failed.
*/
public function get_images( $ai_connection, $token, $business_description ) {
$business_description = ContentProcessor::summarize_business_description( $business_description, $ai_connection, $token );
if ( str_word_count( $business_description ) === 1 ) {
$search_term = $business_description;
} else {
@ -111,6 +114,7 @@ class Pexels {
* @return mixed|\WP_Error
*/
private function define_search_term( $ai_connection, $token, $business_description ) {
$prompt = sprintf( 'You are a teacher. Based on the following business description, \'%s\', describe to a child exactly what this store is selling in one or two words and be as precise as you can possibly be. Do not reply with generic words that could cause confusion and be associated with other businesses as a response. Make sure you do not add double quotes in your response. Do not add any explanations in the response', $business_description );
$response = $ai_connection->fetch_ai_response( $token, $prompt, 30 );
@ -119,6 +123,10 @@ class Pexels {
return $response;
}
if ( isset( $response['code'] ) && 'completion_error' === $response['code'] ) {
return new \WP_Error( 'search_term_definition_failed', __( 'The search term definition failed.', 'woocommerce' ) );
}
if ( ! isset( $response['completion'] ) ) {
return new \WP_Error( 'search_term_definition_failed', __( 'The search term definition failed.', 'woocommerce' ) );
}
@ -222,7 +230,7 @@ class Pexels {
* @return array|\WP_Error The total number of required images, or WP_Error if the request failed.
*/
private function total_number_required_images() {
$patterns_dictionary = PatternUpdater::get_patterns_dictionary();
$patterns_dictionary = UpdatePatterns::get_patterns_dictionary();
if ( is_wp_error( $patterns_dictionary ) ) {
return $patterns_dictionary;

View File

@ -3,9 +3,10 @@
namespace Automattic\WooCommerce\StoreApi\Routes\V1\AI;
use Automattic\WooCommerce\Blocks\AI\Connection;
use Automattic\WooCommerce\Blocks\Patterns\PatternsHelper;
use Automattic\WooCommerce\Blocks\Patterns\PatternUpdater;
use Automattic\WooCommerce\Blocks\AIContent\PatternsHelper;
use Automattic\WooCommerce\Blocks\AIContent\UpdatePatterns;
use Automattic\WooCommerce\StoreApi\Routes\V1\AbstractRoute;
use WP_Error;
/**
* Patterns class.
@ -73,7 +74,7 @@ class Patterns extends AbstractRoute {
*
* @param \WP_REST_Request $request Request object.
*
* @return bool|string|\WP_Error|\WP_REST_Response
* @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'] ) );
@ -91,9 +92,9 @@ class Patterns extends AbstractRoute {
$images = $request['images'];
try {
( new PatternUpdater() )->generate_content( $ai_connection, $token, $images, $business_description );
( new UpdatePatterns() )->generate_content( $ai_connection, $token, $images, $business_description );
return rest_ensure_response( array( 'ai_content_generated' => true ) );
} catch ( \WP_Error $e ) {
} catch ( WP_Error $e ) {
return $this->error_to_response( $e );
}
}
@ -103,7 +104,7 @@ class Patterns extends AbstractRoute {
*
* @param \WP_REST_Request $request Request object.
*
* @return bool|string|\WP_Error|\WP_REST_Response
* @return bool|string|WP_Error|\WP_REST_Response
*/
protected function get_route_delete_response( \WP_REST_Request $request ) {
PatternsHelper::delete_patterns_ai_data_post();

View File

@ -2,7 +2,7 @@
namespace Automattic\WooCommerce\StoreApi\Routes\V1\AI;
use Automattic\WooCommerce\Blocks\Patterns\ProductUpdater;
use Automattic\WooCommerce\Blocks\AIContent\UpdateProducts;
use Automattic\WooCommerce\StoreApi\Routes\V1\AbstractRoute;
/**
@ -69,7 +69,7 @@ class Product extends AbstractRoute {
* @return bool|string|\WP_Error|\WP_REST_Response
*/
protected function get_route_post_response( \WP_REST_Request $request ) {
$product_updater = new ProductUpdater();
$product_updater = new UpdateProducts();
$product_information = $request['products_information'] ?? array();
if ( empty( $product_information ) ) {

View File

@ -3,7 +3,7 @@
namespace Automattic\WooCommerce\StoreApi\Routes\V1\AI;
use Automattic\WooCommerce\Blocks\AI\Connection;
use Automattic\WooCommerce\Blocks\Patterns\ProductUpdater;
use Automattic\WooCommerce\Blocks\AIContent\UpdateProducts;
use Automattic\WooCommerce\StoreApi\Routes\V1\AbstractRoute;
/**
@ -110,7 +110,7 @@ class Products extends AbstractRoute {
$images = $request['images'];
$populate_products = ( new ProductUpdater() )->generate_content( $ai_connection, $token, $images, $business_description );
$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 );
@ -138,7 +138,7 @@ class Products extends AbstractRoute {
* @return bool|string|\WP_Error|\WP_REST_Response
*/
protected function get_route_delete_response( \WP_REST_Request $request ) {
( new ProductUpdater() )->reset_products_content();
( new UpdateProducts() )->reset_products_content();
return rest_ensure_response( array( 'removed' => true ) );
}
}

View File

@ -2,8 +2,8 @@
namespace Automattic\WooCommerce\StoreApi\Routes\V1\AI;
use Automattic\WooCommerce\Blocks\Patterns\PatternsHelper;
use Automattic\WooCommerce\Blocks\Patterns\ProductUpdater;
use Automattic\WooCommerce\Blocks\AIContent\PatternsHelper;
use Automattic\WooCommerce\Blocks\AIContent\UpdateProducts;
use Automattic\WooCommerce\StoreApi\Routes\V1\AbstractRoute;
/**
@ -60,7 +60,7 @@ class StoreInfo extends AbstractRoute {
* @return bool|string|\WP_Error|\WP_REST_Response
*/
protected function get_route_response( \WP_REST_Request $request ) {
$product_updater = new ProductUpdater();
$product_updater = new UpdateProducts();
$patterns = PatternsHelper::get_patterns_ai_data_post();
$products = $product_updater->fetch_product_ids( 'dummy' );

View File

@ -91,8 +91,10 @@ class StoreTitle extends AbstractRoute {
);
}
$store_title = get_option( 'blogname' );
if ( ! ( empty( $store_title ) || self::DEFAULT_TITLE === $store_title ) ) {
$store_title = get_option( 'blogname' );
$previous_ai_generated_title = get_option( 'ai_generated_site_title' );
if ( self::DEFAULT_TITLE === $store_title || ( ! empty( $store_title ) && $previous_ai_generated_title !== $store_title ) ) {
return rest_ensure_response( array( 'ai_content_generated' => false ) );
}
@ -101,6 +103,7 @@ class StoreTitle extends AbstractRoute {
return $this->error_to_response( $ai_generated_title );
}
update_option( 'ai_generated_site_title', $ai_generated_title );
update_option( self::STORE_TITLE_OPTION_NAME, $ai_generated_title );
return rest_ensure_response(