Skip to content

Instantly share code, notes, and snippets.

@thefuxia
Created September 5, 2010 12:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thefuxia/565997 to your computer and use it in GitHub Desktop.
Save thefuxia/565997 to your computer and use it in GitHub Desktop.
Handler for filters and actions for the comment form.
<?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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment