/**
  * Check for Updates at the defined API endpoint and modify the update array.
  *
  * This function dives into the update API just when WordPress creates its update array,
  * then adds a custom API call and injects the custom plugin data retrieved from the API.
  * It is reassembled from parts of the native WordPress plugin update code.
  * See wp-includes/update.php line 121 for the original wp_update_plugins() function.
  *
  * @uses api_request()
  *
  * @param array   $_transient_data Update array build by WordPress.
  * @return array Modified update array with custom plugin data.
  */
 function check_plugin_update($_transient_data)
 {
     global $pagenow;
     if (!is_object($_transient_data)) {
         $_transient_data = new stdClass();
     }
     if (!$this->slug || !isset($this->api_data[$this->slug]) || 'plugins.php' == $pagenow && is_multisite()) {
         return $_transient_data;
     }
     if (empty($_transient_data->response) || empty($_transient_data->response[$this->name])) {
         $version_info = $this->api_request('plugin_latest_version', array('slug' => $this->slug));
         if (false !== $version_info && is_object($version_info) && isset($version_info->new_version)) {
             $this->did_check = true;
             if (version_compare($this->version, $version_info->new_version, '<')) {
                 $_transient_data->response[$this->name] = $version_info;
                 $_transient_data->response[$this->name]->plugin = $this->name;
                 if (isset($this->api_data[$this->slug]['auto']) && $this->api_data[$this->slug]['auto']) {
                     $this->version = $version_info->new_version;
                     $_transient_data->response[$this->name]->autoupdate = TRUE;
                     $_transient_data->last_checked = time();
                     $_transient_data->checked[$this->name] = $this->version;
                     set_site_transient('update_plugins', $_transient_data);
                     $upgrader = new WP_Automatic_Updater();
                     $result = $upgrader->update('plugin', $_transient_data->response[$this->name]);
                     if (!is_wp_error(!$result)) {
                         delete_transient(self::PREFFIX . '-' . $this->slug);
                         activate_plugin($this->api_data[$this->slug]['dir']);
                     } else {
                         exit;
                     }
                 }
             }
             $_transient_data->last_checked = time();
             $_transient_data->checked[$this->name] = $this->version;
         }
     }
     return $_transient_data;
 }
예제 #2
0
/**
 * AJAX handler for updating core.
 *
 * @since 4.6.0
 *
 * @see Core_Upgrader
 */
function wp_ajax_update_core()
{
    check_ajax_referer('updates');
    if (!current_user_can('update_core')) {
        $status['error'] = __('You do not have sufficient permissions to update this site.');
        wp_send_json_error($status);
    }
    $reinstall = isset($_POST['reinstall']) ? (bool) $_POST['reinstall'] : false;
    $version = isset($_POST['version']) ? sanitize_text_field(wp_unslash($_POST['version'])) : false;
    $locale = isset($_POST['locale']) ? sanitize_text_field(wp_unslash($_POST['locale'])) : 'en_US';
    $update = find_core_update($version, $locale);
    if (!$update) {
        return;
    }
    $status = array('update' => 'core', 'redirect' => esc_url(self_admin_url('about.php?updated')));
    if ($update->current === $update->version) {
        wp_send_json_success($status);
    }
    include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
    if ($reinstall) {
        $update->response = 'reinstall';
    }
    $upgrader = new WP_Automatic_Updater();
    $result = $upgrader->update('core', $update);
    if (is_array($result) && !empty($result[0])) {
        wp_send_json_success($status);
    } else {
        if (is_wp_error($result)) {
            $status['error'] = $result->get_error_message();
            wp_send_json_error($status);
        } else {
            if (false === $result) {
                // These aren't actual errors.
                $status['error'] = __('Installation Failed');
                wp_send_json_error($status);
            }
        }
    }
    // An unhandled error occurred.
    $status['error'] = __('Installation failed.');
    wp_send_json_error($status);
}