Skip to content

Commit

Permalink
Override gutenberg rendering. Issue #5: don't run wpautop on template…
Browse files Browse the repository at this point in the history
… parts. Issue #7: cater for unregistered taxonomies
  • Loading branch information
bobbingwide committed Mar 25, 2021
1 parent 3b9cb8b commit 4c61939
Show file tree
Hide file tree
Showing 5 changed files with 264 additions and 0 deletions.
3 changes: 3 additions & 0 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,6 @@ function sb_init() {
}

add_action( 'init', 'sb_init', 20);

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

23 changes: 23 additions & 0 deletions includes/block-override-functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* Overrides a core block's render_callback method, if required.
*
* For the given blockname, if the overriding function is available
* and the current callback is the gutenberg function
* replace the render_callback with our own function.
*
* @param array $args Block attributes.
* @param string $blockname The block name to test for.
* @param string $render_callback The common suffix for the block's callback function.
* @return array Block attributes.
*/
function sb_maybe_override_block($args, $blockname, $render_callback)
{
$sb_render_callback = 'sb_' . $render_callback;
if ($blockname == $args['name'] && function_exists($sb_render_callback)) {
if ('gutenberg_' . $render_callback == $args['render_callback']) {
$args['render_callback'] = $sb_render_callback;
}
}
return $args;
}
53 changes: 53 additions & 0 deletions includes/block-overrides.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

/**
* Implements block overrides as required.
*
*/
require_once __DIR__ . '/block-override-functions.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 to automatically determine whether or not to include the file.
*/
//require_once __DIR__ . '/query-pagination.php';
//require_once __DIR__ . '/query-pagination-numbers.php';
//require_once __DIR__ . '/query-loop.php';
//require_once __DIR__ . '/post-excerpt.php';
//require_once __DIR__ . '/post-content.php';
require_once __DIR__ . '/template-part.php';
//require_once __DIR__ . '/navigation.php';
//require_once __DIR__ . '/navigation-link.php';
//require_once __DIR__ . '/post-hierarchical-terms.php';
//require_once __DIR__ . '/block.php';
require_once __DIR__ . '/tag-cloud.php';

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

/**
* Implements overrides for core blocks which we need to improve.
*
* @param array $args Block attributes.
* @return array
*/
function sb_register_block_type_args( $args ) {
$args = sb_maybe_override_block( $args,'core/query-pagination', 'render_block_core_query_pagination');
$args = sb_maybe_override_block( $args,'core/query-pagination-numbers', 'render_block_core_query_pagination_numbers');

$args = sb_maybe_override_block( $args,'core/query-loop', 'render_block_core_query_loop' );
$args = sb_maybe_override_block( $args,'core/post-excerpt', 'render_block_core_post_excerpt' );
$args = sb_maybe_override_block( $args,'core/post-content', 'render_block_core_post_content' );
$args = sb_maybe_override_block( $args,'core/template-part', 'render_block_core_template_part' );
$args = sb_maybe_override_block( $args,'core/navigation', 'render_block_core_navigation' );
$args = sb_maybe_override_block( $args,'core/navigation-link', 'render_block_core_navigation_link' );
$args = sb_maybe_override_block( $args,'core/post-hierarchical-terms', 'render_block_core_post_hierarchical_terms' );
$args = sb_maybe_override_block( $args,'core/block', 'render_block_core_block' );
$args = sb_maybe_override_block( $args,'core/tag-cloud', 'render_block_core_tag_cloud' );
return $args;
}
55 changes: 55 additions & 0 deletions includes/tag-cloud.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* Server-side rendering of the `core/tag-cloud` block.
*
* @package WordPress
*/

/**
* Renders the `core/tag-cloud` block on server.
*
* @param array $attributes The block attributes.
*
* @return string Returns the tag cloud for selected taxonomy.
*/
function sb_render_block_core_tag_cloud( $attributes ) {
$args = array(
'echo' => false,
'taxonomy' => $attributes['taxonomy'],
'show_count' => $attributes['showTagCounts'],
);
$tag_cloud = wp_tag_cloud( $args );

if ( ! $tag_cloud ) {
$taxonomy = get_taxonomy($attributes['taxonomy']);
if ($taxonomy) {
$labels = get_taxonomy_labels(get_taxonomy($attributes['taxonomy']));
$tag_cloud = esc_html(
sprintf(
/* translators: %s: taxonomy name */
__('Your site doesn&#8217;t have any %s, so there&#8217;s nothing to display here at the moment.'),
strtolower($labels->name)
)
);
} else {
$tag_cloud = esc_html(
sprintf(
/* translators: %s: taxonomy name */
__('Tag cloud not displayed. Taxonomy %s is not registered.'), $attributes['taxonomy']

)
);
}

}

$wrapper_attributes = get_block_wrapper_attributes();

return sprintf(
'<p %1$s>%2$s</p>',
$wrapper_attributes,
$tag_cloud
);
}


