hook_suffix = add_menu_page(
__( 'Visitor Chat Settings', 'abhinavdev-visitor-chat-widget' ),
__( 'Visitor Chat', 'abhinavdev-visitor-chat-widget' ),
'manage_options',
'abhinavdev-visitor-chat-widget',
array( $this, 'render_settings_page' ),
'dashicons-format-chat',
68
);
}
/**
* Cache-busting version for an admin asset (plugin version + file mtime).
*
* @param string $relative Path relative to the plugin directory.
* @return string
*/
private function asset_version( $relative ) {
$file = plugin_dir_path( dirname( __FILE__ ) ) . $relative;
$time = file_exists( $file ) ? filemtime( $file ) : 0;
return ABHINAVDEV_C2C_VERSION . ( $time ? '.' . $time : '' );
}
/**
* Enqueue admin scripts & styles.
*
* @param string $hook Page hook suffix.
*/
public function enqueue_admin_assets( $hook ) {
if ( $this->hook_suffix !== $hook ) {
return;
}
// WordPress core media uploader & color picker.
wp_enqueue_media();
wp_enqueue_style( 'wp-color-picker' );
// Plugin admin style & scripts.
wp_enqueue_style(
'abhinavdev-wa-admin',
ABHINAVDEV_C2C_URL . 'assets/css/admin.css',
array( 'wp-color-picker' ),
$this->asset_version( 'assets/css/admin.css' )
);
wp_enqueue_script(
'abhinavdev-wa-admin',
ABHINAVDEV_C2C_URL . 'assets/js/admin.js',
array( 'jquery', 'wp-color-picker' ),
$this->asset_version( 'assets/js/admin.js' ),
true
);
wp_localize_script(
'abhinavdev-wa-admin',
'abhinavdevWaAdmin',
array(
'optionName' => ABHINAVDEV_C2C_OPTION_NAME,
'unsavedText' => __( 'You have unsaved changes', 'abhinavdev-visitor-chat-widget' ),
'statusLive' => __( 'Live on your site', 'abhinavdev-visitor-chat-widget' ),
'statusPaused' => __( 'Paused', 'abhinavdev-visitor-chat-widget' ),
'statusPhone' => __( 'Add your phone number', 'abhinavdev-visitor-chat-widget' ),
'previewDoc' => $this->get_preview_document(),
'selectAvatarTitle' => __( 'Select or Upload Agent Avatar', 'abhinavdev-visitor-chat-widget' ),
'useAvatarButton' => __( 'Use as Avatar', 'abhinavdev-visitor-chat-widget' ),
)
);
}
/**
* Build the full HTML document rendered inside the live-preview iframe.
* It loads the real frontend stylesheet and the real widget markup.
*
* @return string
*/
private function get_preview_document() {
$options = Abhinavdev_WhatsApp_Chat_Core::get_settings();
$css_url = add_query_arg( 'ver', ABHINAVDEV_C2C_VERSION, ABHINAVDEV_C2C_URL . 'assets/css/frontend.css' );
$markup = Abhinavdev_WhatsApp_Chat_Frontend::build_widget_markup( $options, array( 'preview' => true ) );
$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( $options['theme_color'] ),
esc_attr( $options['theme_color_dark'] ),
esc_attr( $options['header_text_color'] ),
absint( $options['offset_bottom'] ),
absint( $options['offset_side'] )
);
$page_css = 'html,body{margin:0;height:100%;overflow:hidden;background:#F3F4F6;font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,sans-serif}'
. '.pv-nav{height:56px;background:#fff;border-bottom:1px solid #E5E7EB;display:flex;align-items:center;gap:14px;padding:0 24px}'
. '.pv-logo{width:84px;height:18px;border-radius:4px;background:#D1D5DB}.pv-link{width:48px;height:10px;border-radius:4px;background:#E5E7EB}'
. '.pv-hero{margin:28px 24px 0;height:150px;border-radius:12px;background:linear-gradient(135deg,#E5E7EB,#F9FAFB)}'
. '.pv-line{height:11px;border-radius:5px;background:#E5E7EB;margin:14px 24px 0}.pv-line.s{width:55%}.pv-line.m{width:75%}'
. '.pv-grid{display:flex;gap:14px;margin:26px 24px 0}.pv-card{flex:1;height:110px;border-radius:10px;background:#fff;border:1px solid #E5E7EB}';
$page = '
'
. ''
. '';
return ''
. ''
. ''
. ''
. '' . $page . $markup . '';
}
/**
* Register settings with WordPress Settings API.
*/
public function register_settings() {
register_setting(
'abhinavdev_click_to_chat_group',
ABHINAVDEV_C2C_OPTION_NAME,
array(
'type' => 'array',
'sanitize_callback' => array( $this, 'sanitize_settings' ),
'default' => Abhinavdev_WhatsApp_Chat_Core::get_default_settings(),
)
);
}
/**
* Sanitize all input fields.
*
* @param array $input Raw form data.
* @return array
*/
public function sanitize_settings( $input ) {
$output = array();
$defaults = Abhinavdev_WhatsApp_Chat_Core::get_default_settings();
if ( ! is_array( $input ) ) {
return $defaults;
}
// Checkbox flags.
$output['enabled'] = ! empty( $input['enabled'] ) ? 1 : 0;
$output['show_tooltip'] = ! empty( $input['show_tooltip'] ) ? 1 : 0;
$output['open_in_new_tab'] = ! empty( $input['open_in_new_tab'] ) ? 1 : 0;
$output['pulse_animation'] = ! empty( $input['pulse_animation'] ) ? 1 : 0;
$output['notification_badge'] = ! empty( $input['notification_badge'] ) ? 1 : 0;
$output['enable_business_hours'] = ! empty( $input['enable_business_hours'] ) ? 1 : 0;
$output['track_ga4'] = ! empty( $input['track_ga4'] ) ? 1 : 0;
$output['track_gtm'] = ! empty( $input['track_gtm'] ) ? 1 : 0;
$output['track_meta_pixel'] = ! empty( $input['track_meta_pixel'] ) ? 1 : 0;
// Phone number: digits only (wa.me format), 8-15 digits (E.164).
$phone = isset( $input['phone_number'] ) ? Abhinavdev_WhatsApp_Chat_Core::clean_phone( sanitize_text_field( $input['phone_number'] ) ) : '';
if ( '' !== $phone && ! Abhinavdev_WhatsApp_Chat_Core::is_valid_phone( $phone ) ) {
add_settings_error(
'abhinavdev_c2c_messages',
'abhinavdev_c2c_invalid_phone',
__( 'The WhatsApp number looks invalid. Use the full international number (8-15 digits) including the country code. The previous number was kept.', 'abhinavdev-visitor-chat-widget' ),
'error'
);
$previous = get_option( ABHINAVDEV_C2C_OPTION_NAME, array() );
$phone = ( is_array( $previous ) && isset( $previous['phone_number'] ) ) ? $previous['phone_number'] : '';
}
$output['phone_number'] = $phone;
// Text & Textarea fields.
$output['default_message'] = isset( $input['default_message'] ) ? sanitize_textarea_field( $input['default_message'] ) : $defaults['default_message'];
$output['tooltip_text'] = isset( $input['tooltip_text'] ) ? sanitize_text_field( $input['tooltip_text'] ) : $defaults['tooltip_text'];
$output['agent_name'] = isset( $input['agent_name'] ) ? sanitize_text_field( $input['agent_name'] ) : $defaults['agent_name'];
$output['agent_status'] = isset( $input['agent_status'] ) ? sanitize_text_field( $input['agent_status'] ) : $defaults['agent_status'];
$output['agent_avatar_url'] = isset( $input['agent_avatar_url'] ) ? esc_url_raw( $input['agent_avatar_url'] ) : '';
$output['welcome_message'] = isset( $input['welcome_message'] ) ? sanitize_textarea_field( $input['welcome_message'] ) : $defaults['welcome_message'];
$output['cta_btn_text'] = isset( $input['cta_btn_text'] ) ? sanitize_text_field( $input['cta_btn_text'] ) : $defaults['cta_btn_text'];
$output['input_placeholder'] = isset( $input['input_placeholder'] ) ? sanitize_text_field( $input['input_placeholder'] ) : $defaults['input_placeholder'];
$output['notification_count']= isset( $input['notification_count'] ) ? sanitize_text_field( $input['notification_count'] ) : '1';
$output['offline_message'] = isset( $input['offline_message'] ) ? sanitize_textarea_field( $input['offline_message'] ) : $defaults['offline_message'];
$output['exclude_ids'] = isset( $input['exclude_ids'] ) ? implode( ',', array_filter( array_map( 'absint', explode( ',', sanitize_text_field( $input['exclude_ids'] ) ) ) ) ) : '';
$output['timezone'] = isset( $input['timezone'] ) ? sanitize_text_field( $input['timezone'] ) : 'site';
if ( 'site' !== $output['timezone'] && ! in_array( $output['timezone'], timezone_identifiers_list(), true ) ) {
$output['timezone'] = 'site';
}
// Select options validation.
$allowed_positions = array( 'bottom_right', 'bottom_left', 'top_right', 'top_left' );
$output['button_position'] = ( isset( $input['button_position'] ) && in_array( $input['button_position'], $allowed_positions, true ) ) ? $input['button_position'] : 'bottom_right';
$output['button_style'] = ( isset( $input['button_style'] ) && array_key_exists( $input['button_style'], Abhinavdev_WhatsApp_Chat_Core::get_button_styles() ) ) ? $input['button_style'] : 'circle';
$output['button_size'] = ( isset( $input['button_size'] ) && array_key_exists( $input['button_size'], Abhinavdev_WhatsApp_Chat_Core::get_button_sizes() ) ) ? $input['button_size'] : 'medium';
$output['button_label'] = isset( $input['button_label'] ) ? sanitize_text_field( $input['button_label'] ) : $defaults['button_label'];
$allowed_types = array( 'popup', 'direct' );
$output['button_type'] = ( isset( $input['button_type'] ) && in_array( $input['button_type'], $allowed_types, true ) ) ? $input['button_type'] : 'popup';
$allowed_devices = array( 'all', 'mobile', 'desktop' );
$output['visibility_device'] = ( isset( $input['visibility_device'] ) && in_array( $input['visibility_device'], $allowed_devices, true ) ) ? $input['visibility_device'] : 'all';
$allowed_pages = array( 'all', 'home_only', 'posts_only', 'pages_only', 'woo_only', 'exclude' );
$output['visibility_pages'] = ( isset( $input['visibility_pages'] ) && in_array( $input['visibility_pages'], $allowed_pages, true ) ) ? $input['visibility_pages'] : 'all';
$allowed_offline = array( 'hide', 'offline_message' );
$output['offline_behavior'] = ( isset( $input['offline_behavior'] ) && in_array( $input['offline_behavior'], $allowed_offline, true ) ) ? $input['offline_behavior'] : 'offline_message';
// Numeric offsets & triggers.
$output['offset_bottom'] = isset( $input['offset_bottom'] ) ? absint( $input['offset_bottom'] ) : 24;
$output['offset_side'] = isset( $input['offset_side'] ) ? absint( $input['offset_side'] ) : 24;
$output['auto_open_delay'] = isset( $input['auto_open_delay'] ) ? absint( $input['auto_open_delay'] ) : 0;
$output['scroll_trigger'] = isset( $input['scroll_trigger'] ) ? min( 100, absint( $input['scroll_trigger'] ) ) : 0;
// Colors.
$output['theme_color'] = ( ! empty( $input['theme_color'] ) && sanitize_hex_color( $input['theme_color'] ) ) ? sanitize_hex_color( $input['theme_color'] ) : $defaults['theme_color'];
$output['theme_color_dark'] = ( ! empty( $input['theme_color_dark'] ) && sanitize_hex_color( $input['theme_color_dark'] ) ) ? sanitize_hex_color( $input['theme_color_dark'] ) : $defaults['theme_color_dark'];
$output['header_text_color'] = ( ! empty( $input['header_text_color'] ) && sanitize_hex_color( $input['header_text_color'] ) ) ? sanitize_hex_color( $input['header_text_color'] ) : $defaults['header_text_color'];
// Business Hours Schedule.
$days = array( 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday' );
$schedule_out = array();
foreach ( $days as $day ) {
$day_enabled = ! empty( $input['schedule'][ $day ]['enabled'] ) ? 1 : 0;
$start = isset( $input['schedule'][ $day ]['start'] ) ? sanitize_text_field( $input['schedule'][ $day ]['start'] ) : '09:00';
$end = isset( $input['schedule'][ $day ]['end'] ) ? sanitize_text_field( $input['schedule'][ $day ]['end'] ) : '18:00';
// Format HH:MM check.
if ( ! preg_match( '/^([01]\d|2[0-3]):[0-5]\d$/', $start ) ) {
$start = '09:00';
}
if ( ! preg_match( '/^([01]\d|2[0-3]):[0-5]\d$/', $end ) ) {
$end = '18:00';
}
$schedule_out[ $day ] = array(
'enabled' => $day_enabled,
'start' => $start,
'end' => $end,
);
}
$output['schedule'] = $schedule_out;
return $output;
}
/**
* Render the entire settings page view.
*/
public function render_settings_page() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
$options = Abhinavdev_WhatsApp_Chat_Core::get_settings();
?>