wp/wp-includes/rest-api/endpoints/class-wp-rest-block-directory-controller.php
changeset 18 be944660c56a
parent 16 a86126ab1dd4
child 19 3d72ae0968f4
equal deleted inserted replaced
17:34716fd837a4 18:be944660c56a
    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. */
   154 	 * Generates a list of links to include in the response for the plugin.
   151 	 * Generates a list of links to include in the response for the plugin.
   155 	 *
   152 	 *
   156 	 * @since 5.5.0
   153 	 * @since 5.5.0
   157 	 *
   154 	 *
   158 	 * @param array $plugin The plugin data from WordPress.org.
   155 	 * @param array $plugin The plugin data from WordPress.org.
   159 	 *
       
   160 	 * @return array
   156 	 * @return array
   161 	 */
   157 	 */
   162 	protected function prepare_links( $plugin ) {
   158 	protected function prepare_links( $plugin ) {
   163 		$links = array(
   159 		$links = array(
   164 			'https://api.w.org/install-plugin' => array(
   160 			'https://api.w.org/install-plugin' => array(
   182 	 * Finds an installed plugin for the given slug.
   178 	 * Finds an installed plugin for the given slug.
   183 	 *
   179 	 *
   184 	 * @since 5.5.0
   180 	 * @since 5.5.0
   185 	 *
   181 	 *
   186 	 * @param string $slug The WordPress.org directory slug for a plugin.
   182 	 * @param string $slug The WordPress.org directory slug for a plugin.
   187 	 *
       
   188 	 * @return string The plugin file found matching it.
   183 	 * @return string The plugin file found matching it.
   189 	 */
   184 	 */
   190 	protected function find_plugin_for_slug( $slug ) {
   185 	protected function find_plugin_for_slug( $slug ) {
   191 		require_once ABSPATH . 'wp-admin/includes/plugin.php';
   186 		require_once ABSPATH . 'wp-admin/includes/plugin.php';
   192 
   187 
   311 		);
   306 		);
   312 
   307 
   313 		unset( $query_params['search'] );
   308 		unset( $query_params['search'] );
   314 
   309 
   315 		/**
   310 		/**
   316 		 * Filter collection parameters for the block directory controller.
   311 		 * Filters REST API collection parameters for the block directory controller.
   317 		 *
   312 		 *
   318 		 * @since 5.5.0
   313 		 * @since 5.5.0
   319 		 *
   314 		 *
   320 		 * @param array $query_params JSON Schema-formatted collection parameters.
   315 		 * @param array $query_params JSON Schema-formatted collection parameters.
   321 		 */
   316 		 */