Completed
Push — add/theme-install-hooks ( 9ba56b...4b7a1a )
by Rastislav
12:56 queued 03:54
created

Jetpack_JSON_API_Themes_Delete_Endpoint   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 56
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 1

1 Method

Rating   Name   Duplication   Size   Complexity  
C delete() 0 48 9
1
<?php
2
3
class Jetpack_JSON_API_Themes_Delete_Endpoint extends Jetpack_JSON_API_Themes_Endpoint {
4
5
	// POST  /sites/%s/plugins/%s/delete
6
	protected $needed_capabilities = 'delete_themes';
7
	protected $action              = 'delete';
8
9
	protected function delete() {
10
11
		foreach( $this->themes as $theme ) {
12
13
			// Don't delete an active child theme
14
			if ( is_child_theme() && $theme == get_stylesheet() )  {
15
				$error = $this->log[ $theme ]['error'] = 'You cannot delete a theme while it is active on the main site.';
16
				continue;
17
			}
18
19
			if( $theme == get_template() ) {
20
				$error = $this->log[ $theme ]['error'] = 'You cannot delete a theme while it is active on the main site.';
21
				continue;
22
			}
23
24
			/**
25
			 * Filters whether to use an alternative process for deleting a WordPress.com theme.
26
			 * The alternative process can be executed during the filter.
27
			 *
28
			 * The filter can also return an instance of WP_Error; in which case the endpoint response will
29
			 * contain this error.
30
			 *
31
			 * @since 4.4.2
32
			 *
33
			 * @param bool   $use_alternative_delete_method Whether to use the alternative method of deleting
34
			 *                                              a WPCom theme.
35
			 * @param string $theme_slug                    Theme name (slug). If it is a WPCom theme,
36
			 *                                              it should be suffixed with `-wpcom`.
37
			 */
38
			$result = apply_filters( 'jetpack_wpcom_theme_delete', false, $theme );
39
40
			if ( ! $result ) {
41
				$result = delete_theme( $theme );
42
			}
43
44
			if ( is_wp_error( $result ) ) {
45
				$error = $this->log[ $theme ]['error'] = $result->get_error_messages;
46
			} else {
47
				$this->log[ $theme ][] = 'Theme deleted';
48
			}
49
		}
50
51
		if( ! $this->bulk && isset( $error ) ) {
52
			return  new WP_Error( 'delete_theme_error', $error, 400 );
53
		}
54
55
		return true;
56
	}
57
58
}
59