dir = $dir; $this->url = $url; } /** * Hook the asset loader. */ public function register(): void { add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ) ); } /** * Remember which screen is ours. * * @param string $hook_suffix Hook suffix returned by add_menu_page(). */ public function set_hook_suffix( string $hook_suffix ): void { $this->hook_suffix = $hook_suffix; } /** * Enqueue the app's script on the plugin page only. * * @param string $hook_suffix Hook suffix of the screen being loaded. */ public function enqueue( string $hook_suffix ): void { if ( '' === $this->hook_suffix || $hook_suffix !== $this->hook_suffix ) { return; } $script = BuiltAsset::path( $this->dir, 'assets/build/admin/index.js' ); $asset_file = $this->dir . BuiltAsset::asset_path( $script ); if ( ! is_readable( $asset_file ) ) { return; } /** * Dependencies and version generated by @wordpress/scripts. * * @var array{dependencies: string[], version: string} $asset */ $asset = require $asset_file; wp_enqueue_script( self::HANDLE, $this->url . $script, $asset['dependencies'], $asset['version'], true ); // Facts about this site the app reads before it renders; add-on code extends them through the filter. $boot = apply_filters( 'consentfold_admin_boot', array( 'extended' => false ) ); wp_add_inline_script( self::HANDLE, 'window.consentfoldBoot = ' . wp_json_encode( $boot ) . ';', 'before' ); wp_set_script_translations( self::HANDLE, TextDomain::DOMAIN, TextDomain::path( $this->dir ) ); $style = BuiltAsset::path( $this->dir, 'assets/build/admin/style-index.css' ); if ( is_readable( $this->dir . $style ) ) { wp_enqueue_style( self::HANDLE, $this->url . $style, array(), $asset['version'] ); wp_style_add_data( self::HANDLE, 'rtl', 'replace' ); // Lets core derive `style-index-rtl.min.css` from `style-index.min.css`. wp_style_add_data( self::HANDLE, 'suffix', BuiltAsset::style_suffix( $style ) ); } } /** * Render the mount point. It carries the admin theme the user picked, so the app starts in it; without a pick the * attribute is left out and the app follows the device. The header marker is where core puts admin notices. */ public function render(): void { if ( ! current_user_can( Capabilities::MANAGE ) ) { wp_die( esc_html__( 'You do not have permission to access this page.', 'consentfold' ), '', array( 'response' => 403 ) ); } $theme = Preferences::theme( get_current_user_id() ); printf( '

', esc_attr( self::ROOT_ID ), '' === $theme ? '' : ' data-theme="' . esc_attr( $theme ) . '"' ); } }