[wp-trac] [WordPress Trac] #12668: Better support for custom comment types
WordPress Trac
noreply at wordpress.org
Wed Feb 5 05:23:06 UTC 2014
#12668: Better support for custom comment types
-------------------------+------------------------------
Reporter: ptahdunbar | Owner: ptahdunbar
Type: enhancement | Status: reopened
Priority: normal | Milestone: Awaiting Review
Component: Comments | Version:
Severity: normal | Resolution:
Keywords: | Focuses:
-------------------------+------------------------------
Comment (by mordauk):
For plugins that store things like order notes, or other private data,
there are three filters that have to be used if the plugin wants to:
1. Remove the custom comment type from the general comment queries (recent
comments widget for example)
2. Remove the custom comment type from comment feeds
3. Remove the custom comment type from comment counts
To do this, plugins have to use 3 separate filters, the last one being
pretty expensive (causes a lot of slow queries on sites with a lot of
comments):
{{{
/**
* Exclude notes (comments) on edd_payment post type from showing in
Recent
* Comments widgets
*
* @since 1.4.1
* @param array $clauses Comment clauses for comment query
* @param obj $wp_comment_query WordPress Comment Query Object
* @return array $clauses Updated comment clauses
*/
function edd_hide_payment_notes( $clauses, $wp_comment_query ) {
global $wpdb;
$clauses['where'] .= ' AND comment_type != "edd_payment_note"';
return $clauses;
}
add_filter( 'comments_clauses', 'edd_hide_payment_notes', 10, 2 );
/**
* Exclude notes (comments) on edd_payment post type from showing in
comment feeds
*
* @since 1.5.1
* @param array $where
* @param obj $wp_comment_query WordPress Comment Query Object
* @return array $where
*/
function edd_hide_payment_notes_from_feeds( $where, $wp_comment_query ) {
global $wpdb;
$where .= $wpdb->prepare( " AND comment_type != %s",
'edd_payment_note' );
return $where;
}
add_filter( 'comment_feed_where', 'edd_hide_payment_notes_from_feeds', 10,
2 );
/**
* Remove EDD Comments from the wp_count_comments function
*
* @access public
* @since 1.5.2
* @param array $stats (empty from core filter)
* @param int $post_id Post ID
* @return array Array of comment counts
*/
function edd_remove_payment_notes_in_comment_counts( $stats, $post_id ) {
global $wpdb, $pagenow;
if( 'index.php' != $pagenow ) {
return $stats;
}
$post_id = (int) $post_id;
if ( apply_filters( 'edd_count_payment_notes_in_comments', false )
)
return $stats;
$stats = wp_cache_get( "comments-{$post_id}", 'counts' );
if ( false !== $stats )
return $stats;
$where = 'WHERE comment_type != "edd_payment_note"';
if ( $post_id > 0 )
$where .= $wpdb->prepare( " AND comment_post_ID = %d",
$post_id );
$count = $wpdb->get_results( "SELECT comment_approved, COUNT( * )
AS num_comments FROM {$wpdb->comments} {$where} GROUP BY
comment_approved", ARRAY_A );
$total = 0;
$approved = array( '0' => 'moderated', '1' => 'approved', 'spam'
=> 'spam', 'trash' => 'trash', 'post-trashed' => 'post-trashed' );
foreach ( (array) $count as $row ) {
// Don't count post-trashed toward totals
if ( 'post-trashed' != $row['comment_approved'] && 'trash'
!= $row['comment_approved'] )
$total += $row['num_comments'];
if ( isset( $approved[$row['comment_approved']] ) )
$stats[$approved[$row['comment_approved']]] =
$row['num_comments'];
}
$stats['total_comments'] = $total;
foreach ( $approved as $key ) {
if ( empty($stats[$key]) )
$stats[$key] = 0;
}
$stats = (object) $stats;
wp_cache_set( "comments-{$post_id}", $stats, 'counts' );
return $stats;
}
add_filter( 'wp_count_comments',
'edd_remove_payment_notes_in_comment_counts', 10, 2 );
}}}
These are the filters we use in [http://wordpress.org/plugins/easy-
digital-downloads/ Easy Digital Downloads]. You may notice that in the
last one we included a check that causes it to just exit early if not on
`index.php`. We discovered that this was resulting in a lot of slow
queries and so had to disable it everywhere but index.php
If we had a true way of handling custom comment types, we could avoid all
of this mess.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/12668#comment:27>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list