wppaste
WordPress

WP_REST_Block_Directory_Controller

Since
5.5.0
Source
wp-includes/rest-api/endpoints/class-wp-rest-block-directory-controller.php:17
Controller which provides REST endpoint for the blocks.

Compatibility

WordPress
since 5.5.0
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0).

Hooks and filters fired · 1

Every hook that fires from inside WP_REST_Block_Directory_Controller, in the order it appears in the class, grouped by the method that fires it.

Methods · 9

Source code

class WP_REST_Block_Directory_Controller extends WP_REST_Controller { 	/**	 * Constructs the controller.	 */	public function __construct() {		$this->namespace = 'wp/v2';		$this->rest_base = 'block-directory';	} 	/**	 * Registers the necessary REST API routes.	 */	public function register_routes() {		register_rest_route(			$this->namespace,			'/' . $this->rest_base . '/search',			array(				array(					'methods'             => WP_REST_Server::READABLE,					'callback'            => array( $this, 'get_items' ),					'permission_callback' => array( $this, 'get_items_permissions_check' ),					'args'                => $this->get_collection_params(),				),				'schema' => array( $this, 'get_public_item_schema' ),			)		);	} 	/**	 * Checks whether a given request has permission to install and activate plugins.	 *	 * @since 5.5.0	 *	 * @param WP_REST_Request $request Full details about the request.	 * @return true|WP_Error True if the request has permission, WP_Error object otherwise.	 */	public function get_items_permissions_check( $request ) {		if ( ! current_user_can( 'install_plugins' ) || ! current_user_can( 'activate_plugins' ) ) {			return new WP_Error(				'rest_block_directory_cannot_view',				__( 'Sorry, you are not allowed to browse the block directory.' ),				array( 'status' => rest_authorization_required_code() )			);		} 		return true;	} 	/**	 * Search and retrieve blocks metadata	 *	 * @since 5.5.0	 *	 * @param WP_REST_Request $request Full details about the request.	 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.	 */	public function get_items( $request ) {		require_once ABSPATH . 'wp-admin/includes/plugin-install.php';		require_once ABSPATH . 'wp-admin/includes/plugin.php'; 		$response = plugins_api(			'query_plugins',			array(				'block'    => $request['term'],				'per_page' => $request['per_page'],				'page'     => $request['page'],			)		); 		if ( is_wp_error( $response ) ) {			$response->add_data( array( 'status' => 500 ) ); 			return $response;		} 		$result = array(); 		foreach ( $response->plugins as $plugin ) {			// If the API returned a plugin with empty data for 'blocks', skip it.

Changelog

Introduced in 5.5.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 tag, from src/wp-includes/rest-api/endpoints/class-wp-rest-block-directory-controller.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.