| 1 |
<?php |
| 2 |
|
| 3 |
namespace FlyWP\Api; |
| 4 |
|
| 5 |
use Core_Upgrader; |
| 6 |
use Exception; |
| 7 |
use Plugin_Upgrader; |
| 8 |
use Theme_Upgrader; |
| 9 |
use Throwable; |
| 10 |
use WP_Ajax_Upgrader_Skin; |
| 11 |
|
| 12 |
class Updates { |
| 13 |
|
| 14 |
/** |
| 15 |
* Updates constructor. |
| 16 |
*/ |
| 17 |
public function __construct() { |
| 18 |
flywp()->router->get( 'updates', [ $this, 'respond' ] ); |
| 19 |
flywp()->router->post( 'update-plugins', [ $this, 'update_plugins' ] ); |
| 20 |
flywp()->router->post( 'update-themes', [ $this, 'update_themes' ] ); |
| 21 |
flywp()->router->post( 'update-core', [ $this, 'update_core' ] ); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Handle the request. |
| 26 |
* |
| 27 |
* @return void |
| 28 |
*/ |
| 29 |
public function respond() { |
| 30 |
$response = [ |
| 31 |
'core' => $this->core_updates(), |
| 32 |
'plugins' => $this->has_plugin_update(), |
| 33 |
'themes' => $this->has_theme_update(), |
| 34 |
]; |
| 35 |
|
| 36 |
wp_send_json( $response ); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Update a plugin. |
| 41 |
* |
| 42 |
* @param array $args |
| 43 |
* |
| 44 |
* @return void |
| 45 |
*/ |
| 46 |
public function update_plugins( $args ) { |
| 47 |
if ( ! isset( $args['plugins'] ) || ! is_array( $args['plugins'] ) ) { |
| 48 |
wp_send_json_error( 'Missing plugin name(s)' ); |
| 49 |
} |
| 50 |
|
| 51 |
$plugins = array_map( 'sanitize_text_field', wp_unslash( $args['plugins'] ) ); |
| 52 |
|
| 53 |
try { |
| 54 |
$this->update_item( $plugins, 'plugin' ); |
| 55 |
} catch ( Throwable $th ) { |
| 56 |
wp_send_json_error( [ |
| 57 |
'message' => $th->getMessage(), |
| 58 |
], 500 ); |
| 59 |
} |
| 60 |
|
| 61 |
wp_send_json_success( [ |
| 62 |
'message' => 'Plugins updated successfully.', |
| 63 |
] ); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Update a theme. |
| 68 |
* |
| 69 |
* @param array $args |
| 70 |
* |
| 71 |
* @return void |
| 72 |
*/ |
| 73 |
public function update_themes( $args ) { |
| 74 |
if ( ! isset( $args['themes'] ) || ! is_array( $args['themes'] ) ) { |
| 75 |
wp_send_json_error( 'Missing theme name(s)' ); |
| 76 |
} |
| 77 |
|
| 78 |
$themes = array_map( 'sanitize_text_field', wp_unslash( $args['themes'] ) ); |
| 79 |
|
| 80 |
try { |
| 81 |
$this->update_item( $themes, 'theme' ); |
| 82 |
} catch ( Throwable $th ) { |
| 83 |
wp_send_json_error( [ |
| 84 |
'message' => $th->getMessage(), |
| 85 |
], 500 ); |
| 86 |
} |
| 87 |
|
| 88 |
wp_send_json_success( [ |
| 89 |
'message' => 'Themes updated successfully.', |
| 90 |
] ); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Update WordPress core. |
| 95 |
* |
| 96 |
* @param array $args |
| 97 |
* |
| 98 |
* @return void |
| 99 |
*/ |
| 100 |
public function update_core( $args ) { |
| 101 |
$version = isset( $args['version'] ) ? sanitize_text_field( $args['version'] ) : false; |
| 102 |
$locale = isset( $args['locale'] ) ? sanitize_text_field( $args['locale'] ) : get_locale(); |
| 103 |
|
| 104 |
if ( ! function_exists( 'find_core_update' ) ) { |
| 105 |
require_once ABSPATH . 'wp-admin/includes/update.php'; |
| 106 |
} |
| 107 |
|
| 108 |
if ( ! $version ) { |
| 109 |
$updates = get_core_updates(); |
| 110 |
$version = $updates[0]->current; |
| 111 |
} |
| 112 |
|
| 113 |
$update = find_core_update( $version, $locale ); |
| 114 |
|
| 115 |
if ( ! $update ) { |
| 116 |
wp_send_json_error( [ |
| 117 |
'message' => 'Update not found.', |
| 118 |
] ); |
| 119 |
} |
| 120 |
|
| 121 |
$this->include_files(); |
| 122 |
$this->extend_time_limit(); |
| 123 |
|
| 124 |
$upgrader = new Core_Upgrader( new WP_Ajax_Upgrader_Skin() ); |
| 125 |
$result = $upgrader->upgrade( $update ); |
| 126 |
|
| 127 |
if ( is_wp_error( $result ) ) { |
| 128 |
wp_send_json_error( [ |
| 129 |
'message' => $result->get_error_message(), |
| 130 |
], 500 ); |
| 131 |
} |
| 132 |
|
| 133 |
wp_send_json_success( [ |
| 134 |
'message' => 'WordPress core updated successfully.', |
| 135 |
] ); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Include required files. |
| 140 |
* |
| 141 |
* @return void |
| 142 |
*/ |
| 143 |
private function include_files() { |
| 144 |
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 145 |
require_once ABSPATH . 'wp-admin/includes/class-automatic-upgrader-skin.php'; |
| 146 |
require_once ABSPATH . 'wp-admin/includes/class-wp-ajax-upgrader-skin.php'; |
| 147 |
require_once ABSPATH . 'wp-admin/includes/update.php'; |
| 148 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Increase execution time limit. |
| 153 |
* |
| 154 |
* @return void |
| 155 |
*/ |
| 156 |
public function extend_time_limit() { |
| 157 |
set_time_limit( 300 ); |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Update plugins or themes. |
| 162 |
* |
| 163 |
* @param string $type the type of update ('plugin' or 'theme') |
| 164 |
* @param array $args the update arguments |
| 165 |
* |
| 166 |
* @return void |
| 167 |
*/ |
| 168 |
private function update_item( $items, $type = 'plugin' ) { |
| 169 |
$this->include_files(); |
| 170 |
|
| 171 |
$skin = new WP_Ajax_Upgrader_Skin(); |
| 172 |
|
| 173 |
if ( $type === 'plugin' ) { |
| 174 |
$upgrader = new Plugin_Upgrader( $skin ); |
| 175 |
} elseif ( $type === 'theme' ) { |
| 176 |
$upgrader = new Theme_Upgrader( $skin ); |
| 177 |
} else { |
| 178 |
throw new Exception( 'Invalid update type' ); |
| 179 |
} |
| 180 |
|
| 181 |
$this->extend_time_limit(); |
| 182 |
$result = $upgrader->bulk_upgrade( $items ); |
| 183 |
|
| 184 |
if ( false === $result ) { |
| 185 |
throw new Exception( 'Update failed' ); |
| 186 |
} |
| 187 |
|
| 188 |
// refresh the update cache |
| 189 |
if ( $type === 'plugin' ) { |
| 190 |
wp_update_plugins(); |
| 191 |
} elseif ( $type === 'theme' ) { |
| 192 |
wp_update_themes(); |
| 193 |
} |
| 194 |
|
| 195 |
return true; |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Check if WordPress core has an update available. |
| 200 |
* |
| 201 |
* @return array |
| 202 |
*/ |
| 203 |
private function core_updates() { |
| 204 |
$update = get_site_transient( 'update_core' ); |
| 205 |
$current = get_bloginfo( 'version' ); |
| 206 |
|
| 207 |
if ( ! function_exists( 'get_preferred_from_update_core' ) ) { |
| 208 |
require_once ABSPATH . 'wp-admin/includes/update.php'; |
| 209 |
} |
| 210 |
|
| 211 |
$update = get_preferred_from_update_core(); |
| 212 |
|
| 213 |
$response = [ |
| 214 |
'version' => $current, |
| 215 |
'update_available' => false, |
| 216 |
'new_version' => null, |
| 217 |
]; |
| 218 |
|
| 219 |
if ( ! isset( $update->response ) || 'upgrade' !== $update->response ) { |
| 220 |
return $response; |
| 221 |
} |
| 222 |
|
| 223 |
$response['update_available'] = true; |
| 224 |
$response['new_version'] = $update->current; |
| 225 |
|
| 226 |
return $response; |
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* Check if a plugin has an update available. |
| 231 |
* |
| 232 |
* @return bool |
| 233 |
*/ |
| 234 |
private function has_plugin_update() { |
| 235 |
if ( ! function_exists( 'get_plugins' ) ) { |
| 236 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 237 |
} |
| 238 |
|
| 239 |
if ( ! function_exists( 'get_plugin_updates' ) ) { |
| 240 |
require_once ABSPATH . 'wp-admin/includes/update.php'; |
| 241 |
} |
| 242 |
|
| 243 |
$updates = get_plugin_updates(); |
| 244 |
|
| 245 |
if ( $updates && is_array( $updates ) && count( $updates ) > 0 ) { |
| 246 |
return count( $updates ); |
| 247 |
} |
| 248 |
|
| 249 |
return false; |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* Check if a theme has an update available. |
| 254 |
* |
| 255 |
* @return bool |
| 256 |
*/ |
| 257 |
private function has_theme_update() { |
| 258 |
$updates = get_site_transient( 'update_themes' ); |
| 259 |
|
| 260 |
if ( $updates && is_object( $updates ) && count( $updates->response ) > 0 ) { |
| 261 |
return count( $updates->response ); |
| 262 |
} |
| 263 |
|
| 264 |
return false; |
| 265 |
} |
| 266 |
} |
| 267 |
|