| 1 |
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 |
|
| 3 |
/** |
| 4 |
* Themes delete endpoint class. |
| 5 |
* POST /sites/%s/plugins/%s/delete |
| 6 |
*/ |
| 7 |
class Jetpack_JSON_API_Themes_Delete_Endpoint extends Jetpack_JSON_API_Themes_Endpoint { |
| 8 |
|
| 9 |
/** |
| 10 |
* Needed capabilities. |
| 11 |
* |
| 12 |
* @var string |
| 13 |
*/ |
| 14 |
protected $needed_capabilities = 'delete_themes'; |
| 15 |
|
| 16 |
/** |
| 17 |
* The action. |
| 18 |
* |
| 19 |
* @var string |
| 20 |
*/ |
| 21 |
protected $action = 'delete'; |
| 22 |
|
| 23 |
/** |
| 24 |
* Delete the theme. |
| 25 |
* |
| 26 |
* @return bool|WP_Error |
| 27 |
*/ |
| 28 |
protected function delete() { |
| 29 |
|
| 30 |
foreach ( $this->themes as $theme ) { |
| 31 |
|
| 32 |
// Don't delete an active child theme |
| 33 |
if ( is_child_theme() && $theme === get_stylesheet() ) { |
| 34 |
$error = 'You cannot delete a theme while it is active on the main site.'; |
| 35 |
$this->log[ $theme ]['error'] = $error; |
| 36 |
continue; |
| 37 |
} |
| 38 |
|
| 39 |
if ( $theme === get_template() ) { |
| 40 |
$error = 'You cannot delete a theme while it is active on the main site.'; |
| 41 |
$this->log[ $theme ]['error'] = $error; |
| 42 |
continue; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Filters whether to use an alternative process for deleting a WordPress.com theme. |
| 47 |
* The alternative process can be executed during the filter. |
| 48 |
* |
| 49 |
* The filter can also return an instance of WP_Error; in which case the endpoint response will |
| 50 |
* contain this error. |
| 51 |
* |
| 52 |
* @module json-api |
| 53 |
* |
| 54 |
* @since 4.4.2 |
| 55 |
* |
| 56 |
* @param bool $use_alternative_delete_method Whether to use the alternative method of deleting |
| 57 |
* a WPCom theme. |
| 58 |
* @param string $theme_slug Theme name (slug). If it is a WPCom theme, |
| 59 |
* it should be suffixed with `-wpcom`. |
| 60 |
*/ |
| 61 |
$result = apply_filters( 'jetpack_wpcom_theme_delete', false, $theme ); |
| 62 |
|
| 63 |
if ( ! $result ) { |
| 64 |
$result = delete_theme( $theme ); |
| 65 |
} |
| 66 |
|
| 67 |
if ( is_wp_error( $result ) ) { |
| 68 |
$error = $result->get_error_messages(); |
| 69 |
$this->log[ $theme ]['error'] = $error; |
| 70 |
} else { |
| 71 |
$this->log[ $theme ][] = 'Theme deleted'; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
if ( ! $this->bulk && isset( $error ) ) { |
| 76 |
return new WP_Error( 'delete_theme_error', $error, 400 ); |
| 77 |
} |
| 78 |
|
| 79 |
return true; |
| 80 |
} |
| 81 |
|
| 82 |
} |
| 83 |
|