Completed
Push — update/delete-wpcom-theme-on-s... ( 5d2e66...11954d )
by
unknown
54:50 queued 45:00
created

Jetpack_JSON_API_Themes_Active_Endpoint   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 47
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A callback() 0 11 3
B switch_theme() 0 27 6
A get_current_theme() 0 3 1
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