Skip to content

Commit

Permalink
Issue #33 - refactor block-override-functions as Fizzie_Block_Recursi…
Browse files Browse the repository at this point in the history
…on_Control
  • Loading branch information
bobbingwide committed Nov 14, 2020
1 parent 7f72fbd commit 43ead1c
Show file tree
Hide file tree
Showing 3 changed files with 238 additions and 132 deletions.
143 changes: 13 additions & 130 deletions includes/block-override-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* Implements common functions used in block overrides
*
*/
require_once __DIR__ . '/class-block-recursion-control.php';


/**
* Overrides a core block's render_callback method, if required.
Expand All @@ -26,40 +28,15 @@ function fizzie_maybe_override_block( $args, $blockname, $render_callback ) {
return $args;
}


/**
* Determines whether or not to process this content.
*
* @param string|integer Unique ID for the content
* @return bool - true if the post has not been processed. false otherwise
*/
function fizzie_process_this_content( $id ) {
global $fizzie_processed_content;
// Use this rather than context['postId']
global $post;
$fizzie_processed = bw_array_get( $fizzie_processed_content, $id, false );

/*
if ( empty( $fizzie_processed_content ) ) {
$global_id = isset( $post ) ? $post->ID : null;
if ( $global_id ) {
$fizzie_processed_content[ $global_id ] = $global_id;
}
}
*/


if ( !$fizzie_processed ) {
$fizzie_processed_content[$id] = $id ;
}

/** Stop when it looks highly likely we've missed something */
if ( count( $fizzie_processed_content ) > 10 ) {
$fizzie_processed = true;

}
bw_trace2( $fizzie_processed_content, "processed posts", true, BW_TRACE_DEBUG );
return( !$fizzie_processed );
function fizzie_process_this_content( $id ) {
$recursion_control = Fizzie_Block_Recursion_Control::get_instance();
return $recursion_control->process_this_content( $id );
}

/**
Expand All @@ -74,15 +51,12 @@ function fizzie_process_this_content( $id ) {
* - core/block - possibly
*
* Note: The top level is within the template, which loads the template parts and/or queries.
*
* @param string|integer $id
*/
function fizzie_clear_processed_content( $id=null ) {
global $fizzie_processed_content;
if ( $id ) {
array_pop( $fizzie_processed_content );
} else {
$fizzie_processed_content = array();
}
bw_trace2( $fizzie_processed_content, "cleared", false, BW_TRACE_DEBUG );
$recursion_control = Fizzie_Block_Recursion_Control::get_instance();
$recursion_control->clear_processed_content( $id );
}

/**
Expand All @@ -92,102 +66,11 @@ function fizzie_clear_processed_content( $id=null ) {
*
* @param $id string|integer recursive ID detected
* @param $type string content type
* @return string
* @return string HTML reporting the error to the user
*/
function fizzie_report_recursion_error( $id, $type='core/post-content') {
bw_trace2();
bw_backtrace();
global $bad_id, $bad_fizzie_processed_content, $fizzie_processed_content;
$bad_id = $id;
$bad_fizzie_processed_content = $fizzie_processed_content;

$content = array();
$content[] = '<div class="recursion-error">';
switch ( $type ) {
case 'core/post-content':
$content[] = __( 'Content not available; already processed.', 'fizzie' );
break;
case 'core/template-part':
$content[] = __( 'Template part not processed to avoid infinite recursion', 'fizzie');
break;
case 'core/block':
$content[] = __( 'Reusable block not processed to avoid infinite recursion', 'fizzie');
fizzie_add_filter_rest_prepare_wp_block();
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 $fizzie_processed_content;
$content[] = implode( ',', $fizzie_processed_content );
$content[] = '</span>';
}
$content[] = '</div>';
$content = implode( " \n", $content);
return $content;
}

function fizzie_add_filter_rest_prepare_wp_block() {
// We only need to do this in REST API processing
// But this will only be called in REST API processing
// So no need to check
add_filter('rest_prepare_wp_block', 'fizzie_rest_prepare_wp_block', 10, 3);
$recursion_control = Fizzie_Block_Recursion_Control::get_instance();
$html = $recursion_control->report_recursion_error($id, $type);
return $html;
}

/**
*
* Fiddle with the raw content to remove the wp:block block.
*
* [raw] => <!-- wp:paragraph -->
<p>This is reusable block 21117</p>
<!-- /wp:paragraph -->
<!-- wp:block {"ref":1134} /-->
* @param $response
* @param $post
* @param $request
* @return mixed#
*/
function fizzie_rest_prepare_wp_block( $response, $post, $request ) {
bw_trace2();
bw_backtrace();
global $bad_id;
global $bad_fizzie_processed_content;
$content = $response->data['content']['raw'];
$content = fizzie_replace_bad( $content, $bad_id );
foreach ( $bad_fizzie_processed_content as $id ) {
$content = fizzie_replace_bad( $content, $id );
}
// Convert any remaining to a missing block
$content = str_replace( "wp:block {", "missing {", $content );

$response->data['content']['raw'] = $content;
bw_trace2( $response, "response", false );
return $response;
}

function fizzie_replace_bad( $content, $bad_id ) {
$replacement_block = "<!-- wp:paragraph -->";
$replacement_block .= sprintf(__('Error: Recursion was detected while loading the reusable block with post ID %1$s. '), $bad_id);
$replacement_block .= fizzie_title_link($bad_id);
$replacement_block .= '<br />';
$replacement_block .= "Recommendation: Please delete this block.";
$replacement_block .= "<!-- /wp:paragraph -->";
$content = str_replace("<!-- wp:block {\"ref\":$bad_id} /-->", $replacement_block, $content);
return $content;
}


function fizzie_title_link( $id ) {
$link = sprintf( '<a href="%s">%s</a>', esc_url( get_permalink( $id ) ), esc_html( get_the_title( $id ) ) );
return $link;
}



4 changes: 2 additions & 2 deletions includes/block-overrides.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
*
*/
require_once __DIR__ . '/block-override-functions.php';
require_once __DIR__ . '/class-block-recursion-control.php';

/**
* Here we include the blocks we want to override.
*
* Either comment out the ones that aren't needed any more - when Gutenberg/core's
* satisfies the requirement
* or find another way of automatically determining whether or not to include the file.
* or find another way to automatically determine whether or not to include the file.
*/
require_once __DIR__ . '/query-pagination.php';
require_once __DIR__ . '/query-loop.php';
Expand All @@ -27,7 +28,6 @@
*/
add_filter( 'register_block_type_args', 'fizzie_register_block_type_args', 9 );


/**
* Implements overrides for core blocks which we need to improve.
*
Expand Down
Loading

0 comments on commit 43ead1c

Please sign in to comment.