PluginProbe
Gutenberg / 22.3.0
Gutenberg v22.3.0
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
gutenberg / lib / experimental / rest-api-overrides.php

rest-api-overrides.php in Gutenberg 22.3.0, at lib/experimental/rest-api-overrides.php

53 lines 1.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Boot package REST API overrides.
4 *
5 * @package gutenberg
6 */
7
8 /**
9 * Allow auto-draft status in WordPress REST API for all post types.
10 *
11 * By default, WordPress REST API doesn't include 'auto-draft' in the
12 * allowed status values. This function adds it to the schema enum for
13 * all eligible post types.
14 */
15 function gutenberg_boot_allow_auto_draft_in_rest_api() {
16 $auto_draft_filter = function ( $schema ) {
17 if ( isset( $schema['properties']['status']['enum'] ) ) {
18 $schema['properties']['status']['enum'][] = 'auto-draft';
19 }
20 return $schema;
21 };
22
23 // Get post types that should support auto-draft.
24 $post_types = get_post_types(
25 array(
26 'show_in_rest' => true,
27 'show_ui' => true,
28 ),
29 'objects'
30 );
31
32 foreach ( $post_types as $post_type ) {
33 $supports_editor = post_type_supports( $post_type->name, 'editor' );
34 $supports_title = post_type_supports( $post_type->name, 'title' );
35 $is_wp_core_system = strpos( $post_type->name, 'wp_' ) === 0;
36 $is_attachment = 'attachment' === $post_type->name;
37
38 // Only add auto-draft to content post types:
39 // - Must support either editor or title (content editing capabilities).
40 // - Exclude WordPress core system types (wp_block, wp_navigation, etc.).
41 // - Exclude attachments (media files don't need auto-draft status).
42 $should_support_auto_draft = ( $supports_editor || $supports_title ) &&
43 ! $is_wp_core_system &&
44 ! $is_attachment;
45
46 if ( $should_support_auto_draft ) {
47 add_filter( "rest_{$post_type->name}_item_schema", $auto_draft_filter );
48 }
49 }
50 }
51
52 add_action( 'rest_api_init', 'gutenberg_boot_allow_auto_draft_in_rest_api' );
53