/** * Trashes or deletes an attachment. * * When an attachment is permanently deleted, the file will also be removed. * Deletion removes all post meta fields, taxonomy, comments, etc. associated * with the attachment (except the main post). * * The attachment is moved to the trash instead of permanently deleted unless trash * for media is disabled, item is already in the trash, or $force_delete is true. * * @since 2.0.0 * @uses $nxtdb * @uses do_action() Calls 'delete_attachment' hook on Attachment ID. * * @param int $post_id Attachment ID. * @param bool $force_delete Whether to bypass trash and force deletion. Defaults to false. * @return mixed False on failure. Post data on success. */ function nxt_delete_attachment($post_id, $force_delete = false) { global $nxtdb; if (!($post = $nxtdb->get_row($nxtdb->prepare("SELECT * FROM {$nxtdb->posts} WHERE ID = %d", $post_id)))) { return $post; } if ('attachment' != $post->post_type) { return false; } if (!$force_delete && EMPTY_TRASH_DAYS && MEDIA_TRASH && 'trash' != $post->post_status) { return nxt_trash_post($post_id); } delete_post_meta($post_id, '_nxt_trash_meta_status'); delete_post_meta($post_id, '_nxt_trash_meta_time'); $meta = nxt_get_attachment_metadata($post_id); $backup_sizes = get_post_meta($post->ID, '_nxt_attachment_backup_sizes', true); $file = get_attached_file($post_id); if (is_multisite()) { delete_transient('dirsize_cache'); } do_action('delete_attachment', $post_id); nxt_delete_object_term_relationships($post_id, array('category', 'post_tag')); nxt_delete_object_term_relationships($post_id, get_object_taxonomies($post->post_type)); $nxtdb->query($nxtdb->prepare("DELETE FROM {$nxtdb->postmeta} WHERE meta_key = '_thumbnail_id' AND meta_value = %d", $post_id)); $comment_ids = $nxtdb->get_col($nxtdb->prepare("SELECT comment_ID FROM {$nxtdb->comments} WHERE comment_post_ID = %d", $post_id)); if (!empty($comment_ids)) { do_action('delete_comment', $comment_ids); foreach ($comment_ids as $comment_id) { nxt_delete_comment($comment_id, true); } do_action('deleted_comment', $comment_ids); } $post_meta_ids = $nxtdb->get_col($nxtdb->prepare("SELECT meta_id FROM {$nxtdb->postmeta} WHERE post_id = %d ", $post_id)); if (!empty($post_meta_ids)) { do_action('delete_postmeta', $post_meta_ids); $in_post_meta_ids = "'" . implode("', '", $post_meta_ids) . "'"; $nxtdb->query("DELETE FROM {$nxtdb->postmeta} WHERE meta_id IN({$in_post_meta_ids})"); do_action('deleted_postmeta', $post_meta_ids); } do_action('delete_post', $post_id); $nxtdb->query($nxtdb->prepare("DELETE FROM {$nxtdb->posts} WHERE ID = %d", $post_id)); do_action('deleted_post', $post_id); $uploadpath = nxt_upload_dir(); if (!empty($meta['thumb'])) { // Don't delete the thumb if another attachment uses it if (!$nxtdb->get_row($nxtdb->prepare("SELECT meta_id FROM {$nxtdb->postmeta} WHERE meta_key = '_nxt_attachment_metadata' AND meta_value LIKE %s AND post_id <> %d", '%' . $meta['thumb'] . '%', $post_id))) { $thumbfile = str_replace(basename($file), $meta['thumb'], $file); $thumbfile = apply_filters('nxt_delete_file', $thumbfile); @unlink(path_join($uploadpath['basedir'], $thumbfile)); } } // remove intermediate and backup images if there are any foreach (get_intermediate_image_sizes() as $size) { if ($intermediate = image_get_intermediate_size($post_id, $size)) { $intermediate_file = apply_filters('nxt_delete_file', $intermediate['path']); @unlink(path_join($uploadpath['basedir'], $intermediate_file)); } } if (is_array($backup_sizes)) { foreach ($backup_sizes as $size) { $del_file = path_join(dirname($meta['file']), $size['file']); $del_file = apply_filters('nxt_delete_file', $del_file); @unlink(path_join($uploadpath['basedir'], $del_file)); } } $file = apply_filters('nxt_delete_file', $file); if (!empty($file)) { @unlink($file); } clean_post_cache($post_id); return $post; }
/** * Delete a comment. * * By default, the comment will be moved to the trash instead of deleted. * See {@link nxt_delete_comment()} for more information on * this behavior. * * @since 2.7.0 * * @param array $args Method parameters. Contains: * - blog_id * - username * - password * - comment_id * @return mixed {@link nxt_delete_comment()} */ function nxt_deleteComment($args) { $this->escape($args); $blog_id = (int) $args[0]; $username = $args[1]; $password = $args[2]; $comment_ID = (int) $args[3]; if (!($user = $this->login($username, $password))) { return $this->error; } if (!current_user_can('moderate_comments')) { return new IXR_Error(403, __('You are not allowed to moderate comments on this site.')); } if (!get_comment($comment_ID)) { return new IXR_Error(404, __('Invalid comment ID.')); } if (!current_user_can('edit_comment', $comment_ID)) { return new IXR_Error(403, __('You are not allowed to moderate comments on this site.')); } do_action('xmlrpc_call', 'nxt.deleteComment'); return nxt_delete_comment($comment_ID); }
/** * Permanently deletes posts, pages, attachments, and comments which have been in the trash for EMPTY_TRASH_DAYS. * * @since 2.9.0 */ function nxt_scheduled_delete() { global $nxtdb; $delete_timestamp = time() - 60 * 60 * 24 * EMPTY_TRASH_DAYS; $posts_to_delete = $nxtdb->get_results($nxtdb->prepare("SELECT post_id FROM {$nxtdb->postmeta} WHERE meta_key = '_nxt_trash_meta_time' AND meta_value < '%d'", $delete_timestamp), ARRAY_A); foreach ((array) $posts_to_delete as $post) { $post_id = (int) $post['post_id']; if (!$post_id) { continue; } $del_post = get_post($post_id); if (!$del_post || 'trash' != $del_post->post_status) { delete_post_meta($post_id, '_nxt_trash_meta_status'); delete_post_meta($post_id, '_nxt_trash_meta_time'); } else { nxt_delete_post($post_id); } } $comments_to_delete = $nxtdb->get_results($nxtdb->prepare("SELECT comment_id FROM {$nxtdb->commentmeta} WHERE meta_key = '_nxt_trash_meta_time' AND meta_value < '%d'", $delete_timestamp), ARRAY_A); foreach ((array) $comments_to_delete as $comment) { $comment_id = (int) $comment['comment_id']; if (!$comment_id) { continue; } $del_comment = get_comment($comment_id); if (!$del_comment || 'trash' != $del_comment->comment_approved) { delete_comment_meta($comment_id, '_nxt_trash_meta_time'); delete_comment_meta($comment_id, '_nxt_trash_meta_status'); } else { nxt_delete_comment($comment_id); } } }
} elseif (isset($_POST['spam']) && 1 == $_POST['spam']) { if ('spam' == $status) { die((string) time()); } $r = nxt_spam_comment($comment->comment_ID); } elseif (isset($_POST['unspam']) && 1 == $_POST['unspam']) { if ('spam' != $status) { die((string) time()); } $r = nxt_unspam_comment($comment->comment_ID); if (!isset($_POST['comment_status']) || $_POST['comment_status'] != 'spam') { // undo spam, not in spam $delta = 1; } } elseif (isset($_POST['delete']) && 1 == $_POST['delete']) { $r = nxt_delete_comment($comment->comment_ID); } else { die('-1'); } if ($r) { // Decide if we need to send back '1' or a more complicated response including page links and comment counts _nxt_ajax_delete_comment_response($comment->comment_ID, $delta); } die('0'); break; case 'delete-tag': $tag_id = (int) $_POST['tag_ID']; check_ajax_referer("delete-tag_{$tag_id}"); $taxonomy = !empty($_POST['taxonomy']) ? $_POST['taxonomy'] : 'post_tag'; $tax = get_taxonomy($taxonomy); if (!current_user_can($tax->cap->delete_terms)) {
$spammed++; break; case 'unspam': nxt_unspam_comment($comment_id); $unspammed++; break; case 'trash': nxt_trash_comment($comment_id); $trashed++; break; case 'untrash': nxt_untrash_comment($comment_id); $untrashed++; break; case 'delete': nxt_delete_comment($comment_id); $deleted++; break; } } if ($approved) { $redirect_to = add_query_arg('approved', $approved, $redirect_to); } if ($unapproved) { $redirect_to = add_query_arg('unapproved', $unapproved, $redirect_to); } if ($spammed) { $redirect_to = add_query_arg('spammed', $spammed, $redirect_to); } if ($unspammed) { $redirect_to = add_query_arg('unspammed', $unspammed, $redirect_to);
/** * Moves a comment to the Trash * * If trash is disabled, comment is permanently deleted. * * @since 2.9.0 * @uses do_action() on 'trash_comment' before trashing * @uses do_action() on 'trashed_comment' after trashing * @uses nxt_delete_comment() if trash is disabled * * @param int $comment_id Comment ID. * @return mixed False on failure */ function nxt_trash_comment($comment_id) { if (!EMPTY_TRASH_DAYS) { return nxt_delete_comment($comment_id, true); } if (!($comment = get_comment($comment_id))) { return false; } do_action('trash_comment', $comment_id); if (nxt_set_comment_status($comment_id, 'trash')) { add_comment_meta($comment_id, '_nxt_trash_meta_status', $comment->comment_approved); add_comment_meta($comment_id, '_nxt_trash_meta_time', time()); do_action('trashed_comment', $comment_id); return true; } return false; }