Skip to content

Commit cba6228

Browse files
obenlandpfefferle
andauthored
Blocks: Avoid PHP warning in Reply block (#1076)
* Blocks: Avoid PHP warning in Reply block * Fix phpcs I wish Cursor AI knew WP coding standards --------- Co-authored-by: Matthias Pfefferle <[email protected]>
1 parent 14dc525 commit cba6228

File tree

4 files changed

+69
-11
lines changed

4 files changed

+69
-11
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2020
* Improve Interactions moderation
2121
* Compatibility with Akismet
2222

23+
### Fixed
24+
25+
* Empty `url` attributes in the Reply block no longer cause PHP warnings
26+
2327
## [4.4.0] - 2024-12-09
2428

2529
### Added

includes/class-blocks.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -305,23 +305,25 @@ function ( $follower ) {
305305
* @return string The HTML to render.
306306
*/
307307
public static function render_reply_block( $attrs ) {
308+
$html = '';
309+
310+
if ( ! empty( $attrs['url'] ) ) {
311+
$html = sprintf(
312+
'<p><a title="%2$s" aria-label="%2$s" href="%1$s" class="u-in-reply-to" target="_blank">%3$s</a></p>',
313+
esc_url( $attrs['url'] ),
314+
esc_attr__( 'This post is a response to the referenced content.', 'activitypub' ),
315+
// translators: %s is the URL of the post being replied to.
316+
sprintf( __( '&#8620;%s', 'activitypub' ), \str_replace( array( 'https://', 'http://' ), '', esc_url( $attrs['url'] ) ) )
317+
);
318+
}
319+
308320
/**
309321
* Filter the reply block.
310322
*
311323
* @param string $html The HTML to render.
312324
* @param array $attrs The block attributes.
313325
*/
314-
return apply_filters(
315-
'activitypub_reply_block',
316-
sprintf(
317-
'<p><a title="%2$s" aria-label="%2$s" href="%1$s" class="u-in-reply-to" target="_blank">%3$s</a></p>',
318-
esc_url( $attrs['url'] ),
319-
esc_attr__( 'This post is a response to the referenced content.', 'activitypub' ),
320-
// translators: %s is the URL of the post being replied to.
321-
sprintf( __( '&#8620;%s', 'activitypub' ), \str_replace( array( 'https://', 'http://' ), '', $attrs['url'] ) )
322-
),
323-
$attrs
324-
);
326+
return apply_filters( 'activitypub_reply_block', $html, $attrs );
325327
}
326328

327329
/**

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ For reasons of data protection, it is not possible to see the followers of other
141141
* Improved: Email templates for Likes and Reposts
142142
* Improved: Interactions moderation
143143
* Improved: Compatibility with Akismet
144+
* Fixed: Empty `url` attributes in the Reply block no longer cause PHP warnings
144145

145146
= 4.4.0 =
146147

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* Test file for Blocks class.
4+
*
5+
* @package Activitypub
6+
*/
7+
8+
namespace Activitypub\Tests;
9+
10+
/**
11+
* Test class for Blocks.
12+
*
13+
* @coversDefaultClass \Activitypub\Blocks
14+
*/
15+
class Test_Blocks extends \WP_UnitTestCase {
16+
17+
/**
18+
* Test the render_reply_block() method with a valid URL attribute.
19+
*
20+
* @covers ::render_reply_block
21+
*/
22+
public function test_render_reply_block_with_valid_url() {
23+
$attrs = array( 'url' => 'https://example.com/post' );
24+
$output = \Activitypub\Blocks::render_reply_block( $attrs );
25+
$this->assertStringContainsString( 'u-in-reply-to', $output );
26+
$this->assertStringContainsString( 'https://example.com/post', $output );
27+
$this->assertStringContainsString( 'example.com/post', $output );
28+
}
29+
30+
/**
31+
* Test the render_reply_block() method with a missing URL attribute.
32+
*
33+
* @covers ::render_reply_block
34+
*/
35+
public function test_render_reply_block_with_missing_url() {
36+
$attrs = array();
37+
$output = \Activitypub\Blocks::render_reply_block( $attrs );
38+
$this->assertEmpty( $output );
39+
}
40+
41+
/**
42+
* Test the render_reply_block() method with an empty URL attribute.
43+
*
44+
* @covers ::render_reply_block
45+
*/
46+
public function test_render_reply_block_with_empty_url() {
47+
$attrs = array( 'url' => '' );
48+
$output = \Activitypub\Blocks::render_reply_block( $attrs );
49+
$this->assertEmpty( $output );
50+
}
51+
}

0 commit comments

Comments
 (0)