container = $container; $this->package = $container->get( Package::class ); $this->init(); /** * Fires when the woocommerce blocks are loaded and ready to use. * * This hook is intended to be used as a safe event hook for when the plugin * has been loaded, and all dependency requirements have been met. * * To ensure blocks are initialized, you must use the `woocommerce_blocks_loaded` * hook instead of the `plugins_loaded` hook. This is because the functions * hooked into plugins_loaded on the same priority load in an inconsistent and unpredictable manner. * * @since 2.5.0 */ do_action( 'woocommerce_blocks_loaded' ); } /** * Init the package - load the blocks library and define constants. */ protected function init() { $this->register_dependencies(); $this->register_payment_methods(); // We need to initialize BlockTemplatesController and BlockTemplatesRegistry at the end of `after_setup_theme` // so themes had the opportunity to declare support for template parts. add_action( 'after_setup_theme', function () { $is_store_api_request = wc()->is_store_api_request(); if ( ! $is_store_api_request && ( wp_is_block_theme() || current_theme_supports( 'block-template-parts' ) ) ) { $this->container->get( BlockTemplatesRegistry::class )->init(); $this->container->get( BlockTemplatesController::class )->init(); } }, 999 ); $is_rest = wc()->is_rest_api_request(); $is_store_api_request = wc()->is_store_api_request(); // Initialize Store API in non-admin context. if ( ! is_admin() ) { $this->container->get( StoreApi::class )->init(); } // Load and init assets. $this->container->get( PaymentsApi::class )->init(); $this->container->get( DraftOrders::class )->init(); $this->container->get( ShippingController::class )->init(); $this->container->get( CheckoutFields::class )->init(); $this->container->get( CheckoutLink::class )->init(); $this->container->get( AssetDataRegistry::class ); $this->container->get( AssetsController::class ); $this->container->get( DependencyDetection::class ); // Load assets in admin and on the frontend. if ( ! $is_rest ) { $this->add_build_notice(); $this->container->get( Installer::class )->init(); $this->container->get( GoogleAnalytics::class )->init(); $this->container->get( is_admin() ? CheckoutFieldsAdmin::class : CheckoutFieldsFrontend::class )->init(); } // Register block types on demand (priority 8, before do_blocks at 9) so blocks in a description are not empty. add_filter( 'woocommerce_short_description', array( $this, 'maybe_register_blocks_from_content' ), 8 ); add_action( 'woocommerce_email_editor_render_start', array( $this, 'handle_woocommerce_email_editor_render_start' ) ); // Load assets unless this is a request specifically for the store API. if ( ! $is_store_api_request ) { // Skip eager block/pattern/asset registration on non-rendering requests; the block types needed for // a description or an email are still registered on demand (see the hooks above). See BlockRegistrationContext. if ( ( new BlockRegistrationContext() )->should_register() ) { $this->container->get( BlockPatterns::class ); $this->container->get( BlockTypesController::class ); } $this->container->get( ClassicTemplatesCompatibility::class ); $this->container->get( Notices::class )->init(); if ( is_admin() || $is_rest ) { $this->container->get( PTKPatternsStore::class ); } if ( is_admin() ) { $this->container->get( TemplateOptions::class )->init(); } } } /** * Register WooCommerce block types on demand when a description containing one is rendered. * * Eager block registration is skipped on non-rendering requests (Store API, REST, AJAX, webhooks), but * product and variation descriptions still run through do_blocks there, so a WooCommerce block in a * description would render empty. Hooked to woocommerce_short_description just before do_blocks. * * The detection also fires on a synced pattern reference (core/block, `wp:block`): the referenced pattern's * content is not available here without fetching it, so registration happens defensively in case it * contains a WooCommerce block; registering covers nested blocks at any depth. * * @since 11.1.0 * * @param string $content The description content passed through the filter. * @return string The unchanged content. */ public function maybe_register_blocks_from_content( $content ) { if ( is_string( $content ) && preg_match( '/