forked from kellenmace/headless-block-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheadless-block-parser.php
67 lines (59 loc) · 2.26 KB
/
headless-block-parser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
/**
* Plugin Name: Headless Block Parser
* Description: Custom Gutenberg block parser that replaces internal link URL domains with that of the decoupled frontend JS app.
* Version: 0.1.0
* Author: Kellen Mace
* Author URI: https://kellenmace.com/
* License: GPLv2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
/**
* Custom block parser for headless WordPress sites.
*/
class Headless_Block_Parser extends WP_Block_Parser
{
/**
* Parses a document and returns a list of block structures
*
* When encountering an invalid parse will return a best-effort
* parse. In contrast to the specification parser this does not
* return an error on invalid inputs.
*
* @param string $document Input document being parsed.
*
* @return WP_Block_Parser_Block[]
*/
public function parse($document): array
{
$is_graphql_request = function_exists('is_graphql_request') && is_graphql_request();
$is_rest_request = defined('REST_REQUEST');
// Don't modify the document if this is not a GraphQL or REST API request.
if (!$is_graphql_request && !$is_rest_request) {
return parent::parse($document);
}
$document_with_replacements = $this->replace_internal_link_url_domains($document);
return parent::parse($document_with_replacements);
}
/**
* Rewrite internal link URLs to point to the decoupled frontend app.
*
* @param string $document Input document being parsed.
*
* @return string $document Input document with internal link URL domains replaced.
*/
private function replace_internal_link_url_domains(string $document): string
{
// TODO: Get this value from an environment variable or the database.
// If you're using Faust.js, you can call wpe_headless_get_setting( 'frontend_uri' )
$frontend_app_url = 'https://alippo.com';
$site_url = site_url();
return str_replace('href="' . $site_url, 'data-internal-link="true" href="' . $frontend_app_url, $document);
}
}
/**
* Register a custom Gutenberg block parser.
*
* @return string Name of block parser class.
*/
add_filter('block_parser_class', fn (): string => 'Headless_Block_Parser');