File size: 2,146 Bytes
f51c224
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
defined( 'ABSPATH' ) || exit;

/**
 * Admin enqueue for the Sara bubble UI.
 *
 * Injects the Sara floating button + panel CSS/JS on Fluent Boards,
 * Fluent Support, and SA-Orchestration admin pages. The bubble is
 * gated to manage_options users; the REST endpoint behind it is
 * gated identically.
 *
 * The frontend reads URL/hash to detect surface + ref, then POSTs to
 * /sa-orch/v1/sara/invoke. No DOM mutation outside the bubble itself.
 *
 * Pages where Sara appears:
 *   - page=fluent-boards     (any view inside the Boards SPA)
 *   - page=fluent-support    (any view inside the Support SPA)
 *   - page=sa-orchestration  (and its subpages)
 */
class SA_Orch_Sara_Admin {

    const HANDLE_JS  = 'sa-orch-sara-bubble';
    const HANDLE_CSS = 'sa-orch-sara-bubble';

    public static function register_hooks() {
        add_action( 'admin_enqueue_scripts', [ __CLASS__, 'maybe_enqueue' ] );
    }

    public static function maybe_enqueue( $hook ) {
        if ( ! current_user_can( 'manage_options' ) ) {
            return;
        }

        $page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : '';

        // Match the three primary admin surfaces. Sub-pages of sa-orchestration
        // (e.g., sa-orchestration-arbitrations) all start with the parent slug.
        $is_target = (
            $page === 'fluent-boards'
            || $page === 'fluent-support'
            || strpos( $page, 'sa-orchestration' ) === 0
        );
        if ( ! $is_target ) {
            return;
        }

        wp_enqueue_style(
            self::HANDLE_CSS,
            SA_ORCH_URL . 'assets/sara-bubble.css',
            [],
            SA_ORCH_VERSION
        );

        wp_enqueue_script(
            self::HANDLE_JS,
            SA_ORCH_URL . 'assets/sara-bubble.js',
            [],
            SA_ORCH_VERSION,
            true
        );

        wp_localize_script( self::HANDLE_JS, 'SA_ORCH_SARA', [
            'rest_url' => esc_url_raw( rest_url( 'sa-orch/v1/sara/invoke' ) ),
            'nonce'    => wp_create_nonce( 'wp_rest' ),
            'page'     => $page,
        ] );
    }
}