Skip to content

Add allowed comment length note above the comment textarea #150

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

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions admin/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,13 +349,15 @@ public function options_validate( array $input ): array {
case 'comment_policy':
case 'clean_emails':
case 'disable_email_all_commenters':
case 'allowed_com_length_note_show':
$input[ $key ] = $this->sanitize_bool( $value );
break;
case 'email_subject':
case 'email_body':
case 'mass_email_body':
case 'forward_name':
case 'forward_subject':
case 'allowed_com_length_note_text':
$input[ $key ] = $this->sanitize_string( $value, $defaults[ $key ] );
break;
case 'forward_email':
Expand Down
38 changes: 37 additions & 1 deletion admin/views/config-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,42 @@ class="small-text"
</td>
</tr>
</table>

<h3><?php esc_html_e( 'Display note about comment length above comment textarea', 'comment-hacks' ); ?></h3>

<p><?php esc_html_e( 'Display a note above the comment textarea to inform users about the allowed comment length.', 'comment-hacks' ); ?></p>
<table class="form-table">
<tr>
<th scope="row">
<label for="allowed_com_length_note_show">
<?php esc_html_e( 'Display note', 'comment-hacks' ); ?>
</label>
</th>
<td>
<input
type="checkbox"
id="allowed_com_length_note_show"
name="<?php echo esc_attr( Hacks::$option_name . '[allowed_com_length_note_show]' ); ?>"
<?php checked( $this->options['allowed_com_length_note_show'] ); ?>
/>
</td>
</tr>
<tr>
<th scope="row">
<label for="allowed_com_length_note_text">
<?php esc_html_e( 'Note text', 'comment-hacks' ); ?>
</label>
</th>
<td>
<textarea
rows="4"
cols="80"
name="<?php echo esc_attr( Hacks::$option_name . '[allowed_com_length_note_text]' ); ?>"
id="allowedcomnote"
><?php echo esc_textarea( $this->options['allowed_com_length_note_text'] ); ?></textarea>
</td>
</tr>
</table>
</div>

<div id="comment-policy" class="emiliaprojectstab">
Expand Down Expand Up @@ -352,7 +388,7 @@ class="regular-text"
type="checkbox"
id="clean_emails"
name="<?php echo esc_attr( Hacks::$option_name . '[clean_emails]' ); ?>"
<?php checked( $this->options['clean_emails'] ); ?>
<?php checked( $this->options['clean_emails'] ); ?>
/>
</td>
</tr>
Expand Down
3 changes: 3 additions & 0 deletions inc/hacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,9 @@ public static function get_defaults(): array {
'mincomlengtherror' => \__( 'Error: Your comment is too short. Please try to say something useful.', 'comment-hacks' ),
'maxcomlength' => 1500,
'maxcomlengtherror' => \__( 'Error: Your comment is too long. Please try to be more concise.', 'comment-hacks' ),
'allowed_com_length_note_show' => false,
/* translators: %1$s is replaced with the minimum number of characters, %2$s with the maximum number of characters */
'allowed_com_length_note_text' => \sprintf( \__( 'Allowed comment length is between %1$s and %2$s characters.', 'comment-hacks' ), '%mincomlength%', '%maxcomlength%' ),
'redirect_page' => 0,
'forward_email' => '',
'forward_name' => \__( 'Support', 'comment-hacks' ),
Expand Down
23 changes: 23 additions & 0 deletions inc/length.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public function __construct() {

// Process the comment and check it for length.
\add_filter( 'preprocess_comment', [ $this, 'check_comment_length' ] );

// Add a message to the comment form, before the comment textarea, to inform the user about the allowed comment length.
\add_filter( 'comment_form_field_comment', [ $this, 'allowed_comment_length_note' ], 99 );
}

/**
Expand Down Expand Up @@ -73,4 +76,24 @@ private function get_comment_length( string $comment ): int {
}
return \strlen( $comment );
}

/**
* Adds a message to the comment form for the allowed comment length.
*
* @since 2.1.4
*
* @param string $field The comment form field.
*
* @return string The comment form field.
*/
public function allowed_comment_length_note( $field ) {

if ( $this->options['allowed_com_length_note_show'] ) {
$note = \str_replace( '%mincomlength%', $this->options['mincomlength'], $this->options['allowed_com_length_note_text'] );
$note = \str_replace( '%maxcomlength%', $this->options['maxcomlength'], $note );
$field = '<label class="comment-hacks-allowed-comment-length-note">' . \esc_html( $note ) . '</label>' . $field;
}

return $field;
}
}