ID, '_aisp_protector_enable_scraping_protection', true); if (is_single() && ($is_protection_enabled || get_option('aisp_protector_enable_posts'))) { return 'Content hidden from AI scraping.'; // Message for hidden content } if (is_page() && get_option('aisp_protector_enable_pages')) { return 'Content hidden from AI scraping.'; // Message for hidden content } return $content; // Return original content if no conditions are met } add_filter('the_content', 'aisp_protector_hide_content'); // Add HTTP headers to prevent indexing /** * Adds HTTP headers to prevent search engines from indexing the page. */ function aisp_protector_add_http_headers() { // Check if the user is logged in or if the request is from a bot if (!is_user_logged_in() && !aisp_protector_bot_detection()) { header('X-Robots-Tag: noindex, nofollow', true); // Prevent indexing and following links } } add_action('send_headers', 'aisp_protector_add_http_headers'); // Bot detection logic /** * Detects if the current user agent is a known bot. * * @return bool True if a bot is detected, false otherwise. */ function aisp_protector_bot_detection() { // Sanitize and validate user agent $user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? sanitize_text_field(wp_unslash($_SERVER['HTTP_USER_AGENT'])) : ''; $remote_addr = isset($_SERVER['REMOTE_ADDR']) ? sanitize_text_field(wp_unslash($_SERVER['REMOTE_ADDR'])) : ''; // Validate IP address format if (!filter_var($remote_addr, FILTER_VALIDATE_IP)) { return false; // Invalid IP address } $bots = ['Googlebot', 'Bingbot', 'Slurp', 'DuckDuckBot']; foreach ($bots as $bot) { if (stripos($user_agent, $bot) !== false) { return true; } } if (empty($http_referer)) { return true; } $bot_ips = ['66.249.64.0/19', '207.46.13.0/24']; foreach ($bot_ips as $ip_range) { if (aisp_ip_in_range($remote_addr, $ip_range)) { return true; } } return false; } /** * Checks if an IP address is within a given CIDR range. * * @param string $ip The IP address to check. * @param string $cidr The CIDR range to check against. * @return bool True if the IP is in the range, false otherwise. */ function aisp_ip_in_range($ip, $cidr) { list($subnet, $bits) = explode('/', $cidr); $ip = ip2long($ip); $subnet = ip2long($subnet); $mask = -1 << (32 - $bits); $subnet &= $mask; // Apply the subnet mask return ($ip & $mask) === $subnet; // Check if the IP is in the range } // Scramble content for detected bots /** * Scrambles the content if a bot is detected. * * @param string $content The original content. * @return string The modified content. */ function aisp_protector_scramble_content($content) { if (aisp_protector_bot_detection()) { return str_shuffle($content); // Example scrambling } return $content; // Return original content if no bot is detected } add_filter('the_content', 'aisp_protector_scramble_content'); // Add a meta box to the post editor /** * Adds a meta box to the post editor for enabling scraping protection. */ function aisp_protector_add_meta_box() { add_meta_box( 'aisp_protector_meta_box', // ID 'AI Scraping Protection', // Title 'aisp_protector_meta_box_callback', // Callback function 'post', // Post type 'side', // Context 'default' // Priority ); } add_action('add_meta_boxes', 'aisp_protector_add_meta_box'); // Callback function for the meta box /** * Displays the content of the meta box. * * @param WP_Post $post The current post object. */ function aisp_protector_meta_box_callback($post) { // Add nonce field for security wp_nonce_field('aisp_protector_save_meta_box_data', 'aisp_protector_meta_box_nonce'); // Retrieve current value $value = get_post_meta($post->ID, '_aisp_protector_enable_scraping_protection', true); ?>