Skip to content

Commit

Permalink
Merge pull request #5537 from Automattic/add/install-theme-api-endpoint
Browse files Browse the repository at this point in the history
Add Themes New api endpoint.
  • Loading branch information
dereksmart committed Nov 8, 2016
2 parents 8be4793 + e754d24 commit c902e80
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 2 deletions.
6 changes: 6 additions & 0 deletions class.jetpack.php
Expand Up @@ -3097,6 +3097,12 @@ function upload_handler() {
'type' => $attachment->post_mime_type,
'meta' => wp_get_attachment_metadata( $attachment_id ),
);
// Zip files uploads are not supported unless they are done for installation purposed
// lets delete them in case something goes wrong in this whole process
if ( 'application/zip' === $attachment->post_mime_type ) {
// Schedule a cleanup for 2 hours from now in case of failed install.
wp_schedule_single_event( time() + 2 * HOUR_IN_SECONDS, 'upgrader_scheduled_cleanup', array( $attachment_id ) );
}
}
}
if ( ! is_null( $global_post ) ) {
Expand Down
1 change: 1 addition & 0 deletions class.json-api-endpoints.php
Expand Up @@ -384,6 +384,7 @@ function cast_and_filter_item( &$return, $type, $key, $value, $types = array(),
case 'safehtml' :
$return[$key] = wp_kses( (string) $value, wp_kses_allowed_html() );
break;
case 'zip' :
case 'media' :
if ( is_array( $value ) ) {
if ( isset( $value['name'] ) && is_array( $value['name'] ) ) {
Expand Down
Expand Up @@ -12,7 +12,7 @@ class Jetpack_JSON_API_Plugins_Install_Endpoint extends Jetpack_JSON_API_Plugins
protected function install() {
foreach ( $this->plugins as $index => $slug ) {

$skin = new Jetpack_Automatic_Plugin_Install_Skin();
$skin = new Jetpack_Automatic_Install_Skin();
$upgrader = new Plugin_Upgrader( $skin );
$zip_url = self::generate_wordpress_org_plugin_download_link( $slug );

Expand Down Expand Up @@ -98,7 +98,7 @@ protected static function get_slug_from_file_path( $plugin_file ) {
* Allows us to capture that the site doesn't have proper file system access.
* In order to update the plugin.
*/
class Jetpack_Automatic_Plugin_Install_Skin extends Automatic_Upgrader_Skin {
class Jetpack_Automatic_Install_Skin extends Automatic_Upgrader_Skin {
/**
* Stores the last error key;
**/
Expand Down
@@ -0,0 +1,82 @@
<?php

include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
include_once ABSPATH . 'wp-admin/includes/file.php';

class Jetpack_JSON_API_Themes_New_Endpoint extends Jetpack_JSON_API_Themes_Endpoint {

// POST /sites/%s/themes/%s/install
protected $needed_capabilities = 'install_themes';
protected $action = 'install';
protected $download_links = array();

protected function validate_call( $_blog_id, $capability, $check_manage_active = true ) {
$validate = parent::validate_call( $_blog_id, $capability, $check_manage_active );
if ( is_wp_error( $validate ) ) {
// Lets delete the attachment... if the user doesn't have the right permissions to do things.
$args = $this->input();
if ( isset( $args['zip'][0]['id'] ) ) {
wp_delete_attachment( $args['zip'][0]['id'], true );
}
}

return $validate;
}

protected function validate_input( $theme ) {
$this->bulk = false;
$this->themes = array();
}

function install() {
$args = $this->input();

if ( isset( $args['zip'][0]['id'] ) ) {
$attachment_id = $args['zip'][0]['id'];
$local_file = get_attached_file( $attachment_id );
if ( ! $local_file ) {
return new WP_Error( 'local-file-does-not-exist' );
}
$skin = new Jetpack_Automatic_Install_Skin();
$upgrader = new Theme_Upgrader( $skin );

$pre_install_list = wp_get_themes();
$result = $upgrader->install( $local_file );

// clean up.
wp_delete_attachment( $attachment_id, true );

if ( is_wp_error( $result ) ) {
return $result;
}

$after_install_list = wp_get_themes();
$plugin = array_values( array_diff( array_keys( $after_install_list ), array_keys( $pre_install_list ) ) );

if ( ! $result ) {
$error_code = $upgrader->skin->get_main_error_code();
$message = $upgrader->skin->get_main_error_message();
if ( empty( $message ) ) {
$message = __( 'An unknown error occurred during installation', 'jetpack' );
}

if ( 'download_failed' === $error_code ) {
$error_code = 'no_package';
}

return new WP_Error( $error_code, $message, 400 );
}

if ( empty( $plugin ) ) {
return new WP_Error( 'theme_already_installed' );
}

$this->themes = $plugin;
$this->log[ $plugin[0] ] = $upgrader->skin->get_upgrade_messages();

return true;
}

return new WP_Error( 'no_theme_installed' );
}
}
26 changes: 26 additions & 0 deletions json-endpoints/jetpack/json-api-jetpack-endpoints.php
Expand Up @@ -75,6 +75,32 @@
) );

require_once( $json_jetpack_endpoints_dir . 'class.jetpack-json-api-themes-get-endpoint.php' );
require_once( $json_jetpack_endpoints_dir . 'class.jetpack-json-api-themes-new-endpoint.php' );

// POST /sites/%s/themes/%new
new Jetpack_JSON_API_Themes_new_Endpoint( array(
'description' => 'Install a theme to your jetpack blog',
'group' => '__do_not_document',
'stat' => 'themes:new',
'method' => 'POST',
'path' => '/sites/%s/themes/new',
'path_labels' => array(
'$site' => '(int|string) The site ID, The site domain',
),
'request_format' => array(
'zip' => '(zip) Theme package zip file. multipart/form-data encoded. ',
),
'response_format' => Jetpack_JSON_API_Themes_Endpoint::$_response_format,
'example_request_data' => array(
'headers' => array(
'authorization' => 'Bearer YOUR_API_TOKEN'
),
),
'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/example.wordpress.org/themes/new'
) );



new Jetpack_JSON_API_Themes_Get_Endpoint( array(
'description' => 'Get a single theme on a jetpack blog',
'group' => '__do_not_document',
Expand Down

0 comments on commit c902e80

Please sign in to comment.