es'] = $this->get_block_type_attributes(); $block_settings['supports'] = $this->get_block_type_supports(); $block_settings['uses_context'] = $this->get_block_type_uses_context(); register_block_type( $this->get_block_type(), $block_settings ); } /** * Get the block type. * * @return string */ protected function get_block_type() { return $this->namespace . '/' . $this->block_name; } /** * Get the render callback for this block type. * * Dynamic blocks should return a callback, for example, `return [ $this, 'render' ];` * * @see $this->register_block_type() * @return callable|null; */ protected function get_block_type_render_callback() { return [ $this, 'render_callback' ]; } /** * Get the editor script data for this block type. * * @see $this->register_block_type() * @param string $key Data to get, or default to everything. * @return array|string */ protected function get_block_type_editor_script( $key = null ) { $script = [ 'handle' => 'wc-' . $this->block_name . '-block', 'path' => $this->asset_api->get_block_asset_build_path( $this->block_name ), 'dependencies' => [ 'wc-blocks' ], ]; return $key ? $script[ $key ] : $script; } /** * Get the editor style handle for this block type. * * @see $this->register_block_type() * @return string|null */ protected function get_block_type_editor_style() { return 'wc-blocks-editor-style'; } /** * Get the frontend script handle for this block type. * * @see $this->register_block_type() * @param string $key Data to get, or default to everything. * @return array|string|null */ protected function get_block_type_script( $key = null ) { $script = [ 'handle' => 'wc-' . $this->block_name . '-block-frontend', 'path' => $this->asset_api->get_block_asset_build_path( $this->block_name . '-frontend' ), 'dependencies' => [], ]; return $key ? $script[ $key ] : $script; } /** * Get the frontend style handle for this block type. * * @return string[]|null */ protected function get_block_type_style() { $this->asset_api->register_style( 'wc-blocks-style-' . $this->block_name, $this->asset_api->get_block_asset_build_path( $this->block_name, 'css' ), [], 'all', true ); return [ 'wc-blocks-style', 'wc-blocks-style-' . $this->block_name ]; } /** * Get the supports array for this block type. * * @see $this->register_block_type() * @return string; */ protected function get_block_type_supports() { return []; } /** * Get block attributes. * * @return array; */ protected function get_block_type_attributes() { return []; } /** * Get block usesContext. * * @return array; */ protected function get_block_type_uses_context() { return []; } /** * Parses block attributes from the render_callback. * * @param array|WP_Block $attributes Block attributes, or an instance of a WP_Block. Defaults to an empty array. * @return array */ protected function parse_render_callback_attributes( $attributes ) { return is_a( $attributes, 'WP_Block' ) ? $attributes->attributes : $attributes; } /** * Render the block. Extended by children. * * @param array $attributes Block attributes. * @param string $content Block content. * @param WP_Block $block Block instance. * @return string Rendered block type output. */ protected function render( $attributes, $content, $block ) { return $content; } /** * Enqueue frontend assets for this block, just in time for rendering. * * @internal This prevents the block script being enqueued on all pages. It is only enqueued as needed. Note that * we intentionally do not pass 'script' to register_block_type. * * @param array $attributes Any attributes that currently are available from the block. * @param string $content The block content. * @param WP_Block $block The block object. */ protected function enqueue_assets( array $attributes, $content, $block ) { if ( $this->enqueued_assets ) { return; } $this->enqueue_data( $attributes ); $this->enqueue_scripts( $attributes ); $this->enqueued_assets = true; } /** * Data passed through from server to client for block. * * @param array $attributes Any attributes that currently are available from the block. * Note, this will be empty in the editor context when the block is * not in the post content on editor load. */ protected function enqueue_data( array $attributes = [] ) { $registered_script_data = $this->integration_registry->get_all_registered_script_data(); foreach ( $registered_script_data as $asset_data_key => $asset_data_value ) { if ( ! $this->asset_data_registry->exists( $asset_data_key ) ) { $this->asset_data_registry->add( $asset_data_key, $asset_data_value ); } } if ( ! $this->asset_data_registry->exists( 'wcBlocksConfig' ) ) { $wc_blocks_config = [ 'pluginUrl' => plugins_url( '/', dirname( __DIR__, 2 ) ), 'restApiRoutes' => [ '/wc/store/v1' => array_keys( $this->get_routes_from_namespace( 'wc/store/v1' ) ), ], 'defaultAvatar' => get_avatar_url( 0, [ 'force_default' => true ] ), /* * translators: If your word count is based on single characters (e.g. East Asian characters), * enter 'characters_excluding_spaces' or 'characters_including_spaces'. Otherwise, enter 'words'. * Do not translate into your own language. */ 'wordCountType' => _x( 'words', 'Word count type. Do not translate!', 'woocommerce' ), ]; if ( is_admin() && ! WC()->is_rest_api_request() ) { $product_counts = wp_count_posts( 'product' ); $published_products = isset( $product_counts->publish ) ? $product_counts->publish : 0; $wc_blocks_config = array_merge( $wc_blocks_config, [ // Note that while we don't have a consolidated way of doing feature-flagging // we are borrowing from the WC Admin Features implementation. Also note we cannot // use the wcAdminFeatures global because it's not always enqueued in the context of blocks. 'experimentalBlocksEnabled' => Features::is_enabled( 'experimental-blocks' ), 'experimentalWcRestApi' => Features::is_enabled( 'experimental-wc-rest-api' ), 'productCount' => $published_products, ] ); } $this->asset_data_registry->add( 'wcBlocksConfig', $wc_blocks_config ); } } /** * Get routes from a REST API namespace. * * @param string $namespace Namespace to retrieve. * @return array */ protected function get_routes_from_namespace( $namespace ) { /** * Gives opportunity to return routes without invoking the compute intensive REST API. * * @since 8.7.0 * @param array $routes Array of routes. * @param string $namespace Namespace for routes. * @param string $context Context, can be edit or view. */ $routes = apply_filters( 'woocommerce_blocks_pre_get_routes_from_namespace', array(), $namespace, 'view' ); if ( ! empty( $routes ) ) { return $routes; } $rest_server = rest_get_server(); $namespace_index = $rest_server->get_namespace_index( [ 'namespace' => $namespace, 'context' => 'view', ] ); if ( is_wp_error( $namespace_index ) ) { return []; } $response_data = $namespace_index->get_data(); return $response_data['routes'] ?? []; } /** * Register/enqueue scripts used for this block on the frontend, during render. * * @param array $attributes Any attributes that currently are available from the block. */ protected function enqueue_scripts( array $attributes = [] ) { if ( null !== $this->get_block_type_script() ) { wp_enqueue_script( $this->get_block_type_script( 'handle' ) ); } } }