Skip to content

Commit

Permalink
Issue #31 - attempt to allow recursive reusable block problems to be …
Browse files Browse the repository at this point in the history
…fixed using the editor. Issue #25 - start refactoring of overrides
  • Loading branch information
bobbingwide committed Nov 13, 2020
1 parent 2c66153 commit c615928
Show file tree
Hide file tree
Showing 4 changed files with 500 additions and 359 deletions.
360 changes: 1 addition & 359 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,366 +180,8 @@ function fizzie_post_edit( $attrs, $content, $tag ) {
return $link;
}

/**
* Hook into register_block_types_args before WP_Block_Supports
*/
add_filter( 'register_block_type_args', 'fizzie_register_block_type_args', 9 );

function fizzie_register_block_type_args( $args ) {
if ( 'core/query-pagination' == $args['name']) {
if ( 'gutenberg_render_block_core_query_pagination' == $args['render_callback'] ) {
$args['render_callback'] = 'fizzie_render_block_core_query_pagination';
}
}
if ( 'core/query-loop' == $args['name'] ) {
if ( 'gutenberg_render_block_core_query_loop' == $args['render_callback'] ) {
$args['render_callback'] = 'fizzie_render_block_core_query_loop';
}
}

if ( 'core/post-excerpt' == $args['name'] ) {
if ( 'gutenberg_render_block_core_post_excerpt' == $args['render_callback'] ) {
$args['render_callback'] = 'fizzie_render_block_core_post_excerpt';
}
}

if ( 'core/post-content' == $args['name'] ) {
if ( 'gutenberg_render_block_core_post_content' == $args['render_callback'] ) {
$args['render_callback'] = 'fizzie_render_block_core_post_content';
}
}

if ( 'core/template-part' == $args['name'] ) {
if ( 'gutenberg_render_block_core_template_part' == $args['render_callback'] ) {
$args['render_callback'] = 'fizzie_render_block_core_template_part';
}
}

if ( 'core/navigation' == $args['name'] ) {
if ( 'gutenberg_render_block_core_navigation' == $args['render_callback'] ) {
$args['render_callback'] = 'fizzie_render_block_core_navigation';
}
}

if ( 'core/navigation-link' == $args['name'] ) {
if ( 'gutenberg_render_block_core_navigation_link' == $args['render_callback'] ) {
$args['render_callback'] = 'fizzie_render_block_core_navigation_link';
}
}

if ( 'core/post-hierarchical-terms' == $args['name'] ) {
if ( 'gutenberg_render_block_core_post_hierarchical_terms' == $args['render_callback'] ) {
$args['render_callback'] = 'fizzie_render_block_core_post_hierarchical_terms';
}
}
return $args;
}

/**
* Overrides core/query-pagination to implement main query pagination.
*
* Hack until a solution is delivered in Gutenberg.
*
* @param $attributes
* @param $content
* @param $block
* @return string
*/
function fizzie_render_block_core_query_pagination( $attributes, $content, $block ) {
if ( isset( $block->context['queryId'] ) ) {
$html = gutenberg_render_block_core_query_pagination( $attributes, $content, $block );
} else {
$html = fizzie_render_block_core_query_pagination_main_query( $attributes, $content, $block );
}
return $html;
}

/**
* Overrides core/query-loop to implement main query processing.
*
* Hack until a solution is delivered in Gutenberg.
*
* @param $attributes
* @param $content
* @param $block
* @return string
*/
function fizzie_render_block_core_query_loop( $attributes, $content, $block ) {
if ( isset( $block->context['queryId'] ) ) {
$html = gutenberg_render_block_core_query_loop( $attributes, $content, $block );
} else {
$html = fizzie_render_block_core_query_loop_main_query( $attributes, $content, $block );
}
return $html;
}

/**
* Renders the `core/query-pagination` block on the server for the main query.
*
* @param array $attributes Block attributes.
* @param string $content Block default content.
* @param WP_Block $block Block instance.
*
* @return string Returns the pagination for the query.
*/
function fizzie_render_block_core_query_pagination_main_query( $attributes, $content, $block ) {
$html = '<div class="wp-block-query-pagination">';
$html .= paginate_links( [ 'type' => 'list'] );
$html .= "</div>";
return $html;
}

/**
* Renders the `core/query-loop` block for the main query on the server.
*
* @param array $attributes Block attributes.
* @param string $content Block default content.
* @param WP_Block $block Block instance.
*
* @return string Returns the output of the query, structured using the layout defined by the block's inner blocks.
*/
function fizzie_render_block_core_query_loop_main_query( $attributes, $content, $block ) {
if ( have_posts() ) {
$content = '';
while ( have_posts() ) {
the_post();
$post = get_post();
$content .= (
new WP_Block(
$block->parsed_block,
array(
'postType' => $post->post_type,
'postId' => $post->ID,
)
)
)->render(array('dynamic' => false));
}
} else {
$content = __( "No posts found." );
}
return $content;
}

