Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid calling gutenberg_ functions within code shipped through WordPress Core #33331

Merged
merged 18 commits into from
Jul 13, 2021
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 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,43 @@ function build_query_vars_from_query_block( $block, $page ) {
return $query;
}
}

/**
* Ensures that a core legacy widget has a key when on a version of WordPress earlier than 5.8.
*
* This can be removed once the plugin's minimum version of WordPress is increased to 5.8.
*
* @param string $widget_key The widget key.
* @param string $id_base The widget ID base.
* @return string The widget key.
*/
function gutenberg_filter_core_legacy_widget_key( $widget_key, $id_base ) {
global $wp_widget_factory;

if ( method_exists( $wp_widget_factory, 'get_widget_key' ) ) {
return $widget_key;
}

return gutenberg_get_widget_key( $id_base );
}
add_filter( 'render_block_core_legacy_widget_key', 'gutenberg_filter_core_legacy_widget_key', 10, 2 );

/**
* Ensures that a core legacy widget object is found when on a version of WordPress earlier than 5.8.
*
* This can be removed once the plugin's minimum version of WordPress is increased to 5.8.
*
* @param string $widget_object The widget object.
* @param string $id_base The widget ID base.
* @return WP_Widget|null The widget object.
*/
function gutenberg_filter_core_legacy_widget_object( $widget_object, $id_base ) {
global $wp_widget_factory;

if ( method_exists( $wp_widget_factory, 'get_widget_object' ) ) {
return $widget_object;
}

return gutenberg_get_widget_object( $id_base );
}
add_filter( 'render_block_core_legacy_widget_object', 'gutenberg_filter_core_legacy_widget_object', 10, 2 );
14 changes: 8 additions & 6 deletions packages/block-library/src/post-template/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,16 @@ function register_block_core_post_template() {
* 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.
* @since 5.8.0
* @access private
*
* @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 ) {
function wp_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 ),
Expand All @@ -115,11 +116,12 @@ function render_legacy_query_loop_block( $attributes, $content, $block ) {
* 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.
* @since 5.8.0
* @access private
*
* @see https://github.com/WordPress/gutenberg/pull/32514
*/
function gutenberg_register_legacy_query_loop_block() {
desrosj marked this conversation as resolved.
Show resolved Hide resolved
function wp_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' );
Expand All @@ -141,9 +143,9 @@ function gutenberg_register_legacy_query_loop_block() {
'align' => true,
),
'style' => 'wp-block-post-template',
'render_callback' => 'render_legacy_query_loop_block',
'render_callback' => 'wp_render_legacy_query_loop_block',
'skip_inner_blocks' => true,
)
);
}
add_action( 'init', 'gutenberg_register_legacy_query_loop_block' );
add_action( 'init', 'wp_register_legacy_query_loop_block' );
31 changes: 27 additions & 4 deletions packages/widgets/src/blocks/legacy-widget/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,38 @@ function render_block_core_legacy_widget( $attributes ) {
return '';
}

$id_base = $attributes['idBase'];
$id_base = $attributes['idBase'];
$widget_key = '';
$widget_object = null;

if ( method_exists( $wp_widget_factory, 'get_widget_key' ) && method_exists( $wp_widget_factory, 'get_widget_object' ) ) {
$widget_key = $wp_widget_factory->get_widget_key( $id_base );
$widget_object = $wp_widget_factory->get_widget_object( $id_base );
} else {
$widget_key = gutenberg_get_widget_key( $id_base );
$widget_object = gutenberg_get_widget_object( $id_base );
}

/**
* Filters the core legacy widget block key.
*
* @since 5.8.0
*
* @param string $widget_key The widget key.
* @param string $id_base The widget ID base.
* @param array $attributes The block attributes.
*/
$widget_key = apply_filters( 'render_block_core_legacy_widget_key', $widget_key, $id_base, $attributes );
desrosj marked this conversation as resolved.
Show resolved Hide resolved

/**
* Filters the Core legacy widget object.
*
* @since 5.8.0
*
* @param null|WP_Widget $widget_object The widget object.
* @param string $widget_key The widget key.
* @param string $id_base The widget ID base.
* @param array $attributes The block attributes.
*/
$widget_object = apply_filters( 'render_block_core_legacy_widget_object', $widget_object, $widget_key, $id_base, $attributes );

if ( ! $widget_key || ! $widget_object ) {
return '';
}
Expand Down