diff --git a/admin/admin.php b/admin/admin.php index 0696bf8..8a546f9 100644 --- a/admin/admin.php +++ b/admin/admin.php @@ -349,6 +349,7 @@ 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': @@ -356,6 +357,7 @@ public function options_validate( array $input ): array { 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': diff --git a/admin/views/config-page.php b/admin/views/config-page.php index 483cd2f..93a1afc 100644 --- a/admin/views/config-page.php +++ b/admin/views/config-page.php @@ -116,6 +116,42 @@ class="small-text" + +

+ +

+ + + + + + + + + +
+ + + options['allowed_com_length_note_show'] ); ?> + /> +
+ + + +
@@ -352,7 +388,7 @@ class="regular-text" type="checkbox" id="clean_emails" name="" - options['clean_emails'] ); ?> + options['clean_emails'] ); ?> /> diff --git a/inc/hacks.php b/inc/hacks.php index 5909e48..49e61d3 100644 --- a/inc/hacks.php +++ b/inc/hacks.php @@ -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' ), diff --git a/inc/length.php b/inc/length.php index 049621a..5fd5051 100644 --- a/inc/length.php +++ b/inc/length.php @@ -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 ); } /** @@ -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 = '' . $field; + } + + return $field; + } }