Completed
Push — add/wpcom-shortcodes ( 38c379...fb6212 )
by
unknown
62:13 queued 53:42
created

install()   C

Complexity

Conditions 11
Paths 38

Size

Total Lines 63
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 24
nc 38
nop 0
dl 0
loc 63
rs 6.1137
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
4
include_once ABSPATH . 'wp-admin/includes/file.php';
5
6
class Jetpack_JSON_API_Themes_Install_Endpoint extends Jetpack_JSON_API_Themes_Endpoint {
7
8
	// POST  /sites/%s/themes/%s/install
9
	protected $needed_capabilities = 'install_themes';
10
	protected $action              = 'install';
11
	protected $download_links      = array();
12
13
	protected function install() {
14
15
		foreach ( $this->themes as $theme ) {
16
17
			/**
18
			 * Filters whether to use an alternative process for installing a WordPress.com theme.
19
			 * The alternative process can be executed during the filter.
20
			 *
21
			 * The filter can also return an instance of WP_Error; in which case the endpoint response will
22
			 * contain this error.
23
			 *
24
			 * @module json-api
25
			 *
26
			 * @since 4.4.2
27
			 *
28
			 * @param bool   $use_alternative_install_method Whether to use the alternative method of installing
29
			 *                                               a WPCom theme.
30
			 * @param string $theme_slug                     Theme name (slug). If it is a WPCom theme,
31
			 *                                               it should be suffixed with `-wpcom`.
32
			 */
33
			$result = apply_filters( 'jetpack_wpcom_theme_install', false, $theme );
34
35
			$skin = null;
0 ignored issues
show
Unused Code introduced by
$skin is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
36
			$upgrader = null;
37
			$link = null;
38
39
			// If the alternative install method was not used, use the standard method.
40
			if ( ! $result ) {
41
				$skin     = new Jetpack_Automatic_Install_Skin();
42
				$upgrader = new Theme_Upgrader( $skin );
43
44
				$link   = $this->download_links[ $theme ];
45
				$result = $upgrader->install( $link );
46
			}
47
48
			if ( file_exists( $link ) ) {
49
				// Delete if link was tmp local file
50
				unlink( $link );
51
			}
52
53
			if ( ! $this->bulk && is_wp_error( $result ) ) {
54
				return $result;
55
			}
56
57
			if ( ! $result ) {
58
				$error = $this->log[ $theme ]['error'] = __( 'An unknown error occurred during installation', 'jetpack' );
59
			}
60
61
			elseif ( ! self::is_installed_theme( $theme ) ) {
62
				$error = $this->log[ $theme ]['error'] = __( 'There was an error installing your theme', 'jetpack' );
63
			}
64
65
			elseif ( $upgrader ) {
66
				$this->log[ $theme ][] = $upgrader->skin->get_upgrade_messages();
67
			}
68
		}
69
70
		if ( ! $this->bulk && isset( $error ) ) {
71
			return  new WP_Error( 'install_error', $error, 400 );
72
		}
73
74
		return true;
75
	}
76
77
	protected function validate_themes() {
78 View Code Duplication
		if ( empty( $this->themes ) || ! is_array( $this->themes ) ) {
79
			return new WP_Error( 'missing_themes', __( 'No themes found.', 'jetpack' ) );
80
		}
81
		foreach( $this->themes as $index => $theme ) {
82
83
			if ( self::is_installed_theme( $theme ) ) {
84
				return new WP_Error( 'theme_already_installed', __( 'The theme is already installed', 'jetpack' ) );
85
			}
86
87
			/**
88
			 * Filters whether to skip the standard method of downloading and validating a WordPress.com
89
			 * theme. An alternative method of WPCom theme download and validation can be
90
			 * executed during the filter.
91
			 *
92
			 * The filter can also return an instance of WP_Error; in which case the endpoint response will
93
			 * contain this error.
94
			 *
95
			 * @module json-api
96
			 *
97
			 * @since 4.4.2
98
			 *
99
			 * @param bool   $skip_download_filter_result Whether to skip the standard method of downloading
100
			 *                                            and validating a WPCom theme.
101
			 * @param string $theme_slug                  Theme name (slug). If it is a WPCom theme,
102
			 *                                            it should be suffixed with `-wpcom`.
103
			 */
104
			$skip_download_filter_result = apply_filters( 'jetpack_wpcom_theme_skip_download', false, $theme );
105
106
			if ( is_wp_error( $skip_download_filter_result ) ) {
107
				return $skip_download_filter_result;
108
			} elseif ( $skip_download_filter_result ) {
109
				continue;
110
			}
111
112
			if ( wp_endswith( $theme, '-wpcom' ) ) {
113
				$file = self::download_wpcom_theme_to_file( $theme );
114
115
				if ( is_wp_error( $file ) ) {
116
					return $file;
117
				}
118
119
				$this->download_links[ $theme ] = $file;
120
				continue;
121
			}
122
123
			$params = (object) array( 'slug' => $theme );
124
			$url = 'https://api.wordpress.org/themes/info/1.0/';
125
			$args = array(
126
				'body' => array(
127
					'action' => 'theme_information',
128
					'request' => serialize( $params ),
129
				)
130
			);
131
			$response = wp_remote_post( $url, $args );
132
			$theme_data = unserialize( $response['body'] );
133
			if ( is_wp_error( $theme_data ) ) {
134
				return $theme_data;
135
			}
136
137
			if ( ! is_object( $theme_data ) && !isset( $theme_data->download_link ) ) {
138
				return new WP_Error( 'theme_not_found', __( 'This theme does not exist', 'jetpack' ) , 404 );
139
			}
140
141
			$this->download_links[ $theme ] = $theme_data->download_link;
142
143
		}
144
		return true;
145
	}
146
147
	protected static function is_installed_theme( $theme ) {
148
		$wp_theme = wp_get_theme( $theme );
149
		return $wp_theme->exists();
150
	}
151
152
	protected static function download_wpcom_theme_to_file( $theme ) {
153
		$wpcom_theme_slug = preg_replace( '/-wpcom$/', '', $theme );
0 ignored issues
show
Unused Code introduced by
$wpcom_theme_slug is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
154
155
		$file = wp_tempnam( 'theme' );
156
		if ( ! $file ) {
157
			return new WP_Error( 'problem_creating_theme_file', __( 'Problem creating file for theme download', 'jetpack' ) );
158
		}
159
160
		$url = "themes/download/$theme.zip";
161
		$args = array( 'stream' => true, 'filename' => $file );
162
		$result = Jetpack_Client::wpcom_json_api_request_as_blog( $url, '1.1', $args );
163
164
		$response =  $result[ 'response' ];
165
		if ( $response[ 'code' ] !== 200 ) {
166
			unlink( $file );
167
			return new WP_Error( 'problem_fetching_theme', __( 'Problem downloading theme', 'jetpack' ) );
168
		}
169
170
		return $file;
171
	}
172
}
173