| 1 |
<?php |
| 2 |
/** |
| 3 |
* Initialization and wp-admin integration for the Gutenberg editor plugin. |
| 4 |
* |
| 5 |
* @package gutenberg |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
die( 'Silence is golden.' ); |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Collect information about meta_boxes registered for the current post. |
| 14 |
* |
| 15 |
* Redirects to classic editor if a meta box is incompatible. |
| 16 |
* |
| 17 |
* @since 1.5.0 |
| 18 |
*/ |
| 19 |
function gutenberg_collect_meta_box_data() { |
| 20 |
global $current_screen, $wp_meta_boxes, $post, $typenow; |
| 21 |
|
| 22 |
$screen = $current_screen; |
| 23 |
|
| 24 |
// If we are working with an already predetermined post. |
| 25 |
if ( isset( $_REQUEST['post'] ) ) { |
| 26 |
$post = get_post( absint( $_REQUEST['post'] ) ); |
| 27 |
$typenow = $post->post_type; |
| 28 |
|
| 29 |
if ( ! gutenberg_can_edit_post( $post->ID ) ) { |
| 30 |
return; |
| 31 |
} |
| 32 |
} else { |
| 33 |
// Eventually add handling for creating new posts of different types in Gutenberg. |
| 34 |
} |
| 35 |
$post_type = $post->post_type; |
| 36 |
$post_type_object = get_post_type_object( $post_type ); |
| 37 |
|
| 38 |
if ( ! gutenberg_can_edit_post_type( $post_type ) ) { |
| 39 |
return; |
| 40 |
} |
| 41 |
|
| 42 |
// Disable hidden metaboxes because there's no UI to toggle visibility. |
| 43 |
add_filter( 'hidden_meta_boxes', '__return_empty_array' ); |
| 44 |
|
| 45 |
$thumbnail_support = current_theme_supports( 'post-thumbnails', $post_type ) && post_type_supports( $post_type, 'thumbnail' ); |
| 46 |
if ( ! $thumbnail_support && 'attachment' === $post_type && $post->post_mime_type ) { |
| 47 |
if ( wp_attachment_is( 'audio', $post ) ) { |
| 48 |
$thumbnail_support = post_type_supports( 'attachment:audio', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:audio' ); |
| 49 |
} elseif ( wp_attachment_is( 'video', $post ) ) { |
| 50 |
$thumbnail_support = post_type_supports( 'attachment:video', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:video' ); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
/* |
| 55 |
* WIP: Collect and send information needed to render meta boxes. |
| 56 |
* From wp-admin/edit-form-advanced.php |
| 57 |
* Relevant code there: |
| 58 |
* do_action( 'do_meta_boxes', $post_type, {'normal','advanced','side'}, $post ); |
| 59 |
* do_meta_boxes( $post_type, 'side', $post ); |
| 60 |
* do_meta_boxes( null, 'normal', $post ); |
| 61 |
* do_meta_boxes( null, 'advanced', $post ); |
| 62 |
*/ |
| 63 |
$publish_callback_args = null; |
| 64 |
if ( post_type_supports( $post_type, 'revisions' ) && 'auto-draft' !== $post->post_status ) { |
| 65 |
$revisions = wp_get_post_revisions( $post->ID ); |
| 66 |
|
| 67 |
// We should aim to show the revisions meta box only when there are revisions. |
| 68 |
if ( count( $revisions ) > 1 ) { |
| 69 |
reset( $revisions ); // Reset pointer for key(). |
| 70 |
$publish_callback_args = array( |
| 71 |
'revisions_count' => count( $revisions ), |
| 72 |
'revision_id' => key( $revisions ), |
| 73 |
); |
| 74 |
add_meta_box( 'revisionsdiv', __( 'Revisions', 'gutenberg' ), 'post_revisions_meta_box', $screen, 'normal', 'core' ); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
if ( 'attachment' == $post_type ) { |
| 79 |
wp_enqueue_script( 'image-edit' ); |
| 80 |
wp_enqueue_style( 'imgareaselect' ); |
| 81 |
add_meta_box( 'submitdiv', __( 'Save', 'gutenberg' ), 'attachment_submit_meta_box', $screen, 'side', 'core' ); |
| 82 |
add_action( 'edit_form_after_title', 'edit_form_image_editor' ); |
| 83 |
|
| 84 |
if ( wp_attachment_is( 'audio', $post ) ) { |
| 85 |
add_meta_box( 'attachment-id3', __( 'Metadata', 'gutenberg' ), 'attachment_id3_data_meta_box', $screen, 'normal', 'core' ); |
| 86 |
} |
| 87 |
} else { |
| 88 |
add_meta_box( 'submitdiv', __( 'Publish', 'gutenberg' ), 'post_submit_meta_box', $screen, 'side', 'core', $publish_callback_args ); |
| 89 |
} |
| 90 |
|
| 91 |
if ( current_theme_supports( 'post-formats' ) && post_type_supports( $post_type, 'post-formats' ) ) { |
| 92 |
add_meta_box( 'formatdiv', _x( 'Format', 'post format', 'gutenberg' ), 'post_format_meta_box', $screen, 'side', 'core' ); |
| 93 |
} |
| 94 |
|
| 95 |
// All taxonomies. |
| 96 |
foreach ( get_object_taxonomies( $post ) as $tax_name ) { |
| 97 |
$taxonomy = get_taxonomy( $tax_name ); |
| 98 |
if ( ! $taxonomy->show_ui || false === $taxonomy->meta_box_cb ) { |
| 99 |
continue; |
| 100 |
} |
| 101 |
|
| 102 |
$label = $taxonomy->labels->name; |
| 103 |
|
| 104 |
if ( ! is_taxonomy_hierarchical( $tax_name ) ) { |
| 105 |
$tax_meta_box_id = 'tagsdiv-' . $tax_name; |
| 106 |
} else { |
| 107 |
$tax_meta_box_id = $tax_name . 'div'; |
| 108 |
} |
| 109 |
|
| 110 |
add_meta_box( $tax_meta_box_id, $label, $taxonomy->meta_box_cb, $screen, 'side', 'core', array( 'taxonomy' => $tax_name ) ); |
| 111 |
} |
| 112 |
|
| 113 |
if ( post_type_supports( $post_type, 'page-attributes' ) || count( get_page_templates( $post ) ) > 0 ) { |
| 114 |
add_meta_box( 'pageparentdiv', $post_type_object->labels->attributes, 'page_attributes_meta_box', $screen, 'side', 'core' ); |
| 115 |
} |
| 116 |
|
| 117 |
if ( $thumbnail_support && current_user_can( 'upload_files' ) ) { |
| 118 |
add_meta_box( 'postimagediv', esc_html( $post_type_object->labels->featured_image ), 'post_thumbnail_meta_box', $screen, 'side', 'low' ); |
| 119 |
} |
| 120 |
|
| 121 |
if ( post_type_supports( $post_type, 'excerpt' ) ) { |
| 122 |
add_meta_box( 'postexcerpt', __( 'Excerpt', 'gutenberg' ), 'post_excerpt_meta_box', $screen, 'normal', 'core' ); |
| 123 |
} |
| 124 |
|
| 125 |
if ( post_type_supports( $post_type, 'trackbacks' ) ) { |
| 126 |
add_meta_box( 'trackbacksdiv', __( 'Send Trackbacks', 'gutenberg' ), 'post_trackback_meta_box', $screen, 'normal', 'core' ); |
| 127 |
} |
| 128 |
|
| 129 |
if ( post_type_supports( $post_type, 'custom-fields' ) ) { |
| 130 |
add_meta_box( 'postcustom', __( 'Custom Fields', 'gutenberg' ), 'post_custom_meta_box', $screen, 'normal', 'core' ); |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Fires in the middle of built-in meta box registration. |
| 135 |
* |
| 136 |
* @since 2.1.0 |
| 137 |
* @deprecated 3.7.0 Use 'add_meta_boxes' instead. |
| 138 |
* |
| 139 |
* @param WP_Post $post Post object. |
| 140 |
*/ |
| 141 |
do_action( 'dbx_post_advanced', $post ); |
| 142 |
|
| 143 |
// Allow the Discussion meta box to show up if the post type supports comments, |
| 144 |
// or if comments or pings are open. |
| 145 |
if ( comments_open( $post ) || pings_open( $post ) || post_type_supports( $post_type, 'comments' ) ) { |
| 146 |
add_meta_box( 'commentstatusdiv', __( 'Discussion', 'gutenberg' ), 'post_comment_status_meta_box', $screen, 'normal', 'core' ); |
| 147 |
} |
| 148 |
|
| 149 |
$stati = get_post_stati( array( 'public' => true ) ); |
| 150 |
if ( empty( $stati ) ) { |
| 151 |
$stati = array( 'publish' ); |
| 152 |
} |
| 153 |
$stati[] = 'private'; |
| 154 |
|
| 155 |
if ( in_array( get_post_status( $post ), $stati ) ) { |
| 156 |
// If the post type support comments, or the post has comments, allow the |
| 157 |
// Comments meta box. |
| 158 |
if ( comments_open( $post ) || pings_open( $post ) || $post->comment_count > 0 || post_type_supports( $post_type, 'comments' ) ) { |
| 159 |
add_meta_box( 'commentsdiv', __( 'Comments', 'gutenberg' ), 'post_comment_meta_box', $screen, 'normal', 'core' ); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
if ( ! ( 'pending' == get_post_status( $post ) && ! current_user_can( $post_type_object->cap->publish_posts ) ) ) { |
| 164 |
add_meta_box( 'slugdiv', __( 'Slug', 'gutenberg' ), 'post_slug_meta_box', $screen, 'normal', 'core' ); |
| 165 |
} |
| 166 |
|
| 167 |
if ( post_type_supports( $post_type, 'author' ) && current_user_can( $post_type_object->cap->edit_others_posts ) ) { |
| 168 |
add_meta_box( 'authordiv', __( 'Author', 'gutenberg' ), 'post_author_meta_box', $screen, 'normal', 'core' ); |
| 169 |
} |
| 170 |
|
| 171 |
// Run the hooks for adding meta boxes for a specific post type. |
| 172 |
do_action( 'add_meta_boxes', $post_type, $post ); |
| 173 |
do_action( "add_meta_boxes_{$post_type}", $post ); |
| 174 |
|
| 175 |
// Set up meta box locations. |
| 176 |
$locations = array( 'normal', 'advanced', 'side' ); |
| 177 |
|
| 178 |
// Foreach location run the hooks meta boxes are potentially registered on. |
| 179 |
foreach ( $locations as $location ) { |
| 180 |
do_action( |
| 181 |
'do_meta_boxes', |
| 182 |
$screen, |
| 183 |
$location, |
| 184 |
$post |
| 185 |
); |
| 186 |
} |
| 187 |
do_action( 'edit_form_advanced', $post ); |
| 188 |
|
| 189 |
// Copy meta box state. |
| 190 |
$_meta_boxes_copy = $wp_meta_boxes; |
| 191 |
|
| 192 |
/** |
| 193 |
* Documented in lib/meta-box-partial-page.php |
| 194 |
* |
| 195 |
* @param array $wp_meta_boxes Global meta box state. |
| 196 |
*/ |
| 197 |
$_meta_boxes_copy = apply_filters( 'filter_gutenberg_meta_boxes', $_meta_boxes_copy ); |
| 198 |
|
| 199 |
// Redirect to classic editor if a meta box is incompatible. |
| 200 |
foreach ( $locations as $location ) { |
| 201 |
if ( ! isset( $_meta_boxes_copy[ $post->post_type ][ $location ] ) ) { |
| 202 |
continue; |
| 203 |
} |
| 204 |
// Check if we have a meta box that has declared itself incompatible with the block editor. |
| 205 |
foreach ( $_meta_boxes_copy[ $post->post_type ][ $location ] as $boxes ) { |
| 206 |
foreach ( $boxes as $box ) { |
| 207 |
/* |
| 208 |
* If __block_editor_compatible_meta_box is declared as a false-y value, |
| 209 |
* the meta box is not compatible with the block editor. |
| 210 |
*/ |
| 211 |
if ( is_array( $box['args'] ) |
| 212 |
&& isset( $box['args']['__block_editor_compatible_meta_box'] ) |
| 213 |
&& ! $box['args']['__block_editor_compatible_meta_box'] ) { |
| 214 |
$incompatible_meta_box = true; |
| 215 |
?> |
| 216 |
<script type="text/javascript"> |
| 217 |
var joiner = '?'; |
| 218 |
if ( window.location.search ) { |
| 219 |
joiner = '&'; |
| 220 |
} |
| 221 |
window.location.href += joiner + 'classic-editor'; |
| 222 |
</script> |
| 223 |
<?php |
| 224 |
exit; |
| 225 |
} |
| 226 |
} |
| 227 |
} |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Return whether the post can be edited in Gutenberg and by the current user. |
| 233 |
* |
| 234 |
* @since 0.5.0 |
| 235 |
* |
| 236 |
* @param int|WP_Post $post Post ID or WP_Post object. |
| 237 |
* @return bool Whether the post can be edited with Gutenberg. |
| 238 |
*/ |
| 239 |
function gutenberg_can_edit_post( $post ) { |
| 240 |
$post = get_post( $post ); |
| 241 |
$can_edit = true; |
| 242 |
|
| 243 |
if ( ! $post ) { |
| 244 |
$can_edit = false; |
| 245 |
} |
| 246 |
|
| 247 |
if ( $can_edit && 'trash' === $post->post_status ) { |
| 248 |
$can_edit = false; |
| 249 |
} |
| 250 |
|
| 251 |
if ( $can_edit && ! gutenberg_can_edit_post_type( $post->post_type ) ) { |
| 252 |
$can_edit = false; |
| 253 |
} |
| 254 |
|
| 255 |
if ( $can_edit && ! current_user_can( 'edit_post', $post->ID ) ) { |
| 256 |
$can_edit = false; |
| 257 |
} |
| 258 |
|
| 259 |
// Disable the editor if on the blog page and there is no content. |
| 260 |
if ( $can_edit && absint( get_option( 'page_for_posts' ) ) === $post->ID && empty( $post->post_content ) ) { |
| 261 |
$can_edit = false; |
| 262 |
} |
| 263 |
|
| 264 |
/** |
| 265 |
* Filter to allow plugins to enable/disable Gutenberg for particular post. |
| 266 |
* |
| 267 |
* @since 3.5 |
| 268 |
* |
| 269 |
* @param bool $can_edit Whether the post can be edited or not. |
| 270 |
* @param WP_Post $post The post being checked. |
| 271 |
*/ |
| 272 |
return apply_filters( 'gutenberg_can_edit_post', $can_edit, $post ); |
| 273 |
|
| 274 |
} |
| 275 |
|
| 276 |
/** |
| 277 |
* Return whether the post type can be edited in Gutenberg. |
| 278 |
* |
| 279 |
* Gutenberg depends on the REST API, and if the post type is not shown in the |
| 280 |
* REST API, then the post cannot be edited in Gutenberg. |
| 281 |
* |
| 282 |
* @since 1.5.2 |
| 283 |
* |
| 284 |
* @param string $post_type The post type. |
| 285 |
* @return bool Whether the post type can be edited with Gutenberg. |
| 286 |
*/ |
| 287 |
function gutenberg_can_edit_post_type( $post_type ) { |
| 288 |
$can_edit = true; |
| 289 |
if ( ! post_type_exists( $post_type ) ) { |
| 290 |
$can_edit = false; |
| 291 |
} |
| 292 |
|
| 293 |
if ( ! post_type_supports( $post_type, 'editor' ) ) { |
| 294 |
$can_edit = false; |
| 295 |
} |
| 296 |
|
| 297 |
$post_type_object = get_post_type_object( $post_type ); |
| 298 |
if ( $post_type_object && ! $post_type_object->show_in_rest ) { |
| 299 |
$can_edit = false; |
| 300 |
} |
| 301 |
|
| 302 |
/** |
| 303 |
* Filter to allow plugins to enable/disable Gutenberg for particular post types. |
| 304 |
* |
| 305 |
* @since 1.5.2 |
| 306 |
* |
| 307 |
* @param bool $can_edit Whether the post type can be edited or not. |
| 308 |
* @param string $post_type The post type being checked. |
| 309 |
*/ |
| 310 |
return apply_filters( 'gutenberg_can_edit_post_type', $can_edit, $post_type ); |
| 311 |
} |
| 312 |
|
| 313 |
if ( ! function_exists( 'has_blocks' ) ) { |
| 314 |
/** |
| 315 |
* Determine whether a post or content string has blocks. |
| 316 |
* |
| 317 |
* This test optimizes for performance rather than strict accuracy, detecting |
| 318 |
* the pattern of a block but not validating its structure. For strict accuracy |
| 319 |
* you should use the block parser on post content. |
| 320 |
* |
| 321 |
* @since 3.6.0 |
| 322 |
* @see gutenberg_parse_blocks() |
| 323 |
* |
| 324 |
* @param int|string|WP_Post|null $post Optional. Post content, post ID, or post object. Defaults to global $post. |
| 325 |
* @return bool Whether the post has blocks. |
| 326 |
*/ |
| 327 |
function has_blocks( $post = null ) { |
| 328 |
if ( ! is_string( $post ) ) { |
| 329 |
$wp_post = get_post( $post ); |
| 330 |
if ( $wp_post instanceof WP_Post ) { |
| 331 |
$post = $wp_post->post_content; |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
return false !== strpos( (string) $post, '<!-- wp:' ); |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
/** |
| 340 |
* Determine whether a post has blocks. This test optimizes for performance |
| 341 |
* rather than strict accuracy, detecting the pattern of a block but not |
| 342 |
* validating its structure. For strict accuracy, you should use the block |
| 343 |
* parser on post content. |
| 344 |
* |
| 345 |
* @see gutenberg_parse_blocks() |
| 346 |
* |
| 347 |
* @since 0.5.0 |
| 348 |
* @deprecated 3.6.0 Use has_blocks() |
| 349 |
* |
| 350 |
* @param object $post Post. |
| 351 |
* @return bool Whether the post has blocks. |
| 352 |
*/ |
| 353 |
function gutenberg_post_has_blocks( $post ) { |
| 354 |
_deprecated_function( __FUNCTION__, '3.6.0', 'has_blocks()' ); |
| 355 |
return has_blocks( $post ); |
| 356 |
} |
| 357 |
|
| 358 |
/** |
| 359 |
* Determine whether a content string contains blocks. This test optimizes for |
| 360 |
* performance rather than strict accuracy, detecting the pattern of a block |
| 361 |
* but not validating its structure. For strict accuracy, you should use the |
| 362 |
* block parser on post content. |
| 363 |
* |
| 364 |
* @see gutenberg_parse_blocks() |
| 365 |
* |
| 366 |
* @since 1.6.0 |
| 367 |
* @deprecated 3.6.0 Use has_blocks() |
| 368 |
* |
| 369 |
* @param string $content Content to test. |
| 370 |
* @return bool Whether the content contains blocks. |
| 371 |
*/ |
| 372 |
function gutenberg_content_has_blocks( $content ) { |
| 373 |
_deprecated_function( __FUNCTION__, '3.6.0', 'has_blocks()' ); |
| 374 |
return has_blocks( $content ); |
| 375 |
} |
| 376 |
|
| 377 |
if ( ! function_exists( 'has_block' ) ) { |
| 378 |
/** |
| 379 |
* Determine whether a $post or a string contains a specific block type. |
| 380 |
* This test optimizes for performance rather than strict accuracy, detecting |
| 381 |
* the block type exists but not validating its structure. |
| 382 |
* For strict accuracy, you should use the block parser on post content. |
| 383 |
* |
| 384 |
* @since 3.6.0 |
| 385 |
* |
| 386 |
* @param string $block_type Full Block type to look for. |
| 387 |
* @param int|string|WP_Post|null $post Optional. Post content, post ID, or post object. Defaults to global $post. |
| 388 |
* @return bool Whether the post content contains the specified block. |
| 389 |
*/ |
| 390 |
function has_block( $block_type, $post = null ) { |
| 391 |
if ( ! has_blocks( $post ) ) { |
| 392 |
return false; |
| 393 |
} |
| 394 |
|
| 395 |
if ( ! is_string( $post ) ) { |
| 396 |
$wp_post = get_post( $post ); |
| 397 |
if ( $wp_post instanceof WP_Post ) { |
| 398 |
$post = $wp_post->post_content; |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
return false !== strpos( $post, '<!-- wp:' . $block_type . ' ' ); |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
/** |
| 407 |
* Returns the current version of the block format that the content string is using. |
| 408 |
* |
| 409 |
* If the string doesn't contain blocks, it returns 0. |
| 410 |
* |
| 411 |
* @since 2.8.0 |
| 412 |
* @see gutenberg_content_has_blocks() |
| 413 |
* |
| 414 |
* @param string $content Content to test. |
| 415 |
* @return int The block format version. |
| 416 |
*/ |
| 417 |
function gutenberg_content_block_version( $content ) { |
| 418 |
return has_blocks( $content ) ? 1 : 0; |
| 419 |
} |
| 420 |
|
| 421 |
/** |
| 422 |
* Adds a "Gutenberg" post state for post tables, if the post contains blocks. |
| 423 |
* |
| 424 |
* @param array $post_states An array of post display states. |
| 425 |
* @param WP_Post $post The current post object. |
| 426 |
* @return array A filtered array of post display states. |
| 427 |
*/ |
| 428 |
function gutenberg_add_gutenberg_post_state( $post_states, $post ) { |
| 429 |
if ( has_blocks( $post ) ) { |
| 430 |
$post_states[] = 'Gutenberg'; |
| 431 |
} |
| 432 |
|
| 433 |
return $post_states; |
| 434 |
} |
| 435 |
add_filter( 'display_post_states', 'gutenberg_add_gutenberg_post_state', 10, 2 ); |
| 436 |
|
| 437 |
/** |
| 438 |
* Registers custom post types required by the Gutenberg editor. |
| 439 |
* |
| 440 |
* @since 0.10.0 |
| 441 |
*/ |
| 442 |
function gutenberg_register_post_types() { |
| 443 |
register_post_type( |
| 444 |
'wp_block', |
| 445 |
array( |
| 446 |
'labels' => array( |
| 447 |
'name' => __( 'Blocks', 'gutenberg' ), |
| 448 |
'singular_name' => __( 'Block', 'gutenberg' ), |
| 449 |
'search_items' => __( 'Search Blocks', 'gutenberg' ), |
| 450 |
), |
| 451 |
'public' => false, |
| 452 |
'show_ui' => true, |
| 453 |
'show_in_menu' => false, |
| 454 |
'rewrite' => false, |
| 455 |
'show_in_rest' => true, |
| 456 |
'rest_base' => 'blocks', |
| 457 |
'rest_controller_class' => 'WP_REST_Blocks_Controller', |
| 458 |
'capability_type' => 'block', |
| 459 |
'capabilities' => array( |
| 460 |
'read' => 'read_blocks', |
| 461 |
'create_posts' => 'create_blocks', |
| 462 |
), |
| 463 |
'map_meta_cap' => true, |
| 464 |
'supports' => array( |
| 465 |
'title', |
| 466 |
'editor', |
| 467 |
), |
| 468 |
) |
| 469 |
); |
| 470 |
|
| 471 |
$editor_caps = array( |
| 472 |
'edit_blocks', |
| 473 |
'edit_others_blocks', |
| 474 |
'publish_blocks', |
| 475 |
'read_private_blocks', |
| 476 |
'read_blocks', |
| 477 |
'delete_blocks', |
| 478 |
'delete_private_blocks', |
| 479 |
'delete_published_blocks', |
| 480 |
'delete_others_blocks', |
| 481 |
'edit_private_blocks', |
| 482 |
'edit_published_blocks', |
| 483 |
'create_blocks', |
| 484 |
); |
| 485 |
|
| 486 |
$caps_map = array( |
| 487 |
'administrator' => $editor_caps, |
| 488 |
'editor' => $editor_caps, |
| 489 |
'author' => array( |
| 490 |
'edit_blocks', |
| 491 |
'publish_blocks', |
| 492 |
'read_blocks', |
| 493 |
'delete_blocks', |
| 494 |
'delete_published_blocks', |
| 495 |
'edit_published_blocks', |
| 496 |
'create_blocks', |
| 497 |
), |
| 498 |
'contributor' => array( |
| 499 |
'read_blocks', |
| 500 |
), |
| 501 |
); |
| 502 |
|
| 503 |
foreach ( $caps_map as $role_name => $caps ) { |
| 504 |
$role = get_role( $role_name ); |
| 505 |
|
| 506 |
if ( empty( $role ) ) { |
| 507 |
continue; |
| 508 |
} |
| 509 |
|
| 510 |
foreach ( $caps as $cap ) { |
| 511 |
if ( ! $role->has_cap( $cap ) ) { |
| 512 |
$role->add_cap( $cap ); |
| 513 |
} |
| 514 |
} |
| 515 |
} |
| 516 |
} |
| 517 |
add_action( 'init', 'gutenberg_register_post_types' ); |
| 518 |
|
| 519 |
/** |
| 520 |
* Injects a hidden input in the edit form to propagate the information that classic editor is selected. |
| 521 |
* |
| 522 |
* @since 1.5.2 |
| 523 |
*/ |
| 524 |
function gutenberg_remember_classic_editor_when_saving_posts() { |
| 525 |
?> |
| 526 |
<input type="hidden" name="classic-editor" /> |
| 527 |
<?php |
| 528 |
} |
| 529 |
add_action( 'edit_form_top', 'gutenberg_remember_classic_editor_when_saving_posts' ); |
| 530 |
|
| 531 |
/** |
| 532 |
* Appends a query argument to the redirect url to make sure it gets redirected to the classic editor. |
| 533 |
* |
| 534 |
* @since 1.5.2 |
| 535 |
* |
| 536 |
* @param string $url Redirect url. |
| 537 |
* @return string Redirect url. |
| 538 |
*/ |
| 539 |
function gutenberg_redirect_to_classic_editor_when_saving_posts( $url ) { |
| 540 |
if ( isset( $_REQUEST['classic-editor'] ) ) { |
| 541 |
$url = add_query_arg( 'classic-editor', '', $url ); |
| 542 |
} |
| 543 |
return $url; |
| 544 |
} |
| 545 |
add_filter( 'redirect_post_location', 'gutenberg_redirect_to_classic_editor_when_saving_posts', 10, 1 ); |
| 546 |
|
| 547 |
/** |
| 548 |
* Appends a query argument to the edit url to make sure it is redirected to |
| 549 |
* the editor from which the user navigated. |
| 550 |
* |
| 551 |
* @since 1.5.2 |
| 552 |
* |
| 553 |
* @param string $url Edit url. |
| 554 |
* @return string Edit url. |
| 555 |
*/ |
| 556 |
function gutenberg_revisions_link_to_editor( $url ) { |
| 557 |
global $pagenow; |
| 558 |
if ( 'revision.php' !== $pagenow || isset( $_REQUEST['gutenberg'] ) ) { |
| 559 |
return $url; |
| 560 |
} |
| 561 |
|
| 562 |
return add_query_arg( 'classic-editor', '', $url ); |
| 563 |
} |
| 564 |
add_filter( 'get_edit_post_link', 'gutenberg_revisions_link_to_editor' ); |
| 565 |
|
| 566 |
/** |
| 567 |
* Modifies revisions data to preserve Gutenberg argument used in determining |
| 568 |
* where to redirect user returning to editor. |
| 569 |
* |
| 570 |
* @since 1.9.0 |
| 571 |
* |
| 572 |
* @param array $revisions_data The bootstrapped data for the revisions screen. |
| 573 |
* @return array Modified bootstrapped data for the revisions screen. |
| 574 |
*/ |
| 575 |
function gutenberg_revisions_restore( $revisions_data ) { |
| 576 |
if ( isset( $_REQUEST['gutenberg'] ) ) { |
| 577 |
$revisions_data['restoreUrl'] = add_query_arg( |
| 578 |
'gutenberg', |
| 579 |
$_REQUEST['gutenberg'], |
| 580 |
$revisions_data['restoreUrl'] |
| 581 |
); |
| 582 |
} |
| 583 |
|
| 584 |
return $revisions_data; |
| 585 |
} |
| 586 |
add_filter( 'wp_prepare_revision_for_js', 'gutenberg_revisions_restore' ); |
| 587 |
|