function update() { include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; // Clear the cache. wp_update_themes(); foreach ($this->themes as $theme) { /** * Pre-upgrade action * * @since 3.9.3 * * @param object $theme WP_Theme object * @param array $themes Array of theme objects */ do_action('jetpack_pre_theme_upgrade', $theme, $this->themes); // Objects created inside the for loop to clean the messages for each theme $skin = new Automatic_Upgrader_Skin(); $upgrader = new Theme_Upgrader($skin); $upgrader->init(); $result = $upgrader->upgrade($theme); $this->log[$theme][] = $upgrader->skin->get_upgrade_messages(); } if (!$this->bulk && !$result) { return new WP_Error('update_fail', __('There was an error updating your theme', 'jetpack'), 400); } return true; }
function upgrade_bsf_product($request_product_id, $bundled_id) { global $bsf_product_validate_url, $bsf_support_url; if (!current_user_can('update_plugins')) { wp_die(__('You do not have sufficient permissions to update plugins for this site.', 'bsf')); } $brainstrom_users = get_option('brainstrom_users') ? get_option('brainstrom_users') : array(); $brainstrom_products = get_option('brainstrom_products') ? get_option('brainstrom_products') : array(); $brainstrom_bundled_products = get_option('brainstrom_bundled_products') ? get_option('brainstrom_bundled_products') : array(); $plugins = $themes = $mix = array(); if (!empty($brainstrom_products)) { $plugins = isset($brainstrom_products['plugins']) ? $brainstrom_products['plugins'] : array(); $themes = isset($brainstrom_products['themes']) ? $brainstrom_products['themes'] : array(); } $mix = array_merge($plugins, $themes); $bsf_username = $purchase_key = $type = $template = $name = ''; if (!empty($brainstrom_users)) { foreach ($brainstrom_users as $bsf_user) { $bsf_username = $bsf_user['email']; } } $found_in_bsf_products = false; if ($bundled_id !== false) { $product_details_id = $bundled_id; } else { $product_details_id = $request_product_id; } foreach ($mix as $key => $product) { $pid = $product['id']; if ($pid === $product_details_id) { $purchase_key = $product['purchase_key']; $type = $product['type']; $template = $product['template']; $name = $product['product_name']; $found_in_bsf_products = true; break; } } if ($bundled_id !== false) { if (!empty($brainstrom_bundled_products)) { foreach ($brainstrom_bundled_products as $bp) { if ($bp->id === $request_product_id) { $type = $bp->type; $template = $bp->init; $name = $bp->name; } } } } if ($bsf_username === '' || $purchase_key === '' || $request_product_id === '') { wp_die('Not valid to update product'); } $path = base64_decode($bsf_product_validate_url); $data = array('action' => 'bsf_product_update_request', 'id' => $request_product_id, 'username' => $bsf_username, 'purchase_key' => $purchase_key, 'site_url' => get_site_url(), 'bundled' => $bundled_id); $request = @wp_remote_post($path, array('body' => $data, 'timeout' => '60', 'sslverify' => false)); if (!is_wp_error($request) || wp_remote_retrieve_response_code($request) === 200) { $result = json_decode($request['body']); if (isset($result->error) && !$result->error) { $download_path = $result->update_data->download_url; $timezone = date_default_timezone_get(); $call = 'file=' . $download_path . '&hashtime=' . strtotime(date('d-m-Y h:i:s a')) . '&timezone=' . $timezone; $hash = base64_encode($call); $parse = parse_url($path); $download = $parse['scheme'] . '://' . $parse['host']; $get_path = 'http://downloads.brainstormforce.com/'; $download_path = rtrim($get_path, '/') . '/download.php?hash=' . $hash; //echo $download_path; //die(); require_once ABSPATH . '/wp-admin/includes/file.php'; WP_Filesystem(); global $wp_filesystem; require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; $WP_Upgrader = new WP_Upgrader(); $res = $WP_Upgrader->fs_connect(array(WP_CONTENT_DIR)); if (!$res) { wp_die(new WP_Error('Server error', __("Error! Can't connect to filesystem", 'bsf'))); } else { $upgrade_folder = $wp_filesystem->wp_content_dir() . 'upgrade_tmp/bsf_package'; $package_filename = basename($download_path); $plugin_folder = dirname($template); if ($type === 'theme' && $bundled_id === false) { $defaults = array('clear_update_cache' => true); $args = array(); $parsed_args = wp_parse_args($args, $defaults); $Theme_Upgrader = new Theme_Upgrader(); $Theme_Upgrader->init(); $Theme_Upgrader->upgrade_strings(); $Theme_Upgrader->strings['downloading_package'] = __('Downloading package from Server', 'bsf'); add_filter('upgrader_pre_install', array(&$Theme_Upgrader, 'current_before'), 10, 2); add_filter('upgrader_post_install', array(&$Theme_Upgrader, 'current_after'), 10, 2); add_filter('upgrader_clear_destination', array(&$Theme_Upgrader, 'delete_old_theme'), 10, 4); $Theme_Upgrader->run(array('package' => $download_path, 'destination' => get_theme_root($template), 'clear_destination' => false, 'abort_if_destination_exists' => false, 'clear_working' => true, 'hook_extra' => array('theme' => $template, 'type' => 'theme', 'action' => 'update'))); remove_filter('upgrader_pre_install', array(&$Theme_Upgrader, 'current_before')); remove_filter('upgrader_post_install', array(&$Theme_Upgrader, 'current_after')); remove_filter('upgrader_clear_destination', array(&$Theme_Upgrader, 'delete_old_theme')); if (!$Theme_Upgrader->result || is_wp_error($Theme_Upgrader->result)) { return $Theme_Upgrader->result; } wp_clean_themes_cache($parsed_args['clear_update_cache']); $response = array('status' => true, 'type' => 'theme', 'name' => $name); return $response; } elseif ($type === 'plugin') { $Plugin_Upgrader = new Plugin_Upgrader(); $Plugin_Upgrader->init(); $Plugin_Upgrader->upgrade_strings(); $Plugin_Upgrader->strings['downloading_package'] = __('Downloading package from Server', 'bsf'); add_filter('upgrader_pre_install', array(&$Plugin_Upgrader, 'deactivate_plugin_before_upgrade'), 10, 2); add_filter('upgrader_clear_destination', array(&$Plugin_Upgrader, 'delete_old_plugin'), 10, 4); $Plugin_Upgrader->run(array('package' => $download_path, 'destination' => WP_PLUGIN_DIR, 'abort_if_destination_exists' => false, 'clear_destination' => false, 'clear_working' => true, 'hook_extra' => array('plugin' => $plugin_folder))); // Cleanup our hooks, in case something else does a upgrade on this connection. remove_filter('upgrader_pre_install', array(&$Plugin_Upgrader, 'deactivate_plugin_before_upgrade')); remove_filter('upgrader_clear_destination', array(&$Plugin_Upgrader, 'delete_old_plugin')); if (!$Plugin_Upgrader->result || is_wp_error($Plugin_Upgrader->result)) { return $Plugin_Upgrader->result; } if (is_dir($wp_filesystem->wp_content_dir() . 'upgrade_tmp/bsf_package')) { $wp_filesystem->delete($wp_filesystem->wp_content_dir() . 'upgrade_tmp/bsf_package', true); } // Force refresh of plugin update information delete_site_transient('update_plugins'); wp_cache_delete('plugins', 'plugins'); $response = array('status' => true, 'type' => 'plugin', 'name' => $name); return $response; } } } else { echo $result->message; } } }
private function _update_theme($theme) { global $wp_filesystem; $status = array('update' => 'theme', 'theme' => $theme, 'oldVersion' => '', 'newVersion' => ''); if (false !== strpos($theme, '/') || false !== strpos($theme, '\\')) { $status['error'] = 'not_found'; return $status; } $theme_version = $this->get_theme_version($theme); if (false === $theme_version) { $status['error'] = 'not_found'; return $status; } $status['oldVersion'] = $theme_version; if (!current_user_can('update_themes')) { $status['error'] = 'updates_permission_denied'; return $status; } include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; wp_update_themes(); // WP < 3.7 if (!class_exists('Automatic_Upgrader_Skin')) { require_once UPDRAFTPLUS_DIR . '/central/classes/class-automatic-upgrader-skin.php'; } $skin = new Automatic_Upgrader_Skin(); $upgrader = new Theme_Upgrader($skin); $upgrader->init(); $result = $upgrader->bulk_upgrade(array($theme)); if (is_array($result) && empty($result[$theme]) && is_wp_error($skin->result)) { $result = $skin->result; } $status['messages'] = $upgrader->skin->get_upgrade_messages(); if (is_array($result) && !empty($result[$theme])) { $theme_update_data = current($result); /* * If the `update_themes` site transient is empty (e.g. when you update * two plugins in quick succession before the transient repopulates), * this may be the return. * * Preferably something can be done to ensure `update_themes` isn't empty. * For now, surface some sort of error here. */ if ($theme_update_data === true) { $status['error'] = 'update_failed'; return $status; } $new_theme_version = $this->get_theme_version($theme); if (false === $new_theme_version) { $status['error'] = 'update_failed'; return $status; } $status['newVersion'] = $new_theme_version; return $status; } else { if (is_wp_error($result)) { $status['error'] = $result->get_error_code(); $status['error_message'] = $result->get_error_message(); return $status; } else { if (is_bool($result) && !$result) { $status['error'] = 'unable_to_connect_to_filesystem'; // Pass through the error from WP_Filesystem if one was raised if (is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code()) { $status['error'] = $wp_filesystem->errors->get_error_code(); $status['error_message'] = $wp_filesystem->errors->get_error_message(); } return $status; } else { // An unhandled error occured $status['error'] = 'update_failed'; return $status; } } } }