Make WordPress Core


Ignore:
Timestamp:
07/07/2020 06:58:32 PM (6 years ago)
Author:
desrosj
Message:

Administration: Prevent repeat emails for identical plugin or theme auto-update attempt failures.

This change adds a throttle mechanism to plugin and theme auto-update failure emails using similar logic to the email sent for a Core auto-update.

The first time a plugin or theme auto-update fails, the package and new_version will be tracked in the auto_plugin_theme_update_emails option. An email for this specific update attempt will not be resent.

However, if this update fails again and non-repeat failures or successful updates are also present, then the failure information will be included in that email (an email needs to be sent for the new events regardless).

Props johnbillion, arpitgshah, desrosj, audrasjb, pbiron, earnjam.
Fixes #50448.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/class-wp-automatic-updater.php

    r48344 r48397  
    940940        if ( empty( $successful_updates ) && empty( $failed_updates ) ) {
    941941            return;
     942        }
     943
     944        $unique_failures     = false;
     945        $past_failure_emails = get_option( 'auto_plugin_theme_update_emails', array() );
     946
     947        // When only failures have occurred, an email should only be sent if there are unique failures.
     948        // A failure is considered unique if an email has not been sent for an update attempt failure
     949        // to a plugin or theme with the same new_version.
     950        if ( 'fail' === $type ) {
     951            foreach ( $failed_updates as $update_type => $failures ) {
     952                foreach ( $failures as $failed_update ) {
     953                    if ( ! isset( $past_failure_emails[ $failed_update->item->{$update_type} ] ) ) {
     954                        $unique_failures = true;
     955                        continue;
     956                    }
     957
     958                    // Check that the failure represents a new failure based on the new_version.
     959                    if ( version_compare( $past_failure_emails[ $failed_update->item->{$update_type} ], $failed_update->item->new_version, '<' ) ) {
     960                        $unique_failures = true;
     961                    }
     962                }
     963            }
     964
     965            if ( ! $unique_failures ) {
     966                return;
     967            }
    942968        }
    943969
     
    10181044
    10191045                foreach ( $failed_updates['plugin'] as $item ) {
    1020                     $body[] = "- {$item->name}";
     1046                    $body[]                                     = "- {$item->name}";
     1047                    $past_failure_emails[ $item->item->plugin ] = $item->item->new_version;
    10211048                }
    10221049                $body[] = "\n";
     
    10281055
    10291056                foreach ( $failed_updates['theme'] as $item ) {
    1030                     $body[] = "- {$item->name}";
     1057                    $body[]                                    = "- {$item->name}";
     1058                    $past_failure_emails[ $item->item->theme ] = $item->item->new_version;
    10311059                }
    10321060                $body[] = "\n";
     
    10441072                foreach ( $successful_updates['plugin'] as $item ) {
    10451073                    $body[] = "- {$item->name}";
     1074                    unset( $past_failure_emails[ $item->item->plugin ] );
    10461075                }
    10471076                $body[] = "\n";
     
    10541083                foreach ( $successful_updates['theme'] as $item ) {
    10551084                    $body[] = "- {$item->name}";
     1085                    unset( $past_failure_emails[ $item->item->theme ] );
    10561086                }
    10571087                $body[] = "\n";
     
    11091139        $email = apply_filters( 'auto_plugin_theme_update_email', $email, $type, $successful_updates, $failed_updates );
    11101140
    1111         wp_mail( $email['to'], wp_specialchars_decode( $email['subject'] ), $email['body'], $email['headers'] );
     1141        $result = wp_mail( $email['to'], wp_specialchars_decode( $email['subject'] ), $email['body'], $email['headers'] );
     1142
     1143        if ( $result ) {
     1144            update_option( 'auto_plugin_theme_update_emails', $past_failure_emails );
     1145        }
    11121146    }
    11131147
Note: See TracChangeset for help on using the changeset viewer.