MadeCost and loads the page script there (and nowhere else). */ final class Admin { const SLUG = 'madecost'; /** * Hook suffix of the page, set when the menu is added. * * @var string|false */ private $hook_suffix = false; /** * Hook into the admin. */ public function register(): void { add_action( 'admin_menu', array( $this, 'add_menu' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ) ); add_filter( 'plugin_action_links_' . plugin_basename( MADECOST_FILE ), array( $this, 'action_links' ) ); } /** * Add the page under the WooCommerce menu. */ public function add_menu(): void { $this->hook_suffix = add_submenu_page( 'woocommerce', __( 'MadeCost', 'madecost' ), __( 'MadeCost', 'madecost' ), 'manage_woocommerce', self::SLUG, array( $this, 'render' ) ); } /** * Link on the Plugins screen. * * @param string[] $links Existing links. * @return string[] */ public function action_links( array $links ): array { $url = admin_url( 'admin.php?page=' . self::SLUG ); array_unshift( $links, '' . esc_html__( 'Open MadeCost', 'madecost' ) . '' ); return $links; } /** * The page: a mount point, React does the rest. */ public function render(): void { echo '
'; } /** * Data handed to the script. * * @return array */ private function boot_data(): array { return array( 'version' => MADECOST_VERSION, 'currency' => function_exists( 'get_woocommerce_currency' ) ? get_woocommerce_currency() : 'USD', 'locale' => str_replace( '_', '-', determine_locale() ), 'restNamespace' => Rest::API_NAMESPACE, /** * Whether an add-on that unlocks more features is active. The add-on returns true. */ 'proActive' => (bool) apply_filters( 'madecost_pro_active', false ), ); } /** * Load the script and style on our page only. * * @param string $hook Current admin page hook suffix. */ public function enqueue( $hook ): void { if ( ! $this->hook_suffix || $hook !== $this->hook_suffix ) { return; } $asset_file = MADECOST_DIR . 'build/index.asset.php'; if ( ! is_readable( $asset_file ) ) { add_action( 'admin_notices', static function () { echo '

' . esc_html__( 'MadeCost: the page script is missing. Reinstall the plugin.', 'madecost' ) . '

'; } ); return; } $asset = include $asset_file; wp_enqueue_script( 'madecost-admin', MADECOST_URL . 'build/index.js', $asset['dependencies'], $asset['version'], true ); wp_set_script_translations( 'madecost-admin', 'madecost' ); wp_add_inline_script( 'madecost-admin', 'window.madecostBoot = ' . wp_json_encode( $this->boot_data() ) . ';', 'before' ); wp_enqueue_style( 'madecost-admin', MADECOST_URL . 'build/style-index.css', array( 'wp-components' ), $asset['version'] ); } }