register_hooks(); } /** * Register hooks. */ private function register_hooks() { add_action( 'plugins_loaded', array( $this, 'load_textdomain' ) ); add_action( 'init', array( 'Robotstxt_Telemetry_DB', 'maybe_upgrade' ) ); add_action( 'init', array( $this, 'schedule_cleanup' ) ); add_action( 'robotstxt_telemetry_cleanup', array( 'Robotstxt_Telemetry_DB', 'cleanup' ) ); add_filter( 'http_request_args', array( $this, 'hash_useragent_url' ), 9, 2 ); $wordpress_api = new Robotstxt_Telemetry_WordPress_Api(); $wordpress_api->register(); $logger = new Robotstxt_Telemetry_Logger(); $logger->register(); if ( is_admin() ) { $notice = new Robotstxt_Telemetry_Manager_Notice(); $notice->register(); $admin = new Robotstxt_Telemetry_Admin(); $admin->register(); } } /** * Replace the site URL in the outbound User-Agent with a fixed hash. * * The default outbound User-Agent is "WordPress/; ", * and the WordPress.org update checks send an explicit one with the * same shape. When the setting is enabled, the URL part is replaced * with a deterministic SHA-256 hash so the site is no longer * identifiable while staying consistent across requests. * * Runs at priority 9 so the telemetry logger records the value that * is actually sent on the wire. * * @param array $args HTTP request arguments. * @param string $url Request URL. * @return array */ public function hash_useragent_url( $args, $url ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter -- Second argument required by the http_request_args filter signature. if ( ! (bool) Robotstxt_Telemetry_Network::get_setting( 'robotstxt_telemetry_hash_useragent_url', true ) ) { return $args; } $site_url = get_bloginfo( 'url' ); if ( '' === $site_url ) { return $args; } $hash = substr( hash( 'sha256', $site_url ), 0, 16 ); if ( isset( $args['user-agent'] ) && is_string( $args['user-agent'] ) && false !== strpos( $args['user-agent'], $site_url ) ) { $args['user-agent'] = str_replace( $site_url, $hash, $args['user-agent'] ); } if ( isset( $args['headers']['User-Agent'] ) && is_string( $args['headers']['User-Agent'] ) && false !== strpos( $args['headers']['User-Agent'], $site_url ) ) { $args['headers']['User-Agent'] = str_replace( $site_url, $hash, $args['headers']['User-Agent'] ); } return $args; } /** * Schedule the log cleanup event if it is not scheduled yet. * * In global mode the shared table is cleaned from the main site * only, so subsites do not schedule the event. */ public function schedule_cleanup() { if ( Robotstxt_Telemetry_Network::is_global() && ! is_main_site() ) { return; } if ( ! wp_next_scheduled( 'robotstxt_telemetry_cleanup' ) ) { wp_schedule_event( time() + HOUR_IN_SECONDS, 'twicedaily', 'robotstxt_telemetry_cleanup' ); } } /** * Deactivation handler: clear the scheduled cleanup event. */ public static function deactivate() { wp_clear_scheduled_hook( 'robotstxt_telemetry_cleanup' ); if ( is_multisite() ) { foreach ( Robotstxt_Telemetry_Network::get_site_ids() as $site_id ) { switch_to_blog( $site_id ); wp_clear_scheduled_hook( 'robotstxt_telemetry_cleanup' ); restore_current_blog(); } } } /** * Load plugin translations. */ public function load_textdomain() { load_plugin_textdomain( 'robotstxt-telemetry', false, dirname( plugin_basename( ROBOTSTXT_TELEMETRY_PLUGIN_FILE ) ) . '/languages' ); } } }