From 574ab7fd4eb3b0dee968889fa635e0cb68016ff6 Mon Sep 17 00:00:00 2001 From: Ian Dunn Date: Tue, 16 Aug 2022 13:27:04 -0700 Subject: [PATCH] Pledges: Email companies every 6 months to update pledges. Fixes #211 --- plugins/wporg-5ftf/includes/email.php | 40 ++++++++++++++ plugins/wporg-5ftf/includes/pledge.php | 75 ++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) diff --git a/plugins/wporg-5ftf/includes/email.php b/plugins/wporg-5ftf/includes/email.php index 25cabcd..2538a51 100644 --- a/plugins/wporg-5ftf/includes/email.php +++ b/plugins/wporg-5ftf/includes/email.php @@ -257,3 +257,43 @@ function send_contributor_inactive_email( array $contributor ) : bool { return send_email( $contributor['user_email'], $subject, $message ); } + +/** + * Ask a company to update their pledge for accuracy. + */ +function send_pledge_update_email( WP_Post $pledge ) : bool { + $to = $pledge->{'5ftf_org-pledge-email'}; + $subject = 'Please review your Five for the Future pledge'; + $url = get_permalink( $pledge ); + + $message = "Hi $pledge->post_title, this is a bi-annual reminder to please review your Five for the Future pledge if you haven't lately. Please make sure your list of contributors and their hours/teams are accurate, especially if any folks at your company have left or changed roles recently. + + Having an accurate roster helps Make team representatives find contributors to work on projects. + + You can view your contributors by visiting $url. To edit your pledge, click on the Edit Pledge link and enter $to. + + Thanks!"; + $message = str_replace( "\t", '', $message ); + + return send_email( $to, $subject, $message, $pledge->ID ); +} + +/** + * Ask a company to update their pledge for accuracy. + */ +function send_pledge_inactive_email( WP_Post $pledge ) : bool { + $to = $pledge->{'5ftf_org-pledge-email'}; + $subject = 'Please review your Five for the Future pledge'; + $url = get_permalink( $pledge ); + + $message = "Hi $pledge->post_title, we noticed that your Five for the Future pledge doesn't have any confirmed contributors right now. Would you like any help getting plugged into the WordPress project? + + If you're no longer able to sponsor a contributor, that's ok! We understand, and appreciate the effort you've made. You can deactivate your pledge by visiting $url, clicking on the Edit Pledge link, and entering $to. You can reactivate it at any time in the future by contacting us. + + If your pledge doesn't have any contributors in 2 months, then we'll automatically deactivate it. That's necessary in order to help Make team representatives find active contributors who can help with projects. + + Thanks!"; + $message = str_replace( "\t", '', $message ); + + return send_email( $to, $subject, $message, $pledge->ID ); +} diff --git a/plugins/wporg-5ftf/includes/pledge.php b/plugins/wporg-5ftf/includes/pledge.php index ec95bb3..f2e4e83 100755 --- a/plugins/wporg-5ftf/includes/pledge.php +++ b/plugins/wporg-5ftf/includes/pledge.php @@ -39,6 +39,10 @@ add_action( 'wp_enqueue_scripts', __NAMESPACE__ . '\enqueue_assets' ); add_action( 'pledge_footer', __NAMESPACE__ . '\render_manage_link_request' ); add_action( 'wp_footer', __NAMESPACE__ . '\render_js_templates' ); +// Misc +add_action( 'init', __NAMESPACE__ . '\schedule_cron_jobs' ); +add_action( '5ftf_send_update_reminders', __NAMESPACE__ . '\send_update_reminders' ); + /** * Register all the things. * @@ -549,3 +553,74 @@ function render_js_templates() { require_once FiveForTheFuture\get_views_path() . 'modal-request-manage-link.php'; } } + + +/** + * Schedule cron jobs. + * + * This needs to run on the `init` action, because Cavalcade isn't fully loaded before that, and events + * wouldn't be scheduled. + * + * @see https://dotorg.trac.wordpress.org/changeset/15351/ + */ +function schedule_cron_jobs() { + if ( ! wp_next_scheduled( '5ftf_send_update_reminders' ) ) { + wp_schedule_event( time(), 'daily', '5ftf_send_update_reminders' ); + } +} + +/** + * Periodically ask companies to review their pledge for accuracy. + */ +function send_update_reminders() : void { + $resend_interval = 6 * MONTH_IN_SECONDS; + $resend_threshold = time() - ( $resend_interval ); + $deactivation_date = time() + ( 2 * MONTH_IN_SECONDS ); + + $pledges = get_posts( array( + 'post_type' => CPT_ID, + 'post_status' => 'publish', + 'posts_per_page' => 15, // Limit # of emails to maintain IP reputation. + + // New pledges haven't had time to become inaccurate yet. + 'date_query' => array( + 'column' => 'post_date', + 'before' => "$resend_interval seconds ago", + ), + + 'meta_query' => array( + 'relation' => 'AND', + + array( + 'relation' => 'OR', + array( + 'key' => '5ftf_last_update_reminder', + 'compare' => 'NOT EXISTS', + ), + array( + 'key' => '5ftf_last_update_reminder', + 'value' => $resend_threshold, + 'compare' => '<', + ), + ), + + array( + 'key' => '5ftf_inactive_deactivate_date', + 'compare' => 'NOT EXISTS', + ), + ) + ) ); + + foreach ( $pledges as $pledge ) { + $contributor_count = absint( $pledge->{'5ftf_pledge-confirmed-contributors'} ); + + if ( $contributor_count ) { + Email\send_pledge_update_email( $pledge ); + } else { + Email\send_pledge_inactive_email( $pledge ); + update_post_meta( $pledge->ID, '5ftf_inactive_deactivate_date', $deactivation_date ); + } + + update_post_meta( $pledge->ID, '5ftf_last_update_reminder', time() ); + } +}