| 1 |
<?php |
| 2 |
/** |
| 3 |
* Bootstraps the Guidelines page in wp-admin under Settings. |
| 4 |
* |
| 5 |
* @package gutenberg |
| 6 |
*/ |
| 7 |
|
| 8 |
add_action( 'admin_menu', 'gutenberg_register_guidelines_settings_submenu', 10 ); |
| 9 |
add_action( 'admin_enqueue_scripts', 'gutenberg_guidelines_enqueue_block_registry_scripts', 5 ); |
| 10 |
add_action( 'admin_enqueue_scripts', 'gutenberg_guidelines_preload_rest', 6 ); |
| 11 |
|
| 12 |
/** |
| 13 |
* Registers the Guidelines submenu item under Settings. |
| 14 |
* Uses the same layout/style as the Font Library admin page (wp-admin integrated). |
| 15 |
*/ |
| 16 |
function gutenberg_register_guidelines_settings_submenu() { |
| 17 |
add_submenu_page( |
| 18 |
'options-general.php', |
| 19 |
__( 'Guidelines', 'gutenberg' ), |
| 20 |
__( 'Guidelines', 'gutenberg' ), |
| 21 |
'manage_options', |
| 22 |
'guidelines-wp-admin', |
| 23 |
'gutenberg_guidelines_wp_admin_render_page' |
| 24 |
); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Enqueues wp-block-library on the Guidelines admin page so |
| 29 |
* registerCoreBlocks() is available when the app bootstraps the block |
| 30 |
* registry (Core blocks only) on the client. |
| 31 |
* |
| 32 |
* Priority 5 ensures this runs before the main asset enqueue (priority 10). |
| 33 |
*/ |
| 34 |
function gutenberg_guidelines_enqueue_block_registry_scripts( $hook_suffix ) { |
| 35 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 36 |
return; |
| 37 |
} |
| 38 |
|
| 39 |
if ( 'settings_page_guidelines-wp-admin' !== $hook_suffix ) { |
| 40 |
return; |
| 41 |
} |
| 42 |
|
| 43 |
wp_enqueue_script( 'wp-block-library' ); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Preloads the guideline scopes registry on the Guidelines admin page so the |
| 48 |
* client renders its sections without an extra round trip. Mirrors the |
| 49 |
* preloading the generated page template performs for site settings. |
| 50 |
* |
| 51 |
* @param string $hook_suffix The current admin page. |
| 52 |
*/ |
| 53 |
function gutenberg_guidelines_preload_rest( $hook_suffix ) { |
| 54 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
if ( 'settings_page_guidelines-wp-admin' !== $hook_suffix ) { |
| 59 |
return; |
| 60 |
} |
| 61 |
|
| 62 |
$preload_paths = array( |
| 63 |
'/wp/v2/knowledge/guideline-scopes', |
| 64 |
array( '/wp/v2/knowledge/guideline-scopes', 'OPTIONS' ), |
| 65 |
); |
| 66 |
|
| 67 |
$preload_data = array_reduce( $preload_paths, 'rest_preload_api_request', array() ); |
| 68 |
|
| 69 |
wp_add_inline_script( |
| 70 |
'wp-api-fetch', |
| 71 |
sprintf( |
| 72 |
'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );', |
| 73 |
wp_json_encode( $preload_data ) |
| 74 |
), |
| 75 |
'after' |
| 76 |
); |
| 77 |
} |
| 78 |
|