registry = $registry; $registry_factory = static function () use ( $registry ) { return $registry; }; } $this->registry_factory = $registry_factory; } /** * Get the service registry, building it on first call. * * Calling this before `init` forces early resolution, which fires the * `attosocial_service_definitions` and `attosocial_register_services` * hooks before later plugins and the theme can attach to them. * * @return ServiceRegistry */ public function get_registry() { if ( null === $this->registry ) { $this->registry = call_user_func( $this->registry_factory ); } return $this->registry; } /** * Bootstrap plugin hooks. * * @return void */ public function init() { add_action( 'init', array( $this, 'register_blocks' ) ); add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_editor_assets' ) ); } /** * Register all plugin blocks. * * @return void */ public function register_blocks() { $blocks_dir = dirname( __DIR__ ) . '/blocks/build'; $manifest = $blocks_dir . '/blocks-manifest.php'; // WP 6.7+: read block metadata from the generated PHP manifest instead of each block.json. if ( function_exists( 'wp_register_block_metadata_collection' ) && file_exists( $manifest ) ) { wp_register_block_metadata_collection( $blocks_dir, $manifest ); } // 1. Register parent container block (share). $share_block_meta = $blocks_dir . '/share'; if ( file_exists( $share_block_meta . '/block.json' ) ) { register_block_type( $share_block_meta ); } // 2. Register child block with injected renderer callback. $item_block_meta = $blocks_dir . '/share-item'; if ( file_exists( $item_block_meta . '/block.json' ) ) { // The renderer resolves the registry lazily, only when a block is rendered. $item_renderer = new ShareItemRenderer( array( $this, 'get_registry' ) ); register_block_type( $item_block_meta, array( 'render_callback' => array( $item_renderer, 'render' ), ) ); } } /** * Enqueue editor assets and localize registered networks for editor. * * @return void */ public function enqueue_editor_assets() { $options = $this->get_registry()->get_options(); $script = 'window.attosocialNetworks = ' . wp_json_encode( $options ) . ';'; // Attach once, to the share-item editor script that consumes the data. $block_type = WP_Block_Type_Registry::get_instance()->get_registered( 'attosocial/share-item' ); $handle = ( null !== $block_type && ! empty( $block_type->editor_script_handles ) ) ? $block_type->editor_script_handles[0] : 'wp-blocks'; wp_add_inline_script( $handle, $script, 'before' ); } }