rest = new RestController(); } /** * Hooks the screen in. * * @return void */ public function register(): void { $this->rest->register(); if (is_admin()) { add_action('admin_menu', [$this, 'registerMenu']); add_action('network_admin_menu', [$this, 'registerNetworkMenu']); } } /** * Adds the entry under Settings. * * @return void */ public function registerMenu(): void { $hook = add_submenu_page( 'options-general.php', _x('CryptX', 'CryptX settings page', 'cryptx'), _x('CryptX', 'CryptX settings menu', 'cryptx'), 'manage_options', self::MENU_SLUG, [$this, 'render'] ); if ($hook) { add_action('load-' . $hook, [$this, 'onLoad']); } } /** * Adds the network entry, for the defaults a new site starts with. * * Under the network's own Settings and behind manage_network_options: a * site administrator configures their own site, a super administrator * decides what the next site begins with. Two different questions, two * different capabilities. * * @return void */ public function registerNetworkMenu(): void { $hook = add_submenu_page( 'settings.php', _x('CryptX', 'CryptX network defaults page', 'cryptx'), _x('CryptX', 'CryptX network defaults menu', 'cryptx'), 'manage_network_options', self::MENU_SLUG, [$this, 'renderNetwork'] ); if ($hook) { add_action('load-' . $hook, [$this, 'onLoad']); } } /** * Runs only when our own screen is being loaded. * * @return void */ public function onLoad(): void { add_action('admin_enqueue_scripts', [$this, 'enqueueAssets']); } /** * Loads the built application. * * @return void */ public function enqueueAssets(): void { $assetFile = CRYPTX_DIR_PATH . 'build/index.asset.php'; if (!is_readable($assetFile)) { add_action('admin_notices', [$this, 'renderMissingBuildNotice']); return; } $asset = require $assetFile; wp_enqueue_script( self::SCRIPT_HANDLE, CRYPTX_DIR_URL . 'build/index.js', $asset['dependencies'] ?? [], $asset['version'] ?? CRYPTX_VERSION, true ); wp_set_script_translations(self::SCRIPT_HANDLE, 'cryptx'); $style = CRYPTX_DIR_PATH . 'build/index.css'; if (is_readable($style)) { wp_enqueue_style( self::SCRIPT_HANDLE, CRYPTX_DIR_URL . 'build/index.css', ['wp-components'], $asset['version'] ?? CRYPTX_VERSION ); } // The media library picker for the "image from the media library" // option needs the classic media modal to be present. // // Not in the network backend: the one field that needs it is left out // of the network defaults on purpose -- an attachment id means nothing // on another site -- and there is no media library there to pick from // either. if (!is_network_admin()) { wp_enqueue_media(); } } /** * Shown when the plugin was installed without its built assets. * * @return void */ public function renderMissingBuildNotice(): void { echo '
'; echo esc_html__('CryptX: the settings screen could not be loaded because its built assets are missing. If you installed CryptX from a source checkout, run "npm install && npm run build" in the plugin directory.', 'cryptx'); echo '
' . esc_html($loading) . '
'; echo '