| 1 |
<?php |
| 2 |
/** |
| 3 |
* Full Site Editing Utils |
| 4 |
* |
| 5 |
* @package gutenberg |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Returns whether the current theme is an FSE theme or not. |
| 10 |
* |
| 11 |
* @return boolean Whether the current theme is an FSE theme or not. |
| 12 |
*/ |
| 13 |
function gutenberg_is_fse_theme() { |
| 14 |
return is_readable( get_stylesheet_directory() . '/block-templates/index.html' ); |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Show a notice when a Full Site Editing theme is used. |
| 19 |
*/ |
| 20 |
function gutenberg_full_site_editing_notice() { |
| 21 |
if ( ! gutenberg_is_fse_theme() ) { |
| 22 |
return; |
| 23 |
} |
| 24 |
?> |
| 25 |
<div class="notice notice-warning"> |
| 26 |
<p><?php _e( 'You\'re using an experimental Full Site Editing theme. Full Site Editing is an experimental feature and potential API changes are to be expected!', 'gutenberg' ); ?></p> |
| 27 |
</div> |
| 28 |
<?php |
| 29 |
} |
| 30 |
add_action( 'admin_notices', 'gutenberg_full_site_editing_notice' ); |
| 31 |
|
| 32 |
/** |
| 33 |
* Removes legacy pages from FSE themes. |
| 34 |
*/ |
| 35 |
function gutenberg_remove_legacy_pages() { |
| 36 |
if ( ! gutenberg_is_fse_theme() ) { |
| 37 |
return; |
| 38 |
} |
| 39 |
|
| 40 |
global $submenu; |
| 41 |
if ( isset( $submenu['themes.php'] ) ) { |
| 42 |
$indexes_to_remove = array(); |
| 43 |
foreach ( $submenu['themes.php'] as $index => $menu_item ) { |
| 44 |
if ( |
| 45 |
false !== strpos( $menu_item[2], 'customize.php' ) || |
| 46 |
false !== strpos( $menu_item[2], 'gutenberg-widgets' ) |
| 47 |
) { |
| 48 |
$indexes_to_remove[] = $index; |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
foreach ( $indexes_to_remove as $index ) { |
| 53 |
unset( $submenu['themes.php'][ $index ] ); |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
add_action( 'admin_menu', 'gutenberg_remove_legacy_pages' ); |
| 59 |
|
| 60 |
/** |
| 61 |
* Removes legacy adminbar items from FSE themes. |
| 62 |
* |
| 63 |
* @param WP_Admin_Bar $wp_admin_bar The admin-bar instance. |
| 64 |
*/ |
| 65 |
function gutenberg_adminbar_items( $wp_admin_bar ) { |
| 66 |
|
| 67 |
// Early exit if not an FSE theme. |
| 68 |
if ( ! gutenberg_is_fse_theme() ) { |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
// Remove customizer link. |
| 73 |
$wp_admin_bar->remove_node( 'customize' ); |
| 74 |
$wp_admin_bar->remove_node( 'customize-background' ); |
| 75 |
$wp_admin_bar->remove_node( 'customize-header' ); |
| 76 |
$wp_admin_bar->remove_node( 'widgets' ); |
| 77 |
|
| 78 |
// Add site-editor link. |
| 79 |
if ( ! is_admin() && current_user_can( 'edit_theme_options' ) ) { |
| 80 |
$wp_admin_bar->add_node( |
| 81 |
array( |
| 82 |
'id' => 'site-editor', |
| 83 |
'title' => __( 'Edit site', 'gutenberg' ), |
| 84 |
'href' => admin_url( 'admin.php?page=gutenberg-edit-site' ), |
| 85 |
) |
| 86 |
); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
add_action( 'admin_bar_menu', 'gutenberg_adminbar_items', 50 ); |
| 91 |
|
| 92 |
/** |
| 93 |
* Activates the 'menu_order' filter and then hooks into 'menu_order' |
| 94 |
*/ |
| 95 |
add_filter( 'custom_menu_order', '__return_true' ); |
| 96 |
add_filter( 'menu_order', 'gutenberg_menu_order' ); |
| 97 |
|
| 98 |
/** |
| 99 |
* Filters WordPress default menu order |
| 100 |
* |
| 101 |
* @param array $menu_order Menu Order. |
| 102 |
*/ |
| 103 |
function gutenberg_menu_order( $menu_order ) { |
| 104 |
if ( ! gutenberg_is_fse_theme() ) { |
| 105 |
return $menu_order; |
| 106 |
} |
| 107 |
|
| 108 |
$new_positions = array( |
| 109 |
// Position the site editor before the appearnce menu. |
| 110 |
'gutenberg-edit-site' => array_search( 'themes.php', $menu_order, true ), |
| 111 |
); |
| 112 |
|
| 113 |
// Traverse through the new positions and move |
| 114 |
// the items if found in the original menu_positions. |
| 115 |
foreach ( $new_positions as $value => $new_index ) { |
| 116 |
$current_index = array_search( $value, $menu_order, true ); |
| 117 |
if ( $current_index ) { |
| 118 |
$out = array_splice( $menu_order, $current_index, 1 ); |
| 119 |
array_splice( $menu_order, $new_index, 0, $out ); |
| 120 |
} |
| 121 |
} |
| 122 |
return $menu_order; |
| 123 |
} |
| 124 |
|