recurring( $this->generate_start_date(), WEEK_IN_SECONDS ) ->register(); } /** * Randomly pick a timestamp * which is not more than 1 week in the future * starting from next sunday. * * @since 2.3.0 * * @return int */ private function generate_start_date() { $tracking = []; $tracking['days'] = wp_rand( 0, 6 ) * DAY_IN_SECONDS; $tracking['hours'] = wp_rand( 0, 23 ) * HOUR_IN_SECONDS; $tracking['minutes'] = wp_rand( 0, 59 ) * MINUTE_IN_SECONDS; $tracking['seconds'] = wp_rand( 0, 59 ); return strtotime( 'next sunday' ) + array_sum( $tracking ); } /** * Send the actual data in a POST request. * This will be executed in a separate process via Action Scheduler. * * @since 2.3.0 */ public function process() { $last_run = get_option( self::LAST_RUN ); // Make sure we do not run it more than once a day. if ( $last_run !== false && ( time() - $last_run ) < DAY_IN_SECONDS ) { return; } // Send data to the usage tracking API. $ut = new UsageTracking(); wp_remote_post( self::TRACK_URL, [ 'timeout' => 5, 'redirection' => 5, 'httpversion' => '1.1', 'blocking' => true, 'body' => $ut->get_data(), 'user-agent' => $ut->get_user_agent(), ] ); // Update the last run option to the current timestamp. update_option( self::LAST_RUN, time() ); } }