' . 'AgencyBoost: A physical robots.txt file exists on your server. ' . 'WordPress cannot override it via plugin — your custom content is not live. ' . 'Click "Write to Server File" below to update it directly.

'; } } /** * Write robots.txt content directly to the physical file on the server. */ public static function ajax_write_file() { check_ajax_referer( 'agenli_nonce', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'message' => 'Unauthorised.' ) ); } $agenli_content = agenli_get_option( 'robots_txt_content', '' ); if ( empty( $agenli_content ) ) { wp_send_json_error( array( 'message' => 'No content to write.' ) ); } $agenli_path = ABSPATH . 'robots.txt'; $agenli_result = file_put_contents( $agenli_path, $agenli_content ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents if ( false === $agenli_result ) { wp_send_json_error( array( 'message' => 'Could not write file. Check server file permissions.' ) ); } wp_send_json_success( array( 'message' => 'robots.txt written to server successfully.' ) ); } public static function on_activate() { self::set_defaults(); } public static function set_defaults() { if ( agenli_get_option( 'robots_txt_content' ) ) return; $agenli_default = "User-agent: *\n"; $agenli_default .= "Disallow: /wp-admin/\n"; $agenli_default .= "Allow: /wp-admin/admin-ajax.php\n"; $agenli_default .= "\n"; $agenli_default .= "Disallow: /wp-includes/\n"; $agenli_default .= "Disallow: /wp-content/plugins/\n"; $agenli_default .= "Disallow: /wp-content/themes/\n"; $agenli_default .= "Disallow: /wp-login.php\n"; $agenli_default .= "\n"; $agenli_default .= "Sitemap: " . home_url( '/sitemap.xml' ); agenli_set_option( 'robots_txt_content', $agenli_default ); } /** * WordPress calls this filter for /robots.txt requests when no physical * robots.txt file exists on the server. If a physical file exists, * WordPress never fires this filter we inform the user of this. */ public static function filter_robots_txt( $agenli_output, $agenli_public ) { $agenli_custom = agenli_get_option( 'robots_txt_content' ); return $agenli_custom ? $agenli_custom : $agenli_output; } public static function ajax_save() { check_ajax_referer( 'agenli_nonce', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'message' => 'Unauthorised.' ) ); } $agenli_content = isset( $_POST['robots_content'] ) ? sanitize_textarea_field( wp_unslash( $_POST['robots_content'] ) ) : ''; agenli_set_option( 'robots_txt_content', $agenli_content ); wp_send_json_success( array( 'message' => 'robots.txt saved successfully.' ) ); } public static function ajax_reset() { check_ajax_referer( 'agenli_nonce', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'message' => 'Unauthorised.' ) ); } agenli_set_option( 'robots_txt_content', '' ); self::set_defaults(); wp_send_json_success( array( 'message' => 'Reset to default.', 'content' => agenli_get_option( 'robots_txt_content' ), )); } }