track_ai_bot.php
112 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Matomo - free/libre analytics platform |
| 4 | * |
| 5 | * @link https://matomo.org |
| 6 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | * @package matomo |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * This script, when included or visited, will send an AI bot tracking |
| 12 | * request to Matomo in a shutdown function. |
| 13 | * |
| 14 | * It will only send this request if the current user agent is for a |
| 15 | * known AI bot. |
| 16 | * |
| 17 | * This script can be added to a user's wp-config.php or be executed |
| 18 | * via an HTTP request in an <esi:include> directive. It should have as |
| 19 | * few dependencies as possible, and load as few PHP file as possible. |
| 20 | */ |
| 21 | |
| 22 | function matomo_track_if_ai_bot() { |
| 23 | global $wpdb; |
| 24 | |
| 25 | if ( |
| 26 | ( ! defined( 'WP_CACHE' ) || ! WP_CACHE ) |
| 27 | && empty( $_GET['mtm_esi'] ) |
| 28 | ) { // advanced-cache.php not in use and we are not tracking via esi:include |
| 29 | return; |
| 30 | } |
| 31 | |
| 32 | if ( isset( $_GET['mtm_esi'] ) ) { // executing via esi:include directive |
| 33 | $GLOBALS['MATOMO_IN_AI_ESI'] = true; |
| 34 | } |
| 35 | |
| 36 | require_once __DIR__ . '/../app/vendor/matomo/matomo-php-tracker/MatomoTracker.php'; |
| 37 | |
| 38 | // check user agent is AI bot first thing, so if it is a normal request we do |
| 39 | // as little extra work as possible |
| 40 | $user_agent = ! empty( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : ''; |
| 41 | if ( ! MatomoTracker::isUserAgentAIBot( $user_agent ) ) { |
| 42 | return; |
| 43 | } |
| 44 | |
| 45 | $GLOBALS['wp_plugin_paths'] = []; |
| 46 | |
| 47 | if ( ! defined( 'ABSPATH' ) ) { |
| 48 | // being called from a esi:include directive |
| 49 | define( 'SHORTINIT', true ); |
| 50 | |
| 51 | $wp_config_file = dirname( dirname( dirname( dirname( __DIR__ ) ) ) ) . '/wp-config.php'; |
| 52 | if ( ! is_file( $wp_config_file ) ) { |
| 53 | $wp_config_file = dirname( dirname( dirname( dirname( dirname( $_SERVER['SCRIPT_FILENAME'] ) ) ) ) ) . '/wp-config.php'; |
| 54 | } |
| 55 | |
| 56 | require_once $wp_config_file; |
| 57 | } else { |
| 58 | // being called from request that uses advanced-cache.php |
| 59 | require_once ABSPATH . WPINC . '/class-wp-list-util.php'; |
| 60 | require_once ABSPATH . WPINC . '/class-wp-token-map.php'; |
| 61 | require_once ABSPATH . WPINC . '/formatting.php'; |
| 62 | require_once ABSPATH . WPINC . '/functions.php'; |
| 63 | } |
| 64 | |
| 65 | require_once ABSPATH . WPINC . '/link-template.php'; |
| 66 | require_once ABSPATH . WPINC . '/general-template.php'; |
| 67 | require_once ABSPATH . WPINC . '/http.php'; |
| 68 | require_once ABSPATH . WPINC . '/class-wp-http.php'; |
| 69 | require_once ABSPATH . WPINC . '/class-wp-http-streams.php'; |
| 70 | require_once ABSPATH . WPINC . '/class-wp-http-curl.php'; |
| 71 | require_once ABSPATH . WPINC . '/class-wp-http-proxy.php'; |
| 72 | require_once ABSPATH . WPINC . '/class-wp-http-cookie.php'; |
| 73 | require_once ABSPATH . WPINC . '/class-wp-http-encoding.php'; |
| 74 | require_once ABSPATH . WPINC . '/class-wp-http-response.php'; |
| 75 | require_once ABSPATH . WPINC . '/class-wp-http-requests-response.php'; |
| 76 | require_once ABSPATH . WPINC . '/class-wp-http-requests-hooks.php'; |
| 77 | |
| 78 | require_once __DIR__ . '/../classes/WpMatomo/Logger.php'; |
| 79 | require_once __DIR__ . '/../classes/WpMatomo/Site.php'; |
| 80 | require_once __DIR__ . '/../classes/WpMatomo/Paths.php'; |
| 81 | require_once __DIR__ . '/../classes/WpMatomo/Admin/CookieConsent.php'; |
| 82 | require_once __DIR__ . '/../classes/WpMatomo/Settings.php'; |
| 83 | require_once __DIR__ . '/../classes/WpMatomo/TrackingCode/GeneratorOptions.php'; |
| 84 | require_once __DIR__ . '/../classes/WpMatomo/TrackingCode/TrackingCodeGenerator.php'; |
| 85 | require_once __DIR__ . '/../classes/WpMatomo/AjaxTracker.php'; |
| 86 | require_once __DIR__ . '/../classes/WpMatomo/AIBotTracking.php'; |
| 87 | |
| 88 | if ( empty( $wpdb ) ) { |
| 89 | require_wp_db(); |
| 90 | wp_set_wpdb_vars(); |
| 91 | } |
| 92 | |
| 93 | wp_start_object_cache(); |
| 94 | |
| 95 | if ( ! defined( 'WPMU_PLUGIN_DIR' ) ) { |
| 96 | wp_plugin_directory_constants(); |
| 97 | } |
| 98 | |
| 99 | $url = !empty( $_REQUEST['mtm_url'] ) ? $_REQUEST['mtm_url'] : null; |
| 100 | |
| 101 | $settings = new \WpMatomo\Settings(); |
| 102 | $ai_bot_tracking = new \WpMatomo\AIBotTracking( $settings ); |
| 103 | $ai_bot_tracking->do_ai_bot_tracking( $url ); |
| 104 | } |
| 105 | |
| 106 | if ( ! empty( $_GET['mtm_check'] ) ) { |
| 107 | http_response_code( 201 ); |
| 108 | die; |
| 109 | } |
| 110 | |
| 111 | register_shutdown_function( 'matomo_track_if_ai_bot' ); |
| 112 |