| @@ -9,9 +9,9 @@ | ||
| 9 | 9 | |
| 10 | 10 | use \WP_REST_Response; |
| 11 | 11 | use \WP_REST_Server; |
| 12 | 12 | |
| 13 | -const AB_API_NAMESPACE = 'blockspare/v1'; | |
| 13 | +const BS_API_NAMESPACE = 'blockspare/v1'; | |
| 14 | 14 | |
| 15 | 15 | const LAYOUTS_ROUTE = 'layouts'; |
| 16 | 16 | const SINGLE_LAYOUT_ROUTE = 'layouts/([A-Za-z])\w+/'; |
| 17 | 17 | |
| @@ -26,8 +26,10 @@ | ||
| 26 | 26 | |
| 27 | 27 | const FAVORITE_LAYOUTS_ROUTE = 'layouts/favorites'; |
| 28 | 28 | const ALL_LAYOUTS_ROUTE = 'layouts/all'; |
| 29 | 29 | |
| 30 | +const IMPORT_ROUTE = 'importdata'; | |
| 31 | + | |
| 30 | 32 | add_action( 'rest_api_init', __NAMESPACE__ . '\register_layout_endpoints' ); |
| 31 | 33 | /** |
| 32 | 34 | * Create custom endpoints for block settings |
| 33 | 35 | */ |
| @@ -39,9 +41,9 @@ | ||
| 39 | 41 | * Note: Keep this route before the other routes |
| 40 | 42 | * otherwise they may override this one. |
| 41 | 43 | */ |
| 42 | 44 | register_rest_route( |
| 43 | - AB_API_NAMESPACE, | |
| 45 | + BS_API_NAMESPACE, | |
| 44 | 46 | FAVORITE_LAYOUTS_ROUTE, |
| 45 | 47 | [ |
| 46 | 48 | 'methods' => WP_REST_Server::READABLE, |
| 47 | 49 | 'callback' => function () { |
| @@ -58,9 +60,9 @@ | ||
| 58 | 60 | * that combines all sections, layouts, |
| 59 | 61 | * and additional layouts. |
| 60 | 62 | */ |
| 61 | 63 | register_rest_route( |
| 62 | - AB_API_NAMESPACE, | |
| 64 | + BS_API_NAMESPACE, | |
| 63 | 65 | ALL_LAYOUTS_ROUTE, |
| 64 | 66 | [ |
| 65 | 67 | 'methods' => WP_REST_Server::READABLE, |
| 66 | 68 | 'callback' => function ( \WP_REST_Request $request ) { |
| @@ -68,10 +70,11 @@ | ||
| 68 | 70 | $layouts = blockspare_get_layouts(); |
| 69 | 71 | $sections = blockspare_get_sections(); |
| 70 | 72 | $blocks = blockspare_get_blocks(); |
| 71 | 73 | $pages = blockspare_get_pages(); |
| 74 | + $templates = blockspare_get_templates(); | |
| 72 | 75 | $additional_layouts = apply_filters( 'blockspare_additional_layout_components', [] ); |
| 73 | - $all_layouts = array_merge( $layouts, $sections, $blocks,$pages, $additional_layouts ); | |
| 76 | + $all_layouts = array_merge( $layouts, $sections, $blocks,$pages,$templates, $additional_layouts ); | |
| 74 | 77 | $request_params = $request->get_params(); |
| 75 | 78 | |
| 76 | 79 | // Return all layouts if filtering was not requested. "allowed" is the only filter currently supported. |
| 77 | 80 | if ( empty( $request_params['filter'] ) || 'allowed' !== $request_params['filter'] ) { |
| @@ -111,9 +114,9 @@ | ||
| 111 | 114 | * Register the layouts GET endpoint. |
| 112 | 115 | * Returns all registered layouts. |
| 113 | 116 | */ |
| 114 | 117 | register_rest_route( |
| 115 | - AB_API_NAMESPACE, | |
| 118 | + BS_API_NAMESPACE, | |
| 116 | 119 | LAYOUTS_ROUTE, |
| 117 | 120 | [ |
| 118 | 121 | 'methods' => WP_REST_Server::READABLE, |
| 119 | 122 | 'callback' => function () { |
| @@ -129,9 +132,9 @@ | ||
| 129 | 132 | * Register the single layout GET endpoint. |
| 130 | 133 | * Returns a single requested layout. |
| 131 | 134 | */ |
| 132 | 135 | register_rest_route( |
| 133 | - AB_API_NAMESPACE, | |
| 136 | + BS_API_NAMESPACE, | |
| 134 | 137 | SINGLE_LAYOUT_ROUTE, |
| 135 | 138 | [ |
| 136 | 139 | 'methods' => WP_REST_Server::READABLE, |
| 137 | 140 | 'callback' => function ( $request ) { |
| @@ -153,9 +156,9 @@ | ||
| 153 | 156 | /** |
| 154 | 157 | * Register the favorites update endpoint. |
| 155 | 158 | */ |
| 156 | 159 | register_rest_route( |
| 157 | - AB_API_NAMESPACE, | |
| 160 | + BS_API_NAMESPACE, | |
| 158 | 161 | FAVORITE_LAYOUTS_ROUTE, |
| 159 | 162 | [ |
| 160 | 163 | 'methods' => 'PATCH', |
| 161 | 164 | 'callback' => function ( $request ) { |
| @@ -187,9 +190,9 @@ | ||
| 187 | 190 | /** |
| 188 | 191 | * Register the favorites delete endpoint. |
| 189 | 192 | */ |
| 190 | 193 | register_rest_route( |
| 191 | - AB_API_NAMESPACE, | |
| 194 | + BS_API_NAMESPACE, | |
| 192 | 195 | FAVORITE_LAYOUTS_ROUTE, |
| 193 | 196 | [ |
| 194 | 197 | 'methods' => 'DELETE', |
| 195 | 198 | 'callback' => function ( $request ) { |
| @@ -220,14 +223,31 @@ | ||
| 220 | 223 | * Register the sections GET endpoint. |
| 221 | 224 | * Returns all registered sections. |
| 222 | 225 | */ |
| 223 | 226 | register_rest_route( |
| 224 | - AB_API_NAMESPACE, | |
| 227 | + BS_API_NAMESPACE, | |
| 225 | 228 | SECTIONS_ROUTE, |
| 226 | 229 | [ |
| 227 | 230 | 'methods' => WP_REST_Server::READABLE, |
| 228 | 231 | 'callback' => function () { |
| 229 | 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 ); | |
| 230 | 250 | }, |
| 231 | 251 | 'permission_callback' => function () { |
| 232 | 252 | return current_user_can( 'edit_posts' ); |
| 233 | 253 | }, |