| 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 |
|
| 8 |
/** |
| 9 |
* Themes new endpoint class. |
| 10 |
* |
| 11 |
* /sites/%s/themes/%s/install |
| 12 |
*/ |
| 13 |
class Jetpack_JSON_API_Themes_New_Endpoint extends Jetpack_JSON_API_Themes_Endpoint { |
| 14 |
|
| 15 |
/** |
| 16 |
* Needed capabilities. |
| 17 |
* |
| 18 |
* @var string |
| 19 |
*/ |
| 20 |
protected $needed_capabilities = 'install_themes'; |
| 21 |
|
| 22 |
/** |
| 23 |
* Action. |
| 24 |
* |
| 25 |
* @var string |
| 26 |
*/ |
| 27 |
protected $action = 'install'; |
| 28 |
|
| 29 |
/** |
| 30 |
* Download links. |
| 31 |
* |
| 32 |
* @var array |
| 33 |
*/ |
| 34 |
protected $download_links = array(); |
| 35 |
|
| 36 |
/** |
| 37 |
* Validate the call. |
| 38 |
* |
| 39 |
* @param int $_blog_id - the blod ID. |
| 40 |
* @param string $capability - the capability we're checking. |
| 41 |
* @param bool $check_manage_active - if managing capabilities is active. |
| 42 |
* |
| 43 |
* @return bool|WP_Error |
| 44 |
*/ |
| 45 |
protected function validate_call( $_blog_id, $capability, $check_manage_active = true ) { |
| 46 |
$validate = parent::validate_call( $_blog_id, $capability, $check_manage_active ); |
| 47 |
if ( is_wp_error( $validate ) ) { |
| 48 |
// Lets delete the attachment... if the user doesn't have the right permissions to do things. |
| 49 |
$args = $this->input(); |
| 50 |
if ( isset( $args['zip'][0]['id'] ) ) { |
| 51 |
wp_delete_attachment( $args['zip'][0]['id'], true ); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
return $validate; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Validate the input. |
| 60 |
* |
| 61 |
* @param string $theme - the theme. |
| 62 |
*/ |
| 63 |
protected function validate_input( $theme ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 64 |
$this->bulk = false; |
| 65 |
$this->themes = array(); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Install the theme. |
| 70 |
* |
| 71 |
* @return bool |
| 72 |
*/ |
| 73 |
public function install() { |
| 74 |
$args = $this->input(); |
| 75 |
|
| 76 |
if ( isset( $args['zip'][0]['id'] ) ) { |
| 77 |
$attachment_id = $args['zip'][0]['id']; |
| 78 |
$local_file = get_attached_file( $attachment_id ); |
| 79 |
if ( ! $local_file ) { |
| 80 |
return new WP_Error( 'local-file-does-not-exist' ); |
| 81 |
} |
| 82 |
$skin = new Automatic_Install_Skin(); |
| 83 |
$upgrader = new Theme_Upgrader( $skin ); |
| 84 |
|
| 85 |
$pre_install_list = wp_get_themes(); |
| 86 |
$result = $upgrader->install( $local_file ); |
| 87 |
|
| 88 |
// clean up. |
| 89 |
wp_delete_attachment( $attachment_id, true ); |
| 90 |
|
| 91 |
if ( is_wp_error( $result ) ) { |
| 92 |
return $result; |
| 93 |
} |
| 94 |
|
| 95 |
$after_install_list = wp_get_themes(); |
| 96 |
$plugin = array_values( array_diff( array_keys( $after_install_list ), array_keys( $pre_install_list ) ) ); |
| 97 |
|
| 98 |
if ( ! $result ) { |
| 99 |
$error_code = $upgrader->skin->get_main_error_code(); |
| 100 |
$message = $upgrader->skin->get_main_error_message(); |
| 101 |
if ( empty( $message ) ) { |
| 102 |
$message = __( 'An unknown error occurred during installation', 'jetpack' ); |
| 103 |
} |
| 104 |
|
| 105 |
if ( 'download_failed' === $error_code ) { |
| 106 |
$error_code = 'no_package'; |
| 107 |
} |
| 108 |
|
| 109 |
return new WP_Error( $error_code, $message, 400 ); |
| 110 |
} |
| 111 |
|
| 112 |
if ( empty( $plugin ) ) { |
| 113 |
return new WP_Error( 'theme_already_installed' ); |
| 114 |
} |
| 115 |
|
| 116 |
$this->themes = $plugin; |
| 117 |
$this->log[ $plugin[0] ] = $upgrader->skin->get_upgrade_messages(); |
| 118 |
|
| 119 |
return true; |
| 120 |
} |
| 121 |
|
| 122 |
return new WP_Error( 'no_theme_installed' ); |
| 123 |
} |
| 124 |
} |
| 125 |
|