0 ) { if ( ! $scheduled ) { wp_schedule_event( time() + HOUR_IN_SECONDS, 'daily', ACSCF_STALE_HOOK ); } return; } if ( $scheduled ) { wp_unschedule_event( $scheduled, ACSCF_STALE_HOOK ); } } /** * Find submissions that are still marked as untouched after the threshold. * * Only categories that are set to notify are considered, so the reminder never * nags about messages that were filed as sales or spam on purpose. * * @param int $days Threshold in days. * @return array Entries. */ function acscf_get_stale_entries( $days ) { $days = (int) $days; if ( $days < 1 ) { return array(); } $settings = acscf_get_settings(); $categories = array(); foreach ( (array) $settings['notify_categories'] as $category ) { $categories[] = acscf_sanitize_category( $category ); } if ( empty( $categories ) ) { return array(); } $query = new WP_Query( array( 'post_type' => 'acscf_entry', 'post_status' => 'private', 'posts_per_page' => 200, // phpcs:ignore WordPress.WP.PostsPerPage.posts_per_page_posts_per_page -- Bounded batch for the daily reminder e-mail. 'orderby' => 'date', 'order' => 'ASC', 'no_found_rows' => true, 'fields' => 'ids', 'date_query' => array( array( 'before' => gmdate( 'Y-m-d H:i:s', time() - ( $days * DAY_IN_SECONDS ) ), 'column' => 'post_date_gmt', ), ), 'meta_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- A short daily query on a small, plugin owned post type. 'relation' => 'AND', array( 'key' => '_acscf_status', 'value' => 'new', ), array( 'key' => '_acscf_category', 'value' => $categories, 'compare' => 'IN', ), ), ) ); $entries = array(); foreach ( $query->posts as $entry_id ) { $entry = acscf_get_entry( $entry_id ); if ( $entry ) { $entries[] = $entry; } } return $entries; } /** * Send the daily reminder mail. * * @return bool Whether a mail was sent. */ function acscf_run_stale_alert() { $settings = acscf_get_settings(); $days = (int) $settings['stale_days']; if ( $days < 1 ) { return false; } $entries = acscf_get_stale_entries( $days ); if ( empty( $entries ) ) { return false; } $to = ! empty( $settings['stale_email'] ) && is_email( $settings['stale_email'] ) ? $settings['stale_email'] : get_option( 'admin_email' ); $subject = sprintf( /* translators: 1: site name, 2: number of untouched submissions, 3: threshold in days. */ __( '[%1$s] %2$d inquiries are still open (%3$d days or more)', 'acs-contact-form' ), get_bloginfo( 'name' ), count( $entries ), $days ); $body = sprintf( /* translators: %d: threshold in days. */ __( 'These inquiries have been open for %d days or more.', 'acs-contact-form' ), $days ) . "\n\n"; foreach ( $entries as $entry ) { $body .= '- ' . $entry['title'] . "\n"; $body .= ' ' . sprintf( /* translators: 1: category label, 2: sender name, 3: sender email. */ __( 'Category: %1$s / From: %2$s <%3$s>', 'acs-contact-form' ), acscf_category_label( $entry['category'] ), '' !== $entry['name'] ? $entry['name'] : '-', '' !== $entry['email'] ? $entry['email'] : '-' ) . "\n"; $body .= ' ' . sprintf( /* translators: 1: submission date, 2: elapsed time. */ __( 'Received: %1$s (%2$s)', 'acs-contact-form' ), $entry['created'], acscf_elapsed_label( $entry['created_gmt'] ) ) . "\n"; $body .= ' ' . acscf_entry_admin_url( $entry['id'] ) . "\n\n"; } $body .= __( 'See the full list:', 'acs-contact-form' ) . "\n" . acscf_entries_admin_url() . "\n"; return (bool) wp_mail( $to, $subject, $body ); } add_action( ACSCF_STALE_HOOK, 'acscf_run_stale_alert' );