From 921a96e315c3a9ae2ce91d69d5eac5c80088933d Mon Sep 17 00:00:00 2001 From: Joost de Valk Date: Thu, 5 Feb 2026 12:06:35 +0100 Subject: [PATCH 1/2] 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 --- admin/admin.php | 2 +- admin/comment-parent.php | 27 ++++++++++------------- inc/clean-emails.php | 8 +++---- inc/hacks.php | 2 +- inc/notifications.php | 13 +++++------ inc/progress-planner/comment-policy.php | 6 +---- inc/progress-planner/comment-redirect.php | 6 +---- 7 files changed, 24 insertions(+), 40 deletions(-) diff --git a/admin/admin.php b/admin/admin.php index 8af84b8..4ab2d27 100644 --- a/admin/admin.php +++ b/admin/admin.php @@ -401,7 +401,7 @@ public function options_validate( array $input ): array { * @return bool */ private function sanitize_bool( $value ): bool { - return ( $value || ! empty( $value ) ); + return (bool) $value; } /** diff --git a/admin/comment-parent.php b/admin/comment-parent.php index cafa759..b8b9f62 100644 --- a/admin/comment-parent.php +++ b/admin/comment-parent.php @@ -33,18 +33,16 @@ public function comment_parent_box( $comment ) { // phpcs:ignore Generic.CodeAna * @return void */ public function load_comment_parent_box() { - if ( \function_exists( 'add_meta_box' ) ) { - \add_meta_box( - 'comment_parent', - \esc_html__( 'Comment Parent', 'yoast-comment-hacks' ), - [ - $this, - 'comment_parent_box', - ], - 'comment', - 'normal' - ); - } + \add_meta_box( + 'comment_parent', + \esc_html__( 'Comment Parent', 'yoast-comment-hacks' ), + [ + $this, + 'comment_parent_box', + ], + 'comment', + 'normal' + ); } /** @@ -65,11 +63,10 @@ public function update_comment_parent() { return; // There might be another reason for a comment to be updated. } - if ( \function_exists( 'wp_doing_ajax' ) && \wp_doing_ajax() ) { + if ( \wp_doing_ajax() ) { \check_ajax_referer( 'replyto-comment', '_ajax_nonce-replyto-comment' ); } - - if ( ! \function_exists( 'wp_doing_ajax' ) || ! \wp_doing_ajax() ) { + else { \check_admin_referer( 'update-comment_' . $comment_id ); } diff --git a/inc/clean-emails.php b/inc/clean-emails.php index ae6b996..655f4b1 100644 --- a/inc/clean-emails.php +++ b/inc/clean-emails.php @@ -299,14 +299,12 @@ private function comment_notification_actions(): void { * @return void */ private function comment_action_links( array $actions ): void { - $links = ''; + $links = []; foreach ( $actions as $action => $label ) { - $links .= $this->comment_action_link( $label, $action ) . ' | '; + $links[] = $this->comment_action_link( $label, $action ); } - $links = \rtrim( $links, '| ' ); - - $this->message .= $links; + $this->message .= \implode( ' | ', $links ); } /** diff --git a/inc/hacks.php b/inc/hacks.php index 58d3514..1703b9b 100644 --- a/inc/hacks.php +++ b/inc/hacks.php @@ -160,7 +160,7 @@ public function modify_comment_edit_link_block( $block_content, $block ) { public function get_remove_comment_url_link( $comment_id ) { $comment = \get_comment( $comment_id ); - if ( isset( $comment ) && $comment instanceof WP_Comment && ! empty( $comment->comment_author_url ) ) { + if ( $comment instanceof WP_Comment && ! empty( $comment->comment_author_url ) ) { return \sprintf( '%s', \esc_attr( (string) $comment_id ), diff --git a/inc/notifications.php b/inc/notifications.php index 071a2e5..e724765 100644 --- a/inc/notifications.php +++ b/inc/notifications.php @@ -50,18 +50,15 @@ public function filter_notification_recipients( $recipients, $comment_id ): arra public function filter_notification_headers( $message_headers, $comment_id ): string { $comment = \get_comment( $comment_id ); - if ( $comment->comment_author !== '' && $comment->comment_author_email !== '' ) { - $name = \esc_html( $comment->comment_author ); - $message_headers .= "\nReply-To: $name <$comment->comment_author_email>\n"; - + if ( $comment->comment_author_email === '' ) { return $message_headers; } - if ( $comment->comment_author_email !== '' ) { - $message_headers .= "\nReply-To: $comment->comment_author_email <$comment->comment_author_email>\n"; + $name = $comment->comment_author !== '' + ? \esc_html( $comment->comment_author ) + : $comment->comment_author_email; - return $message_headers; - } + $message_headers .= "\nReply-To: $name <$comment->comment_author_email>\n"; return $message_headers; } diff --git a/inc/progress-planner/comment-policy.php b/inc/progress-planner/comment-policy.php index bffd795..e17d739 100644 --- a/inc/progress-planner/comment-policy.php +++ b/inc/progress-planner/comment-policy.php @@ -78,11 +78,7 @@ public function get_description() { * @return bool */ public function should_add_task() { - if ( ! (int) $this->options['comment_policy_page'] || ! (int) $this->options['comment_policy'] ) { - return true; - } - - return false; + return ! (int) $this->options['comment_policy_page'] || ! (int) $this->options['comment_policy']; } /** diff --git a/inc/progress-planner/comment-redirect.php b/inc/progress-planner/comment-redirect.php index d00ff01..fdc2ef8 100644 --- a/inc/progress-planner/comment-redirect.php +++ b/inc/progress-planner/comment-redirect.php @@ -77,11 +77,7 @@ public function get_description() { * @return bool */ public function should_add_task() { - if ( ! $this->options['redirect_page'] ) { - return true; - } - - return false; + return ! $this->options['redirect_page']; } /** From e07cce530afc3bc01fad8c8ac1f6b97a7c626e7d Mon Sep 17 00:00:00 2001 From: Joost de Valk Date: Thu, 5 Feb 2026 12:11:21 +0100 Subject: [PATCH 2/2] style: fix inline ternary to satisfy PHPCS Co-Authored-By: Claude Opus 4.5 --- inc/notifications.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/inc/notifications.php b/inc/notifications.php index e724765..8999127 100644 --- a/inc/notifications.php +++ b/inc/notifications.php @@ -54,9 +54,7 @@ public function filter_notification_headers( $message_headers, $comment_id ): st return $message_headers; } - $name = $comment->comment_author !== '' - ? \esc_html( $comment->comment_author ) - : $comment->comment_author_email; + $name = ( $comment->comment_author !== '' ) ? \esc_html( $comment->comment_author ) : $comment->comment_author_email; $message_headers .= "\nReply-To: $name <$comment->comment_author_email>\n";