Jetpack_JSON_API_Themes_Endpoint

Base class for working with themes, has useful helper functions.

Defined (1)

The class is defined in the following location(s).

/json-endpoints/jetpack/class.jetpack-json-api-themes-endpoint.php  
  1. abstract class Jetpack_JSON_API_Themes_Endpoint extends Jetpack_JSON_API_Endpoint { 
  2.  
  3. protected $themes = array(); 
  4.  
  5. protected $bulk = true; 
  6. protected $log; 
  7. protected $current_theme_id; 
  8.  
  9. static $_response_format = array( 
  10. 'id' => '(string) The theme\'s ID.',  
  11. 'screenshot' => '(string) A theme screenshot URL',  
  12. 'name' => '(string) The name of the theme.',  
  13. 'theme_uri' => '(string) The URI of the theme\'s webpage.',  
  14. 'description' => '(string) A description of the theme.',  
  15. 'author' => '(string) The author of the theme.',  
  16. 'author_uri' => '(string) The website of the theme author.',  
  17. 'tags' => '(array) Tags indicating styles and features of the theme.',  
  18. 'log' => '(array) An array of log strings',  
  19. 'autoupdate' => '(bool) Whether the theme is automatically updated',  
  20. ); 
  21.  
  22. protected function result() { 
  23.  
  24. $themes = $this->get_themes(); 
  25.  
  26. if ( ! $this->bulk && ! empty( $themes ) ) { 
  27. return array_pop( $themes ); 
  28.  
  29. return array( 'themes' => $themes ); 
  30.  
  31.  
  32. /** 
  33. * Walks through either the submitted theme or list of themes and creates the global array 
  34. * @param $theme 
  35. * @return bool 
  36. */ 
  37. protected function validate_input( $theme ) { 
  38. $args = $this->input(); 
  39. // lets set what themes were requested, and validate them 
  40. if ( ! isset( $theme ) || empty( $theme ) ) { 
  41.  
  42. if ( ! $args['themes'] || empty( $args['themes'] ) ) { 
  43. return new WP_Error( 'missing_theme', __( 'You are required to specify a theme to update.', 'jetpack' ), 400 ); 
  44. if ( is_array( $args['themes'] ) ) { 
  45. $this->themes = $args['themes']; 
  46. } else { 
  47. $this->themes[] = $args['themes']; 
  48. } else { 
  49. $this->themes[] = urldecode( $theme ); 
  50. $this->bulk = false; 
  51.  
  52. if ( is_wp_error( $error = $this->validate_themes() ) ) { 
  53. return error; 
  54.  
  55. return parent::validate_input( $theme ); 
  56.  
  57. /** 
  58. * Walks through submitted themes to make sure they are valid 
  59. * @return bool|WP_Error 
  60. */ 
  61. protected function validate_themes() { 
  62. foreach ( $this->themes as $theme ) { 
  63. if ( is_wp_error( $error = wp_get_theme( $theme )->errors() ) ) { 
  64. return new WP_Error( 'unknown_theme', $error->get_error_messages() , 404 ); 
  65. return true; 
  66.  
  67. /** 
  68. * Format a theme for the public API 
  69. * @param object $theme WP_Theme object 
  70. * @return array Named array of theme info used by the API 
  71. */ 
  72. protected function format_theme( $theme ) { 
  73.  
  74. if ( ! ( $theme instanceof WP_Theme ) ) { 
  75. $theme = wp_get_theme( $theme ); 
  76.  
  77. $fields = array( 
  78. 'name' => 'Name',  
  79. 'theme_uri' => 'ThemeURI',  
  80. 'description' => 'Description',  
  81. 'author' => 'Author',  
  82. 'author_uri' => 'AuthorURI',  
  83. 'tags' => 'Tags',  
  84. 'version' => 'Version' 
  85. ); 
  86.  
  87. $id = $theme->get_stylesheet(); 
  88. $formatted_theme = array( 
  89. 'id' => $id,  
  90. 'screenshot' => jetpack_photon_url( $theme->get_screenshot(), array(), 'network_path' ),  
  91. 'active' => $id === $this->current_theme_id,  
  92. ); 
  93.  
  94. foreach( $fields as $key => $field ) { 
  95. $formatted_theme[ $key ] = $theme->get( $field ); 
  96.  
  97. $update_themes = get_site_transient( 'update_themes' ); 
  98. $formatted_theme['update'] = ( isset( $update_themes->response[ $id ] ) ) ? $update_themes->response[ $id ] : null; 
  99.  
  100. $autoupdate_themes = Jetpack_Options::get_option( 'autoupdate_themes', array() ); 
  101.  
  102. $autoupdate = in_array( $id, $autoupdate_themes ); 
  103.  
  104. $formatted_theme['autoupdate'] = $autoupdate; 
  105.  
  106. if( isset( $this->log[ $id ] ) ) { 
  107. $formatted_theme['log'] = $this->log[ $id ]; 
  108.  
  109. return $formatted_theme; 
  110.  
  111. /** 
  112. * Checks the query_args our collection endpoint was passed to ensure that it's in the proper bounds. 
  113. * @return bool|WP_Error a WP_Error object if the args are out of bounds, true if things are good. 
  114. */ 
  115. protected function check_query_args() { 
  116. $args = $this->query_args(); 
  117. if ( $args['offset'] < 0 ) 
  118. return new WP_Error( 'invalid_offset', __( 'Offset must be greater than or equal to 0.', 'jetpack' ), 400 ); 
  119. if ( $args['limit'] < 0 ) 
  120. return new WP_Error( 'invalid_limit', __( 'Limit must be greater than or equal to 0.', 'jetpack' ), 400 ); 
  121. return true; 
  122.  
  123. /** 
  124. * Format a list of themes for public display, using the supplied offset and limit args 
  125. * @uses WPCOM_JSON_API_Endpoint::query_args() 
  126. * @return array Public API theme objects 
  127. */ 
  128. protected function get_themes() { 
  129. // ditch keys 
  130. $themes = array_values( $this->themes ); 
  131. // do offset & limit - we've already returned a 400 error if they're bad numbers 
  132. $args = $this->query_args(); 
  133.  
  134. if ( isset( $args['offset'] ) ) 
  135. $themes = array_slice( $themes, (int) $args['offset'] ); 
  136. if ( isset( $args['limit'] ) ) 
  137. $themes = array_slice( $themes, 0, (int) $args['limit'] ); 
  138.  
  139. $this->current_theme_id = wp_get_theme()->get_stylesheet(); 
  140.  
  141. return array_map( array( $this, 'format_theme' ), $themes ); 
  142.