| 1 |
<?php |
| 2 |
/** |
| 3 |
* Compatibility fixes for WP 5.9 and Site Editor. |
| 4 |
* |
| 5 |
* It can be removed after the minimum required WP version is 6.0 |
| 6 |
* |
| 7 |
* @package WordPress |
| 8 |
*/ |
| 9 |
|
| 10 |
/** |
| 11 |
* Return the correct template for the site's home page. |
| 12 |
* |
| 13 |
* @access private |
| 14 |
* @since 6.0.0 |
| 15 |
* |
| 16 |
* @return array|null A template object, or null if none could be found |
| 17 |
*/ |
| 18 |
function gutenberg_resolve_home_template() { |
| 19 |
$show_on_front = get_option( 'show_on_front' ); |
| 20 |
$front_page_id = get_option( 'page_on_front' ); |
| 21 |
|
| 22 |
if ( 'page' === $show_on_front && $front_page_id ) { |
| 23 |
return array( |
| 24 |
'postType' => 'page', |
| 25 |
'postId' => $front_page_id, |
| 26 |
); |
| 27 |
} |
| 28 |
|
| 29 |
$hierarchy = array( 'front-page', 'home', 'index' ); |
| 30 |
$template = resolve_block_template( 'home', $hierarchy, '' ); |
| 31 |
|
| 32 |
if ( ! $template ) { |
| 33 |
return null; |
| 34 |
} |
| 35 |
|
| 36 |
return array( |
| 37 |
'postType' => 'wp_template', |
| 38 |
'postId' => $template->id, |
| 39 |
); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Do a server-side redirection if missing `postType` and `postId` |
| 44 |
* query args when visiting site editor. |
| 45 |
* |
| 46 |
* Note: This is a backward compatibility redirect for WP 5.9. |
| 47 |
* |
| 48 |
* @return void |
| 49 |
*/ |
| 50 |
function gutenberg_site_editor_maybe_redirect() { |
| 51 |
// Skip redirection for WP 6.0 and later. |
| 52 |
if ( function_exists( '_resolve_home_block_template' ) ) { |
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
// Check theme support. The action runs before checks in the Site Editor. |
| 57 |
if ( ! wp_is_block_theme() ) { |
| 58 |
return; |
| 59 |
} |
| 60 |
|
| 61 |
if ( empty( $_GET['postType'] ) && empty( $_GET['postId'] ) ) { |
| 62 |
$template = gutenberg_resolve_home_template(); |
| 63 |
if ( ! empty( $_GET['styles'] ) ) { |
| 64 |
$template['styles'] = sanitize_key( $_GET['styles'] ); |
| 65 |
} |
| 66 |
|
| 67 |
$redirect_url = add_query_arg( |
| 68 |
$template, |
| 69 |
admin_url( 'site-editor.php' ) |
| 70 |
); |
| 71 |
wp_safe_redirect( $redirect_url ); |
| 72 |
exit; |
| 73 |
} |
| 74 |
} |
| 75 |
add_action( 'load-site-editor.php', 'gutenberg_site_editor_maybe_redirect' ); |
| 76 |
|
| 77 |
/** |
| 78 |
* Add home template settings for WP 5.9. |
| 79 |
* |
| 80 |
* @param array $settings Existing block editor settings. |
| 81 |
* @param WP_Block_Editor_Context $context The current block editor context. |
| 82 |
* @return array |
| 83 |
*/ |
| 84 |
function gutenberg_site_editor_homepage_setting( $settings, $context ) { |
| 85 |
if ( isset( $context->post ) ) { |
| 86 |
return $settings; |
| 87 |
} |
| 88 |
|
| 89 |
if ( ! isset( $settings['__unstableHomeTemplate'] ) ) { |
| 90 |
$settings['__unstableHomeTemplate'] = gutenberg_resolve_home_template(); |
| 91 |
} |
| 92 |
|
| 93 |
return $settings; |
| 94 |
} |
| 95 |
add_filter( 'block_editor_settings_all', 'gutenberg_site_editor_homepage_setting', 10, 2 ); |
| 96 |
|