0.179 ? '#141413' : '#FFFFFF'; // the point where black text beats white for contrast
}
/**
* An operator's photo: the one chosen in Chatdozo (a built-in face or an upload), or '' when they have none yet.
* Nothing is fetched from Gravatar or any other outside service: visitors' browsers only ever load this site.
*/
public static function avatar( $uid, $size = 96 ) {
$uid = (int) $uid;
if ( ! $uid ) { return ''; }
$own = get_user_meta( $uid, 'chatdozo_avatar', true );
return $own ? esc_url_raw( $own ) : '';
}
/** The name visitors see for an operator: the chat name they chose, else their WordPress name. */
public static function op_name( $uid ) {
$uid = (int) $uid;
$own = trim( (string) get_user_meta( $uid, 'chatdozo_name', true ) );
if ( $own !== '' ) { return $own; }
$u = get_userdata( $uid );
return $u ? $u->display_name : '';
}
/** The grey "nobody yet" silhouette. */
public static function blank_face() {
return CHATDOZO_URL . 'assets/avatars/blank.svg';
}
/** The built-in faces: key => URL. */
public static function faces() {
$out = array();
for ( $i = 1; $i <= 16; $i++ ) { $k = sprintf( 'face-%02d', $i ); $out[ $k ] = CHATDOZO_URL . 'assets/avatars/' . $k . '.svg'; }
return $out;
}
public static function initials( $name ) {
$name = trim( wp_strip_all_tags( (string) $name ) );
if ( $name === '' ) { return '?'; }
$parts = preg_split( '/\s+/', $name );
$out = '';
foreach ( $parts as $p ) {
$out .= mb_strtoupper( mb_substr( $p, 0, 1 ) );
if ( mb_strlen( $out ) >= 2 ) { break; }
}
return $out;
}
/** Plain-words traffic source from a referrer. */
public static function source( $referrer, $page_url = '' ) {
$referrer = trim( (string) $referrer );
if ( $referrer === '' ) { return __( 'typed the address', 'chatdozo' ); }
$host = strtolower( (string) wp_parse_url( $referrer, PHP_URL_HOST ) );
$own = strtolower( (string) wp_parse_url( home_url(), PHP_URL_HOST ) );
if ( $host === '' ) { return __( 'typed the address', 'chatdozo' ); }
if ( $own && ( $host === $own || substr( $host, -strlen( '.' . $own ) ) === '.' . $own ) ) { return __( 'from another page here', 'chatdozo' ); }
$map = array(
'google.' => 'Google', 'bing.' => 'Bing', 'duckduckgo.' => 'DuckDuckGo', 'yahoo.' => 'Yahoo', 'yandex.' => 'Yandex', 'baidu.' => 'Baidu',
'facebook.' => 'Facebook', 'fb.com' => 'Facebook', 'instagram.' => 'Instagram', 'threads.' => 'Threads', 't.co' => 'X', 'twitter.' => 'X', 'x.com' => 'X',
'linkedin.' => 'LinkedIn', 'pinterest.' => 'Pinterest', 'youtube.' => 'YouTube', 'tiktok.' => 'TikTok', 'reddit.' => 'Reddit', 'bsky.' => 'Bluesky',
't.me' => 'Telegram', 'whatsapp.' => 'WhatsApp', 'chatgpt.' => 'ChatGPT', 'openai.' => 'ChatGPT', 'perplexity.' => 'Perplexity', 'claude.ai' => 'Claude',
);
foreach ( $map as $needle => $label ) {
/* translators: %s: a website or app name */
if ( strpos( $host, $needle ) !== false ) { return sprintf( __( 'from %s', 'chatdozo' ), $label ); }
}
$host = preg_replace( '/^www\./', '', $host );
/* translators: %s: a website or app name */
return sprintf( __( 'from %s', 'chatdozo' ), $host );
}
public static function device( $ua ) {
$ua = (string) $ua;
if ( preg_match( '/iPad|Tablet/i', $ua ) ) { return __( 'Tablet', 'chatdozo' ); }
if ( preg_match( '/Mobi|Android|iPhone/i', $ua ) ) { return __( 'Phone', 'chatdozo' ); }
return __( 'Desktop', 'chatdozo' );
}
/** The visitor's address as a sanitized string: Cloudflare's header when the site sits behind it, else REMOTE_ADDR. */
private static function raw_ip() {
if ( isset( $_SERVER['HTTP_CF_CONNECTING_IP'] ) ) {
return sanitize_text_field( wp_unslash( $_SERVER['HTTP_CF_CONNECTING_IP'] ) );
}
return isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : '';
}
public static function ip() {
$ip = self::raw_ip();
return filter_var( $ip, FILTER_VALIDATE_IP ) ? $ip : '';
}
/** Country, region and city when Cloudflare sends them (free: Rules → Settings → Add visitor location headers). */
public static function geo() {
$h = function ( $k ) { return isset( $_SERVER[ $k ] ) ? sanitize_text_field( wp_unslash( $_SERVER[ $k ] ) ) : ''; };
$country = strtoupper( $h( 'HTTP_CF_IPCOUNTRY' ) );
if ( $country === 'XX' || $country === 'T1' ) { $country = ''; }
return array(
'country' => substr( $country, 0, 2 ),
'region' => mb_substr( $h( 'HTTP_CF_REGION' ), 0, 80 ),
'city' => mb_substr( $h( 'HTTP_CF_IPCITY' ), 0, 80 ),
);
}
public static function browser( $ua ) {
$ua = (string) $ua;
if ( preg_match( '/Edg\//', $ua ) ) { return 'Edge'; }
if ( preg_match( '/OPR\/|Opera/', $ua ) ) { return 'Opera'; }
if ( preg_match( '/SamsungBrowser/', $ua ) ) { return 'Samsung Internet'; }
if ( preg_match( '/Firefox\//', $ua ) ) { return 'Firefox'; }
if ( preg_match( '/CriOS|Chrome\//', $ua ) ) { return 'Chrome'; }
if ( preg_match( '/Safari\//', $ua ) ) { return 'Safari'; }
return '';
}
public static function os( $ua ) {
$ua = (string) $ua;
if ( preg_match( '/iPhone|iPad|iPod/', $ua ) ) { return 'iOS'; }
if ( preg_match( '/Android/', $ua ) ) { return 'Android'; }
if ( preg_match( '/Windows/', $ua ) ) { return 'Windows'; }
if ( preg_match( '/Mac OS X|Macintosh/', $ua ) ) { return 'macOS'; }
if ( preg_match( '/CrOS/', $ua ) ) { return 'ChromeOS'; }
if ( preg_match( '/Linux/', $ua ) ) { return 'Linux'; }
return '';
}
public static function lang() {
$l = isset( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) ? sanitize_text_field( wp_unslash( (string) $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) ) : ''; // phpcs:ignore
$l = trim( explode( ',', $l )[0] );
return substr( preg_replace( '/[^A-Za-z\-]/', '', explode( ';', $l )[0] ), 0, 16 );
}
/** "15,30,58" for a hex colour. */
public static function rgb( $hex ) {
$c = ltrim( (string) $hex, '#' );
if ( strlen( $c ) === 3 ) { $c = $c[0] . $c[0] . $c[1] . $c[1] . $c[2] . $c[2]; }
if ( strlen( $c ) !== 6 ) { $c = '0F1E3A'; }
return hexdec( substr( $c, 0, 2 ) ) . ',' . hexdec( substr( $c, 2, 2 ) ) . ',' . hexdec( substr( $c, 4, 2 ) );
}
/** Darker (negative) or lighter (positive) shade of a hex colour, e.g. shade('#0F1E3A', -0.18). */
public static function shade( $hex, $amt ) {
$parts = array_map( 'intval', explode( ',', self::rgb( $hex ) ) );
$out = '#';
foreach ( $parts as $v ) {
$v = $amt < 0 ? $v * ( 1 + $amt ) : $v + ( 255 - $v ) * $amt;
$out .= str_pad( dechex( max( 0, min( 255, (int) round( $v ) ) ) ), 2, '0', STR_PAD_LEFT );
}
return strtoupper( $out );
}
/** Built-in wallpapers: a CSS background-image value, or '' for plain. Quiet textures tinted with the brand colour. */
public static function wallpaper( $key, $url = '', $hex = '#0F1E3A' ) {
if ( $key === 'custom' ) { return $url ? 'url(' . esc_url( $url ) . ')' : ''; }
$rgb = self::rgb( $hex );
if ( $key === 'soft' ) { return 'linear-gradient(180deg,rgba(' . $rgb . ',.07) 0%,rgba(' . $rgb . ',0) 45%,rgba(' . $rgb . ',.05) 100%)'; }
$c = ltrim( (string) $hex, '#' );
if ( strlen( $c ) !== 6 ) { $c = '0F1E3A'; }
$col = '#' . $c; // rawurlencode below turns # into %23 exactly once; pre-encoding it made every colour invalid (grey) and the filters black
$p = array_map( function ( $v ) { return round( (int) $v / 255, 3 ); }, explode( ',', $rgb ) );
$svgs = array(
'linen' => '',
'grain' => '',
'dots' => '',
'grid' => '',
'waves' => '',
'carbon' => '',
'stripes' => '',
'chevron' => '',
'hex' => '',
'rings' => '',
'cross' => '',
'topo' => '',
'bubbles' => '',
'leaves' => '',
'confetti' => '',
'paper' => '',
);
if ( $key === 'mesh' ) { return 'radial-gradient(60% 50% at 10% 0%,rgba(' . $rgb . ',.12) 0%,rgba(' . $rgb . ',0) 60%),radial-gradient(50% 45% at 100% 30%,rgba(' . $rgb . ',.09) 0%,rgba(' . $rgb . ',0) 60%),radial-gradient(55% 50% at 30% 100%,rgba(' . $rgb . ',.10) 0%,rgba(' . $rgb . ',0) 60%)'; }
return isset( $svgs[ $key ] ) ? 'url("data:image/svg+xml,' . rawurlencode( $svgs[ $key ] ) . '")' : '';
}
public static function wallpaper_keys() {
return array( 'none' => __( 'Plain', 'chatdozo' ), 'soft' => __( 'Soft gradient', 'chatdozo' ), 'linen' => __( 'Linen', 'chatdozo' ), 'grain' => __( 'Grain', 'chatdozo' ), 'dots' => __( 'Fine dots', 'chatdozo' ), 'grid' => __( 'Fine grid', 'chatdozo' ), 'waves' => __( 'Light waves', 'chatdozo' ), 'carbon' => __( 'Carbon', 'chatdozo' ), 'mesh' => __( 'Soft blobs', 'chatdozo' ), 'stripes' => __( 'Fine stripes', 'chatdozo' ), 'chevron' => __( 'Chevrons', 'chatdozo' ), 'hex' => __( 'Honeycomb', 'chatdozo' ), 'rings' => __( 'Rings', 'chatdozo' ), 'cross' => __( 'Crosses', 'chatdozo' ), 'topo' => __( 'Contours', 'chatdozo' ), 'bubbles' => __( 'Bubbles', 'chatdozo' ), 'leaves' => __( 'Leaves', 'chatdozo' ), 'confetti' => __( 'Confetti', 'chatdozo' ), 'paper' => __( 'Paper', 'chatdozo' ), 'custom' => __( 'Your own image', 'chatdozo' ) );
}
public static function ip_hash() {
return sha1( 'cdz|' . wp_salt( 'auth' ) . '|' . self::raw_ip() );
}
/** "2 min", "Yesterday", "12 Oct" — short, like the inbox drafts. */
public static function ago( $mysql_utc ) {
if ( ! $mysql_utc ) { return ''; }
$ts = strtotime( $mysql_utc . ' UTC' );
$diff = time() - $ts;
if ( $diff < 60 ) { return __( 'now', 'chatdozo' ); }
/* translators: %d: number of minutes */
if ( $diff < 3600 ) { return sprintf( __( '%d min', 'chatdozo' ), floor( $diff / 60 ) ); }
if ( $diff < 86400 && wp_date( 'Y-m-d', $ts ) === wp_date( 'Y-m-d' ) ) { return wp_date( get_option( 'time_format' ), $ts ); }
if ( $diff < 172800 ) { return __( 'Yesterday', 'chatdozo' ); }
return wp_date( 'j M', $ts );
}
public static function page_label( $title, $url ) {
$path = (string) wp_parse_url( (string) $url, PHP_URL_PATH );
if ( $url && ( $path === '' || $path === '/' ) ) { return __( 'Home', 'chatdozo' ); }
$title = trim( (string) $title );
if ( $title !== '' ) {
$site = preg_quote( get_bloginfo( 'name' ), '/' );
$title = preg_replace( '/\s*[\-\|–—]\s*' . $site . '\s*$/iu', '', $title );
$title = preg_replace( '/^' . $site . '\s*[\-\|–—]\s*/iu', '', $title );
return $title !== '' ? $title : __( 'Home', 'chatdozo' );
}
$path = (string) wp_parse_url( (string) $url, PHP_URL_PATH );
return $path && $path !== '/' ? trim( $path, '/' ) : __( 'Home', 'chatdozo' );
}
/** Simple per-key throttle using transients: true when allowed. */
public static function allow( $key, $max, $window ) {
$k = 'chatdozo_rl_' . md5( $key );
$n = (int) get_transient( $k );
if ( $n >= $max ) { return false; }
set_transient( $k, $n + 1, $window );
return true;
}
}