Skip to content

Commit a9ff60e

Browse files
SteveJonesDevclaude
andcommitted
Address code review feedback
- Detect the [edac_simplified_summary] shortcode in block theme template content, not just the block, when suppressing automatic insertion. - Gate explicit shortcode post_id values on is_post_publicly_viewable() so summaries of draft/private posts cannot be exposed. - Output block wrapper attributes in the render callback so the supports.spacing.margin styles reach the front end, with a guard for direct calls outside an active block render. - Make the render callback's $block parameter optional with an instanceof check. - Use lint-approved lowercase currentcolor in the editor stylesheet. - Reset the block template global in tearDown and add regression tests for the template shortcode and non-public post cases. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent 6422d2b commit a9ff60e

7 files changed

Lines changed: 80 additions & 12 deletions

File tree

includes/classes/Blocks/SimplifiedSummaryBlock.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,20 +106,33 @@ public function register() {
106106
*
107107
* @since 1.xx.x
108108
*
109-
* @param array $attributes The block attributes.
110-
* @param string $content The block content.
111-
* @param WP_Block $block The block instance.
109+
* @param array $attributes The block attributes.
110+
* @param string $content The block content.
111+
* @param WP_Block|null $block The block instance.
112112
* @return string
113113
*/
114-
public function render( $attributes, $content, $block ) {
115-
$post_id = isset( $block->context['postId'] )
114+
public function render( $attributes, $content, $block = null ) {
115+
$post_id = ( $block instanceof WP_Block && isset( $block->context['postId'] ) )
116116
? (int) $block->context['postId']
117117
: (int) get_the_ID();
118118

119119
if ( ! $post_id ) {
120120
return '';
121121
}
122122

123-
return ( new Simplified_Summary() )->simplified_summary_markup( $post_id );
123+
$markup = ( new Simplified_Summary() )->simplified_summary_markup( $post_id );
124+
125+
if ( ! $markup ) {
126+
return '';
127+
}
128+
129+
// The wrapper carries the block supports output (margin classes/styles).
130+
// get_block_wrapper_attributes() requires an active block render; guard
131+
// against direct calls to this callback outside of one.
132+
$wrapper_attributes = null !== \WP_Block_Supports::$block_to_render
133+
? get_block_wrapper_attributes()
134+
: 'class="wp-block-edac-simplified-summary"';
135+
136+
return sprintf( '<div %s>%s</div>', $wrapper_attributes, $markup );
124137
}
125138
}

includes/classes/Shortcodes/SimplifiedSummaryShortcode.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public function init_hooks() {
4343
*
4444
* Renders regardless of the edac_simplified_summary_prompt option because
4545
* manual placement is a deliberate act, matching edac_get_simplified_summary().
46+
* An explicit post_id must reference a publicly viewable post so the
47+
* shortcode cannot expose summaries of draft or private posts.
4648
*
4749
* @since 1.xx.x
4850
*
@@ -56,9 +58,13 @@ public function render( $atts ) {
5658
self::SHORTCODE
5759
);
5860

59-
$post_id = absint( $atts['post_id'] )
60-
? absint( $atts['post_id'] )
61-
: (int) get_the_ID();
61+
$explicit_post_id = absint( $atts['post_id'] );
62+
63+
if ( $explicit_post_id && ! is_post_publicly_viewable( $explicit_post_id ) ) {
64+
return '';
65+
}
66+
67+
$post_id = $explicit_post_id ? $explicit_post_id : (int) get_the_ID();
6268

6369
if ( ! $post_id ) {
6470
return '';

includes/classes/class-simplified-summary.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,10 @@ public function is_manually_placed( $post = null ): bool {
8989
if (
9090
! $manually_placed &&
9191
! empty( $_wp_current_template_content ) &&
92-
has_block( 'edac/simplified-summary', $_wp_current_template_content )
92+
(
93+
has_block( 'edac/simplified-summary', $_wp_current_template_content ) ||
94+
has_shortcode( $_wp_current_template_content, 'edac_simplified_summary' )
95+
)
9396
) {
9497
$manually_placed = true;
9598
}

src/simplifiedSummaryBlock/sass/simplified-summary-block.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Editor-only styles for the simplified summary block placeholder.
22
.edac-simplified-summary {
33
.edac-simplified-summary-block__placeholder {
4-
border: 1px dashed currentColor;
4+
border: 1px dashed currentcolor;
55
border-radius: 2px;
66
opacity: 0.62;
77
padding: 0.75em 1em;

tests/phpunit/includes/classes/Blocks/SimplifiedSummaryBlockTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ public function test_block_renders_through_do_blocks() {
130130

131131
$output = do_blocks( '<!-- wp:edac/simplified-summary /-->' );
132132
$this->assertStringContainsString( 'Do blocks summary.', $output );
133+
$this->assertStringContainsString( 'wp-block-edac-simplified-summary', $output );
133134
}
134135

135136
/**

tests/phpunit/includes/classes/Shortcodes/SimplifiedSummaryShortcodeTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,22 @@ public function test_shortcode_returns_empty_string_without_summary() {
6868
$this->assertSame( '', $output );
6969
}
7070

71+
/**
72+
* Tests that the shortcode does not expose summaries of non-public posts.
73+
*
74+
* @return void
75+
*/
76+
public function test_shortcode_does_not_render_non_public_posts() {
77+
$draft_id = self::factory()->post->create( [ 'post_status' => 'draft' ] );
78+
update_post_meta( $draft_id, '_edac_simplified_summary', 'Draft summary.' );
79+
80+
$private_id = self::factory()->post->create( [ 'post_status' => 'private' ] );
81+
update_post_meta( $private_id, '_edac_simplified_summary', 'Private summary.' );
82+
83+
$this->assertSame( '', do_shortcode( '[edac_simplified_summary post_id="' . $draft_id . '"]' ) );
84+
$this->assertSame( '', do_shortcode( '[edac_simplified_summary post_id="' . $private_id . '"]' ) );
85+
}
86+
7187
/**
7288
* Tests that the shortcode returns an empty string outside the loop with no post_id.
7389
*

tests/phpunit/includes/classes/SimplifiedSummaryTest.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,19 @@ public function setUp(): void {
3434
$this->simplified_summary = new Simplified_Summary();
3535
}
3636

37+
/**
38+
* Clean up the test fixture.
39+
*
40+
* The core test framework does not reset the block template global.
41+
*
42+
* @return void
43+
*/
44+
public function tearDown(): void {
45+
global $_wp_current_template_content;
46+
$_wp_current_template_content = null;
47+
parent::tearDown();
48+
}
49+
3750
/**
3851
* Tests output of simplified_summary_markup with a summary.
3952
*
@@ -136,7 +149,23 @@ public function test_output_simplified_summary_suppressed_when_block_in_template
136149

137150
$output = $this->simplified_summary->output_simplified_summary( 'Post content.' );
138151

139-
$_wp_current_template_content = null;
152+
$this->assertStringNotContainsString( 'edac-simplified-summary', $output );
153+
}
154+
155+
/**
156+
* Tests that auto-insertion is suppressed when the shortcode is in the current block theme template.
157+
*
158+
* Covers a core Shortcode block containing [edac_simplified_summary] in an FSE template.
159+
*
160+
* @return void
161+
*/
162+
public function test_output_simplified_summary_suppressed_when_shortcode_in_template() {
163+
$this->create_post_in_loop();
164+
165+
global $_wp_current_template_content;
166+
$_wp_current_template_content = '<!-- wp:shortcode -->[edac_simplified_summary]<!-- /wp:shortcode --><!-- wp:post-content /-->';
167+
168+
$output = $this->simplified_summary->output_simplified_summary( 'Post content.' );
140169

141170
$this->assertStringNotContainsString( 'edac-simplified-summary', $output );
142171
}

0 commit comments

Comments
 (0)