1, 'phone' => '', 'show_desktop' => 1, 'show_mobile' => 1, 'position' => 'right', 'size' => 48, 'offset_x' => 20, 'offset_y' => 20, 'color' => '#25d366', 'label' => '', 'msg_product' => '', 'msg_general' => '', 'product_types' => array( 'product' ), 'woo_button' => 1, 'woo_text' => '', 'note' => 1, 'note_text' => '', 'privacy_url' => '', 'menu' => 0, 'menu_items' => array( 'whatsapp', 'call', 'consent', 'top' ), 'menu_phone' => '', 'menu_email' => '', 'menu_map' => '', 'menu_color' => '#4f63e0', 'menu_dark' => '#3442a8', 'menu_wa_color' => 1, 'menu_radius' => 12, 'menu_hide_consent' => 1, 'menu_hide_top' => 0, 'menu_hide_sel' => '', ); const MENU_KEYS = array( 'whatsapp', 'call', 'email', 'map', 'consent', 'top' ); const RADIUS_MAX = 28; const TOP_PRESET = '.progress-wrap,.scroll-top,.scroll-to-top,.scrolltop,.scrollup,#scrollUp,#scroll-top,.back-to-top,#back-to-top,.backtotop,#backtotop,.go-top,#toTop,.scrollToTop'; const TEXT_KEYS = array( 'label', 'msg_product', 'msg_general', 'woo_text', 'note_text' ); public static function all(): array { $v = get_option( self::OPT, array() ); return array_merge( self::DEFAULTS, is_array( $v ) ? $v : array() ); } public static function phone( $raw ): string { $d = preg_replace( '/\D+/', '', (string) $raw ); if ( 0 === strpos( (string) $d, '00' ) ) { $d = substr( $d, 2 ); } $n = strlen( (string) $d ); return ( $n >= 8 && $n <= 15 && '0' !== $d[0] ) ? (string) $d : ''; } private static function int_in( $v, int $min, int $max, int $def ): int { if ( ! is_numeric( $v ) ) { return $def; } $i = (int) $v; return ( $i >= $min && $i <= $max ) ? $i : $def; } public static function sanitize( array $in ): array { $out = self::DEFAULTS; $out['enabled'] = empty( $in['enabled'] ) ? 0 : 1; $out['phone'] = self::phone( isset( $in['phone'] ) ? $in['phone'] : '' ); $out['show_desktop'] = empty( $in['show_desktop'] ) ? 0 : 1; $out['show_mobile'] = empty( $in['show_mobile'] ) ? 0 : 1; $out['position'] = ( isset( $in['position'] ) && 'left' === $in['position'] ) ? 'left' : 'right'; $out['size'] = self::int_in( isset( $in['size'] ) ? $in['size'] : '', self::SIZE_MIN, self::SIZE_MAX, 48 ); $out['offset_x'] = self::int_in( isset( $in['offset_x'] ) ? $in['offset_x'] : '', 0, 400, 20 ); $out['offset_y'] = self::int_in( isset( $in['offset_y'] ) ? $in['offset_y'] : '', 0, 400, 20 ); $c = isset( $in['color'] ) ? strtolower( trim( (string) $in['color'] ) ) : ''; $out['color'] = preg_match( '/^#[0-9a-f]{6}$/', $c ) ? $c : self::DEFAULTS['color']; foreach ( array( 'label', 'woo_text' ) as $k ) { $out[ $k ] = isset( $in[ $k ] ) ? sanitize_text_field( (string) $in[ $k ] ) : ''; } foreach ( array( 'msg_product', 'msg_general', 'note_text' ) as $k ) { $out[ $k ] = isset( $in[ $k ] ) ? sanitize_textarea_field( (string) $in[ $k ] ) : ''; } $types = array(); if ( isset( $in['product_types'] ) && is_array( $in['product_types'] ) ) { foreach ( $in['product_types'] as $t ) { $t = sanitize_key( (string) $t ); if ( '' !== $t && post_type_exists( $t ) ) { $types[] = $t; } } } $out['product_types'] = array_values( array_unique( $types ) ); $out['woo_button'] = empty( $in['woo_button'] ) ? 0 : 1; $out['note'] = empty( $in['note'] ) ? 0 : 1; $url = isset( $in['privacy_url'] ) ? esc_url_raw( trim( (string) $in['privacy_url'] ), array( 'http', 'https' ) ) : ''; $out['privacy_url'] = (string) $url; $out['menu'] = empty( $in['menu'] ) ? 0 : 1; $out['menu_items'] = self::menu_items_in( $in ); $out['menu_phone'] = self::tel( isset( $in['menu_phone'] ) ? $in['menu_phone'] : '' ); $mail = isset( $in['menu_email'] ) ? sanitize_email( (string) $in['menu_email'] ) : ''; $out['menu_email'] = is_email( $mail ) ? $mail : ''; $out['menu_map'] = isset( $in['menu_map'] ) ? (string) esc_url_raw( trim( (string) $in['menu_map'] ), array( 'http', 'https' ) ) : ''; foreach ( array( 'menu_color', 'menu_dark' ) as $k ) { $c = isset( $in[ $k ] ) ? strtolower( trim( (string) $in[ $k ] ) ) : ''; $out[ $k ] = preg_match( '/^#[0-9a-f]{6}$/', $c ) ? $c : self::DEFAULTS[ $k ]; } $out['menu_wa_color'] = empty( $in['menu_wa_color'] ) ? 0 : 1; $out['menu_radius'] = self::int_in( isset( $in['menu_radius'] ) ? $in['menu_radius'] : '', 0, self::RADIUS_MAX, self::DEFAULTS['menu_radius'] ); $out['menu_hide_consent'] = empty( $in['menu_hide_consent'] ) ? 0 : 1; $out['menu_hide_top'] = empty( $in['menu_hide_top'] ) ? 0 : 1; $out['menu_hide_sel'] = self::selectors( isset( $in['menu_hide_sel'] ) ? $in['menu_hide_sel'] : '' ); return $out; } private static function menu_items_in( array $in ): array { if ( isset( $in['menu_on'] ) || isset( $in['menu_pos'] ) ) { $on = isset( $in['menu_on'] ) && is_array( $in['menu_on'] ) ? $in['menu_on'] : array(); $pos = isset( $in['menu_pos'] ) && is_array( $in['menu_pos'] ) ? $in['menu_pos'] : array(); $row = array(); foreach ( self::MENU_KEYS as $i => $k ) { if ( empty( $on[ $k ] ) ) { continue; } $p = isset( $pos[ $k ] ) && is_numeric( $pos[ $k ] ) ? (int) $pos[ $k ] : 100 + $i; $row[] = array( $p, $i, $k ); } usort( $row, function ( $a, $b ) { return $a[0] === $b[0] ? $a[1] - $b[1] : $a[0] - $b[0]; } ); return array_values( array_map( function ( $r ) { return $r[2]; }, $row ) ); } $list = isset( $in['menu_items'] ) && is_array( $in['menu_items'] ) ? $in['menu_items'] : self::DEFAULTS['menu_items']; $out = array(); foreach ( $list as $k ) { $k = sanitize_key( (string) $k ); if ( in_array( $k, self::MENU_KEYS, true ) && ! in_array( $k, $out, true ) ) { $out[] = $k; } } return $out; } public static function tel( $raw ): string { $raw = trim( (string) $raw ); $plus = 0 === strpos( $raw, '+' ) || 0 === strpos( preg_replace( '/\s+/', '', $raw ), '00' ); $d = preg_replace( '/\D+/', '', $raw ); if ( $plus && 0 === strpos( (string) $d, '00' ) ) { $d = substr( (string) $d, 2 ); } $n = strlen( (string) $d ); if ( $n < 3 || $n > 15 ) { return ''; } return ( $plus ? '+' : '' ) . $d; } public static function selectors( $raw ): string { $s = preg_replace( '/[^A-Za-z0-9_\-#.,:\s>\[\]="\'()*+~^$|]/', '', (string) $raw ); $parts = array(); foreach ( explode( ',', (string) $s ) as $p ) { $p = trim( preg_replace( '/\s+/', ' ', $p ) ); if ( '' === $p || in_array( $p, $parts, true ) || strlen( implode( ', ', array_merge( $parts, array( $p ) ) ) ) > 500 ) { continue; } $parts[] = $p; } return implode( ', ', $parts ); } public static function translated( string $key ): string { $s = self::all(); $v = isset( $s[ $key ] ) ? (string) $s[ $key ] : ''; if ( '' === $v ) { return ''; } if ( function_exists( 'pll__' ) ) { return (string) pll__( $v ); } return (string) apply_filters( 'wpml_translate_single_string', $v, 'ason-chat-button', $key ); } public static function register_strings(): void { $s = self::all(); foreach ( self::TEXT_KEYS as $k ) { $v = (string) $s[ $k ]; if ( '' === $v ) { continue; } if ( function_exists( 'pll_register_string' ) ) { pll_register_string( 'ason-chat-button-' . $k, $v, 'Ason Chat Button', false !== strpos( $k, 'msg' ) || 'note_text' === $k ); } do_action( 'wpml_register_single_string', 'ason-chat-button', $k, $v ); } $u = (string) $s['privacy_url']; if ( '' !== $u ) { if ( function_exists( 'pll_register_string' ) ) { pll_register_string( 'ason-chat-button-privacy_url', $u, 'Ason Chat Button', false ); } do_action( 'wpml_register_single_string', 'ason-chat-button', 'privacy_url', $u ); } } public static function fg( string $hex, float $min ): string { $lum = 0.0; foreach ( array( array( 0.2126, 1 ), array( 0.7152, 3 ), array( 0.0722, 5 ) ) as $p ) { $c = hexdec( substr( $hex, $p[1], 2 ) ) / 255; $c = $c <= 0.03928 ? $c / 12.92 : pow( ( $c + 0.055 ) / 1.055, 2.4 ); $lum += $p[0] * $c; } return ( 1.05 / ( $lum + 0.05 ) ) >= $min ? '#fff' : '#0b141a'; } public static function privacy_url(): string { $s = self::all(); $raw = (string) $s['privacy_url']; if ( '' === $raw ) { $url = function_exists( 'get_privacy_policy_url' ) ? (string) get_privacy_policy_url() : ''; $pid = '' === $url ? 0 : (int) get_option( 'wp_page_for_privacy_policy' ); } else { $tr = self::translated( 'privacy_url' ); if ( '' !== $tr && $tr !== $raw ) { $tr = esc_url_raw( trim( $tr ), array( 'http', 'https' ) ); if ( '' !== $tr ) { return $tr; } } $url = $raw; $pid = self::local_page_id( $raw ); } if ( $pid > 0 ) { $tid = self::in_current_language( $pid ); if ( $tid > 0 && $tid !== $pid ) { $link = get_permalink( $tid ); if ( $link ) { return (string) $link; } } } return $url; } private static function in_current_language( int $pid ): int { if ( function_exists( 'pll_get_post' ) ) { $t = (int) pll_get_post( $pid ); } else { $type = (string) get_post_type( $pid ); $t = (int) apply_filters( 'wpml_object_id', $pid, '' !== $type ? $type : 'page', false ); } return $t > 0 && 'publish' === get_post_status( $t ) ? $t : 0; } private static function local_page_id( string $url ): int { $hp = wp_parse_url( (string) get_option( 'home' ) ); $up = wp_parse_url( $url ); if ( ! is_array( $hp ) || ! is_array( $up ) || empty( $up['host'] ) || empty( $hp['host'] ) || strtolower( (string) $up['host'] ) !== strtolower( (string) $hp['host'] ) ) { return 0; } if ( ! empty( $up['query'] ) ) { parse_str( (string) $up['query'], $q ); foreach ( array( 'page_id', 'p' ) as $k ) { if ( ! empty( $q[ $k ] ) && (int) $q[ $k ] > 0 ) { return (int) $q[ $k ]; } } } $path = isset( $up['path'] ) ? (string) $up['path'] : '/'; $base = isset( $hp['path'] ) ? untrailingslashit( (string) $hp['path'] ) : ''; if ( '' !== $base && 0 === strpos( $path, $base ) ) { $path = (string) substr( $path, strlen( $base ) ); } $path = trim( $path, '/' ); if ( '' === $path ) { return 0; } if ( function_exists( 'pll_languages_list' ) ) { $seg = (string) strtok( $path, '/' ); if ( in_array( $seg, (array) pll_languages_list(), true ) ) { $path = trim( (string) substr( $path, strlen( $seg ) ), '/' ); } } if ( '' === $path ) { return 0; } $page = get_page_by_path( $path, OBJECT, 'page' ); return $page instanceof \WP_Post ? (int) $page->ID : 0; } }