Skip to content

Commit

Permalink
Plugin: Compatibility fixes for WP 5.9 and Site Editor (#42549)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mamaduka authored and ntsekouras committed Jul 21, 2022
1 parent 524619f commit d293b18
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 39 deletions.
38 changes: 0 additions & 38 deletions lib/compat/wordpress-6.0/block-template.php

This file was deleted.

95 changes: 95 additions & 0 deletions lib/compat/wordpress-6.0/site-editor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php
/**
* Compatibility fixes for WP 5.9 and Site Editor.
*
* It can be removed after the minimum required WP version is 6.0
*
* @package WordPress
*/

/**
* Return the correct template for the site's home page.
*
* @access private
* @since 6.0.0
*
* @return array|null A template object, or null if none could be found
*/
function gutenberg_resolve_home_template() {
$show_on_front = get_option( 'show_on_front' );
$front_page_id = get_option( 'page_on_front' );

if ( 'page' === $show_on_front && $front_page_id ) {
return array(
'postType' => 'page',
'postId' => $front_page_id,
);
}

$hierarchy = array( 'front-page', 'home', 'index' );
$template = resolve_block_template( 'home', $hierarchy, '' );

if ( ! $template ) {
return null;
}

return array(
'postType' => 'wp_template',
'postId' => $template->id,
);
}

/**
* Do a server-side redirection if missing `postType` and `postId`
* query args when visiting site editor.
*
* Note: This is a backward compatibility redirect for WP 5.9.
*
* @return void
*/
function gutenberg_site_editor_maybe_redirect() {
// Skip redirection for WP 6.0 and later.
if ( function_exists( '_resolve_home_block_template' ) ) {
return;
}

// Check theme support. The action runs before checks in the Site Editor.
if ( ! wp_is_block_theme() ) {
return;
}

if ( empty( $_GET['postType'] ) && empty( $_GET['postId'] ) ) {
$template = gutenberg_resolve_home_template();
if ( ! empty( $_GET['styles'] ) ) {
$template['styles'] = sanitize_key( $_GET['styles'] );
}

$redirect_url = add_query_arg(
$template,
admin_url( 'site-editor.php' )
);
wp_safe_redirect( $redirect_url );
exit;
}
}
add_action( 'load-site-editor.php', 'gutenberg_site_editor_maybe_redirect' );

/**
* Add home template settings for WP 5.9.
*
* @param array $settings Existing block editor settings.
* @param WP_Block_Editor_Context $context The current block editor context.
* @return array
*/
function gutenberg_site_editor_homepage_setting( $settings, $context ) {
if ( isset( $context->post ) ) {
return $settings;
}

if ( ! isset( $settings['__unstableHomeTemplate'] ) ) {
$settings['__unstableHomeTemplate'] = gutenberg_resolve_home_template();
}

return $settings;
}
add_filter( 'block_editor_settings_all', 'gutenberg_site_editor_homepage_setting', 10, 2 );
2 changes: 1 addition & 1 deletion lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/compat/wordpress-6.0/class-wp-theme-json-6-0.php';
require __DIR__ . '/compat/wordpress-6.0/class-wp-theme-json-resolver-6-0.php';
require __DIR__ . '/compat/wordpress-6.0/block-patterns.php';
require __DIR__ . '/compat/wordpress-6.0/block-template.php';
require __DIR__ . '/compat/wordpress-6.0/site-editor.php';
require __DIR__ . '/compat/wordpress-6.0/edit-form-blocks.php';
require __DIR__ . '/compat/wordpress-6.0/block-patterns-update.php';
require __DIR__ . '/compat/wordpress-6.0/client-assets.php';
Expand Down

0 comments on commit d293b18

Please sign in to comment.