Make WordPress Core


Ignore:
Timestamp:
09/19/2013 08:45:06 AM (12 years ago)
Author:
dd32
Message:

WordPress Core Automatic Updates: Add a post-upgrade summary email to the WordPress install's admin email address. See #22704

File:
1 edited

Legend:

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

    r25495 r25496  
    11841184
    11851185        // 2: If we're running a newer version, that's a nope
    1186         if ( version_compare( $wp_version, $offered_ver, '>=' ) )
     1186        if ( version_compare( $wp_version, $offered_ver, '>' ) )
    11871187            return false;
    11881188
     
    12891289class WP_Automatic_Upgrader {
    12901290
    1291     static $skin;
     1291    static $upgrade_results = array();
    12921292
    12931293    static function upgrader_disabled() {
     
    13631363
    13641364    // Checks to see if WP_Filesystem is setup to allow unattended upgrades
    1365     static function can_auto_update( $context ) {
    1366         if ( ! self::$skin )
    1367             self::$skin = new Automatic_Upgrader_Skin();
    1368         return (bool) self::$skin->request_filesystem_credentials( false, $context );
     1365    static function can_auto_update( $context, $skin = false ) {
     1366        if ( ! $skin )
     1367            $skin = new Automatic_Upgrader_Skin();
     1368        return (bool) $skin->request_filesystem_credentials( false, $context );
    13691369    }
    13701370
    13711371    static function upgrade( $type, $item ) {
    13721372
    1373         if ( ! self::$skin )
    1374             self::$skin = new Automatic_Upgrader_Skin();
     1373        $skin = new Automatic_Upgrader_Skin();
    13751374
    13761375        switch ( $type ) {
    13771376            case 'core':
    13781377                // The Core upgrader doesn't use the Upgrader's skin during the actual main part of the upgrade, instead, firing a filter
    1379                 add_filter( 'update_feedback', array( self::$skin, 'feedback' ) );
    1380                 $upgrader = new Core_Upgrader( self::$skin );
     1378                add_filter( 'update_feedback', array( $skin, 'feedback' ) );
     1379                $upgrader = new Core_Upgrader( $skin );
    13811380                $context  = ABSPATH;
    13821381                break;
    13831382            case 'plugin':
    1384                 $upgrader = new Plugin_Upgrader( self::$skin );
     1383                $upgrader = new Plugin_Upgrader( $skin );
    13851384                $context  = WP_PLUGIN_DIR; // We don't support custom Plugin directories, or updates for WPMU_PLUGIN_DIR
    13861385                break;
    13871386            case 'theme':
    1388                 $upgrader = new Theme_Upgrader( self::$skin );
     1387                $upgrader = new Theme_Upgrader( $skin );
    13891388                $context  = get_theme_root( $item );
    13901389                break;
     
    13921391
    13931392        // Determine if we can perform this upgrade or not
    1394         if ( ! self::should_auto_update( $type, $item, $context )  || ! self::can_auto_update( $context ) )
     1393        if ( ! self::should_auto_update( $type, $item, $context )  || ! self::can_auto_update( $context, $skin ) )
    13951394            return false;
     1395
     1396        switch ( $type ) {
     1397            case 'core':
     1398                $skin->feedback( __( 'Updating to WordPress %s' ), $item->version );
     1399                $item_name = sprintf( __( 'WordPress %s' ), $item->version );
     1400                break;
     1401            case 'theme':
     1402                $theme = wp_get_theme( $item );
     1403                $item_name = $theme->Get( 'Name' );
     1404                $skin->feedback( __( 'Updating Theme: %s' ), $item_name );
     1405                break;
     1406            case 'plugin':
     1407                $plugin_data = get_plugin_data( $context . '/' . $item );
     1408                $item_name = $plugin_data['Name'];
     1409                $skin->feedback( __( 'Updating Plugin: %s' ), $item_name );
     1410                break;
     1411        }
    13961412
    13971413        // Boom, This sites about to get a whole new splash of paint!
     
    14031419        if ( 'core' == $type ) {
    14041420            if ( is_wp_error( $upgrade_result ) ) {
    1405                 self::$skin->error( __( 'Installation Failed' ), $upgrade_result );
     1421                $skin->error( __( 'Installation Failed' ), $upgrade_result );
    14061422            } else {
    1407                 self::$skin->feedback( __( 'WordPress updated successfully' ) );
     1423                $skin->feedback( __( 'WordPress updated successfully' ) );
    14081424            }
    14091425        }
    14101426
    1411         // Clear cache's and transients
    1412         switch ( $type ) {
    1413             case 'core':
    1414                 delete_site_transient( 'update_core' );
    1415                 break;
    1416             case 'theme':
    1417                 wp_clean_themes_cache();
    1418                 break;
    1419             case 'plugin':
    1420                 wp_clean_plugins_cache();
    1421                 break;
    1422         }
    1423 
    1424         wp_mail(
    1425             get_site_option( 'admin_email' ),
    1426             __METHOD__,
    1427             var_export( array(
    1428                 $upgrade_result,
    1429                 $upgrader,
    1430                 self::$skin,
    1431             ), true )
     1427        self::$upgrade_results[ $type ][] = (object) array(
     1428            'item'     => $item,
     1429            'result'   => ! is_wp_error( $upgrade_result ) && $upgrade_result,
     1430            'name'     => $item_name,
     1431            'messages' => $skin->get_upgrade_messages()
    14321432        );
    14331433
     
    14771477        wp_version_check(); // Check for Core updates
    14781478        $core_update = find_core_auto_update();
    1479         if ( $core_update )
     1479        if ( $core_update ) {
    14801480            self::upgrade( 'core', $core_update );
     1481            delete_site_transient( 'update_core' );
     1482        }
    14811483
    14821484        // Cleanup, These won't trigger any updates this time due to the locking transient
     
    14851487        wp_update_plugins(); // Check for Plugin updates
    14861488
     1489        self::send_email();
     1490
    14871491        // Clear the lock
    14881492        delete_site_option( $lock_name );
     
    14901494    }
    14911495
     1496    static function send_email() {
     1497
     1498        if ( empty( self::$upgrade_results ) )
     1499            return;
     1500
     1501        $upgrade_count = 0;
     1502        foreach ( self::$upgrade_results as $type => $upgrades )
     1503            $upgrade_count += count( $upgrades );
     1504
     1505        $subject = sprintf( '[%s] %s updates have finished', get_bloginfo( 'name' ), $upgrade_count );
     1506
     1507        $body = '';
     1508        if ( isset( self::$upgrade_results['core'] ) ) {
     1509            $result = self::$upgrade_results['core'][0];
     1510            if ( $result->result && ! is_wp_error( $result->result ) )
     1511                $body[] = sprintf( __( 'WordPress was successfully updated to %s' ), $result->name );
     1512            else
     1513                $body[] = sprintf( __( 'WordPress unsuccessfully attempted to update to %s' ), $result->name );
     1514        }
     1515
     1516        // Plugins
     1517        if ( isset( self::$upgrade_results['plugin'] ) ) {
     1518            $success_plugins = wp_list_filter( self::$upgrade_results['plugin'], array( 'result' => true ) );
     1519
     1520            if ( $success_plugins )
     1521                $body[] = sprintf( _n( 'The following plugin was successfully updated: %s', 'The following plugins were successfully updated: %s', count( $success_plugins ) ), implode( ', ', wp_list_pluck( $success_plugins, 'name' ) ) );
     1522            if ( $success_plugins != self::$upgrade_results['plugin'] ) {
     1523                // Failed updates
     1524                $failed_plugins = array();
     1525                foreach ( self::$upgrade_results['plugin'] as $plugin ) {
     1526                    if ( ! $plugin->result || is_wp_error( $plugin->result ) )
     1527                        $failed_plugins[] = $plugin;
     1528                }
     1529                $body[] = sprintf( _n( 'The following plugin failed to successfully update: %s', 'The following plugins failed to successfully update: %s', count( $success_plugins ) ), implode( ', ', wp_list_pluck( $failed_plugins, 'name' ) ) );
     1530            }
     1531        }
     1532
     1533        // Themes
     1534        if ( isset( self::$upgrade_results['theme'] ) ) {
     1535            $success_themes = wp_list_filter( self::$upgrade_results['theme'], array( 'result' => true ) );
     1536
     1537            if ( $success_themes )
     1538                $body[] = sprintf( _n( 'The following theme was successfully updated: %s', 'The following themes were successfully updated: %s', count( $success_themes ) ), implode( ', ', wp_list_pluck( $success_themes, 'name' ) ) );
     1539            if ( $success_themes != self::$upgrade_results['theme'] ) {
     1540                // Failed updates
     1541                $failed_themes = array();
     1542                foreach ( self::$upgrade_results['theme'] as $theme ) {
     1543                    if ( ! $theme->result || is_wp_error( $theme->result ) )
     1544                        $failed_themes[] = $theme;
     1545                }
     1546                $body[] = sprintf( _n( 'The following theme failed to successfully update: %s', 'The following themes failed to successfully update: %s', count( $failed_themes ) ), implode( ', ', wp_list_pluck( $failed_themes, 'name' ) ) );
     1547            }
     1548        }
     1549
     1550        $body[] = '';
     1551        $body[] = __( 'Below is the upgrade logs for update performed' );
     1552
     1553        foreach ( array( 'core', 'plugin', 'theme' ) as $type ) {
     1554            if ( ! isset( self::$upgrade_results[ $type ] ) )
     1555                continue;
     1556            foreach ( self::$upgrade_results[ $type ] as $upgrade ) {
     1557                $body[] = $upgrade->name;
     1558                $body[] = str_repeat( '=', strlen( $upgrade->name ) );
     1559                foreach ( $upgrade->messages as $message )
     1560                    $body[] = "  " . html_entity_decode( $message );
     1561                $body[] = '';
     1562            }
     1563        }
     1564
     1565        //echo "<h1>$subject</h1>";
     1566        //echo '<pre>' . implode( "\n", $body ) . '</pre>';
     1567
     1568        wp_mail(
     1569            get_site_option( 'admin_email' ),
     1570            $subject,
     1571            implode( "\n", $body )
     1572        );
     1573    }
     1574
    14921575}
Note: See TracChangeset for help on using the changeset viewer.