SendUsageTask.php
123 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPMailSMTP\UsageTracking; |
| 4 | |
| 5 | use WPMailSMTP\Tasks\Task; |
| 6 | use WPMailSMTP\Tasks\Tasks; |
| 7 | |
| 8 | /** |
| 9 | * Class SendUsageTask. |
| 10 | * |
| 11 | * @since 2.3.0 |
| 12 | */ |
| 13 | class SendUsageTask extends Task { |
| 14 | |
| 15 | /** |
| 16 | * Action name for this task. |
| 17 | * |
| 18 | * @since 2.3.0 |
| 19 | */ |
| 20 | const ACTION = 'wp_mail_smtp_send_usage_data'; |
| 21 | |
| 22 | /** |
| 23 | * Server URL to send requests to. |
| 24 | * |
| 25 | * @since 2.3.0 |
| 26 | */ |
| 27 | const TRACK_URL = 'https://wpmailsmtpusage.com/v1/smtptrack'; |
| 28 | |
| 29 | /** |
| 30 | * Option name to store the timestamp of the last run. |
| 31 | * |
| 32 | * @since 2.5.0 |
| 33 | */ |
| 34 | const LAST_RUN = 'wp_mail_smtp_send_usage_last_run'; |
| 35 | |
| 36 | /** |
| 37 | * Class constructor. |
| 38 | * |
| 39 | * @since 2.3.0 |
| 40 | */ |
| 41 | public function __construct() { |
| 42 | |
| 43 | parent::__construct( self::ACTION ); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Initialize the task with all the proper checks. |
| 48 | * |
| 49 | * @since 2.3.0 |
| 50 | */ |
| 51 | public function init() { |
| 52 | |
| 53 | // Register the action handler. |
| 54 | add_action( self::ACTION, [ $this, 'process' ] ); |
| 55 | |
| 56 | // Add new if none exists. |
| 57 | if ( Tasks::is_scheduled( self::ACTION ) !== false ) { |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | $this->recurring( $this->generate_start_date(), WEEK_IN_SECONDS ) |
| 62 | ->register(); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Randomly pick a timestamp |
| 67 | * which is not more than 1 week in the future |
| 68 | * starting from next sunday. |
| 69 | * |
| 70 | * @since 2.3.0 |
| 71 | * |
| 72 | * @return int |
| 73 | */ |
| 74 | private function generate_start_date() { |
| 75 | |
| 76 | $tracking = []; |
| 77 | |
| 78 | $tracking['days'] = wp_rand( 0, 6 ) * DAY_IN_SECONDS; |
| 79 | $tracking['hours'] = wp_rand( 0, 23 ) * HOUR_IN_SECONDS; |
| 80 | $tracking['minutes'] = wp_rand( 0, 59 ) * MINUTE_IN_SECONDS; |
| 81 | $tracking['seconds'] = wp_rand( 0, 59 ); |
| 82 | |
| 83 | return strtotime( 'next sunday' ) + array_sum( $tracking ); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Send the actual data in a POST request. |
| 88 | * This will be executed in a separate process via Action Scheduler. |
| 89 | * |
| 90 | * @since 2.3.0 |
| 91 | */ |
| 92 | public function process() { |
| 93 | |
| 94 | $last_run = get_option( self::LAST_RUN ); |
| 95 | |
| 96 | // Make sure we do not run it more than once a day. |
| 97 | if ( |
| 98 | $last_run !== false && |
| 99 | ( time() - $last_run ) < DAY_IN_SECONDS |
| 100 | ) { |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | // Send data to the usage tracking API. |
| 105 | $ut = new UsageTracking(); |
| 106 | |
| 107 | wp_remote_post( |
| 108 | self::TRACK_URL, |
| 109 | [ |
| 110 | 'timeout' => 5, |
| 111 | 'redirection' => 5, |
| 112 | 'httpversion' => '1.1', |
| 113 | 'blocking' => true, |
| 114 | 'body' => $ut->get_data(), |
| 115 | 'user-agent' => $ut->get_user_agent(), |
| 116 | ] |
| 117 | ); |
| 118 | |
| 119 | // Update the last run option to the current timestamp. |
| 120 | update_option( self::LAST_RUN, time() ); |
| 121 | } |
| 122 | } |
| 123 |