i18n = $i18n; $this->settings = $settings; } /** * {@inheritdoc} */ public function register(): void { add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin' ) ); } /** * Whether a compiled bundle exists. * * @since 0.1.0 * * @param string $entry `admin` or `public`. * @return bool */ public static function build_exists( string $entry ): bool { return is_readable( INTERVIEWNEA_PATH . 'assets/build/' . $entry . '.js' ) && is_readable( INTERVIEWNEA_PATH . 'assets/build/' . $entry . '.asset.php' ); } /** * Tells whether the compiled admin bundle exists. * * @since 0.1.0 * * @return bool */ public static function admin_build_exists(): bool { return self::build_exists( 'admin' ); } /** * Enqueues the admin app on the InterviewNea 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 INTERVIEWNEA_PATH . 'assets/build/admin.asset.php'; wp_enqueue_script( self::ADMIN_HANDLE, INTERVIEWNEA_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.interviewneaData = ' . wp_json_encode( $this->admin_data(), JSON_HEX_TAG | JSON_HEX_AMP ) . ';', 'before' ); if ( is_readable( INTERVIEWNEA_PATH . 'assets/build/admin.css' ) ) { wp_enqueue_style( self::ADMIN_HANDLE, INTERVIEWNEA_URL . 'assets/build/admin.css', array(), $asset['version'] ); wp_style_add_data( self::ADMIN_HANDLE, 'rtl', 'replace' ); } } /** * Enqueues the public bundle (script and style) of the waiting room. * * @since 0.1.0 * * @return bool False when the bundle has not been built. */ public function enqueue_public(): bool { if ( ! self::build_exists( 'public' ) ) { return false; } $asset = require INTERVIEWNEA_PATH . 'assets/build/public.asset.php'; wp_enqueue_script( self::PUBLIC_HANDLE, INTERVIEWNEA_URL . 'assets/build/public.js', $asset['dependencies'], $asset['version'], true ); $this->i18n->set_script_translations( self::PUBLIC_HANDLE ); if ( is_readable( INTERVIEWNEA_PATH . 'assets/build/public.css' ) ) { wp_enqueue_style( self::PUBLIC_HANDLE, INTERVIEWNEA_URL . 'assets/build/public.css', array(), $asset['version'] ); wp_style_add_data( self::PUBLIC_HANDLE, 'rtl', 'replace' ); } return true; } /** * Data handed to the admin app. * * @return array */ private function admin_data(): array { return apply_filters( 'interviewnea_admin_app_data', array( 'version' => INTERVIEWNEA_VERSION, 'restUrl' => esc_url_raw( rest_url( 'interviewnea/v1/' ) ), 'appUrl' => esc_url_raw( admin_url( 'admin.php?page=' . Menu::SLUG ) ), 'debug' => defined( 'WP_DEBUG' ) && WP_DEBUG, 'timezone' => wp_timezone_string(), 'today' => wp_date( 'Y-m-d' ), 'canCreate' => current_user_can( 'publish_pages' ), 'settings' => $this->settings->export_for_app(), 'pages' => SettingsController::page_options(), 'downloads' => array( 'export' => esc_url_raw( CsvExport::url( 'export' ) ), 'sample' => esc_url_raw( CsvExport::url( 'sample' ) ), ), ) ); } }