1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class Jetpack_JSON_API_Themes_Active_Endpoint extends Jetpack_JSON_API_Themes_Endpoint { |
4
|
|
|
// GET /sites/%s/themes/mine => current theme |
5
|
|
|
// POST /sites/%s/themes/mine => switch theme |
6
|
|
|
public function callback( $path = '', $blog_id = 0 ) { |
7
|
|
|
|
8
|
|
|
if ( is_wp_error( $error = $this->validate_call( $blog_id, 'switch_themes', true ) ) ) { |
9
|
|
|
return $error; |
10
|
|
|
} |
11
|
|
|
|
12
|
|
|
if ( 'POST' === $this->api->method ) |
13
|
|
|
return $this->switch_theme(); |
14
|
|
|
else |
15
|
|
|
return $this->get_current_theme(); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
protected function switch_theme() { |
19
|
|
|
$args = $this->input(); |
20
|
|
|
|
21
|
|
|
if ( ! isset( $args['theme'] ) || empty( $args['theme'] ) ) { |
22
|
|
|
return new WP_Error( 'missing_theme', __( 'You are required to specify a theme to switch to.', 'jetpack' ), 400 ); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
$theme_slug = $args['theme']; |
26
|
|
|
|
27
|
|
|
if ( ! $theme_slug ) { |
28
|
|
|
return new WP_Error( 'theme_not_found', __( 'Theme is empty.', 'jetpack' ), 404 ); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$theme = wp_get_theme( $theme_slug ); |
32
|
|
|
|
33
|
|
|
if ( ! $theme->exists() ) { |
34
|
|
|
return new WP_Error( 'theme_not_found', __( 'The specified theme was not found.', 'jetpack' ), 404 ); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
if ( ! $theme->is_allowed() ) { |
38
|
|
|
return new WP_Error( 'theme_not_found', __( 'You are not allowed to switch to this theme', 'jetpack' ), 403 ); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
switch_theme( $theme_slug ); |
42
|
|
|
|
43
|
|
|
return $this->get_current_theme(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
protected function get_current_theme() { |
47
|
|
|
return $this->format_theme( wp_get_theme() ); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|