Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce a comment policy option #106

Merged
merged 3 commits into from
Dec 9, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified .wordpress-org/screenshot-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .wordpress-org/screenshot-5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
69 changes: 57 additions & 12 deletions admin/admin.php
Original file line number Diff line number Diff line change
@@ -290,18 +290,31 @@ public function save_reroute_comment_emails(): void {
public function options_validate( array $input ): array {
$defaults = Hacks::get_defaults();

$input['mincomlength'] = (int) $input['mincomlength'];
$input['maxcomlength'] = (int) $input['maxcomlength'];
$input['redirect_page'] = (int) $input['redirect_page'];
$input['forward_email'] = \sanitize_email( $input['forward_email'] );
$input['forward_from_email'] = \sanitize_email( $input['forward_from_email'] );
$input['clean_emails'] = isset( $input['clean_emails'] ) ? 1 : 0;
$input['version'] = \JOOST_COMMENT_HACKS_VERSION;

foreach ( [ 'email_subject', 'email_body', 'mass_email_body', 'forward_name', 'forward_subject' ] as $key ) {
$input[ $key ] = \wp_strip_all_tags( $input[ $key ] );
if ( $input[ $key ] === '' ) {
$input[ $key ] = $defaults[ $key ];
foreach ( $input as $key => $value ) {
switch ( $key ) {
case 'mincomlength':
case 'maxcomlength':
case 'redirect_page':
case 'comment_policy_page':
$input[ $key ] = (int) $value;
break;
case 'version':
$input[ $key ] = JOOST_COMMENT_HACKS_VERSION;
break;
case 'comment_policy':
case 'clean_emails':
$input[ $key ] = $this->sanitize_bool( $value );
break;
case 'email_subject':
case 'email_body':
case 'mass_email_body':
case 'forward_name':
case 'forward_subject':
$input[ $key ] = $this->sanitize_string( $value, $defaults[ $key ] );
break;
case 'forward_email':
case 'forward_from_email':
$input[ $key ] = \sanitize_email( $value );
}
}

@@ -314,6 +327,38 @@ public function options_validate( array $input ): array {
return $input;
}

/**
* Turns checkbox values into booleans.
*
* @param mixed $value The input value to cast to boolean.
*/
private function sanitize_bool( $value ): bool {
if ( $value ) {
$value = true;
}
if ( empty( $value ) ) {
$value = false;
}

return $value;
}

/**
* Turns empty string into defaults.
*
* @param mixed $value The input value.
* @param string $default The default value of the string.
*
* @return array $input The array with sanitized input values.
*/
private function sanitize_string( $value, $default ) {
if ( $value === '' ) {
$value = $default;
}

return $value;
}

/**
* Register the config page for all users that have the manage_options capability.
*/
57 changes: 57 additions & 0 deletions admin/views/config-page.php
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@
<h2 class="nav-tab-wrapper" id="yoast-tabs">
<a class="nav-tab nav-tab-active" id="comment-length-tab"
href="#top#comment-length"><?php esc_html_e( 'Comment length', 'yoast-comment-hacks' ); ?></a>
<a class="nav-tab" id="comment-policy-tab" href="#top#comment-policy"><?php esc_html_e( 'Comment policy', 'yoast-comment-hacks' ); ?></a>
<a class="nav-tab" id="email-links-tab"
href="#top#email-links"><?php esc_html_e( 'Email links', 'yoast-comment-hacks' ); ?></a>
<a class="nav-tab" id="comment-redirect-tab"
@@ -84,6 +85,62 @@
</table>
</div>

<div id="comment-policy" class="yoasttab">
<h3><?php esc_html_e( 'Comment policy', 'yoast-comment-hacks' ); ?></h3>
<p><?php esc_html_e( 'This feature allows you to institute a comment policy for your site. To enable it, set a text in the input field below.', 'yoast-comment-hacks' ); ?></p>
<table class="form-table">
<tr>
<th scope="row">
<label for="comment_policy"><?php esc_html_e( 'Comment policy', 'yoast-comment-hacks' ); ?></label>
</th>
<td>
<input type="checkbox" <?php checked( $this->options['comment_policy'] ); ?> name="<?php echo esc_attr( $yoast_comment_option_name . '[comment_policy]' ); ?>" id="comment_policy"/>
<p>
<label for="comment_policy"><?php esc_html_e( 'Enable the comment policy functionality.', 'yoast-comment-hacks' ); ?></label>
</p>
</td>
</tr>
<tr>
<th scope="row">
<label for="comment_policy_page"><?php esc_html_e( 'Comment policy page', 'yoast-comment-hacks' ); ?></label>
</th>
<td>
<?php
wp_dropdown_pages(
[
'depth' => 0,
'id' => 'comment_policy_page',
'name' => esc_attr( $yoast_comment_option_name . '[comment_policy_page]' ),
'option_none_value' => 0,
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $selected is not put out, only compared against.
'selected' => ( isset( $this->options['comment_policy_page'] ) ? (int) $this->options['comment_policy_page'] : 0 ),
'show_option_none' => esc_html__( 'Select comment policy page', 'yoast-comment-hacks' ),
]
);
?>
</td>
</tr>
<tr>
<th scope="row">
<label for="comment_policy_text"><?php esc_html_e( 'Comment policy line', 'yoast-comment-hacks' ); ?></label>
</th>
<td>
<input type="text" class="regular-text" value="<?php echo esc_attr( $this->options['comment_policy_text'] ); ?>" name="<?php echo esc_attr( $yoast_comment_option_name . '[comment_policy_text]' ); ?>" id="comment_policy_text"/>
<p><?php esc_html_e( 'Text that appears along with a checkbox above the comment submit button.', 'yoast-comment-hacks' ); ?></p>
</td>
</tr>
<tr>
<th scope="row">
<label
for="comment_policyesc_html_error"><?php esc_html_e( 'Error message for not checking comment policy checkbox', 'yoast-comment-hacks' ); ?></label>
</th>
<td>
<textarea rows="4" cols="80" name="<?php echo esc_attr( $yoast_comment_option_name . '[comment_policy_error]' ); ?>" id="comment_policy"><?php echo esc_html( $this->options['comment_policy_error'] ); ?></textarea>
</td>
</tr>
</table>
</div>

<div id="email-links" class="yoasttab">
<h3><?php esc_html_e( 'Email links', 'yoast-comment-hacks' ); ?></h3>

45 changes: 43 additions & 2 deletions inc/forms.php
Original file line number Diff line number Diff line change
@@ -7,11 +7,52 @@
*/
class Forms {

/**
* Holds our options.
*/
private array $options;

/**
* Class constructor.
*/
public function __construct() {
\add_filter( 'comment_form_defaults', [ $this, 'filter_defaults' ] );
$this->options = Hacks::get_options();
Hacks::get_defaults();

if ( $this->options['comment_policy'] ) {
\add_action( 'comment_form_after_fields', [ $this, 'comment_form_fields' ] );
\add_filter( 'preprocess_comment', [ $this, 'check_comment_policy' ] );
}
}

/**
* Adds the comment policy checkbox to the comment form.
*
* @return void
*/
public function comment_form_fields() {
echo '<label class="agree-comment-policy">';
echo '<input type="checkbox" name="comment_policy">';
echo ' <a href="' . esc_url( get_permalink( $this->options['comment_policy_page'] ) ) . '" target="_blank">';
echo esc_html( $this->options['comment_policy_text'] );
echo '</a>';
echo '</label>';
}

/**
* Checks whether the comment policy box was checked or not.
*
* @param array $comment_data Array with comment data. Unused.
*
* @return array
*/
public function check_comment_policy( $comment_data ) {
// phpcs:ignore WordPress.Security.NonceVerification.Missing -- Comment forms (unfortunately) are always without nonces.
if ( ! isset( $_POST['comment_policy'] ) || ! ( $_POST['comment_policy'] === 'on' || $_POST['comment_policy'] === true ) ) {
wp_die( esc_html( $this->options['comment_policy_error'] ) . '<br /><br /><a href="javascript:history.go(-1);">' . esc_html__( 'Go back and try again.', 'yoast-comment-hacks' ) . '</a>' );
}

return $comment_data;
}

/**
@@ -21,7 +62,7 @@ public function __construct() {
*
* @return array The filtered defaults.
*/
public function filter_defaults( $defaults ): array {
public function filter_defaults( array $defaults ): array {
$defaults['comment_notes_before'] = '<span class="agree-comment-policy">You have to agree to the comment policy.</span>';

return $defaults;
48 changes: 23 additions & 25 deletions inc/hacks.php
Original file line number Diff line number Diff line change
@@ -11,17 +11,13 @@ class Hacks {

/**
* Holds the plugins option name.
*
* @var string
*/
public static string $option_name = 'yoast_comment_hacks';

/**
* Holds the plugins options.
*
* @var array
*/
private $options = [];
private array $options = [];

/**
* Class constructor.
@@ -58,6 +54,7 @@ public static function get_options(): array {
if ( ! is_array( $options ) ) {
return [];
}

return $options;
}

@@ -78,14 +75,12 @@ private static function get_from_email_default(): string {
/**
* Check whether the current commenter is a first time commenter, if so, redirect them to the specified settings.
*
* @since 1.0
*
* @param string $url The original redirect URL.
* @param object $comment The comment object.
* @param string $url The original redirect URL.
* @param \WP_Comment $comment The comment object.
*
* @return string The URL to be redirected to, altered if this was a first time comment.
*/
public function comment_redirect( $url, $comment ) {
public function comment_redirect( string $url, \WP_Comment $comment ): string {
$has_approved_comment = \get_comments(
[
'author_email' => $comment->comment_author_email,
@@ -135,11 +130,10 @@ public function comment_redirect( $url, $comment ) {
/**
* See if the option has been cached, if it is, return it, otherwise return false.
*
* @since 1.3
*
* @param string $option The option to check for.
*
* @return bool|mixed
* @since 1.3
*/
private function get_option_from_cache( string $option ) {
$options = \wp_load_alloptions();
@@ -181,23 +175,27 @@ private function upgrade(): void {
*/
public static function get_defaults(): array {
return [
'clean_emails' => true,
'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,
/* 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(),
];
}

12 changes: 7 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
@@ -7,6 +7,8 @@ Make comments management easier by applying the simple hacks Joost has gathered
This plugin adds some small hacks around core WordPress comments to make them more bearable:

* Cleaner comment notification emails.
* The option to enforce a comment policy.
* The option to forward comments to an email address (for instance for your support team) and then trash them.
* The option to disallow comments below a certain length.
* The option to redirect first time commenters to a "thank you" page.
* An input field on the comment edit screen to change the comment parent ID.
@@ -18,16 +20,16 @@ This repository uses [the Yoast grunt tasks plugin](https://github.com/Yoast/plu
## Screenshots

Screenshot of a clean comment notification email:<br>
![Screenshot of a clean comment notification email](svn-assets/screenshot-1.png)<br>
![Screenshot of a clean comment notification email](.wordpress-org/screenshot-1.png)<br>
<br>
The comment parent edit box:<br>
![The comment parent edit box](svn-assets/screenshot-2.png)<br>
![The comment parent edit box](.wordpress-org/screenshot-2.png)<br>
<br>
The plugins admin settings:<br>
![The plugins admin settings](svn-assets/screenshot-3.png)<br>
![The plugins admin settings](.wordpress-org/screenshot-3.png)<br>
<br>
The button on the frontend to email all the commenters on a post:<br>
![The button to email all commenters](svn-assets/screenshot-4.png)<br>
![The button to email all commenters](.wordpress-org/screenshot-4.png)<br>
<br>
The link in the backend to email an individual commenters on a post:<br>
![The button to email all commenters](svn-assets/screenshot-5.png)<br><br>
![The button to email all commenters](.wordpress-org/screenshot-5.png)<br><br>
9 changes: 9 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
@@ -18,6 +18,9 @@ Make comments management easier by applying the simple hacks Joost has gathered
This plugin adds some small hacks around core WordPress comments to make them more bearable:

* Cleaner comment notification emails.
* The option to enforce a comment policy: just create a comment policy page, toggle the option on and select it, and
commenters will have to accept your comment policy before being able to comment.
* The option to forward comments to an email address (for instance for your support team) and then trash them.
* The option to disallow comments below and above a certain length.
* The option to redirect first time commenters to a "thank you" page.
* An input field on the comment edit screen to change the comment parent ID.
@@ -51,6 +54,12 @@ See the screenshots to get an even better idea of the plugins' functionality.

== Changelog ==

= 1.9 =

* Introduces a new option to the plugin: adding a comment policy was never easier than this: just create a comment
policy page, toggle the option on and select it, and commenters will have to accept your comment policy before being
able to comment.

= 1.8.1 =

* Fixed a couple of PHP 7.4 related issues.