From fb4df11d41948d69262f70c500763fdbe3503c30 Mon Sep 17 00:00:00 2001 From: Joost de Valk Date: Mon, 1 Jan 2024 21:39:40 +0100 Subject: [PATCH] Fixes #108 and fixes #111 --- admin/admin.php | 20 +++++++++----- admin/views/config-page.php | 18 +++++++++++++ composer.json | 4 +-- composer.lock | 52 +++++++++++++++++++++++++++---------- inc/email-links.php | 2 +- inc/hacks.php | 41 ++++++++++++++++------------- readme.txt | 9 +++++-- yoast-comment-hacks.php | 4 +-- 8 files changed, 104 insertions(+), 46 deletions(-) diff --git a/admin/admin.php b/admin/admin.php index 3a002df..24c6ee3 100644 --- a/admin/admin.php +++ b/admin/admin.php @@ -5,7 +5,7 @@ use WP_Comment; use WP_Post; use JoostBlog\WP\Comment\Inc\Hacks; -use Yoast_I18n_WordPressOrg_v3; +use JoostBlog\WP\Comment\Inc\Comment_Parent; /** * Admin handling class. @@ -52,7 +52,7 @@ public function __construct() { // Filter the comment notification recipients. \add_action( 'add_meta_boxes', [ $this, 'register_meta_boxes' ] ); - \add_action( 'save_post', [ $this, 'save_reroute_comment_emails' ] ); + \add_action( 'pre_post_update', [ $this, 'save_reroute_comment_emails' ] ); \add_filter( 'comment_row_actions', [ $this, 'forward_to_support_action_link' ], 10, 2 ); \add_action( 'admin_head', [ $this, 'forward_comment' ] ); @@ -177,6 +177,7 @@ public function register_meta_boxes(): void { * @param WP_Post $post Current post object. */ public function meta_box_callback( $post ): void { + echo ''; echo '
'; /** @@ -224,7 +225,7 @@ public function meta_box_callback( $post ): void { } /** - * Register the text domain and the options array along with the validation function. + * Register the options array along with the validation function. */ public function init(): void { // Register our option array. @@ -269,13 +270,17 @@ public function enqueue(): void { /** * Saves the comment email recipients post meta. + * + * @param int $post_id The post ID. */ - public function save_reroute_comment_emails(): void { + public function save_reroute_comment_emails( $post_id ): void { - $post_id = \filter_input( \INPUT_POST, 'ID', \FILTER_VALIDATE_INT ); - $recipient_id = \filter_input( \INPUT_POST, 'comment_notification_recipient', \FILTER_VALIDATE_INT ); + if ( ! isset( $_POST['comment_notification_recipient'] ) || ! \wp_verify_nonce( \filter_input( \INPUT_POST, 'comment_notification_recipient_nonce' ), 'comment_notification_recipient_nonce' ) ) { + return; + } - if ( $recipient_id && $post_id ) { + $recipient_id = (int) \sanitize_key( \wp_unslash( $_POST['comment_notification_recipient'] ) ); + if ( $recipient_id > 0 ) { \update_post_meta( $post_id, self::NOTIFICATION_RECIPIENT_KEY, $recipient_id ); } } @@ -303,6 +308,7 @@ public function options_validate( array $input ): array { break; case 'comment_policy': case 'clean_emails': + case 'disable_email_all_commenters': $input[ $key ] = $this->sanitize_bool( $value ); break; case 'email_subject': diff --git a/admin/views/config-page.php b/admin/views/config-page.php index f3a0db2..1e4418f 100644 --- a/admin/views/config-page.php +++ b/admin/views/config-page.php @@ -22,6 +22,8 @@ href="#top#clean-emails"> +
@@ -287,6 +289,22 @@ + +
+

+ +

