| Server IP : 91.240.85.141 / Your IP : 216.73.216.223 Web Server : nginx/1.28.0 System : Linux nuevo.ru 6.8.0-134-generic #134-Ubuntu SMP PREEMPT_DYNAMIC Fri Jun 26 18:43:11 UTC 2026 x86_64 User : ( 1029) PHP Version : 8.3.6 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /var/www/wpface_ru_usr/data/www/wpface.ru/wp-content/plugins/plh/inc/ |
Upload File : |
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class ScotChat {
public ?ScotTelegram $telegram;
function __construct() {
$this->telegram = ScotTelegram::getInstance();
add_action( 'wp_ajax_scot_send_message', [ $this, 'sendMessage' ] );
add_action( 'wp_ajax_nopriv_scot_send_message', [ $this, 'sendMessage' ] );
add_action( 'wp_ajax_scot_get_messages', [ $this, 'getMessages' ] );
add_action( 'wp_ajax_nopriv_scot_get_messages', [ $this, 'getMessages' ] );
add_action( 'wp_footer', [ $this, 'show_chat' ] );
add_action( 'scot_receive_data', [ $this, 'receiveData' ], 100 );
add_shortcode('scot_chat', [$this, 'add_shortcode']);
}
public function getMessages(): void {
check_ajax_referer( 'get_chat_nonce', 'security' );
$last_message_date = ! empty( $_POST['lastMessageDate'] ) ? intval( $_POST['lastMessageDate'] ) : 0;
$user = ScotUser::getOrCreateUser();
$user_id = $user['user_id'];
$thread = ScotThread::getThread( $user_id );
$messages = TgSupDB::getThreadMessages( $thread['thread_id'], $last_message_date );
foreach ( $messages as &$message ) {
if ( ! empty( $message['file_id'] ) ) {
$file_url = $this->telegram->getFileUrl( $message['file_id'] );
$message['message'] = '<img src="' . self::imageToBase64( $file_url ) . '">' . $message['message'];
}
self::convertTextUrlType( $message );
$message['formattedDate'] = $this->formattedDate( $message['date'] );
}
wp_send_json_success( $messages );
}
public static function allowed_html_tags() {
return [
'b' => [],
'i' => [],
'del' => [],
's' => [],
'u' => [],
'pre' => [],
'code' => [
'language' => true,
],
'a' => [
'href' => true,
'target' => true,
'title' => true,
],
];
}
public static function imageToBase64( $path ) {
$type = pathinfo( $path, PATHINFO_EXTENSION );
$response = wp_remote_get( $path );
if ( is_wp_error( $response ) ) {
return '';
}
$data = wp_remote_retrieve_body( $response );
return 'data:image/' . $type . ';base64,' . base64_encode( $data );
}
public function receiveData( $data ): void {
set_transient( 'scot_test', $data, 600 );
if ( empty( $data ) ) {
return;
}
$this->saveChat( $data );
$message_type = self::getMessageType( $data['message'] );
if ( ! $message_type ) {
return;
}
$message_text = ! empty( $data['message']['text'] ) ? $data['message']['text'] : '';
$file_id = '';
if ( $message_type === 'photo' ) {
$message_text = ! empty( $data['message']['caption'] ) ? $data['message']['caption'] : '';
$file_id = $data['message']['photo'][ count( $data['message']['photo'] ) - 1 ]['file_id'] ?? '';
}
TgSupDB::insert_message(
$data['message']['message_id'],
$data['message']['message_thread_id'],
$message_text,
wp_json_encode( $data['message'] ),
$data['message']['from']['id'],
1,
$data['message']['date'],
$message_type,
$file_id
);
}
public function saveChat( $data ): void {
if ( ! empty( $data['message']['chat']['id'] ) && $data['message']['chat']['type'] === 'supergroup' ) {
$chat_id = $data['message']['chat']['id'];
$chat_name = $data['message']['chat']['title'];
$tg_groups = get_option( 'scot_all_groups', [] );
if ( empty( $tg_groups[ $chat_id ] ) || $tg_groups[ $chat_id ] !== $chat_name ) {
$tg_groups[ sanitize_text_field( $chat_id ) ] = sanitize_text_field( $chat_name );
update_option( 'scot_all_groups', $tg_groups );
}
if ( ! empty( $data['message']['left_chat_member'] ) ) {
$bot_username = scot_get_option( 'bot_name', '' );
if ( ! empty( $tg_groups[ $chat_id ] ) && $data['message']['left_chat_member']['username'] === $bot_username ) {
unset( $tg_groups[ $chat_id ] );
update_option( 'scot_all_groups', $tg_groups );
}
}
}
}
/**
* @throws JsonException
*/
public function sendMessage(): void {
check_ajax_referer( 'send_message_nonce', 'security' );
$message = ! empty( $_REQUEST['message'] ) ? wp_kses_post( wp_unslash( $_REQUEST['message'] ) ) : '';
$user = ScotUser::getOrCreateUser();
$user_id = $user['user_id'];
$thread_object = new ScotThread();
$thread = $thread_object->getOrCreateThread( $user_id );
$thread_id = $thread['thread_id'];
if ( ( empty( $message ) ) ) {
wp_send_json_error( [] );
}
$message = ! empty( $_REQUEST['message'] ) ? wp_kses_post( wp_unslash( $_REQUEST['message'] ) ) : '';
$message_sent = $this->telegram->sendMessage( self::groupId(), $message, $thread_id );
if ( ! $message_sent ) {
wp_send_json_error( [] );
}
TgSupDB::insert_message(
$message_sent['message_id'],
$thread_id,
$message,
wp_json_encode( $message_sent ),
$message_sent['from']['id'],
0,
$message_sent['date'],
'text',
);
$user_messages = TgSupDB::getThreadMessages( $thread_id );
if ( count( $user_messages ) === 1 ) {
if ( scot_get_option( 'enable_auto_reply', false ) ) {
$auto_reply_message = scot_get_option( 'auto_reply_message', '' );
$auto_reply_message = wp_kses(wp_unslash($auto_reply_message), self::allowed_html_tags());
$auto_reply_message_sent = $this->telegram->sendMessage( self::groupId(), $auto_reply_message, $thread_id );
TgSupDB::insert_message(
$auto_reply_message_sent['message_id'],
$thread_id,
$auto_reply_message,
wp_json_encode( $auto_reply_message_sent ),
$auto_reply_message_sent['from']['id'],
1,
$auto_reply_message_sent['date'],
'text',
);
}
}
wp_send_json_success($message_sent);
}
public static function getMessageType( $message ) {
$types = [ 'text', 'photo' ];
foreach ( $types as $type ) {
if ( ! empty( $message[ $type ] ) ) {
return $type;
}
}
return false;
}
public function add_shortcode($atts) {
$atts = shortcode_atts( [
'is_open' => '0',
'is_fixed' => '1',
], $atts );
ob_start();
load_template(SCOT_DIR . '/templates/chat.php', false, $atts);
return ob_get_clean();
}
public function show_chat() {
$chat_enabled = scot_get_option( 'enable_widget', true );
if ( ! $chat_enabled ) {
return;
}
$is_chat_visible = self::isChatVisible();
if ( ! $is_chat_visible ) {
return;
}
$is_chat_hidden = self::isChatHidden();
if ( $is_chat_hidden ) {
return;
}
load_template(SCOT_DIR . '/templates/chat.php', false, []);
}
public static function groupId() {
return scot_get_option( 'tg_group' );
}
public static function isChatHidden() {
$is_hidden_on_front_page = self::isChatHiddenOnFrontPage();
if ( $is_hidden_on_front_page ) {
return true;
}
$is_hidden_on_blog_page = self::isChatHiddenOnPostType();
if ( $is_hidden_on_blog_page ) {
return true;
}
$is_hidden_on_blog_page = self::isChatHiddenOnTaxonomies();
if ( $is_hidden_on_blog_page ) {
return true;
}
return false;
}
public static function isChatHiddenOnTaxonomies() {
$queued_object = get_queried_object();
if ( is_null( $queued_object ) || ! isset( $queued_object->term_id ) ) {
return false;
}
$current_taxonomy = $queued_object->taxonomy;
$hidden_taxonomies = array_column(
scot_get_option( 'hidden_taxonomy', [] ),
'value'
);
if ( in_array( $current_taxonomy, $hidden_taxonomies, true ) ) {
return true;
}
$term_id = $queued_object->term_id;
$hidden_terms = scot_get_option( 'hidden_taxonomy_category', [] );
if ( in_array( $term_id, $hidden_terms, true ) ) {
return true;
}
return false;
}
public static function isChatHiddenOnPostType() {
$post_types = get_post_types( [ 'public' => true ], 'objects' );
$post_types_data = [];
foreach ( $post_types as $post_type ) {
$post_types_data[] = $post_type->name;
}
$queued_object = get_queried_object();
if ( is_null( $queued_object ) ) {
return false;
}
if ( ! in_array( $queued_object->post_type, $post_types_data, true ) ) {
return false;
}
$excluded_post_types = scot_get_option( 'exclude_post_type', [] );
if ( empty( $excluded_post_types ) ) {
return false;
}
if ( isset( $queued_object->post_type )
&& in_array( $queued_object->post_type, array_column( $excluded_post_types, 'value' ), true )
) {
return true;
}
// Check specific posts visibility
$specific_hidden_posts = scot_get_option( 'hidden_posts', [] );
if ( in_array( $queued_object->ID, $specific_hidden_posts, true ) ) {
return true;
}
return false;
}
public static function isChatHiddenOnFrontPage() {
if ( ! is_front_page() ) {
return false;
}
return scot_get_option( 'hidden_front_page', false );
}
public static function isChatVisible() {
$visible_entire_site = scot_get_option( 'visible_entire_site', true );
if ( $visible_entire_site ) {
return true;
}
$is_visible_on_front_page = self::isChatVisibleOnFrontPage();
if ( $is_visible_on_front_page ) {
return true;
}
$is_chat_visible_on_post_type = self::isChatVisibleOnPostType();
if ( ! $is_chat_visible_on_post_type ) {
return false;
}
$is_chat_visible_on_taxonomy = self::isChatVisibleOnTaxonomies();
if ( ! $is_chat_visible_on_taxonomy ) {
return false;
}
return true;
}
public static function isChatVisibleOnPostType() {
$post_types = get_post_types( [ 'public' => true ], 'objects' );
$post_types_data = [];
foreach ( $post_types as $post_type ) {
$post_types_data[] = $post_type->name;
}
$queued_object = get_queried_object();
if ( is_null( $queued_object ) ) {
return true;
}
if ( ! in_array( $queued_object->post_type, $post_types_data, true ) ) {
return true;
}
$visible_post_types = scot_get_option( 'visible_post_type', [] );
if ( empty( $visible_post_types ) ) {
return false;
}
if ( isset( $queued_object->post_type )
&& in_array( $queued_object->post_type, array_column( $visible_post_types, 'value' ), true )
) {
return true;
}
// Check specific posts visibility
$specific_visible_posts = scot_get_option( 'visible_posts', [] );
if ( in_array( $queued_object->ID, $specific_visible_posts, true ) ) {
return true;
}
return false;
}
public static function isChatVisibleOnTaxonomies() {
$queued_object = get_queried_object();
// Return true if there's no queried object
if ( is_null( $queued_object ) || ! isset( $queued_object->term_id ) ) {
return true;
}
$current_taxonomy = $queued_object->taxonomy;
$visible_taxonomies = array_column(
scot_get_option( 'visible_taxonomy', [] ),
'value'
);
if ( in_array( $current_taxonomy, $visible_taxonomies, true ) ) {
return true;
}
$term_id = $queued_object->term_id;
$visible_terms = scot_get_option( 'visible_taxonomy_category', [] );
if ( in_array( $term_id, $visible_terms, true ) ) {
return true;
}
return false;
}
private static function isChatVisibleOnFrontPage() {
if ( ! is_front_page() ) {
return true;
}
return scot_get_option( 'visible_front_page', true );
}
private static function isChatVisibleOnBlogPage() {
global $wp_query;
if ( $wp_query->query['pagename'] !== 'blog' ) {
return true;
}
return scot_get_option( 'visible_blog_page', true );
}
public function formattedDate( $timestamp ) {
$date_format = get_option( 'date_format' );
$time_format = get_option( 'time_format' );
$formatted_date = date_i18n( $date_format, $timestamp );
$formatted_time = date_i18n( $time_format, $timestamp );
$today_date = date_i18n( $date_format, current_time( 'timestamp' ) );
if ( $formatted_date === $today_date ) {
return $formatted_time;
} else {
return $formatted_date . ' ' . $formatted_time;
}
}
public static function convertTextUrlType( &$message ) {
$message_data = json_decode( $message['message_data'], true );
if ( empty( $message_data['entities'] ) ) {
return;
}
$entities = $message_data['entities'];
$newMessage = $message_data['text'];
$offsetAdjustment = 0;
foreach ( $entities as $entity ) {
if ( $entity['type'] === 'text_link' ) {
$actualOffset = $entity['offset'] + $offsetAdjustment;
$textToReplace = mb_substr( $newMessage, $actualOffset, $entity['length'], 'UTF-8' );
$replacement = '<a href="' . $entity['url'] . '" target="_blank">' . $textToReplace . '</a>';
$newMessage = mb_substr( $newMessage, 0, $actualOffset, 'UTF-8' ) . $replacement . mb_substr( $newMessage, $actualOffset + $entity['length'], null, 'UTF-8' );
$offsetAdjustment += mb_strlen( $replacement, 'UTF-8' ) - $entity['length'];
}
}
$message['message'] = $newMessage;
}
public static function isWorkingTime(): bool {
$always_enabled = scot_get_option( 'always_enabled' );
if ( ! empty( $always_enabled ) ) {
return true;
}
$currentDateTime = current_time('Y-m-d H:i');
$currentDay = strtolower(date('l', strtotime($currentDateTime)));
$dayOffKey = $currentDay . '_day_off';
if ( ! empty( scot_get_option( $dayOffKey ) )) {
return false;
}
$startKey = $currentDay . '_start';
$endKey = $currentDay . '_end';
$start_time = scot_get_option($startKey);
$end_time = scot_get_option($endKey);
if ( !empty($start_time) && !empty($end_time) ) {
$currentTime = date('H:i', strtotime($currentDateTime));
$startTime = $start_time;
$endTime = $end_time;
if ( $currentTime >= $startTime && $currentTime <= $endTime ) {
return true;
}
}
return false;
}
}