[wp-trac] [WordPress Trac] #24913: comment_text filter used differently in two places in core

WordPress Trac noreply at wordpress.org
Thu Aug 1 15:45:46 UTC 2013


#24913: comment_text filter used differently in two places in core
--------------------------+------------------------------
 Reporter:  ronalfy       |       Owner:
     Type:  defect (bug)  |      Status:  new
 Priority:  normal        |   Milestone:  Awaiting Review
Component:  Comments      |     Version:  3.6
 Severity:  normal        |  Resolution:
 Keywords:  dev-feedback  |
--------------------------+------------------------------
Changes (by ericmann):

 * keywords:  2nd-opinion dev-feedback => dev-feedback


Comment:

 Nope, not a bug.  Two different contexts.

 In the first (`/wp-includes/comment.php`), the `$comment` variable
 contains just the comment text.  In the second (`/wp-includes/comment-
 template.php`), the `$comment` variable contains the actual comment object
 itself (based on the return of `get_comment( $comment_ID )`).

 So the filters are filtering the same fields.  The issue then becomes the
 number of parameters and that, too, is not a bug.

 The first comment text filter is used specifically to filter the text of
 the comment before things like inserting it into the database.  The second
 is used when filtering the text of the comment before displaying it on a
 page - this follows the pattern of not trusting data before inserting it
 in the DB and also not trusting the data when pulling it back out.  Using
 the same filter here helps us use ''exactly'' the same rules to filter the
 content both pre and post DB.

 If the problem is the number of fields, simply make the second parameter
 of your function optional:

 {{{
 function filter_text( $comment_text, $comment = null ) {
     // Do something to the comment, possibly switching based on the
 presence of $comment
 }
 add_filter( 'comment_text', 'filter_text', 10, 2 );
 }}}

 If you only want to do things when the comment is inserted into the DB,
 just verify that `null === $comment` in the function above.  If you only
 want to do things when comments are displayed, verify that `$null !==
 $comment`.

 IMO this isn't a bug but a pretty powerful differentiating factor between
 a filter that can be used in different places to do either the same or
 different things.

--
Ticket URL: <http://core.trac.wordpress.org/ticket/24913#comment:1>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list