PluginProbe ʕ •ᴥ•ʔ
WP Mail SMTP by WPForms – The Most Popular SMTP and Email Log Plugin / 4.9.0
WP Mail SMTP by WPForms – The Most Popular SMTP and Email Log Plugin v4.9.0
4.9.0 0.9.6 1.0.0 1.0.1 1.0.2 1.1.0 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.6.0 1.6.2 1.7.0 1.7.1 1.8.0 1.8.1 1.9.0 2.0.0 2.0.1 2.1.1 2.2.1 2.3.1 2.4.0 2.5.0 2.5.1 2.6.0 2.7.0 2.8.0 2.9.0 3.0.1 3.0.2 3.0.3 3.1.0 3.10.0 3.11.0 3.11.1 3.2.0 3.2.1 3.3.0 3.4.0 3.5.0 3.5.1 3.5.2 3.6.1 3.7.0 3.8.0 3.8.2 3.9.0 4.0.1 4.1.0 4.1.1 4.2.0 4.3.0 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.8.0 trunk 0.10.0 0.10.1 0.11.1 0.11.2 0.3.1 0.3.2 0.4 0.4.1 0.4.2 0.5.0 0.5.1 0.5.2 0.6 0.7 0.8 0.8.2 0.8.3 0.8.4 0.8.5 0.8.6 0.8.7 0.9.0 0.9.1 0.9.2 0.9.3 0.9.4 0.9.5
wp-mail-smtp / src / UsageTracking / SendUsageTask.php
wp-mail-smtp / src / UsageTracking Last commit date
ErrorStats.php 6 days ago SendUsageTask.php 6 days ago UsageTracking.php 6 days ago
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