| 1 |
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 |
|
| 3 |
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 4 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 5 |
|
| 6 |
use Automattic\Jetpack\Automatic_Install_Skin; |
| 7 |
use Automattic\Jetpack\Connection\Client; |
| 8 |
|
| 9 |
/** |
| 10 |
* Themes install endpoint class. |
| 11 |
* |
| 12 |
* POST /sites/%s/themes/%s/install |
| 13 |
*/ |
| 14 |
class Jetpack_JSON_API_Themes_Install_Endpoint extends Jetpack_JSON_API_Themes_Endpoint { |
| 15 |
|
| 16 |
/** |
| 17 |
* Needed capabilities. |
| 18 |
* |
| 19 |
* @var string |
| 20 |
*/ |
| 21 |
protected $needed_capabilities = 'install_themes'; |
| 22 |
|
| 23 |
/** |
| 24 |
* The action. |
| 25 |
* |
| 26 |
* @var string |
| 27 |
*/ |
| 28 |
protected $action = 'install'; |
| 29 |
|
| 30 |
/** |
| 31 |
* Download links. |
| 32 |
* |
| 33 |
* @var array |
| 34 |
*/ |
| 35 |
protected $download_links = array(); |
| 36 |
|
| 37 |
/** |
| 38 |
* Install the theme. |
| 39 |
* |
| 40 |
* @return bool|WP_Error |
| 41 |
*/ |
| 42 |
protected function install() { |
| 43 |
|
| 44 |
foreach ( $this->themes as $theme ) { |
| 45 |
|
| 46 |
/** |
| 47 |
* Filters whether to use an alternative process for installing a WordPress.com theme. |
| 48 |
* The alternative process can be executed during the filter. |
| 49 |
* |
| 50 |
* The filter can also return an instance of WP_Error; in which case the endpoint response will |
| 51 |
* contain this error. |
| 52 |
* |
| 53 |
* @module json-api |
| 54 |
* |
| 55 |
* @since 4.4.2 |
| 56 |
* |
| 57 |
* @param bool $use_alternative_install_method Whether to use the alternative method of installing |
| 58 |
* a WPCom theme. |
| 59 |
* @param string $theme_slug Theme name (slug). If it is a WPCom theme, |
| 60 |
* it should be suffixed with `-wpcom`. |
| 61 |
*/ |
| 62 |
$result = apply_filters( 'jetpack_wpcom_theme_install', false, $theme ); |
| 63 |
|
| 64 |
$skin = null; |
| 65 |
$upgrader = null; |
| 66 |
$link = null; |
| 67 |
|
| 68 |
// If the alternative install method was not used, use the standard method. |
| 69 |
if ( ! $result ) { |
| 70 |
$skin = new Automatic_Install_Skin(); |
| 71 |
$upgrader = new Theme_Upgrader( $skin ); |
| 72 |
|
| 73 |
$link = $this->download_links[ $theme ]; |
| 74 |
$result = $upgrader->install( $link ); |
| 75 |
} |
| 76 |
|
| 77 |
if ( file_exists( $link ) ) { |
| 78 |
// Delete if link was tmp local file |
| 79 |
wp_delete_file( $link ); |
| 80 |
} |
| 81 |
|
| 82 |
if ( ! $this->bulk && is_wp_error( $result ) ) { |
| 83 |
return $result; |
| 84 |
} |
| 85 |
|
| 86 |
if ( ! $result ) { |
| 87 |
$error = __( 'An unknown error occurred during installation', 'jetpack' ); |
| 88 |
$this->log[ $theme ]['error'] = $error; |
| 89 |
} elseif ( ! self::is_installed_theme( $theme ) ) { |
| 90 |
$error = __( 'There was an error installing your theme', 'jetpack' ); |
| 91 |
$this->log[ $theme ]['error'] = $error; |
| 92 |
} elseif ( $upgrader ) { |
| 93 |
$this->log[ $theme ][] = $upgrader->skin->get_upgrade_messages(); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
if ( ! $this->bulk && isset( $error ) ) { |
| 98 |
return new WP_Error( 'install_error', $error, 400 ); |
| 99 |
} |
| 100 |
|
| 101 |
return true; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Validate the themes. |
| 106 |
* |
| 107 |
* @return bool|WP_Error |
| 108 |
*/ |
| 109 |
protected function validate_themes() { |
| 110 |
if ( empty( $this->themes ) || ! is_array( $this->themes ) ) { |
| 111 |
return new WP_Error( 'missing_themes', __( 'No themes found.', 'jetpack' ) ); |
| 112 |
} |
| 113 |
foreach ( $this->themes as $theme ) { |
| 114 |
|
| 115 |
if ( self::is_installed_theme( $theme ) ) { |
| 116 |
return new WP_Error( 'theme_already_installed', __( 'The theme is already installed', 'jetpack' ) ); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Filters whether to skip the standard method of downloading and validating a WordPress.com |
| 121 |
* theme. An alternative method of WPCom theme download and validation can be |
| 122 |
* executed during the filter. |
| 123 |
* |
| 124 |
* The filter can also return an instance of WP_Error; in which case the endpoint response will |
| 125 |
* contain this error. |
| 126 |
* |
| 127 |
* @module json-api |
| 128 |
* |
| 129 |
* @since 4.4.2 |
| 130 |
* |
| 131 |
* @param bool $skip_download_filter_result Whether to skip the standard method of downloading |
| 132 |
* and validating a WPCom theme. |
| 133 |
* @param string $theme_slug Theme name (slug). If it is a WPCom theme, |
| 134 |
* it should be suffixed with `-wpcom`. |
| 135 |
*/ |
| 136 |
$skip_download_filter_result = apply_filters( 'jetpack_wpcom_theme_skip_download', false, $theme ); |
| 137 |
|
| 138 |
if ( is_wp_error( $skip_download_filter_result ) ) { |
| 139 |
return $skip_download_filter_result; |
| 140 |
} elseif ( $skip_download_filter_result ) { |
| 141 |
continue; |
| 142 |
} |
| 143 |
|
| 144 |
if ( wp_endswith( $theme, '-wpcom' ) ) { |
| 145 |
$file = self::download_wpcom_theme_to_file( $theme ); |
| 146 |
|
| 147 |
if ( is_wp_error( $file ) ) { |
| 148 |
return $file; |
| 149 |
} |
| 150 |
|
| 151 |
$this->download_links[ $theme ] = $file; |
| 152 |
continue; |
| 153 |
} |
| 154 |
|
| 155 |
$params = (object) array( 'slug' => $theme ); |
| 156 |
$url = 'https://api.wordpress.org/themes/info/1.0/'; // @todo Switch to https://api.wordpress.org/themes/info/1.1/, which uses JSON rather than PHP serialization. |
| 157 |
$args = array( |
| 158 |
'body' => array( |
| 159 |
'action' => 'theme_information', |
| 160 |
'request' => serialize( $params ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize |
| 161 |
), |
| 162 |
); |
| 163 |
$response = wp_remote_post( $url, $args ); |
| 164 |
$theme_data = unserialize( $response['body'] ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_unserialize |
| 165 |
if ( is_wp_error( $theme_data ) ) { |
| 166 |
return $theme_data; |
| 167 |
} |
| 168 |
|
| 169 |
if ( ! is_object( $theme_data ) && ! isset( $theme_data->download_link ) ) { |
| 170 |
return new WP_Error( 'theme_not_found', __( 'This theme does not exist', 'jetpack' ), 404 ); |
| 171 |
} |
| 172 |
|
| 173 |
$this->download_links[ $theme ] = $theme_data->download_link; |
| 174 |
|
| 175 |
} |
| 176 |
return true; |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Check if the theme is installed. |
| 181 |
* |
| 182 |
* @param string $theme - the theme we're checking. |
| 183 |
* |
| 184 |
* @return bool |
| 185 |
*/ |
| 186 |
protected static function is_installed_theme( $theme ) { |
| 187 |
$wp_theme = wp_get_theme( $theme ); |
| 188 |
return $wp_theme->exists(); |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Download the wpcom theme. |
| 193 |
* |
| 194 |
* @param string $theme - the theme to download. |
| 195 |
* |
| 196 |
* @return string|WP_Error |
| 197 |
*/ |
| 198 |
protected static function download_wpcom_theme_to_file( $theme ) { |
| 199 |
|
| 200 |
$file = wp_tempnam( 'theme' ); |
| 201 |
if ( ! $file ) { |
| 202 |
return new WP_Error( 'problem_creating_theme_file', __( 'Problem creating file for theme download', 'jetpack' ) ); |
| 203 |
} |
| 204 |
|
| 205 |
$url = "themes/download/$theme.zip"; |
| 206 |
$args = array( |
| 207 |
'stream' => true, |
| 208 |
'filename' => $file, |
| 209 |
); |
| 210 |
$result = Client::wpcom_json_api_request_as_blog( $url, '1.1', $args ); |
| 211 |
|
| 212 |
$response = $result['response']; |
| 213 |
if ( $response['code'] !== 200 ) { |
| 214 |
wp_delete_file( $file ); |
| 215 |
return new WP_Error( 'problem_fetching_theme', __( 'Problem downloading theme', 'jetpack' ) ); |
| 216 |
} |
| 217 |
|
| 218 |
return $file; |
| 219 |
} |
| 220 |
} |
| 221 |
|