47 * Checks whether a given request has permission to install and activate plugins. |
47 * Checks whether a given request has permission to install and activate plugins. |
48 * |
48 * |
49 * @since 5.5.0 |
49 * @since 5.5.0 |
50 * |
50 * |
51 * @param WP_REST_Request $request Full details about the request. |
51 * @param WP_REST_Request $request Full details about the request. |
52 * |
52 * @return true|WP_Error True if the request has permission, WP_Error object otherwise. |
53 * @return WP_Error|bool True if the request has permission, WP_Error object otherwise. |
|
54 */ |
53 */ |
55 public function get_items_permissions_check( $request ) { |
54 public function get_items_permissions_check( $request ) { |
56 if ( ! current_user_can( 'install_plugins' ) || ! current_user_can( 'activate_plugins' ) ) { |
55 if ( ! current_user_can( 'install_plugins' ) || ! current_user_can( 'activate_plugins' ) ) { |
57 return new WP_Error( |
56 return new WP_Error( |
58 'rest_block_directory_cannot_view', |
57 'rest_block_directory_cannot_view', |
68 * Search and retrieve blocks metadata |
67 * Search and retrieve blocks metadata |
69 * |
68 * |
70 * @since 5.5.0 |
69 * @since 5.5.0 |
71 * |
70 * |
72 * @param WP_REST_Request $request Full details about the request. |
71 * @param WP_REST_Request $request Full details about the request. |
73 * |
72 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
74 * @return WP_Error|WP_REST_Response Response object on success, or WP_Error object on failure. |
|
75 */ |
73 */ |
76 public function get_items( $request ) { |
74 public function get_items( $request ) { |
77 require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
75 require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
78 require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
76 require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
79 |
77 |
112 * |
110 * |
113 * @since 5.5.0 |
111 * @since 5.5.0 |
114 * |
112 * |
115 * @param array $plugin The plugin metadata. |
113 * @param array $plugin The plugin metadata. |
116 * @param WP_REST_Request $request Request object. |
114 * @param WP_REST_Request $request Request object. |
117 * |
115 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
118 * @return WP_Error|WP_REST_Response Response object on success, or WP_Error object on failure. |
|
119 */ |
116 */ |
120 public function prepare_item_for_response( $plugin, $request ) { |
117 public function prepare_item_for_response( $plugin, $request ) { |
121 // There might be multiple blocks in a plugin. Only the first block is mapped. |
118 // There might be multiple blocks in a plugin. Only the first block is mapped. |
122 $block_data = reset( $plugin['blocks'] ); |
119 $block_data = reset( $plugin['blocks'] ); |
123 |
120 |
124 // A data array containing the properties we'll return. |
121 // A data array containing the properties we'll return. |
125 $block = array( |
122 $block = array( |
126 'name' => $block_data['name'], |
123 'name' => $block_data['name'], |
127 'title' => ( $block_data['title'] ? $block_data['title'] : $plugin['name'] ), |
124 'title' => ( $block_data['title'] ? $block_data['title'] : $plugin['name'] ), |
128 'description' => wp_trim_words( $plugin['description'], 30, '...' ), |
125 'description' => wp_trim_words( $plugin['short_description'], 30, '...' ), |
129 'id' => $plugin['slug'], |
126 'id' => $plugin['slug'], |
130 'rating' => $plugin['rating'] / 20, |
127 'rating' => $plugin['rating'] / 20, |
131 'rating_count' => intval( $plugin['num_ratings'] ), |
128 'rating_count' => (int) $plugin['num_ratings'], |
132 'active_installs' => intval( $plugin['active_installs'] ), |
129 'active_installs' => (int) $plugin['active_installs'], |
133 'author_block_rating' => $plugin['author_block_rating'] / 20, |
130 'author_block_rating' => $plugin['author_block_rating'] / 20, |
134 'author_block_count' => intval( $plugin['author_block_count'] ), |
131 'author_block_count' => (int) $plugin['author_block_count'], |
135 'author' => wp_strip_all_tags( $plugin['author'] ), |
132 'author' => wp_strip_all_tags( $plugin['author'] ), |
136 'icon' => ( isset( $plugin['icons']['1x'] ) ? $plugin['icons']['1x'] : 'block-default' ), |
133 'icon' => ( isset( $plugin['icons']['1x'] ) ? $plugin['icons']['1x'] : 'block-default' ), |
137 'last_updated' => gmdate( 'Y-m-d\TH:i:s', strtotime( $plugin['last_updated'] ) ), |
134 'last_updated' => gmdate( 'Y-m-d\TH:i:s', strtotime( $plugin['last_updated'] ) ), |
138 'humanized_updated' => sprintf( |
135 'humanized_updated' => sprintf( |
139 /* translators: %s: Human-readable time difference. */ |
136 /* translators: %s: Human-readable time difference. */ |