\n" . wp_unslash( $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" . wp_unslash( $chative ) . "\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } } } /** * 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 ), ] ); } 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(); $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; } $val = is_array( $value ) ? implode( ', ', $value ) : (string) $value; $msg .= '' . esc_html( ucfirst( (string) $key ) ) . ': ' . 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();