plugin = $plugin; } /** * Registers the enqueue_block_editor_assets hook. */ public function register(): void { add_action( 'enqueue_block_editor_assets', array( $this, 'maybe_enqueue' ) ); } /** * Enqueues the editor bundle when every condition is met. */ public function maybe_enqueue(): void { if ( ! current_user_can( Capabilities::VIEW ) ) { return; } if ( ! $this->plugin->connection()->is_connected() || '' === (string) $this->plugin->settings()->get( 'property_id' ) ) { return; } $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; if ( null === $screen || ! in_array( $screen->post_type, (array) $this->plugin->settings()->get( 'post_types' ), true ) ) { return; } $this->enqueue(); } /** * Enqueues build/editor.js, its style and the inline tagsmithConfig. */ private function enqueue(): void { $handle = 'tagsmith-editor'; $asset_php = TAGSMITH_DIR . 'build/editor.asset.php'; if ( ! is_readable( $asset_php ) ) { return; } $asset = require $asset_php; wp_enqueue_script( $handle, TAGSMITH_URL . 'build/editor.js', (array) ( $asset['dependencies'] ?? array() ), (string) ( $asset['version'] ?? TAGSMITH_VERSION ), true ); wp_set_script_translations( $handle, 'tagsmith' ); $css_path = TAGSMITH_DIR . 'build/editor.css'; if ( is_readable( $css_path ) ) { wp_enqueue_style( $handle, TAGSMITH_URL . 'build/editor.css', array( 'wp-components' ), (string) ( $asset['version'] ?? TAGSMITH_VERSION ) ); } wp_add_inline_script( $handle, // This is printed inside a script block: < > and & leave as \u escapes. 'window.tagsmithConfig = ' . wp_json_encode( $this->plugin->assets()->config(), JSON_HEX_TAG | JSON_HEX_AMP ) . ';', 'before' ); } }