tionary = PatternsHelper::get_patterns_dictionary(); foreach ( $files as $file ) { $pattern_data = get_file_data( $file, $default_headers ); if ( empty( $pattern_data['slug'] ) ) { _doing_it_wrong( 'register_block_patterns', esc_html( sprintf( /* translators: %s: file name. */ __( 'Could not register file "%s" as a block pattern ("Slug" field missing)', 'woocommerce' ), $file ) ), '6.0.0' ); continue; } if ( ! preg_match( self::SLUG_REGEX, $pattern_data['slug'] ) ) { _doing_it_wrong( 'register_block_patterns', esc_html( sprintf( /* translators: %1s: file name; %2s: slug value found. */ __( 'Could not register file "%1$s" as a block pattern (invalid slug "%2$s")', 'woocommerce' ), $file, $pattern_data['slug'] ) ), '6.0.0' ); continue; } if ( \WP_Block_Patterns_Registry::get_instance()->is_registered( $pattern_data['slug'] ) ) { continue; } if ( $pattern_data['featureFlag'] && ! Features::is_enabled( $pattern_data['featureFlag'] ) ) { continue; } // Title is a required property. if ( ! $pattern_data['title'] ) { _doing_it_wrong( 'register_block_patterns', esc_html( sprintf( /* translators: %1s: file name; %2s: slug value found. */ __( 'Could not register file "%s" as a block pattern ("Title" field missing)', 'woocommerce' ), $file ) ), '6.0.0' ); continue; } // For properties of type array, parse data as comma-separated. foreach ( array( 'categories', 'keywords', 'blockTypes' ) as $property ) { if ( ! empty( $pattern_data[ $property ] ) ) { $pattern_data[ $property ] = array_filter( preg_split( self::COMMA_SEPARATED_REGEX, (string) $pattern_data[ $property ] ) ); } else { unset( $pattern_data[ $property ] ); } } // Parse properties of type int. foreach ( array( 'viewportWidth' ) as $property ) { if ( ! empty( $pattern_data[ $property ] ) ) { $pattern_data[ $property ] = (int) $pattern_data[ $property ]; } else { unset( $pattern_data[ $property ] ); } } // Parse properties of type bool. foreach ( array( 'inserter' ) as $property ) { if ( ! empty( $pattern_data[ $property ] ) ) { $pattern_data[ $property ] = in_array( strtolower( $pattern_data[ $property ] ), array( 'yes', 'true' ), true ); } else { unset( $pattern_data[ $property ] ); } } // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText, WordPress.WP.I18n.LowLevelTranslationFunction $pattern_data['title'] = translate_with_gettext_context( $pattern_data['title'], 'Pattern title', 'woocommerce' ); if ( ! empty( $pattern_data['description'] ) ) { // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText, WordPress.WP.I18n.LowLevelTranslationFunction $pattern_data['description'] = translate_with_gettext_context( $pattern_data['description'], 'Pattern description', 'woocommerce' ); } $pattern_data_from_dictionary = $this->get_pattern_from_dictionary( $dictionary, $pattern_data['slug'] ); // The actual pattern content is the output of the file. ob_start(); /* For patterns that can have AI-generated content, we need to get its content from the dictionary and pass it to the pattern file through the "$content" and "$images" variables. This is to avoid having to access the dictionary for each pattern when it's registered or inserted. Before the "$content" and "$images" variables were populated in each pattern. Since the pattern registration happens in the init hook, the dictionary was being access one for each pattern and for each page load. This way we only do it once on registration. For more context: https://github.com/woocommerce/woocommerce-blocks/pull/11733 */ $content = array(); $images = array(); if ( ! is_null( $pattern_data_from_dictionary ) ) { $content = $pattern_data_from_dictionary['content']; $images = $pattern_data_from_dictionary['images'] ?? array(); } include $file; $pattern_data['content'] = ob_get_clean(); if ( ! $pattern_data['content'] ) { continue; } foreach ( $pattern_data['categories'] as $key => $category ) { $category_slug = _wp_to_kebab_case( $category ); $pattern_data['categories'][ $key ] = $category_slug; register_block_pattern_category( $category_slug, // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText array( 'label' => __( $category, 'woocommerce' ) ) ); } register_block_pattern( $pattern_data['slug'], $pattern_data ); } } /** * Update the patterns content when the store description is changed. * * @param string $option The option name. * @param string $value The option value. */ public function schedule_on_option_update( $option, $value ) { $last_business_description = get_option( 'last_business_description_with_ai_content_generated' ); if ( $last_business_description === $value ) { return; } $this->schedule_patterns_content_update( $value ); } /** * Update the patterns content when the WooCommerce Blocks plugin is updated. * * @param \WP_Upgrader $upgrader_object WP_Upgrader instance. * @param array $options Array of bulk item update data. */ public function schedule_on_plugin_update( $upgrader_object, $options ) { if ( 'update' === $options['action'] && 'plugin' === $options['type'] && isset( $options['plugins'] ) ) { foreach ( $options['plugins'] as $plugin ) { if ( str_contains( $plugin, 'woocommerce.php' ) ) { $business_description = get_option( 'woo_ai_describe_store_description' ); if ( $business_description ) { $this->schedule_patterns_content_update( $business_description ); } } } } } /** * Update the patterns content when the store description is changed. * * @param string $business_description The business description. */ public function schedule_patterns_content_update( $business_description ) { if ( ! class_exists( 'WooCommerce' ) ) { return; } $action_scheduler = WP_PLUGIN_DIR . '/woocommerce/packages/action-scheduler/action-scheduler.php'; if ( ! file_exists( $action_scheduler ) ) { return; } require_once $action_scheduler; as_schedule_single_action( time(), 'woocommerce_update_patterns_content', array( $business_description ) ); } /** * Update the patterns content. * * @param string $value The new value saved for the add_option_woo_ai_describe_store_description option. * * @return bool|string|\WP_Error */ public function update_patterns_content( $value ) { $allow_ai_connection = get_option( 'woocommerce_blocks_allow_ai_connection' ); if ( ! $allow_ai_connection ) { return new \WP_Error( 'ai_connection_not_allowed', __( 'AI content generation is not allowed on this store. Update your store settings if you wish to enable this feature.', 'woocommerce' ) ); } $ai_connection = new Connection(); $site_id = $ai_connection->get_site_id(); if ( is_wp_error( $site_id ) ) { return $site_id->get_error_message(); } $token = $ai_connection->get_jwt_token( $site_id ); if ( is_wp_error( $token ) ) { return $token->get_error_message(); } $business_description = get_option( 'woo_ai_describe_store_description' ); $images = ( new Pexels() )->get_images( $ai_connection, $token, $business_description ); if ( is_wp_error( $images ) ) { return $images->get_error_message(); } $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 UpdateProducts() )->generate_content( $ai_connection, $token, $images, $business_description ); if ( is_wp_error( $populate_products ) ) { return $populate_products->get_error_message(); } return true; } /** * Filter the patterns dictionary to get the pattern data corresponding to the pattern slug. * * @param array $dictionary The patterns dictionary. * @param string $slug The pattern slug. * * @return array|null */ private function get_pattern_from_dictionary( $dictionary, $slug ) { foreach ( $dictionary as $pattern_dictionary ) { if ( isset( $pattern_dictionary['slug'] ) && $pattern_dictionary['slug'] === $slug ) { return $pattern_dictionary; } } return null; } }
Fatal error: Uncaught Error: Class "Automattic\WooCommerce\Blocks\BlockPatterns" not found in /htdocs/wp-content/plugins/woocommerce/src/Blocks/Domain/Bootstrap.php:379 Stack trace: #0 /htdocs/wp-content/plugins/woocommerce/src/Blocks/Registry/AbstractDependencyType.php(42): Automattic\WooCommerce\Blocks\Domain\Bootstrap->Automattic\WooCommerce\Blocks\Domain\{closure}(Object(Automattic\WooCommerce\Blocks\Registry\Container)) #1 /htdocs/wp-content/plugins/woocommerce/src/Blocks/Registry/SharedType.php(28): Automattic\WooCommerce\Blocks\Registry\AbstractDependencyType->resolve_value(Object(Automattic\WooCommerce\Blocks\Registry\Container)) #2 /htdocs/wp-content/plugins/woocommerce/src/Blocks/Registry/Container.php(96): Automattic\WooCommerce\Blocks\Registry\SharedType->get(Object(Automattic\WooCommerce\Blocks\Registry\Container)) #3 /htdocs/wp-content/plugins/woocommerce/src/Blocks/Domain/Bootstrap.php(148): Automattic\WooCommerce\Blocks\Registry\Container->get('Automattic\\WooC...') #4 /htdocs/wp-content/plugins/woocommerce/src/Blocks/Domain/Bootstrap.php(80): Automattic\WooCommerce\Blocks\Domain\Bootstrap->init() #5 /htdocs/wp-content/plugins/woocommerce/src/Blocks/Package.php(124): Automattic\WooCommerce\Blocks\Domain\Bootstrap->__construct(Object(Automattic\WooCommerce\Blocks\Registry\Container)) #6 /htdocs/wp-content/plugins/woocommerce/src/Blocks/Registry/AbstractDependencyType.php(42): Automattic\WooCommerce\Blocks\Package::Automattic\WooCommerce\Blocks\{closure}(Object(Automattic\WooCommerce\Blocks\Registry\Container)) #7 /htdocs/wp-content/plugins/woocommerce/src/Blocks/Registry/SharedType.php(28): Automattic\WooCommerce\Blocks\Registry\AbstractDependencyType->resolve_value(Object(Automattic\WooCommerce\Blocks\Registry\Container)) #8 /htdocs/wp-content/plugins/woocommerce/src/Blocks/Registry/Container.php(96): Automattic\WooCommerce\Blocks\Registry\SharedType->get(Object(Automattic\WooCommerce\Blocks\Registry\Container)) #9 /htdocs/wp-content/plugins/woocommerce/src/Blocks/Package.php(44): Automattic\WooCommerce\Blocks\Registry\Container->get('Automattic\\WooC...') #10 [internal function]: Automattic\WooCommerce\Blocks\Package::init() #11 /htdocs/wp-content/plugins/woocommerce/src/Packages.php(128): call_user_func(Array) #12 /htdocs/wp-content/plugins/woocommerce/src/Packages.php(64): Automattic\WooCommerce\Packages::initialize_packages() #13 /htdocs/wp-includes/class-wp-hook.php(324): Automattic\WooCommerce\Packages::on_init('') #14 /htdocs/wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters(NULL, Array) #15 /htdocs/wp-includes/plugin.php(517): WP_Hook->do_action(Array) #16 /htdocs/wp-settings.php(559): do_action('plugins_loaded') #17 /htdocs/wp-config.php(85): require_once('/htdocs/wp-sett...') #18 /htdocs/wp-load.php(50): require_once('/htdocs/wp-conf...') #19 /htdocs/wp-blog-header.php(13): require_once('/htdocs/wp-load...') #20 /htdocs/index.php(17): require('/htdocs/wp-blog...') #21 {main} thrown in /htdocs/wp-content/plugins/woocommerce/src/Blocks/Domain/Bootstrap.php on line 379