Ticket #46195: 46195.7.diff
File 46195.7.diff, 16.3 KB (added by , 6 years ago) |
---|
-
src/wp-admin/edit-form-advanced.php
18 18 */ 19 19 global $post_type, $post_type_object, $post; 20 20 21 // Flag that we're not loading the block editor. 22 $current_screen = get_current_screen(); 23 $current_screen->is_block_editor( false ); 24 21 25 if ( is_multisite() ) { 22 26 add_action( 'admin_footer', '_admin_notice_post_locked' ); 23 27 } else { -
src/wp-admin/includes/class-wp-screen.php
202 202 return $hook_name; 203 203 } 204 204 205 $post_type = $taxonomy = null; 206 $in_admin = false; 207 $action = ''; 205 $post_type = $taxonomy = null; 206 $in_admin = false; 207 $action = ''; 208 $is_block_editor = false; 208 209 209 210 if ( $hook_name ) { 210 211 $id = $hook_name; … … 294 295 $post = get_post( $post_id ); 295 296 if ( $post ) { 296 297 $post_type = $post->post_type; 298 299 /** This filter is documented in wp-admin/post.php */ 300 $replace_editor = apply_filters( 'replace_editor', false, $post ); 301 302 if ( ! $replace_editor ) { 303 $is_block_editor = use_block_editor_for_post( $post ); 304 } 297 305 } 298 306 } 299 307 break; … … 314 322 if ( null === $post_type ) { 315 323 $post_type = 'post'; 316 324 } 325 326 // When creating a new post, use the default block editor support value for the post type. 327 if ( empty( $post_id ) ) { 328 $is_block_editor = use_block_editor_for_post_type( $post_type ); 329 } 330 317 331 $id = $post_type; 318 332 break; 319 333 case 'edit': … … 357 371 $screen->id = $id; 358 372 } 359 373 360 $screen->base = $base; 361 $screen->action = $action; 362 $screen->post_type = (string) $post_type; 363 $screen->taxonomy = (string) $taxonomy; 364 $screen->is_user = ( 'user' == $in_admin ); 365 $screen->is_network = ( 'network' == $in_admin ); 366 $screen->in_admin = $in_admin; 374 $screen->base = $base; 375 $screen->action = $action; 376 $screen->post_type = (string) $post_type; 377 $screen->taxonomy = (string) $taxonomy; 378 $screen->is_user = ( 'user' == $in_admin ); 379 $screen->is_network = ( 'network' == $in_admin ); 380 $screen->in_admin = $in_admin; 381 $screen->is_block_editor = $is_block_editor; 367 382 368 383 self::$_registry[ $id ] = $screen; 369 384 -
src/wp-admin/post-new.php
74 74 wp_enqueue_script( 'autosave' ); 75 75 include( ABSPATH . 'wp-admin/edit-form-advanced.php' ); 76 76 } 77 } else { 78 // Flag that we're not loading the block editor. 79 $current_screen = get_current_screen(); 80 $current_screen->is_block_editor( false ); 77 81 } 78 82 79 83 include( ABSPATH . 'wp-admin/admin-footer.php' ); -
tests/phpunit/tests/admin/includesScreen.php
7 7 class Tests_Admin_includesScreen extends WP_UnitTestCase { 8 8 var $core_screens = array( 9 9 'index.php' => array( 10 'base' => 'dashboard', 11 'id' => 'dashboard', 10 'base' => 'dashboard', 11 'id' => 'dashboard', 12 'is_block_editor' => false, 12 13 ), 13 14 'edit.php' => array( 14 'base' => 'edit', 15 'id' => 'edit-post', 16 'post_type' => 'post', 15 'base' => 'edit', 16 'id' => 'edit-post', 17 'post_type' => 'post', 18 'is_block_editor' => false, 17 19 ), 18 20 'post-new.php' => array( 19 'action' => 'add', 20 'base' => 'post', 21 'id' => 'post', 22 'post_type' => 'post', 21 'action' => 'add', 22 'base' => 'post', 23 'id' => 'post', 24 'post_type' => 'post', 25 'is_block_editor' => true, 26 ), 27 'post.php' => array( 28 'base' => 'post', 29 'id' => 'post', 30 'post_type' => 'post', 31 'is_block_editor' => true, 23 32 ), 24 33 'edit-tags.php' => array( 25 'base' => 'edit-tags', 26 'id' => 'edit-post_tag', 27 'post_type' => 'post', 28 'taxonomy' => 'post_tag', 34 'base' => 'edit-tags', 35 'id' => 'edit-post_tag', 36 'post_type' => 'post', 37 'taxonomy' => 'post_tag', 38 'is_block_editor' => false, 29 39 ), 30 40 'edit-tags.php?taxonomy=post_tag' => array( 31 'base' => 'edit-tags', 32 'id' => 'edit-post_tag', 33 'post_type' => 'post', 34 'taxonomy' => 'post_tag', 41 'base' => 'edit-tags', 42 'id' => 'edit-post_tag', 43 'post_type' => 'post', 44 'taxonomy' => 'post_tag', 45 'is_block_editor' => false, 35 46 ), 36 47 'edit-tags.php?taxonomy=category' => array( 37 'base' => 'edit-tags', 38 'id' => 'edit-category', 39 'post_type' => 'post', 40 'taxonomy' => 'category', 48 'base' => 'edit-tags', 49 'id' => 'edit-category', 50 'post_type' => 'post', 51 'taxonomy' => 'category', 52 'is_block_editor' => false, 41 53 ), 42 54 'upload.php' => array( 43 'base' => 'upload', 44 'id' => 'upload', 45 'post_type' => 'attachment', 55 'base' => 'upload', 56 'id' => 'upload', 57 'post_type' => 'attachment', 58 'is_block_editor' => false, 46 59 ), 47 60 'media-new.php' => array( 48 'action' => 'add', 49 'base' => 'media', 50 'id' => 'media', 61 'action' => 'add', 62 'base' => 'media', 63 'id' => 'media', 64 'is_block_editor' => false, 51 65 ), 52 66 'edit.php?post_type=page' => array( 53 'base' => 'edit', 54 'id' => 'edit-page', 55 'post_type' => 'page', 67 'base' => 'edit', 68 'id' => 'edit-page', 69 'post_type' => 'page', 70 'is_block_editor' => false, 56 71 ), 57 72 'link-manager.php' => array( 58 'base' => 'link-manager', 59 'id' => 'link-manager', 73 'base' => 'link-manager', 74 'id' => 'link-manager', 75 'is_block_editor' => false, 60 76 ), 61 77 'link-add.php' => array( 62 'action' => 'add', 63 'base' => 'link', 64 'id' => 'link', 78 'action' => 'add', 79 'base' => 'link', 80 'id' => 'link', 81 'is_block_editor' => false, 65 82 ), 66 83 'edit-tags.php?taxonomy=link_category' => array( 67 'base' => 'edit-tags', 68 'id' => 'edit-link_category', 69 'taxonomy' => 'link_category', 70 'post_type' => '', 84 'base' => 'edit-tags', 85 'id' => 'edit-link_category', 86 'taxonomy' => 'link_category', 87 'post_type' => '', 88 'is_block_editor' => false, 71 89 ), 72 90 'edit-comments.php' => array( 73 'base' => 'edit-comments', 74 'id' => 'edit-comments', 91 'base' => 'edit-comments', 92 'id' => 'edit-comments', 93 'is_block_editor' => false, 75 94 ), 76 95 'themes.php' => array( 77 'base' => 'themes', 78 'id' => 'themes', 96 'base' => 'themes', 97 'id' => 'themes', 98 'is_block_editor' => false, 79 99 ), 80 100 'widgets.php' => array( 81 'base' => 'widgets', 82 'id' => 'widgets', 101 'base' => 'widgets', 102 'id' => 'widgets', 103 'is_block_editor' => false, 83 104 ), 84 105 'nav-menus.php' => array( 85 'base' => 'nav-menus', 86 'id' => 'nav-menus', 106 'base' => 'nav-menus', 107 'id' => 'nav-menus', 108 'is_block_editor' => false, 87 109 ), 88 110 'plugins.php' => array( 89 'base' => 'plugins', 90 'id' => 'plugins', 111 'base' => 'plugins', 112 'id' => 'plugins', 113 'is_block_editor' => false, 91 114 ), 92 115 'users.php' => array( 93 'base' => 'users', 94 'id' => 'users', 116 'base' => 'users', 117 'id' => 'users', 118 'is_block_editor' => false, 95 119 ), 96 120 'user-new.php' => array( 97 'action' => 'add', 98 'base' => 'user', 99 'id' => 'user', 121 'action' => 'add', 122 'base' => 'user', 123 'id' => 'user', 124 'is_block_editor' => false, 100 125 ), 101 126 'profile.php' => array( 102 'base' => 'profile', 103 'id' => 'profile', 127 'base' => 'profile', 128 'id' => 'profile', 129 'is_block_editor' => false, 104 130 ), 105 131 'tools.php' => array( 106 'base' => 'tools', 107 'id' => 'tools', 132 'base' => 'tools', 133 'id' => 'tools', 134 'is_block_editor' => false, 108 135 ), 109 136 'import.php' => array( 110 'base' => 'import', 111 'id' => 'import', 137 'base' => 'import', 138 'id' => 'import', 139 'is_block_editor' => false, 112 140 ), 113 141 'export.php' => array( 114 'base' => 'export', 115 'id' => 'export', 142 'base' => 'export', 143 'id' => 'export', 144 'is_block_editor' => false, 116 145 ), 117 146 'options-general.php' => array( 118 'base' => 'options-general', 119 'id' => 'options-general', 147 'base' => 'options-general', 148 'id' => 'options-general', 149 'is_block_editor' => false, 120 150 ), 121 151 'options-writing.php' => array( 122 'base' => 'options-writing', 123 'id' => 'options-writing', 152 'base' => 'options-writing', 153 'id' => 'options-writing', 154 'is_block_editor' => false, 124 155 ), 125 156 ); 126 157 … … 178 209 $this->assertFalse( $current_screen->in_admin( 'network' ) ); 179 210 $this->assertFalse( $current_screen->in_admin( 'user' ) ); 180 211 $this->assertFalse( $current_screen->in_admin( 'garbage' ) ); 212 $this->assertSame( $screen->is_block_editor, $current_screen->is_block_editor ); 181 213 182 214 // With convert_to_screen(), the same ID should return the exact $current_screen. 183 215 $this->assertSame( $current_screen, convert_to_screen( $screen->id ), $hook_name ); … … 195 227 $this->assertEquals( $screen->post_type, 'page' ); 196 228 $this->assertEquals( $screen->base, 'post' ); 197 229 $this->assertEquals( $screen->id, 'page' ); 230 $this->assertTrue( $screen->is_block_editor ); 198 231 } 199 232 200 233 function test_post_type_with_special_suffix_as_hookname() { … … 202 235 $screen = convert_to_screen( 'value-add' ); // the -add part is key. 203 236 $this->assertEquals( $screen->post_type, 'value-add' ); 204 237 $this->assertEquals( $screen->base, 'post' ); 205 $this->assert Equals( $screen->id, 'value-add' );238 $this->assertFalse( $screen->is_block_editor ); // Post types do not support `show_in_rest` by default. 206 239 207 240 $screen = convert_to_screen( 'edit-value-add' ); // the -add part is key. 208 241 $this->assertEquals( $screen->post_type, 'value-add' ); 209 242 $this->assertEquals( $screen->base, 'edit' ); 210 243 $this->assertEquals( $screen->id, 'edit-value-add' ); 244 $this->assertFalse( $screen->is_block_editor ); // Post types do not support `show_in_rest` by default. 211 245 } 212 246 213 247 function test_taxonomy_with_special_suffix_as_hookname() { … … 216 250 $this->assertEquals( $screen->taxonomy, 'old-or-new' ); 217 251 $this->assertEquals( $screen->base, 'edit-tags' ); 218 252 $this->assertEquals( $screen->id, 'edit-old-or-new' ); 253 $this->assertFalse( $screen->is_block_editor ); 219 254 } 220 255 221 256 function test_post_type_with_edit_prefix() { … … 224 259 $this->assertEquals( $screen->post_type, 'edit-some-thing' ); 225 260 $this->assertEquals( $screen->base, 'post' ); 226 261 $this->assertEquals( $screen->id, 'edit-some-thing' ); 262 $this->assertFalse( $screen->is_block_editor ); // Post types do not support `show_in_rest` by default. 227 263 228 264 $screen = convert_to_screen( 'edit-edit-some-thing' ); 229 265 $this->assertEquals( $screen->post_type, 'edit-some-thing' ); 230 266 $this->assertEquals( $screen->base, 'edit' ); 231 267 $this->assertEquals( $screen->id, 'edit-edit-some-thing' ); 268 $this->assertFalse( $screen->is_block_editor ); // Post types do not support `show_in_rest` by default. 232 269 } 233 270 234 271 function test_post_type_edit_collisions() { … … 433 470 434 471 $GLOBALS['current_screen'] = $screen; 435 472 } 473 474 /** 475 * Sets up a method for testing is_block_editor for a custom post type. 476 * 477 * @since 5.2.0 478 * 479 * @param string $hook Admin page hook. 480 * @return WP_Screen Screen object. 481 */ 482 public function setup_block_editor_test( $hook = 'post.php' ) { 483 register_post_type( 'type_shows_in_rest', array( 'show_in_rest' => true ) ); 484 485 $GLOBALS['typenow'] = $_GET['post_type'] = $_POST['post_type'] = $_REQUEST['post_type'] = 'type_shows_in_rest'; 486 $GLOBALS['hook_suffix'] = $hook; 487 488 if ( 'post.php' === $hook ) { 489 $post_id = $this->factory->post->create( 490 array( 491 'post_type' => 'type_shows_in_rest', 492 ) 493 ); 494 $_GET['post'] = $post_id; 495 } 496 497 set_current_screen(); 498 499 return get_current_screen(); 500 } 501 502 /** 503 * Data provider for testing is_block_editor. 504 */ 505 public function data_is_block_editor() { 506 return array( 507 array( 508 // Edit post: Post type supports `show_in_rest`, no filters. 509 'hook' => 'post.php', 510 'filter' => array(), 511 'expected' => true, 512 ), 513 array( 514 // Edit post: Support is disabled using post specific filter. 515 'hook' => 'post.php', 516 'filter' => array( 517 'name' => 'use_block_editor_for_post', 518 'function' => '__return_false', 519 ), 520 'expected' => false, 521 ), 522 array( 523 // Edit post: Support is disabled using post type specific filter. 524 'hook' => 'post.php', 525 'filter' => array( 526 'name' => 'use_block_editor_for_post_type', 527 'function' => '__return_false', 528 ), 529 'expected' => false, 530 ), 531 array( 532 // Edit post: Support is disabled using global replace filter. 533 'hook' => 'post.php', 534 'filter' => array( 535 'name' => 'replace_editor', 536 'function' => '__return_true', 537 ), 538 'expected' => false, 539 ), 540 array( 541 // Create post: Post type supports `show_in_rest`, no filters. 542 'hook' => 'post-new.php', 543 'filter' => array(), 544 'expected' => true, 545 ), 546 array( 547 // Create post: Support is disabled using post type specific filter. 548 'hook' => 'post-new.php', 549 'filter' => array( 550 'name' => 'use_block_editor_for_post_type', 551 'function' => '__return_false', 552 ), 553 'expected' => false, 554 ), 555 556 array( 557 // Create post: Support is not immediately disabled using post specific filter. 558 'hook' => 'post-new.php', 559 'filter' => array( 560 'name' => 'use_block_editor_for_post', 561 'function' => '__return_false', 562 ), 563 'expected' => true, 564 ), 565 566 array( 567 // Create post: Support is not immediately disabled using global replace filter. 568 'hook' => 'post-new.php', 569 'filter' => array( 570 'name' => 'replace_editor', 571 'function' => '__return_true', 572 ), 573 'expected' => true, 574 ), 575 ); 576 } 577 578 /** 579 * When editing a post type with `show_in_rest` support, the is_block_editor should indicate support. 580 * 581 * @ticket 46195 582 * @dataProvider data_is_block_editor 583 * 584 * @param string $hook Admin hook. 585 * @param array $filter { 586 * Optional. Filter name and function to hook. 587 * 588 * $name string Filter name to hook a function. 589 * $function string Function name to hook to the filter. 590 * } 591 * @param bool $expected The expected `is_block_editor` value. 592 */ 593 public function test_is_block_editor( $hook, $filter, $expected ) { 594 if ( ! empty( $filter['name'] ) && ! empty( $filter['function'] ) ) { 595 add_filter( $filter['name'], $filter['function'] ); 596 } 597 598 $screen = $this->setup_block_editor_test( $hook ); 599 600 $this->assertSame( 'post', $screen->base ); 601 $this->assertSame( 'type_shows_in_rest', $screen->post_type ); 602 603 if ( 'post.php' === $hook ) { 604 $this->assertEmpty( $screen->action ); 605 } else { 606 $this->assertSame( 'add', $screen->action ); 607 } 608 609 $this->assertSame( $expected, $screen->is_block_editor ); 610 } 436 611 }