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