i18n = $i18n; $this->settings = $settings; } /** * {@inheritdoc} */ public function register(): void { add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin' ) ); } /** * Tells whether the compiled admin bundle exists. * * @since 0.1.0 * * @return bool */ public static function admin_build_exists(): bool { return is_readable( HOSTKHATA_PATH . 'assets/build/admin.js' ) && is_readable( HOSTKHATA_PATH . 'assets/build/admin.asset.php' ); } /** * Enqueues the admin app on the HostKhata screen. * * @since 0.1.0 * * @param string $hook_suffix Current admin page hook suffix. * @return void */ public function enqueue_admin( string $hook_suffix ): void { if ( ! Menu::is_app_screen( $hook_suffix ) || ! self::admin_build_exists() ) { return; } $asset = require HOSTKHATA_PATH . 'assets/build/admin.asset.php'; wp_enqueue_script( self::ADMIN_HANDLE, HOSTKHATA_URL . 'assets/build/admin.js', $asset['dependencies'], $asset['version'], true ); $this->i18n->set_script_translations( self::ADMIN_HANDLE ); wp_add_inline_script( self::ADMIN_HANDLE, 'window.hostkhataData = ' . wp_json_encode( $this->admin_data(), JSON_HEX_TAG | JSON_HEX_AMP ) . ';', 'before' ); if ( is_readable( HOSTKHATA_PATH . 'assets/build/admin.css' ) ) { wp_enqueue_style( self::ADMIN_HANDLE, HOSTKHATA_URL . 'assets/build/admin.css', array(), $asset['version'] ); wp_style_add_data( self::ADMIN_HANDLE, 'rtl', 'replace' ); } } /** * Data handed to the admin app. * * @return array */ private function admin_data(): array { /** * Filters the data exposed to the admin app as `window.hostkhataData`. * * @since 0.1.0 * * @param array $data Bootstrap data. */ $can = array(); foreach ( Capabilities::staff_caps() as $capability ) { $can[ $capability ] = current_user_can( $capability ); } $exports = array(); foreach ( CsvExport::kinds() as $kind => $capability ) { if ( current_user_can( $capability ) ) { $exports[ $kind ] = esc_url_raw( CsvExport::url( $kind ) ); } } return apply_filters( 'hostkhata_admin_app_data', array( 'version' => HOSTKHATA_VERSION, 'restNamespace' => 'hostkhata/v1', 'restUrl' => esc_url_raw( rest_url( 'hostkhata/v1/' ) ), 'appUrl' => esc_url_raw( admin_url( 'admin.php?page=' . Menu::SLUG ) ), 'debug' => defined( 'WP_DEBUG' ) && WP_DEBUG, 'can' => $can, 'exports' => $exports, 'settings' => $this->settings->export_for_app(), ) ); } }