From e3d999a911979f1b9b6f170579d513c24dbe093e Mon Sep 17 00:00:00 2001 From: Nik Tsekouras Date: Tue, 8 Jun 2021 19:03:17 +0300 Subject: [PATCH] Rename QueryLoop to PostTemplate and change Query label (#32514) --- lib/block-patterns.php | 28 ++-- lib/blocks.php | 2 +- packages/block-library/src/editor.scss | 2 +- packages/block-library/src/index.js | 4 +- .../{query-loop => post-template}/block.json | 10 +- .../src/{query-loop => post-template}/edit.js | 2 +- .../{query-loop => post-template}/editor.scss | 2 +- .../{query-loop => post-template}/index.js | 0 .../block-library/src/post-template/index.php | 148 ++++++++++++++++++ .../src/{query-loop => post-template}/save.js | 2 +- .../{query-loop => post-template}/style.scss | 6 + .../block-library/src/query-loop/index.php | 88 ----------- packages/block-library/src/query/block.json | 2 +- .../block-library/src/query/edit/index.js | 2 +- .../block-library/src/query/variations.js | 9 +- packages/block-library/src/style.scss | 2 +- packages/blocks/src/api/parser.js | 6 + .../fixtures/blocks/core__post-template.html | 1 + ...ery-loop.json => core__post-template.json} | 2 +- ...d.json => core__post-template.parsed.json} | 2 +- .../core__post-template.serialized.html | 1 + .../fixtures/blocks/core__query-loop.html | 1 - .../blocks/core__query-loop.serialized.html | 1 - packages/e2e-tests/plugins/query-block.php | 8 +- 24 files changed, 202 insertions(+), 129 deletions(-) rename packages/block-library/src/{query-loop => post-template}/block.json (60%) rename packages/block-library/src/{query-loop => post-template}/edit.js (98%) rename packages/block-library/src/{query-loop => post-template}/editor.scss (75%) rename packages/block-library/src/{query-loop => post-template}/index.js (100%) create mode 100644 packages/block-library/src/post-template/index.php rename packages/block-library/src/{query-loop => post-template}/save.js (73%) rename packages/block-library/src/{query-loop => post-template}/style.scss (60%) delete mode 100644 packages/block-library/src/query-loop/index.php create mode 100644 packages/e2e-tests/fixtures/blocks/core__post-template.html rename packages/e2e-tests/fixtures/blocks/{core__query-loop.json => core__post-template.json} (79%) rename packages/e2e-tests/fixtures/blocks/{core__query-loop.parsed.json => core__post-template.parsed.json} (69%) create mode 100644 packages/e2e-tests/fixtures/blocks/core__post-template.serialized.html delete mode 100644 packages/e2e-tests/fixtures/blocks/core__query-loop.html delete mode 100644 packages/e2e-tests/fixtures/blocks/core__query-loop.serialized.html diff --git a/lib/block-patterns.php b/lib/block-patterns.php index ccc6f8bbf7b68..ff58b6df019a2 100644 --- a/lib/block-patterns.php +++ b/lib/block-patterns.php @@ -21,7 +21,7 @@ function register_gutenberg_patterns() { 'categories' => array( 'query' ), 'content' => '
- + @@ -29,7 +29,7 @@ function register_gutenberg_patterns() {
- +
', ) @@ -43,7 +43,7 @@ function register_gutenberg_patterns() { 'categories' => array( 'query' ), 'content' => '
- +
@@ -53,7 +53,7 @@ function register_gutenberg_patterns() {
- + ', ) @@ -67,7 +67,7 @@ function register_gutenberg_patterns() { 'categories' => array( 'query' ), 'content' => '
- +
@@ -76,7 +76,7 @@ function register_gutenberg_patterns() {
- +
', ) @@ -90,13 +90,13 @@ function register_gutenberg_patterns() { 'categories' => array( 'query' ), 'content' => '
- +
- + ', ) @@ -110,7 +110,7 @@ function register_gutenberg_patterns() { 'categories' => array( 'query' ), 'content' => '
-
+

@@ -124,7 +124,7 @@ function register_gutenberg_patterns() {
-
+
', ) @@ -140,26 +140,26 @@ function register_gutenberg_patterns() {
-
+
-
+
-
+
-
+
diff --git a/lib/blocks.php b/lib/blocks.php index 68d3404a589e9..e04842e8a9445 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -102,7 +102,7 @@ function gutenberg_reregister_core_block_types() { 'post-navigation-link.php' => 'core/post-navigation-link', 'post-title.php' => 'core/post-title', 'query.php' => 'core/query', - 'query-loop.php' => 'core/query-loop', + 'post-template.php' => 'core/post-template', 'query-title.php' => 'core/query-title', 'query-pagination.php' => 'core/query-pagination', 'query-pagination-next.php' => 'core/query-pagination-next', diff --git a/packages/block-library/src/editor.scss b/packages/block-library/src/editor.scss index cef09dbb86549..4118213572e23 100644 --- a/packages/block-library/src/editor.scss +++ b/packages/block-library/src/editor.scss @@ -46,7 +46,7 @@ @import "./text-columns/editor.scss"; @import "./video/editor.scss"; @import "./query-title/editor.scss"; -@import "./query-loop/editor.scss"; +@import "./post-template/editor.scss"; @import "./query/editor.scss"; @import "./query-pagination/editor.scss"; @import "./query-pagination-numbers/editor.scss"; diff --git a/packages/block-library/src/index.js b/packages/block-library/src/index.js index b0e36ae6dba9e..73713305c1030 100644 --- a/packages/block-library/src/index.js +++ b/packages/block-library/src/index.js @@ -70,7 +70,7 @@ import * as siteTagline from './site-tagline'; import * as siteTitle from './site-title'; import * as templatePart from './template-part'; import * as query from './query'; -import * as queryLoop from './query-loop'; +import * as postTemplate from './post-template'; import * as queryTitle from './query-title'; import * as queryPagination from './query-pagination'; import * as queryPaginationNext from './query-pagination-next'; @@ -175,7 +175,7 @@ export const __experimentalGetCoreBlocks = () => [ siteTitle, query, - queryLoop, + postTemplate, queryTitle, queryPagination, queryPaginationNext, diff --git a/packages/block-library/src/query-loop/block.json b/packages/block-library/src/post-template/block.json similarity index 60% rename from packages/block-library/src/query-loop/block.json rename to packages/block-library/src/post-template/block.json index 1d714c2f38366..e4d5de8281514 100644 --- a/packages/block-library/src/query-loop/block.json +++ b/packages/block-library/src/post-template/block.json @@ -1,10 +1,10 @@ { "apiVersion": 2, - "name": "core/query-loop", - "title": "Query Loop", + "name": "core/post-template", + "title": "Post Template", "category": "design", "parent": [ "core/query" ], - "description": "Query loop.", + "description": "Post Template.", "textdomain": "default", "usesContext": [ "queryId", @@ -18,6 +18,6 @@ "html": false, "align": true }, - "style": "wp-block-query-loop", - "editorStyle": "wp-block-query-loop-editor" + "style": "wp-block-post-template", + "editorStyle": "wp-block-post-template-editor" } diff --git a/packages/block-library/src/query-loop/edit.js b/packages/block-library/src/post-template/edit.js similarity index 98% rename from packages/block-library/src/query-loop/edit.js rename to packages/block-library/src/post-template/edit.js index ec55be2d0d6df..a1fd750cdc861 100644 --- a/packages/block-library/src/query-loop/edit.js +++ b/packages/block-library/src/post-template/edit.js @@ -24,7 +24,7 @@ const TEMPLATE = [ [ 'core/post-date' ], [ 'core/post-excerpt' ], ]; -export default function QueryLoopEdit( { +export default function PostTemplateEdit( { clientId, context: { query: { diff --git a/packages/block-library/src/query-loop/editor.scss b/packages/block-library/src/post-template/editor.scss similarity index 75% rename from packages/block-library/src/query-loop/editor.scss rename to packages/block-library/src/post-template/editor.scss index d0c05ad060565..7b426b0f3d37a 100644 --- a/packages/block-library/src/query-loop/editor.scss +++ b/packages/block-library/src/post-template/editor.scss @@ -1,5 +1,5 @@ .editor-styles-wrapper { - ul.wp-block-query-loop { + ul.wp-block-post-template { padding-left: 0; margin-left: 0; list-style: none; diff --git a/packages/block-library/src/query-loop/index.js b/packages/block-library/src/post-template/index.js similarity index 100% rename from packages/block-library/src/query-loop/index.js rename to packages/block-library/src/post-template/index.js diff --git a/packages/block-library/src/post-template/index.php b/packages/block-library/src/post-template/index.php new file mode 100644 index 0000000000000..88e029946c830 --- /dev/null +++ b/packages/block-library/src/post-template/index.php @@ -0,0 +1,148 @@ +context['queryId'] ) ? 'query-' . $block->context['queryId'] . '-page' : 'query-page'; + $page = empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ]; + + $query_args = build_query_vars_from_query_block( $block, $page ); + // Override the custom query with the global query if needed. + $use_global_query = ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] ); + if ( $use_global_query ) { + global $wp_query; + if ( $wp_query && isset( $wp_query->query_vars ) && is_array( $wp_query->query_vars ) ) { + // Unset `offset` because if is set, $wp_query overrides/ignores the paged parameter and breaks pagination. + unset( $query_args['offset'] ); + $query_args = wp_parse_args( $wp_query->query_vars, $query_args ); + + if ( empty( $query_args['post_type'] ) && is_singular() ) { + $query_args['post_type'] = get_post_type( get_the_ID() ); + } + } + } + + $query = new WP_Query( $query_args ); + + if ( ! $query->have_posts() ) { + return ''; + } + + $classnames = ''; + if ( isset( $block->context['displayLayout'] ) && isset( $block->context['query'] ) ) { + if ( isset( $block->context['displayLayout']['type'] ) && 'flex' === $block->context['displayLayout']['type'] ) { + $classnames = "is-flex-container columns-{$block->context['displayLayout']['columns']}"; + } + } + + $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classnames ) ); + + $content = ''; + while ( $query->have_posts() ) { + $query->the_post(); + $block_content = ( + new WP_Block( + $block->parsed_block, + array( + 'postType' => get_post_type(), + 'postId' => get_the_ID(), + ) + ) + )->render( array( 'dynamic' => false ) ); + $content .= "
  • {$block_content}
  • "; + } + + wp_reset_postdata(); + + return sprintf( + '', + $wrapper_attributes, + $content + ); +} + +/** + * Registers the `core/post-template` block on the server. + */ +function register_block_core_post_template() { + register_block_type_from_metadata( + __DIR__ . '/post-template', + array( + 'render_callback' => 'render_block_core_post_template', + 'skip_inner_blocks' => true, + ) + ); +} +add_action( 'init', 'register_block_core_post_template' ); + +/** + * Renders the legacy `core/query-loop` block on the server. + * It triggers a developer warning and then calls the renamed + * block's `render_callback` function output. + * + * This can be removed when WordPress 5.9 is released. + * + * @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 render_legacy_query_loop_block( $attributes, $content, $block ) { + trigger_error( + /* translators: %1$s: Block type */ + sprintf( __( 'Block %1$s has been renamed to Post Template. %1$s will be supported until WordPress version 5.9.', 'gutenberg' ), $block->name ), + headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE + ); + return render_block_core_post_template( $attributes, $content, $block ); +} + +/** + * Complements the renaming of `Query Loop` to `Post Template`. + * This ensures backwards compatibility for any users running the Gutenberg + * plugin who have used Query Loop prior to its renaming. + * + * This can be removed when WordPress 5.9 is released. + * + * @see https://github.com/WordPress/gutenberg/pull/32514 + */ +function gutenberg_register_legacy_query_loop_block() { + $registry = WP_Block_Type_Registry::get_instance(); + if ( $registry->is_registered( 'core/query-loop' ) ) { + unregister_block_type( 'core/query-loop' ); + } + register_block_type( + 'core/query-loop', + array( + 'category' => 'design', + 'uses_context' => array( + 'queryId', + 'query', + 'queryContext', + 'displayLayout', + 'templateSlug', + ), + 'supports' => array( + 'reusable' => false, + 'html' => false, + 'align' => true, + ), + 'style' => 'wp-block-post-template', + 'render_callback' => 'render_legacy_query_loop_block', + 'skip_inner_blocks' => true, + ) + ); +} +add_action( 'init', 'gutenberg_register_legacy_query_loop_block' ); diff --git a/packages/block-library/src/query-loop/save.js b/packages/block-library/src/post-template/save.js similarity index 73% rename from packages/block-library/src/query-loop/save.js rename to packages/block-library/src/post-template/save.js index 25c0128386512..0533b07693160 100644 --- a/packages/block-library/src/query-loop/save.js +++ b/packages/block-library/src/post-template/save.js @@ -3,6 +3,6 @@ */ import { InnerBlocks } from '@wordpress/block-editor'; -export default function QueryLoopSave() { +export default function PostTemplateSave() { return ; } diff --git a/packages/block-library/src/query-loop/style.scss b/packages/block-library/src/post-template/style.scss similarity index 60% rename from packages/block-library/src/query-loop/style.scss rename to packages/block-library/src/post-template/style.scss index 676040b147ebb..7a83a79bcb11f 100644 --- a/packages/block-library/src/query-loop/style.scss +++ b/packages/block-library/src/post-template/style.scss @@ -1,3 +1,9 @@ +.wp-block-post-template, +// We have kept `wp-block-query-loop` class as well for backwards +// compatibility with existing `QueryLoop` blocks that haven't been +// updated, after the renaming of the block to `Post Template`. +// See: https://github.com/WordPress/gutenberg/pull/32514. +// TODO: Remove this class when WordPress 5.9 is released. .wp-block-query-loop { max-width: 100%; list-style: none; diff --git a/packages/block-library/src/query-loop/index.php b/packages/block-library/src/query-loop/index.php deleted file mode 100644 index 9bdeb13f4b81d..0000000000000 --- a/packages/block-library/src/query-loop/index.php +++ /dev/null @@ -1,88 +0,0 @@ -context['queryId'] ) ? 'query-' . $block->context['queryId'] . '-page' : 'query-page'; - $page = empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ]; - - $query_args = build_query_vars_from_query_block( $block, $page ); - // Override the custom query with the global query if needed. - $use_global_query = ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] ); - if ( $use_global_query ) { - global $wp_query; - if ( $wp_query && isset( $wp_query->query_vars ) && is_array( $wp_query->query_vars ) ) { - // Unset `offset` because if is set, $wp_query overrides/ignores the paged parameter and breaks pagination. - unset( $query_args['offset'] ); - $query_args = wp_parse_args( $wp_query->query_vars, $query_args ); - - if ( empty( $query_args['post_type'] ) && is_singular() ) { - $query_args['post_type'] = get_post_type( get_the_ID() ); - } - } - } - - $query = new WP_Query( $query_args ); - - if ( ! $query->have_posts() ) { - return ''; - } - - $classnames = ''; - if ( isset( $block->context['displayLayout'] ) && isset( $block->context['query'] ) ) { - if ( isset( $block->context['displayLayout']['type'] ) && 'flex' === $block->context['displayLayout']['type'] ) { - $classnames = "is-flex-container columns-{$block->context['displayLayout']['columns']}"; - } - } - - $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classnames ) ); - - $content = ''; - while ( $query->have_posts() ) { - $query->the_post(); - $block_content = ( - new WP_Block( - $block->parsed_block, - array( - 'postType' => get_post_type(), - 'postId' => get_the_ID(), - ) - ) - )->render( array( 'dynamic' => false ) ); - $content .= "
  • {$block_content}
  • "; - } - - wp_reset_postdata(); - - return sprintf( - '', - $wrapper_attributes, - $content - ); -} - -/** - * Registers the `core/query-loop` block on the server. - */ -function register_block_core_query_loop() { - register_block_type_from_metadata( - __DIR__ . '/query-loop', - array( - 'render_callback' => 'render_block_core_query_loop', - 'skip_inner_blocks' => true, - ) - ); -} -add_action( 'init', 'register_block_core_query_loop' ); diff --git a/packages/block-library/src/query/block.json b/packages/block-library/src/query/block.json index b54713a75aac6..a08dbc2e1326e 100644 --- a/packages/block-library/src/query/block.json +++ b/packages/block-library/src/query/block.json @@ -1,7 +1,7 @@ { "apiVersion": 2, "name": "core/query", - "title": "Query", + "title": "Query Loop", "category": "design", "description": "Displays a list of posts as a result of a query.", "textdomain": "default", diff --git a/packages/block-library/src/query/edit/index.js b/packages/block-library/src/query/edit/index.js index 4612033981426..637f05c18a1be 100644 --- a/packages/block-library/src/query/edit/index.js +++ b/packages/block-library/src/query/edit/index.js @@ -24,7 +24,7 @@ import QueryInspectorControls from './query-inspector-controls'; import QueryPlaceholder from './query-placeholder'; import { DEFAULTS_POSTS_PER_PAGE } from '../constants'; -const TEMPLATE = [ [ 'core/query-loop' ] ]; +const TEMPLATE = [ [ 'core/post-template' ] ]; export function QueryContent( { attributes, setAttributes } ) { const { queryId, diff --git a/packages/block-library/src/query/variations.js b/packages/block-library/src/query/variations.js index dbf9290cc58fc..d5790da204810 100644 --- a/packages/block-library/src/query/variations.js +++ b/packages/block-library/src/query/variations.js @@ -35,6 +35,7 @@ const variations = [ author: '', search: '', sticky: 'exclude', + inherit: false, }, }, scope: [ 'inserter' ], @@ -45,7 +46,7 @@ const variations = [ icon: titleDate, innerBlocks: [ [ - 'core/query-loop', + 'core/post-template', {}, [ [ 'core/post-title' ], [ 'core/post-date' ] ], ], @@ -58,7 +59,7 @@ const variations = [ icon: titleExcerpt, innerBlocks: [ [ - 'core/query-loop', + 'core/post-template', {}, [ [ 'core/post-title' ], [ 'core/post-excerpt' ] ], ], @@ -71,7 +72,7 @@ const variations = [ icon: titleDateExcerpt, innerBlocks: [ [ - 'core/query-loop', + 'core/post-template', {}, [ [ 'core/post-title' ], @@ -88,7 +89,7 @@ const variations = [ icon: imageDateTitle, innerBlocks: [ [ - 'core/query-loop', + 'core/post-template', {}, [ [ 'core/post-featured-image' ], diff --git a/packages/block-library/src/style.scss b/packages/block-library/src/style.scss index 92f072e3bccfb..9cca5d3233e27 100644 --- a/packages/block-library/src/style.scss +++ b/packages/block-library/src/style.scss @@ -33,7 +33,7 @@ @import "./post-title/style.scss"; @import "./preformatted/style.scss"; @import "./pullquote/style.scss"; -@import "./query-loop/style.scss"; +@import "./post-template/style.scss"; @import "./query-pagination/style.scss"; @import "./quote/style.scss"; @import "./rss/style.scss"; diff --git a/packages/blocks/src/api/parser.js b/packages/blocks/src/api/parser.js index 829557f89f201..edffe4b4af462 100644 --- a/packages/blocks/src/api/parser.js +++ b/packages/blocks/src/api/parser.js @@ -454,6 +454,12 @@ export function convertLegacyBlocks( name, attributes ) { } name = 'core/embed'; } + + // Convert 'core/query-loop' blocks in existing content to 'core/post-template'. + // TODO: Remove this check when WordPress 5.9 is released. + if ( name === 'core/query-loop' ) { + name = 'core/post-template'; + } return { name, attributes: newAttributes }; } diff --git a/packages/e2e-tests/fixtures/blocks/core__post-template.html b/packages/e2e-tests/fixtures/blocks/core__post-template.html new file mode 100644 index 0000000000000..0664d674bdf95 --- /dev/null +++ b/packages/e2e-tests/fixtures/blocks/core__post-template.html @@ -0,0 +1 @@ + diff --git a/packages/e2e-tests/fixtures/blocks/core__query-loop.json b/packages/e2e-tests/fixtures/blocks/core__post-template.json similarity index 79% rename from packages/e2e-tests/fixtures/blocks/core__query-loop.json rename to packages/e2e-tests/fixtures/blocks/core__post-template.json index e7edef8b5eb8c..0b67fa4c62cb3 100644 --- a/packages/e2e-tests/fixtures/blocks/core__query-loop.json +++ b/packages/e2e-tests/fixtures/blocks/core__post-template.json @@ -1,7 +1,7 @@ [ { "clientId": "_clientId_0", - "name": "core/query-loop", + "name": "core/post-template", "isValid": true, "attributes": {}, "innerBlocks": [], diff --git a/packages/e2e-tests/fixtures/blocks/core__query-loop.parsed.json b/packages/e2e-tests/fixtures/blocks/core__post-template.parsed.json similarity index 69% rename from packages/e2e-tests/fixtures/blocks/core__query-loop.parsed.json rename to packages/e2e-tests/fixtures/blocks/core__post-template.parsed.json index 12985d2dd36f8..f7f1fbd82e69c 100644 --- a/packages/e2e-tests/fixtures/blocks/core__query-loop.parsed.json +++ b/packages/e2e-tests/fixtures/blocks/core__post-template.parsed.json @@ -1,6 +1,6 @@ [ { - "blockName": "core/query-loop", + "blockName": "core/post-template", "attrs": {}, "innerBlocks": [], "innerHTML": "", diff --git a/packages/e2e-tests/fixtures/blocks/core__post-template.serialized.html b/packages/e2e-tests/fixtures/blocks/core__post-template.serialized.html new file mode 100644 index 0000000000000..0664d674bdf95 --- /dev/null +++ b/packages/e2e-tests/fixtures/blocks/core__post-template.serialized.html @@ -0,0 +1 @@ + diff --git a/packages/e2e-tests/fixtures/blocks/core__query-loop.html b/packages/e2e-tests/fixtures/blocks/core__query-loop.html deleted file mode 100644 index 16de7db3703b0..0000000000000 --- a/packages/e2e-tests/fixtures/blocks/core__query-loop.html +++ /dev/null @@ -1 +0,0 @@ - diff --git a/packages/e2e-tests/fixtures/blocks/core__query-loop.serialized.html b/packages/e2e-tests/fixtures/blocks/core__query-loop.serialized.html deleted file mode 100644 index 16de7db3703b0..0000000000000 --- a/packages/e2e-tests/fixtures/blocks/core__query-loop.serialized.html +++ /dev/null @@ -1 +0,0 @@ - diff --git a/packages/e2e-tests/plugins/query-block.php b/packages/e2e-tests/plugins/query-block.php index f985c0d772224..46f9c363b3278 100644 --- a/packages/e2e-tests/plugins/query-block.php +++ b/packages/e2e-tests/plugins/query-block.php @@ -17,9 +17,9 @@ 'title' => __( 'Query Test 1', 'gutenberg' ), 'blockTypes' => array( 'core/query' ), 'content' => ' - + - + ', ) ); @@ -29,10 +29,10 @@ 'title' => __( 'Query Test 2', 'gutenberg' ), 'blockTypes' => array( 'core/query' ), 'content' => ' - + - + ', ) );