/json-endpoints/jetpack/class.jetpack-json-api-themes-active-endpoint.php

  1. <?php 
  2.  
  3. class Jetpack_JSON_API_Themes_Active_Endpoint extends Jetpack_JSON_API_Themes_Endpoint { 
  4. // GET /sites/%s/themes/mine => current theme 
  5. // POST /sites/%s/themes/mine => switch theme 
  6. public function callback( $path = '', $blog_id = 0 ) { 
  7.  
  8. if ( is_wp_error( $error = $this->validate_call( $blog_id, 'switch_themes', true ) ) ) { 
  9. return $error; 
  10.  
  11. if ( 'POST' === $this->api->method ) 
  12. return $this->switch_theme(); 
  13. else 
  14. return $this->get_current_theme(); 
  15.  
  16. protected function switch_theme() { 
  17. $args = $this->input(); 
  18.  
  19. if ( ! isset( $args['theme'] ) || empty( $args['theme'] ) ) { 
  20. return new WP_Error( 'missing_theme', __( 'You are required to specify a theme to switch to.', 'jetpack' ), 400 ); 
  21.  
  22. $theme_slug = $args['theme']; 
  23.  
  24. if ( ! $theme_slug ) { 
  25. return new WP_Error( 'theme_not_found', __( 'Theme is empty.', 'jetpack' ), 404 ); 
  26.  
  27. $theme = wp_get_theme( $theme_slug ); 
  28.  
  29. if ( ! $theme->exists() ) { 
  30. return new WP_Error( 'theme_not_found', __( 'The specified theme was not found.', 'jetpack' ), 404 ); 
  31.  
  32. if ( ! $theme->is_allowed() ) { 
  33. return new WP_Error( 'theme_not_found', __( 'You are not allowed to switch to this theme', 'jetpack' ), 403 ); 
  34.  
  35. switch_theme( $theme_slug ); 
  36.  
  37. return $this->get_current_theme(); 
  38.  
  39. protected function get_current_theme() { 
  40. return $this->format_theme( wp_get_theme() ); 
.