Skip to content

Commit 7577faf

Browse files
jdevalkclaude
andauthored
Simplify redundant code patterns across codebase (#178)
* refactor: simplify redundant code patterns - sanitize_bool: replace redundant logic with (bool) cast - comment-parent: remove function_exists checks for ancient WP functions - notifications: deduplicate filter_notification_headers branches - clean-emails: use implode instead of build-then-rtrim for action links - progress-planner: simplify should_add_task to direct boolean returns - hacks: remove redundant isset before instanceof check Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * style: fix inline ternary to satisfy PHPCS Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
1 parent f15d955 commit 7577faf

7 files changed

Lines changed: 22 additions & 40 deletions

File tree

admin/admin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ public function options_validate( array $input ): array {
406406
* @return bool
407407
*/
408408
private function sanitize_bool( $value ): bool {
409-
return ( $value || ! empty( $value ) );
409+
return (bool) $value;
410410
}
411411

412412
/**

admin/comment-parent.php

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,16 @@ public function comment_parent_box( $comment ) { // phpcs:ignore Generic.CodeAna
3838
* @return void
3939
*/
4040
public function load_comment_parent_box() {
41-
if ( \function_exists( 'add_meta_box' ) ) {
42-
\add_meta_box(
43-
'comment_parent',
44-
\esc_html__( 'Comment Parent', 'yoast-comment-hacks' ),
45-
[
46-
$this,
47-
'comment_parent_box',
48-
],
49-
'comment',
50-
'normal'
51-
);
52-
}
41+
\add_meta_box(
42+
'comment_parent',
43+
\esc_html__( 'Comment Parent', 'yoast-comment-hacks' ),
44+
[
45+
$this,
46+
'comment_parent_box',
47+
],
48+
'comment',
49+
'normal'
50+
);
5351
}
5452

5553
/**
@@ -70,11 +68,10 @@ public function update_comment_parent() {
7068
return; // There might be another reason for a comment to be updated.
7169
}
7270

73-
if ( \function_exists( 'wp_doing_ajax' ) && \wp_doing_ajax() ) {
71+
if ( \wp_doing_ajax() ) {
7472
\check_ajax_referer( 'replyto-comment', '_ajax_nonce-replyto-comment' );
7573
}
76-
77-
if ( ! \function_exists( 'wp_doing_ajax' ) || ! \wp_doing_ajax() ) {
74+
else {
7875
\check_admin_referer( 'update-comment_' . $comment_id );
7976
}
8077

inc/clean-emails.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -304,14 +304,12 @@ private function comment_notification_actions(): void {
304304
* @return void
305305
*/
306306
private function comment_action_links( array $actions ): void {
307-
$links = '';
307+
$links = [];
308308
foreach ( $actions as $action => $label ) {
309-
$links .= $this->comment_action_link( $label, $action ) . ' | ';
309+
$links[] = $this->comment_action_link( $label, $action );
310310
}
311311

312-
$links = \rtrim( $links, '| ' );
313-
314-
$this->message .= $links;
312+
$this->message .= \implode( ' | ', $links );
315313
}
316314

317315
/**

inc/hacks.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public function modify_comment_edit_link_block( $block_content, $block ) {
165165
public function get_remove_comment_url_link( $comment_id ) {
166166
$comment = \get_comment( $comment_id );
167167

168-
if ( isset( $comment ) && $comment instanceof WP_Comment && ! empty( $comment->comment_author_url ) ) {
168+
if ( $comment instanceof WP_Comment && ! empty( $comment->comment_author_url ) ) {
169169
return \sprintf(
170170
'<a href="#" class="comment-remove-url" data-comment-id="%d" aria-label="%s">%s</a>',
171171
\esc_attr( (string) $comment_id ),

inc/notifications.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,13 @@ public function filter_notification_recipients( $recipients, $comment_id ): arra
5555
public function filter_notification_headers( $message_headers, $comment_id ): string {
5656
$comment = \get_comment( $comment_id );
5757

58-
if ( $comment->comment_author !== '' && $comment->comment_author_email !== '' ) {
59-
$name = \esc_html( $comment->comment_author );
60-
$message_headers .= "\nReply-To: $name <$comment->comment_author_email>\n";
61-
58+
if ( $comment->comment_author_email === '' ) {
6259
return $message_headers;
6360
}
6461

65-
if ( $comment->comment_author_email !== '' ) {
66-
$message_headers .= "\nReply-To: $comment->comment_author_email <$comment->comment_author_email>\n";
62+
$name = ( $comment->comment_author !== '' ) ? \esc_html( $comment->comment_author ) : $comment->comment_author_email;
6763

68-
return $message_headers;
69-
}
64+
$message_headers .= "\nReply-To: $name <$comment->comment_author_email>\n";
7065

7166
return $message_headers;
7267
}

inc/progress-planner/comment-policy.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,7 @@ public function get_description() {
8383
* @return bool
8484
*/
8585
public function should_add_task() {
86-
if ( ! (int) $this->options['comment_policy_page'] || ! (int) $this->options['comment_policy'] ) {
87-
return true;
88-
}
89-
90-
return false;
86+
return ! (int) $this->options['comment_policy_page'] || ! (int) $this->options['comment_policy'];
9187
}
9288

9389
/**

inc/progress-planner/comment-redirect.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,7 @@ public function get_description() {
8282
* @return bool
8383
*/
8484
public function should_add_task() {
85-
if ( ! $this->options['redirect_page'] ) {
86-
return true;
87-
}
88-
89-
return false;
85+
return ! $this->options['redirect_page'];
9086
}
9187

9288
/**

0 commit comments

Comments
 (0)