advisor = $advisor; $this->access = $access; } /** * Hooks into WordPress. * * @return void */ public function register() { add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ), 20 ); } /** * Enqueues the panel. * * @return void */ public function enqueue() { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Only picks which screen this is. $page = isset( $_GET['page'] ) ? sanitize_key( wp_unslash( $_GET['page'] ) ) : ''; if ( ! in_array( $page, array( OverviewPage::SLUG, ReportsPage::SLUG ), true ) ) { return; } $advisor = call_user_func( $this->advisor ); $user = get_current_user_id(); if ( ! $advisor instanceof Advisor || ! $advisor->can_use( $user ) ) { return; } $sites = array(); foreach ( $this->access->visible_sites( $user ) as $site ) { $sites[] = array( 'id' => $site->id, 'name' => $site->display_name(), ); } if ( ! $sites ) { return; } wp_enqueue_style( 'turtlereport-advisor', TURTLEREPORT_URL . 'assets/css/advisor.css', array( 'turtlereport-shell' ), AdminPage::asset_version( 'assets/css/advisor.css' ) ); $provider = $advisor->provider(); $boot = array( 'sites' => $sites, 'screen' => OverviewPage::SLUG === $page ? 'monitor' : 'report', 'provider' => $provider ? $provider->label() : '', 'usage' => $advisor->usage_summary(), 'settings' => current_user_can( 'manage_options' ) ? SettingsPage::url() . '#tly-ai' : '', ); $js = 'window.turtlereportAdvisor = ' . wp_json_encode( $boot ) . ';'; // Before the screen's own script, so the issue panel knows to show "Ask AI". foreach ( array( 'turtlereport-overview', 'turtlereport-reports' ) as $handle ) { if ( wp_script_is( $handle, 'enqueued' ) ) { wp_add_inline_script( $handle, $js, 'before' ); return; } } } }