public
Last active

Handler for filters and actions for the comment form.

  • Download Gist
class.TTT_Comment_Form.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
<?php
/**
* Handler for filters and actions for the comment form.
*
* comment_form(); in the theme’s comments.php and WP 3+ is required.
*
* @author "Thomas Scholz" http://toscho.de
* @version 1.2
*
*/
class TTT_Comment_Form
{
public
// Name and e-mail required?
$req
// Current commentor, data from cookie.
, $commenter
// May be free extended
, $options = array (
'textarea_first' => TRUE
)
// Placeholder for the textarea code.
, $textarea = ''
;
 
/**
* Sets default values and calls the filter and actions function.
*
* @param array $options
*/
public function __construct( $options = array () )
{
$this->req = (bool) get_option( 'require_name_email' );
$this->commenter = wp_get_current_commenter();
$this->options = array_merge($this->options, $options);
$this->filters_and_actions();
}
 
/**
* Registers necessary filters and actions.
*
* @return void
*/
public function filters_and_actions()
{
add_filter(
'comment_form_default_fields'
, array ( $this, 'filter_default_fields' )
);
add_filter(
'comment_form_defaults'
, array ( $this, 'filter_defaults' )
);
 
// Plugin: Subscribe To "Double-Opt-In" Comments
if ( function_exists('show_subscription_checkbox') )
{
add_action('comment_form_after', 'show_subscription_checkbox');
}
 
// Textarea above the other fields?
if ( $this->options['textarea_first'] )
{
add_action( 'comment_form_top', array ( $this, 'print_textarea') );
}
 
// Additional container around the comment text.
add_filter(
'comment_text'
, array ( $this, 'surround_comment_text' )
);
}
 
/**
* Sets an additional container around the comment text.
*
* @param string $comment_text
* @return string
*/
public function surround_comment_text($comment_text)
{
$class = apply_filters('ttt_comment_surrounder', 'comment_text_block');
return "<div class='$class'>$comment_text</div>";
}
 
/**
* @see $this->options['textarea_first']
* @return void
*/
public function print_textarea()
{
print apply_filters( 'comment_form_field_comment', $this->textarea);
return;
}
 
/**
* Filter for single line text fields.
*
* @param array $f Array of text fields from WordPress
* @return array
*/
public function filter_default_fields($f)
{
$req_mark = $this->req ? ' *' : '';
// No, I don’t use WAI-ARIA.
$req_attr = $this->req ? ' required' : '';
 
$f['author'] = '<p><label for="author">Name'
. $req_mark
. '</label> '
. '<input id="author" name="author" type="text" size="30" value="'
. esc_attr( $this->commenter['comment_author'] ) . '"'
. $req_attr
. '></p>';
$f['email'] = '<p><label for="email">E-Mail'
. $req_mark . '</label> '
. '<input id="email" name="email" type="email" value="'
. esc_attr( $this->commenter['comment_author_email'] ) . '" size="30"'
. $req_attr . '></p>';
$f['url'] = '<p><label for="url">Website</label> '
. '<input id="url" name="url" type="url" value="'
. esc_attr( $this->commenter['comment_author_url'] )
. '" size="30" /></p>';
 
return $f;
}
 
/**
* Filters some additional strings.
* You probably want to overwrite this in your extended class.
*
* @param array $d
* @return array
*/
public function filter_defaults($d)
{
// unset() throws an error, because WP doesn’t check for existence.
$d['comment_notes_after'] = '';
$d['label_submit'] = 'Absenden';
 
$name = trim($this->commenter['comment_author']);
 
// Personalization
$d['title_reply'] = ( isset ( $name[1] )
? "Hallo $name! " : '' )
. 'Was sagst du dazu?';
 
// Reminder for the required fields.
$d['comment_notes_before'] = $this->req
? '' : 'Nur das Textfeld ist Pflicht.';
$d['comment_field'] = '<p><label for="comment">'
. 'Kommentar</label><textarea id="comment" name="comment"'
. ' cols="50" rows="15" required></textarea></p>';
 
// Store the original textarea in a property and remove it here.
if ( $this->options['textarea_first'] )
{
$this->textarea = $d['comment_field'];
$d['comment_field'] = '';
}
 
return $d;
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.