true,
'strategy' => 'defer',
)
);
$css = self::hide_css( $s, $items );
if ( '' !== $css ) {
wp_add_inline_style( self::MENU_HANDLE, $css );
}
}
}
do_action( 'ason_chat_button_assets' );
}
public static function consent_present(): bool {
$active = array_merge( (array) get_option( 'active_plugins', array() ), array_keys( (array) get_site_option( 'active_sitewide_plugins', array() ) ) );
$on = in_array( 'ason-consent/ason-consent.php', $active, true );
if ( $on ) {
$on = ! (bool) apply_filters( 'ason_consent_skip', isset( $_GET['elementor-preview'] ) || is_customize_preview() );
}
return (bool) apply_filters( 'ason_chat_button_consent_present', $on );
}
public static function menu_items( array $s, array $agents ): array {
$out = array();
foreach ( (array) $s['menu_items'] as $k ) {
$ok = false;
switch ( $k ) {
case 'whatsapp':
$ok = ! empty( $agents );
break;
case 'call':
$ok = '' !== (string) $s['menu_phone'];
break;
case 'email':
$ok = '' !== (string) $s['menu_email'];
break;
case 'map':
$ok = '' !== (string) $s['menu_map'];
break;
case 'consent':
$ok = self::consent_present();
break;
case 'top':
$ok = true;
break;
}
if ( $ok ) {
$out[] = (string) $k;
}
}
return (array) apply_filters( 'ason_chat_button_menu_items', $out, $s );
}
public static function hide_css( array $s, array $items ): string {
$sel = array();
if ( ! empty( $s['menu_hide_consent'] ) && in_array( 'consent', $items, true ) ) {
$sel[] = '.ason-consent-float';
}
if ( ! empty( $s['menu_hide_top'] ) ) {
$sel[] = Settings::TOP_PRESET;
}
if ( '' !== (string) $s['menu_hide_sel'] ) {
$sel[] = Settings::selectors( $s['menu_hide_sel'] );
}
$sel = array_filter(
$sel,
function ( $x ) {
return '' !== $x;
}
);
if ( ! $sel ) {
return '';
}
$rule = implode( ',', $sel ) . '{display:none!important}';
if ( empty( $s['show_mobile'] ) ) {
return '@media (min-width:768px){' . $rule . '}';
}
if ( empty( $s['show_desktop'] ) ) {
return '@media (max-width:767.98px){' . $rule . '}';
}
return $rule;
}
public static function context(): array {
$s = Settings::all();
$site = wp_specialchars_decode( (string) get_bloginfo( 'name' ), ENT_QUOTES );
$id = is_singular() ? (int) get_queried_object_id() : 0;
$type = 'general';
if ( $id > 0 ) {
$title = wp_specialchars_decode( wp_strip_all_tags( (string) get_the_title( $id ) ), ENT_QUOTES );
$url = (string) get_permalink( $id );
if ( in_array( (string) get_post_type( $id ), (array) $s['product_types'], true ) ) {
$type = 'product';
}
} else {
$title = wp_specialchars_decode( wp_strip_all_tags( (string) wp_get_document_title() ), ENT_QUOTES );
$url = self::current_url();
}
$ctx = array(
'type' => $type,
'title' => trim( $title ),
'url' => $url,
'site' => $site,
'id' => $id,
);
return (array) apply_filters( 'ason_chat_button_context', $ctx );
}
private static function current_url(): string {
global $wp;
$path = ( isset( $wp->request ) && '' !== (string) $wp->request ) ? user_trailingslashit( (string) $wp->request ) : '';
return (string) home_url( '/' . ltrim( $path, '/' ) );
}
public static function default_template( string $type ): string {
return 'product' === $type ? __( 'Hello, I would like to get information about {title} ({url})', 'ason-chat-button' ) : __( 'Hello, I would like to get information. ({url})', 'ason-chat-button' );
}
public static function message( array $ctx ): string {
$key = 'product' === $ctx['type'] ? 'msg_product' : 'msg_general';
$tpl = Settings::translated( $key );
if ( '' === $tpl ) {
$tpl = self::default_template( (string) $ctx['type'] );
}
$tpl = (string) apply_filters( 'ason_chat_button_template', $tpl, (string) $ctx['type'], $ctx );
return trim(
strtr(
$tpl,
array(
'{title}' => (string) $ctx['title'],
'{url}' => (string) $ctx['url'],
'{site}' => (string) $ctx['site'],
)
)
);
}
public static function link( string $phone, string $msg ): string {
return 'https://wa.me/' . $phone . ( '' !== $msg ? '?text=' . rawurlencode( $msg ) : '' );
}
public static function agents( array $ctx ): array {
$s = Settings::all();
$list = array(
array(
'phone' => (string) $s['phone'],
'name' => '',
'role' => '',
'hours' => 1,
),
);
$list = (array) apply_filters( 'ason_chat_button_agents', $list, $ctx );
$out = array();
foreach ( $list as $a ) {
$p = Settings::phone( isset( $a['phone'] ) ? $a['phone'] : '' );
if ( '' === $p ) {
continue;
}
$out[] = array(
'phone' => $p,
'name' => isset( $a['name'] ) ? (string) $a['name'] : '',
'role' => isset( $a['role'] ) ? (string) $a['role'] : '',
'hours' => empty( $a['hours'] ) ? 0 : 1,
);
}
return $out;
}
private static function icon( int $px ): string {
return '';
}
private static function label(): string {
$l = Settings::translated( 'label' );
return '' !== $l ? $l : __( 'Chat on WhatsApp', 'ason-chat-button' );
}
private static function note_html(): string {
$t = Settings::translated( 'note_text' );
if ( '' === $t ) {
$t = __( 'You will be redirected to WhatsApp. Your message is processed on Meta\'s infrastructure.', 'ason-chat-button' );
}
$u = Settings::privacy_url();
$h = esc_html( $t );
if ( '' !== $u ) {
$h .= ' ' . esc_html__( 'Privacy notice', 'ason-chat-button' ) . '';
}
return $h;
}
private static function root_attrs( string $kind, array $agents, array $extra = array() ): string {
$a = (array) apply_filters( 'ason_chat_button_root_attrs', array_merge( array( 'data-ason-cb' => $kind ), $extra ), $kind, $agents );
$h = '';
foreach ( $a as $k => $v ) {
$k = preg_replace( '/[^a-z0-9_-]/', '', strtolower( (string) $k ) );
if ( '' === $k || null === $v || false === $v ) {
continue;
}
$h .= ' ' . $k . '="' . esc_attr( (string) $v ) . '"';
}
return $h;
}
private static function agent_text( array $a ): string {
$p = array_filter( array( $a['name'], $a['role'] ), 'strlen' );
return $p ? implode( ' — ', $p ) : __( 'Continue to WhatsApp', 'ason-chat-button' );
}
private static function links( array $agents, string $msg, string $class, bool $menu = false ): string {
$rotate = count( $agents ) > 1 && (bool) apply_filters( 'ason_chat_button_rotate', false );
$list = $rotate ? array( $agents[0] ) : $agents;
$h = '';
foreach ( $list as $i => $a ) {
if ( $menu ) {
$one = $rotate || 1 === count( $agents );
$text = $one ? 'WhatsApp' : self::agent_text( $a );
$alt = $rotate ? self::alt_attr( $agents, $msg ) : '';
$h .= '
' . self::menu_icon( 'whatsapp' ) . '' . esc_html( $text ) . '';
continue;
}
$attrs = ' data-ason-cb-agent="' . esc_attr( (string) $i ) . '" data-ason-cb-role="' . esc_attr( $a['role'] ) . '" data-ason-cb-h="' . (int) $a['hours'] . '"';
if ( $rotate ) {
$attrs .= self::alt_attr( $agents, $msg );
}
$text = $rotate ? __( 'Continue to WhatsApp', 'ason-chat-button' ) : self::agent_text( $a );
$h .= '' . esc_html( $text ) . '';
}
return $h;
}
private static function alt_attr( array $agents, string $msg ): string {
$alt = array();
foreach ( $agents as $b ) {
$alt[] = array(
'u' => self::link( $b['phone'], $msg ),
'r' => $b['role'],
'h' => (int) $b['hours'],
);
}
return ' data-ason-cb-alt="' . esc_attr( (string) wp_json_encode( $alt, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ) ) . '"';
}
private static function menu_icon( string $k ): string {
$d = array(
'more' => '',
'close' => '',
'call' => '',
'email' => '',
'map' => '',
'consent' => '',
'top' => '',
);
if ( 'whatsapp' === $k ) {
return self::icon( 22 );
}
$w = ( 'more' === $k || 'close' === $k ) ? 24 : 22;
return '';
}
private static function menu_label( string $k ): string {
switch ( $k ) {
case 'call':
return __( 'Call', 'ason-chat-button' );
case 'email':
return __( 'Email', 'ason-chat-button' );
case 'map':
return __( 'Location', 'ason-chat-button' );
case 'consent':
return __( 'Cookie settings', 'ason-chat-button' );
case 'top':
return __( 'Back to top', 'ason-chat-button' );
}
return 'WhatsApp';
}
public static function menu_html( array $s, array $ctx, array $agents, array $items ): string {
$label = __( 'Quick actions', 'ason-chat-button' );
$cls = 'ason-cb ason-cb-m ason-cb--' . ( 'left' === $s['position'] ? 'l' : 'r' );
if ( empty( $s['show_mobile'] ) ) {
$cls .= ' ason-cb--no-m';
}
if ( empty( $s['show_desktop'] ) ) {
$cls .= ' ason-cb--no-d';
}
$acc = (string) $s['menu_color'];
$wa = ! empty( $s['menu_wa_color'] ) ? (string) $s['color'] : $acc;
$style = sprintf(
'--ason-cb-s:%dpx;--ason-cb-x:%dpx;--ason-cb-y:%dpx;--ason-cb-c:%s;--ason-cb-t:%s;--ason-cb-mc:%s;--ason-cb-md:%s;--ason-cb-mf:%s;--ason-cb-mw:%s;--ason-cb-mr:%dpx',
max( 44, (int) $s['size'] ),
(int) $s['offset_x'],
(int) $s['offset_y'],
(string) $s['color'],
Settings::fg( (string) $s['color'], 4.5 ),
$acc,
(string) $s['menu_dark'],
Settings::fg( $acc, 3.0 ),
$wa,
(int) $s['menu_radius']
);
$id = self::MENU_ID;
$h = '';
$h .= '
';
$h .= '
';
if ( in_array( 'whatsapp', $items, true ) ) {
ob_start();
do_action( 'ason_chat_button_panel_top', 'menu', $agents );
$h .= (string) ob_get_clean();
if ( ! empty( $s['note'] ) ) {
$h .= '
' . self::note_html() . '
';
}
}
$h .= '
';
$msg = self::message( $ctx );
$li = '';
foreach ( $items as $k ) {
$icon = self::menu_icon( $k ) . '' . esc_html( self::menu_label( $k ) ) . '';
switch ( $k ) {
case 'whatsapp':
$li .= self::links( $agents, $msg, 'ason-cb-m-i ason-cb-m-i--wa', true );
break;
case 'call':
$li .= '- ' . $icon . '
';
break;
case 'email':
$li .= '- ' . $icon . '
';
break;
case 'map':
$li .= '- ' . $icon . '
';
break;
case 'consent':
$li .= '';
break;
case 'top':
$li .= '- ' . $icon . '
';
break;
}
}
$li = (string) preg_replace( '/<(a|button) class="ason-cb-m-i/', '<$1 autofocus class="ason-cb-m-i', $li, 1 );
return $h . $li . '
';
}
public static function kses_menu(): array {
return ason_kses_allowed(
array(
'button' => array(
'popovertarget' => true,
'popovertargetaction' => true,
),
)
);
}
public static function floating(): void {
if ( self::$printed || is_admin() || is_feed() || is_embed() ) {
return;
}
$s = Settings::all();
if ( empty( $s['enabled'] ) || ( empty( $s['show_desktop'] ) && empty( $s['show_mobile'] ) ) ) {
return;
}
$ctx = self::context();
$agents = self::agents( $ctx );
if ( ! empty( $s['menu'] ) ) {
$items = self::menu_items( $s, $agents );
if ( ! $items ) {
return;
}
self::$printed = true;
echo wp_kses( self::menu_html( $s, $ctx, $agents, $items ), self::kses_menu() );
return;
}
if ( ! $agents ) {
return;
}
self::$printed = true;
echo wp_kses( self::fab_html( $s, $ctx, $agents ), ason_kses_allowed() );
}
public static function fab_html( array $s, array $ctx, array $agents ): string {
$msg = self::message( $ctx );
$label = self::label();
$cls = 'ason-cb ason-cb--fab ason-cb--' . ( 'left' === $s['position'] ? 'l' : 'r' );
if ( empty( $s['show_mobile'] ) ) {
$cls .= ' ason-cb--no-m';
}
if ( empty( $s['show_desktop'] ) ) {
$cls .= ' ason-cb--no-d';
}
$style = sprintf( '--ason-cb-s:%dpx;--ason-cb-x:%dpx;--ason-cb-y:%dpx;--ason-cb-c:%s;--ason-cb-f:%s;--ason-cb-t:%s', (int) $s['size'], (int) $s['offset_x'], (int) $s['offset_y'], (string) $s['color'], Settings::fg( (string) $s['color'], 1.6 ), Settings::fg( (string) $s['color'], 4.5 ) );
$icon = self::icon( (int) round( (int) $s['size'] * 0.56 ) );
$rotate = count( $agents ) > 1 && (bool) apply_filters( 'ason_chat_button_rotate', false );
$panel = ! empty( $s['note'] ) || ( count( $agents ) > 1 && ! $rotate );
$panel = (bool) apply_filters( 'ason_chat_button_use_panel', $panel, 'fab', $agents );
$h = '';
if ( ! $panel ) {
$a = $agents[0];
$attrs = ' data-ason-cb-agent="0" data-ason-cb-role="' . esc_attr( $a['role'] ) . '" data-ason-cb-h="' . (int) $a['hours'] . '"';
if ( $rotate ) {
$links = self::links( $agents, $msg, 'ason-cb-b' );
return $h . preg_replace( '/>[^<]*<\/a>$/', ' aria-label="' . esc_attr( $label ) . '">' . $icon . '', $links, 1 ) . '
';
}
return $h . '' . $icon . '';
}
ob_start();
do_action( 'ason_chat_button_panel_top', 'fab', $agents );
$top = (string) ob_get_clean();
$h .= '' . $icon . '
';
$h .= '' . $top;
if ( ! empty( $s['note'] ) ) {
$h .= '
' . self::note_html() . '
';
}
$h .= self::links( $agents, $msg, 'ason-cb-l' ) . '
';
return $h;
}
public static function inline_html( array $args ): string {
$s = Settings::all();
if ( empty( $s['enabled'] ) ) {
return '';
}
$ctx = self::context();
$agents = self::agents( $ctx );
if ( ! $agents ) {
return '';
}
$msg = '';
if ( isset( $args['message'] ) && '' !== (string) $args['message'] ) {
$msg = trim(
strtr(
(string) $args['message'],
array(
'{title}' => (string) $ctx['title'],
'{url}' => (string) $ctx['url'],
'{site}' => (string) $ctx['site'],
)
)
);
} else {
$msg = self::message( $ctx );
}
$text = ( isset( $args['text'] ) && '' !== (string) $args['text'] ) ? (string) $args['text'] : __( 'Ask on WhatsApp', 'ason-chat-button' );
$place = isset( $args['placement'] ) ? sanitize_key( (string) $args['placement'] ) : 'inline';
$style = '--ason-cb-c:' . (string) $s['color'] . ';--ason-cb-t:' . Settings::fg( (string) $s['color'], 4.5 );
$icon = self::icon( 20 );
$rotate = count( $agents ) > 1 && (bool) apply_filters( 'ason_chat_button_rotate', false );
$panel = count( $agents ) > 1 && ! $rotate;
$h = '';
ob_start();
do_action( 'ason_chat_button_panel_top', $place, $agents );
$top = (string) ob_get_clean();
if ( $panel ) {
$h .= '' . $icon . '' . esc_html( $text ) . '
' . $top . self::links( $agents, $msg, 'ason-cb-l' ) . ' ';
} else {
$links = self::links( $agents, $msg, 'ason-cb-i' );
$h .= $top . preg_replace( '/>[^<]*<\/a>$/', '>' . $icon . '' . esc_html( $text ) . '', $links, 1 );
}
if ( ! empty( $s['note'] ) ) {
$h .= '' . self::note_html() . '';
}
return $h . '';
}
public static function shortcode( $atts ): string {
$a = shortcode_atts(
array(
'text' => '',
'message' => '',
),
(array) $atts,
'ason_chat_button'
);
if ( ! wp_style_is( self::HANDLE, 'enqueued' ) && self::ready() ) {
wp_enqueue_style( self::HANDLE, ASON_CB_URL . 'assets/chat-button.css', array(), ASON_CB_VERSION );
do_action( 'ason_chat_button_assets' );
}
$a['placement'] = 'inline';
return self::inline_html( $a );
}
public static function product_button(): void {
$s = Settings::all();
if ( empty( $s['woo_button'] ) ) {
return;
}
$html = self::inline_html(
array(
'text' => Settings::translated( 'woo_text' ),
'placement' => 'product',
)
);
echo wp_kses( $html, ason_kses_allowed() );
}
}