| 1 |
<?php |
| 2 |
/** |
| 3 |
* Register API for Block Patterns. |
| 4 |
* |
| 5 |
* @package sureforms. |
| 6 |
* @since 0.0.1 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace SRFM\API; |
| 10 |
|
| 11 |
use SRFM\Inc\Helper; |
| 12 |
use SRFM\Inc\Traits\Get_Instance; |
| 13 |
use WP_Block_Patterns_Registry; |
| 14 |
use WP_Error; |
| 15 |
use WP_Post_Type; |
| 16 |
use WP_REST_Controller; |
| 17 |
use WP_REST_Request; |
| 18 |
use WP_REST_Response; |
| 19 |
|
| 20 |
if ( ! defined( 'ABSPATH' ) ) { |
| 21 |
exit; // Exit if accessed directly. |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Core class used to access block patterns via the REST API. |
| 26 |
* |
| 27 |
* @see WP_REST_Controller |
| 28 |
* @since 0.0.1 |
| 29 |
*/ |
| 30 |
class Block_Patterns extends WP_REST_Controller { |
| 31 |
use Get_Instance; |
| 32 |
|
| 33 |
/** |
| 34 |
* Class Constructor |
| 35 |
* |
| 36 |
* @return void |
| 37 |
*/ |
| 38 |
public function __construct() { |
| 39 |
$this->namespace = 'sureforms/v1'; |
| 40 |
$this->rest_base = 'form-patterns'; |
| 41 |
|
| 42 |
add_action( 'rest_api_init', [ $this, 'register_routes' ] ); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Registers the routes for the objects of the controller. |
| 47 |
* |
| 48 |
* @since 0.0.1 |
| 49 |
* @return void |
| 50 |
*/ |
| 51 |
public function register_routes() { |
| 52 |
register_rest_route( |
| 53 |
$this->namespace, |
| 54 |
'/' . $this->rest_base, |
| 55 |
[ |
| 56 |
[ |
| 57 |
'methods' => \WP_REST_Server::READABLE, |
| 58 |
'callback' => [ $this, 'get_items' ], |
| 59 |
'permission_callback' => [ $this, 'get_items_permissions_check' ], |
| 60 |
], |
| 61 |
'schema' => [ $this, 'get_public_item_schema' ], |
| 62 |
] |
| 63 |
); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Checks whether a given request has permission to read block patterns. |
| 68 |
* |
| 69 |
* @param WP_REST_Request $request Full details about the request. |
| 70 |
* @return true|WP_Error True if the request has read access, WP_Error object otherwise. |
| 71 |
* @since 0.0.1 |
| 72 |
*/ |
| 73 |
public function get_items_permissions_check( $request ) { |
| 74 |
unset( $request ); |
| 75 |
if ( current_user_can( 'edit_posts' ) ) { |
| 76 |
return true; |
| 77 |
} |
| 78 |
foreach ( get_post_types( [ 'show_in_rest' => true ], 'objects' ) as $post_type ) { |
| 79 |
/** |
| 80 |
* The post type. |
| 81 |
* |
| 82 |
* @var WP_Post_Type $post_type |
| 83 |
*/ |
| 84 |
if ( current_user_can( $post_type->cap->edit_posts ) ) { |
| 85 |
return true; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
return new \WP_Error( |
| 90 |
'rest_cannot_view', |
| 91 |
__( 'Sorry, you are not allowed to view the registered form patterns.', 'sureforms' ), |
| 92 |
[ 'status' => \rest_authorization_required_code() ] |
| 93 |
); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Retrieves all block patterns. |
| 98 |
* |
| 99 |
* @param WP_REST_Request $request Full details about the request. |
| 100 |
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
| 101 |
* @since 0.0.1 |
| 102 |
*/ |
| 103 |
public function get_items( $request ) { |
| 104 |
|
| 105 |
$nonce = Helper::get_string_value( $request->get_header( 'X-WP-Nonce' ) ); |
| 106 |
|
| 107 |
if ( ! wp_verify_nonce( sanitize_text_field( $nonce ), 'wp_rest' ) ) { |
| 108 |
wp_send_json_error( |
| 109 |
[ |
| 110 |
'data' => __( 'Nonce verification failed.', 'sureforms' ), |
| 111 |
'status' => false, |
| 112 |
] |
| 113 |
); |
| 114 |
} |
| 115 |
|
| 116 |
$response = []; |
| 117 |
$patterns = WP_Block_Patterns_Registry::get_instance()->get_all_registered(); |
| 118 |
$filtered = array_filter( |
| 119 |
$patterns, |
| 120 |
static function( $pattern ) { |
| 121 |
return in_array( 'sureforms_form', $pattern['categories'] ?? [], true ); |
| 122 |
} |
| 123 |
); |
| 124 |
|
| 125 |
foreach ( $filtered as $pattern ) { |
| 126 |
$prepared_pattern = $this->prepare_item_for_response( $pattern, $request ); |
| 127 |
if ( ! is_wp_error( $prepared_pattern ) ) { |
| 128 |
$response[] = $this->prepare_response_for_collection( $prepared_pattern ); |
| 129 |
} |
| 130 |
} |
| 131 |
return rest_ensure_response( $response ); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Prepare a raw block pattern before it gets output in a REST API response. |
| 136 |
* |
| 137 |
* @param array<mixed> $item Raw pattern as registered, before any changes. |
| 138 |
* @param WP_REST_Request $request Request object. |
| 139 |
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
| 140 |
* @since 0.0.1 |
| 141 |
*/ |
| 142 |
public function prepare_item_for_response( $item, $request ) { |
| 143 |
$fields = $this->get_fields_for_response( $request ); |
| 144 |
$keys = [ |
| 145 |
'name' => 'name', |
| 146 |
'title' => 'title', |
| 147 |
'info' => 'info', |
| 148 |
'description' => 'description', |
| 149 |
'viewportWidth' => 'viewport_width', |
| 150 |
'blockTypes' => 'block_types', |
| 151 |
'categories' => 'categories', |
| 152 |
'templateCategory' => 'templateCategory', |
| 153 |
'postMetas' => 'postMetas', |
| 154 |
'slug' => 'slug', |
| 155 |
'id' => 'id', |
| 156 |
'isPro' => 'isPro', |
| 157 |
'keywords' => 'keywords', |
| 158 |
'content' => 'content', |
| 159 |
'inserter' => 'inserter', |
| 160 |
]; |
| 161 |
$data = []; |
| 162 |
foreach ( $keys as $item_key => $rest_key ) { |
| 163 |
if ( isset( $item[ $item_key ] ) && rest_is_field_included( $rest_key, $fields ) ) { |
| 164 |
$data[ $rest_key ] = $item[ $item_key ]; |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
$context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
| 169 |
$data = $this->add_additional_fields_to_object( $data, $request ); |
| 170 |
$data = $this->filter_response_by_context( $data, $context ); |
| 171 |
return rest_ensure_response( $data ); |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Retrieves the block pattern schema, conforming to JSON Schema. |
| 176 |
* |
| 177 |
* @return array<mixed> Item schema data. |
| 178 |
* @since 0.0.1 |
| 179 |
*/ |
| 180 |
public function get_item_schema() { |
| 181 |
$schema = [ |
| 182 |
'$schema' => 'http://json-schema.org/draft-04/schema#', |
| 183 |
'title' => 'block-pattern', |
| 184 |
'type' => 'object', |
| 185 |
'properties' => [ |
| 186 |
'name' => [ |
| 187 |
'description' => __( 'The pattern name.', 'sureforms' ), |
| 188 |
'type' => 'string', |
| 189 |
'readonly' => true, |
| 190 |
'context' => [ 'view', 'edit', 'embed' ], |
| 191 |
], |
| 192 |
'title' => [ |
| 193 |
'description' => __( 'The pattern title, in human readable format.', 'sureforms' ), |
| 194 |
'type' => 'string', |
| 195 |
'readonly' => true, |
| 196 |
'context' => [ 'view', 'edit', 'embed' ], |
| 197 |
], |
| 198 |
'info' => [ |
| 199 |
'description' => __( 'The pattern basic description', 'sureforms' ), |
| 200 |
'type' => 'string', |
| 201 |
'readonly' => true, |
| 202 |
'context' => [ 'view', 'edit', 'embed' ], |
| 203 |
], |
| 204 |
'description' => [ |
| 205 |
'description' => __( 'The pattern detailed description.', 'sureforms' ), |
| 206 |
'type' => 'string', |
| 207 |
'readonly' => true, |
| 208 |
'context' => [ 'view', 'edit', 'embed' ], |
| 209 |
], |
| 210 |
'viewport_width' => [ |
| 211 |
'description' => __( 'The pattern viewport width for inserter preview.', 'sureforms' ), |
| 212 |
'type' => 'number', |
| 213 |
'readonly' => true, |
| 214 |
'context' => [ 'view', 'edit', 'embed' ], |
| 215 |
], |
| 216 |
'block_types' => [ |
| 217 |
'description' => __( 'Block types that the pattern is intended to be used with.', 'sureforms' ), |
| 218 |
'type' => 'array', |
| 219 |
'readonly' => true, |
| 220 |
'context' => [ 'view', 'edit', 'embed' ], |
| 221 |
], |
| 222 |
'categories' => [ |
| 223 |
'description' => __( 'The pattern category slugs.', 'sureforms' ), |
| 224 |
'type' => 'array', |
| 225 |
'readonly' => true, |
| 226 |
'context' => [ 'view', 'edit', 'embed' ], |
| 227 |
], |
| 228 |
'templateCategory' => [ |
| 229 |
'description' => __( 'The pattern template category.', 'sureforms' ), |
| 230 |
'type' => 'string', |
| 231 |
'readonly' => true, |
| 232 |
'context' => [ 'view', 'edit', 'embed' ], |
| 233 |
], |
| 234 |
'postMetas' => [ |
| 235 |
'description' => __( 'The pattern post metas.', 'sureforms' ), |
| 236 |
'type' => 'array', |
| 237 |
'readonly' => true, |
| 238 |
'context' => [ 'view', 'edit', 'embed' ], |
| 239 |
], |
| 240 |
'slug' => [ |
| 241 |
'description' => __( 'The pattern slug.', 'sureforms' ), |
| 242 |
'type' => 'string', |
| 243 |
'readonly' => true, |
| 244 |
'context' => [ 'view', 'edit', 'embed' ], |
| 245 |
], |
| 246 |
'id' => [ |
| 247 |
'description' => __( 'The pattern template id.', 'sureforms' ), |
| 248 |
'type' => 'string', |
| 249 |
'readonly' => true, |
| 250 |
'context' => [ 'view', 'edit', 'embed' ], |
| 251 |
], |
| 252 |
'isPro' => [ |
| 253 |
'description' => __( 'Wether pattern is pro or not.', 'sureforms' ), |
| 254 |
'type' => 'boolean', |
| 255 |
'readonly' => true, |
| 256 |
'context' => [ 'view', 'edit', 'embed' ], |
| 257 |
], |
| 258 |
'keywords' => [ |
| 259 |
'description' => __( 'The pattern keywords.', 'sureforms' ), |
| 260 |
'type' => 'array', |
| 261 |
'readonly' => true, |
| 262 |
'context' => [ 'view', 'edit', 'embed' ], |
| 263 |
], |
| 264 |
'content' => [ |
| 265 |
'description' => __( 'The pattern content.', 'sureforms' ), |
| 266 |
'type' => 'string', |
| 267 |
'readonly' => true, |
| 268 |
'context' => [ 'view', 'edit', 'embed' ], |
| 269 |
], |
| 270 |
'inserter' => [ |
| 271 |
'description' => __( 'Determines whether the pattern is visible in inserter.', 'sureforms' ), |
| 272 |
'type' => 'boolean', |
| 273 |
'readonly' => true, |
| 274 |
'context' => [ 'view', 'edit', 'embed' ], |
| 275 |
], |
| 276 |
], |
| 277 |
]; |
| 278 |
|
| 279 |
return $this->add_additional_fields_schema( $schema ); |
| 280 |
} |
| 281 |
} |
| 282 |
|