Skip to content
This repository has been archived by the owner on Feb 22, 2022. It is now read-only.

Handle both themes and plugins email notifications #36

Merged
merged 1 commit into from
Mar 27, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 57 additions & 25 deletions wp-autoupdates.php
Original file line number Diff line number Diff line change
Expand Up @@ -841,42 +841,51 @@ function wp_autoupdates_debug_information( $info ) {


/**
* If we tried to perform plugin updates, check if we should send an email.
* If we tried to perform plugin or theme updates, check if we should send an email.
*
* @param object $results The result of the plugin updates.
* @param object $results The result of updates tasks.
*/
function wp_autoupdates_automatic_updates_complete_notification( $results ) {
$successful_updates = array();
$failed_updates = array();
if ( isset( $results['plugin'] ) ) {
foreach ( $results['plugin'] as $update_result ) {
if ( true === $update_result->result ) {
$successful_updates[] = $update_result;
$successful_updates['plugin'][] = $update_result;
} else {
$failed_updates[] = $update_result;
$failed_updates['plugin'][] = $update_result;
}
}
if ( empty( $successful_updates ) && empty( $failed_updates ) ) {
return;
}
if ( empty( $failed_updates ) ) {
wp_autoupdates_send_email_notification( 'success', $successful_updates, $failed_updates );
} elseif ( empty( $successful_updates ) ) {
wp_autoupdates_send_email_notification( 'fail', $successful_updates, $failed_updates );
} else {
wp_autoupdates_send_email_notification( 'mixed', $successful_updates, $failed_updates );
}
if ( isset( $results['theme'] ) ) {
foreach ( $results['theme'] as $update_result ) {
if ( true === $update_result->result ) {
$successful_updates['theme'][] = $update_result;
} else {
$failed_updates['theme'][] = $update_result;
}
}
}
if ( empty( $successful_updates ) && empty( $failed_updates ) ) {
return;
}
if ( empty( $failed_updates ) ) {
wp_autoupdates_send_email_notification( 'success', $successful_updates, $failed_updates );
} elseif ( empty( $successful_updates ) ) {
wp_autoupdates_send_email_notification( 'fail', $successful_updates, $failed_updates );
} else {
wp_autoupdates_send_email_notification( 'mixed', $successful_updates, $failed_updates );
}
}
add_action( 'automatic_updates_complete', 'wp_autoupdates_automatic_updates_complete_notification' );


/**
* Sends an email upon the completion or failure of a plugin background update.
* Sends an email upon the completion or failure of a plugin or theme background update.
*
* @param string $type The type of email to send. Can be one of 'success', 'failure', 'mixed'.
* @param array $successful_updates A list of plugin updates that succeeded.
* @param array $failed_updates A list of plugin updates that failed.
* @param array $successful_updates A list of updates that succeeded.
* @param array $failed_updates A list of updates that failed.
*/
function wp_autoupdates_send_email_notification( $type, $successful_updates, $failed_updates ) {
// No updates were attempted.
Expand All @@ -888,25 +897,25 @@ function wp_autoupdates_send_email_notification( $type, $successful_updates, $fa
switch ( $type ) {
case 'success':
/* translators: %s: Site title. */
$subject = __( '[%s] Plugins have automatically updated', 'wp-autoupdates' );
$subject = __( '[%s] Some plugins or themes have automatically updated', 'wp-autoupdates' );
break;
case 'fail':
/* translators: %s: Site title. */
$subject = __( '[%s] Plugins have failed to update', 'wp-autoupdates' );
$subject = __( '[%s] Some plugins or themes have failed to update', 'wp-autoupdates' );
$body[] = sprintf(
/* translators: %s: Home URL. */
__( 'Howdy! Failures occurred when attempting to update plugins on your site at %s.', 'wp-autoupdates' ),
__( 'Howdy! Failures occurred when attempting to update plugins/themes on your site at %s.', 'wp-autoupdates' ),
home_url()
);
$body[] = "\n";
$body[] = __( 'Please check out your site now. It’s possible that everything is working. If it says you need to update, you should do so.', 'wp-autoupdates' );
break;
case 'mixed':
/* translators: %s: Site title. */
$subject = __( '[%s] Some plugins have automatically updated', 'wp-autoupdates' );
$subject = __( '[%s] Some plugins/themes have automatically updated', 'wp-autoupdates' );
$body[] = sprintf(
/* translators: %s: Home URL. */
__( 'Howdy! There were some failures while attempting to update plugins on your site at %s.', 'wp-autoupdates' ),
__( 'Howdy! There were some failures while attempting to update plugins/themes on your site at %s.', 'wp-autoupdates' ),
home_url()
);
$body[] = "\n";
Expand All @@ -915,22 +924,45 @@ function wp_autoupdates_send_email_notification( $type, $successful_updates, $fa
break;
}

if ( in_array( $type, array( 'fail', 'mixed' ), true ) && ! empty( $failed_updates ) ) {
// Get failed plugin updates
if ( in_array( $type, array( 'fail', 'mixed' ), true ) && ! empty( $failed_updates['plugin'] ) ) {
$body[] = __( 'The following plugins failed to update:' );
// List failed updates.
foreach ( $failed_updates as $item ) {
foreach ( $failed_updates['plugin'] as $item ) {
/* translators: %s: Name of the related plugin. */
$body[] = ' ' . sprintf( __( '- %s', 'wp-autoupdates' ), $item->name );
}
$body[] = "\n";
}
if ( in_array( $type, array( 'success', 'mixed' ), true ) && ! empty( $successful_updates ) ) {
// Get failed theme updates
if ( in_array( $type, array( 'fail', 'mixed' ), true ) && ! empty( $failed_updates['theme'] ) ) {
$body[] = __( 'The following themes failed to update:' );
// List failed updates.
foreach ( $failed_updates['theme'] as $item ) {
/* translators: %s: Name of the related plugin. */
$body[] = ' ' . sprintf( __( '- %s', 'wp-autoupdates' ), $item->name );
}
$body[] = "\n";
}
// Get successful plugin updates
if ( in_array( $type, array( 'success', 'mixed' ), true ) && ! empty( $successful_updates['plugin'] ) ) {
$body[] = __( 'The following plugins were successfully updated:' );
// List successful updates.
foreach ( $successful_updates as $plugin ) {
foreach ( $successful_updates['plugin'] as $plugin ) {
/* translators: %s: Name of the related plugin. */
$body[] = ' ' . sprintf( __( '- %s', 'wp-autoupdates' ), $plugin->name );
}
$body[] = "\n";
}
// Get successful theme updates
if ( in_array( $type, array( 'success', 'mixed' ), true ) && ! empty( $successful_updates['theme'] ) ) {
$body[] = __( 'The following themes were successfully updated:' );
// List successful updates.
foreach ( $successful_updates['theme'] as $plugin ) {
/* translators: %s: Name of the related plugin. */
$body[] = ' ' . sprintf( __( '- %s', 'wp-autoupdates' ), $plugin->name );
}
$body[] = "\n";
}
$body[] = "\n";

Expand Down