'__return_true', 'methods' => 'POST' ); foreach ( array( 'start', 'send', 'poll', 'read', 'rate', 'transcript', 'upload' ) as $r ) { register_rest_route( self::NS, '/v/' . $r, $pub + array( 'callback' => array( __CLASS__, $r ) ) ); } } /* ---------- helpers ---------- */ /** Is someone (or something, in the Pro edition) there to answer this conversation, or a new one when null? */ public static function online( $conv = null ) { return (bool) apply_filters( 'chatdozo_visitor_online', Store::online(), $conv ); } 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' ); $r->header( 'X-Accel-Expires', '0' ); return $r; } public static function token( WP_REST_Request $req ) { $t = strtolower( (string) $req->get_param( 'token' ) ); return preg_match( '/^[a-f0-9]{40}$/', $t ) ? $t : ''; } public static function ctx( WP_REST_Request $req ) { return array( 'page_url' => esc_url_raw( (string) $req->get_param( 'page' ) ), 'page_title' => mb_substr( sanitize_text_field( (string) $req->get_param( 'title' ) ), 0, 200 ), 'referrer' => esc_url_raw( (string) $req->get_param( 'ref' ) ), 'tz' => preg_replace( '/[^A-Za-z0-9_\/\-+]/', '', (string) $req->get_param( 'tz' ) ), 'ua' => isset( $_SERVER['HTTP_USER_AGENT'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ) : '', ); } /** One message as the widget and the inbox see it. Senders the free edition does not know appear as the business. */ public static function fmt( array $m ) { $name = ''; $avatar = ''; $title = ''; if ( $m['sender'] === 'operator' && (int) $m['user_id'] ) { $u = get_user_by( 'id', (int) $m['user_id'] ); if ( $u ) { $name = Util::op_name( $u->ID ); $avatar = Util::avatar( $u->ID, 64 ); $title = (string) get_user_meta( $u->ID, 'chatdozo_title', true ); } } $known = in_array( $m['sender'], array( 'visitor', 'operator', 'note', 'system' ), true ); $out = array( 'id' => (int) $m['id'], 'from' => $known ? $m['sender'] : 'operator', 'body' => $m['body'], 'at' => gmdate( 'c', strtotime( $m['created_at'] . ' UTC' ) ), 'name' => $name, 'title' => $title, 'avatar' => $avatar, 'file' => $m['attachment'] ? array( 'url' => $m['attachment'], 'type' => $m['attachment_type'], 'name' => $m['attachment_name'] ) : null, 'emailed' => ! empty( $m['emailed'] ), ); return (array) apply_filters( 'chatdozo_fmt_message', $out, $m ); } public static function state( array $conv ) { $state = array( 'id' => (int) $conv['id'], 'status' => $conv['status'], 'name' => $conv['visitor_name'], 'email' => $conv['visitor_email'], 'online' => self::online( $conv ), 'people' => Store::online(), 'operatorTyping' => ! empty( $conv['operator_typing_at'] ) && ( time() - strtotime( $conv['operator_typing_at'] . ' UTC' ) ) < 6, 'operatorRead' => (int) $conv['operator_read_id'], 'rating' => (int) $conv['rating'], 'transcript' => ! empty( $conv['transcript_at'] ), 'invited' => self::invited( $conv ), ); return (array) apply_filters( 'chatdozo_visitor_state', $state, $conv ); } /** A person on the team opened this chat: no name-and-email form in the way of the reply. */ public static function invited( array $conv ) { if ( (int) $conv['last_message_id'] <= 0 ) { return false; } return Store::first_sender( $conv['id'] ) === 'operator'; } public static function blocked( $token ) { return Store::is_banned( $token ); } /* ---------- endpoints ---------- */ /** Open the panel: find the visitor's conversation, return history. No conversation is created here. */ public static function start( WP_REST_Request $req ) { if ( ! Options::get( 'enabled' ) ) { return self::respond( array( 'error' => 'off' ), 403 ); } $token = self::token( $req ); if ( $token === '' ) { $token = sha1( wp_generate_password( 40, true, true ) . microtime( true ) . wp_salt() ); } if ( self::blocked( $token ) ) { return self::respond( array( 'token' => $token, 'banned' => true ) ); } if ( ! Util::allow( 'start|' . Util::ip_hash(), 60, 300 ) ) { return self::respond( array( 'error' => 'slow' ), 429 ); } $conv = Store::for_token( $token ); $ctx = self::ctx( $req ); $prev = $conv ? null : Store::last_for_token( $token ); // name/email from an older chat if ( ! $conv ) { return self::respond( array( 'token' => $token, 'conv' => null, 'online' => self::online( null ), 'people' => Store::online(), 'known' => $prev ? array( 'name' => $prev['visitor_name'], 'email' => $prev['visitor_email'] ) : null, 'messages' => array(), ) ); } $up = array( 'visitor_seen_at' => Plugin::now() ); if ( $ctx['page_url'] ) { $up['page_url'] = $ctx['page_url']; $up['page_title'] = $ctx['page_title']; } Store::update( $conv['id'], $up ); return self::respond( array( 'token' => $token, 'conv' => self::state( $conv ), 'online' => self::online( $conv ), 'people' => Store::online(), 'messages' => array_map( array( __CLASS__, 'fmt' ), Store::messages( $conv['id'], 0, 200, true ) ), ) ); } /** Shared by send and upload: find or make the conversation, apply name/email, enforce the pre-chat rule. */ private static function prepare( WP_REST_Request $req, $token, $has_body ) { $name = mb_substr( sanitize_text_field( (string) $req->get_param( 'name' ) ), 0, 120 ); $mail = sanitize_email( (string) $req->get_param( 'email' ) ); $conv = Store::for_token( $token ); $mode = Options::get( 'prechat' ); if ( $mode === 'required' && $conv && self::invited( $conv ) ) { $mode = 'optional'; } // they are answering us: no form wall if ( $mode === 'required' ) { $known_name = ( $conv && $conv['visitor_name'] !== '' ) || $name !== ''; $known_mail = ( $conv && is_email( $conv['visitor_email'] ) ) || is_email( $mail ); if ( ! $known_name || ! $known_mail ) { return new \WP_Error( 'prechat', __( 'Please add your name and email first.', 'chatdozo' ) ); } } if ( ! $has_body && $name === '' && $mail === '' ) { return new \WP_Error( 'empty', '' ); } $ctx = self::ctx( $req ); $started = false; if ( ! $conv ) { $conv = Store::create( $token, $ctx ); Store::update( $conv['id'], array( 'visits' => Store::visits_for_token( $token ) ) ); $started = true; } elseif ( $conv['status'] === 'closed' ) { Store::reopen( $conv['id'] ); Store::update( $conv['id'], array( 'assigned_to' => 0 ) ); $started = true; } $up = array( 'visitor_seen_at' => Plugin::now() ); if ( $name !== '' ) { $up['visitor_name'] = $name; } if ( is_email( $mail ) ) { $up['visitor_email'] = $mail; } if ( $ctx['page_url'] ) { $up['page_url'] = $ctx['page_url']; $up['page_title'] = $ctx['page_title']; } Store::update( $conv['id'], $up ); $conv = Store::get( $conv['id'] ); if ( $started ) { do_action( 'chatdozo_conversation_started', $conv ); $conv = Store::get( $conv['id'] ); } return $conv; } /** A visitor wrote (text or a file): the first line in, the alert if nobody picks it up, and the answer for the widget. */ private static function after_visitor_message( array $conv, WP_REST_Request $req, $make_message ) { do_action( 'chatdozo_before_visitor_message', $conv, $req ); // the Pro edition may put an automatic line in first $msg = $make_message( $conv ); $conv = Store::get( $conv['id'] ); $handled = (bool) apply_filters( 'chatdozo_visitor_message_handled', false, $conv, $msg ); if ( ! $handled ) { self::alert( $conv, $msg ); } do_action( 'chatdozo_visitor_message', $conv, $msg ); $out = array( 'conv' => self::state( $conv ), 'message' => self::fmt( $msg ) ); return (array) apply_filters( 'chatdozo_send_response', $out, $conv, $msg, $handled ); } public static function send( WP_REST_Request $req ) { if ( ! Options::get( 'enabled' ) ) { return self::respond( array( 'error' => 'off' ), 403 ); } $token = self::token( $req ); if ( $token === '' ) { return self::respond( array( 'error' => 'token' ), 400 ); } if ( self::blocked( $token ) ) { return self::respond( array( 'banned' => true ) ); } if ( ! Util::allow( 'send|' . $token, 30, 300 ) ) { return self::respond( array( 'error' => 'slow' ), 429 ); } $body = trim( wp_strip_all_tags( (string) $req->get_param( 'body' ) ) ); $conv = self::prepare( $req, $token, $body !== '' ); if ( is_wp_error( $conv ) ) { return self::respond( array( 'error' => $conv->get_error_code(), 'message' => $conv->get_error_message() ), 400 ); } if ( $body === '' ) { return self::respond( array( 'conv' => self::state( $conv ) ) ); } // only a name or an email was saved return self::respond( self::after_visitor_message( $conv, $req, function ( $c ) use ( $body ) { return Store::add_message( $c['id'], 'visitor', $body ); } ) ); } /** A visitor message nobody is answering live: the email alert, and (Pro) the phone ping. */ public static function alert( array $conv, array $msg ) { Mail::alert_operators( $conv, $msg ); do_action( 'chatdozo_visitor_alert', $conv, $msg ); } /** Multipart: file + the usual fields. Makes one message carrying the file. */ public static function upload( WP_REST_Request $req ) { if ( ! Options::get( 'enabled' ) || ! Options::get( 'files' ) ) { return self::respond( array( 'error' => 'off' ), 403 ); } $token = self::token( $req ); if ( $token === '' ) { return self::respond( array( 'error' => 'token' ), 400 ); } if ( self::blocked( $token ) ) { return self::respond( array( 'banned' => true ) ); } if ( ! Util::allow( 'upload|' . $token, 10, 600 ) ) { return self::respond( array( 'error' => 'slow' ), 429 ); } $files = $req->get_file_params(); if ( empty( $files['file'] ) ) { return self::respond( array( 'error' => 'none' ), 400 ); } $conv = self::prepare( $req, $token, true ); if ( is_wp_error( $conv ) ) { return self::respond( array( 'error' => $conv->get_error_code(), 'message' => $conv->get_error_message() ), 400 ); } $stored = Uploads::store( $files['file'] ); if ( is_wp_error( $stored ) ) { return self::respond( array( 'error' => 'file', 'message' => $stored->get_error_message() ), 400 ); } $caption = trim( wp_strip_all_tags( (string) $req->get_param( 'body' ) ) ); return self::respond( self::after_visitor_message( $conv, $req, function ( $c ) use ( $caption, $stored ) { return Store::add_message( $c['id'], 'visitor', $caption, 0, array( 'attachment' => $stored['url'], 'attachment_type' => $stored['type'], 'attachment_name' => $stored['name'] ) ); } ) ); } public static function poll( WP_REST_Request $req ) { $token = self::token( $req ); if ( $token === '' ) { return self::respond( array( 'error' => 'token' ), 400 ); } if ( self::blocked( $token ) ) { return self::respond( array( 'banned' => true ) ); } $conv = Store::for_token( $token ); if ( ! $conv ) { return self::respond( array( 'gone' => true, 'online' => self::online( null ), 'people' => Store::online() ) ); } $after = (int) $req->get_param( 'after' ); $up = array( 'visitor_seen_at' => Plugin::now() ); if ( $req->get_param( 'typing' ) ) { $up['visitor_typing_at'] = Plugin::now(); } if ( $req->has_param( 'draft' ) ) { $up['visitor_draft'] = mb_substr( sanitize_text_field( (string) $req->get_param( 'draft' ) ), 0, 500 ); } $ctx = self::ctx( $req ); if ( $ctx['page_url'] && $ctx['page_url'] !== $conv['page_url'] ) { $up['page_url'] = $ctx['page_url']; $up['page_title'] = $ctx['page_title']; } Store::update( $conv['id'], $up ); return self::respond( array( 'conv' => self::state( $conv ), 'messages' => array_map( array( __CLASS__, 'fmt' ), Store::messages( $conv['id'], $after, 200, true ) ) ) ); } public static function read( WP_REST_Request $req ) { $token = self::token( $req ); $conv = $token ? Store::for_token( $token ) : null; if ( ! $conv ) { return self::respond( array( 'ok' => false ) ); } $id = (int) $req->get_param( 'id' ); if ( $id > (int) $conv['visitor_read_id'] ) { Store::update( $conv['id'], array( 'visitor_read_id' => $id, 'visitor_seen_at' => Plugin::now() ) ); } return self::respond( array( 'ok' => true ) ); } /** Thumbs up (1) or down (2) with an optional line, after a chat is closed. */ public static function rate( WP_REST_Request $req ) { $token = self::token( $req ); $conv = $token ? Store::last_for_token( $token ) : null; if ( ! $conv ) { return self::respond( array( 'ok' => false ) ); } $r = (int) $req->get_param( 'rating' ); if ( ! in_array( $r, array( 1, 2 ), true ) ) { return self::respond( array( 'ok' => false ) ); } Store::update( $conv['id'], array( 'rating' => $r, 'rating_note' => mb_substr( sanitize_text_field( (string) $req->get_param( 'note' ) ), 0, 500 ) ) ); return self::respond( array( 'ok' => true ) ); } /** Email the visitor the whole conversation. */ public static function transcript( WP_REST_Request $req ) { $token = self::token( $req ); $conv = $token ? Store::last_for_token( $token ) : null; if ( ! $conv ) { return self::respond( array( 'ok' => false ) ); } $mail = sanitize_email( (string) $req->get_param( 'email' ) ); if ( is_email( $mail ) && $mail !== $conv['visitor_email'] ) { Store::update( $conv['id'], array( 'visitor_email' => $mail ) ); $conv['visitor_email'] = $mail; } if ( ! is_email( $conv['visitor_email'] ) ) { return self::respond( array( 'ok' => false, 'error' => 'email' ) ); } if ( ! Util::allow( 'transcript|' . $token, 3, 3600 ) ) { return self::respond( array( 'ok' => false, 'error' => 'slow' ) ); } $ok = Mail::transcript( $conv ); return self::respond( array( 'ok' => (bool) $ok ) ); } }