screen name the React app understands. */ const PAGES = array( 'brickmoat' => 'overview', 'brickmoat-events' => 'events', 'brickmoat-cleanup' => 'cleanup', 'brickmoat-settings' => 'settings', ); /** * Hook suffixes of the Brickmoat pages, set when the menu is added. * * @var string[] */ private static $hooks = array(); /** * Hooks the admin in. */ public static function register() { add_action( 'admin_menu', array( __CLASS__, 'menu' ) ); add_action( 'admin_enqueue_scripts', array( __CLASS__, 'assets' ) ); add_filter( 'admin_body_class', array( __CLASS__, 'body_class' ) ); add_action( 'admin_notices', array( __CLASS__, 'tables_notice' ) ); } /** * Says so when Brickmoat's database tables could not be created, on Brickmoat's own pages and * on the Plugins screen only. The notice goes away by itself once the tables exist. */ public static function tables_notice() { // The screen is checked first, so that no other admin page pays for looking the option up. $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; if ( ! $screen || ( 'plugins' !== $screen->id && ! self::is_our_page( $screen->id ) ) || ! current_user_can( self::CAPABILITY ) ) { return; } $missing = get_option( Schema::MISSING_OPTION, array() ); if ( empty( $missing ) || ! is_array( $missing ) ) { return; } echo '

' . esc_html__( 'Brickmoat could not create its database tables, so it is not checking your checkout yet.', 'brickmoat' ) . ' '; echo esc_html__( 'Your shop keeps working as it did before. This usually means the database user WordPress uses is not allowed to create tables. Ask your host to allow it; Brickmoat tries again by itself every ten minutes, or straight away if you deactivate and activate it.', 'brickmoat' ); echo '

'; } /** * Adds the Brickmoat menu and its sub-items. */ public static function menu() { self::$hooks[] = (string) add_menu_page( __( 'Brickmoat', 'brickmoat' ), __( 'Brickmoat', 'brickmoat' ), self::CAPABILITY, 'brickmoat', array( __CLASS__, 'render' ), self::menu_icon(), 56 ); $labels = array( 'brickmoat' => __( 'Overview', 'brickmoat' ), 'brickmoat-events' => __( 'Event log', 'brickmoat' ), 'brickmoat-cleanup' => __( 'Cleanup', 'brickmoat' ), 'brickmoat-settings' => __( 'Settings', 'brickmoat' ), ); foreach ( $labels as $slug => $label ) { self::$hooks[] = (string) add_submenu_page( 'brickmoat', /* translators: %s: name of a Brickmoat screen, e.g. "Event log". */ sprintf( __( '%s ‹ Brickmoat', 'brickmoat' ), $label ), $label, self::CAPABILITY, $slug, array( __CLASS__, 'render' ) ); } } /** * The Brickmoat mark (three bricks over a moat) as a single-colour menu icon. * * @return string Data URI. */ private static function menu_icon() { $svg = ''; // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode -- WordPress expects a base64 data URI for an SVG menu icon. return 'data:image/svg+xml;base64,' . base64_encode( $svg ); } /** * Whether the current admin page is one of Brickmoat's. * * @param string $hook Hook suffix of the page being shown. * @return bool */ private static function is_our_page( $hook ) { return in_array( $hook, self::$hooks, true ); } /** * Which screen was asked for. * * @return string */ private static function current_screen_name() { // WordPress has already worked out which admin page was asked for. global $plugin_page; $slug = is_string( $plugin_page ) && '' !== $plugin_page ? sanitize_key( $plugin_page ) : 'brickmoat'; return isset( self::PAGES[ $slug ] ) ? self::PAGES[ $slug ] : 'overview'; } /** * Marks Brickmoat pages on , so the page background can match the app. * * @param string $classes Space-separated class names. * @return string */ public static function body_class( $classes ) { $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; if ( $screen && self::is_our_page( $screen->id ) ) { $classes .= ' brickmoat-admin-page'; } return $classes; } /** * Loads the admin app, on Brickmoat pages only. * * @param string $hook Hook suffix of the page being shown. */ public static function assets( $hook ) { if ( ! self::is_our_page( $hook ) ) { return; } $asset_file = BRICKMOAT_DIR . 'build/admin.asset.php'; if ( ! is_readable( $asset_file ) ) { return; } $asset = require $asset_file; wp_enqueue_script( 'brickmoat-admin', plugins_url( 'build/admin.js', BRICKMOAT_FILE ), $asset['dependencies'], $asset['version'], true ); wp_set_script_translations( 'brickmoat-admin', 'brickmoat' ); $boot = array( 'screen' => self::current_screen_name(), 'version' => BRICKMOAT_VERSION, 'adminUrl' => admin_url( 'admin.php' ), 'pages' => array_flip( self::PAGES ), 'locale' => str_replace( '_', '-', get_user_locale() ), 'assetsUrl' => plugins_url( 'build/', BRICKMOAT_FILE ), ); wp_add_inline_script( 'brickmoat-admin', 'window.brickmoatAdmin = ' . wp_json_encode( $boot ) . ';', 'before' ); self::preload( $boot['screen'] ); if ( is_readable( BRICKMOAT_DIR . 'build/admin.css' ) ) { wp_enqueue_style( 'brickmoat-admin', plugins_url( 'build/admin.css', BRICKMOAT_FILE ), array(), $asset['version'] ); // The build also writes admin-rtl.css; WordPress swaps it in for right-to-left languages. wp_style_add_data( 'brickmoat-admin', 'rtl', 'replace' ); } } /** * Works out the first answers a screen asks for while this page is being built, and hands * them to the app through WordPress's own preloading (as the block editor does). The screen * then appears without two more trips to the server, each of which starts WordPress again. * Each answer is used once; later refreshes ask the server as before. Only this store's own * data is read here; nothing contacts another site. * * @param string $screen The Brickmoat screen being opened. */ private static function preload( $screen ) { if ( ! function_exists( 'rest_preload_api_request' ) ) { return; } // The exact requests the screens make first (see src/admin/screens/*.tsx). $first = array( 'events' => '/brickmoat/v1/events?page=1&perPage=25', 'cleanup' => '/brickmoat/v1/cleanup', 'settings' => '/brickmoat/v1/settings', ); $paths = array( '/brickmoat/v1/state' ); if ( isset( $first[ $screen ] ) ) { $paths[] = $first[ $screen ]; } $preload = array_reduce( $paths, 'rest_preload_api_request', array() ); // Overview's numbers only when Overview is what shows: a new store sees the welcome screen. if ( 'overview' === $screen && ! empty( $preload['/brickmoat/v1/state']['body']['onboarding']['done'] ) ) { $preload = rest_preload_api_request( $preload, '/brickmoat/v1/overview?range=24h' ); } if ( empty( $preload ) ) { return; } wp_add_inline_script( 'wp-api-fetch', sprintf( 'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );', wp_json_encode( $preload ) ), 'after' ); } /** * Prints the page. The app mounts inside .brickmoat-admin, which scopes every style * the plugin ships so that WordPress's own styles and ours never touch. */ public static function render() { echo '
'; // The heading WordPress expects for screen readers and for its notices to attach to. echo '

' . esc_html__( 'Brickmoat', 'brickmoat' ) . '

'; echo '
'; if ( ! is_readable( BRICKMOAT_DIR . 'build/admin.js' ) ) { echo '

' . esc_html__( 'The Brickmoat admin files are missing. Please reinstall the plugin.', 'brickmoat' ) . '

'; } else { printf( '
', esc_attr( self::current_screen_name() ), esc_html__( 'Brickmoat needs JavaScript to show this page. Your checkout is still protected.', 'brickmoat' ) ); } echo '
'; } }