Commit 398a0dd4 authored by Andrei Mondoc's avatar Andrei Mondoc
Browse files

replace serialised block for all queried posts

parent 7111c34a
......@@ -58,8 +58,8 @@ class CiviCRM_Gutenberg_Blocks {
// render block preview
add_filter( 'rest_post_dispatch', [ $this, 'render_block_preview' ], 10, 3);
// conver serialised block to shortcode
add_action( 'wp', [ $this, 'convert_serialised_block_to_shortcode' ], 5 );
// convert serialised civicrm content blocks to shortcode
add_action( 'wp', [ $this, 'prerender' ], 5 );
}
......@@ -353,31 +353,62 @@ class CiviCRM_Gutenberg_Blocks {
/**
* Replaces all serslised content block
* strings with their shorcode string
* in post_content.
* Determine if a CiviCRM block is present
* in any of the posts about to be displayed
* and replace the serialised block string
* with it's shortcode version.
*
* Based on CiviCRM_For_WordPress_Shortcodes::prerender().
*
* @uses 'wp' action
*
* @since 0.1
*
* @return void
*/
public function convert_serialised_block_to_shortcode() {
public function prerender() {
if ( is_404() ) return;
if ( is_admin() ) return;
if ( have_posts() ) {
while ( have_posts() ) : the_post();
$object = get_queried_object();
global $post;
$post = $this->convert_serialised_block_to_shortcode( $post );
endwhile;
}
// reset loop
rewind_posts();
}
if ( is_null( $object ) || ! $object instanceof WP_Post ) return;
if ( ! has_block( 'civicrm/civicrm-content-block', $object ) ) return;
/**
* Replaces all serslised content block
* strings with their shorcode string
* in post_content.
*
* @since 0.1
*
* @param WP_Post $post
* @return WP_Post $post
*/
public function convert_serialised_block_to_shortcode( $post ) {
if ( is_null( $post ) || ! $post instanceof WP_Post ) return $post;
if ( ! has_block( 'civicrm/civicrm-content-block', $post ) ) return $post;
// get blocks
$blocks = parse_blocks( $object->post_content );
$blocks = parse_blocks( $post->post_content );
// get all civicrm content blocks
$civicrm_content_blocks = array_filter(
$blocks,
function ($block) {
function ( $block ) {
return $block['blockName'] == 'civicrm/civicrm-content-block';
}
);
......@@ -395,21 +426,21 @@ class CiviCRM_Gutenberg_Blocks {
// get all serialised content block strings
preg_match_all(
'/<!-- wp:civicrm\/civicrm-content-block.*\/-->/',
$object->post_content,
$post->post_content,
$serialised_content_blocks
);
// bail if no matches
if ( empty( $serialised_content_blocks[0] ) ) return;
if ( empty( $serialised_content_blocks[0] ) ) return $post;
// replace serialised content block with shortcode
array_map(
function( $key, $block ) use ( &$object, $serialised_content_blocks ) {
function( $key, $block ) use ( &$post, $serialised_content_blocks ) {
$object->post_content = str_replace(
$post->post_content = str_replace(
$serialised_content_blocks[0][$key],
$this->block_render( $block['attrs'] ), // render with defaults
$object->post_content
$post->post_content
);
},
......@@ -417,6 +448,8 @@ class CiviCRM_Gutenberg_Blocks {
$civicrm_content_blocks
);
return $post;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment