Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Jetpack_JSON_API_Themes_Active_Endpoint
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 3
110
0.00% covered (danger)
0.00%
0 / 1
 callback
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 switch_theme
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
42
 get_current_theme
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
3if ( ! defined( 'ABSPATH' ) ) {
4    exit( 0 );
5}
6
7/**
8 * GET  /sites/%s/themes/mine => current theme
9 * POST /sites/%s/themes/mine => switch theme
10 *
11 * @phan-constructor-used-for-side-effects
12 */
13class Jetpack_JSON_API_Themes_Active_Endpoint extends Jetpack_JSON_API_Themes_Endpoint {
14
15    /**
16     * Endpoint callback.
17     *
18     * @param string $path - the path.
19     * @param int    $blog_id - the blog ID.
20     * @param object $object - The unused $object parameter is for making the method signature compatible with its parent class method.
21     *
22     * @return array|bool|WP_Error
23     */
24    public function callback( $path = '', $blog_id = 0, $object = null ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
25        $error = $this->validate_call( $blog_id, 'switch_themes', true );
26        if ( is_wp_error( $error ) ) {
27            return $error;
28        }
29
30        if ( 'POST' === $this->api->method ) {
31            return $this->switch_theme();
32        } else {
33            return $this->get_current_theme();
34        }
35    }
36
37    /**
38     * Switch the theme.
39     *
40     * @return array|WP_Error
41     */
42    protected function switch_theme() {
43        $args = $this->input();
44
45        if ( ! isset( $args['theme'] ) || empty( $args['theme'] ) ) {
46            return new WP_Error( 'missing_theme', __( 'You are required to specify a theme to switch to.', 'jetpack' ), 400 );
47        }
48
49        $theme_slug = $args['theme'];
50
51        if ( ! $theme_slug ) {
52            return new WP_Error( 'theme_not_found', __( 'Theme is empty.', 'jetpack' ), 404 );
53        }
54
55        /**
56         * Trigger action before the switch theme happens.
57         *
58         * @module json-api
59         *
60         * @since 11.1
61         *
62         * @param string $theme_slug Directory name for the theme.
63         * @param mixed  $args       POST body data, including info about the theme we must switch to.
64         */
65        do_action( 'jetpack_pre_switch_theme', $theme_slug, $args );
66
67        $theme = wp_get_theme( $theme_slug );
68
69        if ( ! $theme->exists() ) {
70            return new WP_Error( 'theme_not_found', __( 'The specified theme was not found.', 'jetpack' ), 404 );
71        }
72
73        if ( ! $theme->is_allowed() ) {
74            return new WP_Error( 'theme_not_found', __( 'You are not allowed to switch to this theme', 'jetpack' ), 403 );
75        }
76
77        switch_theme( $theme_slug );
78
79        return $this->get_current_theme();
80    }
81
82    /**
83     * Get the current theme.
84     *
85     * @return array
86     */
87    protected function get_current_theme() {
88        return $this->format_theme( wp_get_theme() );
89    }
90}