| 1 |
<?php |
| 2 |
/** |
| 3 |
* Bootstraps the new posts dashboard page. |
| 4 |
* |
| 5 |
* @package gutenberg |
| 6 |
*/ |
| 7 |
|
| 8 |
add_action( 'admin_menu', 'gutenberg_replace_posts_dashboard' ); |
| 9 |
|
| 10 |
if ( isset( $_GET['page'] ) && 'gutenberg-posts-dashboard' === $_GET['page'] ) { |
| 11 |
// Default to is-fullscreen-mode to avoid jumps in the UI. |
| 12 |
add_filter( |
| 13 |
'admin_body_class', |
| 14 |
static function ( $classes ) { |
| 15 |
return "$classes is-fullscreen-mode"; |
| 16 |
} |
| 17 |
); |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Renders the new posts dashboard page. |
| 22 |
*/ |
| 23 |
function gutenberg_posts_dashboard() { |
| 24 |
$block_editor_context = new WP_Block_Editor_Context( array( 'name' => 'core/edit-site' ) ); |
| 25 |
$custom_settings = array( |
| 26 |
'siteUrl' => site_url(), |
| 27 |
'styles' => get_block_editor_theme_styles(), |
| 28 |
'supportsLayout' => wp_theme_has_theme_json(), |
| 29 |
); |
| 30 |
|
| 31 |
$editor_settings = get_block_editor_settings( $custom_settings, $block_editor_context ); |
| 32 |
$active_global_styles_id = WP_Theme_JSON_Resolver_Gutenberg::get_user_global_styles_post_id(); |
| 33 |
$active_theme = get_stylesheet(); |
| 34 |
|
| 35 |
$preload_paths = array( |
| 36 |
array( '/wp/v2/media', 'OPTIONS' ), |
| 37 |
'/wp/v2/types?context=view', |
| 38 |
'/wp/v2/global-styles/' . $active_global_styles_id . '?context=edit', |
| 39 |
'/wp/v2/global-styles/' . $active_global_styles_id, |
| 40 |
'/wp/v2/global-styles/themes/' . $active_theme, |
| 41 |
); |
| 42 |
block_editor_rest_api_preload( $preload_paths, $block_editor_context ); |
| 43 |
|
| 44 |
// Preload server-registered block schemas. |
| 45 |
wp_add_inline_script( |
| 46 |
'wp-blocks', |
| 47 |
'wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . wp_json_encode( get_block_editor_server_block_settings(), JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ) . ');' |
| 48 |
); |
| 49 |
|
| 50 |
/** This action is documented in wp-admin/edit-form-blocks.php */ |
| 51 |
do_action( 'enqueue_block_editor_assets' ); |
| 52 |
wp_register_style( |
| 53 |
'wp-gutenberg-posts-dashboard', |
| 54 |
gutenberg_url( 'build/edit-site/posts.css' ), |
| 55 |
array( 'wp-components', 'wp-commands', 'wp-edit-site' ) |
| 56 |
); |
| 57 |
wp_enqueue_style( 'wp-gutenberg-posts-dashboard' ); |
| 58 |
wp_add_inline_script( |
| 59 |
'wp-edit-site', |
| 60 |
sprintf( |
| 61 |
'wp.domReady( function() { |
| 62 |
wp.editSite.initializePostsDashboard( "gutenberg-posts-dashboard", %s ); |
| 63 |
} );', |
| 64 |
wp_json_encode( $editor_settings, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ) |
| 65 |
) |
| 66 |
); |
| 67 |
wp_enqueue_script( 'wp-edit-site' ); |
| 68 |
wp_enqueue_media(); |
| 69 |
echo '<div id="gutenberg-posts-dashboard"></div>'; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Replaces the default posts menu item with the new posts dashboard. |
| 74 |
*/ |
| 75 |
function gutenberg_replace_posts_dashboard() { |
| 76 |
$gutenberg_experiments = get_option( 'gutenberg-experiments' ); |
| 77 |
if ( ! $gutenberg_experiments || ! array_key_exists( 'gutenberg-new-posts-dashboard', $gutenberg_experiments ) || ! $gutenberg_experiments['gutenberg-new-posts-dashboard'] ) { |
| 78 |
return; |
| 79 |
} |
| 80 |
$ptype_obj = get_post_type_object( 'post' ); |
| 81 |
add_submenu_page( |
| 82 |
'gutenberg', |
| 83 |
$ptype_obj->labels->name, |
| 84 |
$ptype_obj->labels->name, |
| 85 |
'edit_posts', |
| 86 |
'gutenberg-posts-dashboard', |
| 87 |
'gutenberg_posts_dashboard' |
| 88 |
); |
| 89 |
} |
| 90 |
|