Use wp_post table instead wp_option to store patterns data generated by AI (https://github.com/woocommerce/woocommerce-blocks/pull/11659)

* Use wp_post table instead wp_option to store patterns data generated by AI

* avoid crash when there isn't any patterns_ai_data post type

* restore check

* remove unnecessary constant

* catch error

* pass boolean to return WP_Error
This commit is contained in:
Luigi Teschio 2023-11-08 12:14:37 +01:00 committed by GitHub
parent b2efba9fef
commit 4ebea101c7
3 changed files with 71 additions and 13 deletions

View File

@ -33,8 +33,9 @@ use Automattic\WooCommerce\Blocks\Patterns\ProductUpdater;
* @internal
*/
class BlockPatterns {
const SLUG_REGEX = '/^[A-z0-9\/_-]+$/';
const COMMA_SEPARATED_REGEX = '/[\s,]+/';
const SLUG_REGEX = '/^[A-z0-9\/_-]+$/';
const COMMA_SEPARATED_REGEX = '/[\s,]+/';
const PATTERNS_AI_DATA_POST_TYPE = 'patterns_ai_data';
/**
* Path to the patterns directory.
@ -92,6 +93,22 @@ class BlockPatterns {
return;
}
register_post_type(
self::PATTERNS_AI_DATA_POST_TYPE,
array(
'labels' => array(
'name' => __( 'Patterns AI Data', 'woo-gutenberg-products-block' ),
'singular_name' => __( 'Patterns AI Data', 'woo-gutenberg-products-block' ),
),
'public' => false,
'hierarchical' => false,
'rewrite' => false,
'query_var' => false,
'delete_with_user' => false,
'can_export' => true,
)
);
$default_headers = array(
'title' => 'Title',
'slug' => 'Slug',

View File

@ -10,11 +10,6 @@ use WP_Error;
*/
class PatternUpdater {
/**
* The patterns content option name.
*/
const WC_BLOCKS_PATTERNS_CONTENT = 'wc_blocks_patterns_content';
/**
* Creates the patterns content for the given vertical.
*
@ -42,13 +37,15 @@ class PatternUpdater {
return new WP_Error( 'failed_to_set_pattern_content', __( 'Failed to set the pattern content.', 'woo-gutenberg-products-block' ) );
}
if ( get_option( self::WC_BLOCKS_PATTERNS_CONTENT ) === $patterns_with_images_and_content ) {
$patterns_ai_data_post = PatternsHelper::get_patterns_ai_data_post();
if ( isset( $patterns_ai_data_post->post_content ) && json_decode( $patterns_ai_data_post->post_content ) === $patterns_with_images_and_content ) {
return true;
}
$updated_content = update_option( self::WC_BLOCKS_PATTERNS_CONTENT, $patterns_with_images_and_content );
$updated_content = PatternsHelper::upsert_patterns_ai_data_post( $patterns_with_images_and_content );
if ( ! $updated_content ) {
if ( is_wp_error( $updated_content ) ) {
return new WP_Error( 'failed_to_update_patterns_content', __( 'Failed to update patterns content.', 'woo-gutenberg-products-block' ) );
}

View File

@ -74,6 +74,48 @@ class PatternsHelper {
return $image;
}
/**
* Returns the post that has the generated data by the AI for the patterns.
*
* @return WP_Post|null
*/
public static function get_patterns_ai_data_post() {
$arg = array(
'post_type' => 'patterns_ai_data',
'posts_per_page' => 1,
'no_found_rows' => true,
'cache_results' => true,
);
$query = new \WP_Query( $arg );
$posts = $query->get_posts();
return isset( $posts[0] ) ? $posts[0] : null;
}
/**
* Upsert the patterns AI data.
*
* @param array $patterns_dictionary The patterns dictionary.
*
* @return WP_Error|null
*/
public static function upsert_patterns_ai_data_post( $patterns_dictionary ) {
$patterns_ai_data_post = self::get_patterns_ai_data_post();
if ( isset( $patterns_ai_data_post ) ) {
$patterns_ai_data_post->post_content = wp_json_encode( $patterns_dictionary );
return wp_update_post( $patterns_ai_data_post, true );
} else {
$patterns_ai_data_post = array(
'post_title' => 'Patterns AI Data',
'post_content' => wp_json_encode( $patterns_dictionary ),
'post_status' => 'publish',
'post_type' => 'patterns_ai_data',
);
return wp_insert_post( $patterns_ai_data_post, true );
}
}
/**
* Get the Patterns Dictionary.
@ -83,9 +125,11 @@ class PatternsHelper {
* @return mixed|WP_Error|null
*/
private static function get_patterns_dictionary( $pattern_slug = null ) {
$patterns_dictionary = get_option( PatternUpdater::WC_BLOCKS_PATTERNS_CONTENT );
if ( ! empty( $patterns_dictionary ) ) {
$patterns_ai_data_post = self::get_patterns_ai_data_post();
if ( isset( $patterns_ai_data_post ) ) {
$patterns_dictionary = json_decode( $patterns_ai_data_post->post_content, true );
if ( empty( $pattern_slug ) ) {
return $patterns_dictionary;
}
@ -103,7 +147,7 @@ class PatternsHelper {
return new WP_Error( 'missing_patterns_dictionary', __( 'The patterns dictionary is missing.', 'woo-gutenberg-products-block' ) );
}
$patterns_dictionary = wp_json_file_decode( $patterns_dictionary_file, array( 'associative' => true ) );
$patterns_dictionary = json_decode( $patterns_dictionary_file, true );
if ( ! empty( $pattern_slug ) ) {
foreach ( $patterns_dictionary as $pattern_dictionary ) {