PluginProbe
Genesis Blocks / trunk
Genesis Blocks vtrunk
3.1.11 trunk 1.0.0 1.1.0 1.1.1 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.3.0 1.4.0 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 2.0.0 3.0.0 3.0.1 3.1.0 3.1.1 3.1.10 All 32 releases
genesis-blocks / includes / layout / layout-endpoints.php

layout-endpoints.php in Genesis Blocks trunk, at includes/layout/layout-endpoints.php

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