PHP 8.0.30
Preview: class-wp-rest-block-renderer-controller.php Size: 5.73 KB
/home/certprox/template.certproxywizard.com/wp-includes/rest-api/endpoints/class-wp-rest-block-renderer-controller.php

<?php
/**
 * Block Renderer REST API: WP_REST_Block_Renderer_Controller class
 *
 * @package WordPress
 * @subpackage REST_API
 * @since 5.0.0
 */

/**
 * Controller which provides REST endpoint for rendering a block.
 *
 * @since 5.0.0
 *
 * @see WP_REST_Controller
 */
class WP_REST_Block_Renderer_Controller extends WP_REST_Controller {

	/**
	 * Constructs the controller.
	 *
	 * @since 5.0.0
	 */
	public function __construct() {
		$this->namespace = 'wp/v2';
		$this->rest_base = 'block-renderer';
	}

	/**
	 * Registers the necessary REST API routes, one for each dynamic block.
	 *
	 * @since 5.0.0
	 *
	 * @see register_rest_route()
	 */
	public function register_routes() {
		register_rest_route(
			$this->namespace,
			'/' . $this->rest_base . '/(?P<name>[a-z0-9-]+/[a-z0-9-]+)',
			array(
				'args'   => array(
					'name' => array(
						'description' => __( 'Unique registered name for the block.' ),
						'type'        => 'string',
					),
				),
				array(
					'methods'             => array( WP_REST_Server::READABLE, WP_REST_Server::CREATABLE ),
					'callback'            => array( $this, 'get_item' ),
					'permission_callback' => array( $this, 'get_item_permissions_check' ),
					'args'                => array(
						'context'    => $this->get_context_param( array( 'default' => 'view' ) ),
						'attributes' => array(
							'description'       => __( 'Attributes for the block.' ),
							'type'              => 'object',
							'default'           => array(),
							'validate_callback' => static function ( $value, $request ) {
								$block = WP_Block_Type_Registry::get_instance()->get_registered( $request['name'] );

								if ( ! $block ) {
									// This will get rejected in ::get_item().
									return true;
								}

								$schema = array(
									'type'                 => 'object',
									'properties'           => $block->get_attributes(),
									'additionalProperties' => false,
								);

								return rest_validate_value_from_schema( $value, $schema );
							},
							'sanitize_callback' => static function ( $value, $request ) {
								$block = WP_Block_Type_Registry::get_instance()->get_registered( $request['name'] );

								if ( ! $block ) {
									// This will get rejected in ::get_item().
									return true;
								}

								$schema = array(
									'type'                 => 'object',
									'properties'           => $block->get_attributes(),
									'additionalProperties' => false,
								);

								return rest_sanitize_value_from_schema( $value, $schema );
							},
						),
						'post_id'    => array(
							'description' => __( 'ID of the post context.' ),
							'type'        => 'integer',
						),
					),
				),
				'schema' => array( $this, 'get_public_item_schema' ),
			)
		);
	}

	/**
	 * Checks if a given request has access to read blocks.
	 *
	 * @since 5.0.0
	 *
	 * @global WP_Post $post Global post object.
	 *
	 * @param WP_REST_Request $request Request.
	 * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
	 */
	public function get_item_permissions_check( $request ) {
		global $post;

		$post_id = isset( $request['post_id'] ) ? (int) $request['post_id'] : 0;

		if ( $post_id > 0 ) {
			$post = get_post( $post_id );

			if ( ! $post || ! current_user_can( 'edit_post', $post->ID ) ) {
				return new WP_Error(
					'block_cannot_read',
					__( 'Sorry, you are not allowed to read blocks of this post.' ),
					array(
						'status' => rest_authorization_required_code(),
					)
				);
			}
		} else {
			if ( ! current_user_can( 'edit_posts' ) ) {
				return new WP_Error(
					'block_cannot_read',
					__( 'Sorry, you are not allowed to read blocks as this user.' ),
					array(
						'status' => rest_authorization_required_code(),
					)
				);
			}
		}

		return true;
	}

	/**
	 * Returns block output from block's registered render_callback.
	 *
	 * @since 5.0.0
	 *
	 * @global WP_Post $post Global post object.
	 *
	 * @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_item( $request ) {
		global $post;

		$post_id = isset( $request['post_id'] ) ? (int) $request['post_id'] : 0;

		if ( $post_id > 0 ) {
			$post = get_post( $post_id );

			// Set up postdata since this will be needed if post_id was set.
			setup_postdata( $post );
		}

		$registry   = WP_Block_Type_Registry::get_instance();
		$registered = $registry->get_registered( $request['name'] );

		if ( null === $registered || ! $registered->is_dynamic() ) {
			return new WP_Error(
				'block_invalid',
				__( 'Invalid block.' ),
				array(
					'status' => 404,
				)
			);
		}

		$attributes = $request->get_param( 'attributes' );

		// Create an array representation simulating the output of parse_blocks.
		$block = array(
			'blockName'    => $request['name'],
			'attrs'        => $attributes,
			'innerBlocks'  => array(),
			'innerHTML'    => '',
			'innerContent' => array(),
		);

		// Render using render_block to ensure all relevant filters are used.
		$data = array(
			'rendered' => render_block( $block ),
		);

		return rest_ensure_response( $data );
	}

	/**
	 * Retrieves block's output schema, conforming to JSON Schema.
	 *
	 * @since 5.0.0
	 *
	 * @return array Item schema data.
	 */
	public function get_item_schema() {
		if ( $this->schema ) {
			return $this->schema;
		}

		$this->schema = array(
			'$schema'    => 'http://json-schema.org/schema#',
			'title'      => 'rendered-block',
			'type'       => 'object',
			'properties' => array(
				'rendered' => array(
					'description' => __( 'The rendered block.' ),
					'type'        => 'string',
					'required'    => true,
					'context'     => array( 'edit' ),
				),
			),
		);

		return $this->schema;
	}
}

