init_components(); } /** * Initialize plugin subcomponents. */ private function init_components() { $this->settings = new Abhinavdev_WhatsApp_Chat_Settings(); $this->frontend = new Abhinavdev_WhatsApp_Chat_Frontend(); $this->shortcode = new Abhinavdev_WhatsApp_Chat_Shortcode(); } /** * Return plugin default configuration options. * * @return array */ public static function get_default_settings() { return array( // General. 'enabled' => 1, 'phone_number' => '', 'default_message' => 'Hello! I came across your website and have a question regarding {page_title}.', 'button_type' => 'popup', // 'popup' or 'direct'. 'button_position' => 'bottom_right', // 'bottom_right', 'bottom_left', 'top_right', 'top_left'. 'offset_bottom' => 24, 'offset_side' => 24, 'tooltip_text' => 'Need help? Chat with us', 'show_tooltip' => 1, 'open_in_new_tab' => 1, 'button_style' => 'circle', // See get_button_styles(). 'button_size' => 'medium', // 'small', 'medium', 'large'. 'button_label' => 'Chat with us', // Text shown by label styles (pill, bubble). // Design & Appearance. 'agent_name' => 'Support Team', 'agent_status' => 'Typically replies in seconds', 'agent_avatar_url' => '', 'welcome_message' => "Hi there! 👋\nHow can we help you today? Send us a message on WhatsApp and our team will get back to you right away.", 'cta_btn_text' => 'Start WhatsApp Chat', 'input_placeholder' => 'Type your message...', 'theme_color' => '#25D366', 'theme_color_dark' => '#075E54', 'header_text_color' => '#FFFFFF', 'pulse_animation' => 1, 'notification_badge' => 1, 'notification_count' => '1', // Display Rules. 'visibility_device' => 'all', // 'all', 'mobile', 'desktop'. 'visibility_pages' => 'all', // 'all', 'home_only', 'posts_only', 'pages_only', 'woo_only', 'exclude'. 'exclude_ids' => '', 'auto_open_delay' => 0, // In seconds (0 = disabled). 'scroll_trigger' => 0, // Percentage of page scrolled (0 = disabled). // Business Hours. 'enable_business_hours' => 0, 'timezone' => 'site', // 'site' or custom PHP timezone string. 'offline_behavior' => 'offline_message', // 'hide' or 'offline_message'. 'offline_message' => "We are currently offline. Leave us a message on WhatsApp and we will get back to you as soon as we're back!", 'schedule' => array( 'monday' => array( 'enabled' => 1, 'start' => '09:00', 'end' => '18:00' ), 'tuesday' => array( 'enabled' => 1, 'start' => '09:00', 'end' => '18:00' ), 'wednesday' => array( 'enabled' => 1, 'start' => '09:00', 'end' => '18:00' ), 'thursday' => array( 'enabled' => 1, 'start' => '09:00', 'end' => '18:00' ), 'friday' => array( 'enabled' => 1, 'start' => '09:00', 'end' => '18:00' ), 'saturday' => array( 'enabled' => 0, 'start' => '10:00', 'end' => '16:00' ), 'sunday' => array( 'enabled' => 0, 'start' => '10:00', 'end' => '16:00' ), ), // Analytics. 'track_ga4' => 1, 'track_gtm' => 1, 'track_meta_pixel' => 0, ); } /** * Retrieve saved settings merged with defaults. * * @return array */ public static function get_settings() { $saved = get_option( ABHINAVDEV_C2C_OPTION_NAME, array() ); $defaults = self::get_default_settings(); if ( ! is_array( $saved ) ) { $saved = array(); } // Recursively merge schedule if needed. $settings = wp_parse_args( $saved, $defaults ); if ( isset( $saved['schedule'] ) && is_array( $saved['schedule'] ) ) { $settings['schedule'] = wp_parse_args( $saved['schedule'], $defaults['schedule'] ); } return $settings; } /** * Available floating button styles (slug => label). * * @return array */ public static function get_button_styles() { return array( 'circle' => __( 'Classic Circle', 'abhinavdev-visitor-chat-widget' ), 'rounded' => __( 'Rounded Square', 'abhinavdev-visitor-chat-widget' ), 'pill' => __( 'Pill with Label', 'abhinavdev-visitor-chat-widget' ), 'bubble' => __( 'Speech Bubble', 'abhinavdev-visitor-chat-widget' ), 'outline' => __( 'Outline', 'abhinavdev-visitor-chat-widget' ), 'gradient' => __( 'Gradient Glow', 'abhinavdev-visitor-chat-widget' ), ); } /** * Available floating button sizes (slug => label). * * @return array */ public static function get_button_sizes() { return array( 'small' => __( 'Small', 'abhinavdev-visitor-chat-widget' ), 'medium' => __( 'Medium', 'abhinavdev-visitor-chat-widget' ), 'large' => __( 'Large', 'abhinavdev-visitor-chat-widget' ), ); } /** * Reduce a phone number to digits only (wa.me format). * * @param string $phone Raw phone number. * @return string */ public static function clean_phone( $phone ) { return preg_replace( '/\D+/', '', (string) $phone ); } /** * Whether a cleaned phone number has a plausible E.164 length. * * @param string $phone Digits-only phone number. * @return bool */ public static function is_valid_phone( $phone ) { $len = strlen( $phone ); return $len >= 8 && $len <= 15; } /** * Replace dynamic placeholders in a message string. * * Used for the server-rendered fallback; the frontend script re-applies the * tags client-side so full-page caches never serve a stale page title or URL. * * @param string $message Raw message string. * @return string */ public static function replace_dynamic_tags( $message ) { $url = ''; if ( ! empty( $_SERVER['HTTP_HOST'] ) && ! empty( $_SERVER['REQUEST_URI'] ) ) { $url = ( is_ssl() ? 'https://' : 'http://' ) . sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ) . sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ); } $message = str_replace( array( '{page_title}', '{page_url}', '{site_title}' ), array( wp_get_document_title(), $url, get_bloginfo( 'name' ) ), (string) $message ); /** * Filter the final pre-filled WhatsApp message. * * @param string $message Message with placeholders replaced. */ return apply_filters( 'abhinavdev_c2c_message', $message ); } /** * Check whether the current moment falls inside the saved schedule. * Supports overnight shifts (e.g. 22:00 - 02:00). * * @param array $options Plugin settings. * @return bool */ public static function is_within_business_hours( $options ) { $tz_string = ! empty( $options['timezone'] ) && 'site' !== $options['timezone'] ? $options['timezone'] : wp_timezone_string(); try { $now = new DateTime( 'now', new DateTimeZone( $tz_string ) ); } catch ( Exception $e ) { $now = new DateTime( 'now', wp_timezone() ); } $days = array( 'sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday' ); $today = (int) $now->format( 'w' ); $minutes = ( (int) $now->format( 'G' ) * 60 ) + (int) $now->format( 'i' ); $to_min = static function ( $hhmm ) { $parts = explode( ':', $hhmm ); return ( (int) $parts[0] * 60 ) + (int) ( isset( $parts[1] ) ? $parts[1] : 0 ); }; $schedule = isset( $options['schedule'] ) && is_array( $options['schedule'] ) ? $options['schedule'] : array(); // Today's window. $d = isset( $schedule[ $days[ $today ] ] ) ? $schedule[ $days[ $today ] ] : null; if ( $d && ! empty( $d['enabled'] ) ) { $start = $to_min( ! empty( $d['start'] ) ? $d['start'] : '00:00' ); $end = $to_min( ! empty( $d['end'] ) ? $d['end'] : '23:59' ); if ( $start <= $end ) { if ( $minutes >= $start && $minutes <= $end ) { return true; } } elseif ( $minutes >= $start ) { // Overnight: evening part. return true; } } // Yesterday's overnight window spilling into the early hours. $y = isset( $schedule[ $days[ ( $today + 6 ) % 7 ] ] ) ? $schedule[ $days[ ( $today + 6 ) % 7 ] ] : null; if ( $y && ! empty( $y['enabled'] ) ) { $start = $to_min( ! empty( $y['start'] ) ? $y['start'] : '00:00' ); $end = $to_min( ! empty( $y['end'] ) ? $y['end'] : '23:59' ); if ( $end < $start && $minutes <= $end ) { return true; } } return false; } /** * Get a specific setting value. * * @param string $key Setting key. * @param mixed $default Fallback value. * @return mixed */ public static function get_setting( $key, $default = null ) { $settings = self::get_settings(); if ( array_key_exists( $key, $settings ) ) { return $settings[ $key ]; } return $default; } }