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
|
|
|
$skin = new Jetpack_Automatic_Install_Skin(); |
18
|
|
|
$upgrader = new Theme_Upgrader( $skin ); |
19
|
|
|
|
20
|
|
|
$link = $this->download_links[ $theme ]; |
21
|
|
|
$result = $upgrader->install( $link ); |
22
|
|
|
|
23
|
|
|
if ( file_exists( $link ) ) { |
24
|
|
|
// Delete if link was tmp local file |
25
|
|
|
unlink( $link ); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
if ( ! $this->bulk && is_wp_error( $result ) ) { |
29
|
|
|
return $result; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
if ( ! $result ) { |
33
|
|
|
$error = $this->log[ $theme ]['error'] = __( 'An unknown error occurred during installation', 'jetpack' ); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
elseif ( ! self::is_installed_theme( $theme ) ) { |
37
|
|
|
$error = $this->log[ $theme ]['error'] = __( 'There was an error installing your theme', 'jetpack' ); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
else { |
41
|
|
|
$this->log[ $theme ][] = $upgrader->skin->get_upgrade_messages(); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if ( ! $this->bulk && isset( $error ) ) { |
46
|
|
|
return new WP_Error( 'install_error', $error, 400 ); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return true; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
protected function validate_themes() { |
53
|
|
View Code Duplication |
if ( empty( $this->themes ) || ! is_array( $this->themes ) ) { |
54
|
|
|
return new WP_Error( 'missing_themes', __( 'No themes found.', 'jetpack' ) ); |
55
|
|
|
} |
56
|
|
|
foreach( $this->themes as $index => $theme ) { |
57
|
|
|
|
58
|
|
|
if ( self::is_installed_theme( $theme ) ) { |
59
|
|
|
return new WP_Error( 'theme_already_installed', __( 'The theme is already installed', 'jetpack' ) ); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if ( wp_endswith( $theme, '-wpcom' ) ) { |
63
|
|
|
$file = self::download_wpcom_theme_to_file( $theme ); |
64
|
|
|
if ( is_wp_error( $file ) ) { |
65
|
|
|
return $file; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$this->download_links[ $theme ] = $file; |
69
|
|
|
continue; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$params = (object) array( 'slug' => $theme ); |
73
|
|
|
$url = 'https://api.wordpress.org/themes/info/1.0/'; |
74
|
|
|
$args = array( |
75
|
|
|
'body' => array( |
76
|
|
|
'action' => 'theme_information', |
77
|
|
|
'request' => serialize( $params ), |
78
|
|
|
) |
79
|
|
|
); |
80
|
|
|
$response = wp_remote_post( $url, $args ); |
81
|
|
|
$theme_data = unserialize( $response['body'] ); |
82
|
|
|
if ( is_wp_error( $theme_data ) ) { |
83
|
|
|
return $theme_data; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if ( ! is_object( $theme_data ) && !isset( $theme_data->download_link ) ) { |
87
|
|
|
return new WP_Error( 'theme_not_found', __( 'This theme does not exist', 'jetpack' ) , 404 ); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$this->download_links[ $theme ] = $theme_data->download_link; |
91
|
|
|
|
92
|
|
|
} |
93
|
|
|
return true; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
protected static function is_installed_theme( $theme ) { |
97
|
|
|
$wp_theme = wp_get_theme( $theme ); |
98
|
|
|
return $wp_theme->exists(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
protected static function download_wpcom_theme_to_file( $theme ) { |
102
|
|
|
$wpcom_theme_slug = preg_replace( '/-wpcom$/', '', $theme ); |
|
|
|
|
103
|
|
|
|
104
|
|
|
$file = wp_tempnam( 'theme' ); |
105
|
|
|
if ( ! $file ) { |
106
|
|
|
return new WP_Error( 'problem_creating_theme_file', __( 'Problem creating file for theme download', 'jetpack' ) ); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$url = "themes/download/$theme.zip"; |
110
|
|
|
$args = array( 'stream' => true, 'filename' => $file ); |
111
|
|
|
$result = Jetpack_Client::wpcom_json_api_request_as_blog( $url, '1.1', $args ); |
112
|
|
|
|
113
|
|
|
$response = $result[ 'response' ]; |
114
|
|
|
if ( $response[ 'code' ] !== 200 ) { |
115
|
|
|
unlink( $file ); |
116
|
|
|
return new WP_Error( 'problem_fetching_theme', __( 'Problem downloading theme', 'jetpack' ) ); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $file; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
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.