detector = new AiMiles_Detector();
$this->injector = new AiMiles_Injector();
$this->logger = new AiMiles_Logger();
add_action('init', [$this, 'detect_ai_agent'], 1);
add_action('admin_menu', [$this, 'add_admin_menu']);
add_action('admin_init', [$this, 'register_settings']);
add_action('admin_notices', [$this, 'admin_notices']);
add_filter('plugin_action_links_' . plugin_basename(__FILE__), [$this, 'add_settings_link']);
}
public function admin_notices() {
$api_key = get_option('aimiles_api_key', '');
if (empty($api_key)) {
$settings_url = admin_url('options-general.php?page=aimiles');
echo '
';
}
}
public function detect_ai_agent() {
$user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? sanitize_text_field(wp_unslash($_SERVER['HTTP_USER_AGENT'])) : '';
$request_uri = isset($_SERVER['REQUEST_URI']) ? esc_url_raw(wp_unslash($_SERVER['REQUEST_URI'])) : '';
$this->detection_result = $this->detector->detect($user_agent);
if ($this->detection_result['is_ai_agent']) {
// Bypass page caching so AI agents receive fresh sponsored content.
// Filterable so site owners can disable this behaviour if needed.
if (apply_filters('aimiles_bypass_cache', true)) {
if (!defined('DONOTCACHEPAGE')) {
define('DONOTCACHEPAGE', true);
}
nocache_headers();
}
// Fetch sponsored text from API first
$this->sponsored_text = $this->logger->fetch_sponsored_texts();
// Log detection with sponsored_text_id included in the same API call
$sponsored_text_id = $this->sponsored_text['id'] ?? null;
$this->pagevisit_id = $this->logger->log_visit(
$this->detection_result,
$request_uri,
$sponsored_text_id
);
// Inject sponsored content into post content via filter
if (get_option('aimiles_enabled', true)) {
add_filter('the_content', [$this, 'inject_sponsored_content'], 99);
}
}
}
public function inject_sponsored_content($content) {
if (empty($this->sponsored_text) || empty($this->sponsored_text['ad_text'])) {
return $content;
}
return $this->injector->inject($content, $this->sponsored_text['ad_text']);
}
public function add_admin_menu() {
add_options_page(
'aiMiles Settings',
'aiMiles',
'manage_options',
'aimiles',
[$this, 'render_settings_page']
);
}
public function register_settings() {
register_setting('aimiles_settings', 'aimiles_enabled', [
'type' => 'boolean',
'sanitize_callback' => 'rest_sanitize_boolean',
'default' => true,
]);
register_setting('aimiles_settings', 'aimiles_api_key', [
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
'default' => '',
]);
}
public function add_settings_link($links) {
$settings_link = 'Settings';
array_unshift($links, $settings_link);
return $links;
}
public function render_settings_page() {
include AIMILES_PLUGIN_DIR . 'admin/settings-page.php';
}
}
function aimiles_init() {
return AiMiles::get_instance();
}
add_action('plugins_loaded', 'aimiles_init');
register_activation_hook(__FILE__, 'aimiles_activate');
function aimiles_activate() {
add_option('aimiles_enabled', true);
add_option('aimiles_api_key', '');
}
register_deactivation_hook(__FILE__, 'aimiles_deactivate');
function aimiles_deactivate() {
// Clean up if needed
}