EmailTelemetry.php
4.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/**
* Measure email throughput to determine the potential scale of email related issues.
* @TODO: Remove this entire file at a later date.
*/
class NF_EmailTelemetry
{
private $is_opted_in = false;
/**
* Constructor which takes in a paremeter to tell the class whether the site is opted
* in for telemetry or not
*
* @param $opted_in
*
* @since 3.3.21
*/
public function __construct( $opted_in = false ) {
$this->is_opted_in = $opted_in;
}
/**
* @hook phpmailer_init The last action before the email is sent.
*/
public function setup()
{
if( $this->is_opted_in ) {
/**
* @link https://codex.wordpress.org/Plugin_API/Action_Reference/phpmailer_init
*/
// Stop collecting data.
// add_action( 'phpmailer_init', array( $this, 'update_metrics' ) );
// Stop scheduling new events.
// add_action( 'wp', array( $this, 'maybe_schedule_push' ) );
// Leave this function registered for now to avoid throwing a cron error.
add_action( 'nf_email_telemetry_push', array( $this, 'push_telemetry' ) );
}
}
/**
* @NOTE No need to return $phpmailer as it is passed in by reference (aka Output Parameter).
*/
public function update_metrics(&$phpmailer)
{
$send_count_metric = NF_Telemetry_MetricFactory::create( 'CountMetric', 'nf_email_send_count' );
$send_count_metric->increment();
$sent_with_attachments = NF_Telemetry_MetricFactory::create( 'CountMetric', 'nf_email_with_attachment_count' );
if( $phpmailer->attachmentExists() ) $sent_with_attachments->increment();
$to_count = count( $phpmailer->getToAddresses() );
$to_count_metric = NF_Telemetry_MetricFactory::create( 'CountMetric', 'nf_email_to_count' );
$to_count_metric->increment( $to_count );
$cc_count = count( $phpmailer->getCcAddresses() );
$cc_count_metric = NF_Telemetry_MetricFactory::create( 'CountMetric', 'nf_email_cc_count' );
$cc_count_metric->increment( $cc_count );
$bcc_count = count( $phpmailer->getBccAddresses() );
$bcc_count_metric = NF_Telemetry_MetricFactory::create( 'CountMetric', 'nf_email_bcc_count' );
$bcc_count_metric->increment( $bcc_count );
$to_max_metric = NF_Telemetry_MetricFactory::create( 'MaxMetric', 'nf_email_to_max' );
$to_max_metric->update( $to_count );
$cc_max_metric = NF_Telemetry_MetricFactory::create( 'MaxMetric', 'nf_email_cc_max' );
$cc_max_metric->update( $cc_count );
$bcc_max_metric = NF_Telemetry_MetricFactory::create( 'MaxMetric', 'nf_email_bcc_max' );
$bcc_max_metric->update( $bcc_count );
$recipient_max_metric = NF_Telemetry_MetricFactory::create( 'MaxMetric', 'nf_email_recipient_max' );
$recipient_max_metric->update( count( $phpmailer->getAllRecipientAddresses() ) );
$attachment_count = count( $phpmailer->getAttachments() );
$attachment_count_metric = NF_Telemetry_MetricFactory::create( 'CountMetric', 'nf_email_attachment_count' );
$attachment_count_metric->increment( $attachment_count );
$attachment_filesize_count_metric = NF_Telemetry_MetricFactory::create( 'CountMetric', 'nf_email_attachment_filesize_count' );
$attachment_filesize_max_metric = NF_Telemetry_MetricFactory::create( 'MaxMetric', 'nf_email_attachment_filesize_max' );
foreach( $phpmailer->getAttachments() as $attachment ) {
$filename = $attachment[0];
if( $filesize = filesize( $filename ) ){
$attachment_filesize_count_metric->increment( $filesize );
$attachment_filesize_max_metric->update( $filesize );
}
}
}
public function maybe_schedule_push()
{
if ( ! wp_next_scheduled( 'nf_email_telemetry_push' ) ) {
wp_schedule_event( current_time( 'timestamp' ), 'nf-weekly', 'nf_email_telemetry_push' );
}
}
public function push_telemetry()
{
// (Deprecated) Exit without doing anything.
return false;
$metrics = array(
'nf_email_send_count',
'nf_email_with_attachment_count',
'nf_email_to_count',
'nf_email_to_max',
'nf_email_cc_count',
'nf_email_cc_max',
'nf_email_bcc_count',
'nf_email_bcc_max',
'nf_email_recipient_max',
'nf_email_attachment_count',
'nf_email_attachment_filesize_count',
'nf_email_attachment_filesize_max',
);
$telemetry_data = array();
foreach( $metrics as $metric ) {
$repository = new NF_Telemetry_MetricRepository( $metric, $default = 0 );
$telemetry_data[ $metric ] = $repository->get();
$repository->save( 0 );
}
Ninja_Forms()->dispatcher()->send( 'wpsend_stats', $telemetry_data );
}
}