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
85 changes: 83 additions & 2 deletions src/data.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,50 @@
use WP_Block;
use pQuery;

function merge_override_content($content_override, $blocks)
{
foreach ($content_override as $key => $value) {
if (!isset($value['content']) && !isset($value['text']) && !isset($value['url'])) {
continue;
}

foreach ($blocks as &$block) {
if (isset($block['attrs']['metadata']['name'])) {
if ($block['attrs']['metadata']['name'] === $key) {
if (isset($value['content'])) {
$block['attrs']['content'] = $value['content'];
}
if (isset($value['text'])) {
$block['attrs']['text'] = $value['text'];
}
if (isset($value['linkTarget'])) {
$block['attrs']['linkTarget'] = $value['linkTarget'];
}
if (isset($value['rel'])) {
$block['attrs']['rel'] = $value['rel'];
}
if (isset($value['url'])) {
$block['attrs']['url'] = $value['url'];
}
if (isset($value['id'])) {
$block['attrs']['id'] = $value['id'];
}
if (isset($value['alt'])) {
$block['attrs']['alt'] = $value['alt'];
}
if (isset($value['title'])) {
$block['attrs']['title'] = $value['title'];
}
}
}
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 +85,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 Expand Up @@ -69,8 +138,20 @@ function handle_do_block( array $block, $post_id = 0 ) {
}
}

$block['rendered'] = $block_object->render();
$block['rendered'] = do_shortcode( $block['rendered'] );
// Process shortcodes
$is_supports_shortcode_for_blocks = $block['blockName'] === 'core/paragraph' || $block['blockName'] === 'core/heading';
if($is_supports_shortcode_for_blocks){
if (isset($attr['content'])) {
$attr['content'] = do_shortcode($attr['content']);
}
if (isset($attr['text'])) {
$attr['text'] = do_shortcode($attr['text']);
}
}

// * Removed by Ava
// $block['rendered'] = $block_object->render();
// $block['rendered'] = do_shortcode( $block['rendered'] );
$block['attrs'] = $attr;
if ( ! empty( $block['innerBlocks'] ) ) {
$inner_blocks = $block['innerBlocks'];
Expand Down
8 changes: 5 additions & 3 deletions wp-rest-blocks.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
/**
* Plugin Name: REST API Blocks
* Plugin Name: REST API Blocks (Edited by AVA)
* Plugin URI: https://github.com/spacedmonkey/wp-rest-blocks
* Description: Add gutenberg blocks data into post / page / widget REST API endpoints.
* Author: Jonathan Harris
* Description: Add gutenberg blocks data into post / page / widget REST API endpoints. (Edited by AVA to support sync pattern and override content)
* Author: Jonathan Harris (Edited by AVA)
* Author URI: https://www.spacedmonkey.com/
* Text Domain: wp-rest-blocks
* Domain Path: /languages
Expand All @@ -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