Skip to content

Commit

Permalink
Do not send organiser reminders with "send X days after the camp ends…
Browse files Browse the repository at this point in the history
…" trigger if WordCamp didn't happen (#970)

* add wordcamp public status check for emails set to send with wcor_send_after.

* fix test for wcor_send_after.

* add tests for wcor_send_after when wordcamp does not have public status

---------

Co-authored-by: ren <[email protected]>
  • Loading branch information
timiwahalahti and renintw authored Apr 29, 2024
1 parent 4bbcc10 commit f5adc25
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,18 @@ public function test_triggered_message_sent() {
*
* @covers WCOR_Mailer::send_timed_emails
*/
public function test_timed_messages_sent( $send_when, $send_when_period, $send_when_days, $compare_date ) {
public function test_timed_messages_sent( $send_when, $send_when_period, $send_when_days, $compare_date, $wordcamp_post_status ) {
/** @var WCOR_Mailer $WCOR_Mailer */
global $WCOR_Mailer;

update_post_meta( self::$timed_reminder_post_id, 'wcor_send_when', $send_when );
update_post_meta( self::$timed_reminder_post_id, $send_when_period, $send_when_days );

wp_update_post( array(
'ID' => self::$wordcamp_dayton_post_id,
'post_status' => $wordcamp_post_status,
) );

if ( in_array( $send_when, array( 'wcor_send_before', 'wcor_send_after', 'wcor_send_after_and_no_report' ) ) ) {
update_post_meta( self::$wordcamp_dayton_post_id, 'Start Date (YYYY-mm-dd)', $compare_date );
} elseif ( 'wcor_send_after_pending' === $send_when ) {
Expand All @@ -174,14 +179,18 @@ public function test_timed_messages_sent( $send_when, $send_when_period, $send_w

do_action( 'wcor_send_timed_emails' );

$this->assert_mail_succeeded(
'[email protected]',
"It's time to submit WordCamp Dayton reimbursement requests",
"Howdy Sally Smith, now's the perfect time to request reimbursement for any out of pocket expenses. You can do that at https://2019.dayton.wordcamp.org/wp-admin/edit.php?post_type=wcb_reimbursement.\n"
);

$this->assertIsArray( $wordcamp->wcor_sent_email_ids );
$this->assertContains( self::$timed_reminder_post_id, $wordcamp->wcor_sent_email_ids );
if ( 'wcor_send_after' === $send_when && 'wcpt-cancelled' === $wordcamp_post_status ) {
$this->assertSame( '', $wordcamp->wcor_sent_email_ids );
} else {
$this->assert_mail_succeeded(
'[email protected]',
"It's time to submit WordCamp Dayton reimbursement requests",
"Howdy Sally Smith, now's the perfect time to request reimbursement for any out of pocket expenses. You can do that at https://2019.dayton.wordcamp.org/wp-admin/edit.php?post_type=wcb_reimbursement.\n"
);

$this->assertIsArray( $wordcamp->wcor_sent_email_ids );
$this->assertContains( self::$timed_reminder_post_id, $wordcamp->wcor_sent_email_ids );
}
}

/**
Expand All @@ -197,6 +206,7 @@ public function data_timed_messages_sent() {
'wcor_send_days_before',
3,
strtotime( 'now + 3 days' ),
'wcpt-scheduled',
),

// After the camp ends.
Expand All @@ -205,6 +215,16 @@ public function data_timed_messages_sent() {
'wcor_send_days_after',
3,
strtotime( 'now - 3 days' ),
'wcpt-scheduled',
),

// After the camp ends but it does not have public status.
array(
'wcor_send_after',
'wcor_send_days_after',
3,
strtotime( 'now - 3 days' ),
'wcpt-cancelled',
),

// After added to the pending schedule.
Expand All @@ -213,6 +233,7 @@ public function data_timed_messages_sent() {
'wcor_send_days_after_pending',
3,
strtotime( 'now - 3 days' ),
'wcpt-scheduled',
),

// After the camp ends and no transparency report is received.
Expand All @@ -221,6 +242,7 @@ public function data_timed_messages_sent() {
'wcor_send_days_after_and_no_report',
3,
strtotime( 'now - 3 days' ),
'wcpt-scheduled',
),
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -548,14 +548,18 @@ public function send_timed_emails() {
}

foreach ( $reminder_emails as $email ) {
if ( ! $this->timed_email_is_ready_to_send( $wordcamp, $email, $sent_email_ids ) ) {
continue;
}

$recipient = $this->get_recipients( $wordcamp->ID, $email->ID );

if ( $this->timed_email_is_ready_to_send( $wordcamp, $email, $sent_email_ids ) ) {
if ( $this->mail( $recipient, $email->post_title, $email->post_content, array(), $email, $wordcamp ) ) {
$sent_email_ids[] = $email->ID;
update_post_meta( $wordcamp->ID, 'wcor_sent_email_ids', $sent_email_ids );
}
if ( ! $this->mail( $recipient, $email->post_title, $email->post_content, array(), $email, $wordcamp ) ) {
continue;
}

$sent_email_ids[] = $email->ID;
update_post_meta( $wordcamp->ID, 'wcor_sent_email_ids', $sent_email_ids );
}
}
}
Expand Down Expand Up @@ -587,6 +591,10 @@ protected function timed_email_is_ready_to_send( $wordcamp, $email, $sent_email_
return $ready;
}

if ( in_array( $email->ID, $sent_email_ids ) ) {
return $ready;
}

$send_when = get_post_meta( $email->ID, 'wcor_send_when', true );
$start_date = get_post_meta( $wordcamp->ID, 'Start Date (YYYY-mm-dd)', true );
$end_date = get_post_meta( $wordcamp->ID, 'End Date (YYYY-mm-dd)', true );
Expand All @@ -595,18 +603,22 @@ protected function timed_email_is_ready_to_send( $wordcamp, $email, $sent_email_
$end_date = $start_date;
}

if ( ! in_array( $email->ID, $sent_email_ids ) ) {
if ( 'wcor_send_before' == $send_when ) {
$days_before = absint( get_post_meta( $email->ID, 'wcor_send_days_before', true ) );
if ( 'wcor_send_before' == $send_when ) {
$days_before = absint( get_post_meta( $email->ID, 'wcor_send_days_before', true ) );

if ( $start_date && $days_before ) {
$send_date = $start_date - ( $days_before * DAY_IN_SECONDS );
if ( $start_date && $days_before ) {
$send_date = $start_date - ( $days_before * DAY_IN_SECONDS );

if ( $send_date <= current_time( 'timestamp' ) ) {
$ready = true;
}
if ( $send_date <= current_time( 'timestamp' ) ) {
$ready = true;
}
} elseif ( 'wcor_send_after' == $send_when ) {
}
} elseif ( 'wcor_send_after' == $send_when ) {
/**
* Do not send emails with "send X days after the camp ends" trigger if WordCamp didn't happen.
* All WordCamps that happen, should have public status.
*/
if ( in_array( $wordcamp->post_status, WordCamp_Loader::get_public_post_statuses() ) ) {
$days_after = absint( get_post_meta( $email->ID, 'wcor_send_days_after', true ) );

if ( $end_date && $days_after ) {
Expand All @@ -616,27 +628,27 @@ protected function timed_email_is_ready_to_send( $wordcamp, $email, $sent_email_
$ready = true;
}
}
} elseif ( 'wcor_send_after_pending' == $send_when ) {
$days_after_pending = absint( get_post_meta( $email->ID, 'wcor_send_days_after_pending', true ) );
$timestamp_added_to_pending_schedule = absint( get_post_meta( $wordcamp->ID, '_timestamp_added_to_planning_schedule', true ) );
}
} elseif ( 'wcor_send_after_pending' == $send_when ) {
$days_after_pending = absint( get_post_meta( $email->ID, 'wcor_send_days_after_pending', true ) );
$timestamp_added_to_pending_schedule = absint( get_post_meta( $wordcamp->ID, '_timestamp_added_to_planning_schedule', true ) );

if ( $days_after_pending && $timestamp_added_to_pending_schedule ) {
$execution_timestamp = $timestamp_added_to_pending_schedule + ( $days_after_pending * DAY_IN_SECONDS );
if ( $days_after_pending && $timestamp_added_to_pending_schedule ) {
$execution_timestamp = $timestamp_added_to_pending_schedule + ( $days_after_pending * DAY_IN_SECONDS );

if ( $execution_timestamp <= current_time( 'timestamp' ) ) {
$ready = true;
}
if ( $execution_timestamp <= current_time( 'timestamp' ) ) {
$ready = true;
}
} elseif ( 'wcor_send_after_and_no_report' == $send_when ) {
$days_after_and_no_report = absint( get_post_meta( $email->ID, 'wcor_send_days_after_and_no_report', true ) );
$report_received = get_post_meta( $wordcamp->ID, 'Transparency Report Received', true );
}
} elseif ( 'wcor_send_after_and_no_report' == $send_when ) {
$days_after_and_no_report = absint( get_post_meta( $email->ID, 'wcor_send_days_after_and_no_report', true ) );
$report_received = get_post_meta( $wordcamp->ID, 'Transparency Report Received', true );

if ( $end_date && $days_after_and_no_report && ! $report_received ) {
$execution_timestamp = $end_date + ( $days_after_and_no_report * DAY_IN_SECONDS );
if ( $end_date && $days_after_and_no_report && ! $report_received ) {
$execution_timestamp = $end_date + ( $days_after_and_no_report * DAY_IN_SECONDS );

if ( $execution_timestamp <= current_time( 'timestamp' ) ) {
$ready = true;
}
if ( $execution_timestamp <= current_time( 'timestamp' ) ) {
$ready = true;
}
}
}
Expand Down

0 comments on commit f5adc25

Please sign in to comment.