assets = $assets; $this->stories_script_data = $stories_script_data; $this->context = $context; } /** * Initialization actions. * * @since 1.5.0 */ public function register(): void { if ( $this->context->is_block_editor() || $this->context->is_story_editor() ) { return; } $this->register_assets(); add_action( 'wp_enqueue_editor', [ $this, 'enqueue_assets' ] ); add_filter( 'mce_buttons', [ $this, 'tinymce_web_stories_button' ] ); add_filter( 'mce_external_plugins', [ $this, 'web_stories_mce_plugin' ] ); add_action( 'admin_footer', [ $this, 'web_stories_tinymce_root_element' ] ); add_filter( 'script_loader_tag', [ $this, 'script_loader_tag' ], 10, 3 ); } /** * Get the action to use for registering the service. * * @since 1.6.0 * * @return string Registration action to use. */ public static function get_registration_action(): string { return 'admin_enqueue_scripts'; } /** * Add web stories button in TinyMCE editor. * * @since 1.5.0 * * @param array|mixed $buttons Array of TinyMCE buttons. * @return array|mixed * * @template T * * @phpstan-return ($buttons is array ? array : mixed) */ public function tinymce_web_stories_button( $buttons ) { if ( ! \is_array( $buttons ) ) { return $buttons; } $buttons[] = 'web_stories'; return $buttons; } /** * Register web stories plugin for tinycemce editor. * * @since 1.5.0 * * @param array|mixed $plugins Array of TinyMCE plugin scripts. * @return array|mixed * * @template T * * @phpstan-return ($plugins is array ? array : mixed) */ public function web_stories_mce_plugin( $plugins ) { if ( ! \is_array( $plugins ) ) { return $plugins; } $plugins['web_stories'] = $this->assets->get_base_url( sprintf( 'assets/js/%s.js', self::SCRIPT_HANDLE ) ); return $plugins; } /** * Enqueue related scripts. * * @since 1.5.0 */ public function register_assets(): void { $this->assets->enqueue_style( 'wp-components' ); $this->assets->enqueue_script_asset( self::SCRIPT_HANDLE ); wp_localize_script( self::SCRIPT_HANDLE, 'webStoriesData', $this->stories_script_data->get_script_data() ); } /** * Hijack the button's script to render an empty script tag. * * @since 1.5.0 * * @param string|mixed $tag The `#is', '', $tag ); } return $tag; } /** * Enqueue related scripts. * * @since 1.5.0 */ public function enqueue_assets(): void { $this->assets->enqueue_style( 'wp-components' ); $this->assets->enqueue_script_asset( self::SCRIPT_HANDLE ); } /** * Root element for tinymce editor. * This is useful for performing some react operations. * * @since 1.5.0 */ public function web_stories_tinymce_root_element(): void { ?>