| 1 |
<?php |
| 2 |
/** |
| 3 |
* PHP and WordPress configuration compatibility functions for the Gutenberg |
| 4 |
* editor plugin changes related to REST API. |
| 5 |
* |
| 6 |
* @package gutenberg |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
die( 'Silence is golden.' ); |
| 11 |
} |
| 12 |
|
| 13 |
if ( ! function_exists( 'wp_api_template_access_controller' ) ) { |
| 14 |
/** |
| 15 |
* Hook in to the template and template part post types and modify the |
| 16 |
* access control for the rest endpoint to allow lower user roles to access |
| 17 |
* the templates and template parts. |
| 18 |
* |
| 19 |
* @param array $args Current registered post type args. |
| 20 |
* @param string $post_type Name of post type. |
| 21 |
* |
| 22 |
* @return array |
| 23 |
*/ |
| 24 |
function wp_api_template_access_controller( $args, $post_type ) { |
| 25 |
if ( 'wp_template' === $post_type || 'wp_template_part' === $post_type ) { |
| 26 |
$args['rest_controller_class'] = 'Gutenberg_REST_Templates_Controller_6_6'; |
| 27 |
} |
| 28 |
return $args; |
| 29 |
} |
| 30 |
} |
| 31 |
add_filter( 'register_post_type_args', 'wp_api_template_access_controller', 10, 2 ); |
| 32 |
|
| 33 |
/** |
| 34 |
* Adds the post classes to the REST API response. |
| 35 |
* |
| 36 |
* @param array $post The response object data. |
| 37 |
* |
| 38 |
* @return array |
| 39 |
*/ |
| 40 |
function gutenberg_add_class_list_to_api_response( $post ) { |
| 41 |
|
| 42 |
if ( ! isset( $post['id'] ) ) { |
| 43 |
return array(); |
| 44 |
} |
| 45 |
|
| 46 |
return get_post_class( array(), $post['id'] ); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Adds the post classes to public post types in the REST API. |
| 51 |
*/ |
| 52 |
function gutenberg_add_class_list_to_public_post_types() { |
| 53 |
$post_types = get_post_types( |
| 54 |
array( |
| 55 |
'public' => true, |
| 56 |
'show_in_rest' => true, |
| 57 |
), |
| 58 |
'names' |
| 59 |
); |
| 60 |
|
| 61 |
if ( ! empty( $post_types ) ) { |
| 62 |
register_rest_field( |
| 63 |
$post_types, |
| 64 |
'class_list', |
| 65 |
array( |
| 66 |
'get_callback' => 'gutenberg_add_class_list_to_api_response', |
| 67 |
'schema' => array( |
| 68 |
'description' => __( 'An array of the class names for the post container element.', 'gutenberg' ), |
| 69 |
'type' => 'array', |
| 70 |
'items' => array( |
| 71 |
'type' => 'string', |
| 72 |
), |
| 73 |
), |
| 74 |
) |
| 75 |
); |
| 76 |
} |
| 77 |
} |
| 78 |
add_action( 'rest_api_init', 'gutenberg_add_class_list_to_public_post_types' ); |
| 79 |
|
| 80 |
|
| 81 |
/** |
| 82 |
* Registers the Global Styles Revisions REST API routes. |
| 83 |
*/ |
| 84 |
function gutenberg_register_global_styles_revisions_endpoints() { |
| 85 |
$global_styles_revisions_controller = new Gutenberg_REST_Global_Styles_Revisions_Controller_6_6(); |
| 86 |
$global_styles_revisions_controller->register_routes(); |
| 87 |
} |
| 88 |
|
| 89 |
add_action( 'rest_api_init', 'gutenberg_register_global_styles_revisions_endpoints' ); |
| 90 |
|
| 91 |
/** |
| 92 |
* Adds `stylesheet_uri` fields to WP_REST_Themes_Controller class. |
| 93 |
*/ |
| 94 |
function gutenberg_register_wp_rest_themes_stylesheet_directory_uri_field() { |
| 95 |
register_rest_field( |
| 96 |
'theme', |
| 97 |
'stylesheet_uri', |
| 98 |
array( |
| 99 |
'get_callback' => function ( $item ) { |
| 100 |
if ( ! empty( $item['stylesheet'] ) ) { |
| 101 |
$theme = wp_get_theme( $item['stylesheet'] ); |
| 102 |
$current_theme = wp_get_theme(); |
| 103 |
if ( $theme->get_stylesheet() === $current_theme->get_stylesheet() ) { |
| 104 |
return get_stylesheet_directory_uri(); |
| 105 |
} else { |
| 106 |
return $theme->get_stylesheet_directory_uri(); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
return null; |
| 111 |
}, |
| 112 |
'schema' => array( |
| 113 |
'type' => 'string', |
| 114 |
'description' => __( 'The uri for the theme\'s stylesheet directory.', 'gutenberg' ), |
| 115 |
'format' => 'uri', |
| 116 |
'readonly' => true, |
| 117 |
'context' => array( 'view', 'edit', 'embed' ), |
| 118 |
), |
| 119 |
) |
| 120 |
); |
| 121 |
} |
| 122 |
add_action( 'rest_api_init', 'gutenberg_register_wp_rest_themes_stylesheet_directory_uri_field' ); |
| 123 |
|
| 124 |
/** |
| 125 |
* Adds `template_uri` fields to WP_REST_Themes_Controller class. |
| 126 |
*/ |
| 127 |
function gutenberg_register_wp_rest_themes_template_directory_uri_field() { |
| 128 |
register_rest_field( |
| 129 |
'theme', |
| 130 |
'template_uri', |
| 131 |
array( |
| 132 |
'get_callback' => function ( $item ) { |
| 133 |
if ( ! empty( $item['stylesheet'] ) ) { |
| 134 |
$theme = wp_get_theme( $item['stylesheet'] ); |
| 135 |
$current_theme = wp_get_theme(); |
| 136 |
if ( $theme->get_stylesheet() === $current_theme->get_stylesheet() ) { |
| 137 |
return get_template_directory_uri(); |
| 138 |
} else { |
| 139 |
return $theme->get_template_directory_uri(); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
return null; |
| 144 |
}, |
| 145 |
'schema' => array( |
| 146 |
'type' => 'string', |
| 147 |
'description' => __( 'The uri for the theme\'s template directory. If this is a child theme, this refers to the parent theme, otherwise this is the same as the theme\'s stylesheet directory.', 'gutenberg' ), |
| 148 |
'format' => 'uri', |
| 149 |
'readonly' => true, |
| 150 |
'context' => array( 'view', 'edit', 'embed' ), |
| 151 |
), |
| 152 |
) |
| 153 |
); |
| 154 |
} |
| 155 |
add_action( 'rest_api_init', 'gutenberg_register_wp_rest_themes_template_directory_uri_field' ); |
| 156 |
|
| 157 |
/** |
| 158 |
* Adds `template` and `template_lock` fields to WP_REST_Post_Types_Controller class. |
| 159 |
*/ |
| 160 |
function gutenberg_register_wp_rest_post_types_controller_fields() { |
| 161 |
register_rest_field( |
| 162 |
'type', |
| 163 |
'template', |
| 164 |
array( |
| 165 |
'get_callback' => function ( $item ) { |
| 166 |
$post_type = get_post_type_object( $item['slug'] ); |
| 167 |
if ( ! empty( $post_type ) ) { |
| 168 |
return $post_type->template ?? array(); |
| 169 |
} |
| 170 |
}, |
| 171 |
'schema' => array( |
| 172 |
'type' => 'array', |
| 173 |
'description' => __( 'The block template associated with the post type.', 'gutenberg' ), |
| 174 |
'readonly' => true, |
| 175 |
'context' => array( 'view', 'edit', 'embed' ), |
| 176 |
), |
| 177 |
) |
| 178 |
); |
| 179 |
register_rest_field( |
| 180 |
'type', |
| 181 |
'template_lock', |
| 182 |
array( |
| 183 |
'get_callback' => function ( $item ) { |
| 184 |
$post_type = get_post_type_object( $item['slug'] ); |
| 185 |
if ( ! empty( $post_type ) ) { |
| 186 |
return ! empty( $post_type->template_lock ) ? $post_type->template_lock : false; |
| 187 |
} |
| 188 |
}, |
| 189 |
'schema' => array( |
| 190 |
'type' => array( 'string', 'boolean' ), |
| 191 |
'enum' => array( 'all', 'insert', 'contentOnly', false ), |
| 192 |
'description' => __( 'The template_lock associated with the post type, or false if none.', 'gutenberg' ), |
| 193 |
'readonly' => true, |
| 194 |
'context' => array( 'view', 'edit', 'embed' ), |
| 195 |
), |
| 196 |
) |
| 197 |
); |
| 198 |
} |
| 199 |
add_action( 'rest_api_init', 'gutenberg_register_wp_rest_post_types_controller_fields' ); |
| 200 |
|
| 201 |
/** |
| 202 |
* Preload theme and global styles paths to avoid flash of variation styles in post editor. |
| 203 |
* |
| 204 |
* @param array $paths REST API paths to preload. |
| 205 |
* @param WP_Block_Editor_Context $context Current block editor context. |
| 206 |
* @return array Filtered preload paths. |
| 207 |
*/ |
| 208 |
function gutenberg_block_editor_preload_paths_6_6( $paths, $context ) { |
| 209 |
if ( 'core/edit-post' === $context->name ) { |
| 210 |
$paths[] = '/wp/v2/global-styles/themes/' . get_stylesheet(); |
| 211 |
$paths[] = '/wp/v2/themes?context=edit&status=active'; |
| 212 |
$paths[] = '/wp/v2/global-styles/' . WP_Theme_JSON_Resolver_Gutenberg::get_user_global_styles_post_id() . '?context=edit'; |
| 213 |
} |
| 214 |
return $paths; |
| 215 |
} |
| 216 |
add_filter( 'block_editor_rest_api_preload_paths', 'gutenberg_block_editor_preload_paths_6_6', 10, 2 ); |
| 217 |
|