/** * Handle a few different variations of writing a comment. */ public static function write_comment($message, $method) { switch ($method) { case 'delete': // @todo comment delete // @todo capability check self::die_failure('delete_comment_not_supported', __('Deleting comments is not supported yet', 'o2')); case 'update': // Update an existing comment add_filter('map_meta_cap', array('o2_Write_API', 'restrict_comment_editing'), 10, 4); if (!current_user_can('edit_comment', $message->id)) { self::die_failure('cannot_edit_comment', __('You are not allowed to edit this comment', 'o2')); } remove_filter('map_meta_cap', array('o2_Write_API', 'restrict_comment_editing'), 10, 4); // Get current comment data $comment_status = wp_get_comment_status($message->id); // Assume that if comment_status is false, that the comment has been deleted. if (false == $comment_status) { self::die_failure('comment_already_deleted', __('Comment has already been deleted by another session.', 'o2')); } else { if ('trash' != $comment_status && $message->isTrashed) { if (!wp_trash_comment($message->id)) { self::die_failure('trash_comment_failed', __('Trashing that comment failed.', 'o2')); } do_action('o2_writeapi_comment_trashed', $message->id); } else { if ('trash' == $comment_status && !$message->isTrashed) { if (!wp_untrash_comment($message->id)) { self::die_failure('untrash_comment_failed', __('Untrashing that comment failed.', 'o2')); } do_action('o2_writeapi_comment_untrashed', $message->id); } else { // Load comment data, merge in new stuff, then save again $comment = get_comment($message->id); $comment->comment_content = addslashes($message->contentRaw); wp_update_comment((array) $comment); // Modifying trash status is bumped in o2:bump_trashed_comment based on trash actions. o2_Fragment::bump_comment_modified_time($message->id); do_action('o2_writeapi_comment_updated', $message->id); } } } // Reload the full, clean object and output it $comment = get_comment($message->id); self::die_success(o2_Fragment::get_fragment($comment)); case 'create': // Posting a new comment. We use the core-provided file to // avoid re-implementing a bunch of logic // Re-map some incoming data to match the expected _POST elements $remap = array('comment_post_ID' => $message->postID, 'comment' => addslashes($message->contentRaw), 'comment_parent' => $message->parentID); $_POST = array_merge($_POST, $remap); // Let the core comment handler take it from here // Have to suppress warnings from wp-comments-post because ABSPATH gets redefined global $wpdb, $user_ID; @(require_once ABSPATH . 'wp-comments-post.php'); // If we get here, it means the core actions weren't fired, so // something most likely went wrong self::die_failure('unknown_comment_error', __('Failed to post comment.', 'o2')); } }
/** * If this comment has no approved siblings, then go ahead and delete its parent as well. * * @param $comment_ID * @param bool|object $comment */ public function remove_trashed_parents($comment_ID, $comment = false) { $child_count = 0; $has_approved_children = false; if (empty($comment)) { // If $comment isn't set, then assume we haven't recursed and setup vars. $child_count = 1; $comment = get_comment($comment_ID); $has_approved_children = $this->has_approved_child($comment_ID); } if (!$has_approved_children && 0 < $comment->comment_parent) { $parent = get_comment($comment->comment_parent); if ('trash' == $parent->comment_approved) { $children = get_comments(array('count' => true, 'parent' => $parent->comment_ID)); if ($child_count == $children) { delete_comment_meta($parent->comment_ID, 'o2_comment_has_children', true); o2_Fragment::bump_comment_modified_time($parent->comment_ID); $this->remove_trashed_parents($parent->comment_ID, $parent); } } } }