PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-beta
Jetpack – WP Security, Backup, Speed, & Growth v16.3-beta
16.3-beta 16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 All 507 releases
jetpack / json-endpoints / jetpack / class.jetpack-json-api-themes-active-endpoint.php

class.jetpack-json-api-themes-active-endpoint.php in Jetpack – WP Security, Backup, Speed, & Growth 16.3-beta, at json-endpoints/jetpack/class.jetpack-json-api-themes-active-endpoint.php

91 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
3 if ( ! 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 */
13 class 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 }
91