sessions = $sessions; $this->browse = $browse; } /** * Hooks. */ public function register_hooks(): void { add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ) ); } /** * Data for the app. */ public function data(): array { $latest = 0; foreach ( $this->sessions->all( 10 ) as $s ) { if ( 'done' === $s['status'] ) { $latest = (int) $s['id']; break; } } return array( 'restUrl' => esc_url_raw( rest_url( 'bloatwright/v1' ) ), 'nonce' => wp_create_nonce( 'wp_rest' ), 'pageUrl' => admin_url( 'tools.php?page=' . Menu::SLUG ), 'exportUrl' => Export::url(), 'homeUrl' => home_url( '/' ), 'tab' => Menu::tab(), 'latest' => $latest, 'browse' => $this->browse->active(), 'version' => BLOATWRIGHT_VERSION, 'settings' => array( 'passes' => (int) Settings::get( 'passes', 3 ), 'mode' => (string) Settings::get( 'mode', 'auto' ), 'cookie_ttl' => (int) Settings::get( 'cookie_ttl', 120 ), 'warmup' => (bool) Settings::get( 'warmup', true ), ), ); } /** * Asset manifest. * * @param string $name Entry. */ private function asset( string $name ): array { $file = BLOATWRIGHT_DIR . 'assets/build/' . $name . '.asset.php'; if ( is_readable( $file ) ) { $asset = include $file; if ( is_array( $asset ) ) { return $asset; } } return array( 'dependencies' => array( 'wp-element', 'wp-components', 'wp-api-fetch', 'wp-i18n' ), 'version' => BLOATWRIGHT_VERSION, ); } /** * Enqueue. * * @param string $hook Hook suffix. */ public function enqueue( string $hook ): void { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found -- routing is on the page query arg, not the hook suffix. $this->enqueue_bar(); if ( '' === Menu::current_page() ) { return; } $asset = $this->asset( 'admin' ); wp_enqueue_script( 'bloatwright-admin', BLOATWRIGHT_URL . 'assets/build/admin.js', $asset['dependencies'], $asset['version'], true ); wp_set_script_translations( 'bloatwright-admin', 'bloatwright', BLOATWRIGHT_DIR . 'languages' ); wp_localize_script( 'bloatwright-admin', 'bloatwrightData', $this->data() ); wp_enqueue_style( 'wp-components' ); wp_enqueue_style( 'bloatwright-admin', BLOATWRIGHT_URL . 'assets/css/admin.css', array(), BLOATWRIGHT_VERSION ); } /** * Stylesheet for the admin-bar "recording" item (admin and front end) while browse mode is active. */ public function enqueue_bar(): void { if ( ! is_admin_bar_showing() || ! current_user_can( Settings::capability() ) || ! $this->browse->active() ) { return; } wp_enqueue_style( 'bloatwright-admin', BLOATWRIGHT_URL . 'assets/css/admin.css', array(), BLOATWRIGHT_VERSION ); } /** * Admin bar item while browse mode is active. * * @param \WP_Admin_Bar $bar Bar. */ public function admin_bar( \WP_Admin_Bar $bar ): void { if ( ! current_user_can( Settings::capability() ) || ! $this->browse->active() ) { return; } $bar->add_node( array( 'id' => 'bloatwright-browse', 'title' => '● ' . esc_html__( 'Bloatwright: recording', 'bloatwright' ), 'href' => admin_url( 'tools.php?page=' . Menu::SLUG . '&tab=results&bloatwright_stop_browse=1' ), 'meta' => array( 'title' => __( 'Click to stop browse-and-profile and see results', 'bloatwright' ) ), ) ); } }