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