| 1 |
<?php |
| 2 |
/** |
| 3 |
* Guidelines experimental feature. |
| 4 |
* |
| 5 |
* @package gutenberg |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
require_once __DIR__ . '/guidelines.php'; |
| 13 |
require_once __DIR__ . '/class-gutenberg-guidelines-post-type.php'; |
| 14 |
require_once __DIR__ . '/class-gutenberg-guidelines-rest-controller.php'; |
| 15 |
require_once __DIR__ . '/class-gutenberg-content-guidelines-revisions-controller.php'; |
| 16 |
require_once __DIR__ . '/class-gutenberg-content-guidelines-rest-controller.php'; |
| 17 |
|
| 18 |
/* |
| 19 |
* Register the guideline post type. |
| 20 |
* The standard /wp/v2/guidelines collection uses the default posts controller. |
| 21 |
*/ |
| 22 |
add_action( 'init', array( 'Gutenberg_Guidelines_Post_Type', 'register' ) ); |
| 23 |
|
| 24 |
/* |
| 25 |
* Ensure the post type is registered before any other `rest_api_init` callback |
| 26 |
* runs. `init` normally fires before `rest_api_init`, but anything that calls |
| 27 |
* `rest_get_server()` early (e.g. from `plugins_loaded`) fires `rest_api_init` |
| 28 |
* before `init` priority 10. The callbacks below — both `register_post_meta` |
| 29 |
* and the controller instantiations — dereference the post type object and |
| 30 |
* would fatal (or trip `_doing_it_wrong`) without this guard. |
| 31 |
*/ |
| 32 |
add_action( |
| 33 |
'rest_api_init', |
| 34 |
static function () { |
| 35 |
if ( ! post_type_exists( Gutenberg_Guidelines_Post_Type::POST_TYPE ) ) { |
| 36 |
Gutenberg_Guidelines_Post_Type::register(); |
| 37 |
} |
| 38 |
}, |
| 39 |
1 |
| 40 |
); |
| 41 |
|
| 42 |
// Register post meta once the REST API loads and the block registry is available. |
| 43 |
add_action( 'rest_api_init', array( 'Gutenberg_Guidelines_Post_Type', 'register_post_meta' ) ); |
| 44 |
|
| 45 |
/* |
| 46 |
* Register content singleton routes beside the standard CPT routes. |
| 47 |
* The singleton rule is scoped to /wp/v2/content-guidelines for UI handling. |
| 48 |
* The standard /wp/v2/guidelines route keeps default post handling for every |
| 49 |
* `wp_guideline` post. If `content` becomes a data level singleton, add |
| 50 |
* enforcement to the default CPT route too. |
| 51 |
*/ |
| 52 |
add_action( |
| 53 |
'rest_api_init', |
| 54 |
static function () { |
| 55 |
$content_controller = new Gutenberg_Content_Guidelines_REST_Controller(); |
| 56 |
$content_controller->register_routes(); |
| 57 |
|
| 58 |
$content_revisions_controller = new Gutenberg_Content_Guidelines_Revisions_Controller(); |
| 59 |
$content_revisions_controller->register_routes(); |
| 60 |
} |
| 61 |
); |
| 62 |
|
| 63 |
add_action( |
| 64 |
'current_screen', |
| 65 |
function ( $screen ) { |
| 66 |
if ( Gutenberg_Guidelines_Post_Type::POST_TYPE !== $screen->post_type ) { |
| 67 |
return; |
| 68 |
} |
| 69 |
|
| 70 |
// Disable the block editor for this post type. |
| 71 |
add_filter( 'use_block_editor_for_post_type', '__return_false' ); |
| 72 |
|
| 73 |
// Remove the media button. |
| 74 |
remove_action( 'media_buttons', 'media_buttons' ); |
| 75 |
|
| 76 |
// Use a plain textarea by disabling TinyMCE and Quicktags. |
| 77 |
add_filter( |
| 78 |
'wp_editor_settings', |
| 79 |
static function ( $settings, $editor_id ) { |
| 80 |
if ( 'content' === $editor_id ) { |
| 81 |
$settings['tinymce'] = false; |
| 82 |
$settings['quicktags'] = false; |
| 83 |
} |
| 84 |
return $settings; |
| 85 |
}, |
| 86 |
10, |
| 87 |
2 |
| 88 |
); |
| 89 |
} |
| 90 |
); |
| 91 |
|