Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API: Install .com themes by using -wpcom suffix #5392

Merged
merged 13 commits into from
Nov 8, 2016
8 changes: 7 additions & 1 deletion class.jetpack-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public static function remote_request( $args, $body = null ) {
'timeout' => 10,
'redirection' => 0,
'headers' => array(),
'stream' => false,
'filename' => null,
Copy link
Contributor Author

@seear seear Nov 8, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See wp_remote_request() defaults

);

$args = wp_parse_args( $args, $defaults );
Expand All @@ -38,8 +40,10 @@ public static function remote_request( $args, $body = null ) {
$timeout = intval( $args['timeout'] );

$redirection = $args['redirection'];
$stream = $args['stream'];
$filename = $args['filename'];

$request = compact( 'method', 'body', 'timeout', 'redirection' );
$request = compact( 'method', 'body', 'timeout', 'redirection', 'stream', 'filename' );

@list( $token_key, $secret ) = explode( '.', $token->secret );
if ( empty( $token ) || empty( $secret ) ) {
Expand Down Expand Up @@ -270,6 +274,8 @@ static function wpcom_json_api_request_as_blog( $path, $version = self::WPCOM_JS
'method' => 'string',
'timeout' => 'int',
'redirection' => 'int',
'stream' => 'boolean',
'filename' => 'string',
) );

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,16 @@ protected function install() {

foreach ( $this->themes as $theme ) {

$skin = new Automatic_Upgrader_Skin();
$skin = new Jetpack_Automatic_Install_Skin();
$upgrader = new Theme_Upgrader( $skin );

$result = $upgrader->install( $this->download_links[ $theme ] );
$link = $this->download_links[ $theme ];
$result = $upgrader->install( $link );

if ( file_exists( $link ) ) {
// Delete if link was tmp local file
unlink( $link );
}

if ( ! $this->bulk && is_wp_error( $result ) ) {
return $result;
Expand Down Expand Up @@ -53,6 +59,16 @@ protected function validate_themes() {
return new WP_Error( 'theme_already_installed', __( 'The theme is already installed', 'jetpack' ) );
}

if ( wp_endswith( $theme, '-wpcom' ) ) {
$file = self::download_wpcom_theme_to_file( $theme );
if ( is_wp_error( $file ) ) {
return $file;
}

$this->download_links[ $theme ] = $file;
continue;
}

$params = (object) array( 'slug' => $theme );
$url = 'https://api.wordpress.org/themes/info/1.0/';
$args = array(
Expand All @@ -66,6 +82,11 @@ protected function validate_themes() {
if ( is_wp_error( $theme_data ) ) {
return $theme_data;
}

if ( ! is_object( $theme_data ) && !isset( $theme_data->download_link ) ) {
return new WP_Error( 'theme_not_found', __( 'This theme does not exits') , 404 );
}

$this->download_links[ $theme ] = $theme_data->download_link;

}
Expand All @@ -77,6 +98,24 @@ protected static function is_installed_theme( $theme ) {
return $wp_theme->exists();
}

protected static function download_wpcom_theme_to_file( $theme ) {
$wpcom_theme_slug = preg_replace( '/-wpcom$/', '', $theme );

}
$file = wp_tempnam( 'theme' );
if ( ! $file ) {
return new WP_Error( 'problem_creating_theme_file', __( 'Problem creating file for theme download', 'jetpack' ) );
}

$url = "themes/download/$theme.zip";
$args = array( 'stream' => true, 'filename' => $file );
$result = Jetpack_Client::wpcom_json_api_request_as_blog( $url, '1.1', $args );

$response = $result[ 'response' ];
if ( $response[ 'code' ] !== 200 ) {
unlink( $file );
return new WP_Error( 'problem_fetching_theme', __( 'Problem downloading theme' ) );
}

return $file;
}
}