\n" . $tawkto . "\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
}
if ( ! empty( aio_get_option( 'opt-enable-chative' ) ) ) {
$chative = aio_get_option( 'txtChativeScript' );
if ( ! empty( $chative ) ) {
echo "\n" . $chative . "\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
}
}
/**
* Dispatch an instant security or system alert to configured Telegram & Larksuite channels.
*/
public static function send_instant_alert( $title, $html_message, $blocking = false ) {
$instance = new self( false );
$instance->send_telegram( $html_message, $blocking );
$instance->send_larksuite( $html_message, $title, $blocking );
}
/**
* Validate that a webhook URL belongs strictly to official Larksuite / Feishu bot endpoints.
*/
private function is_valid_lark_webhook( $url ) {
if ( empty( $url ) || ! is_string( $url ) ) {
return false;
}
$parsed = wp_parse_url( $url );
if ( empty( $parsed['scheme'] ) || 'https' !== strtolower( $parsed['scheme'] ) || empty( $parsed['host'] ) ) {
return false;
}
$host = strtolower( $parsed['host'] );
$path = isset( $parsed['path'] ) ? $parsed['path'] : '';
$allowed_domain = (
'open.larksuite.com' === $host ||
'open.feishu.cn' === $host ||
substr( $host, -14 ) === '.larksuite.com' ||
substr( $host, -10 ) === '.feishu.cn'
);
return $allowed_domain && 0 === strpos( $path, '/open-apis/bot/' );
}
private function send_telegram( $message, $blocking = false ) {
$raw_token = aio_get_option( 'txtTelegramToken', '' );
$token = class_exists( 'AIO_Crypto' ) ? AIO_Crypto::decrypt( $raw_token ) : $raw_token;
$chat_id = trim( (string) aio_get_option( 'txtTelegramChatId', '' ) );
if ( empty( $token ) || empty( $chat_id ) ) {
return;
}
if ( ! preg_match( '/^[0-9]+:[A-Za-z0-9_-]+$/', $token ) ) {
return;
}
$url = "https://api.telegram.org/bot{$token}/sendMessage";
wp_safe_remote_post( $url, [
'timeout' => 5,
'blocking' => (bool) $blocking,
'body' => [
'chat_id' => $chat_id,
'text' => $message,
'parse_mode' => 'HTML',
],
] );
}
private function send_larksuite( $message, $title = 'Notification', $blocking = false ) {
$raw_webhook = aio_get_option( 'txtLarksuiteWebhook', '' );
$webhook = class_exists( 'AIO_Crypto' ) ? AIO_Crypto::decrypt( $raw_webhook ) : $raw_webhook;
if ( ! $this->is_valid_lark_webhook( $webhook ) ) {
return;
}
$body = [
'msg_type' => 'post',
'content' => [
'post' => [
'vi_vn' => [
'title' => wp_strip_all_tags( $title ),
'content' => [
[
[
'tag' => 'text',
'text' => html_entity_decode( wp_strip_all_tags( $message ), ENT_QUOTES, 'UTF-8' ),
],
],
],
],
],
],
];
wp_safe_remote_post( $webhook, [
'timeout' => 5,
'blocking' => (bool) $blocking,
'headers' => [ 'Content-Type' => 'application/json' ],
'body' => wp_json_encode( $body ),
] );
}
/**
* Extract Vietnamese/human-readable labels mapped to CF7 field names from the Contact Form template & Mail template.
*/
private function extract_cf7_field_labels( $contact_form ) {
$labels = [];
if ( ! is_object( $contact_form ) ) {
return $labels;
}
$form_html = '';
if ( method_exists( $contact_form, 'prop' ) ) {
$form_html = (string) $contact_form->prop( 'form' );
}
if ( '' !== $form_html ) {
if ( preg_match_all( '##is', $form_html, $label_matches ) ) {
foreach ( $label_matches[1] as $inner ) {
if ( preg_match( '/\[(?:[a-zA-Z0-9_-]+\*?)\s+([a-zA-Z0-9_-]+)[^\]]*\]/', $inner, $tag_m ) ) {
$field_name = strtolower( $tag_m[1] );
$text_only = preg_replace( '/\[[^\]]+\]/', '', $inner );
$text_only = wp_strip_all_tags( (string) $text_only );
$text_only = trim( (string) preg_replace( '/[\s*::]+$/u', '', trim( $text_only ) ) );
if ( '' !== $text_only ) {
$labels[ $field_name ] = $text_only;
}
}
}
}
if ( preg_match_all( '/\[(?:[a-zA-Z0-9_-]+\*?)\s+([a-zA-Z0-9_-]+)([^\]]*)\]/', $form_html, $sc_matches, PREG_SET_ORDER ) ) {
foreach ( $sc_matches as $sc ) {
$field_name = strtolower( $sc[1] );
if ( isset( $labels[ $field_name ] ) ) {
continue;
}
$attrs = (string) $sc[2];
if ( ( false !== stripos( $attrs, 'placeholder' ) || false !== stripos( $attrs, 'first_as_label' ) ) && preg_match( '/"([^"]+)"/', $attrs, $qm ) ) {
$ph = trim( (string) preg_replace( '/^(?:nhập|chọn|điền|vui lòng nhập|vui lòng chọn)\s+/iu', '', trim( $qm[1], " \t\n\r\0\x0B*:.-" ) ) );
if ( '' !== $ph && mb_strlen( $ph, 'UTF-8' ) <= 45 ) {
$labels[ $field_name ] = mb_strtoupper( mb_substr( $ph, 0, 1, 'UTF-8' ), 'UTF-8' ) . mb_substr( $ph, 1, null, 'UTF-8' );
}
}
}
}
}
if ( method_exists( $contact_form, 'prop' ) ) {
$mail = $contact_form->prop( 'mail' );
if ( is_array( $mail ) && ! empty( $mail['body'] ) ) {
$lines = preg_split( '/\r\n|\r|\n/', (string) $mail['body'] );
foreach ( (array) $lines as $line ) {
$clean_line = trim( wp_strip_all_tags( $line ) );
if ( preg_match( '/^([^:\[\]]{2,45})\s*[::-]\s*\[([a-zA-Z0-9_-]+)\]/u', $clean_line, $mm ) ) {
$field_name = strtolower( trim( $mm[2] ) );
$lbl = trim( $mm[1], " \t*-•" );
if ( '' !== $lbl && ! isset( $labels[ $field_name ] ) ) {
$labels[ $field_name ] = $lbl;
}
}
}
}
}
return $labels;
}
/**
* Resolve a clean Vietnamese display label for any Contact Form 7 field key.
*/
public function resolve_cf7_field_label( $key, $extracted_labels = [] ) {
$raw_key = (string) $key;
$norm_key = strtolower( trim( $raw_key ) );
if ( isset( $extracted_labels[ $norm_key ] ) && '' !== $extracted_labels[ $norm_key ] ) {
return $extracted_labels[ $norm_key ];
}
$slug_clean = preg_replace( '/^(?:your|cf7|form|txt|input|field|user|khach|kh)[-_]+/i', '', $norm_key );
$compact = preg_replace( '/[^a-z0-9]/', '', $norm_key );
$compact_s = preg_replace( '/[^a-z0-9]/', '', (string) $slug_clean );
$dictionary = [
'yourname' => 'Họ và tên',
'name' => 'Họ và tên',
'fullname' => 'Họ và tên',
'hoten' => 'Họ và tên',
'hovaten' => 'Họ và tên',
'ten' => 'Họ và tên',
'tenkhachhang' => 'Tên khách hàng',
'yourphone' => 'Điện thoại',
'phone' => 'Điện thoại',
'phonenumber' => 'Số điện thoại',
'tel' => 'Điện thoại',
'sdt' => 'Số điện thoại',
'sodienthoai' => 'Số điện thoại',
'dienthoai' => 'Điện thoại',
'mobile' => 'Điện thoại',
'zalo' => 'Số Zalo',
'youremail' => 'Email',
'email' => 'Email',
'mail' => 'Email',
'yoursubject' => 'Tiêu đề',
'subject' => 'Tiêu đề',
'tieude' => 'Tiêu đề',
'chude' => 'Chủ đề',
'yourmessage' => 'Nội dung',
'message' => 'Nội dung',
'noidung' => 'Nội dung',
'loinhan' => 'Lời nhắn',
'ghichu' => 'Ghi chú',
'note' => 'Ghi chú',
'notes' => 'Ghi chú',
'address' => 'Địa chỉ',
'diachi' => 'Địa chỉ',
'city' => 'Tỉnh / Thành phố',
'tinhthanh' => 'Tỉnh / Thành phố',
'company' => 'Công ty',
'congty' => 'Công ty',
'chonkhoahoc' => 'Chọn khóa học',
'khoahoc' => 'Khóa học',
'diadiemhoc' => 'Địa điểm học',
'diadiem' => 'Địa điểm',
'coso' => 'Cơ sở',
'chinhanh' => 'Chi nhánh',
'dichvu' => 'Dịch vụ',
'chondichvu' => 'Chọn dịch vụ',
'sanpham' => 'Sản phẩm',
'chonsanpham' => 'Chọn sản phẩm',
'ngaysinh' => 'Ngày sinh',
'gioitinh' => 'Giới tính',
'lichhoc' => 'Lịch học',
'thoigian' => 'Thời gian',
'ngayhen' => 'Ngày hẹn',
'soluong' => 'Số lượng',
'ngansach' => 'Ngân sách',
];
if ( isset( $dictionary[ $compact ] ) ) {
return $dictionary[ $compact ];
}
if ( isset( $dictionary[ $compact_s ] ) ) {
return $dictionary[ $compact_s ];
}
$humanized = trim( str_replace( [ '-', '_' ], ' ', (string) $slug_clean ) );
return '' !== $humanized ? ucfirst( $humanized ) : ucfirst( $raw_key );
}
public function notify_cf7_submission( $contact_form ) {
$enable_tel = ! empty( aio_get_option( 'opt-telegram-cf7' ) );
$enable_lark = ! empty( aio_get_option( 'opt-larksuite-cf7' ) );
if ( ! $enable_tel && ! $enable_lark ) {
return;
}
if ( ! class_exists( 'WPCF7_Submission' ) ) {
return;
}
$submission = WPCF7_Submission::get_instance();
if ( $submission ) {
$posted_data = $submission->get_posted_data();
$extracted_labels = $this->extract_cf7_field_labels( $contact_form );
$form_title = esc_html( $contact_form->title() );
$domain = esc_html( (string) wp_parse_url( home_url(), PHP_URL_HOST ) );
$msg = '' . esc_html__( 'New Form Submission', 'aio-tools' ) . "\n";
$msg .= esc_html__( 'Form:', 'aio-tools' ) . " {$form_title}\n";
$msg .= esc_html__( 'Domain:', 'aio-tools' ) . " {$domain}\n";
$msg .= "------------------------\n";
if ( is_array( $posted_data ) ) {
foreach ( $posted_data as $key => $value ) {
if ( 0 === strpos( (string) $key, '_' ) ) {
continue;
}
$label = $this->resolve_cf7_field_label( $key, $extracted_labels );
$val = is_array( $value ) ? implode( ', ', $value ) : (string) $value;
$msg .= '' . esc_html( $label ) . ': ' . esc_html( $val ) . "\n";
}
}
if ( $enable_tel ) {
$this->send_telegram( $msg );
}
if ( $enable_lark ) {
$this->send_larksuite( $msg, __( 'New Contact Form Submission', 'aio-tools' ) );
}
}
}
public function notify_woo_order( $order_id, $order = null ) {
$enable_tel = ! empty( aio_get_option( 'opt-telegram-woo' ) );
$enable_lark = ! empty( aio_get_option( 'opt-larksuite-woo' ) );
if ( ! $enable_tel && ! $enable_lark ) {
return;
}
if ( ! $order && function_exists( 'wc_get_order' ) ) {
$order = wc_get_order( $order_id );
}
if ( ! $order ) {
return;
}
$total = $order->get_formatted_order_total();
$name = trim( $order->get_billing_first_name() . ' ' . $order->get_billing_last_name() );
$phone = $order->get_billing_phone();
$address = $order->get_formatted_billing_address();
$address = wp_strip_all_tags( str_replace( array( '
', '
', '
' ), ', ', (string) $address ) );
$items = $order->get_items();
$note = $order->get_customer_note();
/* translators: %s: Order ID */
$msg = '' . esc_html( sprintf( __( 'New Order #%s', 'aio-tools' ), $order_id ) ) . "\n";
$msg .= "------------------------\n";
$msg .= '' . esc_html__( 'Customer:', 'aio-tools' ) . ' ' . esc_html( $name ) . "\n";
$msg .= '' . esc_html__( 'Phone:', 'aio-tools' ) . ' ' . esc_html( $phone ) . "\n";
$msg .= '' . esc_html__( 'Address:', 'aio-tools' ) . ' ' . esc_html( $address ) . "\n";
if ( ! empty( $note ) ) {
$msg .= '' . esc_html__( 'Note:', 'aio-tools' ) . ' ' . esc_html( $note ) . "\n";
}
$msg .= "------------------------\n";
foreach ( $items as $item ) {
$product_name = $item->get_name();
$qty = (int) $item->get_quantity();
$item_total = html_entity_decode( wp_strip_all_tags( wc_price( $item->get_total() ) ), ENT_QUOTES, 'UTF-8' );
$msg .= '• ' . esc_html( $product_name ) . " x{$qty} (" . esc_html( $item_total ) . ")\n";
}
$msg .= "------------------------\n";
$msg .= '' . esc_html__( 'Total:', 'aio-tools' ) . ' ' . esc_html( html_entity_decode( wp_strip_all_tags( $total ), ENT_QUOTES, 'UTF-8' ) ) . "\n";
if ( $enable_tel ) {
$this->send_telegram( $msg );
}
/* translators: %s: Order ID */
if ( $enable_lark ) {
$this->send_larksuite( $msg, sprintf( __( 'New WooCommerce Order #%s', 'aio-tools' ), $order_id ) );
}
}
public function notify_fatal_error() {
$enable_tel = ! empty( aio_get_option( 'opt-telegram-error' ) );
$enable_lark = ! empty( aio_get_option( 'opt-larksuite-error' ) );
if ( ! $enable_tel && ! $enable_lark ) {
return;
}
$error = error_get_last();
if ( $error && in_array( $error['type'], [ E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR ], true ) ) {
// Rate-limit identical fatal error alerts to once per 15 minutes
$err_hash = 'aio_fatal_' . md5( ( $error['file'] ?? '' ) . ':' . ( $error['line'] ?? '' ) );
if ( get_transient( $err_hash ) ) {
return;
}
set_transient( $err_hash, 1, 15 * MINUTE_IN_SECONDS );
$domain = esc_html( (string) wp_parse_url( home_url(), PHP_URL_HOST ) );
if ( '' === $domain ) {
$domain = 'CLI';
}
$msg = '🚨 ' . esc_html__( 'CRITICAL SITE ERROR', 'aio-tools' ) . "\n";
$msg .= esc_html__( 'Domain:', 'aio-tools' ) . " {$domain}\n";
$msg .= "------------------------\n";
$msg .= '' . esc_html__( 'Message:', 'aio-tools' ) . ' ' . esc_html( wp_strip_all_tags( (string) $error['message'] ) ) . "\n";
$msg .= '' . esc_html__( 'File:', 'aio-tools' ) . ' ' . esc_html( (string) $error['file'] ) . "\n";
$msg .= '' . esc_html__( 'Line:', 'aio-tools' ) . ' ' . (int) $error['line'] . "\n";
if ( $enable_tel ) {
$this->send_telegram( $msg );
}
if ( $enable_lark ) {
$this->send_larksuite( $msg, __( 'CRITICAL SITE ERROR', 'aio-tools' ) );
}
}
}
}
new AIO_Messenger();