| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * Desktop Mode — Agents: persisted chat conversations. | |
| 3 | + * OpenStation — Agents: persisted chat conversations. | |
| 4 | 4 | * |
| 5 | 5 | * Each conversation is one post of the private `desktop_mode_chat` |
| 6 | 6 | * post type: `post_author` is the human who held the conversation, |
| 7 | 7 | * the messages live as JSON in `post_content`, the agent's user id in |
| @@ -21,27 +21,35 @@ | ||
| 21 | 21 | * GET /agents/conversations/:id — one conversation with messages |
| 22 | 22 | * PUT /agents/conversations/:id — replace messages ({messages}) |
| 23 | 23 | * DELETE /agents/conversations/:id — delete |
| 24 | 24 | * |
| 25 | - * @package WPDesktopMode | |
| 25 | + * @package OpenStation | |
| 26 | 26 | */ |
| 27 | 27 | |
| 28 | 28 | defined( 'ABSPATH' ) || exit; |
| 29 | 29 | |
| 30 | -/** Post type holding one conversation per post. */ | |
| 31 | -const DESKTOP_MODE_AGENT_CHAT_POST_TYPE = 'desktop_mode_chat'; | |
| 30 | +/** | |
| 31 | + * Post type holding one conversation per post. | |
| 32 | + * | |
| 33 | + * The VALUE keeps its pre-rebrand spelling on purpose: it is a | |
| 34 | + * persisted or externally-visible identifier, so renaming it would | |
| 35 | + * orphan data already written by live installs (or break a live | |
| 36 | + * URL). The mismatch between this constant's name and its value is | |
| 37 | + * deliberate — it is NOT a half-finished rename. | |
| 38 | + */ | |
| 39 | +const OPENSTATION_AGENT_CHAT_POST_TYPE = 'desktop_mode_chat'; | |
| 32 | 40 | |
| 33 | 41 | /** Newest conversations kept per user — creating past the cap prunes the oldest. */ |
| 34 | -const DESKTOP_MODE_AGENT_CONVERSATION_CAP = 100; | |
| 42 | +const OPENSTATION_AGENT_CONVERSATION_CAP = 100; | |
| 35 | 43 | |
| 36 | 44 | /** Newest messages kept per conversation — updates past the cap trim the oldest. */ |
| 37 | -const DESKTOP_MODE_AGENT_CONVERSATION_MESSAGE_CAP = 200; | |
| 45 | +const OPENSTATION_AGENT_CONVERSATION_MESSAGE_CAP = 200; | |
| 38 | 46 | |
| 39 | 47 | /** Stored per-message text cap. Wider than the runner's replay cap so long answers reload intact. */ |
| 40 | -const DESKTOP_MODE_AGENT_CONVERSATION_TEXT_CAP = 20000; | |
| 48 | +const OPENSTATION_AGENT_CONVERSATION_TEXT_CAP = 20000; | |
| 41 | 49 | |
| 42 | 50 | /** Characters of the last message shown as the sidebar's second line. */ |
| 43 | -const DESKTOP_MODE_AGENT_CONVERSATION_PREVIEW_CAP = 80; | |
| 51 | +const OPENSTATION_AGENT_CONVERSATION_PREVIEW_CAP = 80; | |
| 44 | 52 | |
| 45 | 53 | /** |
| 46 | 54 | * Entity kinds a message attachment may reference — mirrors the |
| 47 | 55 | * client's `DroppedEntityKind` and the drag-trigger config enum. |
| @@ -47,9 +55,9 @@ | ||
| 47 | 55 | * client's `DroppedEntityKind` and the drag-trigger config enum. |
| 48 | 56 | * |
| 49 | 57 | * @return string[] |
| 50 | 58 | */ |
| 51 | -function desktop_mode_agent_conversation_attachment_kinds() { | |
| 59 | +function openstation_agent_conversation_attachment_kinds() { | |
| 52 | 60 | return array( 'post', 'page', 'media', 'user', 'comment' ); |
| 53 | 61 | } |
| 54 | 62 | |
| 55 | 63 | /** |
| @@ -57,11 +65,11 @@ | ||
| 57 | 65 | * no front-end queries, no revisions; rows die with their author. |
| 58 | 66 | * |
| 59 | 67 | * @return void |
| 60 | 68 | */ |
| 61 | -function desktop_mode_agent_conversations_register_post_type() { | |
| 69 | +function openstation_agent_conversations_register_post_type() { | |
| 62 | 70 | register_post_type( |
| 63 | - DESKTOP_MODE_AGENT_CHAT_POST_TYPE, | |
| 71 | + OPENSTATION_AGENT_CHAT_POST_TYPE, | |
| 64 | 72 | array( |
| 65 | 73 | 'label' => __( 'Agent conversations', 'desktop-mode' ), |
| 66 | 74 | 'public' => false, |
| 67 | 75 | 'show_ui' => false, |
| @@ -74,9 +82,9 @@ | ||
| 74 | 82 | 'delete_with_user' => true, |
| 75 | 83 | ) |
| 76 | 84 | ); |
| 77 | 85 | } |
| 78 | -add_action( 'init', 'desktop_mode_agent_conversations_register_post_type', 5 ); | |
| 86 | +add_action( 'init', 'openstation_agent_conversations_register_post_type', 5 ); | |
| 79 | 87 | |
| 80 | 88 | /** |
| 81 | 89 | * Normalize caller-supplied messages for storage. |
| 82 | 90 | * |
| @@ -92,9 +100,9 @@ | ||
| 92 | 100 | * |
| 93 | 101 | * @param mixed $messages Incoming message rows. |
| 94 | 102 | * @return array<int, array<string, mixed>> |
| 95 | 103 | */ |
| 96 | -function desktop_mode_agent_conversation_sanitize_messages( $messages ) { | |
| 104 | +function openstation_agent_conversation_sanitize_messages( $messages ) { | |
| 97 | 105 | if ( ! is_array( $messages ) ) { |
| 98 | 106 | return array(); |
| 99 | 107 | } |
| 100 | 108 | |
| @@ -113,9 +121,9 @@ | ||
| 113 | 121 | } |
| 114 | 122 | |
| 115 | 123 | $entry = array( |
| 116 | 124 | 'role' => $role, |
| 117 | - 'text' => mb_substr( $text, 0, DESKTOP_MODE_AGENT_CONVERSATION_TEXT_CAP ), | |
| 125 | + 'text' => mb_substr( $text, 0, OPENSTATION_AGENT_CONVERSATION_TEXT_CAP ), | |
| 118 | 126 | 'at' => isset( $row['at'] ) ? (int) $row['at'] : 0, |
| 119 | 127 | ); |
| 120 | 128 | |
| 121 | 129 | // Call-to-action buttons survive with the message so a reopened |
| @@ -120,10 +128,10 @@ | ||
| 120 | 128 | |
| 121 | 129 | // Call-to-action buttons survive with the message so a reopened |
| 122 | 130 | // conversation still shows them (spent ones stay disabled via |
| 123 | 131 | // `ctaUsed`). Reuses the runner's sanitizer — same caps. |
| 124 | - if ( isset( $row['callToActions'] ) && function_exists( 'desktop_mode_agent_sanitize_call_to_actions' ) ) { | |
| 125 | - $ctas = desktop_mode_agent_sanitize_call_to_actions( $row['callToActions'] ); | |
| 132 | + if ( isset( $row['callToActions'] ) && function_exists( 'openstation_agent_sanitize_call_to_actions' ) ) { | |
| 133 | + $ctas = openstation_agent_sanitize_call_to_actions( $row['callToActions'] ); | |
| 126 | 134 | if ( ! empty( $ctas ) ) { |
| 127 | 135 | $entry['callToActions'] = $ctas; |
| 128 | 136 | } |
| 129 | 137 | } |
| @@ -131,9 +139,9 @@ | ||
| 131 | 139 | $entry['ctaUsed'] = true; |
| 132 | 140 | } |
| 133 | 141 | |
| 134 | 142 | if ( isset( $row['attachment'] ) ) { |
| 135 | - $attachment = desktop_mode_agent_conversation_sanitize_attachment( $row['attachment'] ); | |
| 143 | + $attachment = openstation_agent_conversation_sanitize_attachment( $row['attachment'] ); | |
| 136 | 144 | if ( null !== $attachment ) { |
| 137 | 145 | $entry['attachment'] = $attachment; |
| 138 | 146 | } |
| 139 | 147 | } |
| @@ -158,10 +166,10 @@ | ||
| 158 | 166 | |
| 159 | 167 | $clean[] = $entry; |
| 160 | 168 | } |
| 161 | 169 | |
| 162 | - if ( count( $clean ) > DESKTOP_MODE_AGENT_CONVERSATION_MESSAGE_CAP ) { | |
| 163 | - $clean = array_slice( $clean, -DESKTOP_MODE_AGENT_CONVERSATION_MESSAGE_CAP ); | |
| 170 | + if ( count( $clean ) > OPENSTATION_AGENT_CONVERSATION_MESSAGE_CAP ) { | |
| 171 | + $clean = array_slice( $clean, -OPENSTATION_AGENT_CONVERSATION_MESSAGE_CAP ); | |
| 164 | 172 | } |
| 165 | 173 | |
| 166 | 174 | return $clean; |
| 167 | 175 | } |
| @@ -177,15 +185,15 @@ | ||
| 177 | 185 | * |
| 178 | 186 | * @param mixed $raw Incoming attachment block. |
| 179 | 187 | * @return array<string, mixed>|null |
| 180 | 188 | */ |
| 181 | -function desktop_mode_agent_conversation_sanitize_attachment( $raw ) { | |
| 189 | +function openstation_agent_conversation_sanitize_attachment( $raw ) { | |
| 182 | 190 | if ( ! is_array( $raw ) ) { |
| 183 | 191 | return null; |
| 184 | 192 | } |
| 185 | 193 | $kind = isset( $raw['kind'] ) ? sanitize_key( (string) $raw['kind'] ) : ''; |
| 186 | 194 | $id = isset( $raw['id'] ) ? (int) $raw['id'] : 0; |
| 187 | - if ( $id <= 0 || ! in_array( $kind, desktop_mode_agent_conversation_attachment_kinds(), true ) ) { | |
| 195 | + if ( $id <= 0 || ! in_array( $kind, openstation_agent_conversation_attachment_kinds(), true ) ) { | |
| 188 | 196 | return null; |
| 189 | 197 | } |
| 190 | 198 | $title = isset( $raw['title'] ) ? trim( wp_strip_all_tags( (string) $raw['title'] ) ) : ''; |
| 191 | 199 | return array( |
| @@ -200,9 +208,9 @@ | ||
| 200 | 208 | * |
| 201 | 209 | * @param array $messages Sanitized messages. |
| 202 | 210 | * @return string |
| 203 | 211 | */ |
| 204 | -function desktop_mode_agent_conversation_title( array $messages ) { | |
| 212 | +function openstation_agent_conversation_title( array $messages ) { | |
| 205 | 213 | foreach ( $messages as $row ) { |
| 206 | 214 | if ( 'user' === $row['role'] ) { |
| 207 | 215 | return wp_html_excerpt( $row['text'], 60, '…' ); |
| 208 | 216 | } |
| @@ -222,9 +230,9 @@ | ||
| 222 | 230 | * |
| 223 | 231 | * @param array $messages Sanitized messages. |
| 224 | 232 | * @return string |
| 225 | 233 | */ |
| 226 | -function desktop_mode_agent_conversation_preview( array $messages ) { | |
| 234 | +function openstation_agent_conversation_preview( array $messages ) { | |
| 227 | 235 | $last = empty( $messages ) ? null : $messages[ count( $messages ) - 1 ]; |
| 228 | 236 | if ( ! is_array( $last ) ) { |
| 229 | 237 | return ''; |
| 230 | 238 | } |
| @@ -235,12 +243,12 @@ | ||
| 235 | 243 | $text = trim( (string) preg_replace( '/\s+/u', ' ', wp_strip_all_tags( (string) $last['text'] ) ) ); |
| 236 | 244 | if ( '' === $text ) { |
| 237 | 245 | return ''; |
| 238 | 246 | } |
| 239 | - if ( mb_strlen( $text ) <= DESKTOP_MODE_AGENT_CONVERSATION_PREVIEW_CAP ) { | |
| 247 | + if ( mb_strlen( $text ) <= OPENSTATION_AGENT_CONVERSATION_PREVIEW_CAP ) { | |
| 240 | 248 | return $text; |
| 241 | 249 | } |
| 242 | - return '…' . mb_substr( $text, -DESKTOP_MODE_AGENT_CONVERSATION_PREVIEW_CAP ); | |
| 250 | + return '…' . mb_substr( $text, -OPENSTATION_AGENT_CONVERSATION_PREVIEW_CAP ); | |
| 243 | 251 | } |
| 244 | 252 | |
| 245 | 253 | /** |
| 246 | 254 | * The conversation post for `$id` when it exists AND belongs to the |
| @@ -248,14 +256,14 @@ | ||
| 248 | 256 | * |
| 249 | 257 | * @param int $id Post id. |
| 250 | 258 | * @return WP_Post|null |
| 251 | 259 | */ |
| 252 | -function desktop_mode_agent_conversation_get_own( $id ) { | |
| 260 | +function openstation_agent_conversation_get_own( $id ) { | |
| 253 | 261 | $post = get_post( (int) $id ); |
| 254 | - if ( ! $post || DESKTOP_MODE_AGENT_CHAT_POST_TYPE !== $post->post_type ) { | |
| 262 | + if ( ! $post || OPENSTATION_AGENT_CHAT_POST_TYPE !== $post->post_type ) { | |
| 255 | 263 | return null; |
| 256 | 264 | } |
| 257 | - if ( (int) $post->post_author !== get_current_user_id() ) { | |
| 265 | + if ( get_current_user_id() !== (int) $post->post_author ) { | |
| 258 | 266 | return null; |
| 259 | 267 | } |
| 260 | 268 | return $post; |
| 261 | 269 | } |
| @@ -268,9 +276,9 @@ | ||
| 268 | 276 | * @param WP_Post $post Conversation post. |
| 269 | 277 | * @param bool $with_messages Include the decoded messages array. |
| 270 | 278 | * @return array<string, mixed> |
| 271 | 279 | */ |
| 272 | -function desktop_mode_agent_conversation_prepare( WP_Post $post, $with_messages = false ) { | |
| 280 | +function openstation_agent_conversation_prepare( WP_Post $post, $with_messages = false ) { | |
| 273 | 281 | $agent_id = (int) get_post_meta( $post->ID, '_desktop_mode_agent_chat_agent_id', true ); |
| 274 | 282 | $agent = $agent_id > 0 ? get_userdata( $agent_id ) : false; |
| 275 | 283 | |
| 276 | 284 | $messages = json_decode( (string) $post->post_content, true ); |
| @@ -284,13 +292,13 @@ | ||
| 284 | 292 | 'id' => (int) $post->ID, |
| 285 | 293 | 'agentId' => $agent_id, |
| 286 | 294 | 'agentName' => $agent ? $agent->display_name : __( 'Deleted agent', 'desktop-mode' ), |
| 287 | 295 | 'agentDescription' => $agent ? (string) get_user_meta( $agent_id, '_desktop_mode_agent_description', true ) : '', |
| 288 | - 'agentAvatarUrl' => function_exists( 'desktop_mode_agent_avatar_url' ) ? desktop_mode_agent_avatar_url() : '', | |
| 296 | + 'agentAvatarUrl' => function_exists( 'openstation_agent_avatar_url' ) ? openstation_agent_avatar_url( $agent_id ) : '', | |
| 289 | 297 | 'title' => (string) $post->post_title, |
| 290 | 298 | // Second sidebar line + who spoke last, so the list can say |
| 291 | 299 | // where each conversation got to instead of repeating its opener. |
| 292 | - 'preview' => desktop_mode_agent_conversation_preview( $messages ), | |
| 300 | + 'preview' => openstation_agent_conversation_preview( $messages ), | |
| 293 | 301 | 'lastRole' => is_array( $last ) && isset( $last['role'] ) ? (string) $last['role'] : '', |
| 294 | 302 | 'messageCount' => count( $messages ), |
| 295 | 303 | 'createdAt' => mysql2date( 'c', $post->post_date_gmt, false ), |
| 296 | 304 | 'updatedAt' => mysql2date( 'c', $post->post_modified_gmt, false ), |
| @@ -309,9 +317,9 @@ | ||
| 309 | 317 | * handler scope every read and write to the caller's rows. |
| 310 | 318 | * |
| 311 | 319 | * @return void |
| 312 | 320 | */ |
| 313 | -function desktop_mode_agent_conversations_register_routes() { | |
| 321 | +function openstation_agent_conversations_register_routes() { | |
| 314 | 322 | register_rest_route( |
| 315 | 323 | 'desktop-mode/v1', |
| 316 | 324 | '/agents/conversations', |
| 317 | 325 | array( |
| @@ -316,15 +324,15 @@ | ||
| 316 | 324 | '/agents/conversations', |
| 317 | 325 | array( |
| 318 | 326 | array( |
| 319 | 327 | 'methods' => WP_REST_Server::READABLE, |
| 320 | - 'callback' => 'desktop_mode_agents_rest_conversations_list', | |
| 321 | - 'permission_callback' => 'desktop_mode_agents_rest_invoke_permission', | |
| 328 | + 'callback' => 'openstation_agents_rest_conversations_list', | |
| 329 | + 'permission_callback' => 'openstation_agents_rest_invoke_permission', | |
| 322 | 330 | ), |
| 323 | 331 | array( |
| 324 | 332 | 'methods' => WP_REST_Server::CREATABLE, |
| 325 | - 'callback' => 'desktop_mode_agents_rest_conversations_create', | |
| 326 | - 'permission_callback' => 'desktop_mode_agents_rest_invoke_permission', | |
| 333 | + 'callback' => 'openstation_agents_rest_conversations_create', | |
| 334 | + 'permission_callback' => 'openstation_agents_rest_invoke_permission', | |
| 327 | 335 | 'args' => array( |
| 328 | 336 | 'agentId' => array( |
| 329 | 337 | 'type' => 'integer', |
| 330 | 338 | 'required' => true, |
| @@ -344,15 +352,15 @@ | ||
| 344 | 352 | '/agents/conversations/(?P<id>\d+)', |
| 345 | 353 | array( |
| 346 | 354 | array( |
| 347 | 355 | 'methods' => WP_REST_Server::READABLE, |
| 348 | - 'callback' => 'desktop_mode_agents_rest_conversations_get', | |
| 349 | - 'permission_callback' => 'desktop_mode_agents_rest_invoke_permission', | |
| 356 | + 'callback' => 'openstation_agents_rest_conversations_get', | |
| 357 | + 'permission_callback' => 'openstation_agents_rest_invoke_permission', | |
| 350 | 358 | ), |
| 351 | 359 | array( |
| 352 | 360 | 'methods' => WP_REST_Server::EDITABLE, |
| 353 | - 'callback' => 'desktop_mode_agents_rest_conversations_update', | |
| 354 | - 'permission_callback' => 'desktop_mode_agents_rest_invoke_permission', | |
| 361 | + 'callback' => 'openstation_agents_rest_conversations_update', | |
| 362 | + 'permission_callback' => 'openstation_agents_rest_invoke_permission', | |
| 355 | 363 | 'args' => array( |
| 356 | 364 | 'messages' => array( |
| 357 | 365 | 'type' => 'array', |
| 358 | 366 | 'required' => true, |
| @@ -360,15 +368,15 @@ | ||
| 360 | 368 | ), |
| 361 | 369 | ), |
| 362 | 370 | array( |
| 363 | 371 | 'methods' => WP_REST_Server::DELETABLE, |
| 364 | - 'callback' => 'desktop_mode_agents_rest_conversations_delete', | |
| 365 | - 'permission_callback' => 'desktop_mode_agents_rest_invoke_permission', | |
| 372 | + 'callback' => 'openstation_agents_rest_conversations_delete', | |
| 373 | + 'permission_callback' => 'openstation_agents_rest_invoke_permission', | |
| 366 | 374 | ), |
| 367 | 375 | ) |
| 368 | 376 | ); |
| 369 | 377 | } |
| 370 | -add_action( 'rest_api_init', 'desktop_mode_agent_conversations_register_routes' ); | |
| 378 | +add_action( 'rest_api_init', 'openstation_agent_conversations_register_routes' ); | |
| 371 | 379 | |
| 372 | 380 | /** |
| 373 | 381 | * GET /agents/conversations — the caller's conversations, most |
| 374 | 382 | * recently updated first, without message bodies (the list must stay |
| @@ -375,15 +383,15 @@ | ||
| 375 | 383 | * light; the sidebar fetches bodies on click). |
| 376 | 384 | * |
| 377 | 385 | * @return WP_REST_Response |
| 378 | 386 | */ |
| 379 | -function desktop_mode_agents_rest_conversations_list() { | |
| 387 | +function openstation_agents_rest_conversations_list() { | |
| 380 | 388 | $posts = get_posts( |
| 381 | 389 | array( |
| 382 | - 'post_type' => DESKTOP_MODE_AGENT_CHAT_POST_TYPE, | |
| 390 | + 'post_type' => OPENSTATION_AGENT_CHAT_POST_TYPE, | |
| 383 | 391 | 'post_status' => 'publish', |
| 384 | 392 | 'author' => get_current_user_id(), |
| 385 | - 'numberposts' => desktop_mode_agent_conversation_cap(), | |
| 393 | + 'numberposts' => openstation_agent_conversation_cap(), | |
| 386 | 394 | 'orderby' => 'modified', |
| 387 | 395 | 'order' => 'DESC', |
| 388 | 396 | 'suppress_filters' => false, |
| 389 | 397 | ) |
| @@ -389,9 +397,9 @@ | ||
| 389 | 397 | ) |
| 390 | 398 | ); |
| 391 | 399 | |
| 392 | 400 | return rest_ensure_response( |
| 393 | - array_map( 'desktop_mode_agent_conversation_prepare', $posts ) | |
| 401 | + array_map( 'openstation_agent_conversation_prepare', $posts ) | |
| 394 | 402 | ); |
| 395 | 403 | } |
| 396 | 404 | |
| 397 | 405 | /** |
| @@ -399,22 +407,22 @@ | ||
| 399 | 407 | * |
| 400 | 408 | * @param WP_REST_Request $request Request. |
| 401 | 409 | * @return WP_REST_Response|WP_Error |
| 402 | 410 | */ |
| 403 | -function desktop_mode_agents_rest_conversations_create( WP_REST_Request $request ) { | |
| 411 | +function openstation_agents_rest_conversations_create( WP_REST_Request $request ) { | |
| 404 | 412 | $agent_id = (int) $request['agentId']; |
| 405 | - if ( ! function_exists( 'desktop_mode_agent_is_agent' ) || ! desktop_mode_agent_is_agent( $agent_id ) ) { | |
| 413 | + if ( ! function_exists( 'openstation_agent_is_agent' ) || ! openstation_agent_is_agent( $agent_id ) ) { | |
| 406 | 414 | return new WP_Error( |
| 407 | - 'desktop_mode_agent_not_found', | |
| 415 | + 'openstation_agent_not_found', | |
| 408 | 416 | __( 'No agent with that id exists.', 'desktop-mode' ), |
| 409 | 417 | array( 'status' => 404 ) |
| 410 | 418 | ); |
| 411 | 419 | } |
| 412 | 420 | |
| 413 | - $messages = desktop_mode_agent_conversation_sanitize_messages( $request['messages'] ); | |
| 421 | + $messages = openstation_agent_conversation_sanitize_messages( $request['messages'] ); | |
| 414 | 422 | if ( empty( $messages ) ) { |
| 415 | 423 | return new WP_Error( |
| 416 | - 'desktop_mode_agent_conversation_empty', | |
| 424 | + 'openstation_agent_conversation_empty', | |
| 417 | 425 | __( 'A conversation needs at least one message.', 'desktop-mode' ), |
| 418 | 426 | array( 'status' => 400 ) |
| 419 | 427 | ); |
| 420 | 428 | } |
| @@ -420,12 +428,12 @@ | ||
| 420 | 428 | } |
| 421 | 429 | |
| 422 | 430 | $post_id = wp_insert_post( |
| 423 | 431 | array( |
| 424 | - 'post_type' => DESKTOP_MODE_AGENT_CHAT_POST_TYPE, | |
| 432 | + 'post_type' => OPENSTATION_AGENT_CHAT_POST_TYPE, | |
| 425 | 433 | 'post_status' => 'publish', |
| 426 | 434 | 'post_author' => get_current_user_id(), |
| 427 | - 'post_title' => desktop_mode_agent_conversation_title( $messages ), | |
| 435 | + 'post_title' => openstation_agent_conversation_title( $messages ), | |
| 428 | 436 | // JSON survives the insert-path unslashing only when slashed. |
| 429 | 437 | 'post_content' => wp_slash( (string) wp_json_encode( $messages ) ), |
| 430 | 438 | ), |
| 431 | 439 | true |
| @@ -434,12 +442,12 @@ | ||
| 434 | 442 | return $post_id; |
| 435 | 443 | } |
| 436 | 444 | update_post_meta( $post_id, '_desktop_mode_agent_chat_agent_id', $agent_id ); |
| 437 | 445 | |
| 438 | - desktop_mode_agent_conversations_prune( get_current_user_id() ); | |
| 446 | + openstation_agent_conversations_prune( get_current_user_id() ); | |
| 439 | 447 | |
| 440 | 448 | return rest_ensure_response( |
| 441 | - desktop_mode_agent_conversation_prepare( get_post( $post_id ), true ) | |
| 449 | + openstation_agent_conversation_prepare( get_post( $post_id ), true ) | |
| 442 | 450 | ); |
| 443 | 451 | } |
| 444 | 452 | |
| 445 | 453 | /** |
| @@ -447,15 +455,15 @@ | ||
| 447 | 455 | * |
| 448 | 456 | * @param WP_REST_Request $request Request. |
| 449 | 457 | * @return WP_REST_Response|WP_Error |
| 450 | 458 | */ |
| 451 | -function desktop_mode_agents_rest_conversations_get( WP_REST_Request $request ) { | |
| 452 | - $post = desktop_mode_agent_conversation_get_own( (int) $request['id'] ); | |
| 459 | +function openstation_agents_rest_conversations_get( WP_REST_Request $request ) { | |
| 460 | + $post = openstation_agent_conversation_get_own( (int) $request['id'] ); | |
| 453 | 461 | if ( ! $post ) { |
| 454 | - return desktop_mode_agent_conversation_not_found(); | |
| 462 | + return openstation_agent_conversation_not_found(); | |
| 455 | 463 | } |
| 456 | 464 | return rest_ensure_response( |
| 457 | - desktop_mode_agent_conversation_prepare( $post, true ) | |
| 465 | + openstation_agent_conversation_prepare( $post, true ) | |
| 458 | 466 | ); |
| 459 | 467 | } |
| 460 | 468 | |
| 461 | 469 | /** |
| @@ -465,18 +473,18 @@ | ||
| 465 | 473 | * |
| 466 | 474 | * @param WP_REST_Request $request Request. |
| 467 | 475 | * @return WP_REST_Response|WP_Error |
| 468 | 476 | */ |
| 469 | -function desktop_mode_agents_rest_conversations_update( WP_REST_Request $request ) { | |
| 470 | - $post = desktop_mode_agent_conversation_get_own( (int) $request['id'] ); | |
| 477 | +function openstation_agents_rest_conversations_update( WP_REST_Request $request ) { | |
| 478 | + $post = openstation_agent_conversation_get_own( (int) $request['id'] ); | |
| 471 | 479 | if ( ! $post ) { |
| 472 | - return desktop_mode_agent_conversation_not_found(); | |
| 480 | + return openstation_agent_conversation_not_found(); | |
| 473 | 481 | } |
| 474 | 482 | |
| 475 | - $messages = desktop_mode_agent_conversation_sanitize_messages( $request['messages'] ); | |
| 483 | + $messages = openstation_agent_conversation_sanitize_messages( $request['messages'] ); | |
| 476 | 484 | if ( empty( $messages ) ) { |
| 477 | 485 | return new WP_Error( |
| 478 | - 'desktop_mode_agent_conversation_empty', | |
| 486 | + 'openstation_agent_conversation_empty', | |
| 479 | 487 | __( 'A conversation needs at least one message.', 'desktop-mode' ), |
| 480 | 488 | array( 'status' => 400 ) |
| 481 | 489 | ); |
| 482 | 490 | } |
| @@ -483,9 +491,9 @@ | ||
| 483 | 491 | |
| 484 | 492 | $updated = wp_update_post( |
| 485 | 493 | array( |
| 486 | 494 | 'ID' => $post->ID, |
| 487 | - 'post_title' => desktop_mode_agent_conversation_title( $messages ), | |
| 495 | + 'post_title' => openstation_agent_conversation_title( $messages ), | |
| 488 | 496 | 'post_content' => wp_slash( (string) wp_json_encode( $messages ) ), |
| 489 | 497 | ), |
| 490 | 498 | true |
| 491 | 499 | ); |
| @@ -493,9 +501,9 @@ | ||
| 493 | 501 | return $updated; |
| 494 | 502 | } |
| 495 | 503 | |
| 496 | 504 | return rest_ensure_response( |
| 497 | - desktop_mode_agent_conversation_prepare( get_post( $post->ID ), true ) | |
| 505 | + openstation_agent_conversation_prepare( get_post( $post->ID ), true ) | |
| 498 | 506 | ); |
| 499 | 507 | } |
| 500 | 508 | |
| 501 | 509 | /** |
| @@ -503,12 +511,12 @@ | ||
| 503 | 511 | * |
| 504 | 512 | * @param WP_REST_Request $request Request. |
| 505 | 513 | * @return WP_REST_Response|WP_Error |
| 506 | 514 | */ |
| 507 | -function desktop_mode_agents_rest_conversations_delete( WP_REST_Request $request ) { | |
| 508 | - $post = desktop_mode_agent_conversation_get_own( (int) $request['id'] ); | |
| 515 | +function openstation_agents_rest_conversations_delete( WP_REST_Request $request ) { | |
| 516 | + $post = openstation_agent_conversation_get_own( (int) $request['id'] ); | |
| 509 | 517 | if ( ! $post ) { |
| 510 | - return desktop_mode_agent_conversation_not_found(); | |
| 518 | + return openstation_agent_conversation_not_found(); | |
| 511 | 519 | } |
| 512 | 520 | wp_delete_post( $post->ID, true ); |
| 513 | 521 | return rest_ensure_response( array( 'deleted' => true ) ); |
| 514 | 522 | } |
| @@ -518,11 +526,11 @@ | ||
| 518 | 526 | * "does not exist" and "not yours" so ids can't be probed. |
| 519 | 527 | * |
| 520 | 528 | * @return WP_Error |
| 521 | 529 | */ |
| 522 | -function desktop_mode_agent_conversation_not_found() { | |
| 530 | +function openstation_agent_conversation_not_found() { | |
| 523 | 531 | return new WP_Error( |
| 524 | - 'desktop_mode_agent_conversation_not_found', | |
| 532 | + 'openstation_agent_conversation_not_found', | |
| 525 | 533 | __( 'No conversation with that id exists.', 'desktop-mode' ), |
| 526 | 534 | array( 'status' => 404 ) |
| 527 | 535 | ); |
| 528 | 536 | } |
| @@ -531,9 +539,9 @@ | ||
| 531 | 539 | * The effective per-user conversation cap. |
| 532 | 540 | * |
| 533 | 541 | * @return int |
| 534 | 542 | */ |
| 535 | -function desktop_mode_agent_conversation_cap() { | |
| 543 | +function openstation_agent_conversation_cap() { | |
| 536 | 544 | /** |
| 537 | 545 | * Filters how many conversations are kept per user. Creating past |
| 538 | 546 | * the cap prunes the least recently updated rows. |
| 539 | 547 | * |
| @@ -538,9 +546,9 @@ | ||
| 538 | 546 | * the cap prunes the least recently updated rows. |
| 539 | 547 | * |
| 540 | 548 | * @param int $cap Maximum stored conversations per user. |
| 541 | 549 | */ |
| 542 | - return max( 1, (int) apply_filters( 'desktop_mode_agent_conversation_cap', DESKTOP_MODE_AGENT_CONVERSATION_CAP ) ); | |
| 550 | + return max( 1, (int) apply_filters( 'openstation_agent_conversation_cap', OPENSTATION_AGENT_CONVERSATION_CAP ) ); | |
| 543 | 551 | } |
| 544 | 552 | |
| 545 | 553 | /** |
| 546 | 554 | * Drop the user's oldest conversations beyond the cap. |
| @@ -547,13 +555,13 @@ | ||
| 547 | 555 | * |
| 548 | 556 | * @param int $user_id Owner. |
| 549 | 557 | * @return void |
| 550 | 558 | */ |
| 551 | -function desktop_mode_agent_conversations_prune( $user_id ) { | |
| 552 | - $cap = desktop_mode_agent_conversation_cap(); | |
| 559 | +function openstation_agent_conversations_prune( $user_id ) { | |
| 560 | + $cap = openstation_agent_conversation_cap(); | |
| 553 | 561 | $posts = get_posts( |
| 554 | 562 | array( |
| 555 | - 'post_type' => DESKTOP_MODE_AGENT_CHAT_POST_TYPE, | |
| 563 | + 'post_type' => OPENSTATION_AGENT_CHAT_POST_TYPE, | |
| 556 | 564 | 'post_status' => 'publish', |
| 557 | 565 | 'author' => (int) $user_id, |
| 558 | 566 | // One page past the cap is plenty — pruning runs on every create. |
| 559 | 567 | 'numberposts' => $cap + 10, |