mp-wp_genesis 1 <?php
mp-wp_genesis 2 /**
mp-wp_genesis 3 * WordPress Administration Update API
mp-wp_genesis 4 *
mp-wp_genesis 5 * @package WordPress
mp-wp_genesis 6 * @subpackage Admin
mp-wp_genesis 7 */
mp-wp_genesis 8
mp-wp_genesis 9 // The admin side of our 1.1 update system
mp-wp_genesis 10
mp-wp_genesis 11 /**
mp-wp_genesis 12 * Selects the first update version from the update_core option
mp-wp_genesis 13 *
mp-wp_genesis 14 * @return object the response from the API
mp-wp_genesis 15 */
mp-wp_genesis 16 function get_preferred_from_update_core() {
mp-wp_genesis 17 $updates = get_core_updates();
mp-wp_genesis 18 if ( !is_array( $updates ) )
mp-wp_genesis 19 return false;
mp-wp_genesis 20 if ( empty( $updates ) )
mp-wp_genesis 21 return (object)array('response' => 'latest');
mp-wp_genesis 22 return $updates[0];
mp-wp_genesis 23 }
mp-wp_genesis 24
mp-wp_genesis 25 /**
mp-wp_genesis 26 * Get available core updates
mp-wp_genesis 27 *
mp-wp_genesis 28 * @param array $options Set $options['dismissed'] to true to show dismissed upgrades too,
mp-wp_genesis 29 * set $options['available'] to false to skip not-dimissed updates.
mp-wp_genesis 30 * @return array Array of the update objects
mp-wp_genesis 31 */
mp-wp_genesis 32 function get_core_updates( $options = array() ) {
mp-wp_genesis 33 $options = array_merge( array('available' => true, 'dismissed' => false ), $options );
mp-wp_genesis 34 $dismissed = get_option( 'dismissed_update_core' );
mp-wp_genesis 35 if ( !is_array( $dismissed ) ) $dismissed = array();
mp-wp_genesis 36 $from_api = get_option( 'update_core' );
mp-wp_genesis 37 if ( empty($from_api) )
mp-wp_genesis 38 return false;
mp-wp_genesis 39 if ( !is_array( $from_api->updates ) ) return false;
mp-wp_genesis 40 $updates = $from_api->updates;
mp-wp_genesis 41 if ( !is_array( $updates ) ) return false;
mp-wp_genesis 42 $result = array();
mp-wp_genesis 43 foreach($updates as $update) {
mp-wp_genesis 44 if ( array_key_exists( $update->current.'|'.$update->locale, $dismissed ) ) {
mp-wp_genesis 45 if ( $options['dismissed'] ) {
mp-wp_genesis 46 $update->dismissed = true;
mp-wp_genesis 47 $result[]= $update;
mp-wp_genesis 48 }
mp-wp_genesis 49 } else {
mp-wp_genesis 50 if ( $options['available'] ) {
mp-wp_genesis 51 $update->dismissed = false;
mp-wp_genesis 52 $result[]= $update;
mp-wp_genesis 53 }
mp-wp_genesis 54 }
mp-wp_genesis 55 }
mp-wp_genesis 56 return $result;
mp-wp_genesis 57 }
mp-wp_genesis 58
mp-wp_genesis 59 function dismiss_core_update( $update ) {
mp-wp_genesis 60 $dismissed = get_option( 'dismissed_update_core' );
mp-wp_genesis 61 $dismissed[ $update->current.'|'.$update->locale ] = true;
mp-wp_genesis 62 return update_option( 'dismissed_update_core', $dismissed );
mp-wp_genesis 63 }
mp-wp_genesis 64
mp-wp_genesis 65 function undismiss_core_update( $version, $locale ) {
mp-wp_genesis 66 $dismissed = get_option( 'dismissed_update_core' );
mp-wp_genesis 67 $key = $version.'|'.$locale;
mp-wp_genesis 68 if ( !isset( $dismissed[$key] ) ) return false;
mp-wp_genesis 69 unset( $dismissed[$key] );
mp-wp_genesis 70 return update_option( 'dismissed_update_core', $dismissed );
mp-wp_genesis 71 }
mp-wp_genesis 72
mp-wp_genesis 73 function find_core_update( $version, $locale ) {
mp-wp_genesis 74 $from_api = get_option( 'update_core' );
mp-wp_genesis 75 if ( !is_array( $from_api->updates ) ) return false;
mp-wp_genesis 76 $updates = $from_api->updates;
mp-wp_genesis 77 foreach($updates as $update) {
mp-wp_genesis 78 if ( $update->current == $version && $update->locale == $locale )
mp-wp_genesis 79 return $update;
mp-wp_genesis 80 }
mp-wp_genesis 81 return false;
mp-wp_genesis 82 }
mp-wp_genesis 83
mp-wp_genesis 84 function core_update_footer( $msg = '' ) {
mp-wp_genesis 85 if ( !current_user_can('manage_options') )
mp-wp_genesis 86 return sprintf( '| '.__( 'Version %s' ), $GLOBALS['wp_version'] );
mp-wp_genesis 87
mp-wp_genesis 88 $cur = get_preferred_from_update_core();
mp-wp_genesis 89 if ( ! isset( $cur->current ) )
mp-wp_genesis 90 $cur->current = '';
mp-wp_genesis 91
mp-wp_genesis 92 if ( ! isset( $cur->url ) )
mp-wp_genesis 93 $cur->url = '';
mp-wp_genesis 94
mp-wp_genesis 95 if ( ! isset( $cur->response ) )
mp-wp_genesis 96 $cur->response = '';
mp-wp_genesis 97
mp-wp_genesis 98 switch ( $cur->response ) {
mp-wp_genesis 99 case 'development' :
mp-wp_genesis 100 return sprintf( __( 'You are using a development version (%1$s). Cool! Please <a href="%2$s">stay updated</a>.' ), $GLOBALS['wp_version'], 'update-core.php');
mp-wp_genesis 101 break;
mp-wp_genesis 102
mp-wp_genesis 103 case 'upgrade' :
mp-wp_genesis 104 if ( current_user_can('manage_options') ) {
mp-wp_genesis 105 return sprintf( '<strong>'.__( '<a href="%1$s">Get Version %2$s</a>' ).'</strong>', 'update-core.php', $cur->current);
mp-wp_genesis 106 break;
mp-wp_genesis 107 }
mp-wp_genesis 108
mp-wp_genesis 109 case 'latest' :
mp-wp_genesis 110 default :
mp-wp_genesis 111 return sprintf( __( 'Version %s' ), $GLOBALS['wp_version'] );
mp-wp_genesis 112 break;
mp-wp_genesis 113 }
mp-wp_genesis 114 }
mp-wp_genesis 115 add_filter( 'update_footer', 'core_update_footer' );
mp-wp_genesis 116
mp-wp_genesis 117 function update_nag() {
mp-wp_genesis 118 global $pagenow;
mp-wp_genesis 119
mp-wp_genesis 120 if ( 'update-core.php' == $pagenow )
mp-wp_genesis 121 return;
mp-wp_genesis 122
mp-wp_genesis 123 $cur = get_preferred_from_update_core();
mp-wp_genesis 124
mp-wp_genesis 125 if ( ! isset( $cur->response ) || $cur->response != 'upgrade' )
mp-wp_genesis 126 return false;
mp-wp_genesis 127
mp-wp_genesis 128 if ( current_user_can('manage_options') )
mp-wp_genesis 129 $msg = sprintf( __('WordPress %1$s is available! <a href="%2$s">Please update now</a>.'), $cur->current, 'update-core.php' );
mp-wp_genesis 130 else
mp-wp_genesis 131 $msg = sprintf( __('WordPress %1$s is available! Please notify the site administrator.'), $cur->current );
mp-wp_genesis 132
mp-wp_genesis 133 echo "<div id='update-nag'>$msg</div>";
mp-wp_genesis 134 }
mp-wp_genesis 135 add_action( 'admin_notices', 'update_nag', 3 );
mp-wp_genesis 136
mp-wp_genesis 137 // Called directly from dashboard
mp-wp_genesis 138 function update_right_now_message() {
mp-wp_genesis 139 $cur = get_preferred_from_update_core();
mp-wp_genesis 140
mp-wp_genesis 141 $msg = sprintf( __('You are using <span class="b">WordPress %s</span>.'), $GLOBALS['wp_version'] );
mp-wp_genesis 142 if ( isset( $cur->response ) && $cur->response == 'upgrade' && current_user_can('manage_options') )
mp-wp_genesis 143 $msg .= " <a href='update-core.php' class='button'>" . sprintf( __('Update to %s'), $cur->current ? $cur->current : __( 'Latest' ) ) . '</a>';
mp-wp_genesis 144
mp-wp_genesis 145 echo "<span id='wp-version-message'>$msg</span>";
mp-wp_genesis 146 }
mp-wp_genesis 147
mp-wp_genesis 148 function wp_plugin_update_row( $file, $plugin_data ) {
mp-wp_genesis 149 $current = get_option( 'update_plugins' );
mp-wp_genesis 150 if ( !isset( $current->response[ $file ] ) )
mp-wp_genesis 151 return false;
mp-wp_genesis 152
mp-wp_genesis 153 $r = $current->response[ $file ];
mp-wp_genesis 154
mp-wp_genesis 155 $details_url = admin_url('plugin-install.php?tab=plugin-information&plugin=' . $r->slug . '&TB_iframe=true&width=600&height=800');
mp-wp_genesis 156
mp-wp_genesis 157 echo '<tr><td colspan="5" class="plugin-update">';
mp-wp_genesis 158 if ( ! current_user_can('update_plugins') )
mp-wp_genesis 159 printf( __('There is a new version of %1$s available. <a href="%2$s" class="thickbox" title="%1$s">View version %3$s Details</a>.'), $plugin_data['Name'], $details_url, $r->new_version);
mp-wp_genesis 160 else if ( empty($r->package) )
mp-wp_genesis 161 printf( __('There is a new version of %1$s available. <a href="%2$s" class="thickbox" title="%1$s">View version %3$s Details</a> <em>automatic upgrade unavailable for this plugin</em>.'), $plugin_data['Name'], $details_url, $r->new_version);
mp-wp_genesis 162 else
mp-wp_genesis 163 printf( __('There is a new version of %1$s available. <a href="%2$s" class="thickbox" title="%1$s">View version %3$s Details</a> or <a href="%4$s">upgrade automatically</a>.'), $plugin_data['Name'], $details_url, $r->new_version, wp_nonce_url('update.php?action=upgrade-plugin&plugin=' . $file, 'upgrade-plugin_' . $file) );
mp-wp_genesis 164
mp-wp_genesis 165 echo '</td></tr>';
mp-wp_genesis 166 }
mp-wp_genesis 167 add_action( 'after_plugin_row', 'wp_plugin_update_row', 10, 2 );
mp-wp_genesis 168
mp-wp_genesis 169 function wp_update_plugin($plugin, $feedback = '') {
mp-wp_genesis 170 global $wp_filesystem;
mp-wp_genesis 171
mp-wp_genesis 172 if ( !empty($feedback) )
mp-wp_genesis 173 add_filter('update_feedback', $feedback);
mp-wp_genesis 174
mp-wp_genesis 175 // Is an update available?
mp-wp_genesis 176 $current = get_option( 'update_plugins' );
mp-wp_genesis 177 if ( !isset( $current->response[ $plugin ] ) )
mp-wp_genesis 178 return new WP_Error('up_to_date', __('The plugin is at the latest version.'));
mp-wp_genesis 179
mp-wp_genesis 180 // Is a filesystem accessor setup?
mp-wp_genesis 181 if ( ! $wp_filesystem || ! is_object($wp_filesystem) )
mp-wp_genesis 182 WP_Filesystem();
mp-wp_genesis 183
mp-wp_genesis 184 if ( ! is_object($wp_filesystem) )
mp-wp_genesis 185 return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
mp-wp_genesis 186
mp-wp_genesis 187 if ( $wp_filesystem->errors->get_error_code() )
mp-wp_genesis 188 return new WP_Error('fs_error', __('Filesystem error'), $wp_filesystem->errors);
mp-wp_genesis 189
mp-wp_genesis 190 //Get the base plugin folder
mp-wp_genesis 191 $plugins_dir = $wp_filesystem->wp_plugins_dir();
mp-wp_genesis 192 if ( empty($plugins_dir) )
mp-wp_genesis 193 return new WP_Error('fs_no_plugins_dir', __('Unable to locate WordPress Plugin directory.'));
mp-wp_genesis 194
mp-wp_genesis 195 //And the same for the Content directory.
mp-wp_genesis 196 $content_dir = $wp_filesystem->wp_content_dir();
mp-wp_genesis 197 if( empty($content_dir) )
mp-wp_genesis 198 return new WP_Error('fs_no_content_dir', __('Unable to locate WordPress Content directory (wp-content).'));
mp-wp_genesis 199
mp-wp_genesis 200 $plugins_dir = trailingslashit( $plugins_dir );
mp-wp_genesis 201 $content_dir = trailingslashit( $content_dir );
mp-wp_genesis 202
mp-wp_genesis 203 // Get the URL to the zip file
mp-wp_genesis 204 $r = $current->response[ $plugin ];
mp-wp_genesis 205
mp-wp_genesis 206 if ( empty($r->package) )
mp-wp_genesis 207 return new WP_Error('no_package', __('Upgrade package not available.'));
mp-wp_genesis 208
mp-wp_genesis 209 // Download the package
mp-wp_genesis 210 $package = $r->package;
mp-wp_genesis 211 apply_filters('update_feedback', sprintf(__('Downloading update from %s'), $package));
mp-wp_genesis 212 $download_file = download_url($package);
mp-wp_genesis 213
mp-wp_genesis 214 if ( is_wp_error($download_file) )
mp-wp_genesis 215 return new WP_Error('download_failed', __('Download failed.'), $download_file->get_error_message());
mp-wp_genesis 216
mp-wp_genesis 217 $working_dir = $content_dir . 'upgrade/' . basename($plugin, '.php');
mp-wp_genesis 218
mp-wp_genesis 219 // Clean up working directory
mp-wp_genesis 220 if ( $wp_filesystem->is_dir($working_dir) )
mp-wp_genesis 221 $wp_filesystem->delete($working_dir, true);
mp-wp_genesis 222
mp-wp_genesis 223 apply_filters('update_feedback', __('Unpacking the update'));
mp-wp_genesis 224 // Unzip package to working directory
mp-wp_genesis 225 $result = unzip_file($download_file, $working_dir);
mp-wp_genesis 226
mp-wp_genesis 227 // Once extracted, delete the package
mp-wp_genesis 228 unlink($download_file);
mp-wp_genesis 229
mp-wp_genesis 230 if ( is_wp_error($result) ) {
mp-wp_genesis 231 $wp_filesystem->delete($working_dir, true);
mp-wp_genesis 232 return $result;
mp-wp_genesis 233 }
mp-wp_genesis 234
mp-wp_genesis 235 if ( is_plugin_active($plugin) ) {
mp-wp_genesis 236 //Deactivate the plugin silently, Prevent deactivation hooks from running.
mp-wp_genesis 237 apply_filters('update_feedback', __('Deactivating the plugin'));
mp-wp_genesis 238 deactivate_plugins($plugin, true);
mp-wp_genesis 239 }
mp-wp_genesis 240
mp-wp_genesis 241 // Remove the existing plugin.
mp-wp_genesis 242 apply_filters('update_feedback', __('Removing the old version of the plugin'));
mp-wp_genesis 243 $this_plugin_dir = trailingslashit( dirname($plugins_dir . $plugin) );
mp-wp_genesis 244
mp-wp_genesis 245 // If plugin is in its own directory, recursively delete the directory.
mp-wp_genesis 246 if ( strpos($plugin, '/') && $this_plugin_dir != $plugins_dir ) //base check on if plugin includes directory seperator AND that its not the root plugin folder
mp-wp_genesis 247 $deleted = $wp_filesystem->delete($this_plugin_dir, true);
mp-wp_genesis 248 else
mp-wp_genesis 249 $deleted = $wp_filesystem->delete($plugins_dir . $plugin);
mp-wp_genesis 250
mp-wp_genesis 251 if ( ! $deleted ) {
mp-wp_genesis 252 $wp_filesystem->delete($working_dir, true);
mp-wp_genesis 253 return new WP_Error('delete_failed', __('Could not remove the old plugin'));
mp-wp_genesis 254 }
mp-wp_genesis 255
mp-wp_genesis 256 apply_filters('update_feedback', __('Installing the latest version'));
mp-wp_genesis 257 // Copy new version of plugin into place.
mp-wp_genesis 258 $result = copy_dir($working_dir, $plugins_dir);
mp-wp_genesis 259 if ( is_wp_error($result) ) {
mp-wp_genesis 260 $wp_filesystem->delete($working_dir, true);
mp-wp_genesis 261 return $result;
mp-wp_genesis 262 }
mp-wp_genesis 263
mp-wp_genesis 264 //Get a list of the directories in the working directory before we delete it, We need to know the new folder for the plugin
mp-wp_genesis 265 $filelist = array_keys( $wp_filesystem->dirlist($working_dir) );
mp-wp_genesis 266
mp-wp_genesis 267 // Remove working directory
mp-wp_genesis 268 $wp_filesystem->delete($working_dir, true);
mp-wp_genesis 269
mp-wp_genesis 270 // Force refresh of plugin update information
mp-wp_genesis 271 delete_option('update_plugins');
mp-wp_genesis 272
mp-wp_genesis 273 if( empty($filelist) )
mp-wp_genesis 274 return false; //We couldnt find any files in the working dir, therefor no plugin installed? Failsafe backup.
mp-wp_genesis 275
mp-wp_genesis 276 $folder = $filelist[0];
mp-wp_genesis 277 $plugin = get_plugins('/' . $folder); //Ensure to pass with leading slash
mp-wp_genesis 278 $pluginfiles = array_keys($plugin); //Assume the requested plugin is the first in the list
mp-wp_genesis 279
mp-wp_genesis 280 return $folder . '/' . $pluginfiles[0];
mp-wp_genesis 281 }
mp-wp_genesis 282
mp-wp_genesis 283 function wp_update_theme($theme, $feedback = '') {
mp-wp_genesis 284 global $wp_filesystem;
mp-wp_genesis 285
mp-wp_genesis 286 if ( !empty($feedback) )
mp-wp_genesis 287 add_filter('update_feedback', $feedback);
mp-wp_genesis 288
mp-wp_genesis 289 // Is an update available?
mp-wp_genesis 290 $current = get_option( 'update_themes' );
mp-wp_genesis 291 if ( !isset( $current->response[ $theme ] ) )
mp-wp_genesis 292 return new WP_Error('up_to_date', __('The theme is at the latest version.'));
mp-wp_genesis 293
mp-wp_genesis 294 $r = $current->response[ $theme ];
mp-wp_genesis 295
mp-wp_genesis 296 $themes = get_themes();
mp-wp_genesis 297 foreach ( (array) $themes as $this_theme ) {
mp-wp_genesis 298 if ( $this_theme['Stylesheet'] == $theme ) {
mp-wp_genesis 299 $theme_directory = preg_replace('!^/themes/!i', '', $this_theme['Stylesheet Dir']);
mp-wp_genesis 300 break;
mp-wp_genesis 301 }
mp-wp_genesis 302 }
mp-wp_genesis 303 unset($themes);
mp-wp_genesis 304
mp-wp_genesis 305 if ( empty($theme_directory) )
mp-wp_genesis 306 return new WP_Error('theme_non_existant', __('Theme does not exist.'));
mp-wp_genesis 307
mp-wp_genesis 308 // Is a filesystem accessor setup?
mp-wp_genesis 309 if ( ! $wp_filesystem || ! is_object($wp_filesystem) )
mp-wp_genesis 310 WP_Filesystem();
mp-wp_genesis 311
mp-wp_genesis 312 if ( ! is_object($wp_filesystem) )
mp-wp_genesis 313 return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
mp-wp_genesis 314
mp-wp_genesis 315 if ( $wp_filesystem->errors->get_error_code() )
mp-wp_genesis 316 return new WP_Error('fs_error', __('Filesystem error'), $wp_filesystem->errors);
mp-wp_genesis 317
mp-wp_genesis 318 //Get the base plugin folder
mp-wp_genesis 319 $themes_dir = $wp_filesystem->wp_themes_dir();
mp-wp_genesis 320 if ( empty($themes_dir) )
mp-wp_genesis 321 return new WP_Error('fs_no_themes_dir', __('Unable to locate WordPress Theme directory.'));
mp-wp_genesis 322
mp-wp_genesis 323 //And the same for the Content directory.
mp-wp_genesis 324 $content_dir = $wp_filesystem->wp_content_dir();
mp-wp_genesis 325 if( empty($content_dir) )
mp-wp_genesis 326 return new WP_Error('fs_no_content_dir', __('Unable to locate WordPress Content directory (wp-content).'));
mp-wp_genesis 327
mp-wp_genesis 328 $themes_dir = trailingslashit( $themes_dir );
mp-wp_genesis 329 $content_dir = trailingslashit( $content_dir );
mp-wp_genesis 330
mp-wp_genesis 331 if ( empty($r->package) )
mp-wp_genesis 332 return new WP_Error('no_package', __('Upgrade package not available.'));
mp-wp_genesis 333
mp-wp_genesis 334 // Download the package
mp-wp_genesis 335 apply_filters('update_feedback', sprintf(__('Downloading update from %s'), $r['package']));
mp-wp_genesis 336 $download_file = download_url($r['package']);
mp-wp_genesis 337
mp-wp_genesis 338 if ( is_wp_error($download_file) )
mp-wp_genesis 339 return new WP_Error('download_failed', __('Download failed.'), $download_file->get_error_message());
mp-wp_genesis 340
mp-wp_genesis 341 $working_dir = $content_dir . 'upgrade/' . basename($theme_directory);
mp-wp_genesis 342
mp-wp_genesis 343 // Clean up working directory
mp-wp_genesis 344 if ( $wp_filesystem->is_dir($working_dir) )
mp-wp_genesis 345 $wp_filesystem->delete($working_dir, true);
mp-wp_genesis 346
mp-wp_genesis 347 apply_filters('update_feedback', __('Unpacking the update'));
mp-wp_genesis 348 // Unzip package to working directory
mp-wp_genesis 349 $result = unzip_file($download_file, $working_dir);
mp-wp_genesis 350
mp-wp_genesis 351 // Once extracted, delete the package
mp-wp_genesis 352 unlink($download_file);
mp-wp_genesis 353
mp-wp_genesis 354 if ( is_wp_error($result) ) {
mp-wp_genesis 355 $wp_filesystem->delete($working_dir, true);
mp-wp_genesis 356 return $result;
mp-wp_genesis 357 }
mp-wp_genesis 358
mp-wp_genesis 359 //TODO: Is theme currently active? If so, set default theme
mp-wp_genesis 360 /*
mp-wp_genesis 361 if ( is_plugin_active($plugin) ) {
mp-wp_genesis 362 //Deactivate the plugin silently, Prevent deactivation hooks from running.
mp-wp_genesis 363 apply_filters('update_feedback', __('Deactivating the plugin'));
mp-wp_genesis 364 deactivate_plugins($plugin, true);
mp-wp_genesis 365 }*/
mp-wp_genesis 366
mp-wp_genesis 367 // Remove the existing plugin.
mp-wp_genesis 368 apply_filters('update_feedback', __('Removing the old version of the theme'));
mp-wp_genesis 369 $deleted = $wp_filesystem->delete($themes_dir . $theme_directory, true);
mp-wp_genesis 370
mp-wp_genesis 371 if ( ! $deleted ) {
mp-wp_genesis 372 $wp_filesystem->delete($working_dir, true);
mp-wp_genesis 373 return new WP_Error('delete_failed', __('Could not remove the old plugin'));
mp-wp_genesis 374 }
mp-wp_genesis 375
mp-wp_genesis 376 apply_filters('update_feedback', __('Installing the latest version'));
mp-wp_genesis 377 // Copy new version of plugin into place.
mp-wp_genesis 378 $result = copy_dir($working_dir, $themes_dir);
mp-wp_genesis 379 if ( is_wp_error($result) ) {
mp-wp_genesis 380 $wp_filesystem->delete($working_dir, true);
mp-wp_genesis 381 return $result;
mp-wp_genesis 382 }
mp-wp_genesis 383
mp-wp_genesis 384 //Get a list of the directories in the working directory before we delete it, We need to know the new folder for the plugin
mp-wp_genesis 385 //$filelist = array_keys( $wp_filesystem->dirlist($working_dir) );
mp-wp_genesis 386
mp-wp_genesis 387 // Remove working directory
mp-wp_genesis 388 $wp_filesystem->delete($working_dir, true);
mp-wp_genesis 389
mp-wp_genesis 390 // Force refresh of plugin update information
mp-wp_genesis 391 delete_option('update_themes');
mp-wp_genesis 392
mp-wp_genesis 393 /*if( empty($filelist) )
mp-wp_genesis 394 return false; //We couldnt find any files in the working dir, therefor no plugin installed? Failsafe backup.
mp-wp_genesis 395
mp-wp_genesis 396 $folder = $filelist[0];
mp-wp_genesis 397 $plugin = get_plugins('/' . $folder); //Ensure to pass with leading slash
mp-wp_genesis 398 $pluginfiles = array_keys($plugin); //Assume the requested plugin is the first in the list
mp-wp_genesis 399
mp-wp_genesis 400 return $folder . '/' . $pluginfiles[0];*/
mp-wp_genesis 401 }
mp-wp_genesis 402
mp-wp_genesis 403
mp-wp_genesis 404 function wp_update_core($current, $feedback = '') {
mp-wp_genesis 405 global $wp_filesystem;
mp-wp_genesis 406
mp-wp_genesis 407 @set_time_limit( 300 );
mp-wp_genesis 408
mp-wp_genesis 409 if ( !empty($feedback) )
mp-wp_genesis 410 add_filter('update_feedback', $feedback);
mp-wp_genesis 411
mp-wp_genesis 412 // Is an update available?
mp-wp_genesis 413 if ( !isset( $current->response ) || $current->response == 'latest' )
mp-wp_genesis 414 return new WP_Error('up_to_date', __('WordPress is at the latest version.'));
mp-wp_genesis 415
mp-wp_genesis 416 // Is a filesystem accessor setup?
mp-wp_genesis 417 if ( ! $wp_filesystem || ! is_object($wp_filesystem) )
mp-wp_genesis 418 WP_Filesystem();
mp-wp_genesis 419
mp-wp_genesis 420 if ( ! is_object($wp_filesystem) )
mp-wp_genesis 421 return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
mp-wp_genesis 422
mp-wp_genesis 423 if ( $wp_filesystem->errors->get_error_code() )
mp-wp_genesis 424 return new WP_Error('fs_error', __('Filesystem error'), $wp_filesystem->errors);
mp-wp_genesis 425
mp-wp_genesis 426 // Get the base WP folder
mp-wp_genesis 427 $wp_dir = $wp_filesystem->abspath();
mp-wp_genesis 428 if ( empty($wp_dir) )
mp-wp_genesis 429 return new WP_Error('fs_no_wp_dir', __('Unable to locate WordPress directory.'));
mp-wp_genesis 430
mp-wp_genesis 431 // And the same for the Content directory.
mp-wp_genesis 432 $content_dir = $wp_filesystem->wp_content_dir();
mp-wp_genesis 433 if( empty($content_dir) )
mp-wp_genesis 434 return new WP_Error('fs_no_content_dir', __('Unable to locate WordPress Content directory (wp-content).'));
mp-wp_genesis 435
mp-wp_genesis 436 $wp_dir = trailingslashit( $wp_dir );
mp-wp_genesis 437 $content_dir = trailingslashit( $content_dir );
mp-wp_genesis 438
mp-wp_genesis 439 // Get the URL to the zip file
mp-wp_genesis 440 $package = $current->package;
mp-wp_genesis 441
mp-wp_genesis 442 // Download the package
mp-wp_genesis 443 apply_filters('update_feedback', sprintf(__('Downloading update from %s'), $package));
mp-wp_genesis 444 $download_file = download_url($package);
mp-wp_genesis 445
mp-wp_genesis 446 if ( is_wp_error($download_file) )
mp-wp_genesis 447 return new WP_Error('download_failed', __('Download failed.'), $download_file->get_error_message());
mp-wp_genesis 448
mp-wp_genesis 449 $working_dir = $content_dir . 'upgrade/core';
mp-wp_genesis 450 // Clean up working directory
mp-wp_genesis 451 if ( $wp_filesystem->is_dir($working_dir) ) {
mp-wp_genesis 452 $wp_filesystem->delete($working_dir, true);
mp-wp_genesis 453 }
mp-wp_genesis 454
mp-wp_genesis 455 apply_filters('update_feedback', __('Unpacking the core update'));
mp-wp_genesis 456 // Unzip package to working directory
mp-wp_genesis 457 $result = unzip_file($download_file, $working_dir);
mp-wp_genesis 458 // Once extracted, delete the package
mp-wp_genesis 459 unlink($download_file);
mp-wp_genesis 460
mp-wp_genesis 461 if ( is_wp_error($result) ) {
mp-wp_genesis 462 $wp_filesystem->delete($working_dir, true);
mp-wp_genesis 463 return $result;
mp-wp_genesis 464 }
mp-wp_genesis 465
mp-wp_genesis 466 // Copy update-core.php from the new version into place.
mp-wp_genesis 467 if ( !$wp_filesystem->copy($working_dir . '/wordpress/wp-admin/includes/update-core.php', $wp_dir . 'wp-admin/includes/update-core.php', true) ) {
mp-wp_genesis 468 $wp_filesystem->delete($working_dir, true);
mp-wp_genesis 469 return new WP_Error('copy_failed', __('Could not copy files'));
mp-wp_genesis 470 }
mp-wp_genesis 471 $wp_filesystem->chmod($wp_dir . 'wp-admin/includes/update-core.php', FS_CHMOD_FILE);
mp-wp_genesis 472
mp-wp_genesis 473 require(ABSPATH . 'wp-admin/includes/update-core.php');
mp-wp_genesis 474
mp-wp_genesis 475 return update_core($working_dir, $wp_dir);
mp-wp_genesis 476 }
mp-wp_genesis 477
mp-wp_genesis 478 function maintenance_nag() {
mp-wp_genesis 479 global $upgrading;
mp-wp_genesis 480 if ( ! isset( $upgrading ) )
mp-wp_genesis 481 return false;
mp-wp_genesis 482
mp-wp_genesis 483 if ( current_user_can('manage_options') )
mp-wp_genesis 484 $msg = sprintf( __('An automated WordPress update has failed to complete - <a href="%s">please attempt the update again now</a>.'), 'update-core.php' );
mp-wp_genesis 485 else
mp-wp_genesis 486 $msg = __('An automated WordPress update has failed to complete! Please notify the site administrator.');
mp-wp_genesis 487
mp-wp_genesis 488 echo "<div id='update-nag'>$msg</div>";
mp-wp_genesis 489 }
mp-wp_genesis 490 add_action( 'admin_notices', 'maintenance_nag' );
mp-wp_genesis 491
mp-wp_genesis 492 ?>