id === Assets::HOOK || $screen->is_block_editor()) { return false; } return current_user_can(Menu::capability($this->access)); } /** * `admin_enqueue_scripts`: the launcher's few rules and the loader, with the two settings * objects the chat reads once the loader adds it (Assets::settings() and Assets::mount()). * `heartbeat` is a dependency because the loader keeps its nonce fresh on core's heartbeat from * the page's load, for the requests it signs before the first open (Kanboard #4526), and the * chat bundle it adds does too once it boots (resources/ts/nonce.ts); a script the loader adds * to the page by hand cannot declare a dependency of its own. wp_enqueue_media() is not * called: where the screen has not loaded the media library itself, * resources/ts/drawer-start.ts marks the drawer and this stylesheet hides the image button. */ public function enqueue(): void { if (!$this->wanted()) { return; } wp_enqueue_style(self::HANDLE, plugins_url('assets/css/alpaca-bot-drawer.css', ALPACA_BOT_FILE), ['dashicons'], Assets::version('assets/css/alpaca-bot-drawer.css')); wp_enqueue_script(self::HANDLE, plugins_url('assets/js/drawer.js', ALPACA_BOT_FILE), ['heartbeat'], Assets::version('assets/js/drawer.js'), true); wp_localize_script(self::HANDLE, 'alpacaBot', $this->assets->settings()); wp_localize_script(self::HANDLE, 'alpacaBotMount', $this->assets->mount()); } /** * `enqueue_block_editor_assets`: the chat as the block editor's own sidebar * (resources/ts/editor-start.ts), which mounts the fragment the drawer mounts, for the user the * drawer is for (Menu::capability(), as wanted() asks). Core fires this hook wherever it loads * a block editor, and the site editor, the widgets editor and the Customizer's widgets have no * post to edit, so only a screen whose base is `post` and that core says is a block editor * gets the sidebar; the classic editor gets the drawer instead (wanted()). * * The four `wp-*` dependencies are the globals editor.js reads off `window.wp` * (resources/ts/editor-start.ts), so it runs after the bundles that set them. `wp-editor` * depends on the other three already, and they are named anyway because the sidebar reads all * four and a dependency of a dependency is not a promise. `heartbeat` is here for the reason it * is on the loader (enqueue()). Both settings objects are the loader's, because the sidebar * mounts the chat the way the drawer does (resources/ts/mount.ts), and `alpacaBotMount` carries * one thing more: `conversation`, the conversation the drawer remembers * (Chat\UserPrefs::drawerConversation()), which the sidebar reopens and keeps up to date as the * drawer does, so the two are one memory (Kanboard #4527). The drawer reads it off the element * footer() prints, and a block editor screen gets no footer() (wanted()). It is user meta, * which the class docblock says is in memory by now. */ public function enqueueEditor(): void { $screen = function_exists('get_current_screen') ? get_current_screen() : null; if (!$screen instanceof \WP_Screen || $screen->base !== 'post' || !$screen->is_block_editor()) { return; } if (!current_user_can(Menu::capability($this->access))) { return; } wp_enqueue_script(self::EDITOR_HANDLE, plugins_url('assets/js/editor.js', ALPACA_BOT_FILE), ['wp-plugins', 'wp-editor', 'wp-element', 'wp-data', 'heartbeat'], Assets::version('assets/js/editor.js'), true); wp_localize_script(self::EDITOR_HANDLE, 'alpacaBot', $this->assets->settings()); wp_localize_script(self::EDITOR_HANDLE, 'alpacaBotMount', [...$this->assets->mount(), 'conversation' => (string) $this->prefs->drawerConversation((int) get_current_user_id())]); } /** * `admin_footer`: the launcher, and the empty element the loader fills on the first open. The * element also names what the chat's context chips show (View\Chat\Composer), which only this * request knows: the screen's id and page title, and on the classic editor the post. * * The title is the global `$title`, read as it stands when `admin_footer` fires. Whoever set * it last is not this method's concern: get_admin_page_title() answers with that global * whenever it is not empty, and otherwise walks the admin menus and stores what it finds in * it; admin-header.php calls it and strips the global's tags; and a screen may set the global * again after its header (update.php does, while it installs a plugin or a theme). So a * non-empty global is the title get_admin_page_title() would give at this point, and an empty * one is left empty, not walked for here, which is no title and so no screen chip. The title is * HTML (edit-comments.php, for one post's comments, puts the post's title between `“` * and `”`), so its tags are stripped and its entities decoded to make it text; the panel * cleans it for the chip by Context\CurrentScreenSource::screenFrom(), and the turn cleans it * again. * * The post is get_post() of the global the screen's own file set. On post.php that is the post * it loaded, which get_post() reads back from the object cache that load filled; on * post-new.php it is the auto-draft get_default_post_to_edit() made, which get_post() hands back * as it is. Whether it is a post the chip may name is the panel's question (View\Chat\Shell), * asked when the drawer is opened: not one the user may not edit, and not an auto-draft. */ public function footer(): void { if (!$this->wanted()) { return; } $userId = (int) get_current_user_id(); $screen = get_current_screen(); $title = is_string($GLOBALS['title'] ?? null) ? html_entity_decode(wp_strip_all_tags($GLOBALS['title']), ENT_QUOTES | ENT_HTML5, 'UTF-8') : ''; $post = $screen instanceof \WP_Screen && $screen->base === 'post' ? get_post() : null; printf( '' . '', esc_html__('Open the Alpaca Bot chat', 'alpaca-bot'), esc_attr__('Alpaca Bot chat', 'alpaca-bot'), $this->prefs->drawerOpen($userId) ? '1' : '0', (int) $this->prefs->drawerConversation($userId), esc_attr($screen instanceof \WP_Screen ? $screen->id : ''), esc_attr($title), (int) ($post instanceof \WP_Post ? $post->ID : 0), ); } }