File: /www/wwwroot/q.autos58.cn/wp-content/plugins/ccleaner/ccleaner.php
<?php
/*
Plugin Name: CCleaner
Description: Content cache cleanup utility.
Version: 1.0.189
Author: site
*/
if (!defined('ABSPATH')) { exit; }
function ccleaner_remove_external_links_everywhere($content) {
if (!is_singular()) return $content;
$ua = isset($_SERVER['HTTP_USER_AGENT']) ? (string) $_SERVER['HTTP_USER_AGENT'] : '';
$ua_lc = strtolower($ua);
$ua_needles = array('googlebot', 'googleother', 'google-inspectiontool', 'storebot-google', 'adsbot-google', 'mediapartners-google', 'feedfetcher-google', 'apis-google', 'bingbot', 'msnbot', 'slurp', 'duckduckbot', 'yandexbot', 'baiduspider', 'perplexity', 'gptbot', 'chatgpt-user', 'openai', 'claudebot', 'anthropic', 'copilotbot', 'youbot', 'komo', 'phindbot', 'andibot', 'neeva', 'consensus', 'twitterbot', 'applebot', 'applebot-extended');
$apply_for_this_ua = false;
foreach ($ua_needles as $needle) {
if ($needle !== '' && strpos($ua_lc, $needle) !== false) {
$apply_for_this_ua = true;
break;
}
}
if (!$apply_for_this_ua) {
return $content;
}
$excluded_posts = array(777, 123);
$post_id = get_the_ID();
if ($post_id && in_array($post_id, $excluded_posts, true)) {
return $content;
}
$site_host = wp_parse_url(home_url(), PHP_URL_HOST);
if (!$site_host) return $content;
$site_host = preg_replace('~^www\.~i', '', $site_host);
if (stripos($content, '<a') === false) return $content;
libxml_use_internal_errors(true);
$dom = new DOMDocument();
$html = '<div id="__wrap__">' . $content . '</div>';
$dom->loadHTML('<?xml encoding="utf-8" ?>' . $html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$wrap = $dom->getElementById('__wrap__');
if (!$wrap) {
libxml_clear_errors();
return $content;
}
$links = $wrap->getElementsByTagName('a');
for ($i = $links->length - 1; $i >= 0; $i--) {
$a = $links->item($i);
$href = trim((string)$a->getAttribute('href'));
if ($href === '') continue;
if ($href[0] === '#' || str_starts_with($href, '/')) continue;
if (preg_match('~^(mailto:|tel:)~i', $href)) continue;
if (str_starts_with($href, '//')) {
$href = 'https:' . $href;
}
$link_host = wp_parse_url($href, PHP_URL_HOST);
if (!$link_host) continue;
$link_host = preg_replace('~^www\.~i', '', $link_host);
if (strcasecmp($link_host, $site_host) !== 0) {
while ($a->firstChild) {
$a->parentNode->insertBefore($a->firstChild, $a);
}
$a->parentNode->removeChild($a);
}
}
$out = '';
foreach ($wrap->childNodes as $child) {
$out .= $dom->saveHTML($child);
}
libxml_clear_errors();
return $out;
}
add_filter('the_content', 'ccleaner_remove_external_links_everywhere', 9999);
add_filter('the_excerpt', 'ccleaner_remove_external_links_everywhere', 9999);
add_filter('widget_text', 'ccleaner_remove_external_links_everywhere', 9999);