Editor: Universalize functions for checking block editor status.

`use_block_editor_for_post_type` and `use_block_editor_for_post` can be very useful in more contexts than wp-admin, especially when a site is in transition. For example, you may want to do things on init that are different.

Neither function depends on other functions that are available only in wp-admin (other than use_block_editor_for_post() relying on use_block_editor_for_post_type() and an admin-referrer check that's historically gated by a query variable and now also gated by is_admin), therefore  moving them to wp-includes seems both feasible and beneficial

Props ethitter, jorbin.
Fixes #51819.



git-svn-id: https://develop.svn.wordpress.org/trunk@53559 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Aaron Jorbin
2022-06-23 18:46:18 +00:00
parent 9f27d13efb
commit 828d518988
4 changed files with 111 additions and 103 deletions
+39
View File
@@ -1844,4 +1844,43 @@ class Tests_Post extends WP_UnitTestCase {
unstick_post( 3 );
$this->assertSameSets( array( 1, 2, 2 ), get_option( 'sticky_posts' ) );
}
/**
* Check if post supports block editor.
*
* @ticket 51819
* @covers ::use_block_editor_for_post
*/
public function test_use_block_editor_for_post() {
$this->assertFalse( use_block_editor_for_post( -1 ) );
$bogus_post_id = $this->factory()->post->create(
array(
'post_type' => 'bogus',
)
);
$this->assertFalse( use_block_editor_for_post( $bogus_post_id ) );
register_post_type(
'restless',
array(
'show_in_rest' => false,
)
);
$restless_post_id = $this->factory()->post->create(
array(
'post_type' => 'restless',
)
);
$this->assertFalse( use_block_editor_for_post( $restless_post_id ) );
$generic_post_id = $this->factory()->post->create();
add_filter( 'use_block_editor_for_post', '__return_false' );
$this->assertFalse( use_block_editor_for_post( $generic_post_id ) );
remove_filter( 'use_block_editor_for_post', '__return_false' );
add_filter( 'use_block_editor_for_post', '__return_true' );
$this->assertTrue( use_block_editor_for_post( $restless_post_id ) );
remove_filter( 'use_block_editor_for_post', '__return_true' );
}
}