| @@ -18,10 +18,11 @@ | ||
| 18 | 18 | * the "Agent chat" native window (run-window.php), and the Agents |
| 19 | 19 | * section inside WP Explorer (my-wordpress.php). |
| 20 | 20 | * |
| 21 | 21 | * The module is opt-in behind the `agents` extended option — while |
| 22 | - * off, no user-meta registration, no REST routes, no window, no | |
| 23 | - * WP Explorer entity. The one exception is guard.php: see below. | |
| 22 | + * off, no user-meta registration, no REST routes and no chat window. | |
| 23 | + * Guard, WP Explorer discovery, and job cleanup load regardless of the flag. | |
| 24 | + * See below for why. | |
| 24 | 25 | * |
| 25 | 26 | * @package OpenStation |
| 26 | 27 | */ |
| 27 | 28 | |
| @@ -35,9 +36,89 @@ | ||
| 35 | 36 | * blocks are a property of the rows, not of the feature. |
| 36 | 37 | */ |
| 37 | 38 | require_once OPENSTATION_DIR . 'includes/agents/guard.php'; |
| 38 | 39 | |
| 40 | +// --------------------------------------------------------------------------- | |
| 41 | +// Always-available helpers | |
| 42 | +// | |
| 43 | +// The capability helpers and the avatar URL live here rather than in | |
| 44 | +// rest.php / identity.php because the WP Explorer integration needs | |
| 45 | +// all four to build its entity descriptor, and it now loads while the | |
| 46 | +// feature flag is off — when neither of those files is loaded. | |
| 47 | +// --------------------------------------------------------------------------- | |
| 48 | + | |
| 39 | 49 | /** |
| 50 | + * URL of the bot avatar as a real static file. | |
| 51 | + * | |
| 52 | + * The avatar MUST be a file URL, not the data URI: consumers routinely | |
| 53 | + * run avatar URLs through `esc_url()` (wp-admin's `get_avatar()`, the | |
| 54 | + * desktop user-tile renderer), and `data` is not in | |
| 55 | + * `wp_allowed_protocols()` — the data URI silently becomes an empty | |
| 56 | + * string and the avatar renders broken. | |
| 57 | + * | |
| 58 | + * With an agent id, this is that agent's own Mio portrait when one has | |
| 59 | + * been written. Without one, or before the face file exists, it is the | |
| 60 | + * shipped robot glyph, which is what every agent wore before faces. | |
| 61 | + * | |
| 62 | + * @param int $user_id Agent user id, or 0 for the generic glyph. | |
| 63 | + * @return string | |
| 64 | + */ | |
| 65 | +function openstation_agent_avatar_url( $user_id = 0 ) { | |
| 66 | + // Called with no argument for the generic case: the WP Explorer | |
| 67 | + // section icon, and the fallback for an agent that has no face yet. | |
| 68 | + // A section icon wearing one agent's face would be wrong. | |
| 69 | + if ( $user_id > 0 && function_exists( 'openstation_agent_face_url' ) ) { | |
| 70 | + $face = openstation_agent_face_url( (int) $user_id ); | |
| 71 | + if ( '' !== $face ) { | |
| 72 | + return $face; | |
| 73 | + } | |
| 74 | + } | |
| 75 | + return OPENSTATION_URL . 'assets/images/agent-avatar.svg'; | |
| 76 | +} | |
| 77 | + | |
| 78 | +/** | |
| 79 | + * Whether the current user can see agents. | |
| 80 | + * | |
| 81 | + * @return bool | |
| 82 | + */ | |
| 83 | +function openstation_agents_user_can_read() { | |
| 84 | + /** | |
| 85 | + * Filter whether the current user can read OpenStation agents. | |
| 86 | + * | |
| 87 | + * @param bool $can Default: `edit_posts` capability. | |
| 88 | + */ | |
| 89 | + return (bool) apply_filters( 'openstation_agents_user_can_read', current_user_can( 'edit_posts' ) ); | |
| 90 | +} | |
| 91 | + | |
| 92 | +/** | |
| 93 | + * Whether the current user can create / edit / delete agents. | |
| 94 | + * | |
| 95 | + * @return bool | |
| 96 | + */ | |
| 97 | +function openstation_agents_user_can_manage() { | |
| 98 | + /** | |
| 99 | + * Filter whether the current user can manage OpenStation agents. | |
| 100 | + * | |
| 101 | + * @param bool $can Default: `edit_users` capability. | |
| 102 | + */ | |
| 103 | + return (bool) apply_filters( 'openstation_agents_user_can_manage', current_user_can( 'edit_users' ) ); | |
| 104 | +} | |
| 105 | + | |
| 106 | +/** | |
| 107 | + * Whether the current user can invoke agents. | |
| 108 | + * | |
| 109 | + * @return bool | |
| 110 | + */ | |
| 111 | +function openstation_agents_user_can_invoke() { | |
| 112 | + /** | |
| 113 | + * Filter whether the current user can invoke OpenStation agents. | |
| 114 | + * | |
| 115 | + * @param bool $can Default: `edit_posts` capability. | |
| 116 | + */ | |
| 117 | + return (bool) apply_filters( 'openstation_agents_user_can_invoke', current_user_can( 'edit_posts' ) ); | |
| 118 | +} | |
| 119 | + | |
| 120 | +/** | |
| 40 | 121 | * Whether the Agents framework is enabled site-wide. |
| 41 | 122 | * |
| 42 | 123 | * Backed by the `agents` key of the extended options bundle |
| 43 | 124 | * (`openstation_get_extended_options()`), default off — opt-in. |
| @@ -60,8 +141,25 @@ | ||
| 60 | 141 | return (bool) apply_filters( 'openstation_agents_enabled', $enabled ); |
| 61 | 142 | } |
| 62 | 143 | |
| 63 | 144 | /** |
| 145 | + * The WP Explorer integration also loads unconditionally, so the Agents | |
| 146 | + * section is always discoverable: a site that has never turned the | |
| 147 | + * framework on still shows the tile, and the section explains how to | |
| 148 | + * switch it on instead of hiding the feature from the one person who | |
| 149 | + * could enable it. Everything inside renders inert while the flag is | |
| 150 | + * off — `openstation_agents_my_wordpress_window_args()` ships | |
| 151 | + * `enabled => false`, and the renderer disables every control and skips | |
| 152 | + * every fetch (the REST routes genuinely do not exist while off). | |
| 153 | + * | |
| 154 | + * Loaded after the helpers above, which it needs to build the entity | |
| 155 | + * descriptor and the section config. | |
| 156 | + */ | |
| 157 | +require_once OPENSTATION_DIR . 'includes/agents/my-wordpress.php'; | |
| 158 | +// Queued work must fail safely and retained data must expire even when disabled. | |
| 159 | +require_once OPENSTATION_DIR . 'includes/agents/jobs.php'; | |
| 160 | + | |
| 161 | +/** | |
| 64 | 162 | * Loads the agents module when the framework is enabled. |
| 65 | 163 | * |
| 66 | 164 | * @access private |
| 67 | 165 | */ |
| @@ -72,13 +170,14 @@ | ||
| 72 | 170 | |
| 73 | 171 | require_once OPENSTATION_DIR . 'includes/agents/store.php'; |
| 74 | 172 | require_once OPENSTATION_DIR . 'includes/agents/defaults.php'; |
| 75 | 173 | require_once OPENSTATION_DIR . 'includes/agents/identity.php'; |
| 174 | + require_once OPENSTATION_DIR . 'includes/agents/face.php'; | |
| 76 | 175 | require_once OPENSTATION_DIR . 'includes/agents/abilities.php'; |
| 77 | 176 | require_once OPENSTATION_DIR . 'includes/agents/runner.php'; |
| 177 | + require_once OPENSTATION_DIR . 'includes/agents/draft.php'; | |
| 78 | 178 | require_once OPENSTATION_DIR . 'includes/agents/rest.php'; |
| 79 | 179 | require_once OPENSTATION_DIR . 'includes/agents/conversations.php'; |
| 80 | 180 | require_once OPENSTATION_DIR . 'includes/agents/privacy.php'; |
| 81 | 181 | require_once OPENSTATION_DIR . 'includes/agents/run-window.php'; |
| 82 | - require_once OPENSTATION_DIR . 'includes/agents/my-wordpress.php'; | |
| 83 | 182 | } |
| 84 | 183 | add_action( 'plugins_loaded', 'openstation_agents_load', 5 ); |