/**
* Appends the missing </div> to the core/post-excerpt block.
*
* @param $attributes
* @param $content
* @param $block
* @return string
*/
function fizzie_render_block_core_post_excerpt( $attributes, $content, $block ) {
$html = gutenberg_render_block_core_post_excerpt( $attributes, $content, $block );
// Should really check that it's missing.
if ( 0 !== strrpos( $html, '</div>') ) {
$html .= '</div>';
}
return $html;
}

/**
* Overrides core/post-content 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_post_content( $attributes, $content, $block ) {
if ( ! isset( $block->context['postId'] ) ) {
return '';
}
/*
if ( 'revision' === $block->context['postType'] ) {
return '';
}
*/

if ( fizzie_process_this_content( get_the_ID() ) ) {
$html = gutenberg_render_block_core_post_content( $attributes, $content, $block );
} else {
$html = fizzie_report_recursion_error( get_the_ID() );
//$html .= $content;
}

return $html;
}

/**
* 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 $processed_content;
$processed = bw_array_get( $processed_content, $id, false );
if ( !$processed ) {
$processed_content[$id] = $id ;
}
bw_trace2( $processed_content, "processed posts", true, BW_TRACE_DEBUG );
return( !$processed );
}

/**
* 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_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;
}

require_once __DIR__ . '/includes/block-overrides.php';

/**
* /**
* 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;
}

function fizzie_render_block_core_navigation( $attributes, $content, $block ) {
//bw_trace2();
$html = gutenberg_render_block_core_navigation( $attributes, $content, $block );
bw_trace2( $html, "html");
return $html;
}

function fizzie_render_block_core_navigation_link( $attributes, $content, $block ) {
$attributes = fizzie_fiddle_nav_atts( $attributes );
$html = gutenberg_render_block_core_navigation_link($attributes, $content, $block);
return $html;
}

/**
* Fiddles navigation-link attributes to get the required CSS classes.
*
* This is much simpler than the logic in `_wp_menu_item_classes_by_context( $menu_items )`.
*
* @TODO But, we also need to set the other classes such as current-menu-ancestor
* which probably means we have to do this in core/navigation rather than core/navigation-link
*
* @param $attributes
* @return mixed
*/
function fizzie_fiddle_nav_atts( $attributes ) {
$attributes['url'] = str_replace( 'https://s.b/wp56', site_url(), $attributes['url'] );
$request_uri = $_SERVER["REQUEST_URI"];
$request_uri_path = parse_url( $request_uri, PHP_URL_PATH );
$url_path = parse_url( $attributes['url'], PHP_URL_PATH );
$site_url = trailingslashit( parse_url( site_url(), PHP_URL_PATH ) );
/*
echo $request_uri_path;
echo ' ';
echo $url_path;
echo ' ';
echo $site_url;
echo '<br/>';
*/
// We need to avoid the home page: home_url() or site_url()
if ( $url_path === $site_url ) {
if ( $url_path === $request_uri_path ) {
$attributes['className'] = ['current-menu-item'];
} else {
// don't match this $url path with all the other paths

}
} elseif ( 0 === strpos( $request_uri, $url_path ) ) {

// @TODO check that the attributes URL is
//if ( parse_url( $request_uri, PHP_URL_PATH ) === parse_url( $attributes['url'], PHP_URL_PATH ) ) {
$attributes['className'] = ['current-menu-item'];
}
return $attributes;
}

/**
* Renders the `core/post-hierarchical-terms` block on the server.
*
* @param array $attributes Block attributes.
* @param string $content Block default content.
* @param WP_Block $block Block instance.
* @return string Returns the filtered post hierarchical terms for the current post wrapped inside "a" tags.
*/
function fizzie_render_block_core_post_hierarchical_terms( $attributes, $content, $block ) {
//bw_trace2();
if ( ! isset( $block->context['postId'] ) || ! isset( $attributes['term'] ) ) {
return '';
}

$post_hierarchical_terms = get_the_terms( $block->context['postId'], $attributes['term'] );
//bw_trace2( $post_hierarchical_terms, "pht", false );
if ( is_wp_error( $post_hierarchical_terms ) ) {
return 'Taxonomy not recognised';
}
$html=gutenberg_render_block_core_post_hierarchical_terms( $attributes, $content, $block );

return $html;
}


if ( !function_exists( "bw_trace2" ) ) {
Expand Down
Loading

0 comments on commit c615928

Please sign in to comment.