PluginProbe
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 4.2.1
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v4.2.1
4.7.2 4.7.1 4.7.0 4.6.6 4.6.5 4.6.4 4.6.3 4.6.2 4.6.1 4.6.0 4.5.1 4.5.0 4.4.2 4.4.1 4.4.0 4.3.3 4.3.2 4.3.1 4.3.0 4.2.3 4.2.2 4.2.1 1.0.3 1.0.4 1.0.5 All 281 releases
surecart / app / src / Rest / BlockPatternsRestServiceProvider.php
BlockPatternsRestServiceProvider.php
230 lines 6.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace SureCart\Rest;
3
4 use SureCartCore\ServiceProviders\ServiceProviderInterface;
5
6 /**
7 * REST API: BlockPatternsRestServiceProvider class
8 *
9 * @package WordPress
10 * @subpackage REST_API
11 * @since 6.0.0
12 */
13
14 /**
15 * Core class used to access block patterns via the REST API.
16 *
17 * @since 6.0.0
18 *
19 * @see WP_REST_Controller
20 */
21 class BlockPatternsRestServiceProvider extends \WP_REST_Controller implements ServiceProviderInterface {
22 /**
23 * {@inheritDoc}
24 *
25 * @param \Pimple\Container $container Service Container.
26 */
27 public function register( $container ) {
28 // nothing to register.
29 }
30
31 /**
32 * Bootstrap routes
33 *
34 * @param \Pimple\Container $container Service Container.
35 *
36 * @return void
37 */
38 public function bootstrap( $container ) {
39 add_action( 'rest_api_init', [ $this, 'register_routes' ] );
40 }
41
42 /**
43 * Constructs the controller.
44 *
45 * @since 6.0.0
46 */
47 public function __construct() {
48 $this->namespace = 'surecart/v1';
49 $this->rest_base = 'form-patterns';
50 }
51
52 /**
53 * Registers the routes for the objects of the controller.
54 *
55 * @since 6.0.0
56 */
57 public function register_routes() {
58 register_rest_route(
59 $this->namespace,
60 '/' . $this->rest_base,
61 array(
62 array(
63 'methods' => \WP_REST_Server::READABLE,
64 'callback' => array( $this, 'get_items' ),
65 'permission_callback' => array( $this, 'get_items_permissions_check' ),
66 ),
67 'schema' => array( $this, 'get_public_item_schema' ),
68 )
69 );
70 }
71
72 /**
73 * Checks whether a given request has permission to read block patterns.
74 *
75 * @since 6.0.0
76 *
77 * @param WP_REST_Request $request Full details about the request.
78 * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
79 */
80 public function get_items_permissions_check( $request ) {
81 if ( current_user_can( 'edit_posts' ) ) {
82 return true;
83 }
84
85 foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $post_type ) {
86 if ( current_user_can( $post_type->cap->edit_posts ) ) {
87 return true;
88 }
89 }
90
91 return new \WP_Error(
92 'rest_cannot_view',
93 __( 'Sorry, you are not allowed to view the registered form patterns.', 'surecart' ),
94 array( 'status' => rest_authorization_required_code() )
95 );
96 }
97
98 /**
99 * Retrieves all block patterns.
100 *
101 * @since 6.0.0
102 *
103 * @param WP_REST_Request $request Full details about the request.
104 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
105 */
106 public function get_items( $request ) {
107 $response = array();
108 $patterns = \WP_Block_Patterns_Registry::get_instance()->get_all_registered();
109 $filtered = array_filter(
110 $patterns,
111 function( $pattern ) {
112 return in_array( 'surecart_form', $pattern['categories'] ?? [] );
113 }
114 );
115 foreach ( $filtered as $pattern ) {
116 $prepared_pattern = $this->prepare_item_for_response( $pattern, $request );
117 $response[] = $this->prepare_response_for_collection( $prepared_pattern );
118 }
119 return rest_ensure_response( $response );
120 }
121
122 /**
123 * Prepare a raw block pattern before it gets output in a REST API response.
124 *
125 * @since 6.0.0
126 *
127 * @param array $item Raw pattern as registered, before any changes.
128 * @param WP_REST_Request $request Request object.
129 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
130 */
131 public function prepare_item_for_response( $item, $request ) {
132 $fields = $this->get_fields_for_response( $request );
133 $keys = array(
134 'name' => 'name',
135 'title' => 'title',
136 'description' => 'description',
137 'viewportWidth' => 'viewport_width',
138 'blockTypes' => 'block_types',
139 'categories' => 'categories',
140 'keywords' => 'keywords',
141 'content' => 'content',
142 'inserter' => 'inserter',
143 );
144 $data = array();
145 foreach ( $keys as $item_key => $rest_key ) {
146 if ( isset( $item[ $item_key ] ) && rest_is_field_included( $rest_key, $fields ) ) {
147 $data[ $rest_key ] = $item[ $item_key ];
148 }
149 }
150
151 $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
152 $data = $this->add_additional_fields_to_object( $data, $request );
153 $data = $this->filter_response_by_context( $data, $context );
154 return rest_ensure_response( $data );
155 }
156
157 /**
158 * Retrieves the block pattern schema, conforming to JSON Schema.
159 *
160 * @since 6.0.0
161 *
162 * @return array Item schema data.
163 */
164 public function get_item_schema() {
165 $schema = array(
166 '$schema' => 'http://json-schema.org/draft-04/schema#',
167 'title' => 'block-pattern',
168 'type' => 'object',
169 'properties' => array(
170 'name' => array(
171 'description' => __( 'The pattern name.', 'surecart' ),
172 'type' => 'string',
173 'readonly' => true,
174 'context' => array( 'view', 'edit', 'embed' ),
175 ),
176 'title' => array(
177 'description' => __( 'The pattern title, in human readable format.', 'surecart' ),
178 'type' => 'string',
179 'readonly' => true,
180 'context' => array( 'view', 'edit', 'embed' ),
181 ),
182 'description' => array(
183 'description' => __( 'The pattern detailed description.', 'surecart' ),
184 'type' => 'string',
185 'readonly' => true,
186 'context' => array( 'view', 'edit', 'embed' ),
187 ),
188 'viewport_width' => array(
189 'description' => __( 'The pattern viewport width for inserter preview.', 'surecart' ),
190 'type' => 'number',
191 'readonly' => true,
192 'context' => array( 'view', 'edit', 'embed' ),
193 ),
194 'block_types' => array(
195 'description' => __( 'Block types that the pattern is intended to be used with.', 'surecart' ),
196 'type' => 'array',
197 'readonly' => true,
198 'context' => array( 'view', 'edit', 'embed' ),
199 ),
200 'categories' => array(
201 'description' => __( 'The pattern category slugs.', 'surecart' ),
202 'type' => 'array',
203 'readonly' => true,
204 'context' => array( 'view', 'edit', 'embed' ),
205 ),
206 'keywords' => array(
207 'description' => __( 'The pattern keywords.', 'surecart' ),
208 'type' => 'array',
209 'readonly' => true,
210 'context' => array( 'view', 'edit', 'embed' ),
211 ),
212 'content' => array(
213 'description' => __( 'The pattern content.', 'surecart' ),
214 'type' => 'string',
215 'readonly' => true,
216 'context' => array( 'view', 'edit', 'embed' ),
217 ),
218 'inserter' => array(
219 'description' => __( 'Determines whether the pattern is visible in inserter.', 'surecart' ),
220 'type' => 'boolean',
221 'readonly' => true,
222 'context' => array( 'view', 'edit', 'embed' ),
223 ),
224 ),
225 );
226
227 return $this->add_additional_fields_schema( $schema );
228 }
229 }
230