'; private $client = null; public $notices = []; public $limit = AIAUTOTOOL_FREE; private $plan_limit_aiautotool ; public $name_plan ; public $config = array(); public $notice ; public $robotstext = ''; private $aiautotool_user_agents_regex; public function __construct() { $this->name_plan = __('Robots & Ads txt','ai-auto-tool'); $this->plan_limit_aiautotool = 'plan_limit_aiautotool_'.$this->active_option_name; $this->robotstext = 'User-agent: GPTBot Disallow: / User-agent: CCbot Disallow: / User-agent: anthropic-ai Disallow: / User-agent: Claude-Web Disallow: / User-agent: Google-Extended Disallow: /'; $this->notice = new aiautotool_Warning_Notice(); $this->active = get_option($this->active_option_name, false); if ($this->active=='true') { $this->init(); } add_action('wp_ajax_update_active_option_canonical_'.$this->active_option_name, array($this, 'update_active_option_callback')); } public function update_active_option_callback() { check_ajax_referer('aiautotool_nonce', 'security'); if (!current_user_can('manage_options')) { wp_die(__('Permission denied', 'ai-auto-tool'), '', array('response' => 403)); } if (isset($_POST['active'])) { $active = sanitize_text_field($_POST['active']); update_option($this->active_option_name, $active,null, 'no'); print_r($active); } wp_die(); } public function init(){ $this->aiautotool_user_agents_regex = $this->aiautotool_get_user_agents_regex(); add_action('admin_init', array($this, 'page_init')); remove_all_filters('robots_txt'); add_filter( 'robots_txt', array($this,'aiautotool_robots_ads_robots_txt'), 5, 2 ); add_action( 'init',array($this, 'aiautotool_robots_ads_ads_txt') ); add_action('init', array($this, 'aiautotool_block_user_agents')); add_action('login_init', array($this, 'aiautotool_login_auth_login_authentication')); add_action('wp_ajax_update_remote_txt', array($this, 'aiautotool_remote_content')); // Gọi hàm khi WordPress được khởi động add_action('init', array($this, 'add_rewrite_rules_to_htaccess')); add_action('admin_post_save_robots_content', array($this, 'save_robots_content')); add_action('admin_enqueue_scripts', array($this, 'enqueue_security_scripts')); } /** * Enqueue scripts and styles for security bot */ public function enqueue_security_scripts($hook) { // Only load on AI Auto Tool settings page if ($hook !== 'toplevel_page_ai_auto_tool') { return; } // Enqueue common tabs CSS & JS wp_enqueue_style( 'aiautotool-tabs-common-css', plugins_url('../css/tabs-common.css', __FILE__), array(), defined('AIAUTOTOOL_VERSION_CACHE') ? AIAUTOTOOL_VERSION_CACHE : '1.0.0' ); wp_enqueue_script( 'aiautotool-tabs-common-js', plugins_url('../js/tabs-common.js', __FILE__), array('jquery'), defined('AIAUTOTOOL_VERSION_CACHE') ? AIAUTOTOOL_VERSION_CACHE : '1.0.0', true ); } public function save_robots_content() { if (!current_user_can('manage_options')) { return; } // Kiểm tra nonce để đảm bảo tính xác thực của dữ liệu if (!isset($_POST['_wpnonce']) || !wp_verify_nonce($_POST['_wpnonce'], 'save_robots_content_nonce')) { return; } // Lấy dữ liệu từ form $options = isset($_POST['ai_auto_tool_security_bot_options']) ? $_POST['ai_auto_tool_security_bot_options'] : array(); $robots_content = isset($options['robots_txt_content']) ? $options['robots_txt_content'] : ''; $ads_content = isset($options['ads_txt_content']) ? $options['ads_txt_content'] : ''; // Lưu nội dung robots.txt vào biến options $options['robots_txt_content'] = $robots_content; // Lưu nội dung ads.txt vào biến options $options['ads_txt_content'] = $ads_content; // Lưu nội dung robots.txt vào tệp $robots_file = ABSPATH . 'robots.txt'; file_put_contents($robots_file, $robots_content); // Lưu nội dung ads.txt vào tệp $ads_file = ABSPATH . 'ads.txt'; file_put_contents($ads_file, $ads_content); // Lưu biến options update_option('ai_auto_tool_security_bot_options', $options); wp_redirect(admin_url('admin.php?page=ai_auto_tool')); exit; } // Hàm để thêm quy tắc rewrite vào file .htaccess function add_rewrite_rules_to_htaccess() { $htaccess_file = ABSPATH . '.htaccess'; // Đường dẫn đến file .htaccess // Kiểm tra nếu file .htaccess có tồn tại và có thể ghi if (file_exists($htaccess_file) && is_writable($htaccess_file)) { // Tạo nội dung quy tắc rewrite $rewrite_rules = " RewriteEngine On RewriteRule ^/ads.txt$ /index.php?ads=1 [L] RewriteRule ^/robots.txt$ /index.php?robots=1 [L] "; // Thêm quy tắc vào cuối file .htaccess file_put_contents($htaccess_file, $rewrite_rules, FILE_APPEND); } } public static function get_robots_data() { global $wp_filesystem; $public = absint( get_option( 'blog_public' ) ); $default = '# This file is automatically added by Ai auto tool plugin to help a website index better'; $default .= "User-Agent: *\n"; if ( 0 === $public ) { $default .= "Disallow: /\n"; } else { $default .= "Disallow: /wp-admin/\n"; $default .= "Allow: /wp-admin/admin-ajax.php\n"; } $default = apply_filters( 'robots_txt', $default, $public ); if ( empty( $wp_filesystem )) { return [ 'exists' => false, 'default' => $default, 'public' => $public, 'writable' => false, ]; } if ( $wp_filesystem->exists( ABSPATH . 'robots.txt' ) ) { return [ 'exists' => true, 'default' => $wp_filesystem->get_contents( ABSPATH . 'robots.txt' ), 'public' => $public, ]; } return [ 'exists' => false, 'default' => $default, 'public' => $public, ]; } public function aiautotool_remote_content(){ check_ajax_referer('aiautotool_nonce', 'security'); if (!current_user_can('manage_options')) { wp_die(__('Permission denied', 'ai-auto-tool'), '', array('response' => 403)); } $url = esc_url_raw($_POST['url']); echo esc_html($this->aiautotool_remote_txt_content($url)); wp_die(); } public function aiautotool_remote_txt_content($url) { if (filter_var($url, FILTER_VALIDATE_URL) === false) { return false; } if (!preg_match("~^(?:f|ht)tps?://~i", $url)) { return false; } $content = wp_remote_retrieve_body(wp_remote_get($url)); if (empty($content)) { return false; } return $content; } private function aiautotool_has_plugin_data() { return get_option($this->plan_limit_aiautotool) !== false; } private function aiautotool_initialize_plugin_data() { $current_date = date('Y-m-d'); $expiration = strtotime('+1 month', strtotime($current_date)); $expiration = date('Y-m-d', $expiration); update_option($this->plan_limit_aiautotool, array( 'start_date' => $current_date,'expiration'=>$expiration)); } public function aiautotool_check_post_limit() { if (!$this->aiautotool_checklimit($this->config)) { $this->notice->add_notice( AIAUTOTOOL_TITLE_UPGRADE, 'notice-error', null, true, 'aiautotool' ); return false; } $stored_data = get_option($this->plan_limit_aiautotool, array()); $current_date = date('Y-m-d'); if ($this->is_new_month($current_date, $stored_data['start_date'])) { $expiration = strtotime('+1 month', strtotime($current_date)); $expiration = date('Y-m-d', $expiration); update_option($this->plan_limit_aiautotool, array( 'start_date' => $current_date,'expiration'=>$expiration)); update_option($this->usage_option_name, 0,null, 'no'); } } public function render_tab_setting() { if ($this->active=='true') { echo ''; } } public function render_setting() { $options = get_option('ai_auto_tool_security_bot_options'); $robots = $this->get_robots_data(); // Check if $options is an array if (!is_array($options)) { $options = array(); // Initialize $options as an array if it's not already } if($robots['exists']){ $options['robots_txt_content'] = $robots['default']; }else{ if (!isset($options['robots_txt_content'])) { $options['robots_txt_content'] = $this->robotstext; } } if (empty($options['robots_txt_content'])) { $options['robots_txt_content'] = $this->robotstext; } if (!isset($options['ads_txt_content'])) { $options['ads_txt_content'] = ''; } $username = get_option('aiautotool_login_auth_login_username'); $password = get_option('aiautotool_login_auth_login_password'); ?>
icon).' '.__('Robots.txt & Ads.txt Manager','ai-auto-tool'), AiAutoTool_Feature_Descriptions::get('robots_txt'), "https://doc.aiautotool.com/features/robots-ads-txt", $this->active_option_name, $this->active, plugins_url('../../images/logo.svg', __FILE__) ); echo ($autoToolBox->generateHTML()); } public function page_init() { register_setting('aiautotool_user_agents_settings_group', 'aiautotool_user_agents_list', array( 'type' => 'array', 'sanitize_callback' => array($this, 'sanitize_user_agents_list'), )); register_setting('wp_login_security_settings', 'aiautotool_login_auth_login_username', array( 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', )); register_setting('wp_login_security_settings', 'aiautotool_login_auth_login_password', array( 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', )); register_setting( 'ai_auto_tool_security_bot_group', 'ai_auto_tool_security_bot_options', array( 'type' => 'array', 'sanitize_callback' => array($this, 'sanitize'), ) ); add_settings_section( 'setting_section_id', 'Settings', array($this, 'print_section_info'), 'ai_auto_tool_security_bot_page' ); add_settings_field( 'robots_txt_content', 'Robots.txt Content
', array($this, 'robots_txt_content_callback'), 'ai_auto_tool_security_bot_page', 'setting_section_id' ); add_settings_field( 'ads_txt_content', 'Ads.txt Content
', array($this, 'ads_txt_content_callback'), 'ai_auto_tool_security_bot_page', 'setting_section_id' ); } /** * Sanitize user agents list * * @param mixed $value Input value * @return array Sanitized array */ public function sanitize_user_agents_list($value) { if (!is_array($value)) { return array(); } return map_deep($value, 'sanitize_text_field'); } public function sanitize($input) { $sanitized_input = array(); if (isset($input['robots_txt_content'])) { $sanitized_input['robots_txt_content'] = wp_kses_post($input['robots_txt_content']); } if (isset($input['ads_txt_content'])) { $sanitized_input['ads_txt_content'] = wp_kses_post($input['ads_txt_content']); } return $sanitized_input; } public function print_section_info() { // echo 'Enter the content for Robots.txt and Ads.txt below:'; } public function robots_txt_content_callback() { } public function ads_txt_content_callback() { } public function aiautotool_robots_ads_robots_txt() { if (!is_admin()) { $options = get_option('ai_auto_tool_security_bot_options'); $robots_content = isset($options['robots_txt_content']) ? $options['robots_txt_content'] : ''; header('Content-Type: text/plain; charset=utf-8'); echo esc_html($robots_content); exit; } } public function aiautotool_robots_ads_ads_txt() { $request = isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : false; if ( '/ads.txt' === $request || '/ads.txt?' === substr( $request, 0, 9 ) ) { header( 'X-Ads-Txt-Generator: ai-auto-tool' ); $options = get_option('ai_auto_tool_security_bot_options'); $ads_content = isset($options['ads_txt_content']) ? $options['ads_txt_content'] : ''; header('Content-Type: text/plain; charset=utf-8'); echo esc_html($ads_content); exit; } } public function aiautotool_get_user_agents_regex() { $user_agents = get_option('aiautotool_user_agents_list', ''); if (!empty($user_agents)) { $user_agents_array = explode("\n", $user_agents); $user_agents_array = array_map('trim', $user_agents_array); $user_agents_array = array_filter($user_agents_array); $user_agents_regex = '/(' . implode('|', array_map('preg_quote', $user_agents_array)) . ')/i'; return $user_agents_regex; } else { return '/^$/'; } } public function aiautotool_block_user_agents() { if (!is_admin()) { $user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : ''; if(!empty($this->aiautotool_user_agents_regex)){ if (preg_match($this->aiautotool_user_agents_regex, $user_agent)) { header("HTTP/1.1 403 Forbidden"); exit; } } } } public function aiautotool_login_auth_login_authentication() { $username = get_option('aiautotool_login_auth_login_username'); $password = get_option('aiautotool_login_auth_login_password'); if (!empty($username) && !empty($password)) { if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']) || $_SERVER['PHP_AUTH_USER'] != $username || $_SERVER['PHP_AUTH_PW'] != $password) { header("WWW-Authenticate: Basic realm=\"Login Security\""); header("HTTP/1.0 401 Unauthorized"); echo <<