Repository::POST_TYPE, 'post_status' => 'publish', 'posts_per_page' => 100, 'fields' => 'ids', 'no_found_rows' => true, ); // phpcs:disable WordPress.DB.SlowDBQuery.slow_db_query_meta_query $some = get_posts( $base + array( 'meta_query' => array( array( 'key' => 'mapp_stage', 'value' => $stage ), array( 'key' => $meta, 'value' => $count, 'compare' => '<', 'type' => 'NUMERIC' ), ), ) ); // Applications that never had one have no sent-count row. $none = get_posts( $base + array( 'meta_query' => array( array( 'key' => 'mapp_stage', 'value' => $stage ), array( 'key' => $meta, 'compare' => 'NOT EXISTS' ), ), ) ); // phpcs:enable return array_map( 'intval', array_unique( array_merge( $some, $none ) ) ); } /** * @param int $id Application ID. * @return bool Whether reminders may go to this application at all. */ private static function sendable( $id ) { // No address, or an admin revoked this application's links: send nothing. return '' !== (string) get_post_meta( $id, 'email', true ) && '' === (string) get_post_meta( $id, 'mapp_links_revoked_at', true ); } /** * @param int $id Application ID. * @param int[] $schedule Hours. */ public static function maybe_send( $id, $schedule ) { $sent = (int) get_post_meta( $id, 'mapp_reminders_sent', true ); $contact = (int) get_post_meta( $id, 'mapp_contact_at', true ); if ( ! $contact || ! isset( $schedule[ $sent ] ) || ! self::sendable( $id ) ) { return; } if ( time() - $contact < $schedule[ $sent ] * HOUR_IN_SECONDS ) { return; } $token = Tokens::issue( $id, 'link' ); Mailer::reminder( $id, Controller::resume_url( $id, $token ) ); update_post_meta( $id, 'mapp_reminders_sent', $sent + 1 ); update_post_meta( $id, 'mapp_reminder_' . ( $sent + 1 ) . '_at', time() ); } /** * Full 1003 started but not submitted: remind after the gap since the * last time the borrower worked on it. * * @param int $id Application ID. * @param int[] $schedule Hours. */ public static function maybe_send_full( $id, $schedule ) { $sent = (int) get_post_meta( $id, 'mapp_full_reminders_sent', true ); $last = max( (int) get_post_meta( $id, 'mapp_full_started_at', true ), (int) get_post_meta( $id, 'mapp_updated_at', true ), (int) get_post_meta( $id, 'mapp_full_reminder_' . $sent . '_at', true ) ); if ( ! $last || ! isset( $schedule[ $sent ] ) || ! self::sendable( $id ) ) { return; } // The second waits for the gap after the first, so the schedule is 2 days, then 5. $wait = $sent ? $schedule[ $sent ] - $schedule[ $sent - 1 ] : $schedule[0]; if ( time() - $last < $wait * HOUR_IN_SECONDS ) { return; } $token = Tokens::issue( $id, 'link' ); Mailer::full_reminder( $id, Controller::resume_url( $id, $token ) ); update_post_meta( $id, 'mapp_full_reminders_sent', $sent + 1 ); update_post_meta( $id, 'mapp_full_reminder_' . ( $sent + 1 ) . '_at', time() ); } /** * Reminder history and the next one due, for the application screen. * * @param int $id Application ID. * @return array [ sent => [ [ label, time ] ], next => time|0, next_label => string ]. */ public static function status( $id ) { $out = array( 'sent' => array(), 'next' => 0, 'next_label' => '' ); foreach ( array( 'mapp_reminder_' => 'Short app reminder', 'mapp_full_reminder_' => 'Full 1003 reminder' ) as $prefix => $label ) { for ( $n = 1; $n <= 5; $n++ ) { $at = (int) get_post_meta( $id, $prefix . $n . '_at', true ); if ( $at ) { $out['sent'][] = array( $label . ' ' . $n, $at ); } } } if ( 'no' === get_option( 'mapp_reminders', 'yes' ) || ! self::sendable( $id ) ) { return $out; } $stage = Repository::stage( $id ); if ( Repository::STAGE_CONTACT === $stage ) { $sent = (int) get_post_meta( $id, 'mapp_reminders_sent', true ); $schedule = self::schedule(); $contact = (int) get_post_meta( $id, 'mapp_contact_at', true ); if ( $contact && isset( $schedule[ $sent ] ) ) { $out['next'] = $contact + $schedule[ $sent ] * HOUR_IN_SECONDS; $out['next_label'] = 'Short app reminder ' . ( $sent + 1 ); } } elseif ( Repository::STAGE_FULL_STARTED === $stage ) { $sent = (int) get_post_meta( $id, 'mapp_full_reminders_sent', true ); $schedule = self::full_schedule(); $last = max( (int) get_post_meta( $id, 'mapp_full_started_at', true ), (int) get_post_meta( $id, 'mapp_updated_at', true ), (int) get_post_meta( $id, 'mapp_full_reminder_' . $sent . '_at', true ) ); if ( $last && isset( $schedule[ $sent ] ) ) { $out['next'] = $last + ( $sent ? $schedule[ $sent ] - $schedule[ $sent - 1 ] : $schedule[0] ) * HOUR_IN_SECONDS; $out['next_label'] = 'Full 1003 reminder ' . ( $sent + 1 ); } } return $out; } }