WordPress.org

Plugin Directory

Changeset 631863


Ignore:
Timestamp:
11/29/12 16:35:38 (17 months ago)
Author:
pento
Message:

A big ol' pile of code reshuffling, for OO and WordPress Code Standards shininess.

Location:
automatic-updater/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • automatic-updater/trunk/admin.php

    r629846 r631863  
    11<?php 
    22 
    3 function auto_updater_plugin_menu() { 
    4     $slug = 'options-general.php'; 
    5     if ( is_multisite() ) 
    6         $slug = 'settings.php'; 
     3class Automatic_Updater_Admin { 
     4    private $automatic_updater; 
    75 
    8     $hook = add_submenu_page( $slug, esc_html__( 'Automatic Updater', 'automatic-updater' ), esc_html__( 'Automatic Updater', 'automatic-updater' ), 'update_core', 'automatic-updater', 'auto_updater_settings' ); 
     6    function init( $automatic_updater ) { 
     7        static $instance = false; 
    98 
    10     add_action( "load-$hook", 'auto_updater_settings_loader' ); 
    11 } 
    12 if ( is_multisite() ) 
    13     add_action( 'network_admin_menu', 'auto_updater_plugin_menu' ); 
    14 else 
    15     add_action( 'admin_menu', 'auto_updater_plugin_menu' ); 
     9        if( ! $instance ) 
     10            $instance = new Automatic_Updater_Admin( $automatic_updater ); 
    1611 
    17 function auto_updater_settings_loader() { 
    18     get_current_screen()->add_help_tab( array( 
    19     'id'        => 'overview', 
    20     'title'     => esc_html__( 'Overview', 'automatic-updater' ), 
    21     'content'   => 
    22         '<p>' . esc_html__( 'This settings page allows you to select whether you would like WordPress Core, your plugins, and your themes to be automatically updated.', 'automatic-updater' ) . '</p>' . 
    23         '<p>' . esc_html__( 'It is very important to keep your WordPress installation up to date for security reasons, so unless you have a specific reason not to, we recommend allowing everything to automatically update.', 'automatic-updater' ) . '</p>' 
    24     ) ); 
    25  
    26     get_current_screen()->set_help_sidebar( 
    27         '<p><strong>' . wp_kses( sprintf( __( 'A Plugin By <a href="%s" target="_blank">Gary</a>', 'automatic-updater' ), 'http://pento.net/' ), array( 'a' => array( 'href' => array(), 'target' => array() ) ) ) . '</strong></p>' . 
    28         '<p><a href="http://pento.net/donate/">' . esc_html__( 'Donations', 'automatic-updater' ) . '</a></p>' . 
    29         '<p><a href="http://wordpress.org/support/plugin/automatic-updater">' . esc_html__( 'Support Forums', 'automatic-updater' ) . '</a></p>' 
    30     ); 
    31  
    32     wp_enqueue_style( 'automatic-updater-admin', plugins_url( 'css/admin.css', __FILE__ ) ); 
    33 } 
    34  
    35 function auto_updater_settings() { 
    36     if ( ! current_user_can( 'update_core' ) ) 
    37         wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'automatic-updater' ) ); 
    38  
    39     $message = ''; 
    40     if ( ! empty( $_REQUEST['submit'] ) ) { 
    41         check_admin_referer( 'automatic-updater-settings' ); 
    42  
    43         auto_updater_save_settings(); 
    44         $message = esc_html__( 'Settings updated', 'automatic-updater' ); 
    45     } 
    46     $options = get_option( 'automatic-updater' ); 
    47     $messages = array( 
    48                     'core' => wp_kses( __( 'Update WordPress Core automatically? <strong>(Strongly Recommended)</strong>', 'automatic-updater' ), array( 'strong' => array() ) ), 
    49                     'plugins' => esc_html__( 'Update your plugins automatically?', 'automatic-updater' ), 
    50                     'themes' => esc_html__( 'Update your themes automatically?', 'automatic-updater' ) 
    51                 ); 
    52 ?> 
    53     <div class="wrap"> 
    54         <?php screen_icon('tools'); ?> 
    55         <h2><?php esc_html_e( 'Automatic Updater', 'automatic-updater' ); ?></h2> 
    56 <?php 
    57     if ( ! empty( $message ) ) { 
    58 ?> 
    59         <div class="updated"> 
    60             <p><?php echo $message; ?></p> 
    61         </div> 
    62 <?php 
    63     } 
    64 ?> 
    65         <form method="post"> 
    66         <?php wp_nonce_field( 'automatic-updater-settings' ); ?> 
    67 <?php 
    68     foreach ( $options['update'] as $type => $enabled ) { 
    69         $checked = ''; 
    70         if ( $enabled ) 
    71             $checked = ' checked="checked"'; 
    72  
    73         echo "<p><input type='checkbox' id='$type' name='$type' value='1'$checked> <label for='$type'>{$messages[$type]}</label></p>"; 
    74     } 
    75 ?> 
    76     <br> 
    77     <h3><?php esc_html_e( 'Notification Email', 'automatic-updater' ); ?></h3> 
    78     <p><?php esc_html_e( 'By default, Automatic Updater will send an email to the Site Admin when an update is performed. If you would like to send that email to a different address, you can set it here.', 'automatic-updater' ); ?></p> 
    79     <p><label for="override-email"><?php esc_html_e( 'Override Email Address', 'automatic-updater' ); ?>:</label> <input type="text" name="override-email" id="override-email" value="<?php echo esc_attr( $options['override-email'] ); ?>"></p> 
    80 <?php 
    81     $checked = ''; 
    82     if ( $options['disable-email'] ) 
    83         $checked = ' checked="checked"'; 
    84 ?> 
    85     <p><?php esc_html_e( "If you don't want to receive an email when updates are installed, you can disable them completely.", 'automatic-updater' ); ?></p> 
    86     <p><input type="checkbox" name="disable-email" id="disable-email" value="1"> <label for="disable-email"><?php esc_html_e( 'Disable email notifications.', 'automatic-updater' ); ?></label></p> 
    87 <?php 
    88     if ( is_dir( ABSPATH . '/.svn' ) ) { 
    89         $checked = ''; 
    90         if ( $options['svn'] ) 
    91             $checked = ' checked="checked"'; 
    92 ?> 
    93     <br> 
    94     <h3><?php esc_html_e( 'Retries', 'automatic-updater' ); ?></h3> 
    95     <p><?php esc_html_e( 'To avoid updating when something is broken, Automatic Updater can limit the number of times it will attempt to update WordPress Core, a plugin or a theme. If, for example, a plugin update reaches this limit, Automatic Updater will stop trying to update it until you manually install the update, or a new version is released.', 'automatic-updater' ); ?></p> 
    96     <p><label for="retries-limit"><?php esc_html_e( 'Retries Limit', 'automatic-updater' ); ?>:</label> <input type="number" step="1" name="retries-limit" id="retries-limit" class="small-text" value="<?php echo esc_attr( $options['retries-limit'] ); ?>"></p> 
    97  
    98     <br> 
    99     <h3><?php esc_html_e( 'SVN Support', 'automatic-updater' ); ?></h3> 
    100     <p><?php echo wp_kses( __( "It looks like you're running an SVN version of WordPress, that's cool! Automatic Updater can run <tt>svn up</tt> once an hour, to keep you up-to-date. For safety, enabling this option will disable the normal WordPress core updates.", 'automatic-updater' ), array( 'tt' => array() ) ); ?></p> 
    101 <?php 
    102     if ( !is_writable( ABSPATH . '/.svn' ) ) { 
    103         $uid = posix_getuid(); 
    104         $user = posix_getpwuid( $uid ); 
    105         echo '<div class="automatic-updater-notice"><p>' . wp_kses( sprintf( __( "The .svn directory isn't writable, so <tt>svn up</tt> will probably fail when the web server runs it. You need to give the user <tt>%s</tt> write permissions to your entire WordPress install, including .svn directories.", 'automatic-updater' ), $user['name'] ), array( 'tt' => array() ) ) . '</p></div>'; 
    106     } 
    107 ?> 
    108     <p><input type="checkbox" id="svn" name="svn" value="1"<?php echo $checked; ?>> <label for="svn"><?php echo wp_kses( __( 'Run <tt>svn up</tt> hourly?', 'automatic-updater' ), array( 'tt' => array() ) ); ?></label></p> 
    109 <?php 
    110     } 
    111     else { 
    112         echo '<input type="hidden" name="svn" value="0">'; 
     12        return $instance; 
    11313    } 
    11414 
    115     $checked = ''; 
    116     if ( $options['debug'] ) 
    117         $checked = ' checked="checked"'; 
    118 ?> 
    119         <br> 
    120         <h3><?php esc_html_e( 'Debug Information', 'automatic-updater' ); ?></h3> 
    121         <p><input type="checkbox" id="debug" name="debug" value="1"<?php echo $checked; ?>> <label for="debug"><?php esc_html_e( 'Show debug information in the notification email.', 'automatic-updater' ); ?></label></p> 
    122         <p><input class="button button-primary" type="submit" name="submit" id="submit" value="<?php esc_attr_e( 'Save Changes', 'automatic-updater' ); ?>" /></p> 
    123         </form> 
    124     </div> 
    125 <?php 
    126 } 
     15    function __construct( $automatic_updater ) { 
     16        $this->automatic_updater = $automatic_updater; 
    12717 
    128 function auto_updater_save_settings() { 
    129     $options = get_option( 'automatic-updater' ); 
     18        if ( is_multisite() ) 
     19            add_action( 'network_admin_menu', array( &$this, 'plugin_menu' ) ); 
     20        else 
     21            add_action( 'admin_menu', array( &$this, 'plugin_menu' ) ); 
    13022 
    131     $types = array( 'core', 'plugins', 'themes' ); 
    132     foreach ( $types as $type ) { 
    133         if ( ! empty( $_REQUEST[$type] ) ) 
    134             $options['update'][$type] = true; 
    135         else 
    136             $options['update'][$type] = false; 
     23        add_filter( 'plugin_action_links_' . Automatic_Updater::$basename, array( &$this, 'plugin_row_links' ) ); 
     24        add_filter( 'network_admin_plugin_action_links_' . Automatic_Updater::$basename, array( &$this, 'plugin_row_links' ) ); 
    13725    } 
    13826 
    139     $top_bool_options = array( 'debug', 'svn', 'disable-email' ); 
    140     foreach ( $top_bool_options as $option ) { 
    141         if ( ! empty( $_REQUEST[$option] ) ) 
    142             $options[$option] = true; 
    143         else 
    144             $options[$option] = false; 
     27    function plugin_menu() { 
     28        $slug = 'options-general.php'; 
     29        if ( is_multisite() ) 
     30            $slug = 'settings.php'; 
     31 
     32        $hook = add_submenu_page( $slug, esc_html__( 'Automatic Updater', 'automatic-updater' ), esc_html__( 'Automatic Updater', 'automatic-updater' ), 'update_core', 'automatic-updater', array( &$this, 'settings' ) ); 
     33 
     34        add_action( "load-$hook", array( &$this, 'settings_loader' ) ); 
    14535    } 
    14636 
    147     $top_options = array( 'override-email', 'retries-limit' ); 
    148     foreach ( $top_options as $option ) 
    149         $options[$option] = $_REQUEST[$option]; 
     37    function settings_loader() { 
     38        get_current_screen()->add_help_tab( array( 
     39        'id'        => 'overview', 
     40        'title'     => esc_html__( 'Overview', 'automatic-updater' ), 
     41        'content'   => 
     42            '<p>' . esc_html__( 'This settings page allows you to select whether you would like WordPress Core, your plugins, and your themes to be automatically updated.', 'automatic-updater' ) . '</p>' . 
     43            '<p>' . esc_html__( 'It is very important to keep your WordPress installation up to date for security reasons, so unless you have a specific reason not to, we recommend allowing everything to automatically update.', 'automatic-updater' ) . '</p>' 
     44        ) ); 
    15045 
    151     update_option( 'automatic-updater', $options ); 
     46        get_current_screen()->set_help_sidebar( 
     47            '<p><strong>' . wp_kses( sprintf( __( 'A Plugin By <a href="%s" target="_blank">Gary</a>', 'automatic-updater' ), 'http://pento.net/' ), array( 'a' => array( 'href' => array(), 'target' => array() ) ) ) . '</strong></p>' . 
     48            '<p><a href="http://pento.net/donate/">' . esc_html__( 'Donations', 'automatic-updater' ) . '</a></p>' . 
     49            '<p><a href="http://wordpress.org/support/plugin/automatic-updater">' . esc_html__( 'Support Forums', 'automatic-updater' ) . '</a></p>' 
     50        ); 
     51 
     52        wp_enqueue_style( 'automatic-updater-admin', plugins_url( 'css/admin.css', __FILE__ ) ); 
     53    } 
     54 
     55    function settings() { 
     56        if ( ! current_user_can( 'update_core' ) ) 
     57            wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'automatic-updater' ) ); 
     58 
     59        $message = ''; 
     60        if ( ! empty( $_REQUEST['submit'] ) ) { 
     61            check_admin_referer( 'automatic-updater-settings' ); 
     62 
     63            $this->save_settings(); 
     64            $message = esc_html__( 'Settings updated', 'automatic-updater' ); 
     65        } 
     66 
     67        $messages = array( 
     68                        'core' => wp_kses( __( 'Update WordPress Core automatically? <strong>(Strongly Recommended)</strong>', 'automatic-updater' ), array( 'strong' => array() ) ), 
     69                        'plugins' => esc_html__( 'Update your plugins automatically?', 'automatic-updater' ), 
     70                        'themes' => esc_html__( 'Update your themes automatically?', 'automatic-updater' ) 
     71                    ); 
     72        ?> 
     73        <div class="wrap"> 
     74            <?php screen_icon( 'tools' ); ?> 
     75            <h2><?php esc_html_e( 'Automatic Updater', 'automatic-updater' ); ?></h2> 
     76 
     77            <?php if ( ! empty( $message ) ) { ?> 
     78                <div class="updated"> 
     79                    <p><?php echo $message; ?></p> 
     80                </div> 
     81            <?php } ?> 
     82 
     83            <form method="post"> 
     84                <?php wp_nonce_field( 'automatic-updater-settings' ); ?> 
     85 
     86                <?php 
     87                foreach ( $this->automatic_updater->get_option( 'update' ) as $type => $enabled ) { 
     88                    $checked = ''; 
     89                    if ( $enabled ) 
     90                        $checked = ' checked="checked"'; 
     91         
     92                    echo "<p><input type='checkbox' id='$type' name='$type' value='1'$checked> <label for='$type'>{$messages[$type]}</label></p>"; 
     93                } 
     94                ?> 
     95 
     96                <br> 
     97                <h3><?php esc_html_e( 'Notification Email', 'automatic-updater' ); ?></h3> 
     98                <p><?php esc_html_e( 'By default, Automatic Updater will send an email to the Site Admin when an update is performed. If you would like to send that email to a different address, you can set it here.', 'automatic-updater' ); ?></p> 
     99                <p><label for="override-email"><?php esc_html_e( 'Override Email Address', 'automatic-updater' ); ?>:</label> <input type="text" name="override-email" id="override-email" value="<?php echo esc_attr( $this->automatic_updater->get_option( 'override-email' ) ); ?>"></p> 
     100 
     101                <?php 
     102                $checked = ''; 
     103                if ( $this->automatic_updater->get_option( 'disable-email' ) ) 
     104                    $checked = ' checked="checked"'; 
     105                ?> 
     106 
     107                <p><?php esc_html_e( "If you don't want to receive an email when updates are installed, you can disable them completely.", 'automatic-updater' ); ?></p> 
     108                <p><input type="checkbox" name="disable-email" id="disable-email" value="1"> <label for="disable-email"><?php esc_html_e( 'Disable email notifications.', 'automatic-updater' ); ?></label></p> 
     109 
     110                <br> 
     111                <h3><?php esc_html_e( 'Retries', 'automatic-updater' ); ?></h3> 
     112                <p><?php esc_html_e( 'To avoid updating when something is broken, Automatic Updater can limit the number of times it will attempt to update WordPress Core, a plugin or a theme. If, for example, a plugin update reaches this limit, Automatic Updater will stop trying to update it until you manually install the update, or a new version is released.', 'automatic-updater' ); ?></p> 
     113                <p><label for="retries-limit"><?php esc_html_e( 'Retries Limit', 'automatic-updater' ); ?>:</label> <input type="number" step="1" name="retries-limit" id="retries-limit" class="small-text" value="<?php echo esc_attr( $this->automatic_updater->get_option( 'retries-limit' ) ); ?>"></p> 
     114 
     115                <?php 
     116                if ( is_dir( ABSPATH . '/.svn' ) ) { 
     117                    $checked = ''; 
     118                    if ( $this->automatic_updater->get_option( 'svn' ) ) 
     119                        $checked = ' checked="checked"'; 
     120                ?> 
     121 
     122                    <br> 
     123                    <h3><?php esc_html_e( 'SVN Support', 'automatic-updater' ); ?></h3> 
     124                    <p><?php echo wp_kses( __( "It looks like you're running an SVN version of WordPress, that's cool! Automatic Updater can run <tt>svn up</tt> once an hour, to keep you up-to-date. For safety, enabling this option will disable the normal WordPress core updates.", 'automatic-updater' ), array( 'tt' => array() ) ); ?></p> 
     125 
     126                    <?php 
     127                    if ( !is_writable( ABSPATH . '/.svn' ) ) { 
     128                        $uid = posix_getuid(); 
     129                        $user = posix_getpwuid( $uid ); 
     130                        echo '<div class="automatic-updater-notice"><p>' . wp_kses( sprintf( __( "The .svn directory isn't writable, so <tt>svn up</tt> will probably fail when the web server runs it. You need to give the user <tt>%s</tt> write permissions to your entire WordPress install, including .svn directories.", 'automatic-updater' ), $user['name'] ), array( 'tt' => array() ) ) . '</p></div>'; 
     131                    } 
     132                    ?> 
     133 
     134                    <p><input type="checkbox" id="svn" name="svn" value="1"<?php echo $checked; ?>> <label for="svn"><?php echo wp_kses( __( 'Run <tt>svn up</tt> hourly?', 'automatic-updater' ), array( 'tt' => array() ) ); ?></label></p> 
     135 
     136                <?php 
     137                } else { 
     138                    echo '<input type="hidden" name="svn" value="0">'; 
     139                } 
     140 
     141                $checked = ''; 
     142                if ( $this->automatic_updater->get_option( 'debug' ) ) 
     143                    $checked = ' checked="checked"'; 
     144                ?> 
     145 
     146                <br> 
     147                <h3><?php esc_html_e( 'Debug Information', 'automatic-updater' ); ?></h3> 
     148                <p><input type="checkbox" id="debug" name="debug" value="1"<?php echo $checked; ?>> <label for="debug"><?php esc_html_e( 'Show debug information in the notification email.', 'automatic-updater' ); ?></label></p> 
     149                <p><input class="button button-primary" type="submit" name="submit" id="submit" value="<?php esc_attr_e( 'Save Changes', 'automatic-updater' ); ?>" /></p> 
     150            </form> 
     151        </div> 
     152    <?php 
     153    } 
     154 
     155    function save_settings() { 
     156        $types = array( 'core', 'plugins', 'themes' ); 
     157        $update = array(); 
     158        foreach ( $types as $type ) { 
     159            if ( ! empty( $_REQUEST[$type] ) ) 
     160                $update[$type] = true; 
     161            else 
     162                $update[$type] = false; 
     163        } 
     164        $this->automatic_updater->update_option( 'update', $update ); 
     165 
     166        $top_bool_options = array( 'debug', 'svn', 'disable-email' ); 
     167        foreach ( $top_bool_options as $option ) { 
     168            if ( ! empty( $_REQUEST[$option] ) ) 
     169                $this->automatic_updater->update_option( $option, true ); 
     170            else 
     171                $this->automatic_updater->update_option( $option, false ); 
     172        } 
     173 
     174        $top_options = array( 'override-email', 'retries-limit' ); 
     175        foreach ( $top_options as $option ) 
     176            $this->automatic_updater->update_option( $option, $_REQUEST[$option] ); 
     177    } 
     178 
     179    function plugin_row_links( $links ) { 
     180        $url = admin_url( 'options-general.php?page=automatic-updater' ); 
     181        if ( is_multisite() ) 
     182            $url = network_admin_url( 'settings.php?page=automatic-updater' ); 
     183     
     184        array_unshift( $links, "<a href='$url'>" . esc_html__( 'Settings', 'automatic-updater' ) . '</a>' ); 
     185     
     186        return $links; 
     187    } 
    152188} 
    153  
    154 function auto_updater_plugin_row_links( $links ) { 
    155     $url = admin_url( 'options-general.php?page=automatic-updater' ); 
    156     if ( is_multisite() ) 
    157         $url = network_admin_url( 'settings.php?page=automatic-updater' ); 
    158  
    159     array_unshift( $links, "<a href='$url'>" . esc_html__( 'Settings', 'automatic-updater' ) . '</a>' ); 
    160  
    161     return $links; 
    162 } 
    163 add_filter( 'plugin_action_links_' . AUTOMATIC_UPDATER_BASENAME, 'auto_updater_plugin_row_links' ); 
    164 add_filter( 'network_admin_plugin_action_links_' . AUTOMATIC_UPDATER_BASENAME, 'auto_updater_plugin_row_links' ); 
  • automatic-updater/trunk/automatic-updater.php

    r629847 r631863  
    1212 */ 
    1313 
    14 global $auto_updater_running; 
    15 $auto_updater_running = false; 
    16  
    1714$automatic_updater_file = __FILE__; 
    1815 
     
    2421    $automatic_updater_file = $network_plugin; 
    2522 
    26 define( 'AUTOMATIC_UPDATER_BASENAME', plugin_basename( $automatic_updater_file ) ); 
    27  
    28 function auto_updater_requires_wordpress_version() { 
    29     if ( version_compare( $GLOBALS['wp_version'], '3.4', '<' ) ) { 
    30         if ( is_plugin_active( AUTOMATIC_UPDATER_BASENAME ) ) { 
    31             deactivate_plugins( AUTOMATIC_UPDATER_BASENAME ); 
    32             add_action( 'admin_notices', 'auto_updater_disabled_notice' ); 
    33             if ( isset( $_GET['activate'] ) ) 
    34                 unset( $_GET['activate'] ); 
    35         } 
     23Automatic_Updater::$basename = plugin_basename( $automatic_updater_file ); 
     24 
     25class Automatic_Updater { 
     26    private $running = false; 
     27    private $options = NULL; 
     28 
     29    public static $basename; 
     30 
     31    function init() { 
     32        static $instance = false; 
     33 
     34        if( ! $instance ) 
     35            $instance = new Automatic_Updater; 
     36 
     37        return $instance; 
     38    } 
     39 
     40    function __construct() { 
     41        // Load the translations 
     42        load_plugin_textdomain( 'automatic-updater', false, dirname( self::$basename ) . '/languages/' ); 
     43 
     44        // Load the options, and check that they're all up to date. 
     45        $this->options = get_option( 'automatic-updater', array() ); 
     46        $this->plugin_upgrade(); 
     47 
     48        add_action( 'shutdown', array( &$this, 'shutdown' ) ); 
     49 
     50        add_action( 'auto_updater_core_event', array( &$this, 'update_core' ) ); 
     51        add_action( 'auto_updater_plugins_event', array( &$this, 'update_plugins' ) ); 
     52        add_action( 'auto_updater_themes_event', array( &$this, 'update_themes' ) ); 
     53        add_action( 'auto_updater_svn_event', array( &$this, 'update_svn' ) ); 
     54 
     55        // Nothing else matters if we're on WPMS and not on the main site 
     56        if ( is_multisite() && ! is_main_site() ) 
     57            return; 
     58 
     59        if ( is_admin() ) { 
     60            include_once( dirname( __FILE__ ) . '/admin.php' ); 
     61            Automatic_Updater_Admin::init( $this ); 
     62        } 
     63 
     64        add_action( 'admin_init', array( &$this, 'check_wordpress_version' ) ); 
     65 
     66        // Configure SVN updates cron, if it's enabled 
     67        if ( $this->options['svn'] ) { 
     68            if ( ! wp_next_scheduled( 'auto_updater_svn_event' ) ) 
     69                wp_schedule_event( time(), 'hourly', 'auto_updater_svn_event' ); 
     70        } else { 
     71            if ( $timestamp = wp_next_scheduled( 'auto_updater_svn_event' ) ) 
     72                wp_unschedule_event( $timestamp, 'auto_updater_svn_event' ); 
     73        } 
     74 
     75        // If the update check was one we called manually, don't get into a crazy recursive loop. 
     76        if ( $this->running ) 
     77            return; 
     78 
     79        $types = array( 'wordpress' => 'core', 'plugins' => 'plugins', 'themes' => 'themes' ); 
     80        if ( defined( 'DOING_CRON' ) && DOING_CRON ) { 
     81            // We're in a cron, do updates now 
     82            foreach ( $types as $type ) { 
     83                if ( ! empty( $this->options['update'][$type] ) ) { 
     84                    add_action( "set_site_transient_update_$type", array( &$this, "update_$type" ) ); 
     85                    add_action( "set_site_transient__site_transient_update_$type", array( &$this, "update_$type" ) ); 
     86                } 
     87            } 
     88        } else { 
     89            include_once( ABSPATH . 'wp-admin/includes/update.php' ); 
     90            $update_data = $this->get_update_data(); 
     91            // Not in a cron, schedule updates to happen in the next cron run 
     92            foreach ( $types as $internal => $type ) { 
     93                if ( ! empty( $this->options['update'][$type] ) && $update_data['counts'][$internal] > 0 ) { 
     94                    wp_schedule_single_event( time(), "auto_updater_{$type}_event" ); 
     95                } 
     96            } 
     97        } 
     98    } 
     99 
     100    function shutdown() { 
     101        update_option( 'automatic-updater', $this->options ); 
     102    } 
     103 
     104    function get_option( $name ) { 
     105        if ( array_key_exists( $name, $this->options ) ) 
     106            return $this->options[$name]; 
     107 
     108        return NULL; 
     109    } 
     110 
     111    function update_option( $name, $value ) { 
     112        if ( array_key_exists( $name, $this->options ) ) 
     113            return $this->options[$name] = $value; 
     114 
     115        return NULL; 
     116    } 
     117 
     118    function check_wordpress_version() { 
     119        if ( version_compare( $GLOBALS['wp_version'], '3.4', '<' ) ) { 
     120            if ( is_plugin_active( self::$basename ) ) { 
     121                deactivate_plugins( self::$basename ); 
     122                add_action( 'admin_notices', array( &$this, 'disabled_notice' ) ); 
     123                if ( isset( $_GET['activate'] ) ) 
     124                    unset( $_GET['activate'] ); 
     125            } 
     126        } 
     127    } 
     128 
     129    function disabled_notice() { 
     130        echo '<div class="updated"><p><strong>' . esc_html__( 'Automatic Updater requires WordPress 3.4 or higher! Please upgrade WordPress manually, then reactivate Automatic Updater.', 'automatic-updater' ) . '</strong></p></div>'; 
     131    } 
     132 
     133    function plugin_upgrade() { 
     134        if ( empty( $this->options ) ) { 
     135            $this->options = array( 
     136                        'update' => array( 
     137                                    'core' => true, 
     138                                    'plugins' => false, 
     139                                    'themes' => false, 
     140                                ), 
     141                        'retries-limit' => 3, 
     142                        'tries' => array( 
     143                                    'core' => array( 
     144                                                'version' => 0, 
     145                                                'tries' => 0, 
     146                                    ), 
     147                                    'plugins' => array(), 
     148                                    'themes' => array(), 
     149                                ), 
     150                        'svn' => false, 
     151                        'debug' => false, 
     152                        'next-development-update' => time(), 
     153                        'override-email' => '', 
     154                        'disable-email' => false, 
     155                    ); 
     156        } 
     157 
     158        // 'debug' option added in version 0.3 
     159        if ( ! array_key_exists( 'debug', $this->options ) ) 
     160            $this->options['debug'] = false; 
     161 
     162        // SVN updates added in version 0.5 
     163        if ( ! array_key_exists( 'svn', $this->options ) ) 
     164            $this->options['svn'] = false; 
     165 
     166        // Development version updates added in version 0.6 
     167        if ( ! array_key_exists( 'next-development-update', $this->options ) ) 
     168            $this->options['next-development-update'] = time(); 
     169 
     170        // Override contact email added in version 0.7 
     171        if ( ! array_key_exists( 'override-email', $this->options ) ) 
     172            $this->options['override-email'] = ''; 
     173 
     174        // Ability to disable email added in version 0.7 
     175        if ( ! array_key_exists( 'disable-email', $this->options ) ) 
     176            $this->options['disable-email'] = false; 
     177 
     178        // Ability to limit retries added in version 0.8 
     179        if ( ! array_key_exists( 'retries-limit', $this->options ) ) { 
     180            $this->options['retries-limit'] = 3; 
     181            $this->options['tries'] = array( 
     182                                    'core' => array( 
     183                                                'version' => 0, 
     184                                                'tries' => 0, 
     185                                    ), 
     186                                    'plugins' => array(), 
     187                                    'themes' => array(), 
     188                                ); 
     189        } 
     190    } 
     191 
     192    function update_core() { 
     193        if ( $this->running ) 
     194            return; 
     195 
     196        if ( $this->options['svn'] ) 
     197            return; 
     198 
     199        // Forgive me father, for I have sinned. I have included wp-admin files in a plugin. 
     200        include_once( ABSPATH . 'wp-admin/includes/update.php' ); 
     201        include_once( ABSPATH . 'wp-admin/includes/file.php' ); 
     202 
     203        include_once( dirname( __FILE__ ) . '/updater-skin.php' ); 
     204 
     205        $updates = get_core_updates(); 
     206        if ( empty( $updates ) ) 
     207            return; 
     208 
     209        if ( 'development' == $updates[0]->response ) 
     210            $update = $updates[0]; 
     211        else 
     212            $update = find_core_update( $updates[0]->current, $updates[0]->locale ); 
     213 
     214        $update = apply_filters( 'auto_updater_core_updates', $update ); 
     215        if ( empty( $update ) ) 
     216            return; 
     217 
     218        // Check that we haven't failed to upgrade to the updated version in the past 
     219        if ( version_compare( $update->current, $this->options['retries']['core']['version'], '>=' ) && $this->options['retries']['core']['tries'] >= $this->option['retries-limit'] ) 
     220            return; 
     221 
     222        $old_version = $GLOBALS['wp_version']; 
     223 
     224        // Sanity check that the new upgrade is actually an upgrade 
     225        if ( 'development' != $update->response && version_compare( $old_version, $update->current, '>=' ) ) 
     226            return; 
     227 
     228        // Only do development version updates once every 24 hours 
     229        if ( 'development' == $update->response ) { 
     230            if ( time() < $this->options['next-development-update'] ) 
     231                return; 
     232 
     233            $this->options['next-development-update'] = strtotime( '+24 hours' ); 
     234        } 
     235 
     236        $this->running = true; 
     237 
     238        do_action( 'auto_updater_before_update', 'core' ); 
     239 
     240        $upgrade_failed = false; 
     241 
     242        $skin = new Auto_Updater_Skin(); 
     243        $upgrader = new Core_Upgrader( $skin ); 
     244        $result = $upgrader->upgrade( $update ); 
     245 
     246        do_action( 'auto_updater_after_update', 'core' ); 
     247 
     248        if ( is_wp_error( $result ) ) { 
     249            if ( $this->options['tries']['core']['version'] != $update->current ) 
     250                $this->options['tries']['core']['version'] = $update->current; 
     251 
     252            $this->options['tries']['core']['tries']++; 
     253 
     254            $upgrade_failed = true; 
     255 
     256            $message = esc_html__( "While trying to upgrade WordPress, we ran into the following error:", 'automatic-updater' ); 
     257            $message .= '<br><br>' . $result->get_error_message() . '<br><br>'; 
     258            $message .= sprintf( esc_html__( 'We\'re sorry it didn\'t work out. Please try upgrading manually, instead. This is attempt %1$d of %2$d.', 'automatic-updater' ), 
     259                            $this->options['tries']['core']['tries'], 
     260                            $this->option['retries-limit'] ); 
     261        } else if( 'development' == $update->response ) { 
     262            $message = esc_html__( "We've successfully upgraded WordPress to the latest nightly build!", 'automatic-updater' ); 
     263            $message .= '<br><br>' . esc_html__( 'Have fun!', 'automatic-updater' ); 
     264 
     265            $this->options['tries']['core']['version'] = 0; 
     266            $this->options['tries']['core']['tries'] = 0; 
     267        } else { 
     268            $message = sprintf( esc_html__( 'We\'ve successfully upgraded WordPress from version %1$s to version %2$s!', 'automatic-updater' ), $old_version, $update->current ); 
     269            $message .= '<br><br>' . esc_html__( 'Have fun!', 'automatic-updater' ); 
     270 
     271            $this->options['tries']['core']['version'] = 0; 
     272            $this->options['tries']['core']['tries'] = 0; 
     273        } 
     274 
     275        $message .= '<br>'; 
     276 
     277        $debug = join( '<br>', $skin->messages ); 
     278 
     279        $this->notification( $message, $debug, $upgrade_failed ); 
     280 
     281        wp_version_check(); 
     282    } 
     283 
     284    function update_plugins() { 
     285        if ( $this->running ) 
     286            return; 
     287 
     288        include_once( ABSPATH . 'wp-admin/includes/update.php' ); 
     289        include_once( ABSPATH . 'wp-admin/includes/plugin.php' ); 
     290        include_once( ABSPATH . 'wp-admin/includes/file.php' ); 
     291 
     292        include_once( dirname( __FILE__ ) . '/updater-skin.php' ); 
     293 
     294        $plugins = apply_filters( 'auto_updater_plugin_updates', get_plugin_updates() ); 
     295 
     296        foreach ( $plugins as $id => $plugin ) { 
     297            // Remove any plugins from the list that may've already been updated 
     298            if ( version_compare( $plugin->Version, $plugin->update->new_version, '>=' ) ) 
     299                unset( $plugins[$id] ); 
     300 
     301            // Remove any plugins that have failed to upgrade 
     302            if ( ! empty( $this->options['retries']['plugins'][$id] ) ) { 
     303                // If there's a new version of a failed plugin, we should give it another go. 
     304                if ( $this->options['retries']['plugins'][$id]['version'] != $plugin->update->new_version ) 
     305                    unset( $this->options['retries']['plugins'][$id] ); 
     306                // If the plugin has already had it's chance, move on. 
     307                else if ($this->options['retries']['plugins'][$id]['tries'] > $this->options['retries-limit'] ) 
     308                    unset( $plugins[$id] ); 
     309            } 
     310        } 
     311 
     312        if ( empty( $plugins ) ) 
     313            return; 
     314 
     315        $this->running = true; 
     316 
     317        do_action( 'auto_updater_before_update', 'plugins' ); 
     318 
     319        $skin = new Auto_Updater_Skin(); 
     320        $upgrader = new Plugin_Upgrader( $skin ); 
     321        $result = $upgrader->bulk_upgrade( array_keys( $plugins ) ); 
     322 
     323        do_action( 'auto_updater_after_update', 'plugins' ); 
     324 
     325        $message = esc_html( _n( 'We found a plugin upgrade!', 'We found upgrades for some plugins!', count( $plugins ), 'automatic-updater' ) ); 
     326        $message .= '<br><br>'; 
     327 
     328        $upgrade_failed = false; 
     329 
     330        foreach ( $plugins as $id => $plugin ) { 
     331            if ( is_wp_error( $result[$id] ) ) { 
     332                if ( empty( $this->options['retries']['plugins'][$id] ) ) 
     333                    $this->options['retries']['plugins'][$id] = array( 
     334                                                            'tries' => 1, 
     335                                                            'version' => $plugin->update->new_version, 
     336                                                        ); 
     337                else 
     338                    $this->options['retries']['plugins'][$id]['tries']++; 
     339 
     340                $upgrade_failed = true; 
     341 
     342                /* translators: First argument is the plugin url, second argument is the Plugin name, third argument is the error encountered while upgrading. The fourth and fifth arguments refer to how many retries we've had at installing this plugin. */ 
     343                $message .= wp_kses( sprintf( __( '<a href="%1$s">%2$s</a>: We encounted an error upgrading this plugin: %3$s (Attempt %4$d of %5$d)', 'automatic-updater' ), 
     344                                            $plugin->update->url, 
     345                                            $plugin->Name, 
     346                                            $result[$id]->get_error_message(), 
     347                                            $this->options['retries']['plugins'][$id]['tries'], 
     348                                            $this->options['retries-limit'] ), 
     349                                    array( 'a' => array( 'href' => array() ) ) ); 
     350            } else { 
     351                /* translators: First argument is the plugin url, second argument is the Plugin name, third argument is the old version number, fourth argument is the new version number */ 
     352                $message .= wp_kses( sprintf( __( '<a href="%1$s">%2$s</a>: Successfully upgraded from version %3$s to %4$s!', 'automatic-updater' ), 
     353                                            $plugin->update->url, 
     354                                            $plugin->Name, 
     355                                            $plugin->Version, 
     356                                            $plugin->update->new_version ), array( 'a' => array( 'href' => array() ) ) ); 
     357 
     358                if ( ! empty( $this->options['retries']['plugins'][$id] ) ) 
     359                    unset( $this->options['retries']['plugins'][$id] ); 
     360            } 
     361 
     362            $message .= '<br>'; 
     363        } 
     364 
     365        $message .= '<br>' . esc_html__( 'Plugin authors depend on your feedback to make their plugins better, and the WordPress community depends on plugin ratings for checking the quality of a plugin. If you have a couple of minutes, click on the plugin names above, and leave a Compatibility Vote or a Rating!', 'automatic-updater' ) . '<br>'; 
     366 
     367        $debug = join( '<br>', $skin->messages ); 
     368 
     369        $this->notification( $message, $debug, $upgrade_failed ); 
     370 
     371        wp_update_plugins(); 
     372    } 
     373 
     374    function update_themes() { 
     375        if ( $this->running ) 
     376            return; 
     377 
     378        include_once( ABSPATH . 'wp-admin/includes/update.php' ); 
     379        include_once( ABSPATH . 'wp-admin/includes/file.php' ); 
     380 
     381        include_once( dirname( __FILE__ ) . '/updater-skin.php' ); 
     382 
     383        $themes = apply_filters( 'auto_updater_theme_updates', get_theme_updates() ); 
     384 
     385        foreach ( $themes as $id => $theme ) { 
     386            // Remove any themes from the list that may've already been updated 
     387            if ( version_compare( $theme->Version, $theme->update['new_version'], '>=' ) ) 
     388                unset( $themes[$id] ); 
     389 
     390            // Remove any themes that have failed to upgrade 
     391            if ( ! empty( $this->options['retries']['themes'][$id] ) ) { 
     392                // If there's a new version of a failed theme, we should give it another go. 
     393                if ( $this->options['retries']['themes'][$id]['version'] != $theme->update['new_version'] ) 
     394                    unset( $this->options['retries']['themes'][$id] ); 
     395                // If the themes has already had it's chance, move on. 
     396                else if ($this->options['retries']['themes'][$id]['tries'] > $this->options['retries-limit'] ) 
     397                    unset( $themes[$id] ); 
     398            } 
     399        } 
     400 
     401        if ( empty( $themes ) ) 
     402            return; 
     403 
     404    $this->running = true; 
     405 
     406        do_action( 'auto_updater_before_update', 'themes' ); 
     407 
     408        $skin = new Auto_Updater_Skin(); 
     409        $upgrader = new Theme_Upgrader( $skin ); 
     410        $result = $upgrader->bulk_upgrade( array_keys( $themes ) ); 
     411 
     412        do_action( 'auto_updater_after_update', 'themes' ); 
     413 
     414        $message = esc_html( _n( 'We found a theme upgrade!', 'We found upgrades for some themes!', count( $themes ), 'automatic-updater' ) ); 
     415        $message .= '<br><br>'; 
     416 
     417        $upgrade_failed = false; 
     418 
     419        foreach ( $themes as $id => $theme ) { 
     420            if ( is_wp_error( $result[$id] ) ) { 
     421                if ( empty( $this->options['retries']['themes'][$id] ) ) 
     422                    $this->options['retries']['themes'][$id] = array( 
     423                                                            'tries' => 1, 
     424                                                            'version' => $themes->update['new_version'], 
     425                                                        ); 
     426                else 
     427                    $this->options['retries']['themes'][$id]['tries']++; 
     428 
     429                $upgrade_failed = true; 
     430 
     431                /* translators: First argument is the theme URL, second argument is the Theme name, third argument is the error encountered while upgrading. The fourth and fifth arguments refer to how many retries we've had at installing this theme. */ 
     432                $message .= wp_kses( sprintf( __( '<a href="%1$s">%2$s</a>: We encounted an error upgrading this theme: %3$s (Attempt %4$d of %5$d)', 'automatic-updater' ), 
     433                                            $theme->update['url'], 
     434                                            $theme->name, 
     435                                            $result[$id]->get_error_message(), 
     436                                            $this->options['retries']['plugins'][$id]['tries'], 
     437                                            $this->options['retries-limit'] ), 
     438                                    array( 'a' => array( 'href' => array() ) ) ); 
     439            } else { 
     440                /* translators: First argument is the theme URL, second argument is the Theme name, third argument is the old version number, fourth argument is the new version number */ 
     441                $message .= wp_kses( sprintf( __( '<a href="%1$s">%2$s</a>: Successfully upgraded from version %3$s to %4$s!', 'automatic-updater' ), 
     442                                            $theme->update['url'], 
     443                                            $theme->name, 
     444                                            $theme->version, 
     445                                            $theme->update['new_version'] ), array( 'a' => array( 'href' => array() ) ) ); 
     446 
     447                if ( ! empty( $this->options['retries']['themes'][$id] ) ) 
     448                    unset( $this->options['retries']['themes'][$id] ); 
     449            } 
     450 
     451            $message .= '<br>'; 
     452        } 
     453 
     454        $message .= '<br>' . esc_html__( 'Theme authors depend on your feedback to make their plugins better, and the WordPress community depends on theme ratings for checking the quality of a theme. If you have a couple of minutes, click on the theme names above, and leave a Compatibility Vote or a Rating!', 'automatic-updater' ) . '<br>'; 
     455 
     456        $debug = join( '<br>', $skin->messages ); 
     457 
     458        $this->notification( $message, $debug, $upgrade_failed ); 
     459 
     460        wp_update_themes(); 
     461    } 
     462 
     463    function update_svn() { 
     464        $output = array(); 
     465        $return = NULL; 
     466 
     467        exec( 'svn up ' . ABSPATH, $output, $return ); 
     468 
     469        if ( 0 === $return ) { 
     470            $update = end( $output ); 
     471            // No need to email if there was no update. 
     472            if ( 0 === strpos( $update, "At revision" ) ) 
     473                return; 
     474 
     475            $message = esc_html__( 'We successfully upgraded from SVN!', 'automatic-updater' ); 
     476            $message .= "<br><br>$update"; 
     477        } else { 
     478            $message = esc_html__( 'While upgrading from SVN, we ran into the following error:', 'automatic-updater' ); 
     479            $message .= '<br><br>' . end( $output ) . '<br><br>'; 
     480            $message .= esc_html__( "We're sorry it didn't work out. Please try upgrading manually, instead.", 'automatic-updater' ); 
     481        } 
     482 
     483        $message .= '<br>'; 
     484 
     485        $debug = join( '<br>', $output ); 
     486 
     487        $this->notification( $message, $debug ); 
     488    } 
     489 
     490    function notification( $info = '', $debug = '', $upgrade_failed = false ) { 
     491    echo 1; 
     492        if ( $this->options['disable-email'] ) 
     493            return; 
     494echo 2; 
     495        $site = get_home_url(); 
     496        $subject = sprintf( esc_html__( 'WordPress Update: %s', 'automatic-updater' ), $site ); 
     497 
     498        $message = '<html>'; 
     499        $message .= '<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>'; 
     500        $message .= '<body>'; 
     501 
     502        $message .= esc_html__( 'Howdy!', 'automatic-updater' ); 
     503        $message .= '<br><br>'; 
     504        $message .= wp_kses( sprintf( __( 'Automatic Updater just ran on your site, <a href="%1$s">%1$s</a>, with the following result:', 'automatic-updater' ), $site ), array( 'a' => array( 'href' => array() ) ) ); 
     505        $message .= '<br><br>'; 
     506 
     507        $message .= $info; 
     508 
     509        $message .= '<br>'; 
     510 
     511        if ( $upgrade_failed ) { 
     512            $message .= esc_html__( 'It looks like something went wrong during the update. Note that, if Automatic Updater continues to encounter problems, it will stop trying to do this update, and will not try again until after you manually update.', 'automatic-updater' ); 
     513            $message .= '<br><br>'; 
     514        } 
     515 
     516        $message .= esc_html__( 'Thanks for using the Automatic Updater plugin!', 'automatic-updater' ); 
     517 
     518        if ( ! empty( $this->options['debug'] ) ) { 
     519            $message .= "<br><br><br>"; 
     520            $message .= esc_html__( 'Debug Information:', 'automatic-updater' ); 
     521            $message .= "<br><br>$debug"; 
     522        } 
     523 
     524        $message .= '</body></html>'; 
     525 
     526        $email = get_option( 'admin_email' ); 
     527        if ( ! empty( $this->options['override-email'] ) ) 
     528            $email = $this->options['override-email']; 
     529 
     530        $email = apply_filters( 'auto_updater_notification_email_address', $email ); 
     531 
     532        $headers = array( 
     533                        'MIME-Version: 1.0', 
     534                        'Content-Type: text/html; charset=UTF-8' 
     535                    ); 
     536echo $message; 
     537        add_filter( 'wp_mail_content_type', array( &$this, 'wp_mail_content_type' ) ); 
     538        wp_mail( $email, $subject, $message, $headers ); 
     539        remove_filter( 'wp_mail_content_type', array( &$this, 'wp_mail_content_type' ) ); 
     540    } 
     541 
     542    function wp_mail_content_type() { 
     543        return 'text/html'; 
     544    } 
     545 
     546    function get_update_data() { 
     547        $counts = array( 'plugins' => 0, 'themes' => 0, 'wordpress' => 0 ); 
     548 
     549        $update_plugins = get_site_transient( 'update_plugins' ); 
     550        if ( ! empty( $update_plugins->response ) ) 
     551            $counts['plugins'] = count( $update_plugins->response ); 
     552 
     553        $update_themes = get_site_transient( 'update_themes' ); 
     554        if ( ! empty( $update_themes->response ) ) 
     555            $counts['themes'] = count( $update_themes->response ); 
     556 
     557        if ( function_exists( 'get_core_updates' ) ) { 
     558            $update_wordpress = get_core_updates( array( 'dismissed' => false ) ); 
     559            if ( ! empty( $update_wordpress ) && 'latest' != $update_wordpress[0]->response ) 
     560                $counts['wordpress'] = 1; 
     561        } 
     562 
     563        $counts['total'] = $counts['plugins'] + $counts['themes'] + $counts['wordpress']; 
     564 
     565        return apply_filters( 'auto_update_get_update_data', array( 'counts' => $counts ) ); 
    36566    } 
    37567} 
    38 add_action( 'admin_init', 'auto_updater_requires_wordpress_version' ); 
    39  
    40 function auto_updater_disabled_notice() { 
    41     echo '<div class="updated"><p><strong>' . esc_html__( 'Automatic Updater requires WordPress 3.4 or higher! Please upgrade WordPress manually, then reactivate Automatic Updater.', 'automatic-updater' ) . '</strong></p></div>'; 
    42 } 
    43  
    44 function auto_updater_init() { 
    45     if ( is_multisite() && ! is_main_site() ) 
    46         return; 
    47  
    48     if ( is_admin() ) 
    49         include_once( dirname( __FILE__ ) . '/admin.php' ); 
    50  
    51     $options = get_option( 'automatic-updater', array() ); 
    52  
    53     if ( empty( $options ) ) { 
    54         $options = array( 
    55                     'update' => array( 
    56                                 'core' => true, 
    57                                 'plugins' => false, 
    58                                 'themes' => false, 
    59                             ), 
    60                     'retries-limit' => 3, 
    61                     'tries' => array( 
    62                                 'core' => array( 
    63                                             'version' => 0, 
    64                                             'tries' => 0, 
    65                                 ), 
    66                                 'plugins' => array(), 
    67                                 'themes' => array(), 
    68                             ), 
    69                     'svn' => false, 
    70                     'debug' => false, 
    71                     'next-development-update' => time(), 
    72                     'override-email' => '', 
    73                     'disable-email' => false, 
    74                 ); 
    75         update_option( 'automatic-updater', $options ); 
    76     } 
    77  
    78     // 'debug' option added in version 0.3 
    79     if ( ! array_key_exists( 'debug', $options ) ) { 
    80         $options['debug'] = false; 
    81         update_option( 'automatic-updater', $options ); 
    82     } 
    83  
    84     // SVN updates added in version 0.5 
    85     if ( ! array_key_exists( 'svn', $options ) ) { 
    86         $options['svn'] = false; 
    87         update_option( 'automatic-updater', $options ); 
    88     } 
    89  
    90     // Development version updates added in version 0.6 
    91     if ( ! array_key_exists( 'next-development-update', $options ) ) { 
    92         $options['next-development-update'] = time(); 
    93         update_option( 'automatic-updater', $options ); 
    94     } 
    95  
    96     // Override contact email added in version 0.7 
    97     if ( ! array_key_exists( 'override-email', $options ) ) { 
    98         $options['override-email'] = ''; 
    99         update_option( 'automatic-updater', $options ); 
    100     } 
    101  
    102     // Ability to disable email added in version 0.7 
    103     if ( ! array_key_exists( 'disable-email', $options ) ) { 
    104         $options['disable-email'] = false; 
    105         update_option( 'automatic-updater', $options ); 
    106     } 
    107  
    108     // Ability to limit retries added in version 0.8 
    109     if ( ! array_key_exists( 'retries-limit', $options ) ) { 
    110         $options['retries-limit'] = 3; 
    111         $option['tries'] = array( 
    112                                 'core' => array( 
    113                                             'version' => 0, 
    114                                             'tries' => 0, 
    115                                 ), 
    116                                 'plugins' => array(), 
    117                                 'themes' => array(), 
    118                             ); 
    119         update_option( 'automatic-updater', $options ); 
    120     } 
    121  
    122     // Configure SVN updates cron, if it's enabled 
    123     if ( $options['svn'] ) { 
    124         if ( ! wp_next_scheduled( 'auto_updater_svn_event' ) ) 
    125             wp_schedule_event( time(), 'hourly', 'auto_updater_svn_event' ); 
    126     } 
    127     else { 
    128         if ( $timestamp = wp_next_scheduled( 'auto_updater_svn_event' ) ) 
    129             wp_unschedule_event( $timestamp, 'auto_updater_svn_event' ); 
    130     } 
    131  
    132     // Load the translations 
    133     load_plugin_textdomain( 'automatic-updater', false, dirname( AUTOMATIC_UPDATER_BASENAME ) . '/languages/' ); 
    134  
    135     global $auto_updater_running; 
    136     // If the update check was one we called manually, don't get into a crazy recursive loop. 
    137     if ( $auto_updater_running ) 
    138         return; 
    139  
    140     $types = array( 'wordpress' => 'core', 'plugins' => 'plugins', 'themes' => 'themes' ); 
    141     if ( defined( 'DOING_CRON' ) && DOING_CRON ) { 
    142         // We're in a cron, do updates now 
    143         foreach ( $types as $type ) { 
    144             if ( ! empty( $options['update'][$type] ) ) { 
    145                 add_action( "set_site_transient_update_$type", "auto_updater_$type" ); 
    146                 add_action( "set_site_transient__site_transient_update_$type", "auto_updater_$type" ); 
    147             } 
    148         } 
    149     } 
    150     else { 
    151         include_once( ABSPATH . 'wp-admin/includes/update.php' ); 
    152         $update_data = auto_updater_get_update_data(); 
    153         // Not in a cron, schedule updates to happen in the next cron run 
    154         foreach ( $types as $internal => $type ) { 
    155             if ( ! empty( $options['update'][$type] ) && $update_data['counts'][$internal] > 0 ) { 
    156                 wp_schedule_single_event( time(), "auto_updater_{$type}_event" ); 
    157             } 
    158         } 
    159     } 
    160 } 
    161 add_action( 'init', 'auto_updater_init' ); 
    162  
    163 function auto_updater_core() { 
    164     global $auto_updater_running; 
    165     if ( $auto_updater_running ) 
    166         return; 
    167  
    168     $options = get_option( 'automatic-updater' ); 
    169     if ( $options['svn'] ) 
    170         return; 
    171  
    172     // Forgive me father, for I have sinned. I have included wp-admin files in a plugin. 
    173     include_once( ABSPATH . 'wp-admin/includes/update.php' ); 
    174     include_once( ABSPATH . 'wp-admin/includes/file.php' ); 
    175  
    176     include_once( dirname( __FILE__ ) . '/updater-skin.php' ); 
    177  
    178     $updates = get_core_updates(); 
    179     if ( empty( $updates ) ) 
    180         return; 
    181  
    182     if ( 'development' == $updates[0]->response ) 
    183         $update = $updates[0]; 
    184     else 
    185         $update = find_core_update( $updates[0]->current, $updates[0]->locale ); 
    186  
    187     $update = apply_filters( 'auto_updater_core_updates', $update ); 
    188     if ( empty( $update ) ) 
    189         return; 
    190  
    191     // Check that we haven't failed to upgrade to the updated version in the past 
    192     if ( version_compare( $update->current, $options['retries']['core']['version'], '>=' ) && $options['retries']['core']['tries'] >= $option['retries-limit'] ) 
    193         return; 
    194  
    195     $old_version = $GLOBALS['wp_version']; 
    196  
    197     // Sanity check that the new upgrade is actually an upgrade 
    198     if ( 'development' != $update->response && version_compare( $old_version, $update->current, '>=' ) ) 
    199         return; 
    200  
    201     // Only do development version updates once every 24 hours 
    202     if ( 'development' == $update->response ) { 
    203         if ( time() < $options['next-development-update'] ) 
    204             return; 
    205  
    206         $options['next-development-update'] = strtotime( '+24 hours' ); 
    207     } 
    208  
    209     $auto_updater_running = true; 
    210  
    211     do_action( 'auto_updater_before_update', 'core' ); 
    212  
    213     $upgrade_failed = false; 
    214  
    215     $skin = new Auto_Updater_Skin(); 
    216     $upgrader = new Core_Upgrader( $skin ); 
    217     $result = $upgrader->upgrade( $update ); 
    218  
    219     do_action( 'auto_updater_after_update', 'core' ); 
    220  
    221     if ( is_wp_error( $result ) ) { 
    222         if ( $options['tries']['core']['version'] != $update->current ) 
    223             $options['tries']['core']['version'] = $update->current; 
    224  
    225         $options['tries']['core']['tries']++; 
    226  
    227         $upgrade_failed = true; 
    228  
    229         $message = esc_html__( "While trying to upgrade WordPress, we ran into the following error:", 'automatic-updater' ); 
    230         $message .= '<br><br>' . $result->get_error_message() . '<br><br>'; 
    231         $message .= sprintf( esc_html__( 'We\'re sorry it didn\'t work out. Please try upgrading manually, instead. This is attempt %1$d of %2$d.', 'automatic-updater' ), 
    232                         $options['tries']['core']['tries'], 
    233                         $option['retries-limit'] ); 
    234     } 
    235     else if( 'development' == $update->response ) { 
    236         $message = esc_html__( "We've successfully upgraded WordPress to the latest nightly build!", 'automatic-updater' ); 
    237         $message .= '<br><br>' . esc_html__( 'Have fun!', 'automatic-updater' ); 
    238  
    239         $options['tries']['core']['version'] = 0; 
    240         $options['tries']['core']['tries'] = 0; 
    241     } 
    242     else { 
    243         $message = sprintf( esc_html__( 'We\'ve successfully upgraded WordPress from version %1$s to version %2$s!', 'automatic-updater' ), $old_version, $update->current ); 
    244         $message .= '<br><br>' . esc_html__( 'Have fun!', 'automatic-updater' ); 
    245  
    246         $options['tries']['core']['version'] = 0; 
    247         $options['tries']['core']['tries'] = 0; 
    248     } 
    249  
    250     $message .= '<br>'; 
    251  
    252     $debug = join( '<br>', $skin->messages ); 
    253  
    254     update_option( 'automatic-updater', $options ); 
    255  
    256     auto_updater_notification( $message, $debug, $upgrade_failed ); 
    257  
    258     wp_version_check(); 
    259 } 
    260 add_action( 'auto_updater_core_event', 'auto_updater_core' ); 
    261  
    262 function auto_updater_plugins() { 
    263     global $auto_updater_running; 
    264     if ( $auto_updater_running ) 
    265         return; 
    266  
    267     include_once( ABSPATH . 'wp-admin/includes/update.php' ); 
    268     include_once( ABSPATH . 'wp-admin/includes/plugin.php' ); 
    269     include_once( ABSPATH . 'wp-admin/includes/file.php' ); 
    270  
    271     include_once( dirname( __FILE__ ) . '/updater-skin.php' ); 
    272  
    273     $options = get_option( 'automatic-updater' ); 
    274  
    275     $plugins = apply_filters( 'auto_updater_plugin_updates', get_plugin_updates() ); 
    276  
    277     foreach ( $plugins as $id => $plugin ) { 
    278         // Remove any plugins from the list that may've already been updated 
    279         if ( version_compare( $plugin->Version, $plugin->update->new_version, '>=' ) ) 
    280             unset( $plugins[$id] ); 
    281  
    282         // Remove any plugins that have failed to upgrade 
    283         if ( ! empty( $options['retries']['plugins'][$id] ) ) { 
    284             // If there's a new version of a failed plugin, we should give it another go. 
    285             if ( $options['retries']['plugins'][$id]['version'] != $plugin->update->new_version ) 
    286                 unset( $options['retries']['plugins'][$id] ); 
    287             // If the plugin has already had it's chance, move on. 
    288             else if ($options['retries']['plugins'][$id]['tries'] > $options['retries-limit'] ) 
    289                 unset( $plugins[$id] ); 
    290         } 
    291     } 
    292  
    293     if ( empty( $plugins ) ) 
    294         return; 
    295  
    296     $auto_updater_running = true; 
    297  
    298     do_action( 'auto_updater_before_update', 'plugins' ); 
    299  
    300     $skin = new Auto_Updater_Skin(); 
    301     $upgrader = new Plugin_Upgrader( $skin ); 
    302     $result = $upgrader->bulk_upgrade( array_keys( $plugins ) ); 
    303  
    304     do_action( 'auto_updater_after_update', 'plugins' ); 
    305  
    306     $message = esc_html( _n( 'We found a plugin upgrade!', 'We found upgrades for some plugins!', count( $plugins ), 'automatic-updater' ) ); 
    307     $message .= '<br><br>'; 
    308  
    309     $upgrade_failed = false; 
    310  
    311     foreach ( $plugins as $id => $plugin ) { 
    312         if ( is_wp_error( $result[$id] ) ) { 
    313             if ( empty( $options['retries']['plugins'][$id] ) ) 
    314                 $options['retries']['plugins'][$id] = array( 
    315                                                         'tries' => 1, 
    316                                                         'version' => $plugin->update->new_version, 
    317                                                     ); 
    318             else 
    319                 $options['retries']['plugins'][$id]['tries']++; 
    320  
    321             $upgrade_failed = true; 
    322  
    323             /* translators: First argument is the plugin url, second argument is the Plugin name, third argument is the error encountered while upgrading. The fourth and fifth arguments refer to how many retries we've had at installing this plugin. */ 
    324             $message .= wp_kses( sprintf( __( '<a href="%1$s">%2$s</a>: We encounted an error upgrading this plugin: %3$s (Attempt %4$d of %5$d)', 'automatic-updater' ), 
    325                                         $plugin->update->url, 
    326                                         $plugin->Name, 
    327                                         $result[$id]->get_error_message(), 
    328                                         $options['retries']['plugins'][$id]['tries'], 
    329                                         $options['retries-limit'] ), 
    330                                 array( 'a' => array( 'href' => array() ) ) ); 
    331         } 
    332         else { 
    333             /* translators: First argument is the plugin url, second argument is the Plugin name, third argument is the old version number, fourth argument is the new version number */ 
    334             $message .= wp_kses( sprintf( __( '<a href="%1$s">%2$s</a>: Successfully upgraded from version %3$s to %4$s!', 'automatic-updater' ), 
    335                                         $plugin->update->url, 
    336                                         $plugin->Name, 
    337                                         $plugin->Version, 
    338                                         $plugin->update->new_version ), array( 'a' => array( 'href' => array() ) ) ); 
    339  
    340             if ( ! empty( $options['retries']['plugins'][$id] ) ) 
    341                 unset( $options['retries']['plugins'][$id] ); 
    342         } 
    343  
    344         $message .= '<br>'; 
    345     } 
    346  
    347     $message .= '<br>' . esc_html__( 'Plugin authors depend on your feedback to make their plugins better, and the WordPress community depends on plugin ratings for checking the quality of a plugin. If you have a couple of minutes, click on the plugin names above, and leave a Compatibility Vote or a Rating!', 'automatic-updater' ) . '<br>'; 
    348  
    349     $debug = join( '<br>', $skin->messages ); 
    350  
    351     update_option( 'automatic-updater', $options ); 
    352  
    353     auto_updater_notification( $message, $debug, $upgrade_failed ); 
    354  
    355     wp_update_plugins(); 
    356 } 
    357 add_action( 'auto_updater_plugins_event', 'auto_updater_plugins' ); 
    358  
    359 function auto_updater_themes() { 
    360     global $auto_updater_running; 
    361     if ( $auto_updater_running ) 
    362         return; 
    363  
    364     include_once( ABSPATH . 'wp-admin/includes/update.php' ); 
    365     include_once( ABSPATH . 'wp-admin/includes/file.php' ); 
    366  
    367     include_once( dirname( __FILE__ ) . '/updater-skin.php' ); 
    368  
    369     $options = get_option( 'automatic-updater' ); 
    370  
    371     $themes = apply_filters( 'auto_updater_theme_updates', get_theme_updates() ); 
    372  
    373     foreach ( $themes as $id => $theme ) { 
    374         // Remove any themes from the list that may've already been updated 
    375         if ( version_compare( $theme->Version, $theme->update['new_version'], '>=' ) ) 
    376             unset( $themes[$id] ); 
    377  
    378         // Remove any themes that have failed to upgrade 
    379         if ( ! empty( $options['retries']['themes'][$id] ) ) { 
    380             // If there's a new version of a failed theme, we should give it another go. 
    381             if ( $options['retries']['themes'][$id]['version'] != $theme->update['new_version'] ) 
    382                 unset( $options['retries']['themes'][$id] ); 
    383             // If the themes has already had it's chance, move on. 
    384             else if ($options['retries']['themes'][$id]['tries'] > $options['retries-limit'] ) 
    385                 unset( $themes[$id] ); 
    386         } 
    387     } 
    388  
    389     if ( empty( $themes ) ) 
    390         return; 
    391  
    392     $auto_updater_running = true; 
    393  
    394     do_action( 'auto_updater_before_update', 'themes' ); 
    395  
    396     $skin = new Auto_Updater_Skin(); 
    397     $upgrader = new Theme_Upgrader( $skin ); 
    398     $result = $upgrader->bulk_upgrade( array_keys( $themes ) ); 
    399  
    400     do_action( 'auto_updater_after_update', 'themes' ); 
    401  
    402     $message = esc_html( _n( 'We found a theme upgrade!', 'We found upgrades for some themes!', count( $themes ), 'automatic-updater' ) ); 
    403     $message .= '<br><br>'; 
    404  
    405     $upgrade_failed = false; 
    406  
    407     foreach ( $themes as $id => $theme ) { 
    408         if ( is_wp_error( $result[$id] ) ) { 
    409             if ( empty( $options['retries']['themes'][$id] ) ) 
    410                 $options['retries']['themes'][$id] = array( 
    411                                                         'tries' => 1, 
    412                                                         'version' => $themes->update['new_version'], 
    413                                                     ); 
    414             else 
    415                 $options['retries']['themes'][$id]['tries']++; 
    416  
    417             $upgrade_failed = true; 
    418  
    419             /* translators: First argument is the theme URL, second argument is the Theme name, third argument is the error encountered while upgrading. The fourth and fifth arguments refer to how many retries we've had at installing this theme. */ 
    420             $message .= wp_kses( sprintf( __( '<a href="%1$s">%2$s</a>: We encounted an error upgrading this theme: %3$s (Attempt %4$d of %5$d)', 'automatic-updater' ), 
    421                                         $theme->update['url'], 
    422                                         $theme->name, 
    423                                         $result[$id]->get_error_message(), 
    424                                         $options['retries']['plugins'][$id]['tries'], 
    425                                         $options['retries-limit'] ), 
    426                                 array( 'a' => array( 'href' => array() ) ) ); 
    427         } 
    428         else { 
    429             /* translators: First argument is the theme URL, second argument is the Theme name, third argument is the old version number, fourth argument is the new version number */ 
    430             $message .= wp_kses( sprintf( __( '<a href="%1$s">%2$s</a>: Successfully upgraded from version %3$s to %4$s!', 'automatic-updater' ), 
    431                                         $theme->update['url'], 
    432                                         $theme->name, 
    433                                         $theme->version, 
    434                                         $theme->update['new_version'] ), array( 'a' => array( 'href' => array() ) ) ); 
    435  
    436             if ( ! empty( $options['retries']['themes'][$id] ) ) 
    437                 unset( $options['retries']['themes'][$id] ); 
    438         } 
    439  
    440         $message .= '<br>'; 
    441     } 
    442  
    443     $message .= '<br>' . esc_html__( 'Theme authors depend on your feedback to make their plugins better, and the WordPress community depends on theme ratings for checking the quality of a theme. If you have a couple of minutes, click on the theme names above, and leave a Compatibility Vote or a Rating!', 'automatic-updater' ) . '<br>'; 
    444  
    445     $debug = join( '<br>', $skin->messages ); 
    446  
    447     update_option( 'automatic-updater', $options ); 
    448  
    449     auto_updater_notification( $message, $debug, $upgrade_failed ); 
    450  
    451     wp_update_themes(); 
    452 } 
    453 add_action( 'auto_updater_themes_event', 'auto_updater_themes' ); 
    454  
    455 function auto_updater_svn() { 
    456     $output = array(); 
    457     $return = NULL; 
    458  
    459     exec( 'svn up ' . ABSPATH, $output, $return ); 
    460  
    461     if ( 0 === $return ) { 
    462         $update = end( $output ); 
    463         // No need to email if there was no update. 
    464         if ( 0 === strpos( $update, "At revision" ) ) 
    465             return; 
    466  
    467         $message = esc_html__( 'We successfully upgraded from SVN!', 'automatic-updater' ); 
    468         $message .= "<br><br>$update"; 
    469     } 
    470     else { 
    471         $message = esc_html__( 'While upgrading from SVN, we ran into the following error:', 'automatic-updater' ); 
    472         $message .= '<br><br>' . end( $output ) . '<br><br>'; 
    473         $message .= esc_html__( "We're sorry it didn't work out. Please try upgrading manually, instead.", 'automatic-updater' ); 
    474     } 
    475  
    476     $message .= '<br>'; 
    477  
    478     $debug = join( '<br>', $output ); 
    479  
    480     auto_updater_notification( $message, $debug ); 
    481 } 
    482 add_action( 'auto_updater_svn_event', 'auto_updater_svn' ); 
    483  
    484 function auto_updater_notification( $info = '', $debug = '', $upgrade_failed = false ) { 
    485     $options = get_option( 'automatic-updater' ); 
    486  
    487     if ( $options['disable-email'] ) 
    488         return; 
    489  
    490     $site = get_home_url(); 
    491     $subject = sprintf( esc_html__( 'WordPress Update: %s', 'automatic-updater' ), $site ); 
    492  
    493     $message = '<html>'; 
    494     $message .= '<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>'; 
    495     $message .= '<body>'; 
    496  
    497     $message .= esc_html__( 'Howdy!', 'automatic-updater' ); 
    498     $message .= '<br><br>'; 
    499     $message .= wp_kses( sprintf( __( 'Automatic Updater just ran on your site, <a href="%1$s">%1$s</a>, with the following result:', 'automatic-updater' ), $site ), array( 'a' => array( 'href' => array() ) ) ); 
    500     $message .= '<br><br>'; 
    501  
    502     $message .= $info; 
    503  
    504     $message .= '<br>'; 
    505  
    506     if ( $upgrade_failed ) { 
    507         $message .= esc_html__( 'It looks like something went wrong during the update. Note that, if Automatic Updater continues to encounter problems, it will stop trying to do this update, and will not try again until after you manually update.', 'automatic-updater' ); 
    508         $message .= '<br><br>'; 
    509     } 
    510  
    511     $message .= esc_html__( 'Thanks for using the Automatic Updater plugin!', 'automatic-updater' ); 
    512  
    513     if ( ! empty( $options['debug'] ) ) { 
    514         $message .= "<br><br><br>"; 
    515         $message .= esc_html__( 'Debug Information:', 'automatic-updater' ); 
    516         $message .= "<br><br>$debug"; 
    517     } 
    518  
    519     $message .= '</body></html>'; 
    520  
    521     $email = get_option( 'admin_email' ); 
    522     if ( ! empty( $options['override-email'] ) ) 
    523         $email = $options['override-email']; 
    524  
    525     $email = apply_filters( 'auto_updater_notification_email_address', $email ); 
    526  
    527     $headers = array( 
    528                     'MIME-Version: 1.0', 
    529                     'Content-Type: text/html; charset=UTF-8' 
    530                 ); 
    531  
    532     add_filter( 'wp_mail_content_type', 'auto_updater_wp_mail_content_type' ); 
    533     wp_mail( $email, $subject, $message, $headers ); 
    534     remove_filter( 'wp_mail_content_type', 'auto_updater_wp_mail_content_type' ); 
    535 } 
    536  
    537 function auto_updater_wp_mail_content_type() { 
    538     return 'text/html'; 
    539 } 
    540  
    541 function auto_updater_get_update_data() { 
    542     $counts = array( 'plugins' => 0, 'themes' => 0, 'wordpress' => 0 ); 
    543  
    544     $update_plugins = get_site_transient( 'update_plugins' ); 
    545     if ( ! empty( $update_plugins->response ) ) 
    546         $counts['plugins'] = count( $update_plugins->response ); 
    547  
    548     $update_themes = get_site_transient( 'update_themes' ); 
    549     if ( ! empty( $update_themes->response ) ) 
    550         $counts['themes'] = count( $update_themes->response ); 
    551  
    552     if ( function_exists( 'get_core_updates' ) ) { 
    553         $update_wordpress = get_core_updates( array( 'dismissed' => false ) ); 
    554         if ( ! empty( $update_wordpress ) && 'latest' != $update_wordpress[0]->response ) 
    555             $counts['wordpress'] = 1; 
    556     } 
    557  
    558     $counts['total'] = $counts['plugins'] + $counts['themes'] + $counts['wordpress']; 
    559  
    560     return apply_filters( 'auto_update_get_update_data', array( 'counts' => $counts ) ); 
    561 } 
     568 
     569add_action( 'init', array( 'Automatic_Updater', 'init' ) ); 
Note: See TracChangeset for help on using the changeset viewer.