| 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 |
|