Skip to content

Instantly share code, notes, and snippets.

@nataliepo
Created December 28, 2010 01:41
Show Gist options
  • Save nataliepo/756778 to your computer and use it in GitHub Desktop.
Save nataliepo/756778 to your computer and use it in GitHub Desktop.
comment_count updates happening in core
MT::Comment->add_callback( 'post_save', 0, MT->component('core'),
sub {
my ($cb, $comment) = @_;
my $entry = MT::Entry->load( $comment->entry_id )
or return;
$entry->clear_cache('comment_latest');
my $count = MT::Comment->count(
{
entry_id => $comment->entry_id,
visible => 1,
}
);
return unless ( $entry->comment_count != $count );
$entry->comment_count($count);
$entry->save;
},
);
MT::Comment->add_callback( 'post_remove', 0, MT->component('core'),
sub {
my ($cb, $comment) = @_;
my $entry = MT::Entry->load( $comment->entry_id )
or return;
$entry->clear_cache('comment_latest');
if ( $comment->visible ) {
my $count = $entry->comment_count > 0 ? $entry->comment_count - 1 : 0;
$entry->comment_count($count);
$entry->save;
}
},
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment