(string) $key, 'url' => $url, 'ua' => aiprofile_inside_visit_user_agent(), 'referrer' => aiprofile_inside_visit_referrer(), 'method' => '' !== $method ? $method : 'GET', 'logged_in' => is_user_logged_in(), 'time' => time(), 'version' => AIPROFILE_INSIDE_VERSION, ) ); add_action( 'shutdown', 'aiprofile_inside_send_visit', 9999 ); } /** * Can this server hand the page to the visitor before PHP is done? * * @return bool */ function aiprofile_inside_can_finish_early() { return (bool) apply_filters( 'aiprofile_inside_can_finish_early', function_exists( 'fastcgi_finish_request' ) || function_exists( 'litespeed_finish_request' ) ); } /** * POST reports to AiProfile with the site's secret. * * @param array $body One report, or array( 'visits' => reports ). * @param bool $blocking Wait for the answer (WP-Cron) or not (after the page). * @param float $timeout Seconds. */ function aiprofile_inside_post_visits( $body, $blocking, $timeout ) { $site = aiprofile_inside_site(); $origin = aiprofile_inside_origin(); $json = wp_json_encode( $body, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); if ( empty( $site['secret'] ) || '' === $origin || ! is_string( $json ) ) { return; } wp_safe_remote_post( $origin . AIPROFILE_INSIDE_VISIT_PATH, aiprofile_inside_http_args( array( 'timeout' => $timeout, 'blocking' => $blocking, 'redirection' => 0, 'headers' => array( 'Content-Type' => 'application/json', 'Referer' => home_url( '/' ), 'Authorization' => 'Bearer ' . $site['secret'], ), 'body' => $json, ) ) ); } /** * At shutdown: send the report after the page went to the visitor, or, where * the server cannot do that, put it in the queue for WP-Cron. */ function aiprofile_inside_send_visit() { $report = aiprofile_inside_visit_queue(); if ( empty( $report ) ) { return; } if ( ! aiprofile_inside_can_finish_early() ) { aiprofile_inside_enqueue_visit( $report ); return; } if ( function_exists( 'fastcgi_finish_request' ) ) { fastcgi_finish_request(); } elseif ( function_exists( 'litespeed_finish_request' ) ) { litespeed_finish_request(); } // blocking false: the answer is not read. The timeout still has to cover // the TLS handshake and sending the body, or the report never arrives; // AiProfile answers 204 at once. aiprofile_inside_post_visits( $report, false, 1 ); } /** * Take the queue's lock: one row in the options table, made with INSERT * IGNORE, as WordPress's own upgrader lock does (add_option() would overwrite * a row another request just made). A lock older than * AIPROFILE_INSIDE_VISIT_LOCK_STALE seconds is taken over. A few short tries. * * @return bool Whether this request holds the lock. */ function aiprofile_inside_visit_lock() { global $wpdb; for ( $try = 0; $try < 5; $try++ ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- an atomic lock row, as WP_Upgrader::create_lock(). $made = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO `$wpdb->options` ( `option_name`, `option_value`, `autoload` ) VALUES ( %s, %s, 'no' )", AIPROFILE_INSIDE_VISIT_LOCK_OPTION, (string) time() ) ); if ( $made ) { return true; } // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- read past the options cache. $since = (int) $wpdb->get_var( $wpdb->prepare( "SELECT option_value FROM `$wpdb->options` WHERE option_name = %s", AIPROFILE_INSIDE_VISIT_LOCK_OPTION ) ); if ( $since && $since < time() - AIPROFILE_INSIDE_VISIT_LOCK_STALE ) { aiprofile_inside_visit_unlock(); continue; } usleep( 20000 ); } return false; } /** Give the queue's lock back. */ function aiprofile_inside_visit_unlock() { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- the lock row, made the same way. $wpdb->query( $wpdb->prepare( "DELETE FROM `$wpdb->options` WHERE option_name = %s", AIPROFILE_INSIDE_VISIT_LOCK_OPTION ) ); wp_cache_delete( AIPROFILE_INSIDE_VISIT_LOCK_OPTION, 'options' ); } /** * The queue as stored now, past the options cache (another request may just * have changed it). * * @return array */ function aiprofile_inside_visit_queue_read() { wp_cache_delete( AIPROFILE_INSIDE_VISIT_QUEUE_OPTION, 'options' ); $queue = get_option( AIPROFILE_INSIDE_VISIT_QUEUE_OPTION, array() ); return is_array( $queue ) ? array_values( $queue ) : array(); } /** * Keep a report for WP-Cron, at most the last AIPROFILE_INSIDE_VISIT_QUEUE_MAX, * and make sure a send is scheduled. Under the lock; when the lock cannot be * had in about a tenth of a second, this one report is dropped. * * @param array $report One report. */ function aiprofile_inside_enqueue_visit( $report ) { if ( ! aiprofile_inside_visit_lock() ) { return; } $queue = aiprofile_inside_visit_queue_read(); $queue[] = $report; update_option( AIPROFILE_INSIDE_VISIT_QUEUE_OPTION, array_slice( $queue, -AIPROFILE_INSIDE_VISIT_QUEUE_MAX ), false ); aiprofile_inside_visit_unlock(); if ( ! wp_next_scheduled( AIPROFILE_INSIDE_VISIT_FLUSH_EVENT ) ) { wp_schedule_single_event( time(), AIPROFILE_INSIDE_VISIT_FLUSH_EVENT ); } } /** * Split reports into batches of at most AIPROFILE_INSIDE_VISIT_QUEUE_MAX and * at most AIPROFILE_INSIDE_VISIT_BATCH_BYTES of JSON each. * * @param array $reports The reports. * @return array[] Batches. */ function aiprofile_inside_visit_batches( $reports ) { $batches = array(); $batch = array(); $bytes = 20; foreach ( $reports as $report ) { $size = strlen( (string) wp_json_encode( $report, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ) ) + 1; if ( ! empty( $batch ) && ( count( $batch ) >= AIPROFILE_INSIDE_VISIT_QUEUE_MAX || $bytes + $size > AIPROFILE_INSIDE_VISIT_BATCH_BYTES ) ) { $batches[] = $batch; $batch = array(); $bytes = 20; } $batch[] = $report; $bytes += $size; } if ( ! empty( $batch ) ) { $batches[] = $batch; } return $batches; } /** * WP-Cron: send the queue in batches, in a request of its own. Takes only the * reports it read: under the lock the queue is read and emptied in one go, so * a report that arrives while the batches go out waits for the next send. */ function aiprofile_inside_flush_visits() { if ( ! aiprofile_inside_visit_lock() ) { // Busy: try again in a minute. wp_schedule_single_event( time() + MINUTE_IN_SECONDS, AIPROFILE_INSIDE_VISIT_FLUSH_EVENT ); return; } $queue = aiprofile_inside_visit_queue_read(); delete_option( AIPROFILE_INSIDE_VISIT_QUEUE_OPTION ); aiprofile_inside_visit_unlock(); if ( empty( $queue ) || ! aiprofile_inside_option( 'visits' ) || ! aiprofile_inside_link_verified() ) { return; } foreach ( aiprofile_inside_visit_batches( array_slice( $queue, -AIPROFILE_INSIDE_VISIT_QUEUE_MAX ) ) as $batch ) { aiprofile_inside_post_visits( array( 'visits' => $batch ), true, 5 ); } } add_action( AIPROFILE_INSIDE_VISIT_FLUSH_EVENT, 'aiprofile_inside_flush_visits' ); /** Drop what waits in the queue (the owner switched the reports off). */ function aiprofile_inside_clear_visit_queue() { delete_option( AIPROFILE_INSIDE_VISIT_QUEUE_OPTION ); wp_clear_scheduled_hook( AIPROFILE_INSIDE_VISIT_FLUSH_EVENT ); } /** The explanation next to the switch: what is sent, in one line. */ function aiprofile_inside_visits_help() { esc_html_e( 'Each time someone opens your answers page, the plugin sends AiProfile the visitor\'s user agent (the browser or bot name), the page address, the site the visitor came from, the time and whether they are logged in. No IP address, no cookies. For this the plugin registers your site with AiProfile once, as for automatic updates.', 'aiprofile-inside' ); } /** * Suggested text for the site's privacy policy (Settings, Privacy), since * 1.4.0: what the plugin sends to AiProfile about visitors, and when. */ function aiprofile_inside_privacy_policy_content() { if ( ! function_exists( 'wp_add_privacy_policy_content' ) ) { return; } $text = '

' . esc_html__( 'This site shows questions and answers from AiProfile (www.aiprofile.com) with the AiProfile Inside plugin. The answers are fetched by our server; no script or cookie from AiProfile reaches your browser.', 'aiprofile-inside' ) . '

'; $text .= '

' . esc_html__( 'When the site owner has switched on the visit count, our server tells AiProfile each time our answers page is opened: your browser\'s user agent, the address of the page, the site you came from (without its search terms), the time and whether you are logged in. Your IP address is not sent. AiProfile uses this to count how often the page is read, by people and by AI services.', 'aiprofile-inside' ) . '

'; $text .= '

' . esc_html__( 'When you send a question through the form under the answers, it goes to AiProfile with the language and the page address; your email address only if you add it and tick the consent box.', 'aiprofile-inside' ) . '

'; $text .= '

' . esc_html__( 'AiProfile privacy policy', 'aiprofile-inside' ) . '

'; wp_add_privacy_policy_content( 'AiProfile Inside', wp_kses_post( $text ) ); } add_action( 'admin_init', 'aiprofile_inside_privacy_policy_content' );