+ + + + + +
+ + options['disable_email_all_commenters'] ); ?> /> +
+
diff --git a/composer.json b/composer.json index ed8c09d..55d5796 100644 --- a/composer.json +++ b/composer.json @@ -35,8 +35,8 @@ "php": "7.4" }, "allow-plugins": { - "dealerdirect/phpcodesniffer-composer-installer": true, - "composer/installers": true + "composer/installers": true, + "dealerdirect/phpcodesniffer-composer-installer": true } }, "minimum-stability": "dev", diff --git a/composer.lock b/composer.lock index bb31edb..4f7c08d 100644 --- a/composer.lock +++ b/composer.lock @@ -2489,16 +2489,16 @@ }, { "name": "squizlabs/php_codesniffer", - "version": "3.7.1", + "version": "3.8.0", "source": { "type": "git", - "url": "https://github.com/squizlabs/PHP_CodeSniffer.git", - "reference": "1359e176e9307e906dc3d890bcc9603ff6d90619" + "url": "https://github.com/PHPCSStandards/PHP_CodeSniffer.git", + "reference": "5805f7a4e4958dbb5e944ef1e6edae0a303765e7" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/1359e176e9307e906dc3d890bcc9603ff6d90619", - "reference": "1359e176e9307e906dc3d890bcc9603ff6d90619", + "url": "https://api.github.com/repos/PHPCSStandards/PHP_CodeSniffer/zipball/5805f7a4e4958dbb5e944ef1e6edae0a303765e7", + "reference": "5805f7a4e4958dbb5e944ef1e6edae0a303765e7", "shasum": "" }, "require": { @@ -2508,7 +2508,7 @@ "php": ">=5.4.0" }, "require-dev": { - "phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0" + "phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0 || ^8.0 || ^9.0" }, "bin": [ "bin/phpcs", @@ -2527,21 +2527,45 @@ "authors": [ { "name": "Greg Sherwood", - "role": "lead" + "role": "Former lead" + }, + { + "name": "Juliette Reinders Folmer", + "role": "Current lead" + }, + { + "name": "Contributors", + "homepage": "https://github.com/PHPCSStandards/PHP_CodeSniffer/graphs/contributors" } ], "description": "PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.", - "homepage": "https://github.com/squizlabs/PHP_CodeSniffer", + "homepage": "https://github.com/PHPCSStandards/PHP_CodeSniffer", "keywords": [ "phpcs", - "standards" + "standards", + "static analysis" ], "support": { - "issues": "https://github.com/squizlabs/PHP_CodeSniffer/issues", - "source": "https://github.com/squizlabs/PHP_CodeSniffer", - "wiki": "https://github.com/squizlabs/PHP_CodeSniffer/wiki" + "issues": "https://github.com/PHPCSStandards/PHP_CodeSniffer/issues", + "security": "https://github.com/PHPCSStandards/PHP_CodeSniffer/security/policy", + "source": "https://github.com/PHPCSStandards/PHP_CodeSniffer", + "wiki": "https://github.com/PHPCSStandards/PHP_CodeSniffer/wiki" }, - "time": "2022-06-18T07:21:10+00:00" + "funding": [ + { + "url": "https://github.com/PHPCSStandards", + "type": "github" + }, + { + "url": "https://github.com/jrfnl", + "type": "github" + }, + { + "url": "https://opencollective.com/php_codesniffer", + "type": "open_collective" + } + ], + "time": "2023-12-08T12:32:31+00:00" }, { "name": "theseer/tokenizer", @@ -2842,5 +2866,5 @@ "platform-overrides": { "php": "7.4" }, - "plugin-api-version": "2.3.0" + "plugin-api-version": "2.6.0" } diff --git a/inc/email-links.php b/inc/email-links.php index 5d42952..db4c9ce 100644 --- a/inc/email-links.php +++ b/inc/email-links.php @@ -39,7 +39,7 @@ public function init(): void { * Adds an email link to the admin bar to email all commenters. */ public function admin_bar_comment_link(): void { - if ( ! \is_singular() ) { + if ( ! \is_singular() || $this->options['disable_email_all_commenters'] ) { return; } diff --git a/inc/hacks.php b/inc/hacks.php index 92a8e45..137b16e 100644 --- a/inc/hacks.php +++ b/inc/hacks.php @@ -38,7 +38,7 @@ public function __construct() { new Clean_Emails(); } - if ( \is_admin() ) { + if ( \is_admin() || \wp_doing_ajax() ) { new Admin(); } @@ -173,6 +173,10 @@ private function upgrade(): void { $this->options['version'] = \JOOST_COMMENT_HACKS_VERSION; } + if ( ! isset( $this->options['disable_email_all_commenters'] ) ) { + $this->options['disable_email_all_commenters'] = false; + } + \update_option( self::$option_name, $this->options ); } @@ -181,27 +185,28 @@ private function upgrade(): void { */ public static function get_defaults(): array { return [ - 'clean_emails' => true, - 'comment_policy' => false, - 'comment_policy_text' => __( 'I agree to the comment policy.', 'yoast-comment-hacks' ), - 'comment_policy_error' => __( 'You have to agree to the comment policy.', 'yoast-comment-hacks' ), - 'comment_policy_page' => 0, + 'clean_emails' => true, + 'comment_policy' => false, + 'comment_policy_text' => __( 'I agree to the comment policy.', 'yoast-comment-hacks' ), + 'comment_policy_error' => __( 'You have to agree to the comment policy.', 'yoast-comment-hacks' ), + 'comment_policy_page' => 0, + 'disable_email_all_commenters' => false, /* translators: %s expands to the post title */ - 'email_subject' => \sprintf( \__( 'RE: %s', 'yoast-comment-hacks' ), '%title%' ), + 'email_subject' => \sprintf( \__( 'RE: %s', 'yoast-comment-hacks' ), '%title%' ), /* translators: %1$s expands to the commenters first name, %2$s to the post tittle, %3$s to the post permalink, %4$s expands to a double line break. */ - 'email_body' => \sprintf( \__( 'Hi %1$s,%4$sI\'m emailing you because you commented on my post "%2$s" - %3$s', 'yoast-comment-hacks' ), '%firstname%', '%title%', '%permalink%', "\r\n\r\n" ) . "\r\n", + 'email_body' => \sprintf( \__( 'Hi %1$s,%4$sI\'m emailing you because you commented on my post "%2$s" - %3$s', 'yoast-comment-hacks' ), '%firstname%', '%title%', '%permalink%', "\r\n\r\n" ) . "\r\n", /* translators: %1$s expands to the post tittle, %2$s to the post permalink, %3$s expands to a double line break. */ - 'mass_email_body' => \sprintf( \__( 'Hi,%3$sI\'m sending you all this email because you commented on my post "%1$s" - %2$s', 'yoast-comment-hacks' ), '%title%', '%permalink%', "\r\n\r\n" ) . "\r\n", - 'mincomlength' => 15, - 'mincomlengtherror' => \__( 'Error: Your comment is too short. Please try to say something useful.', 'yoast-comment-hacks' ), - 'maxcomlength' => 1500, - 'maxcomlengtherror' => \__( 'Error: Your comment is too long. Please try to be more concise.', 'yoast-comment-hacks' ), - 'redirect_page' => 0, - 'forward_email' => '', - 'forward_name' => \__( 'Support', 'yoast-comment-hacks' ), + 'mass_email_body' => \sprintf( \__( 'Hi,%3$sI\'m sending you all this email because you commented on my post "%1$s" - %2$s', 'yoast-comment-hacks' ), '%title%', '%permalink%', "\r\n\r\n" ) . "\r\n", + 'mincomlength' => 15, + 'mincomlengtherror' => \__( 'Error: Your comment is too short. Please try to say something useful.', 'yoast-comment-hacks' ), + 'maxcomlength' => 1500, + 'maxcomlengtherror' => \__( 'Error: Your comment is too long. Please try to be more concise.', 'yoast-comment-hacks' ), + 'redirect_page' => 0, + 'forward_email' => '', + 'forward_name' => \__( 'Support', 'yoast-comment-hacks' ), /* translators: %1$s is replaced by the blog's name. */ - 'forward_subject' => \sprintf( \__( 'Comment forwarded from %1$s', 'yoast-comment-hacks' ), \get_bloginfo( 'name' ) ), - 'forward_from_email' => self::get_from_email_default(), + 'forward_subject' => \sprintf( \__( 'Comment forwarded from %1$s', 'yoast-comment-hacks' ), \get_bloginfo( 'name' ) ), + 'forward_from_email' => self::get_from_email_default(), ]; } diff --git a/readme.txt b/readme.txt index bac218c..1c9e373 100644 --- a/readme.txt +++ b/readme.txt @@ -3,8 +3,8 @@ Contributors: joostdevalk Tags: comments, spam, emails Text Domain: yoast-comment-hacks Requires at least: 5.9 -Tested up to: 6.1 -Stable tag: 1.9.2 +Tested up to: 6.4 +Stable tag: 2.0 License: GPLv3 License URI: https://www.gnu.org/licenses/gpl-3.0.html Requires PHP: 7.4 @@ -59,6 +59,11 @@ If you have bugs to report, please go to [the plugin's GitHub repository](https: == Changelog == += 1.9.3 = + +* Fix bug where comment reroute recipient would not save. +* Added an option to disable the "Email all commenters" admin bar button. + = 1.9.2 = * Fix missing autoloader diff --git a/yoast-comment-hacks.php b/yoast-comment-hacks.php index 206a670..55b9001 100644 --- a/yoast-comment-hacks.php +++ b/yoast-comment-hacks.php @@ -4,7 +4,7 @@ * * @wordpress-plugin * Plugin Name: Comment Hacks - * Version: 1.9.2 + * Version: 1.9.3 * Plugin URI: https://joost.blog/plugins/comment-hacks/ * Description: Make comments management easier by applying the simple hacks Joost has gathered over the years. * Requires PHP: 7.4 @@ -34,7 +34,7 @@ /** * Used for version checks. */ -define( 'JOOST_COMMENT_HACKS_VERSION', '1.9.2' ); +define( 'JOOST_COMMENT_HACKS_VERSION', '1.9.3' ); /** * Used for asset embedding.