array( Roles::class, 'can_rest' ) ); $admin = array( 'permission_callback' => function () { return current_user_can( 'manage_options' ); } ); register_rest_route( self::NS, '/op/duty', $op + array( 'methods' => 'POST', 'callback' => array( __CLASS__, 'duty' ) ) ); register_rest_route( self::NS, '/op/users', $admin + array( 'methods' => 'GET', 'callback' => array( __CLASS__, 'users' ) ) ); $get = array( 'list' => 'list_route', 'conversation' => 'conversation', 'export' => 'export' ); foreach ( $get as $r => $m ) { register_rest_route( self::NS, '/op/' . $r, $op + array( 'methods' => 'GET', 'callback' => array( __CLASS__, $m ) ) ); } $post = array( 'reply', 'note', 'status', 'typing', 'read', 'visitor', 'heartbeat', 'assign', 'tags', 'ban', 'unban', 'delete', 'upload', 'transcript', 'bulk' ); foreach ( $post as $r ) { register_rest_route( self::NS, '/op/' . $r, $op + array( 'methods' => 'POST', 'callback' => array( __CLASS__, $r ) ) ); } } public static function respond( $data, $code = 200 ) { $r = new WP_REST_Response( $data, $code ); $r->header( 'Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0' ); return $r; } public static function conv( WP_REST_Request $req ) { return Store::get( (int) $req->get_param( 'id' ) ); } /** Conversation row shaped for the inbox list and header. */ public static function row( array $c, $preview = null ) { $name = $c['visitor_name'] !== '' ? $c['visitor_name'] : __( 'Visitor', 'chatdozo' ); $seen = $c['visitor_seen_at'] ? ( time() - strtotime( $c['visitor_seen_at'] . ' UTC' ) ) : 99999; $assigned = ''; if ( (int) $c['assigned_to'] ) { $u = get_user_by( 'id', (int) $c['assigned_to'] ); $assigned = $u ? Util::op_name( $u->ID ) : ''; } $prev = ''; if ( $preview ) { $prev = $preview['body'] !== '' ? $preview['body'] : ( $preview['attachment_type'] === 'image' ? __( '📷 Photo', 'chatdozo' ) : __( '📎 File', 'chatdozo' ) ); } $tags = array_values( array_filter( array_map( 'trim', explode( ',', (string) $c['tags'] ) ) ) ); $row = array( 'id' => (int) $c['id'], 'status' => $c['status'], 'name' => $name, 'rawName' => $c['visitor_name'], 'initials' => Util::initials( $name ), 'email' => $c['visitor_email'], 'page' => Util::page_label( $c['page_title'], $c['page_url'] ), 'pageUrl' => $c['page_url'], 'referrer' => $c['referrer'], 'source' => $c['source'], 'device' => $c['device'], 'browser' => $c['browser'], 'os' => $c['os'], 'country' => $c['country'], 'region' => $c['region'], 'city' => $c['city'], 'ip' => $c['ip'], 'lang' => $c['lang'], 'tz' => $c['tz'], 'visits' => (int) $c['visits'], 'needs' => $c['status'] === 'open' && (int) $c['last_message_id'] > (int) $c['operator_read_id'], 'lastAt' => $c['last_message_at'] ? gmdate( 'c', strtotime( $c['last_message_at'] . ' UTC' ) ) : null, 'ago' => Util::ago( $c['last_message_at'] ?: $c['created_at'] ), 'lastFrom' => $c['last_from'], 'preview' => $prev, 'firstSeen' => wp_date( 'j M', strtotime( $c['created_at'] . ' UTC' ) ), 'here' => $seen < 90, 'typing' => ! empty( $c['visitor_typing_at'] ) && ( time() - strtotime( $c['visitor_typing_at'] . ' UTC' ) ) < 6, 'draft' => ( ! empty( $c['visitor_typing_at'] ) && ( time() - strtotime( $c['visitor_typing_at'] . ' UTC' ) ) < 20 ) ? $c['visitor_draft'] : '', 'visitorRead' => (int) $c['visitor_read_id'], 'assigned' => $assigned, 'assignedId' => (int) $c['assigned_to'], 'tags' => $tags, 'rating' => (int) $c['rating'], 'ratingNote' => $c['rating_note'], 'banned' => ! empty( $c['banned'] ), 'transcript' => ! empty( $c['transcript_at'] ), ); return (array) apply_filters( 'chatdozo_conv_row', $row, $c ); } public static function operators() { $out = array(); foreach ( Roles::operators() as $u ) { $out[] = array( 'id' => $u->ID, 'name' => Util::op_name( $u->ID ), 'avatar' => Util::avatar( $u->ID, 48 ) ); } return $out; } /** Answering now: put someone on or off duty. Anyone can set their own; administrators can set anyone's. */ public static function duty( WP_REST_Request $req ) { $uid = (int) $req->get_param( 'uid' ); $on = (bool) $req->get_param( 'on' ); if ( $uid !== get_current_user_id() && ! current_user_can( 'manage_options' ) ) { return self::respond( array( 'error' => 'who', 'message' => __( 'Only an administrator can change someone else.', 'chatdozo' ) ), 403 ); } if ( ! $uid || ! user_can( $uid, Roles::CAP ) ) { return self::respond( array( 'error' => 'who', 'message' => __( 'That person cannot answer chats.', 'chatdozo' ) ), 400 ); } Store::set_duty( $uid, $on ); return self::respond( array( 'ok' => true, 'away' => Store::is_away( get_current_user_id() ), 'duty' => Store::duty(), 'situation' => Store::situation() ) ); } /** Settings: people of one role, or a search, as rendered rows; the ones already answering are left out. */ public static function users( WP_REST_Request $req ) { $role = sanitize_key( (string) $req->get_param( 'role' ) ); $q = sanitize_text_field( (string) $req->get_param( 'q' ) ); if ( $role !== '' && ! isset( wp_roles()->get_names()[ $role ] ) ) { return self::respond( array( 'error' => 'role', 'message' => __( 'No such role.', 'chatdozo' ) ), 400 ); } $o = Options::all(); $have = array(); foreach ( Roles::answering( $o ) as $u ) { $have[ (int) $u->ID ] = true; } $rows = array(); $found = Roles::find( $role, $q, 60 ); foreach ( $found as $u ) { if ( isset( $have[ (int) $u->ID ] ) ) { continue; } ob_start(); Admin::person_row( $u, false, in_array( 'administrator', (array) $u->roles, true ) ); $rows[] = ob_get_clean(); } return self::respond( array( 'ok' => true, 'rows' => $rows, 'found' => count( $found ), 'more' => count( $found ) >= 60 ) ); } public static function list_route( WP_REST_Request $req ) { $tab = in_array( $req->get_param( 'tab' ), array( 'needs', 'mine', 'open', 'closed' ), true ) ? $req->get_param( 'tab' ) : 'needs'; $q = sanitize_text_field( (string) $req->get_param( 'q' ) ); $uid = get_current_user_id(); Store::heartbeat( $uid ); Store::prune( Options::get( 'retention_days' ) ); $rows = Store::listing( $tab, $q, 100, $uid ); $prev = Store::previews( wp_list_pluck( $rows, 'id' ) ); $out = array(); foreach ( $rows as $c ) { $out[] = self::row( $c, $prev[ (int) $c['id'] ] ?? null ); } $res = array( 'items' => $out, 'counts' => Store::counts( $uid ), 'online' => Store::online_names(), 'away' => Store::is_away( $uid ), 'situation' => Store::situation(), 'operators' => self::operators(), 'canned' => Options::get( 'canned' ), 'hours' => Options::hours_state(), ); return self::respond( (array) apply_filters( 'chatdozo_list_response', $res, $uid ) ); } public static function conversation( WP_REST_Request $req ) { $c = self::conv( $req ); if ( ! $c ) { return self::respond( array( 'error' => 'missing' ), 404 ); } $after = (int) $req->get_param( 'after' ); return self::respond( array( 'conv' => self::row( $c ), 'messages' => array_map( array( VisitorApi::class, 'fmt' ), Store::messages( $c['id'], $after ) ) ) ); } public static function reply( WP_REST_Request $req ) { $c = self::conv( $req ); if ( ! $c ) { return self::respond( array( 'error' => 'missing' ), 404 ); } $body = trim( wp_strip_all_tags( (string) $req->get_param( 'body' ) ) ); if ( $body === '' ) { return self::respond( array( 'error' => 'empty' ), 400 ); } if ( $c['status'] === 'closed' ) { Store::reopen( $c['id'] ); } $uid = get_current_user_id(); $msg = Store::add_message( $c['id'], 'operator', $body, $uid ); if ( ! (int) $c['assigned_to'] ) { Store::update( $c['id'], array( 'assigned_to' => $uid ) ); } $emailed = Mail::reply_to_visitor( $c, $msg, Util::op_name( $uid ) ); Store::heartbeat( $uid ); do_action( 'chatdozo_operator_message', $c, $msg ); $out = VisitorApi::fmt( $msg ); $out['emailed'] = $emailed; return self::respond( array( 'message' => $out ) ); } /** Private note: operators see it, the visitor never does. */ public static function note( WP_REST_Request $req ) { $c = self::conv( $req ); if ( ! $c ) { return self::respond( array( 'error' => 'missing' ), 404 ); } $body = trim( wp_strip_all_tags( (string) $req->get_param( 'body' ) ) ); if ( $body === '' ) { return self::respond( array( 'error' => 'empty' ), 400 ); } $msg = Store::add_message( $c['id'], 'note', $body, get_current_user_id() ); $out = VisitorApi::fmt( $msg ); $out['name'] = Util::op_name( get_current_user_id() ); return self::respond( array( 'message' => $out ) ); } /** Operator sends a file or image. */ public static function upload( WP_REST_Request $req ) { $c = self::conv( $req ); if ( ! $c ) { return self::respond( array( 'error' => 'missing' ), 404 ); } $files = $req->get_file_params(); if ( empty( $files['file'] ) ) { return self::respond( array( 'error' => 'none' ), 400 ); } $stored = Uploads::store( $files['file'] ); if ( is_wp_error( $stored ) ) { return self::respond( array( 'error' => 'file', 'message' => $stored->get_error_message() ), 400 ); } if ( $c['status'] === 'closed' ) { Store::reopen( $c['id'] ); } $uid = get_current_user_id(); $msg = Store::add_message( $c['id'], 'operator', (string) $req->get_param( 'body' ), $uid, array( 'attachment' => $stored['url'], 'attachment_type' => $stored['type'], 'attachment_name' => $stored['name'] ) ); $emailed = Mail::reply_to_visitor( $c, $msg, Util::op_name( $uid ) ); do_action( 'chatdozo_operator_message', $c, $msg ); $out = VisitorApi::fmt( $msg ); $out['emailed'] = $emailed; return self::respond( array( 'message' => $out ) ); } public static function status( WP_REST_Request $req ) { $c = self::conv( $req ); if ( ! $c ) { return self::respond( array( 'error' => 'missing' ), 404 ); } if ( $req->get_param( 'status' ) === 'closed' ) { Store::close( $c['id'] ); Store::update( $c['id'], array( 'operator_read_id' => (int) $c['last_message_id'] ) ); if ( Options::get( 'auto_transcript' ) && is_email( $c['visitor_email'] ) ) { Mail::transcript( Store::get( $c['id'] ) ); } } else { Store::reopen( $c['id'] ); } return self::respond( array( 'conv' => self::row( Store::get( $c['id'] ) ), 'counts' => Store::counts( get_current_user_id() ) ) ); } public static function typing( WP_REST_Request $req ) { $id = (int) $req->get_param( 'id' ); if ( $id ) { Store::update( $id, array( 'operator_typing_at' => Plugin::now(), 'operator_typing_user' => get_current_user_id() ) ); } return self::respond( array( 'ok' => true ) ); } public static function read( WP_REST_Request $req ) { $c = self::conv( $req ); if ( ! $c ) { return self::respond( array( 'ok' => false ) ); } $last = (int) $req->get_param( 'last' ); if ( $last > (int) $c['operator_read_id'] ) { Store::update( $c['id'], array( 'operator_read_id' => $last ) ); } return self::respond( array( 'ok' => true, 'counts' => Store::counts( get_current_user_id() ) ) ); } /** Operator edits the visitor's name or email. */ public static function visitor( WP_REST_Request $req ) { $c = self::conv( $req ); if ( ! $c ) { return self::respond( array( 'error' => 'missing' ), 404 ); } $up = array(); if ( $req->has_param( 'name' ) ) { $up['visitor_name'] = mb_substr( sanitize_text_field( (string) $req->get_param( 'name' ) ), 0, 120 ); } if ( $req->has_param( 'email' ) ) { $e = sanitize_email( (string) $req->get_param( 'email' ) ); $up['visitor_email'] = is_email( $e ) ? $e : ''; } if ( $up ) { Store::update( $c['id'], $up ); } return self::respond( array( 'conv' => self::row( Store::get( $c['id'] ) ) ) ); } /** Hand a chat to a colleague. */ public static function assign( WP_REST_Request $req ) { $c = self::conv( $req ); if ( ! $c ) { return self::respond( array( 'error' => 'missing' ), 404 ); } $to = (int) $req->get_param( 'user' ); if ( $to && ! user_can( $to, Roles::CAP ) ) { return self::respond( array( 'error' => 'user' ), 400 ); } Store::update( $c['id'], array( 'assigned_to' => $to ) ); if ( $to && $to !== get_current_user_id() ) { $u = get_user_by( 'id', $to ); /* translators: 1: the operator who handed over, 2: who it went to */ if ( $u ) { Store::add_message( $c['id'], 'note', sprintf( __( '%1$s handed this chat to %2$s.', 'chatdozo' ), Util::op_name( get_current_user_id() ), Util::op_name( $u->ID ) ), get_current_user_id() ); } } return self::respond( array( 'conv' => self::row( Store::get( $c['id'] ) ), 'counts' => Store::counts( get_current_user_id() ) ) ); } /** Tags on the visitor card. */ public static function tags( WP_REST_Request $req ) { $c = self::conv( $req ); if ( ! $c ) { return self::respond( array( 'error' => 'missing' ), 404 ); } $raw = (array) $req->get_param( 'tags' ); $tags = array(); foreach ( $raw as $t ) { $t = mb_substr( sanitize_text_field( (string) $t ), 0, 30 ); if ( $t !== '' && ! in_array( $t, $tags, true ) ) { $tags[] = $t; } } Store::update( $c['id'], array( 'tags' => implode( ',', array_slice( $tags, 0, 12 ) ) ) ); return self::respond( array( 'conv' => self::row( Store::get( $c['id'] ) ) ) ); } public static function ban( WP_REST_Request $req ) { $c = self::conv( $req ); if ( ! $c ) { return self::respond( array( 'error' => 'missing' ), 404 ); } Store::ban( $c ); Store::close( $c['id'] ); return self::respond( array( 'conv' => self::row( Store::get( $c['id'] ) ), 'counts' => Store::counts( get_current_user_id() ) ) ); } public static function unban( WP_REST_Request $req ) { $hash = preg_replace( '/[^a-f0-9]/', '', (string) $req->get_param( 'hash' ) ); $c = self::conv( $req ); if ( $c ) { $hash = $c['ip_hash']; } if ( $hash ) { Store::unban( $hash ); } return self::respond( array( 'ok' => true, 'conv' => $c ? self::row( Store::get( $c['id'] ) ) : null ) ); } public static function delete( WP_REST_Request $req ) { $c = self::conv( $req ); if ( ! $c ) { return self::respond( array( 'error' => 'missing' ), 404 ); } Store::delete( $c['id'] ); return self::respond( array( 'ok' => true, 'counts' => Store::counts( get_current_user_id() ) ) ); } /** One action on several conversations at once: delete, close, reopen, or take (assign to me). */ public static function bulk( WP_REST_Request $req ) { $action = (string) $req->get_param( 'action' ); if ( ! in_array( $action, array( 'delete', 'close', 'reopen', 'take' ), true ) ) { return self::respond( array( 'error' => 'action' ), 400 ); } $ids = array_values( array_unique( array_filter( array_map( 'intval', (array) $req->get_param( 'ids' ) ) ) ) ); if ( ! $ids ) { return self::respond( array( 'error' => 'ids' ), 400 ); } $ids = array_slice( $ids, 0, 200 ); $uid = get_current_user_id(); $done = 0; foreach ( $ids as $id ) { $c = Store::get( $id ); if ( ! $c ) { continue; } if ( $action === 'delete' ) { Store::delete( $id ); } elseif ( $action === 'close' ) { if ( $c['status'] !== 'closed' ) { Store::close( $id ); } Store::update( $id, array( 'operator_read_id' => (int) $c['last_message_id'] ) ); } elseif ( $action === 'reopen' ) { if ( $c['status'] === 'closed' ) { Store::reopen( $id ); } } else { Store::update( $id, array( 'assigned_to' => $uid ) ); } $done++; } return self::respond( array( 'ok' => true, 'done' => $done, 'counts' => Store::counts( $uid ) ) ); } /** Email the visitor a copy. */ public static function transcript( WP_REST_Request $req ) { $c = self::conv( $req ); if ( ! $c ) { return self::respond( array( 'error' => 'missing' ), 404 ); } if ( ! is_email( $c['visitor_email'] ) ) { return self::respond( array( 'ok' => false, 'error' => 'email' ) ); } return self::respond( array( 'ok' => (bool) Mail::transcript( $c ) ) ); } /** Plain-text download of one conversation, private notes included. */ public static function export( WP_REST_Request $req ) { $c = self::conv( $req ); if ( ! $c ) { return self::respond( array( 'error' => 'missing' ), 404 ); } // The inbox turns this into a .txt download in the browser; nothing is printed as a page. return self::respond( array( 'ok' => true, 'name' => 'chat-' . (int) $c['id'] . '.txt', 'text' => Mail::transcript_text( $c, true ) ) ); } public static function heartbeat( WP_REST_Request $req ) { $uid = get_current_user_id(); $away = $req->has_param( 'away' ) ? (bool) $req->get_param( 'away' ) : null; Store::heartbeat( $uid, $away ); $out = array( 'ok' => true, 'counts' => Store::counts( $uid ), 'away' => Store::is_away( $uid ), 'online' => Store::online_names(), 'situation' => Store::situation() ); if ( $req->get_param( 'duty' ) ) { $out['duty'] = Store::duty(); } // the Settings page asks for the whole team so its Answering now strip follows every switch return self::respond( $out ); } }