Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@
"support": {
"issues": "https://github.com/spacedmonkey/wp-rest-blocks/issues"
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/guyavatrade/pquery"
}
],
"require": {
"php": "^7.0 || ^8.0",
"composer/installers": "^1.10",
"tburry/pquery": "^1.1"
"guyavatrade/pquery": "^1.1"
},
"require-dev": {
"phpcompatibility/phpcompatibility-wp": "^2.1",
Expand Down
44 changes: 44 additions & 0 deletions src/data.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,25 @@
use WP_Block;
use pQuery;

function merge_override_content($content_override, $blocks)
{
foreach ($content_override as $key => $value) {
$content = $value['content'];

foreach ($blocks as &$block) {
if (isset($block['attrs']['metadata']['name'])) {
if ($block['attrs']['metadata']['name'] === $key) {
$block['attrs']['content'] = $content;
}
}
if (!empty($block['innerBlocks'])) {
$block['innerBlocks'] = merge_override_content($content_override, $block['innerBlocks']);
}
}
}
return $blocks;
}

/**
* Get blocks from html string.
*
Expand Down Expand Up @@ -41,6 +60,31 @@ function get_blocks( $content, $post_id = 0 ) {
* @return array|false
*/
function handle_do_block( array $block, $post_id = 0 ) {
// Sync Patterns: Parsing and processing the pattern inner blocks.
if ($block['blockName'] === 'core/block' && isset($block['attrs']['ref']) && !empty($block['attrs']['ref'])) {
$sync_pattern = get_post($block['attrs']['ref']);

$content_override = [];
if (isset($block['attrs']['content'])) {
$content_override = $block['attrs']['content'];
}

if ($sync_pattern && 'wp_block' === $sync_pattern->post_type) {
// parse the inner blocks
$block['innerBlocks'] = parse_blocks($sync_pattern->post_content);
// remove the empty blocks
$sync_inner_blocks = [];
foreach ($block['innerBlocks'] as $_block) {
if ($_block['blockName']) {
$sync_inner_blocks[] = $_block;
}
}
$block['innerBlocks'] = $sync_inner_blocks;
// merge the content override
$block['innerBlocks'] = merge_override_content($content_override, $block['innerBlocks']);
}
}

if ( ! $block['blockName'] ) {
return false;
}
Expand Down
2 changes: 2 additions & 0 deletions wp-rest-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
use WP_REST_Blocks\Posts;
use WP_REST_Blocks\Widgets;

if ( is_admin() ) return;

if ( is_readable( __DIR__ . '/vendor/autoload.php' ) ) {
require_once __DIR__ . '/vendor/autoload.php';
}
Expand Down