暫無描述

class.jetpack-json-api-themes-endpoint.php 5.5KB

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