Here you go:
Code: Select all
// adjust parent comment count and topic posts approved if deleted post is a comment
public function handle_post_delete_conditions(event $event)
{
$post_id = $event['post_id'];
$topic_id = $event['topic_id'];
$post_data = $event['post_data'];
$parent_id = $post_data['parent_id'];
// for some reason comments run this event twice, this failed attempt makes sure that on the second time we don't update the tables
$select_from_closure = 'SELECT parent_id FROM ' . POSTS_TABLE . '
WHERE post_id = ' . $post_id;
$result = $parent_checker = array();
$result = $this->db->sql_query($select_from_closure);
while($row = $this->db->sql_fetchrow($result))
{
$parent_checker = $row['parent_id'];
}
unset($result);
if(($parent_id == $parent_checker) && $parent_id > 0)
{ // decrement parent's comment count by 1
$parent_count_query = 'UPDATE ' . POSTS_TABLE . '
SET post_comments = post_comments - 1
WHERE post_id = ' . $parent_id;
$result = array();
$result = $this->db->sql_query($parent_count_query);
unset($result);
$posts_approved_query = 'UPDATE ' . TOPICS_TABLE . '
SET topic_posts_approved = topic_posts_approved + 1
WHERE topic_id = ' . $topic_id;
$result = array();
$result = $this->db->sql_query($posts_approved_query);
unset($result);
}
}
// delete nested posts when parent post is being deleted
// adapted some from functions_posting.php if statemant at line 1406
public function delete_post_after(event $event)
{
$data = $event['data'];
$is_soft = $event['is_soft'];
$post_id = $event['post_id'];
$topic_id = $event['topic_id'];
$forum_id = $event['forum_id'];
$next_post_id = $event['next_post_id'];
$softdelete_reason = $event['softdelete_reason'];
// only way to transfer info from the recursive call I made in this event
if(strlen($softdelete_reason) > 10)
{
$delete_runner = substr($softdelete_reason, -10);
// not sure how much use this is
$softdelete_reason = substr($softdelete_reason, 0, -10);
}
// delete_runner is an attempt to not have this if block ran on comments as the parent's call to this event is going to take care of all of this
if(!$is_soft && $delete_runner != "DO~NOT~RUN")
{ // get post ids of every post that will be deleted except this post
$select_from_closure = 'SELECT child_id FROM phpbb_posts_kinship
WHERE child_id IN
( SELECT child_id
FROM phpbb_posts_kinship
WHERE ancestor_id = ' . $post_id . '
AND child_id != ' . $post_id . '
)';
$result = $post_list = array();
$result = $this->db->sql_query($select_from_closure);
while($row = $this->db->sql_fetchrow($result))
{
$post_list[] = $row['child_id'];
}
unset($result);
$delete_from_closure = 'DELETE FROM phpbb_posts_kinship
WHERE child_id IN
( SELECT * FROM
( SELECT child_id
FROM phpbb_posts_kinship
WHERE ancestor_id = ' . $post_id . '
) AS tmp
)';
$result = array();
$result = $this->db->sql_query($delete_from_closure);
unset($result);
// remove repeats, more efficient than array_unique (used to be array_flip(array_flip()) but that gave warnings)
$post_list = array_unique($post_list);
$comment_count = count($post_list);
// so the post we're deleting has comments...
if($comment_count > 0)
{
$post_data_query = 'SELECT post_visibility, post_reported, post_time, poster_id, post_postcount
FROM ' . POSTS_TABLE . '
WHERE post_id IN (' . implode(',', $post_list) . ')';
$result = $post_data = array();
$result = $this->db->sql_query($post_data_query);
while($row = $this->db->sql_fetchrow($result))
{
$post_data['post_visibility'][] = $row['post_visibility'];
$post_data['post_reported'][] = $row['post_reported'];
$post_data['post_time'][] = $row['post_time'];
$post_data['poster_id'][] = $row['poster_id'];
$post_data['post_postcount'][] = $row['post_postcount'];
}
unset($result);
// add the number of comments we're about to delete to topic_posts_approved since delete_post decrements it
$posts_approved_query = 'UPDATE ' . TOPICS_TABLE . '
SET topic_posts_approved = topic_posts_approved + ' . $comment_count . '
WHERE topic_id = ' . $topic_id;
$result = array();
$result = $this->db->sql_query($posts_approved_query);
unset($result);
for($i = 0; $i < $comment_count; $i++)
{
$data['post_visibility'] = $post_data['post_visibility'][$i];
$data['post_reported'] = $post_data['post_reported'][$i];
$data['post_time'] = $post_data['post_time'][$i];
$data['poster_id'] = $post_data['poster_id'][$i];
$data['post_postcount'] = $post_data['post_postcount'][$i];
$next_post_id = delete_post($forum_id, $topic_id, $post_list[$i], $data, false, "~DO~NOT~RUN");
}
}
}
$event['data'] = $data;
$event['next_post_id'] = $next_post_id;
$event['softdelete_reason'] = $softdelete_reason;
}
event $event
should be just $event
.I'm making an extension that allows you to make nested comments, think reddit. For permanently deleting posts, I want to remove parent P's whole comment tree, which I have to recursively call delete_post with on all of it's comment's post ids excluding the current post id,
$post_list
. I get parent P's whole comment tree list from the query $select_from_closure
and store it in $post_list.handle_post_delete_conditions
is called twice. I can tell because if the post I'm deleting is a comment itself, I decrement its parent's total comment count by 1. But if this post has comments itself, it runs the event twice for the original post_id that you're permanently deleting. I thought, if I pull the parent id from the posts table for the second call and check for it, it wouldn't matter. You can see my attempt at this in the code I provided. But it seems the event is called twice BEFORE the original parent_id is deleted.I saw it that way in some other extension, thank you for the tip