Skip to content

Commit 53c8e61

Browse files
authored
Merge pull request #4388 from ampproject/fix/leave-reply-heading-comment-form
Prevent failed attempts to override the reply-title heading in comment form
2 parents c077bf0 + 0705f38 commit 53c8e61

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

includes/class-amp-theme-support.php

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,7 +1095,7 @@ static function( $html ) {
10951095
add_action( 'template_redirect', [ __CLASS__, 'start_output_buffering' ], $priority );
10961096

10971097
// Commenting hooks.
1098-
add_filter( 'comment_form_defaults', [ __CLASS__, 'filter_comment_form_defaults' ] );
1098+
add_filter( 'comment_form_defaults', [ __CLASS__, 'filter_comment_form_defaults' ], PHP_INT_MAX );
10991099
add_filter( 'comment_reply_link', [ __CLASS__, 'filter_comment_reply_link' ], 10, 4 );
11001100
add_filter( 'cancel_comment_reply_link', [ __CLASS__, 'filter_cancel_comment_reply_link' ], 10, 3 );
11011101
add_action( 'comment_form', [ __CLASS__, 'amend_comment_form' ], 100 );
@@ -1303,29 +1303,48 @@ public static function get_comment_form_state_id( $post_id ) {
13031303
* @since 0.7
13041304
* @see comment_form()
13051305
*
1306-
* @param array $args Comment form args.
1306+
* @param array $default_args Comment form arg defaults.
13071307
* @return array Filtered comment form args.
13081308
*/
1309-
public static function filter_comment_form_defaults( $args ) {
1310-
$state_id = self::get_comment_form_state_id( get_the_ID() );
1309+
public static function filter_comment_form_defaults( $default_args ) {
1310+
1311+
// Obtain the actual args provided to the comment_form() function since it is not available in the filter.
1312+
$args = [];
1313+
$backtrace = debug_backtrace(); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace -- Due to limitation in WordPress core.
1314+
foreach ( $backtrace as $call ) {
1315+
if ( 'comment_form' === $call['function'] ) {
1316+
$args = isset( $call['args'][0] ) ? $call['args'][0] : [];
1317+
break;
1318+
}
1319+
}
1320+
1321+
// Abort if the comment_form() was called with arguments which we cannot override the defaults for.
1322+
// @todo This and the debug_backtrace() call above would be unnecessary if WordPress had a comment_form_args filter.
1323+
$overridden_keys = [ 'cancel_reply_before', 'title_reply', 'title_reply_before', 'title_reply_to' ];
1324+
foreach ( $overridden_keys as $key ) {
1325+
if ( array_key_exists( $key, $args ) && array_key_exists( $key, $default_args ) && $default_args[ $key ] !== $args[ $key ] ) {
1326+
return $default_args;
1327+
}
1328+
}
13111329

1330+
$state_id = self::get_comment_form_state_id( get_the_ID() );
13121331
$text_binding = sprintf(
13131332
'%s.replyToName ? %s : %s',
13141333
$state_id,
13151334
str_replace(
13161335
'%s',
13171336
sprintf( '" + %s.replyToName + "', $state_id ),
1318-
wp_json_encode( $args['title_reply_to'], JSON_UNESCAPED_UNICODE )
1337+
wp_json_encode( $default_args['title_reply_to'], JSON_UNESCAPED_UNICODE )
13191338
),
1320-
wp_json_encode( $args['title_reply'], JSON_UNESCAPED_UNICODE )
1339+
wp_json_encode( $default_args['title_reply'], JSON_UNESCAPED_UNICODE )
13211340
);
13221341

1323-
$args['title_reply_before'] .= sprintf(
1342+
$default_args['title_reply_before'] .= sprintf(
13241343
'<span [text]="%s">',
13251344
esc_attr( $text_binding )
13261345
);
1327-
$args['cancel_reply_before'] = '</span>' . $args['cancel_reply_before'];
1328-
return $args;
1346+
$default_args['cancel_reply_before'] = '</span>' . $default_args['cancel_reply_before'];
1347+
return $default_args;
13291348
}
13301349

13311350
/**

tests/php/test-class-amp-theme-support.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1068,7 +1068,7 @@ public function test_add_hooks() {
10681068
$priority = defined( 'PHP_INT_MIN' ) ? PHP_INT_MIN : ~PHP_INT_MAX; // phpcs:ignore PHPCompatibility.Constants.NewConstants.php_int_minFound
10691069
$this->assertEquals( $priority, has_action( 'template_redirect', [ self::TESTED_CLASS, 'start_output_buffering' ] ) );
10701070

1071-
$this->assertEquals( 10, has_filter( 'comment_form_defaults', [ self::TESTED_CLASS, 'filter_comment_form_defaults' ] ) );
1071+
$this->assertEquals( PHP_INT_MAX, has_filter( 'comment_form_defaults', [ self::TESTED_CLASS, 'filter_comment_form_defaults' ] ) );
10721072
$this->assertEquals( 10, has_filter( 'comment_reply_link', [ self::TESTED_CLASS, 'filter_comment_reply_link' ] ) );
10731073
$this->assertEquals( 10, has_filter( 'cancel_comment_reply_link', [ self::TESTED_CLASS, 'filter_cancel_comment_reply_link' ] ) );
10741074
$this->assertEquals( 100, has_action( 'comment_form', [ self::TESTED_CLASS, 'amend_comment_form' ] ) );

0 commit comments

Comments
 (0)