From 1c40953d68c3554499859d61bbbb859870fb5ffa Mon Sep 17 00:00:00 2001 From: Adam Wood <1017872+adamwoodnz@users.noreply.github.com> Date: Thu, 9 May 2024 12:18:38 +1200 Subject: [PATCH] Fix compatibility issues with shortcodes in blocks and Gutenberg 18.1+ (#517) * Move article meta logic from blocks to templates and patterns After https://github.com/WordPress/gutenberg/pull/60349, shortcodes passed via block attributes don't work. Shortcodes in block markup in patterns do work, so stop using custom blocks and move the logic to the template level instead. * Remove debug * Use function to get post title --- .../themes/wporg-developer-2023/functions.php | 10 ++- .../wporg-developer-2023/inc/block-hooks.php | 28 ------- .../wporg-developer-2023/inc/shortcodes.php | 11 +++ .../patterns/article-meta-block-editor.php | 55 +++++++++++++ .../patterns/article-meta-github.php | 78 +++++++++++++++++++ .../patterns/article-meta.php | 22 ++++-- .../src/article-meta-date/block.json | 26 ------- .../src/article-meta-date/block.php | 52 ------------- .../src/article-meta-date/index.js | 17 ---- .../single-handbook-block-editor.html | 44 +++++++++++ .../templates/single-handbook-github.html | 44 +++++++++++ 11 files changed, 257 insertions(+), 130 deletions(-) create mode 100644 source/wp-content/themes/wporg-developer-2023/patterns/article-meta-block-editor.php create mode 100644 source/wp-content/themes/wporg-developer-2023/patterns/article-meta-github.php delete mode 100644 source/wp-content/themes/wporg-developer-2023/src/article-meta-date/block.json delete mode 100644 source/wp-content/themes/wporg-developer-2023/src/article-meta-date/block.php delete mode 100644 source/wp-content/themes/wporg-developer-2023/src/article-meta-date/index.js create mode 100644 source/wp-content/themes/wporg-developer-2023/templates/single-handbook-block-editor.html create mode 100644 source/wp-content/themes/wporg-developer-2023/templates/single-handbook-github.html diff --git a/source/wp-content/themes/wporg-developer-2023/functions.php b/source/wp-content/themes/wporg-developer-2023/functions.php index 4f2229822..0472684d4 100644 --- a/source/wp-content/themes/wporg-developer-2023/functions.php +++ b/source/wp-content/themes/wporg-developer-2023/functions.php @@ -150,7 +150,6 @@ require __DIR__ . '/inc/block-hooks.php'; // Block files -require_once __DIR__ . '/src/article-meta-date/block.php'; require_once __DIR__ . '/src/chapter-list/block.php'; require_once __DIR__ . '/src/cli-command-table/block.php'; require_once __DIR__ . '/src/code-changelog/block.php'; @@ -599,12 +598,19 @@ function add_site_navigation_menus( $menus ) { function add_handbook_templates( $templates ) { $is_handbook = function_exists( 'wporg_is_handbook' ) && wporg_is_handbook(); $is_github_source = ! empty( get_post_meta( get_the_ID(), 'wporg_markdown_source', true ) ); + if ( $is_handbook ) { array_unshift( $templates, 'single-handbook.php' ); } + if ( $is_github_source ) { - array_unshift( $templates, 'single-handbook-github.php' ); + if ( get_post_type() === 'blocks-handbook' ) { + array_unshift( $templates, 'single-handbook-block-editor.php' ); + } else { + array_unshift( $templates, 'single-handbook-github.php' ); + } } + return $templates; } diff --git a/source/wp-content/themes/wporg-developer-2023/inc/block-hooks.php b/source/wp-content/themes/wporg-developer-2023/inc/block-hooks.php index 8622d82f9..5c4fec7bf 100644 --- a/source/wp-content/themes/wporg-developer-2023/inc/block-hooks.php +++ b/source/wp-content/themes/wporg-developer-2023/inc/block-hooks.php @@ -7,7 +7,6 @@ use function DevHub\is_parsed_post_type; -add_filter( 'render_block', __NAMESPACE__ . '\filter_handbook_meta_link_block', 10, 2 ); add_filter( 'render_block_data', __NAMESPACE__ . '\modify_header_template_part' ); /** @@ -52,33 +51,6 @@ function get_block_content_by_home_url( $block_content, $replacement_home_url = ); } -/** - * Filters an article meta block and conditionally removes it. - * - * @param string $block_content - * @param array $block - * @return string - */ -function filter_handbook_meta_link_block( $block_content, $block ) { - if ( 'wporg/handbook-meta-link' === $block['blockName'] ) { - // Not all handbooks come from GitHub. - $local_handbooks = array( 'apis-handbook', 'plugin-handbook', 'theme-handbook' ); - $post_type = get_post_type(); - - if ( in_array( $post_type, $local_handbooks ) ) { - return ''; - } - - // The block editor handbook doesn't have a changelog. - // We only know it's the changelog because of the linkURL attribute. - if ( 'blocks-handbook' === $post_type && '[article_changelog_link]' === $block['attrs']['linkURL'] ) { - return ''; - } - } - - return $block_content; -} - /** * Update header template based on current query. * diff --git a/source/wp-content/themes/wporg-developer-2023/inc/shortcodes.php b/source/wp-content/themes/wporg-developer-2023/inc/shortcodes.php index 553940cea..0a8b14e70 100644 --- a/source/wp-content/themes/wporg-developer-2023/inc/shortcodes.php +++ b/source/wp-content/themes/wporg-developer-2023/inc/shortcodes.php @@ -57,6 +57,17 @@ function() { } ); +/** + * Get the title of the article. + */ +add_shortcode( + 'article_title', + function() { + global $post; + return get_the_title(); + } +); + /** * Only display the 'Last updated' if the modified date is later than the publishing date. */ diff --git a/source/wp-content/themes/wporg-developer-2023/patterns/article-meta-block-editor.php b/source/wp-content/themes/wporg-developer-2023/patterns/article-meta-block-editor.php new file mode 100644 index 000000000..51eb95d13 --- /dev/null +++ b/source/wp-content/themes/wporg-developer-2023/patterns/article-meta-block-editor.php @@ -0,0 +1,55 @@ + + + +
+ + +
+ +

+ + + +
+ + + +
+ +

[last_updated]

+ + + +
+ + + +
+ + +

+ + + + + + +
+ +
+ diff --git a/source/wp-content/themes/wporg-developer-2023/patterns/article-meta-github.php b/source/wp-content/themes/wporg-developer-2023/patterns/article-meta-github.php new file mode 100644 index 000000000..71763276c --- /dev/null +++ b/source/wp-content/themes/wporg-developer-2023/patterns/article-meta-github.php @@ -0,0 +1,78 @@ + + + +
+ + +
+ +

+ + + +
+ + + +
+ +

[last_updated]

+ + + +
+ + + +
+ + +

+ + + + + + +
+ + + +
+ + +

+ + + + + + +
+ + +
+ diff --git a/source/wp-content/themes/wporg-developer-2023/patterns/article-meta.php b/source/wp-content/themes/wporg-developer-2023/patterns/article-meta.php index f02b2c2b7..5db560163 100644 --- a/source/wp-content/themes/wporg-developer-2023/patterns/article-meta.php +++ b/source/wp-content/themes/wporg-developer-2023/patterns/article-meta.php @@ -10,13 +10,25 @@
- + +
+ +

+ - + +
+ - - - + +
+ +

[last_updated]

+ + + +
+
diff --git a/source/wp-content/themes/wporg-developer-2023/src/article-meta-date/block.json b/source/wp-content/themes/wporg-developer-2023/src/article-meta-date/block.json deleted file mode 100644 index 79ad9266b..000000000 --- a/source/wp-content/themes/wporg-developer-2023/src/article-meta-date/block.json +++ /dev/null @@ -1,26 +0,0 @@ -{ - "$schema": "https://schemas.wp.org/trunk/block.json", - "apiVersion": 2, - "name": "wporg/article-meta-date", - "version": "0.1.0", - "title": "Article Meta Date", - "category": "widgets", - "icon": "smiley", - "description": "", - "usesContext": [], - "attributes": { - "heading": { - "type": "string", - "default": "" - }, - "displayType": { - "type": "string", - "default": "published" - } - }, - "supports": { - "inserter": false - }, - "textdomain": "wporg", - "editorScript": "file:./index.js" -} diff --git a/source/wp-content/themes/wporg-developer-2023/src/article-meta-date/block.php b/source/wp-content/themes/wporg-developer-2023/src/article-meta-date/block.php deleted file mode 100644 index 856f46544..000000000 --- a/source/wp-content/themes/wporg-developer-2023/src/article-meta-date/block.php +++ /dev/null @@ -1,52 +0,0 @@ - __NAMESPACE__ . '\render', - ) - ); -} - -/** - * Render the block content. - * - * @param array $attributes Block attributes. - * @param string $content Block default content. - * @param WP_Block $block Block instance. - * - * @return string Returns the block markup. - */ -function render( $attributes, $content, $block ) { - return sprintf( - do_blocks( - ' -
- -

%1$s

- - - %2$s -
- ' - ), - esc_html( $attributes['heading'] ), - do_blocks( - sprintf( - '', - esc_js( $attributes['displayType'] ), - ), - ), - ); -} diff --git a/source/wp-content/themes/wporg-developer-2023/src/article-meta-date/index.js b/source/wp-content/themes/wporg-developer-2023/src/article-meta-date/index.js deleted file mode 100644 index abba51c28..000000000 --- a/source/wp-content/themes/wporg-developer-2023/src/article-meta-date/index.js +++ /dev/null @@ -1,17 +0,0 @@ -/** - * WordPress dependencies - */ -import { registerBlockType } from '@wordpress/blocks'; - -/** - * Internal dependencies - */ -import Edit from '../shared/dynamic-edit'; -import metadata from './block.json'; - -registerBlockType( metadata.name, { - /** - * @see ./edit.js - */ - edit: Edit, -} ); diff --git a/source/wp-content/themes/wporg-developer-2023/templates/single-handbook-block-editor.html b/source/wp-content/themes/wporg-developer-2023/templates/single-handbook-block-editor.html new file mode 100644 index 000000000..8eddc7337 --- /dev/null +++ b/source/wp-content/themes/wporg-developer-2023/templates/single-handbook-block-editor.html @@ -0,0 +1,44 @@ + + + + + +
+ + +
+ + +
+ + +
+ + + + + + + + + + +
+ + +
+ + + + + + + + +
+ + +
+ + + diff --git a/source/wp-content/themes/wporg-developer-2023/templates/single-handbook-github.html b/source/wp-content/themes/wporg-developer-2023/templates/single-handbook-github.html new file mode 100644 index 000000000..0b23c4c87 --- /dev/null +++ b/source/wp-content/themes/wporg-developer-2023/templates/single-handbook-github.html @@ -0,0 +1,44 @@ + + + + + +
+ + +
+ + +
+ + +
+ + + + + + + + + + +
+ + +
+ + + + + + + + +
+ + +
+ + +