暫無描述

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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 response.
  96. *
  97. * @since 5.5.0
  98. * @since 5.9.0 Renamed `$plugin` to `$item` to match parent class for PHP 8 named parameter support.
  99. *
  100. * @param array $item The plugin metadata.
  101. * @param WP_REST_Request $request Request object.
  102. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
  103. */
  104. public function prepare_item_for_response( $item, $request ) {
  105. // Restores the more descriptive, specific name for use within this method.
  106. $plugin = $item;
  107. // There might be multiple blocks in a plugin. Only the first block is mapped.
  108. $block_data = reset( $plugin['blocks'] );
  109. // A data array containing the properties we'll return.
  110. $block = array(
  111. 'name' => $block_data['name'],
  112. 'title' => ( $block_data['title'] ? $block_data['title'] : $plugin['name'] ),
  113. 'description' => wp_trim_words( $plugin['short_description'], 30, '...' ),
  114. 'id' => $plugin['slug'],
  115. 'rating' => $plugin['rating'] / 20,
  116. 'rating_count' => (int) $plugin['num_ratings'],
  117. 'active_installs' => (int) $plugin['active_installs'],
  118. 'author_block_rating' => $plugin['author_block_rating'] / 20,
  119. 'author_block_count' => (int) $plugin['author_block_count'],
  120. 'author' => wp_strip_all_tags( $plugin['author'] ),
  121. 'icon' => ( isset( $plugin['icons']['1x'] ) ? $plugin['icons']['1x'] : 'block-default' ),
  122. 'last_updated' => gmdate( 'Y-m-d\TH:i:s', strtotime( $plugin['last_updated'] ) ),
  123. 'humanized_updated' => sprintf(
  124. /* translators: %s: Human-readable time difference. */
  125. __( '%s ago' ),
  126. human_time_diff( strtotime( $plugin['last_updated'] ) )
  127. ),
  128. );
  129. $this->add_additional_fields_to_object( $block, $request );
  130. $response = new WP_REST_Response( $block );
  131. $response->add_links( $this->prepare_links( $plugin ) );
  132. return $response;
  133. }
  134. /**
  135. * Generates a list of links to include in the response for the plugin.
  136. *
  137. * @since 5.5.0
  138. *
  139. * @param array $plugin The plugin data from WordPress.org.
  140. * @return array
  141. */
  142. protected function prepare_links( $plugin ) {
  143. $links = array(
  144. 'https://api.w.org/install-plugin' => array(
  145. 'href' => add_query_arg( 'slug', urlencode( $plugin['slug'] ), rest_url( 'wp/v2/plugins' ) ),
  146. ),
  147. );
  148. $plugin_file = $this->find_plugin_for_slug( $plugin['slug'] );
  149. if ( $plugin_file ) {
  150. $links['https://api.w.org/plugin'] = array(
  151. 'href' => rest_url( 'wp/v2/plugins/' . substr( $plugin_file, 0, - 4 ) ),
  152. 'embeddable' => true,
  153. );
  154. }
  155. return $links;
  156. }
  157. /**
  158. * Finds an installed plugin for the given slug.
  159. *
  160. * @since 5.5.0
  161. *
  162. * @param string $slug The WordPress.org directory slug for a plugin.
  163. * @return string The plugin file found matching it.
  164. */
  165. protected function find_plugin_for_slug( $slug ) {
  166. require_once ABSPATH . 'wp-admin/includes/plugin.php';
  167. $plugin_files = get_plugins( '/' . $slug );
  168. if ( ! $plugin_files ) {
  169. return '';
  170. }
  171. $plugin_files = array_keys( $plugin_files );
  172. return $slug . '/' . reset( $plugin_files );
  173. }
  174. /**
  175. * Retrieves the theme's schema, conforming to JSON Schema.
  176. *
  177. * @since 5.5.0
  178. *
  179. * @return array Item schema data.
  180. */
  181. public function get_item_schema() {
  182. if ( $this->schema ) {
  183. return $this->add_additional_fields_schema( $this->schema );
  184. }
  185. $this->schema = array(
  186. '$schema' => 'http://json-schema.org/draft-04/schema#',
  187. 'title' => 'block-directory-item',
  188. 'type' => 'object',
  189. 'properties' => array(
  190. 'name' => array(
  191. 'description' => __( 'The block name, in namespace/block-name format.' ),
  192. 'type' => 'string',
  193. 'context' => array( 'view' ),
  194. ),
  195. 'title' => array(
  196. 'description' => __( 'The block title, in human readable format.' ),
  197. 'type' => 'string',
  198. 'context' => array( 'view' ),
  199. ),
  200. 'description' => array(
  201. 'description' => __( 'A short description of the block, in human readable format.' ),
  202. 'type' => 'string',
  203. 'context' => array( 'view' ),
  204. ),
  205. 'id' => array(
  206. 'description' => __( 'The block slug.' ),
  207. 'type' => 'string',
  208. 'context' => array( 'view' ),
  209. ),
  210. 'rating' => array(
  211. 'description' => __( 'The star rating of the block.' ),
  212. 'type' => 'number',
  213. 'context' => array( 'view' ),
  214. ),
  215. 'rating_count' => array(
  216. 'description' => __( 'The number of ratings.' ),
  217. 'type' => 'integer',
  218. 'context' => array( 'view' ),
  219. ),
  220. 'active_installs' => array(
  221. 'description' => __( 'The number sites that have activated this block.' ),
  222. 'type' => 'integer',
  223. 'context' => array( 'view' ),
  224. ),
  225. 'author_block_rating' => array(
  226. 'description' => __( 'The average rating of blocks published by the same author.' ),
  227. 'type' => 'number',
  228. 'context' => array( 'view' ),
  229. ),
  230. 'author_block_count' => array(
  231. 'description' => __( 'The number of blocks published by the same author.' ),
  232. 'type' => 'integer',
  233. 'context' => array( 'view' ),
  234. ),
  235. 'author' => array(
  236. 'description' => __( 'The WordPress.org username of the block author.' ),
  237. 'type' => 'string',
  238. 'context' => array( 'view' ),
  239. ),
  240. 'icon' => array(
  241. 'description' => __( 'The block icon.' ),
  242. 'type' => 'string',
  243. 'format' => 'uri',
  244. 'context' => array( 'view' ),
  245. ),
  246. 'last_updated' => array(
  247. 'description' => __( 'The date when the block was last updated.' ),
  248. 'type' => 'string',
  249. 'format' => 'date-time',
  250. 'context' => array( 'view' ),
  251. ),
  252. 'humanized_updated' => array(
  253. 'description' => __( 'The date when the block was last updated, in fuzzy human readable format.' ),
  254. 'type' => 'string',
  255. 'context' => array( 'view' ),
  256. ),
  257. ),
  258. );
  259. return $this->add_additional_fields_schema( $this->schema );
  260. }
  261. /**
  262. * Retrieves the search params for the blocks collection.
  263. *
  264. * @since 5.5.0
  265. *
  266. * @return array Collection parameters.
  267. */
  268. public function get_collection_params() {
  269. $query_params = parent::get_collection_params();
  270. $query_params['context']['default'] = 'view';
  271. $query_params['term'] = array(
  272. 'description' => __( 'Limit result set to blocks matching the search term.' ),
  273. 'type' => 'string',
  274. 'required' => true,
  275. 'minLength' => 1,
  276. );
  277. unset( $query_params['search'] );
  278. /**
  279. * Filters REST API collection parameters for the block directory controller.
  280. *
  281. * @since 5.5.0
  282. *
  283. * @param array $query_params JSON Schema-formatted collection parameters.
  284. */
  285. return apply_filters( 'rest_block_directory_collection_params', $query_params );
  286. }
  287. }