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