Skip to content

Commit

Permalink
Issues #27 & #30 - improve solution to detect recursive post-content …
Browse files Browse the repository at this point in the history
…blocks for template-parts.
  • Loading branch information
bobbingwide committed Nov 12, 2020
1 parent e186112 commit 2c66153
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 43 deletions.
8 changes: 8 additions & 0 deletions block-template-parts/issue-27.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<ul>
<li>Issue-27 test case.
<!-- wp:template-part { "slug": "issue-30", "theme": "fizzie" } /-->
</li>
</ul>



8 changes: 8 additions & 0 deletions block-template-parts/issue-30.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<ul>
<li>Issue-30 test case.
<!-- wp:template-part { "slug": "issue-27", "theme": "fizzie" } /-->
</li>
</ul>



4 changes: 4 additions & 0 deletions block-templates/404.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

<!-- wp:template-part {"slug":"breadcrumbs","theme":"fizzie", "className": "breadcrumbs"} /-->

<!-- wp:template-part {"slug":"issue-27","theme":"fizzie"} /-->

<!-- wp:template-part {"slug":"issue-30","theme":"fizzie"} /-->

<!-- wp:template-part { "slug":"category-query", "theme":"fizzie", "className": "main"} /-->

<!-- wp:template-part {"slug":"search","theme":"fizzie"} /-->
Expand Down
108 changes: 89 additions & 19 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ function fizzie_after_setup_theme()
* Enables oik based shortcodes.
*/
function fizzie_init() {
do_action( "oik_add_shortcodes" );
if (function_exists('bw_add_shortcode')) {
do_action("oik_add_shortcodes");
}
add_shortcode( 'archive_description', 'fizzie_archive_description' );
add_shortcode( 'post-edit', 'fizzie_post_edit' );
}
Expand Down Expand Up @@ -349,53 +351,113 @@ function fizzie_render_block_core_post_content( $attributes, $content, $block )
if ( ! isset( $block->context['postId'] ) ) {
return '';
}
/*
if ( 'revision' === $block->context['postType'] ) {
return '';
}
*/

if ( fizzie_process_this_post( get_the_ID() ) ) {
if ( fizzie_process_this_content( get_the_ID() ) ) {
$html = gutenberg_render_block_core_post_content( $attributes, $content, $block );
} else {
$html = "Invalid use of post-content block. postId:" . $block->context['postId'];
$html .= $content;
$html = fizzie_report_recursion_error( get_the_ID() );
//$html .= $content;
}

return $html;
}

