| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('UPDRAFTCENTRAL_CLIENT_DIR')) die('No access.'); |
| 4 |
|
| 5 |
/** |
| 6 |
* Handles UpdraftCentral Theme Commands which basically handles |
| 7 |
* the installation and activation of a theme |
| 8 |
*/ |
| 9 |
class UpdraftCentral_Theme_Commands extends UpdraftCentral_Commands { |
| 10 |
|
| 11 |
private $switched = false; |
| 12 |
|
| 13 |
/** |
| 14 |
* Function that gets called before every action |
| 15 |
* |
| 16 |
* @param string $command a string that corresponds to UDC command to call a certain method for this class. |
| 17 |
* @param array $data an array of data post or get fields |
| 18 |
* @param array $extra_info extrainfo use in the udrpc_action, e.g. user_id |
| 19 |
* |
| 20 |
* link to udrpc_action main function in class UpdraftCentral_Listener |
| 21 |
*/ |
| 22 |
public function _pre_action($command, $data, $extra_info) {// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable -- This function is called from listener.php and $extra_info is being sent. |
| 23 |
// Here we assign the current blog_id to a variable $blog_id |
| 24 |
$blog_id = get_current_blog_id(); |
| 25 |
if (!empty($data['site_id'])) $blog_id = $data['site_id']; |
| 26 |
|
| 27 |
if (function_exists('switch_to_blog') && is_multisite() && $blog_id) { |
| 28 |
$this->switched = switch_to_blog($blog_id); |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Function that gets called after every action |
| 34 |
* |
| 35 |
* @param string $command a string that corresponds to UDC command to call a certain method for this class. |
| 36 |
* @param array $data an array of data post or get fields |
| 37 |
* @param array $extra_info extrainfo use in the udrpc_action, e.g. user_id |
| 38 |
* |
| 39 |
* link to udrpc_action main function in class UpdraftCentral_Listener |
| 40 |
*/ |
| 41 |
public function _post_action($command, $data, $extra_info) {// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable -- Unused parameter is present because the caller from UpdraftCentral_Listener class uses 3 arguments. |
| 42 |
// Here, we're restoring to the current (default) blog before we switched |
| 43 |
if ($this->switched) restore_current_blog(); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Constructor |
| 48 |
*/ |
| 49 |
public function __construct() { |
| 50 |
$this->_admin_include('theme.php', 'file.php', 'template.php', 'class-wp-upgrader.php', 'theme-install.php', 'update.php'); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Installs and activates a theme through upload |
| 55 |
* |
| 56 |
* @param array $params Parameter array containing information pertaining the currently uploaded theme |
| 57 |
* @return array Contains the result of the current process |
| 58 |
*/ |
| 59 |
public function upload_theme($params) { |
| 60 |
return $this->process_chunk_upload($params, 'theme'); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Checks whether the theme is currently installed and activated. |
| 65 |
* |
| 66 |
* @param array $query Parameter array containing the name of the theme to check |
| 67 |
* @return array Contains the result of the current process |
| 68 |
*/ |
| 69 |
public function is_theme_installed($query) { |
| 70 |
|
| 71 |
if (!isset($query['theme'])) |
| 72 |
return $this->_generic_error_response('theme_name_required'); |
| 73 |
|
| 74 |
|
| 75 |
$result = $this->_get_theme_info($query['theme']); |
| 76 |
return $this->_response($result); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Applies currently requested action for theme processing |
| 81 |
* |
| 82 |
* @param string $action The action to apply (e.g. activate or install) |
| 83 |
* @param array $query Parameter array containing information for the currently requested action |
| 84 |
* |
| 85 |
* @return array |
| 86 |
*/ |
| 87 |
private function _apply_theme_action($action, $query) { |
| 88 |
|
| 89 |
$result = array(); |
| 90 |
switch ($action) { |
| 91 |
case 'activate': |
| 92 |
$info = $this->_get_theme_info($query['theme']); |
| 93 |
if ($info['installed']) { |
| 94 |
switch_theme($info['slug']); |
| 95 |
if (wp_get_theme()->get_stylesheet() === $info['slug']) { |
| 96 |
$result = array('activated' => true, 'info' => $this->_get_theme_info($query['theme']), 'last_state' => $info); |
| 97 |
} else { |
| 98 |
$result = $this->_generic_error_response('theme_not_activated', array( |
| 99 |
'theme' => $query['theme'], |
| 100 |
'error_code' => 'theme_not_activated', |
| 101 |
'error_message' => __('There appears to be a problem activating or switching to the intended theme.', 'updraftplus').' '.__('Please check your permissions and try again.', 'updraftplus'), |
| 102 |
'info' => $this->_get_theme_info($query['theme']) |
| 103 |
)); |
| 104 |
} |
| 105 |
} else { |
| 106 |
$result = $this->_generic_error_response('theme_not_installed', array( |
| 107 |
'theme' => $query['theme'], |
| 108 |
'error_code' => 'theme_not_installed', |
| 109 |
'error_message' => __('The theme you wish to activate is either not installed or has been removed recently.', 'updraftplus'), |
| 110 |
'info' => $info |
| 111 |
)); |
| 112 |
} |
| 113 |
break; |
| 114 |
case 'network_enable': |
| 115 |
$info = $this->_get_theme_info($query['theme']); |
| 116 |
if ($info['installed']) { |
| 117 |
if (current_user_can('manage_network_themes')) { |
| 118 |
// Make sure that network_enable_theme is present and callable since |
| 119 |
// it is only available at 4.6. If not, we'll do things the old fashion way |
| 120 |
if (is_callable(array('WP_Theme', 'network_enable_theme'))) { |
| 121 |
WP_Theme::network_enable_theme($info['slug']); |
| 122 |
} else { |
| 123 |
$allowed_themes = get_site_option('allowedthemes'); |
| 124 |
$allowed_themes[$info['slug']] = true; |
| 125 |
|
| 126 |
update_site_option('allowedthemes', $allowed_themes); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
$allowed = WP_Theme::get_allowed_on_network(); |
| 131 |
if (is_array($allowed) && !empty($allowed[$info['slug']])) { |
| 132 |
$result = array('enabled' => true, 'info' => $this->_get_theme_info($query['theme']), 'last_state' => $info); |
| 133 |
} else { |
| 134 |
$result = $this->_generic_error_response('theme_not_enabled', array( |
| 135 |
'theme' => $query['theme'], |
| 136 |
'error_code' => 'theme_not_enabled', |
| 137 |
'error_message' => __('There appears to be a problem enabling the intended theme on your network.', 'updraftplus').' '.__('Please kindly check your permission and try again.', 'updraftplus'), |
| 138 |
'info' => $this->_get_theme_info($query['theme']) |
| 139 |
)); |
| 140 |
} |
| 141 |
} else { |
| 142 |
$result = $this->_generic_error_response('theme_not_installed', array( |
| 143 |
'theme' => $query['theme'], |
| 144 |
'error_code' => 'theme_not_installed', |
| 145 |
'error_message' => __('The theme you wish to enable on your network is either not installed or has been removed recently.', 'updraftplus'), |
| 146 |
'info' => $info |
| 147 |
)); |
| 148 |
} |
| 149 |
break; |
| 150 |
case 'network_disable': |
| 151 |
$info = $this->_get_theme_info($query['theme']); |
| 152 |
if ($info['installed']) { |
| 153 |
if (current_user_can('manage_network_themes')) { |
| 154 |
// Make sure that network_disable_theme is present and callable since |
| 155 |
// it is only available at 4.6. If not, we'll do things the old fashion way |
| 156 |
if (is_callable(array('WP_Theme', 'network_disable_theme'))) { |
| 157 |
WP_Theme::network_disable_theme($info['slug']); |
| 158 |
} else { |
| 159 |
$allowed_themes = get_site_option('allowedthemes'); |
| 160 |
if (isset($allowed_themes[$info['slug']])) { |
| 161 |
unset($allowed_themes[$info['slug']]); |
| 162 |
} |
| 163 |
|
| 164 |
update_site_option('allowedthemes', $allowed_themes); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
$allowed = WP_Theme::get_allowed_on_network(); |
| 169 |
if (is_array($allowed) && empty($allowed[$info['slug']])) { |
| 170 |
$result = array('disabled' => true, 'info' => $this->_get_theme_info($query['theme']), 'last_state' => $info); |
| 171 |
} else { |
| 172 |
$result = $this->_generic_error_response('theme_not_disabled', array( |
| 173 |
'theme' => $query['theme'], |
| 174 |
'error_code' => 'theme_not_disabled', |
| 175 |
'error_message' => __('There appears to be a problem disabling the intended theme from your network.', 'updraftplus').' '.__('Please kindly check your permission and try again.', 'updraftplus'), |
| 176 |
'info' => $this->_get_theme_info($query['theme']) |
| 177 |
)); |
| 178 |
} |
| 179 |
} else { |
| 180 |
$result = $this->_generic_error_response('theme_not_installed', array( |
| 181 |
'theme' => $query['theme'], |
| 182 |
'error_code' => 'theme_not_installed', |
| 183 |
'error_message' => __('The theme you wish to disable from your network is either not installed or has been removed recently.', 'updraftplus'), |
| 184 |
'info' => $info |
| 185 |
)); |
| 186 |
} |
| 187 |
break; |
| 188 |
case 'install': |
| 189 |
$api = themes_api('theme_information', array( |
| 190 |
'slug' => $query['slug'], |
| 191 |
'fields' => array( |
| 192 |
'description' => true, |
| 193 |
'sections' => false, |
| 194 |
'rating' => true, |
| 195 |
'ratings' => true, |
| 196 |
'downloaded' => true, |
| 197 |
'downloadlink' => true, |
| 198 |
'last_updated' => true, |
| 199 |
'screenshot_url' => true, |
| 200 |
'parent' => true, |
| 201 |
) |
| 202 |
)); |
| 203 |
|
| 204 |
$info = $this->_get_theme_info($query['theme']); |
| 205 |
if (is_wp_error($api)) { |
| 206 |
$result = $this->_generic_error_response('generic_response_error', array( |
| 207 |
'theme' => $query['theme'], |
| 208 |
'error_code' => 'theme_not_installed', |
| 209 |
'error_message' => $api->get_error_message(), |
| 210 |
'info' => $info |
| 211 |
)); |
| 212 |
} else { |
| 213 |
$installed = $info['installed']; |
| 214 |
|
| 215 |
$error_code = $error_message = ''; |
| 216 |
if (!$installed) { |
| 217 |
// WP < 3.7 |
| 218 |
if (!class_exists('Automatic_Upgrader_Skin')) include_once(dirname(dirname(__FILE__)).'/classes/class-automatic-upgrader-skin.php'); |
| 219 |
|
| 220 |
$skin = new Automatic_Upgrader_Skin(); |
| 221 |
$upgrader = new Theme_Upgrader($skin); |
| 222 |
|
| 223 |
$download_link = $api->download_link; |
| 224 |
$installed = $upgrader->install($download_link); |
| 225 |
|
| 226 |
if (is_wp_error($installed)) { |
| 227 |
$error_code = $installed->get_error_code(); |
| 228 |
$error_message = $installed->get_error_message(); |
| 229 |
} elseif (is_wp_error($skin->result)) { |
| 230 |
$error_code = $skin->result->get_error_code(); |
| 231 |
$error_message = $skin->result->get_error_message(); |
| 232 |
|
| 233 |
$error_data = $skin->result->get_error_data($error_code); |
| 234 |
if (!empty($error_data)) { |
| 235 |
if (is_array($error_data)) $error_data = json_encode($error_data); |
| 236 |
$error_message .= ' '.$error_data; |
| 237 |
} |
| 238 |
} elseif (is_null($installed) || !$installed) { |
| 239 |
global $wp_filesystem; |
| 240 |
$upgrade_messages = $skin->get_upgrade_messages(); |
| 241 |
|
| 242 |
if (!class_exists('WP_Filesystem_Base')) include_once(ABSPATH.'/wp-admin/includes/class-wp-filesystem-base.php'); |
| 243 |
|
| 244 |
// Pass through the error from WP_Filesystem if one was raised. |
| 245 |
if ($wp_filesystem instanceof WP_Filesystem_Base && is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code()) { |
| 246 |
$error_code = $wp_filesystem->errors->get_error_code(); |
| 247 |
$error_message = $wp_filesystem->errors->get_error_message(); |
| 248 |
} elseif (!empty($upgrade_messages)) { |
| 249 |
// We're only after for the last feedback that we received from the install process. Mostly, |
| 250 |
// that is where the last error has been inserted. |
| 251 |
$messages = $skin->get_upgrade_messages(); |
| 252 |
$error_code = 'install_failed'; |
| 253 |
$error_message = end($messages); |
| 254 |
} else { |
| 255 |
$error_code = 'unable_to_connect_to_filesystem'; |
| 256 |
$error_message = __('Unable to connect to the filesystem.', 'updraftplus').' '.__('Please confirm your credentials.', 'updraftplus'); |
| 257 |
} |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
if (!$installed || is_wp_error($installed)) { |
| 262 |
$result = $this->_generic_error_response('theme_install_failed', array( |
| 263 |
'theme' => $query['theme'], |
| 264 |
'error_code' => $error_code, |
| 265 |
'error_message' => $error_message, |
| 266 |
'info' => $this->_get_theme_info($query['theme']) |
| 267 |
)); |
| 268 |
} else { |
| 269 |
$result = array('installed' => true, 'info' => $this->_get_theme_info($query['theme']), 'last_state' => $info); |
| 270 |
} |
| 271 |
} |
| 272 |
break; |
| 273 |
} |
| 274 |
|
| 275 |
return $result; |
| 276 |
} |
| 277 |
|
| 278 |
/** |
| 279 |
* Preloads the submitted credentials to the global $_POST variable |
| 280 |
* |
| 281 |
* @param array $query Parameter array containing information for the currently requested action |
| 282 |
*/ |
| 283 |
private function _preload_credentials($query) { |
| 284 |
if (!empty($query) && isset($query['filesystem_credentials'])) { |
| 285 |
parse_str($query['filesystem_credentials'], $filesystem_credentials); |
| 286 |
if (is_array($filesystem_credentials)) { |
| 287 |
foreach ($filesystem_credentials as $key => $value) { |
| 288 |
// Put them into $_POST, which is where request_filesystem_credentials() checks for them. |
| 289 |
$_POST[$key] = $value; |
| 290 |
} |
| 291 |
} |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
/** |
| 296 |
* Checks whether we have the required fields submitted and the user has |
| 297 |
* the capabilities to execute the requested action |
| 298 |
* |
| 299 |
* @param array $query The submitted information |
| 300 |
* @param array $fields The required fields to check |
| 301 |
* @param array $capabilities The capabilities to check and validate |
| 302 |
* |
| 303 |
* @return array|string |
| 304 |
*/ |
| 305 |
private function _validate_fields_and_capabilities($query, $fields, $capabilities) { |
| 306 |
|
| 307 |
$error = ''; |
| 308 |
if (!empty($fields)) { |
| 309 |
for ($i=0; $i<count($fields); $i++) { |
| 310 |
$field = $fields[$i]; |
| 311 |
|
| 312 |
if (!isset($query[$field])) { |
| 313 |
if ('keyword' === $field) { |
| 314 |
$error = $this->_generic_error_response('keyword_required'); |
| 315 |
} else { |
| 316 |
$error = $this->_generic_error_response('theme_'.$query[$field].'_required'); |
| 317 |
} |
| 318 |
break; |
| 319 |
} |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
if (empty($error) && !empty($capabilities)) { |
| 324 |
for ($i=0; $i<count($capabilities); $i++) { |
| 325 |
if (!current_user_can($capabilities[$i])) { |
| 326 |
$error = $this->_generic_error_response('theme_insufficient_permission'); |
| 327 |
break; |
| 328 |
} |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
return $error; |
| 333 |
} |
| 334 |
|
| 335 |
/** |
| 336 |
* Processing an action for multiple items |
| 337 |
* |
| 338 |
* @param array $query Parameter array containing a list of themes to process |
| 339 |
* @return array Contains the results of the bulk process |
| 340 |
*/ |
| 341 |
public function process_action_in_bulk($query) { |
| 342 |
$action = isset($query['action']) ? $query['action'] : ''; |
| 343 |
$items = isset($query['args']) ? $query['args']['items'] : array(); |
| 344 |
|
| 345 |
$results = array(); |
| 346 |
if (!empty($action) && !empty($items) && is_array($items)) { |
| 347 |
foreach ($items as $value) { |
| 348 |
if (method_exists($this, $action)) { |
| 349 |
$results[] = $this->$action($value); |
| 350 |
} |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
return $this->_response($results); |
| 355 |
} |
| 356 |
|
| 357 |
/** |
| 358 |
* Activates the theme |
| 359 |
* |
| 360 |
* @param array $query Parameter array containing the name of the theme to activate |
| 361 |
* @return array Contains the result of the current process |
| 362 |
*/ |
| 363 |
public function activate_theme($query) { |
| 364 |
|
| 365 |
$fields = array('theme'); |
| 366 |
$permissions = array('switch_themes'); |
| 367 |
|
| 368 |
$error = $this->_validate_fields_and_capabilities($query, $fields, $permissions); |
| 369 |
if (!empty($error)) { |
| 370 |
return $error; |
| 371 |
} |
| 372 |
|
| 373 |
$this->_preload_credentials($query); |
| 374 |
|
| 375 |
$result = $this->_apply_theme_action('activate', $query); |
| 376 |
if (empty($result['activated'])) { |
| 377 |
return $result; |
| 378 |
} |
| 379 |
|
| 380 |
return $this->_response($result); |
| 381 |
} |
| 382 |
|
| 383 |
/** |
| 384 |
* Enables theme for network |
| 385 |
* |
| 386 |
* @param array $query Parameter array containing the name of the theme to activate |
| 387 |
* @return array Contains the result of the current process |
| 388 |
*/ |
| 389 |
public function network_enable_theme($query) { |
| 390 |
|
| 391 |
$fields = array('theme'); |
| 392 |
$permissions = array('switch_themes'); |
| 393 |
|
| 394 |
$error = $this->_validate_fields_and_capabilities($query, $fields, $permissions); |
| 395 |
if (!empty($error)) { |
| 396 |
return $error; |
| 397 |
} |
| 398 |
|
| 399 |
$this->_preload_credentials($query); |
| 400 |
|
| 401 |
$result = $this->_apply_theme_action('network_enable', $query); |
| 402 |
if (empty($result['enabled'])) { |
| 403 |
return $result; |
| 404 |
} |
| 405 |
|
| 406 |
return $this->_response($result); |
| 407 |
} |
| 408 |
|
| 409 |
/** |
| 410 |
* Disables theme from network |
| 411 |
* |
| 412 |
* @param array $query Parameter array containing the name of the theme to activate |
| 413 |
* @return array Contains the result of the current process |
| 414 |
*/ |
| 415 |
public function network_disable_theme($query) { |
| 416 |
|
| 417 |
$fields = array('theme'); |
| 418 |
$permissions = array('switch_themes'); |
| 419 |
|
| 420 |
$error = $this->_validate_fields_and_capabilities($query, $fields, $permissions); |
| 421 |
if (!empty($error)) { |
| 422 |
return $error; |
| 423 |
} |
| 424 |
|
| 425 |
$this->_preload_credentials($query); |
| 426 |
|
| 427 |
$result = $this->_apply_theme_action('network_disable', $query); |
| 428 |
if (empty($result['disabled'])) { |
| 429 |
return $result; |
| 430 |
} |
| 431 |
|
| 432 |
return $this->_response($result); |
| 433 |
} |
| 434 |
|
| 435 |
/** |
| 436 |
* Download, install and activates the theme |
| 437 |
* |
| 438 |
* @param array $query Parameter array containing the filesystem credentials entered by the user along with the theme name and slug |
| 439 |
* @return array Contains the result of the current process |
| 440 |
*/ |
| 441 |
public function install_activate_theme($query) { |
| 442 |
|
| 443 |
$fields = array('theme', 'slug'); |
| 444 |
$permissions = array('install_themes', 'switch_themes'); |
| 445 |
|
| 446 |
$error = $this->_validate_fields_and_capabilities($query, $fields, $permissions); |
| 447 |
if (!empty($error)) { |
| 448 |
return $error; |
| 449 |
} |
| 450 |
|
| 451 |
$this->_preload_credentials($query); |
| 452 |
|
| 453 |
$result = $this->_apply_theme_action('install', $query); |
| 454 |
if (!empty($result['installed']) && $result['installed']) { |
| 455 |
$result = $this->_apply_theme_action('activate', $query); |
| 456 |
if (empty($result['activated'])) { |
| 457 |
return $result; |
| 458 |
} |
| 459 |
} else { |
| 460 |
return $result; |
| 461 |
} |
| 462 |
|
| 463 |
return $this->_response($result); |
| 464 |
} |
| 465 |
|
| 466 |
/** |
| 467 |
* Download, install the theme |
| 468 |
* |
| 469 |
* @param array $query Parameter array containing the filesystem credentials entered by the user along with the theme name and slug |
| 470 |
* @return array Contains the result of the current process |
| 471 |
*/ |
| 472 |
public function install_theme($query) { |
| 473 |
|
| 474 |
$fields = array('theme', 'slug'); |
| 475 |
$permissions = array('install_themes'); |
| 476 |
|
| 477 |
$error = $this->_validate_fields_and_capabilities($query, $fields, $permissions); |
| 478 |
if (!empty($error)) { |
| 479 |
return $error; |
| 480 |
} |
| 481 |
|
| 482 |
$this->_preload_credentials($query); |
| 483 |
|
| 484 |
$result = $this->_apply_theme_action('install', $query); |
| 485 |
if (empty($result['installed'])) { |
| 486 |
return $result; |
| 487 |
} |
| 488 |
|
| 489 |
return $this->_response($result); |
| 490 |
} |
| 491 |
|
| 492 |
/** |
| 493 |
* Uninstall/delete the theme |
| 494 |
* |
| 495 |
* @param array $query Parameter array containing the filesystem credentials entered by the user along with the theme name and slug |
| 496 |
* @return array Contains the result of the current process |
| 497 |
*/ |
| 498 |
public function delete_theme($query) { |
| 499 |
|
| 500 |
$fields = array('theme'); |
| 501 |
$permissions = array('delete_themes'); |
| 502 |
|
| 503 |
$error = $this->_validate_fields_and_capabilities($query, $fields, $permissions); |
| 504 |
if (!empty($error)) { |
| 505 |
return $error; |
| 506 |
} |
| 507 |
|
| 508 |
$this->_preload_credentials($query); |
| 509 |
|
| 510 |
$info = $this->_get_theme_info($query['theme']); |
| 511 |
if ($info['installed']) { |
| 512 |
$deleted = delete_theme($info['slug']); |
| 513 |
|
| 514 |
if ($deleted) { |
| 515 |
$result = array('deleted' => true, 'info' => $this->_get_theme_info($query['theme']), 'last_state' => $info); |
| 516 |
} else { |
| 517 |
return $this->_generic_error_response('delete_theme_failed', array( |
| 518 |
'theme' => $query['theme'], |
| 519 |
'error_code' => 'delete_theme_failed', |
| 520 |
'info' => $info |
| 521 |
)); |
| 522 |
} |
| 523 |
} else { |
| 524 |
return $this->_generic_error_response('theme_not_installed', array( |
| 525 |
'theme' => $query['theme'], |
| 526 |
'error_code' => 'theme_not_installed', |
| 527 |
'info' => $info |
| 528 |
)); |
| 529 |
} |
| 530 |
|
| 531 |
return $this->_response($result); |
| 532 |
} |
| 533 |
|
| 534 |
/** |
| 535 |
* Updates/upgrade the theme |
| 536 |
* |
| 537 |
* @param array $query Parameter array containing the filesystem credentials entered by the user along with the theme name and slug |
| 538 |
* @return array Contains the result of the current process |
| 539 |
*/ |
| 540 |
public function update_theme($query) { |
| 541 |
|
| 542 |
$fields = array('theme'); |
| 543 |
$permissions = array('update_themes'); |
| 544 |
|
| 545 |
$error = $this->_validate_fields_and_capabilities($query, $fields, $permissions); |
| 546 |
if (!empty($error)) { |
| 547 |
return $error; |
| 548 |
} |
| 549 |
|
| 550 |
$this->_preload_credentials($query); |
| 551 |
|
| 552 |
// Make sure that we still have the theme installed before running |
| 553 |
// the update process |
| 554 |
$info = $this->_get_theme_info($query['theme']); |
| 555 |
if ($info['installed']) { |
| 556 |
// Load the updates command class if not existed |
| 557 |
if (!class_exists('UpdraftCentral_Updates_Commands')) include_once('updates.php'); |
| 558 |
$update_command = new UpdraftCentral_Updates_Commands($this->rc); |
| 559 |
|
| 560 |
$result = $update_command->update_theme($info['slug']); |
| 561 |
if (!empty($result['error'])) { |
| 562 |
$result['values'] = array('theme' => $query['theme'], 'info' => $info); |
| 563 |
} |
| 564 |
} else { |
| 565 |
return $this->_generic_error_response('theme_not_installed', array( |
| 566 |
'theme' => $query['theme'], |
| 567 |
'error_code' => 'theme_not_installed', |
| 568 |
'info' => $info |
| 569 |
)); |
| 570 |
} |
| 571 |
|
| 572 |
return $this->_response($result); |
| 573 |
} |
| 574 |
|
| 575 |
/** |
| 576 |
* Gets the theme information along with its active and install status |
| 577 |
* |
| 578 |
* @internal |
| 579 |
* @param array $theme The name of the theme to pull the information from |
| 580 |
* @return array Contains the theme information |
| 581 |
*/ |
| 582 |
private function _get_theme_info($theme) { |
| 583 |
|
| 584 |
$info = array( |
| 585 |
'active' => false, |
| 586 |
'installed' => false |
| 587 |
); |
| 588 |
|
| 589 |
// Clear theme cache so that newly installed/downloaded themes |
| 590 |
// gets reflected when calling "get_themes" |
| 591 |
if (function_exists('wp_clean_themes_cache')) { |
| 592 |
wp_clean_themes_cache(); |
| 593 |
} |
| 594 |
|
| 595 |
// Gets all themes available. |
| 596 |
$themes = wp_get_themes(); |
| 597 |
$current_theme_slug = basename(get_stylesheet_directory()); |
| 598 |
|
| 599 |
// Loops around each theme available. |
| 600 |
foreach ($themes as $slug => $value) { |
| 601 |
$name = $value->get('Name'); |
| 602 |
$theme_name = !empty($name) ? $name : $slug; |
| 603 |
|
| 604 |
// If the theme name matches that of the specified name, it will gather details. |
| 605 |
if ($theme_name === $theme) { |
| 606 |
$info['installed'] = true; |
| 607 |
$info['active'] = ($slug === $current_theme_slug) ? true : false; |
| 608 |
$info['slug'] = $slug; |
| 609 |
$info['data'] = $value; |
| 610 |
$info['name'] = $theme_name; |
| 611 |
break; |
| 612 |
} |
| 613 |
} |
| 614 |
|
| 615 |
return $info; |
| 616 |
} |
| 617 |
|
| 618 |
/** |
| 619 |
* Loads all available themes with additional attributes and settings needed by UpdraftCentral |
| 620 |
* |
| 621 |
* @param array $query Parameter array Any available parameters needed for this action |
| 622 |
* @return array Contains the result of the current process |
| 623 |
*/ |
| 624 |
public function load_themes($query) { |
| 625 |
|
| 626 |
$permissions = array('install_themes', 'switch_themes'); |
| 627 |
$args = array(); |
| 628 |
if (is_multisite() && !is_super_admin(get_current_user_id())) { |
| 629 |
$permissions = array('switch_themes'); |
| 630 |
$args = array('allowed' => true, 'blog_id' => get_current_blog_id()); |
| 631 |
} |
| 632 |
|
| 633 |
$error = $this->_validate_fields_and_capabilities($query, array(), $permissions); |
| 634 |
if (!empty($error)) { |
| 635 |
return $error; |
| 636 |
} |
| 637 |
|
| 638 |
$website = get_bloginfo('name'); |
| 639 |
$results = array(); |
| 640 |
|
| 641 |
// Load the updates command class if not existed |
| 642 |
if (!class_exists('UpdraftCentral_Updates_Commands')) include_once('updates.php'); |
| 643 |
$updates = new UpdraftCentral_Updates_Commands($this->rc); |
| 644 |
|
| 645 |
// Get themes for update |
| 646 |
$theme_updates = (array) $updates->get_item_updates('themes'); |
| 647 |
|
| 648 |
// Get all themes |
| 649 |
$themes = wp_get_themes($args); |
| 650 |
$current_theme_slug = basename(get_stylesheet_directory()); |
| 651 |
|
| 652 |
foreach ($themes as $slug => $value) { |
| 653 |
$name = $value->get('Name'); |
| 654 |
$theme_name = !empty($name) ? $name : $slug; |
| 655 |
|
| 656 |
$theme = new stdClass(); |
| 657 |
$theme->name = $theme_name; |
| 658 |
$theme->description = $value->get('Description'); |
| 659 |
$theme->slug = $slug; |
| 660 |
$theme->version = $value->get('Version'); |
| 661 |
$theme->author = $value->get('Author'); |
| 662 |
$theme->status = ($slug === $current_theme_slug) ? 'active' : 'inactive'; |
| 663 |
|
| 664 |
$template = $value->get('Template'); |
| 665 |
$theme->child_theme = !empty($template) ? true : false; |
| 666 |
$theme->website = $website; |
| 667 |
$theme->multisite = is_multisite(); |
| 668 |
$theme->site_url = trailingslashit(get_bloginfo('url')); |
| 669 |
|
| 670 |
if ($theme->child_theme) { |
| 671 |
$parent_theme = wp_get_theme($template); |
| 672 |
$parent_name = $parent_theme->get('Name'); |
| 673 |
|
| 674 |
$theme->parent = !empty($parent_name) ? $parent_name : $parent_theme->get_stylesheet(); |
| 675 |
} |
| 676 |
|
| 677 |
if (!empty($theme_updates[$slug])) { |
| 678 |
$update_info = $theme_updates[$slug]; |
| 679 |
|
| 680 |
if (version_compare($theme->version, $update_info->update['new_version'], '<')) { |
| 681 |
if (!empty($update_info->update['new_version'])) $theme->latest_version = $update_info->update['new_version']; |
| 682 |
if (!empty($update_info->update['package'])) $theme->download_link = $update_info->update['package']; |
| 683 |
} |
| 684 |
} |
| 685 |
|
| 686 |
if (empty($theme->short_description) && !empty($theme->description)) { |
| 687 |
// Only pull the first sentence as short description, it should be enough rather than displaying |
| 688 |
// an empty description or a full blown one which the user can access anytime if they press on |
| 689 |
// the view details link in UpdraftCentral. |
| 690 |
$temp = explode('.', $theme->description); |
| 691 |
$short_description = $temp[0]; |
| 692 |
|
| 693 |
// Adding the second sentence wouldn't hurt, in case the first sentence is too short. |
| 694 |
if (isset($temp[1])) $short_description .= '.'.$temp[1]; |
| 695 |
|
| 696 |
$theme->short_description = $short_description.'.'; |
| 697 |
} |
| 698 |
|
| 699 |
$results[] = $theme; |
| 700 |
} |
| 701 |
|
| 702 |
$result = array( |
| 703 |
'themes' => $results, |
| 704 |
'theme_updates' => $theme_updates, |
| 705 |
'is_super_admin' => is_super_admin(), |
| 706 |
); |
| 707 |
|
| 708 |
$result = array_merge($result, $this->_get_backup_credentials_settings(get_theme_root())); |
| 709 |
return $this->_response($result); |
| 710 |
} |
| 711 |
|
| 712 |
/** |
| 713 |
* Gets the backup and security credentials settings for this website |
| 714 |
* |
| 715 |
* @param array $query Parameter array Any available parameters needed for this action |
| 716 |
* @return array Contains the result of the current process |
| 717 |
*/ |
| 718 |
public function get_theme_requirements() { |
| 719 |
return $this->_response($this->_get_backup_credentials_settings(get_theme_root())); |
| 720 |
} |
| 721 |
} |
| 722 |
|