From 4c61939f27bd611ae8ff38d32e26d1457e994c83 Mon Sep 17 00:00:00 2001 From: Herb Miller Date: Thu, 25 Mar 2021 09:53:24 +0000 Subject: [PATCH] Override gutenberg rendering. Issue #5: don't run wpautop on template parts. Issue #7: cater for unregistered taxonomies --- functions.php | 3 + includes/block-override-functions.php | 23 +++++ includes/block-overrides.php | 53 +++++++++++ includes/tag-cloud.php | 55 +++++++++++ includes/template-part.php | 130 ++++++++++++++++++++++++++ 5 files changed, 264 insertions(+) create mode 100644 includes/block-override-functions.php create mode 100644 includes/block-overrides.php create mode 100644 includes/tag-cloud.php create mode 100644 includes/template-part.php diff --git a/functions.php b/functions.php index d5ad5cb..c929871 100644 --- a/functions.php +++ b/functions.php @@ -91,3 +91,6 @@ function sb_init() { } add_action( 'init', 'sb_init', 20); + +require_once __DIR__ . '/includes/block-overrides.php'; + diff --git a/includes/block-override-functions.php b/includes/block-override-functions.php new file mode 100644 index 0000000..b4fae0e --- /dev/null +++ b/includes/block-override-functions.php @@ -0,0 +1,23 @@ + 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’t have any %s, so there’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( + '

%2$s

', + $wrapper_attributes, + $tag_cloud + ); +} + + diff --git a/includes/template-part.php b/includes/template-part.php new file mode 100644 index 0000000..7bb2120 --- /dev/null +++ b/includes/template-part.php @@ -0,0 +1,130 @@ +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: %s. 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( ']]>', ']]>', $content ) . ""; +} +