Themes.php
| 1 | <?php |
| 2 | /** |
| 3 | * REST API Themes Controller |
| 4 | * |
| 5 | * Handles requests to /themes |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\WooCommerce\Admin\API; |
| 9 | |
| 10 | defined( 'ABSPATH' ) || exit; |
| 11 | |
| 12 | use Automattic\WooCommerce\Admin\Overrides\ThemeUpgrader; |
| 13 | use Automattic\WooCommerce\Admin\Overrides\ThemeUpgraderSkin; |
| 14 | |
| 15 | /** |
| 16 | * Themes controller. |
| 17 | * |
| 18 | * @internal |
| 19 | * @extends WC_REST_Data_Controller |
| 20 | */ |
| 21 | class Themes extends \WC_REST_Data_Controller { |
| 22 | /** |
| 23 | * Endpoint namespace. |
| 24 | * |
| 25 | * @var string |
| 26 | */ |
| 27 | protected $namespace = 'wc-admin'; |
| 28 | |
| 29 | /** |
| 30 | * Route base. |
| 31 | * |
| 32 | * @var string |
| 33 | */ |
| 34 | protected $rest_base = 'themes'; |
| 35 | |
| 36 | /** |
| 37 | * Register routes. |
| 38 | */ |
| 39 | public function register_routes() { |
| 40 | register_rest_route( |
| 41 | $this->namespace, |
| 42 | '/' . $this->rest_base, |
| 43 | array( |
| 44 | array( |
| 45 | 'methods' => \WP_REST_Server::EDITABLE, |
| 46 | 'callback' => array( $this, 'upload_theme' ), |
| 47 | 'permission_callback' => array( $this, 'upload_theme_permissions_check' ), |
| 48 | 'args' => $this->get_collection_params(), |
| 49 | ), |
| 50 | 'schema' => array( $this, 'get_public_item_schema' ), |
| 51 | ) |
| 52 | ); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Check whether a given request has permission to edit upload plugins/themes. |
| 57 | * |
| 58 | * @param WP_REST_Request $request Full details about the request. |
| 59 | * @return WP_Error|boolean |
| 60 | */ |
| 61 | public function upload_theme_permissions_check( $request ) { |
| 62 | if ( ! current_user_can( 'upload_themes' ) ) { |
| 63 | return new \WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you are not allowed to install themes on this site.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); |
| 64 | } |
| 65 | |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Upload and install a theme. |
| 71 | * |
| 72 | * @param WP_REST_Request $request Request data. |
| 73 | * @return WP_Error|WP_REST_Response |
| 74 | */ |
| 75 | public function upload_theme( $request ) { |
| 76 | if ( |
| 77 | ! isset( $_FILES['pluginzip'] ) || ! isset( $_FILES['pluginzip']['tmp_name'] ) || ! is_uploaded_file( $_FILES['pluginzip']['tmp_name'] ) || ! is_file( $_FILES['pluginzip']['tmp_name'] ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 78 | return new \WP_Error( 'woocommerce_rest_invalid_file', __( 'Specified file failed upload test.', 'woocommerce' ) ); |
| 79 | } |
| 80 | |
| 81 | include_once ABSPATH . 'wp-admin/includes/file.php'; |
| 82 | include_once ABSPATH . '/wp-admin/includes/admin.php'; |
| 83 | include_once ABSPATH . '/wp-admin/includes/theme-install.php'; |
| 84 | include_once ABSPATH . '/wp-admin/includes/theme.php'; |
| 85 | include_once ABSPATH . '/wp-admin/includes/class-wp-upgrader.php'; |
| 86 | include_once ABSPATH . '/wp-admin/includes/class-theme-upgrader.php'; |
| 87 | |
| 88 | $_GET['package'] = true; |
| 89 | $file_upload = new \File_Upload_Upgrader( 'pluginzip', 'package' ); |
| 90 | $upgrader = new ThemeUpgrader( new ThemeUpgraderSkin() ); |
| 91 | $install = $upgrader->install( $file_upload->package ); |
| 92 | |
| 93 | if ( $install || is_wp_error( $install ) ) { |
| 94 | $file_upload->cleanup(); |
| 95 | } |
| 96 | |
| 97 | if ( ! is_wp_error( $install ) && isset( $install['destination_name'] ) ) { |
| 98 | $theme = $install['destination_name']; |
| 99 | $result = array( |
| 100 | 'status' => 'success', |
| 101 | 'message' => $upgrader->strings['process_success'], |
| 102 | 'theme' => $theme, |
| 103 | ); |
| 104 | |
| 105 | /** |
| 106 | * Fires when a theme is successfully installed. |
| 107 | * |
| 108 | * @param string $theme The theme name. |
| 109 | */ |
| 110 | do_action( 'woocommerce_theme_installed', $theme ); |
| 111 | } else { |
| 112 | if ( is_wp_error( $install ) && $install->get_error_code() ) { |
| 113 | $error_message = isset( $upgrader->strings[ $install->get_error_code() ] ) ? $upgrader->strings[ $install->get_error_code() ] : $install->get_error_data(); |
| 114 | } else { |
| 115 | $error_message = $upgrader->strings['process_failed']; |
| 116 | } |
| 117 | |
| 118 | $result = array( |
| 119 | 'status' => 'error', |
| 120 | 'message' => $error_message, |
| 121 | ); |
| 122 | } |
| 123 | |
| 124 | $response = $this->prepare_item_for_response( $result, $request ); |
| 125 | $data = $this->prepare_response_for_collection( $response ); |
| 126 | |
| 127 | return rest_ensure_response( $data ); |
| 128 | } |
| 129 | |
| 130 | |
| 131 | /** |
| 132 | * Prepare the data object for response. |
| 133 | * |
| 134 | * @param object $item Data object. |
| 135 | * @param WP_REST_Request $request Request object. |
| 136 | * @return WP_REST_Response $response Response data. |
| 137 | */ |
| 138 | public function prepare_item_for_response( $item, $request ) { |
| 139 | $data = $this->add_additional_fields_to_object( $item, $request ); |
| 140 | $data = $this->filter_response_by_context( $data, 'view' ); |
| 141 | $response = rest_ensure_response( $data ); |
| 142 | |
| 143 | /** |
| 144 | * Filter the list returned from the API. |
| 145 | * |
| 146 | * @param WP_REST_Response $response The response object. |
| 147 | * @param array $item The original item. |
| 148 | * @param WP_REST_Request $request Request used to generate the response. |
| 149 | */ |
| 150 | return apply_filters( 'woocommerce_rest_prepare_themes', $response, $item, $request ); |
| 151 | } |
| 152 | |
| 153 | |
| 154 | /** |
| 155 | * Get the schema, conforming to JSON Schema. |
| 156 | * |
| 157 | * @return array |
| 158 | */ |
| 159 | public function get_item_schema() { |
| 160 | $schema = array( |
| 161 | '$schema' => 'http://json-schema.org/draft-04/schema#', |
| 162 | 'title' => 'upload_theme', |
| 163 | 'type' => 'object', |
| 164 | 'properties' => array( |
| 165 | 'status' => array( |
| 166 | 'description' => __( 'Theme installation status.', 'woocommerce' ), |
| 167 | 'type' => 'string', |
| 168 | 'context' => array( 'view', 'edit' ), |
| 169 | 'readonly' => true, |
| 170 | ), |
| 171 | 'message' => array( |
| 172 | 'description' => __( 'Theme installation message.', 'woocommerce' ), |
| 173 | 'type' => 'string', |
| 174 | 'context' => array( 'view', 'edit' ), |
| 175 | 'readonly' => true, |
| 176 | ), |
| 177 | 'theme' => array( |
| 178 | 'description' => __( 'Uploaded theme.', 'woocommerce' ), |
| 179 | 'type' => 'object', |
| 180 | 'context' => array( 'view', 'edit' ), |
| 181 | 'readonly' => true, |
| 182 | ), |
| 183 | ), |
| 184 | ); |
| 185 | |
| 186 | return $this->add_additional_fields_schema( $schema ); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Get the query params for collections. |
| 191 | * |
| 192 | * @return array |
| 193 | */ |
| 194 | public function get_collection_params() { |
| 195 | $params = array( 'context' => $this->get_context_param( array( 'default' => 'view' ) ) ); |
| 196 | $params['pluginzip'] = array( |
| 197 | 'description' => __( 'A zip file of the theme to be uploaded.', 'woocommerce' ), |
| 198 | 'type' => 'file', |
| 199 | 'validate_callback' => 'rest_validate_request_arg', |
| 200 | ); |
| 201 | |
| 202 | return apply_filters( 'woocommerce_rest_themes_collection_params', $params ); |
| 203 | } |
| 204 | } |
| 205 |