Directory Contents

Dirs: 0 × Files: 46

Name Size Perms Modified Actions
7.90 KB lrw-r--r-- 2025-10-22 12:04:30
Edit Download
13.07 KB lrw-r--r-- 2026-05-06 14:49:46
Edit Download
6.87 KB lrw-r--r-- 2025-10-22 12:04:30
Edit Download
23.75 KB lrw-r--r-- 2025-07-02 09:00:38
Edit Download
56.80 KB lrw-r--r-- 2026-03-20 15:11:54
Edit Download
15.14 KB lrw-r--r-- 2026-05-08 12:59:44
Edit Download
9.68 KB lrw-r--r-- 2026-01-05 02:33:34
Edit Download
4.70 KB lrw-r--r-- 2025-03-11 12:19:20
Edit Download
9.08 KB lrw-r--r-- 2024-06-13 12:06:08
Edit Download
5.73 KB lrw-r--r-- 2026-03-17 14:50:48
Edit Download
26.25 KB lrw-r--r-- 2025-03-11 12:19:20
Edit Download
3.05 KB lrw-r--r-- 2026-01-05 02:33:34
Edit Download
61.56 KB lrw-r--r-- 2026-03-10 10:49:46
Edit Download
18.59 KB lrw-r--r-- 2026-02-01 13:47:44
Edit Download
2.06 KB lrw-r--r-- 2025-03-04 04:07:22
Edit Download
10.39 KB lrw-r--r-- 2026-02-17 21:01:44
Edit Download
29.40 KB lrw-r--r-- 2026-02-27 20:59:40
Edit Download
17.44 KB lrw-r--r-- 2026-02-27 20:59:40
Edit Download
23.25 KB lrw-r--r-- 2026-01-29 20:03:44
Edit Download
12.98 KB lrw-r--r-- 2025-11-25 00:54:36
Edit Download
7.06 KB lrw-r--r-- 2026-03-20 13:41:02
Edit Download
32.49 KB lrw-r--r-- 2025-04-17 09:10:36
Edit Download
8.67 KB lrw-r--r-- 2026-01-05 02:33:34
Edit Download
16.68 KB lrw-r--r-- 2025-01-28 02:09:22
Edit Download
5.05 KB lrw-r--r-- 2023-10-16 12:17:24
Edit Download
12.58 KB lrw-r--r-- 2026-01-05 02:33:34
Edit Download
27.86 KB lrw-r--r-- 2024-09-17 18:33:14
Edit Download
10.07 KB lrw-r--r-- 2024-07-09 10:53:16
Edit Download
13.95 KB lrw-r--r-- 2025-03-11 12:19:20
Edit Download
102.47 KB lrw-r--r-- 2026-03-12 21:26:02
Edit Download
27.27 KB lrw-r--r-- 2026-03-12 21:27:22
Edit Download
11.21 KB lrw-r--r-- 2025-03-11 12:19:20
Edit Download
10.09 KB lrw-r--r-- 2026-01-05 02:33:34
Edit Download
15.53 KB lrw-r--r-- 2026-01-05 02:33:34
Edit Download
9.61 KB lrw-r--r-- 2023-09-12 12:23:18
Edit Download
13.69 KB lrw-r--r-- 2025-03-11 12:19:20
Edit Download
7.64 KB lrw-r--r-- 2025-03-02 20:07:26
Edit Download
8.52 KB lrw-r--r-- 2025-03-02 20:07:26
Edit Download
37.34 KB lrw-r--r-- 2026-02-01 13:47:44
Edit Download
34.61 KB lrw-r--r-- 2025-09-30 12:51:28
Edit Download
22.91 KB lrw-r--r-- 2026-03-06 16:03:36
Edit Download
20.07 KB lrw-r--r-- 2024-07-11 03:24:18
Edit Download
48.67 KB lrw-r--r-- 2026-01-05 02:33:34
Edit Download
18.75 KB lrw-r--r-- 2026-01-05 02:33:34
Edit Download
26.23 KB lrw-r--r-- 2026-01-05 02:33:34
Edit Download
215.54 KB lrw-r--r-- 2026-07-26 21:29:02
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).