plugin_file = $plugin_file; $this->capabilities = $capabilities; } /** * {@inheritDoc} */ public function register_hooks(): void { add_action( 'admin_menu', array( $this, 'register_page' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) ); } /** * Adds the page under the Tools menu. * * @return void */ public function register_page(): void { $hook_suffix = add_management_page( __( 'Cherrylift', 'cherrylift' ), __( 'Cherrylift', 'cherrylift' ), $this->capabilities->get_capability(), self::MENU_SLUG, array( $this, 'render' ) ); $this->hook_suffix = is_string( $hook_suffix ) ? $hook_suffix : ''; } /** * Outputs the page container the React app mounts into. * * @return void */ public function render(): void { printf( '

%1$s

', esc_html__( 'Cherrylift', 'cherrylift' ), esc_attr( self::ROOT_ELEMENT_ID ) ); } /** * Enqueues the admin app on the plugin page only. * * @param string $hook_suffix Current admin page hook suffix. * @return void */ public function enqueue_assets( string $hook_suffix ): void { if ( '' === $this->hook_suffix || $hook_suffix !== $this->hook_suffix ) { return; } $asset_file = plugin_dir_path( $this->plugin_file ) . 'build/index.asset.php'; if ( ! is_readable( $asset_file ) ) { return; } $asset = require $asset_file; wp_enqueue_script( self::SCRIPT_HANDLE, plugins_url( 'build/index.js', $this->plugin_file ), $asset['dependencies'], $asset['version'], array( 'in_footer' => true ) ); // Translations come from WordPress.org language packs. wp_set_script_translations( self::SCRIPT_HANDLE, 'cherrylift' ); wp_enqueue_style( 'wp-components' ); if ( is_readable( plugin_dir_path( $this->plugin_file ) . 'build/style-index.css' ) ) { wp_enqueue_style( self::SCRIPT_HANDLE, plugins_url( 'build/style-index.css', $this->plugin_file ), array( 'wp-components' ), $asset['version'] ); wp_style_add_data( self::SCRIPT_HANDLE, 'rtl', 'replace' ); } } }