130 changes: 130 additions & 0 deletions includes/template-part.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php
/**
* Server-side rendering of the `core/template-part` block.
*
* @package WordPress
*/

/**
* Renders the `core/template-part` block on the server.
*
* @param array $attributes The block attributes.
*
* @return string The render.
*/
function sb_render_block_core_template_part( $attributes ) {
static $seen_ids = array();
bw_trace2();
bw_backtrace();

$template_part_id = null;
$content = null;
$area = WP_TEMPLATE_PART_AREA_UNCATEGORIZED;

if ( ! empty( $attributes['postId'] ) && get_post_status( $attributes['postId'] ) ) {
$template_part_id = $attributes['postId'];
// If we have a post ID and the post exists, which means this template part
// is user-customized, render the corresponding post content.
$content = get_post( $attributes['postId'] )->post_content;
} elseif (
isset( $attributes['slug'] ) &&
isset( $attributes['theme'] ) &&
wp_get_theme()->get_stylesheet() === $attributes['theme']
) {
$template_part_id = $attributes['theme'] . '//' . $attributes['slug'];
$template_part_query = new WP_Query(
array(
'post_type' => 'wp_template_part',
'post_status' => 'publish',
'post_name__in' => array( $attributes['slug'] ),
'tax_query' => array(
array(
'taxonomy' => 'wp_theme',
'field' => 'slug',
'terms' => $attributes['theme'],
),
),
'posts_per_page' => 1,
'no_found_rows' => true,
)
);
$template_part_post = $template_part_query->have_posts() ? $template_part_query->next_post() : null;
if ( $template_part_post ) {
// A published post might already exist if this template part was customized elsewhere
// or if it's part of a customized template.
$content = $template_part_post->post_content;
$area_terms = get_the_terms( $template_part_post, 'wp_template_part_area' );
if ( ! is_wp_error( $area_terms ) && false !== $area_terms ) {
$area = $area_terms[0]->name;
}
} else {
// Else, if the template part was provided by the active theme,
// render the corresponding file content.
$template_part_file_path = get_stylesheet_directory() . '/block-template-parts/' . $attributes['slug'] . '.html';
if ( 0 === validate_file( $attributes['slug'] ) && file_exists( $template_part_file_path ) ) {
$content = file_get_contents( $template_part_file_path );
}
}
}

if ( is_null( $content ) && is_user_logged_in() ) {
//print_r( $attributes );
return sprintf(
/* translators: %s: Template part slug. */
__( 'Template part has been deleted or is unavailable: %s' ),
$attributes['slug']
);
}

if ( isset( $seen_ids[ $template_part_id ] ) ) {
if ( ! is_admin() ) {
trigger_error(
sprintf(
// translators: %s are the block attributes.
__( 'Could not render Template Part block with the attributes: <code>%s</code>. Block cannot be rendered inside itself.' ),
wp_json_encode( $attributes )
),
E_USER_WARNING
);
}

// WP_DEBUG_DISPLAY must only be honored when WP_DEBUG. This precedent
// is set in `wp_debug_mode()`.
$is_debug = defined( 'WP_DEBUG' ) && WP_DEBUG &&
defined( 'WP_DEBUG_DISPLAY' ) && WP_DEBUG_DISPLAY;
return $is_debug ?
// translators: Visible only in the front end, this warning takes the place of a faulty block.
__( '[block rendering halted]' ) :
'';
}

// Run through the actions that are typically taken on the_content.
$seen_ids[ $template_part_id ] = true;
$content = do_blocks( $content );
unset( $seen_ids[ $template_part_id ] );
$content = wptexturize( $content );
$content = convert_smilies( $content );
//$content = wpautop( $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 );

if ( empty( $attributes['tagName'] ) ) {
$area_tags = array(
WP_TEMPLATE_PART_AREA_HEADER => 'header',
WP_TEMPLATE_PART_AREA_FOOTER => 'footer',
WP_TEMPLATE_PART_AREA_UNCATEGORIZED => 'div',
);
$html_tag = null !== $area && isset( $area_tags[ $area ] ) ? $area_tags[ $area ] : $area_tags[ WP_TEMPLATE_PART_AREA_UNCATEGORIZED ];
} else {
$html_tag = esc_attr( $attributes['tagName'] );
}
$wrapper_attributes = get_block_wrapper_attributes();

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

0 comments on commit 4c61939

Please sign in to comment.