PluginProbe
BlockSpare – Gutenberg Blocks for News, Magazine, Blog & Business Websites / trunk
BlockSpare – Gutenberg Blocks for News, Magazine, Blog & Business Websites vtrunk
4.1.0 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.1.0 1.1.1 1.1.2 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 2.1.2 2.5.0 2.5.3 2.6.1 All 38 releases
blockspare / inc / layout / endpoints.php

endpoints.php in BlockSpare – Gutenberg Blocks for News, Magazine, Blog & Business Websites trunk, at inc/layout/endpoints.php

257 lines 7.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * REST API Endpoints for Sections and Layouts.
4 *
5 * @package Blockspare
6 */
7
8 namespace Blockspare\Layouts;
9
10 use \WP_REST_Response;
11 use \WP_REST_Server;
12
13 const BS_API_NAMESPACE = 'blockspare/v1';
14
15 const LAYOUTS_ROUTE = 'layouts';
16 const SINGLE_LAYOUT_ROUTE = 'layouts/([A-Za-z])\w+/';
17
18 const SECTIONS_ROUTE = 'sections';
19 const SINGLE_SECTION_ROUTE = 'sections/([A-Za-z])\w+/';
20
21 const BLOCKS_ROUTE = 'blocks';
22 const SINGLE_BLOCK_ROUTE = 'blocks/([A-Za-z])\w+/';
23
24 const PAGES_ROUTE = 'pages';
25 const SINGLE_PAGE_ROUTE = 'pages/([A-Za-z])\w+/';
26
27 const FAVORITE_LAYOUTS_ROUTE = 'layouts/favorites';
28 const ALL_LAYOUTS_ROUTE = 'layouts/all';
29
30 const IMPORT_ROUTE = 'importdata';
31
32 add_action( 'rest_api_init', __NAMESPACE__ . '\register_layout_endpoints' );
33 /**
34 * Create custom endpoints for block settings
35 */
36 function register_layout_endpoints() {
37
38 /**
39 * Register the favorites GET endpoint.
40 *
41 * Note: Keep this route before the other routes
42 * otherwise they may override this one.
43 */
44 register_rest_route(
45 BS_API_NAMESPACE,
46 FAVORITE_LAYOUTS_ROUTE,
47 [
48 'methods' => WP_REST_Server::READABLE,
49 'callback' => function () {
50 return new WP_REST_Response( (array) get_user_meta( get_current_user_id(), 'blockspare_favorite_layouts', true ) );
51 },
52 'permission_callback' => function () {
53 return current_user_can( 'edit_posts' );
54 },
55 ]
56 );
57
58 /**
59 * Register the layouts GET endpoint
60 * that combines all sections, layouts,
61 * and additional layouts.
62 */
63 register_rest_route(
64 BS_API_NAMESPACE,
65 ALL_LAYOUTS_ROUTE,
66 [
67 'methods' => WP_REST_Server::READABLE,
68 'callback' => function ( \WP_REST_Request $request ) {
69
70 $layouts = blockspare_get_layouts();
71 $sections = blockspare_get_sections();
72 $blocks = blockspare_get_blocks();
73 $pages = blockspare_get_pages();
74 $templates = blockspare_get_templates();
75 $additional_layouts = apply_filters( 'blockspare_additional_layout_components', [] );
76 $all_layouts = array_merge( $layouts, $sections, $blocks,$pages,$templates, $additional_layouts );
77 $request_params = $request->get_params();
78
79 // Return all layouts if filtering was not requested. "allowed" is the only filter currently supported.
80 if ( empty( $request_params['filter'] ) || 'allowed' !== $request_params['filter'] ) {
81 return new WP_REST_Response( $all_layouts );
82 }
83
84 /**
85 * Filters the list of sections and layouts allowed to show in the layouts library.
86 *
87 * @since 2.5.0
88 *
89 * @param array $all_layouts Array of unique layout keys allowed. Defaults to all layouts.
90 */
91 $allowed_layouts = (array) apply_filters( 'blockspare_allowed_layout_components', array_keys( $all_layouts ) );
92
93 if ( empty( $allowed_layouts ) ) {
94 return new WP_REST_Response( [] );
95 }
96
97 $filtered_layouts = [];
98
99 foreach ( $all_layouts as $key => $layout ) {
100 if ( in_array( $key, $allowed_layouts, true ) ) {
101 $filtered_layouts[ $key ] = $layout;
102 }
103 }
104
105 return new WP_REST_Response( $filtered_layouts );
106 },
107 'permission_callback' => function () {
108 return current_user_can( 'edit_posts' );
109 },
110 ]
111 );
112
113 /**
114 * Register the layouts GET endpoint.
115 * Returns all registered layouts.
116 */
117 register_rest_route(
118 BS_API_NAMESPACE,
119 LAYOUTS_ROUTE,
120 [
121 'methods' => WP_REST_Server::READABLE,
122 'callback' => function () {
123 return new WP_REST_Response( (array) blockspare_get_layouts() );
124 },
125 'permission_callback' => function () {
126 return current_user_can( 'edit_posts' );
127 },
128 ]
129 );
130
131 /**
132 * Register the single layout GET endpoint.
133 * Returns a single requested layout.
134 */
135 register_rest_route(
136 BS_API_NAMESPACE,
137 SINGLE_LAYOUT_ROUTE,
138 [
139 'methods' => WP_REST_Server::READABLE,
140 'callback' => function ( $request ) {
141 $route = $request->get_route();
142 $layout_key = substr( strrchr( $route, '/' ), 1 );
143 $layouts = blockspare_get_layouts();
144 if ( isset( $layouts[ $layout_key ] ) ) {
145 return new WP_REST_Response( $layouts[ $layout_key ] );
146 }
147
148 return new WP_REST_Response( esc_html__( 'Layout not found.', 'blockspare' ) );
149 },
150 'permission_callback' => function () {
151 return current_user_can( 'edit_posts' );
152 },
153 ]
154 );
155
156 /**
157 * Register the favorites update endpoint.
158 */
159 register_rest_route(
160 BS_API_NAMESPACE,
161 FAVORITE_LAYOUTS_ROUTE,
162 [
163 'methods' => 'PATCH',
164 'callback' => function ( $request ) {
165
166 $body = json_decode( $request->get_body(), true );
167 $new = sanitize_key( $body['blockspare_favorite_key'] );
168 $favorites = (array) get_user_meta( get_current_user_id(), 'blockspare_favorite_layouts', true );
169
170 if ( in_array( $new, $favorites, true ) ) {
171 return new WP_REST_Response( $favorites );
172 }
173
174 if ( empty( $favorites[0] ) ) {
175 $favorites = array( $new );
176 } else {
177 $favorites[] = $new;
178 }
179
180 update_user_meta( get_current_user_id(), 'blockspare_favorite_layouts', array_values( $favorites ) );
181
182 return new WP_REST_Response( (array) get_user_meta( get_current_user_id(), 'blockspare_favorite_layouts', true ) );
183 },
184 'permission_callback' => function () {
185 return current_user_can( 'edit_posts' );
186 },
187 ]
188 );
189
190 /**
191 * Register the favorites delete endpoint.
192 */
193 register_rest_route(
194 BS_API_NAMESPACE,
195 FAVORITE_LAYOUTS_ROUTE,
196 [
197 'methods' => 'DELETE',
198 'callback' => function ( $request ) {
199
200 $body = json_decode( $request->get_body(), true );
201 $delete_id = sanitize_key( $body['blockspare_favorite_key'] );
202 $favorites = (array) get_user_meta( get_current_user_id(), 'blockspare_favorite_layouts', true );
203
204 if ( ! in_array( $delete_id, $favorites, true ) ) {
205 return new WP_REST_Response( $favorites );
206 }
207
208 $position = array_search( $delete_id, $favorites, true );
209
210 unset( $favorites[ $position ] );
211
212 update_user_meta( get_current_user_id(), 'blockspare_favorite_layouts', array_values( $favorites ) );
213
214 return new WP_REST_Response( (array) get_user_meta( get_current_user_id(), 'blockspare_favorite_layouts', true ) );
215 },
216 'permission_callback' => function () {
217 return current_user_can( 'edit_posts' );
218 },
219 ]
220 );
221
222 /**
223 * Register the sections GET endpoint.
224 * Returns all registered sections.
225 */
226 register_rest_route(
227 BS_API_NAMESPACE,
228 SECTIONS_ROUTE,
229 [
230 'methods' => WP_REST_Server::READABLE,
231 'callback' => function () {
232 return new WP_REST_Response( (array) blockspare_get_sections() );
233 },
234 'permission_callback' => function () {
235 return current_user_can( 'edit_posts' );
236 },
237 ]
238 );
239
240 register_rest_route(
241 BS_API_NAMESPACE,
242 IMPORT_ROUTE,
243 [
244 'methods' => WP_REST_Server::EDITABLE,
245 'callback' => function (\WP_REST_Request $request) {
246 $request_params = $request->get_params();
247
248 $data = blockspare_import_images_replace_url($request_params['content'] ) ;
249 return new WP_REST_Response( $data );
250 },
251 'permission_callback' => function () {
252 return current_user_can( 'edit_posts' );
253 },
254 ]
255 );
256 }
257