PluginProbe
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 0.0.6
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v0.0.6
2.12.6 2.12.5 2.12.4 2.12.3 2.12.2 2.12.1 2.12.0 2.11.1 2.11.0 2.10.1 2.10.0 2.9.1 2.9.0 2.8.2 2.8.1 2.7.0 2.7.1 2.8.0 trunk 0.0.10 0.0.11 0.0.12 0.0.13 0.0.2 0.0.3 All 96 releases
sureforms / api / block-patterns.php

block-patterns.php in SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz 0.0.6, at api/block-patterns.php

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