/**
* Determine whether or not to process this post
* Determines whether or not to process this content.
*
* @param integer $id - the post ID
* @param string|integer Unique ID for the content
* @return bool - true if the post has not been processed. false otherwise
*/
function fizzie_process_this_post( $id ) {
global $processed_posts;
$processed = bw_array_get( $processed_posts, $id, false );
function fizzie_process_this_content( $id ) {
global $processed_content;
$processed = bw_array_get( $processed_content, $id, false );
if ( !$processed ) {
$processed_posts[$id] = $id ;
$processed_content[$id] = $id ;
}
bw_trace2( $processed_posts, "processed posts", true, BW_TRACE_DEBUG );
bw_trace2( $processed_content, "processed posts", true, BW_TRACE_DEBUG );
return( !$processed );
}

/**
* Clear the array of processed posts
* Pops or clears the array of processed content.
*
* As we return to the previous level we can clear the processed content.
* Basically this is something we have to do while processing certain inner blocks:
*
* - core/post-content
* - core/template-part
* - core/post-excerpt - possibly
* - core/block - possibly
*
* Note: The top level is within the template, which loads the template parts and/or queries.
*/
function fizzie_clear_processed_posts() {
global $processed_posts;
$processed_posts = array();
bw_trace2( $processed_posts, "cleared", false, BW_TRACE_DEBUG );
function fizzie_clear_processed_content( $id=null ) {
global $processed_content;
if ( $id ) {
array_pop( $processed_content );
} else {
$processed_content = array();
}
bw_trace2( $processed_content, "cleared", false, BW_TRACE_DEBUG );
}

/**
* Reports a recursion error to the user.
*
* If WP_DEBUG is true then additional information is displayed.
*
* @param $id string|integer recursive ID detected
* @param $type string content type
* @return string
*/
function fizzie_report_recursion_error( $id, $type='post-content') {
$content = array();
$content[] = '<div class="recursion-error">';
switch ( $type ) {
case 'post-content':
$content[] = __( 'Content not available; already processed.', 'fizzie' );
break;
case 'template-part':
$content[] = __( 'Template part not processed to avoid infinite recursion', 'fizzie');
break;
default:
$content[] = __( 'Infinite recursion error prevented', 'fizzie');
}
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
$content[] = "<span class=\"recursion-error-context $type\">";
$content[] = '<br />';
$content[] = $id;
$content[] = '<br />';
$content[] = $type;
$content[] = '<br />';
global $processed_content;
$content[] = implode( ',', $processed_content );
$content[] = '</span>';
}
$content[] = '</div>';
$content = implode( " \n", $content);
return $content;
}


function fizzie_render_block_core_template_part( $attributes, $content, $block ) {
fizzie_clear_processed_posts();
/**
* /**
* Overrides core/template-part to return early in certain circumstances.
*
* Hack until a solution is delivered in Gutenberg.
*
* @param $attributes
* @param $content
* @param $block
* @return string
*/
function fizzie_render_block_core_template_part( $attributes, $content, $block ) {
require_once __DIR__ . '/template-part.php';

$html = fizzie_lazy_render_block_core_template_part( $attributes, $content, $block );
return $html;
}
Expand Down Expand Up @@ -477,4 +539,12 @@ function fizzie_render_block_core_post_hierarchical_terms( $attributes, $content
$html=gutenberg_render_block_core_post_hierarchical_terms( $attributes, $content, $block );

return $html;
}


if ( !function_exists( "bw_trace2" ) ) {
function bw_trace2( $content, $args) {
return $content;
}

}
12 changes: 12 additions & 0 deletions style.css
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,10 @@ img {

}

.archive .wp-block-group.article:nth-of-type(3n+1) {
clear: both;
}

div.wp-site-blocks .content { max-width: 1100px; margin: 0 auto;}
.content:after { clear:both; display: table; content: " ";}

Expand Down Expand Up @@ -545,5 +549,13 @@ input, select, textarea {
width: 100%;
}

div.recursion-error {
display: block;
margin: 0 auto;
border: 1px solid grey;
max-width: 1140px;
padding: 5px;
}



83 changes: 59 additions & 24 deletions template-part.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,41 +115,76 @@ function fizzie_load_template_part_file_by_slug( $slug, $template_part_file_path
return $content;
}

/**
* Returns the unique template ID.
*
* The template ID is a concatenation of strings with a separator. eg slug:theme:postID
* The separator should not be something that could be used in a slug or theme name.
* Colon's not valid in a Windows directory name.
*
* @param $attributes array Attributes passed to the template-part
* @return string Template ID
*/
function fizzie_get_template_id( $attributes ) {
$parts = [];
$parts[] = isset( $attributes['slug'] ) ? $attributes['slug'] : '';
$parts[] = isset( $attributes['theme'] ) ? $attributes['theme'] : '';
$parts[] = isset( $attributes['postId'] ) ? $attributes['postId'] : '0';
$template_id = implode( ':', $parts );
return $template_id;
}

/**
* Renders the template part if it's not recursive.
*
* @param $attributes
* @param $content
* @param $block
* @return string
*/

function fizzie_lazy_render_block_core_template_part( $attributes, $content, $block ) {
$content = null;
$template_part_file_path = null;
$postId = null;
//bw_trace2();
//bw_trace2( $attributes);
//bw_backtrace();
$template_id = fizzie_get_template_id( $attributes );

if ( fizzie_process_this_content( $template_id ) ) {

$content = fizzie_load_template_part($attributes);
//bw_trace2( $content, "raw content" );

$content = fizzie_load_template_part( $attributes );
//bw_trace2( $content, "raw content" );

// Run through the actions that are typically taken on the_content.
$content = do_blocks( $content );
$content = wptexturize( $content );
$content = convert_smilies( $content );
// Should we run wpautop() here?
//$before = $content;
//$content = wpautop( $content );
/*
if ( 0 !== strcmp( $before, $content ) ) {
// Run through the actions that are typically taken on the_content.
$content = do_blocks($content);
$content = wptexturize($content);
$content = convert_smilies($content);
// Should we run wpautop() here?
//$before = $content;
//$content = wpautop( $content );
/*
if ( 0 !== strcmp( $before, $content ) ) {
bw_trace2( $before, "before", false );
bw_trace2( $content, "after", false );
bw_trace2(bw_trace_hexdump( $before ), "before wpautop", false);
bw_trace2(bw_trace_hexdump( $content ), "after wpautop", false);
}
}
*/
$content = shortcode_unautop( $content );
if ( function_exists( 'wp_filter_content_tags' ) ) {
$content = wp_filter_content_tags( $content );
*/
$content = shortcode_unautop($content);
if (function_exists('wp_filter_content_tags')) {
$content = wp_filter_content_tags($content);
} else {
$content = wp_make_content_images_responsive($content);
}
$content = do_shortcode($content);
$html_tag = esc_attr($attributes['tagName']);
$wrapper_attributes = get_block_wrapper_attributes();
$html = "<$html_tag $wrapper_attributes>" . str_replace(']]>', ']]&gt;', $content) . "</$html_tag>";
fizzie_clear_processed_content( $template_id );
} else {
$content = wp_make_content_images_responsive( $content );
$html = fizzie_report_recursion_error( $template_id, "template-part");
}
$content = do_shortcode( $content );
$html_tag = esc_attr( $attributes['tagName'] );
$wrapper_attributes = get_block_wrapper_attributes();

return "<$html_tag $wrapper_attributes>" . str_replace( ']]>', ']]&gt;', $content ) . "</$html_tag>";
return $html;
}

0 comments on commit 2c66153

Please sign in to comment.