PluginProbe
BlockSpare – Gutenberg Blocks for News, Magazine, Blog & Business Websites / 2.6.1
BlockSpare – Gutenberg Blocks for News, Magazine, Blog & Business Websites v2.6.1
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 2.6.1, at inc/layout/endpoints.php

237 lines 6.6 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 AB_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 add_action( 'rest_api_init', __NAMESPACE__ . '\register_layout_endpoints' );
31 /**
32 * Create custom endpoints for block settings
33 */
34 function register_layout_endpoints() {
35
36 /**
37 * Register the favorites GET endpoint.
38 *
39 * Note: Keep this route before the other routes
40 * otherwise they may override this one.
41 */
42 register_rest_route(
43 AB_API_NAMESPACE,
44 FAVORITE_LAYOUTS_ROUTE,
45 [
46 'methods' => WP_REST_Server::READABLE,
47 'callback' => function () {
48 return new WP_REST_Response( (array) get_user_meta( get_current_user_id(), 'blockspare_favorite_layouts', true ) );
49 },
50 'permission_callback' => function () {
51 return current_user_can( 'edit_posts' );
52 },
53 ]
54 );
55
56 /**
57 * Register the layouts GET endpoint
58 * that combines all sections, layouts,
59 * and additional layouts.
60 */
61 register_rest_route(
62 AB_API_NAMESPACE,
63 ALL_LAYOUTS_ROUTE,
64 [
65 'methods' => WP_REST_Server::READABLE,
66 'callback' => function ( \WP_REST_Request $request ) {
67
68 $layouts = blockspare_get_layouts();
69 $sections = blockspare_get_sections();
70 $blocks = blockspare_get_blocks();
71 $pages = blockspare_get_pages();
72 $additional_layouts = apply_filters( 'blockspare_additional_layout_components', [] );
73 $all_layouts = array_merge( $layouts, $sections, $blocks,$pages, $additional_layouts );
74 $request_params = $request->get_params();
75
76 // Return all layouts if filtering was not requested. "allowed" is the only filter currently supported.
77 if ( empty( $request_params['filter'] ) || 'allowed' !== $request_params['filter'] ) {
78 return new WP_REST_Response( $all_layouts );
79 }
80
81 /**
82 * Filters the list of sections and layouts allowed to show in the layouts library.
83 *
84 * @since 2.5.0
85 *
86 * @param array $all_layouts Array of unique layout keys allowed. Defaults to all layouts.
87 */
88 $allowed_layouts = (array) apply_filters( 'blockspare_allowed_layout_components', array_keys( $all_layouts ) );
89
90 if ( empty( $allowed_layouts ) ) {
91 return new WP_REST_Response( [] );
92 }
93
94 $filtered_layouts = [];
95
96 foreach ( $all_layouts as $key => $layout ) {
97 if ( in_array( $key, $allowed_layouts, true ) ) {
98 $filtered_layouts[ $key ] = $layout;
99 }
100 }
101
102 return new WP_REST_Response( $filtered_layouts );
103 },
104 'permission_callback' => function () {
105 return current_user_can( 'edit_posts' );
106 },
107 ]
108 );
109
110 /**
111 * Register the layouts GET endpoint.
112 * Returns all registered layouts.
113 */
114 register_rest_route(
115 AB_API_NAMESPACE,
116 LAYOUTS_ROUTE,
117 [
118 'methods' => WP_REST_Server::READABLE,
119 'callback' => function () {
120 return new WP_REST_Response( (array) blockspare_get_layouts() );
121 },
122 'permission_callback' => function () {
123 return current_user_can( 'edit_posts' );
124 },
125 ]
126 );
127
128 /**
129 * Register the single layout GET endpoint.
130 * Returns a single requested layout.
131 */
132 register_rest_route(
133 AB_API_NAMESPACE,
134 SINGLE_LAYOUT_ROUTE,
135 [
136 'methods' => WP_REST_Server::READABLE,
137 'callback' => function ( $request ) {
138 $route = $request->get_route();
139 $layout_key = substr( strrchr( $route, '/' ), 1 );
140 $layouts = blockspare_get_layouts();
141 if ( isset( $layouts[ $layout_key ] ) ) {
142 return new WP_REST_Response( $layouts[ $layout_key ] );
143 }
144
145 return new WP_REST_Response( esc_html__( 'Layout not found.', 'blockspare' ) );
146 },
147 'permission_callback' => function () {
148 return current_user_can( 'edit_posts' );
149 },
150 ]
151 );
152
153 /**
154 * Register the favorites update endpoint.
155 */
156 register_rest_route(
157 AB_API_NAMESPACE,
158 FAVORITE_LAYOUTS_ROUTE,
159 [
160 'methods' => 'PATCH',
161 'callback' => function ( $request ) {
162
163 $body = json_decode( $request->get_body(), true );
164 $new = sanitize_key( $body['blockspare_favorite_key'] );
165 $favorites = (array) get_user_meta( get_current_user_id(), 'blockspare_favorite_layouts', true );
166
167 if ( in_array( $new, $favorites, true ) ) {
168 return new WP_REST_Response( $favorites );
169 }
170
171 if ( empty( $favorites[0] ) ) {
172 $favorites = array( $new );
173 } else {
174 $favorites[] = $new;
175 }
176
177 update_user_meta( get_current_user_id(), 'blockspare_favorite_layouts', array_values( $favorites ) );
178
179 return new WP_REST_Response( (array) get_user_meta( get_current_user_id(), 'blockspare_favorite_layouts', true ) );
180 },
181 'permission_callback' => function () {
182 return current_user_can( 'edit_posts' );
183 },
184 ]
185 );
186
187 /**
188 * Register the favorites delete endpoint.
189 */
190 register_rest_route(
191 AB_API_NAMESPACE,
192 FAVORITE_LAYOUTS_ROUTE,
193 [
194 'methods' => 'DELETE',
195 'callback' => function ( $request ) {
196
197 $body = json_decode( $request->get_body(), true );
198 $delete_id = sanitize_key( $body['blockspare_favorite_key'] );
199 $favorites = (array) get_user_meta( get_current_user_id(), 'blockspare_favorite_layouts', true );
200
201 if ( ! in_array( $delete_id, $favorites, true ) ) {
202 return new WP_REST_Response( $favorites );
203 }
204
205 $position = array_search( $delete_id, $favorites, true );
206
207 unset( $favorites[ $position ] );
208
209 update_user_meta( get_current_user_id(), 'blockspare_favorite_layouts', array_values( $favorites ) );
210
211 return new WP_REST_Response( (array) get_user_meta( get_current_user_id(), 'blockspare_favorite_layouts', true ) );
212 },
213 'permission_callback' => function () {
214 return current_user_can( 'edit_posts' );
215 },
216 ]
217 );
218
219 /**
220 * Register the sections GET endpoint.
221 * Returns all registered sections.
222 */
223 register_rest_route(
224 AB_API_NAMESPACE,
225 SECTIONS_ROUTE,
226 [
227 'methods' => WP_REST_Server::READABLE,
228 'callback' => function () {
229 return new WP_REST_Response( (array) blockspare_get_sections() );
230 },
231 'permission_callback' => function () {
232 return current_user_can( 'edit_posts' );
233 },
234 ]
235 );
236 }
237