Skip to content

Commit

Permalink
Avoid calling gutenberg_ functions within code shipped through Word…
Browse files Browse the repository at this point in the history
…Press Core (#33331)
  • Loading branch information
desrosj authored and youknowriad committed Jul 13, 2021
1 parent 585cba3 commit 3428eb7
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 60 deletions.
61 changes: 61 additions & 0 deletions lib/compat/wordpress-5.8/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,64 @@ function build_query_vars_from_query_block( $block, $page ) {
return $query;
}
}

if ( ! function_exists( 'gutenberg_register_legacy_query_loop_block' ) ) {
/**
* 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.
*
* @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 gutenberg_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 );
}
}

if ( ! function_exists( 'gutenberg_register_legacy_query_loop_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.
*
* @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' => 'gutenberg_render_legacy_query_loop_block',
'skip_inner_blocks' => true,
)
);
}

add_action( 'init', 'gutenberg_register_legacy_query_loop_block' );
}
60 changes: 0 additions & 60 deletions packages/block-library/src/post-template/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,63 +86,3 @@ function register_block_core_post_template() {
);
}
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.' ), $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' );
7 changes: 7 additions & 0 deletions packages/widgets/src/blocks/legacy-widget/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ function render_block_core_legacy_widget( $attributes ) {
$widget_key = $wp_widget_factory->get_widget_key( $id_base );
$widget_object = $wp_widget_factory->get_widget_object( $id_base );
} else {
/*
* This file is copied from the published @wordpress/widgets package when WordPress
* Core is built. Because the package is a dependency of both WordPress Core and the
* Gutenberg plugin where the block editor is developed, this fallback condition is
* required until the minimum required version of WordPress for the plugin is raised
* to 5.8.
*/
$widget_key = gutenberg_get_widget_key( $id_base );
$widget_object = gutenberg_get_widget_object( $id_base );
}
Expand Down

0 comments on commit 3428eb7

Please sign in to comment.