From 373b3fe246d615dd4c1e01f64660211fad7bf112 Mon Sep 17 00:00:00 2001 From: Ian Dunn Date: Tue, 4 Jan 2022 11:05:49 -0800 Subject: [PATCH] Header/Footer: Add API route stubs. These are still experimental and may change significantly. See #45 --- .../blocks/global-header-footer/blocks.php | 57 +++++++++++++++++-- 1 file changed, 52 insertions(+), 5 deletions(-) diff --git a/mu-plugins/blocks/global-header-footer/blocks.php b/mu-plugins/blocks/global-header-footer/blocks.php index beed442c0..bcf0732c7 100644 --- a/mu-plugins/blocks/global-header-footer/blocks.php +++ b/mu-plugins/blocks/global-header-footer/blocks.php @@ -1,14 +1,14 @@ WP_REST_Server::READABLE, + 'callback' => __NAMESPACE__ . '\render_global_header', + ), + ) + ); + + register_rest_route( + 'global-header-footer/v1', + 'footer', + array( + array( + 'methods' => WP_REST_Server::READABLE, + 'callback' => __NAMESPACE__ . '\render_global_footer', + ), + ) + ); +} + /** * Register block types in JS, for the editor. * @@ -166,13 +193,25 @@ function render_global_header() { restore_inner_group_container(); - // Render the classic markup second, so the `wp_head()` call will execute callbacks that blocks added above. - if ( ! wp_is_block_theme() ) { + $is_rest_request = defined( 'REST_REQUEST' ) && REST_REQUEST; + + /* + * Render the classic markup second, so the `wp_head()` call will execute callbacks that blocks added above. + * + * API requests also need `` etc so they can get the styles. + */ + if ( ! wp_is_block_theme() || $is_rest_request ) { ob_start(); require __DIR__ . '/classic-header.php'; $markup = ob_get_clean() . $markup; } + if ( $is_rest_request ) { + header( 'Content-Type: text/html' ); + echo $markup; + die(); // this is an ugly hack. todo get the api to return html + } + return $markup; } @@ -438,13 +477,21 @@ function render_global_footer() { restore_inner_group_container(); + $is_rest_request = defined( 'REST_REQUEST' ) && REST_REQUEST; + // Render the classic markup second, so the `wp_footer()` call will execute callbacks that blocks added. - if ( ! wp_is_block_theme() ) { + if ( ! wp_is_block_theme() || $is_rest_request ) { ob_start(); require_once __DIR__ . '/classic-footer.php'; $markup .= ob_get_clean(); } + if ( $is_rest_request ) { + header( 'Content-Type: text/html' ); + echo $markup; + die(); // this is an ugly hack. todo get the api to return html + } + return $markup; }