Keine Beschreibung

class-wp-rest-block-directory-controller.php 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <?php
  2. /**
  3. * REST API: WP_REST_Block_Directory_Controller class
  4. *
  5. * @package WordPress
  6. * @subpackage REST_API
  7. * @since 5.5.0
  8. */
  9. /**
  10. * Controller which provides REST endpoint for the blocks.
  11. *
  12. * @since 5.5.0
  13. *
  14. * @see WP_REST_Controller
  15. */
  16. class WP_REST_Block_Directory_Controller extends WP_REST_Controller {
  17. /**
  18. * Constructs the controller.
  19. */
  20. public function __construct() {
  21. $this->namespace = 'wp/v2';
  22. $this->rest_base = 'block-directory';
  23. }
  24. /**
  25. * Registers the necessary REST API routes.
  26. */
  27. public function register_routes() {
  28. register_rest_route(
  29. $this->namespace,
  30. '/' . $this->rest_base . '/search',
  31. array(
  32. array(
  33. 'methods' => WP_REST_Server::READABLE,
  34. 'callback' => array( $this, 'get_items' ),
  35. 'permission_callback' => array( $this, 'get_items_permissions_check' ),
  36. 'args' => $this->get_collection_params(),
  37. ),
  38. 'schema' => array( $this, 'get_public_item_schema' ),
  39. )
  40. );
  41. }
  42. /**
  43. * Checks whether a given request has permission to install and activate plugins.
  44. *
  45. * @since 5.5.0
  46. *
  47. * @param WP_REST_Request $request Full details about the request.
  48. * @return true|WP_Error True if the request has permission, WP_Error object otherwise.
  49. */
  50. public function get_items_permissions_check( $request ) {
  51. if ( ! current_user_can( 'install_plugins' ) || ! current_user_can( 'activate_plugins' ) ) {
  52. return new WP_Error(
  53. 'rest_block_directory_cannot_view',
  54. __( 'Sorry, you are not allowed to browse the block directory.' ),
  55. array( 'status' => rest_authorization_required_code() )
  56. );
  57. }
  58. return true;
  59. }
  60. /**
  61. * Search and retrieve blocks metadata
  62. *
  63. * @since 5.5.0
  64. *
  65. * @param WP_REST_Request $request Full details about the request.
  66. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
  67. */
  68. public function get_items( $request ) {
  69. require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
  70. require_once ABSPATH . 'wp-admin/includes/plugin.php';
  71. $response = plugins_api(
  72. 'query_plugins',
  73. array(
  74. 'block' => $request['term'],
  75. 'per_page' => $request['per_page'],
  76. 'page' => $request['page'],
  77. )
  78. );
  79. if ( is_wp_error( $response ) ) {
  80. $response->add_data( array( 'status' => 500 ) );
  81. return $response;
  82. }
  83. $result = array();
  84. foreach ( $response->plugins as $plugin ) {
  85. // If the API returned a plugin with empty data for 'blocks', skip it.
  86. if ( empty( $plugin['blocks'] ) ) {
  87. continue;
  88. }
  89. $data = $this->prepare_item_for_response( $plugin, $request );
  90. $result[] = $this->prepare_response_for_collection( $data );
  91. }
  92. return rest_ensure_response( $result );
  93. }
  94. /**
  95. * Parse block metadata for a block, and prepare it for an API repsonse.
  96. *
  97. * @since 5.5.0
  98. *
  99. * @param array $plugin The plugin metadata.
  100. * @param WP_REST_Request $request Request object.
  101. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
  102. */
  103. public function prepare_item_for_response( $plugin, $request ) {
  104. // There might be multiple blocks in a plugin. Only the first block is mapped.
  105. $block_data = reset( $plugin['blocks'] );
  106. // A data array containing the properties we'll return.
  107. $block = array(
  108. 'name' => $block_data['name'],
  109. 'title' => ( $block_data['title'] ? $block_data['title'] : $plugin['name'] ),
  110. 'description' => wp_trim_words( $plugin['short_description'], 30, '...' ),
  111. 'id' => $plugin['slug'],
  112. 'rating' => $plugin['rating'] / 20,
  113. 'rating_count' => (int) $plugin['num_ratings'],
  114. 'active_installs' => (int) $plugin['active_installs'],
  115. 'author_block_rating' => $plugin['author_block_rating'] / 20,
  116. 'author_block_count' => (int) $plugin['author_block_count'],
  117. 'author' => wp_strip_all_tags( $plugin['author'] ),
  118. 'icon' => ( isset( $plugin['icons']['1x'] ) ? $plugin['icons']['1x'] : 'block-default' ),
  119. 'last_updated' => gmdate( 'Y-m-d\TH:i:s', strtotime( $plugin['last_updated'] ) ),
  120. 'humanized_updated' => sprintf(
  121. /* translators: %s: Human-readable time difference. */
  122. __( '%s ago' ),
  123. human_time_diff( strtotime( $plugin['last_updated'] ) )
  124. ),
  125. );
  126. $this->add_additional_fields_to_object( $block, $request );
  127. $response = new WP_REST_Response( $block );
  128. $response->add_links( $this->prepare_links( $plugin ) );
  129. return $response;
  130. }
  131. /**
  132. * Generates a list of links to include in the response for the plugin.
  133. *
  134. * @since 5.5.0
  135. *
  136. * @param array $plugin The plugin data from WordPress.org.
  137. * @return array
  138. */
  139. protected function prepare_links( $plugin ) {
  140. $links = array(
  141. 'https://api.w.org/install-plugin' => array(
  142. 'href' => add_query_arg( 'slug', urlencode( $plugin['slug'] ), rest_url( 'wp/v2/plugins' ) ),
  143. ),
  144. );
  145. $plugin_file = $this->find_plugin_for_slug( $plugin['slug'] );
  146. if ( $plugin_file ) {
  147. $links['https://api.w.org/plugin'] = array(
  148. 'href' => rest_url( 'wp/v2/plugins/' . substr( $plugin_file, 0, - 4 ) ),
  149. 'embeddable' => true,
  150. );
  151. }
  152. return $links;
  153. }
  154. /**
  155. * Finds an installed plugin for the given slug.
  156. *
  157. * @since 5.5.0
  158. *
  159. * @param string $slug The WordPress.org directory slug for a plugin.
  160. * @return string The plugin file found matching it.
  161. */
  162. protected function find_plugin_for_slug( $slug ) {
  163. require_once ABSPATH . 'wp-admin/includes/plugin.php';
  164. $plugin_files = get_plugins( '/' . $slug );
  165. if ( ! $plugin_files ) {
  166. return '';
  167. }
  168. $plugin_files = array_keys( $plugin_files );
  169. return $slug . '/' . reset( $plugin_files );
  170. }
  171. /**
  172. * Retrieves the theme's schema, conforming to JSON Schema.
  173. *
  174. * @since 5.5.0
  175. *
  176. * @return array Item schema data.
  177. */
  178. public function get_item_schema() {
  179. if ( $this->schema ) {
  180. return $this->add_additional_fields_schema( $this->schema );
  181. }
  182. $this->schema = array(
  183. '$schema' => 'http://json-schema.org/draft-04/schema#',
  184. 'title' => 'block-directory-item',
  185. 'type' => 'object',
  186. 'properties' => array(
  187. 'name' => array(
  188. 'description' => __( 'The block name, in namespace/block-name format.' ),
  189. 'type' => 'string',
  190. 'context' => array( 'view' ),
  191. ),
  192. 'title' => array(
  193. 'description' => __( 'The block title, in human readable format.' ),
  194. 'type' => 'string',
  195. 'context' => array( 'view' ),
  196. ),
  197. 'description' => array(
  198. 'description' => __( 'A short description of the block, in human readable format.' ),
  199. 'type' => 'string',
  200. 'context' => array( 'view' ),
  201. ),
  202. 'id' => array(
  203. 'description' => __( 'The block slug.' ),
  204. 'type' => 'string',
  205. 'context' => array( 'view' ),
  206. ),
  207. 'rating' => array(
  208. 'description' => __( 'The star rating of the block.' ),
  209. 'type' => 'integer',
  210. 'context' => array( 'view' ),
  211. ),
  212. 'rating_count' => array(
  213. 'description' => __( 'The number of ratings.' ),
  214. 'type' => 'integer',
  215. 'context' => array( 'view' ),
  216. ),
  217. 'active_installs' => array(
  218. 'description' => __( 'The number sites that have activated this block.' ),
  219. 'type' => 'string',
  220. 'context' => array( 'view' ),
  221. ),
  222. 'author_block_rating' => array(
  223. 'description' => __( 'The average rating of blocks published by the same author.' ),
  224. 'type' => 'integer',
  225. 'context' => array( 'view' ),
  226. ),
  227. 'author_block_count' => array(
  228. 'description' => __( 'The number of blocks published by the same author.' ),
  229. 'type' => 'integer',
  230. 'context' => array( 'view' ),
  231. ),
  232. 'author' => array(
  233. 'description' => __( 'The WordPress.org username of the block author.' ),
  234. 'type' => 'string',
  235. 'context' => array( 'view' ),
  236. ),
  237. 'icon' => array(
  238. 'description' => __( 'The block icon.' ),
  239. 'type' => 'string',
  240. 'format' => 'uri',
  241. 'context' => array( 'view' ),
  242. ),
  243. 'last_updated' => array(
  244. 'description' => __( 'The date when the block was last updated, in fuzzy human readable format.' ),
  245. 'type' => 'string',
  246. 'format' => 'date-time',
  247. 'context' => array( 'view' ),
  248. ),
  249. 'humanized_updated' => array(
  250. 'description' => __( 'The date when the block was last updated, in fuzzy human readable format.' ),
  251. 'type' => 'string',
  252. 'context' => array( 'view' ),
  253. ),
  254. ),
  255. );
  256. return $this->add_additional_fields_schema( $this->schema );
  257. }
  258. /**
  259. * Retrieves the search params for the blocks collection.
  260. *
  261. * @since 5.5.0
  262. *
  263. * @return array Collection parameters.
  264. */
  265. public function get_collection_params() {
  266. $query_params = parent::get_collection_params();
  267. $query_params['context']['default'] = 'view';
  268. $query_params['term'] = array(
  269. 'description' => __( 'Limit result set to blocks matching the search term.' ),
  270. 'type' => 'string',
  271. 'required' => true,
  272. 'minLength' => 1,
  273. );
  274. unset( $query_params['search'] );
  275. /**
  276. * Filters REST API collection parameters for the block directory controller.
  277. *
  278. * @since 5.5.0
  279. *
  280. * @param array $query_params JSON Schema-formatted collection parameters.
  281. */
  282. return apply_filters( 'rest_block_directory_collection_params', $query_params );
  283. }
  284. }