Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
Jetpack_JSON_API_Themes_Delete_Endpoint
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
90
0.00% covered (danger)
0.00%
0 / 1
 delete
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
90
1<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
3if ( ! defined( 'ABSPATH' ) ) {
4    exit( 0 );
5}
6
7/**
8 * Themes delete endpoint class.
9 * POST  /sites/%s/plugins/%s/delete
10 *
11 * @phan-constructor-used-for-side-effects
12 */
13class Jetpack_JSON_API_Themes_Delete_Endpoint extends Jetpack_JSON_API_Themes_Endpoint {
14
15    /**
16     * Needed capabilities.
17     *
18     * @var string
19     */
20    protected $needed_capabilities = 'delete_themes';
21
22    /**
23     * The action.
24     *
25     * @var string
26     */
27    protected $action = 'delete';
28
29    /**
30     * Delete the theme.
31     *
32     * @return bool|WP_Error
33     */
34    protected function delete() {
35
36        foreach ( $this->themes as $theme ) {
37
38            // Don't delete an active child theme
39            if ( is_child_theme() && $theme === get_stylesheet() ) {
40                $error                        = 'You cannot delete a theme while it is active on the main site.';
41                $this->log[ $theme ]['error'] = $error;
42                continue;
43            }
44
45            if ( $theme === get_template() ) {
46                $error                        = 'You cannot delete a theme while it is active on the main site.';
47                $this->log[ $theme ]['error'] = $error;
48                continue;
49            }
50
51            /**
52             * Filters whether to use an alternative process for deleting a WordPress.com theme.
53             * The alternative process can be executed during the filter.
54             *
55             * The filter can also return an instance of WP_Error; in which case the endpoint response will
56             * contain this error.
57             *
58             * @module json-api
59             *
60             * @since 4.4.2
61             *
62             * @param bool   $use_alternative_delete_method Whether to use the alternative method of deleting
63             *                                              a WPCom theme.
64             * @param string $theme_slug                    Theme name (slug). If it is a WPCom theme,
65             *                                              it should be suffixed with `-wpcom`.
66             */
67            $result = apply_filters( 'jetpack_wpcom_theme_delete', false, $theme );
68
69            if ( ! $result ) {
70                $result = delete_theme( $theme );
71            }
72
73            if ( is_wp_error( $result ) ) {
74                $error                        = $result->get_error_messages();
75                $this->log[ $theme ]['error'] = $error;
76            } else {
77                $this->log[ $theme ][] = 'Theme deleted';
78            }
79        }
80
81        if ( ! $this->bulk && isset( $error ) ) {
82            return new WP_Error( 'delete_theme_error', $error, 400 );
83        }
84
85        return true;
86    }
87}