Skip to content

Commit

Permalink
Merge pull request #7736 from Automattic/fix/no-sensei-blocks-for-posts
Browse files Browse the repository at this point in the history
Initialize Sensei blocks for new posts
  • Loading branch information
donnapep authored Jan 15, 2025
2 parents 0aea63c + e3d50c1 commit fbcb785
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
4 changes: 4 additions & 0 deletions changelog/fix-no-sensei-blocks-for-posts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Initialize Sensei blocks for posts
8 changes: 6 additions & 2 deletions includes/blocks/class-sensei-blocks-initializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,12 @@ private function get_admin_page_post_type(): ?string {
return $post_type ? $post_type : null;
}

if ( 'post-new.php' === $pagenow && isset( $_GET['post_type'] ) && post_type_exists( $_GET['post_type'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput -- Only reading the post type.
return $_GET['post_type']; // phpcs:ignore WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput -- Already validated.
if ( 'post-new.php' === $pagenow ) {
if ( isset( $_GET['post_type'] ) && post_type_exists( $_GET['post_type'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput -- Only reading the post type.
return $_GET['post_type']; // phpcs:ignore WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput -- Already validated.
} else {
return 'post';
}
}

return null;
Expand Down
19 changes: 18 additions & 1 deletion tests/unit-tests/blocks/test-class-sensei-blocks-initializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,24 @@ public function testMaybeInitializeBlocks_WhenInAdminOnGutenbergEditSiteScreen_I
$initializer_mock->maybe_initialize_blocks();
}

public function testMaybeInitializeBlocks_WhenInAdminOnNewPostScreenAndHasCorrectPostType_InitializesBlocks() {
public function testMaybeInitializeBlocks_WhenInAdminOnNewPostScreen_InitializesBlocks() {
/* Arrange */
$initializer_mock = $this->getMockForAbstractClass( Sensei_Blocks_Initializer::class );

set_current_screen( 'post' );

global $pagenow;
$pagenow = 'post-new.php';

/* Assert */
$initializer_mock->expects( $this->once() )
->method( 'initialize_blocks' );

/* Act */
$initializer_mock->maybe_initialize_blocks();
}

public function testMaybeInitializeBlocks_WhenInAdminOnNewCourseScreenAndHasCorrectPostType_InitializesBlocks() {
/* Arrange */
$initializer_mock = $this->getMockForAbstractClass( Sensei_Blocks_Initializer::class, [ [ 'course' ] ] );

Expand Down

0 comments on commit fbcb785

Please sign in to comment.