options = Abhinavdev_WhatsApp_Chat_Core::get_settings(); add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend_assets' ) ); add_action( 'wp_footer', array( $this, 'render_widget' ) ); } /** * Determine if widget should be rendered on current page/device. * * @return bool */ public function should_display() { // Master toggle. if ( empty( $this->options['enabled'] ) ) { return false; } // Phone number must not be empty. if ( empty( $this->options['phone_number'] ) ) { return false; } // Page visibility rules. $pages_rule = ! empty( $this->options['visibility_pages'] ) ? $this->options['visibility_pages'] : 'all'; $current_id = get_queried_object_id(); // Excluded IDs check. if ( ! empty( $this->options['exclude_ids'] ) ) { $excluded = array_map( 'absint', array_filter( explode( ',', $this->options['exclude_ids'] ) ) ); if ( in_array( $current_id, $excluded, true ) ) { return false; } } if ( 'home_only' === $pages_rule && ! ( is_front_page() || is_home() ) ) { return false; } if ( 'posts_only' === $pages_rule && ! is_single() ) { return false; } if ( 'pages_only' === $pages_rule && ! is_page() ) { return false; } if ( 'woo_only' === $pages_rule ) { $is_woo = false; if ( function_exists( 'is_woocommerce' ) && ( is_woocommerce() || is_shop() || is_product_category() || is_product_tag() || is_product() ) ) { $is_woo = true; } if ( ! $is_woo ) { return false; } } /** * Filter whether the widget should display on the current request. * * Device targeting and business hours are evaluated in the browser so that * full-page caches serve the correct state to every visitor. * * @param bool $display Whether to display. * @param array $options Plugin settings. */ return (bool) apply_filters( 'abhinavdev_c2c_should_display', true, $this->options ); } /** * Verify if current time is within business hours. * * @return bool */ public function check_business_hours() { return Abhinavdev_WhatsApp_Chat_Core::is_within_business_hours( $this->options ); } /** * Enqueue styles and scripts. */ public function enqueue_frontend_assets() { if ( ! $this->should_display() ) { return; } wp_enqueue_style( 'abhinavdev-wa-frontend', ABHINAVDEV_C2C_URL . 'assets/css/frontend.css', array(), ABHINAVDEV_C2C_VERSION ); // Output dynamic CSS variables. $custom_vars = sprintf( ':root { --abhinavdev-wa-theme: %s; --abhinavdev-wa-theme-dark: %s; --abhinavdev-wa-header-text: %s; --abhinavdev-wa-offset-bottom: %dpx; --abhinavdev-wa-offset-side: %dpx; }', esc_attr( $this->options['theme_color'] ), esc_attr( $this->options['theme_color_dark'] ), esc_attr( $this->options['header_text_color'] ), absint( $this->options['offset_bottom'] ), absint( $this->options['offset_side'] ) ); wp_add_inline_style( 'abhinavdev-wa-frontend', $custom_vars ); wp_enqueue_script( 'abhinavdev-wa-frontend', ABHINAVDEV_C2C_URL . 'assets/js/frontend.js', array(), ABHINAVDEV_C2C_VERSION, true ); $hours_on = ! empty( $this->options['enable_business_hours'] ); $tz = ! empty( $this->options['timezone'] ) && 'site' !== $this->options['timezone'] ? $this->options['timezone'] : wp_timezone_string(); $data = array( 'phone' => Abhinavdev_WhatsApp_Chat_Core::clean_phone( $this->options['phone_number'] ), 'defaultMessage' => $this->options['default_message'], // Raw; tags are resolved in JS. 'buttonType' => $this->options['button_type'], 'openInNewTab' => ! empty( $this->options['open_in_new_tab'] ), 'autoOpenDelay' => absint( $this->options['auto_open_delay'] ), 'scrollTrigger' => absint( $this->options['scroll_trigger'] ), 'trackGA4' => ! empty( $this->options['track_ga4'] ), 'trackGTM' => ! empty( $this->options['track_gtm'] ), 'trackMetaPixel' => ! empty( $this->options['track_meta_pixel'] ), 'device' => $this->options['visibility_device'], 'siteTitle' => get_bloginfo( 'name' ), 'welcomeMessage' => $this->options['welcome_message'], 'agentStatus' => $this->options['agent_status'], 'offlineMessage' => $this->options['offline_message'], 'offlineStatus' => __( 'Offline', 'abhinavdev-visitor-chat-widget' ), 'businessHours' => array( 'enabled' => $hours_on, 'timezone' => $tz, 'behavior' => $this->options['offline_behavior'], 'schedule' => $this->options['schedule'], ), ); wp_add_inline_script( 'abhinavdev-wa-frontend', 'window.abhinavdevWaData = ' . wp_json_encode( $data ) . ';', 'before' ); } /** * Replace dynamic placeholders in message string. * * @param string $message Raw message string. * @return string */ public function replace_dynamic_tags( $message ) { return Abhinavdev_WhatsApp_Chat_Core::replace_dynamic_tags( $message ); } /** * Inline WhatsApp glyph (single source for every icon instance). * * @param int $size Width/height in px. * @param string $fill Fill attribute. * @return string */ public static function whatsapp_svg( $size = 32, $fill = 'currentColor' ) { return ''; } /** * Render widget markup in footer. */ public function render_widget() { if ( ! $this->should_display() ) { return; } $this->is_offline = ! empty( $this->options['enable_business_hours'] ) && ! $this->check_business_hours(); $wa_url = 'https://wa.me/' . Abhinavdev_WhatsApp_Chat_Core::clean_phone( $this->options['phone_number'] ) . '?text=' . rawurlencode( $this->replace_dynamic_tags( $this->options['default_message'] ) ); // Markup is built from escaped parts inside build_widget_markup(). echo self::build_widget_markup( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped $this->options, array( 'offline' => $this->is_offline, 'wa_url' => $wa_url, 'target_attr' => ! empty( $this->options['open_in_new_tab'] ) ? 'target="_blank" rel="noopener noreferrer"' : '', 'needs_js_check' => 'all' !== $this->options['visibility_device'] || ! empty( $this->options['enable_business_hours'] ), ) ); } /** * Build the widget HTML. Shared by the live site and the admin live preview * so that what you see in settings is exactly what visitors get. * * @param array $o Plugin settings. * @param array $args { * @type bool $preview Always render popup + button element (admin preview). * @type bool $offline Render in offline state. * @type string $wa_url Pre-filled WhatsApp URL. * @type string $target_attr Pre-escaped target/rel attributes. * @type bool $needs_js_check Hide until the script checked device / hours. * } * @return string */ public static function build_widget_markup( $o, $args = array() ) { $args = wp_parse_args( $args, array( 'preview' => false, 'offline' => false, 'wa_url' => '#', 'target_attr' => '', 'needs_js_check' => false, ) ); $preview = (bool) $args['preview']; $offline = (bool) $args['offline']; $position = ! empty( $o['button_position'] ) ? $o['button_position'] : 'bottom_right'; $button_type = ! empty( $o['button_type'] ) ? $o['button_type'] : 'popup'; $styles = Abhinavdev_WhatsApp_Chat_Core::get_button_styles(); $sizes = Abhinavdev_WhatsApp_Chat_Core::get_button_sizes(); $style = ( ! empty( $o['button_style'] ) && isset( $styles[ $o['button_style'] ] ) ) ? $o['button_style'] : 'circle'; $size = ( ! empty( $o['button_size'] ) && isset( $sizes[ $o['button_size'] ] ) ) ? $o['button_size'] : 'medium'; $label = isset( $o['button_label'] ) ? trim( $o['button_label'] ) : ''; $has_label = in_array( $style, array( 'pill', 'bubble' ), true ) && '' !== $label; $has_popup = $preview || 'popup' === $button_type; $is_link = ! $preview && 'direct' === $button_type; $classes = array( 'abhinavdev-wa-wrapper', 'abhinavdev-pos-' . sanitize_html_class( $position ), 'abhinavdev-style-' . $style, 'abhinavdev-size-' . $size, ); if ( $has_label ) { $classes[] = 'has-label'; } if ( $args['needs_js_check'] && ! $preview ) { $classes[] = 'abhinavdev-wa-pending'; } $btn_class = 'abhinavdev-wa-btn' . ( ! empty( $o['pulse_animation'] ) ? ' has-pulse' : '' ); $greeting = ( $offline && ! empty( $o['offline_message'] ) ) ? $o['offline_message'] : $o['welcome_message']; $status = $offline ? __( 'Offline', 'abhinavdev-visitor-chat-widget' ) : $o['agent_status']; $time = $preview ? '10:45 AM' : current_time( 'g:i A' ); ob_start(); ?>