| Server IP : 34.67.85.211 / Your IP : 216.73.217.52 Web Server : Apache System : Linux wordpress-1-vm 4.9.0-13-amd64 #1 SMP Debian 4.9.228-1 (2020-07-05) x86_64 User : root ( 0) PHP Version : 7.4.9 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : OFF Directory : /var/www/html/wp-content/plugins/wp-mail-smtp/src/UsageTracking/ |
Upload File : |
<?php
namespace WPMailSMTP\UsageTracking;
use WPMailSMTP\Tasks\Task;
/**
* Class SendUsageTask.
*
* @since 2.3.0
*/
class SendUsageTask extends Task {
/**
* Action name for this task.
*
* @since 2.3.0
*/
const ACTION = 'wp_mail_smtp_send_usage_data';
/**
* Server URL to send requests to.
*
* @since 2.3.0
*/
const TRACK_URL = 'https://wpmailsmtpusage.com/v1/smtptrack';
/**
* Option name to store the timestamp of the last run.
*
* @since 2.5.0
*/
const LAST_RUN = 'wp_mail_smtp_send_usage_last_run';
/**
* Class constructor.
*
* @since 2.3.0
*/
public function __construct() {
parent::__construct( self::ACTION );
}
/**
* Initialize the task with all the proper checks.
*
* @since 2.3.0
*/
public function init() {
// Register the action handler.
add_action( self::ACTION, [ $this, 'process' ] );
if ( ! function_exists( 'as_next_scheduled_action' ) ) {
return;
}
// Add new if none exists.
if ( as_next_scheduled_action( self::ACTION ) !== false ) {
return;
}
$this->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() );
}
}