1, 'business' => get_bloginfo( 'name' ), 'colour' => '#0F1E3A', 'colour_text' => '', // text on the brand colour: '' = chosen automatically 'position' => 'right', 'launcher_text' => '', 'welcome' => __( "Hi! Ask us anything, we're happy to help.", 'chatdozo' ), 'offline' => __( "We're away right now, but go ahead and write. We'll reply as soon as we're back.", 'chatdozo' ), 'prechat' => 'required', // required | optional | off 'privacy_text' => '', 'privacy_url' => '', 'operators' => array(), 'notify_email' => get_option( 'admin_email' ), 'hide_paths' => '', 'ignore_ips' => '', // addresses never counted as visitors (you, the office, a harvester), one per line, a trailing dot matches a range 'sound' => 1, 'hours_on' => 0, 'hours' => self::default_hours(), 'closed_line' => __( "We're closed right now and back at {time}. Leave a message and we'll reply then.", 'chatdozo' ), 'canned' => array(), 'retention_days' => 0, 'auto_transcript'=> 0, 'ask_rating' => 1, 'files' => 1, 'visitor_sound' => 1, 'widget_bg' => 'soft', 'widget_bg_url' => '', 'inbox_bg' => 'none', 'inbox_bg_url' => '', 'logo_url' => '', 'banner_url' => '', 'font' => 'manrope', // manrope | site 'greeter' => 0, // who fronts the welcome line: a user ID, or 0 the logo (Pro adds -1, the Assistant) 'hide_mobile' => 0, 'tease_delay' => 3, // seconds before the greeting card; 0 = never 'offset' => 22, // px from the edge of the screen 'words' => array(), // visitor-facing wording overrides, key => text ); return (array) apply_filters( 'chatdozo_default_settings', $d ); } /** Visitor-facing wording the site owner may change: key => array( default, label ). */ public static function words() { return array( 'online' => array( __( 'Online now', 'chatdozo' ), __( 'Status when someone is online', 'chatdozo' ) ), 'away' => array( __( "We'll reply by email", 'chatdozo' ), __( 'Status when nobody is online', 'chatdozo' ) ), 'closedNow' => array( __( 'Closed right now', 'chatdozo' ), __( 'Status outside business hours', 'chatdozo' ) ), 'placeholder' => array( __( 'Write a message', 'chatdozo' ), __( 'Message box placeholder', 'chatdozo' ) ), 'name' => array( __( 'Your name', 'chatdozo' ), __( 'Name field label', 'chatdozo' ) ), 'email' => array( __( 'Your email', 'chatdozo' ), __( 'Email field label', 'chatdozo' ) ), 'needBoth' => array( __( 'Please add your name and email so we can reply.', 'chatdozo' ), __( 'Asked for name and email', 'chatdozo' ) ), 'leave' => array( __( "We'll reply by email if you leave the page.", 'chatdozo' ), __( 'Reassurance under the message box', 'chatdozo' ) ), 'closed' => array( __( 'This chat was closed. Write again to start a new one.', 'chatdozo' ), __( 'After a chat is closed', 'chatdozo' ) ), 'rateQ' => array( __( 'How was this chat?', 'chatdozo' ), __( 'Rating question', 'chatdozo' ) ), 'rateUp' => array( __( 'Good', 'chatdozo' ), __( 'Rating: good', 'chatdozo' ) ), 'rateDown' => array( __( 'Not good', 'chatdozo' ), __( 'Rating: not good', 'chatdozo' ) ), 'rateThanks' => array( __( 'Thanks for letting us know.', 'chatdozo' ), __( 'After rating', 'chatdozo' ) ), 'copy' => array( __( 'Email me a copy of this chat', 'chatdozo' ), __( 'Transcript button', 'chatdozo' ) ), 'today' => array( __( 'Today', 'chatdozo' ), __( 'Date divider for today', 'chatdozo' ) ), 'newChat' => array( __( 'Start a new chat', 'chatdozo' ), __( 'Button after a chat is closed', 'chatdozo' ) ), ); } public static function default_hours() { $out = array(); foreach ( array( 'mon', 'tue', 'wed', 'thu', 'fri' ) as $d ) { $out[ $d ] = array( 'on' => 1, 'from' => '09:00', 'to' => '17:00' ); } foreach ( array( 'sat', 'sun' ) as $d ) { $out[ $d ] = array( 'on' => 0, 'from' => '09:00', 'to' => '17:00' ); } return $out; } public static function seed() { $cur = get_option( self::KEY ); if ( ! is_array( $cur ) ) { $cur = array(); } if ( isset( $cur['require_email'] ) && ! isset( $cur['prechat'] ) ) { $cur['prechat'] = $cur['require_email'] ? 'required' : 'optional'; } // 0.1.x $old = array( 'hearts' => 'soft', 'confetti' => 'soft', 'bubbles' => 'soft', 'topo' => 'waves', 'diagonal' => 'linen' ); // 0.2.1 wallpapers foreach ( array( 'widget_bg', 'inbox_bg' ) as $k ) { if ( isset( $cur[ $k ], $old[ $cur[ $k ] ] ) ) { $cur[ $k ] = $old[ $cur[ $k ] ]; } } if ( isset( $cur['offline'] ) && $cur['offline'] === "We're away right now. Leave your email and we'll reply as soon as we're back." ) { unset( $cur['offline'] ); } // 0.7.5: the email is asked for after the first message, not before $merged = array_merge( self::defaults(), $cur ); if ( $merged !== $cur ) { update_option( self::KEY, $merged, true ); } } public static function all() { $cur = get_option( self::KEY ); if ( ! is_array( $cur ) ) { $cur = array(); } return array_merge( self::defaults(), $cur ); } public static function get( $key, $fallback = null ) { $all = self::all(); return array_key_exists( $key, $all ) ? $all[ $key ] : $fallback; } /** Change one or more keys outside the settings form (a button that saves itself). */ public static function set( array $pairs ) { $all = self::all(); foreach ( $pairs as $k => $v ) { $all[ $k ] = $v; } update_option( self::KEY, $all, true ); return $all; } /** Save the settings form. The Pro edition adds its own fields through the chatdozo_save_settings filter. */ /** * Save the settings form. $in is the submitted form already run through sanitize_textarea_field() value by * value (Admin::save_settings() does that first); each field is then validated here for its own shape. */ public static function save( array $in ) { $d = self::defaults(); $out = self::all(); $before = array_map( 'intval', (array) ( $out['operators'] ?? array() ) ); $out['enabled'] = empty( $in['enabled'] ) ? 0 : 1; $out['business'] = sanitize_text_field( $in['business'] ?? $d['business'] ); if ( $out['business'] === '' ) { $out['business'] = $d['business']; } $out['colour'] = self::hex( $in['colour'] ?? '', $d['colour'] ); $mode = (string) ( $in['text_mode'] ?? 'auto' ); $tcol = self::hex( $in['colour_text'] ?? '', '' ); $out['colour_text'] = $mode === 'white' ? '#FFFFFF' : ( $mode === 'dark' ? '#0F1E3A' : ( $mode === 'custom' && $tcol ? $tcol : '' ) ); $out['position'] = ( ( $in['position'] ?? 'right' ) === 'left' ) ? 'left' : 'right'; $out['launcher_text'] = sanitize_text_field( $in['launcher_text'] ?? '' ); $out['welcome'] = sanitize_textarea_field( $in['welcome'] ?? $d['welcome'] ); $out['offline'] = sanitize_textarea_field( $in['offline'] ?? $d['offline'] ); $pc = $in['prechat'] ?? 'required'; $out['prechat'] = in_array( $pc, array( 'required', 'optional', 'off' ), true ) ? $pc : 'required'; $out['privacy_text'] = sanitize_text_field( $in['privacy_text'] ?? '' ); $out['privacy_url'] = esc_url_raw( (string) ( $in['privacy_url'] ?? '' ) ); $mail = sanitize_email( $in['notify_email'] ?? '' ); $out['notify_email'] = $mail ? $mail : $d['notify_email']; $out['hide_paths'] = sanitize_textarea_field( $in['hide_paths'] ?? '' ); $out['ignore_ips'] = self::clean_ips( (string) ( $in['ignore_ips'] ?? '' ) ); $out['sound'] = empty( $in['sound'] ) ? 0 : 1; $out['visitor_sound'] = empty( $in['visitor_sound'] ) ? 0 : 1; $out['logo_url'] = esc_url_raw( (string) ( $in['logo_url'] ?? '' ) ); $out['banner_url'] = esc_url_raw( (string) ( $in['banner_url'] ?? '' ) ); $out['font'] = ( ( $in['font'] ?? 'manrope' ) === 'site' ) ? 'site' : 'manrope'; $out['greeter'] = max( -1, (int) ( $in['greeter'] ?? 0 ) ); $out['hide_mobile'] = empty( $in['hide_mobile'] ) ? 0 : 1; $out['offset'] = max( 0, min( 120, (int) ( $in['offset'] ?? 22 ) ) ); $out['tease_delay'] = max( 0, min( 120, (int) ( $in['tease_delay'] ?? 3 ) ) ); $out['files'] = empty( $in['files'] ) ? 0 : 1; $out['ask_rating'] = empty( $in['ask_rating'] ) ? 0 : 1; $out['auto_transcript'] = empty( $in['auto_transcript'] ) ? 0 : 1; $keys = array_keys( Util::wallpaper_keys() ); $out['widget_bg'] = in_array( $in['widget_bg'] ?? 'none', $keys, true ) ? ( $in['widget_bg'] ?? 'none' ) : 'none'; $out['widget_bg_url'] = esc_url_raw( (string) ( $in['widget_bg_url'] ?? '' ) ); $out['inbox_bg'] = in_array( $in['inbox_bg'] ?? 'none', $keys, true ) ? ( $in['inbox_bg'] ?? 'none' ) : 'none'; $out['inbox_bg_url'] = esc_url_raw( (string) ( $in['inbox_bg_url'] ?? '' ) ); $out['retention_days'] = max( 0, min( 3650, (int) ( $in['retention_days'] ?? 0 ) ) ); // who answers $ops = isset( $in['operators'] ) && is_array( $in['operators'] ) ? array_map( 'intval', $in['operators'] ) : array(); $out['operators'] = array_values( array_filter( $ops ) ); foreach ( array( 'op_name' => array( 'chatdozo_name', 60 ), 'op_title' => array( 'chatdozo_title', 40 ) ) as $field => $meta ) { if ( isset( $in[ $field ] ) && is_array( $in[ $field ] ) ) { foreach ( $in[ $field ] as $uid => $v ) { $uid = (int) $uid; if ( ! $uid || ! current_user_can( 'edit_user', $uid ) ) { continue; } $v = mb_substr( trim( sanitize_text_field( (string) $v ) ), 0, $meta[1] ); if ( $v === '' ) { delete_user_meta( $uid, $meta[0] ); } else { update_user_meta( $uid, $meta[0], $v ); } } } } if ( isset( $in['op_avatar'] ) && is_array( $in['op_avatar'] ) ) { foreach ( $in['op_avatar'] as $uid => $url ) { $uid = (int) $uid; if ( ! $uid || ! current_user_can( 'edit_user', $uid ) ) { continue; } $url = esc_url_raw( (string) $url ); if ( $url === '' ) { delete_user_meta( $uid, 'chatdozo_avatar' ); } else { update_user_meta( $uid, 'chatdozo_avatar', $url ); } } } // wording $out['words'] = array(); if ( isset( $in['words'] ) && is_array( $in['words'] ) ) { foreach ( self::words() as $k => $w ) { $v = trim( sanitize_text_field( (string) ( $in['words'][ $k ] ?? '' ) ) ); if ( $v !== '' && $v !== $w[0] ) { $out['words'][ $k ] = mb_substr( $v, 0, 160 ); } } } // business hours $out['closed_line'] = sanitize_textarea_field( $in['closed_line'] ?? $d['closed_line'] ); $out['hours_on'] = empty( $in['hours_on'] ) ? 0 : 1; $hours = self::default_hours(); if ( isset( $in['hours'] ) && is_array( $in['hours'] ) ) { foreach ( $hours as $day => $row ) { $h = $in['hours'][ $day ] ?? array(); $hours[ $day ] = array( 'on' => empty( $h['on'] ) ? 0 : 1, 'from' => self::clock( $h['from'] ?? '09:00' ), 'to' => self::clock( $h['to'] ?? '17:00' ), ); } } $out['hours'] = $hours; // saved replies $canned = array(); if ( isset( $in['canned_title'] ) && is_array( $in['canned_title'] ) ) { foreach ( $in['canned_title'] as $i => $title ) { $title = sanitize_text_field( $title ); $text = sanitize_textarea_field( $in['canned_text'][ $i ] ?? '' ); if ( $title === '' && $text === '' ) { continue; } $canned[] = array( 'title' => $title !== '' ? $title : mb_substr( $text, 0, 30 ), 'text' => $text ); } } $out['canned'] = array_slice( $canned, 0, 100 ); $out = (array) apply_filters( 'chatdozo_save_settings', $out, $in, $d ); update_option( self::KEY, $out, true ); Roles::sync_operators( $out['operators'], $before ); return $out; } /** A hex colour from a form field: #6B1919, 6B1919 or #6B1 all work. $fallback when it is none of those. */ public static function hex( $raw, $fallback ) { $raw = trim( (string) $raw ); if ( preg_match( '/^[0-9a-f]{6}$/i', $raw ) ) { $raw = '#' . $raw; } if ( preg_match( '/^#?([0-9a-f])([0-9a-f])([0-9a-f])$/i', $raw, $m ) ) { $raw = '#' . $m[1] . $m[1] . $m[2] . $m[2] . $m[3] . $m[3]; } $col = sanitize_hex_color( $raw ); return $col ? strtoupper( $col ) : $fallback; } private static function clock( $v ) { $v = trim( (string) $v ); return preg_match( '/^([01]\d|2[0-3]):[0-5]\d$/', $v ) ? $v : '09:00'; } /* ---------- not visitors ---------- */ /** One address or prefix per line, nothing else. */ public static function clean_ips( $text ) { $out = array(); foreach ( preg_split( '/[\r\n,\s]+/', (string) $text ) as $l ) { $l = trim( $l ); if ( $l === '' ) { continue; } if ( filter_var( $l, FILTER_VALIDATE_IP ) || preg_match( '/^[0-9a-f:.]{2,}[.:]$/i', $l ) ) { $out[ $l ] = 1; } } return implode( "\n", array_keys( $out ) ); } /** Is this address on the ignore list? Exact, or a line ending in a dot or colon matches as a prefix. */ public static function ignored_ip( $ip ) { $ip = (string) $ip; if ( $ip === '' ) { return false; } foreach ( preg_split( '/\r\n|\r|\n/', (string) self::get( 'ignore_ips' ) ) as $l ) { $l = trim( $l ); if ( $l === '' ) { continue; } if ( $l === $ip ) { return true; } if ( ( substr( $l, -1 ) === '.' || substr( $l, -1 ) === ':' ) && strpos( $ip, $l ) === 0 ) { return true; } } return false; } /** Add one address to the ignore list (the "that's me" button). */ public static function ignore_ip( $ip ) { $all = self::all(); $all['ignore_ips'] = self::clean_ips( $all['ignore_ips'] . "\n" . $ip ); update_option( self::KEY, $all, true ); return $all['ignore_ips']; } /* ---------- business hours ---------- */ /** array( open => bool, next => 'tomorrow 9:00 am' or '', on => bool ) in the site's timezone. */ public static function hours_state() { $o = self::all(); if ( ! $o['hours_on'] ) { return array( 'open' => true, 'next' => '', 'on' => false ); } $tz = wp_timezone(); $now = new \DateTime( 'now', $tz ); $keys = array( 'mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun' ); $dow = (int) $now->format( 'N' ) - 1; $row = $o['hours'][ $keys[ $dow ] ]; $hm = $now->format( 'H:i' ); if ( $row['on'] && $hm >= $row['from'] && $hm < $row['to'] ) { return array( 'open' => true, 'next' => '', 'on' => true ); } // next opening for ( $i = 0; $i < 8; $i++ ) { $d = ( $dow + $i ) % 7; $r = $o['hours'][ $keys[ $d ] ]; if ( ! $r['on'] ) { continue; } if ( $i === 0 && $hm >= $r['from'] ) { continue; } $when = clone $now; $when->modify( '+' . $i . ' day' ); list( $h, $m ) = explode( ':', $r['from'] ); $when->setTime( (int) $h, (int) $m ); $label = ( $i === 0 ? __( 'today', 'chatdozo' ) : ( $i === 1 ? __( 'tomorrow', 'chatdozo' ) : wp_date( 'l', $when->getTimestamp(), $tz ) ) ) . ' ' . wp_date( get_option( 'time_format' ), $when->getTimestamp(), $tz ); return array( 'open' => false, 'next' => $label, 'on' => true ); } return array( 'open' => false, 'next' => '', 'on' => true ); } /** The greeter: the chosen person's face; else whatever the Pro edition offers for the choice (the Assistant); else the logo. */ public static function greeter() { $o = self::all(); $g = (int) $o['greeter']; if ( $g > 0 && Roles::can( $g ) ) { return array( 'face' => Util::avatar( $g, 96 ), 'title' => (string) get_user_meta( $g, 'chatdozo_title', true ), 'name' => Util::op_name( $g ) ); } $alt = apply_filters( 'chatdozo_greeter', null, $g ); if ( is_array( $alt ) && isset( $alt['face'], $alt['title'], $alt['name'] ) ) { return $alt; } return array( 'face' => '', 'title' => '', 'name' => '' ); } /** Public bits the widget needs. The Pro edition adds its own through chatdozo_widget_config. */ public static function widget_config() { $o = self::all(); $hs = self::hours_state(); $g = self::greeter(); $cfg = array( 'business' => $o['business'], 'colour' => $o['colour'], 'brand2' => Util::shade( $o['colour'], -0.18 ), 'text' => $o['colour_text'] !== '' ? $o['colour_text'] : Util::text_on( $o['colour'] ), 'logo' => $o['logo_url'], 'banner' => $o['banner_url'], 'font' => $o['font'], 'teaseDelay' => (int) $o['tease_delay'], 'offset' => (int) $o['offset'], 'face' => $g['face'], 'faceTitle' => $g['title'], 'faceName' => $g['name'], 'position' => $o['position'], 'launcher' => $o['launcher_text'], 'welcome' => $o['welcome'], 'offline' => $o['offline'], 'prechat' => $o['prechat'], 'privacy' => $o['privacy_text'], 'privacyUrl' => $o['privacy_url'], 'initial' => Util::initials( $o['business'] ), 'files' => (int) $o['files'], 'rating' => (int) $o['ask_rating'], 'copy' => 1, 'closed' => $hs['open'] ? '' : str_replace( '{time}', $hs['next'], $o['closed_line'] ), 'sound' => (int) $o['visitor_sound'], 'wall' => Util::wallpaper( $o['widget_bg'], $o['widget_bg_url'], $o['colour'] ), 'wallCustom' => $o['widget_bg'] === 'custom' ? 1 : 0, ); return (array) apply_filters( 'chatdozo_widget_config', $cfg, $o ); } }