/** * Cleanup importer. * * Removes attachment based on ID. * * @since 0.0.1 * * @param string $id Importer ID. */ function hq_import_cleanup($id) { hq_delete_attachment($id); }
/** * Trash or delete a post or page. * * When the post and page is permanently deleted, everything that is tied to * it is deleted also. This includes comments, post meta fields, and terms * associated with the post. * * The post or page is moved to trash instead of permanently deleted unless * trash is disabled, item is already in the trash, or $force_delete is true. * * @since 0.0.1 * * @global hqdb $hqdb HiveQueen database abstraction object. * @see hq_delete_attachment() * @see hq_trash_post() * * @param int $postid Optional. Post ID. Default 0. * @param bool $force_delete Optional. Whether to bypass trash and force deletion. * Default false. * @return array|false|HQ_Post False on failure. */ function hq_delete_post($postid = 0, $force_delete = false) { global $hqdb; if (!($post = $hqdb->get_row($hqdb->prepare("SELECT * FROM {$hqdb->posts} WHERE ID = %d", $postid)))) { return $post; } if (!$force_delete && ($post->post_type == 'post' || $post->post_type == 'page') && get_post_status($postid) != 'trash' && EMPTY_TRASH_DAYS) { return hq_trash_post($postid); } if ($post->post_type == 'attachment') { return hq_delete_attachment($postid, $force_delete); } /** * Fires before a post is deleted, at the start of hq_delete_post(). * * @since 0.0.1 * * @see hq_delete_post() * * @param int $postid Post ID. */ do_action('before_delete_post', $postid); delete_post_meta($postid, '_hq_trash_meta_status'); delete_post_meta($postid, '_hq_trash_meta_time'); hq_delete_object_term_relationships($postid, get_object_taxonomies($post->post_type)); $parent_data = array('post_parent' => $post->post_parent); $parent_where = array('post_parent' => $postid); if (is_post_type_hierarchical($post->post_type)) { // Point children of this page to its parent, also clean the cache of affected children. $children_query = $hqdb->prepare("SELECT * FROM {$hqdb->posts} WHERE post_parent = %d AND post_type = %s", $postid, $post->post_type); $children = $hqdb->get_results($children_query); $hqdb->update($hqdb->posts, $parent_data, $parent_where + array('post_type' => $post->post_type)); } // Do raw query. hq_get_post_revisions() is filtered. $revision_ids = $hqdb->get_col($hqdb->prepare("SELECT ID FROM {$hqdb->posts} WHERE post_parent = %d AND post_type = 'revision'", $postid)); // Use hq_delete_post (via hq_delete_post_revision) again. Ensures any meta/misplaced data gets cleaned up. foreach ($revision_ids as $revision_id) { hq_delete_post_revision($revision_id); } // Point all attachments to this post up one level. $hqdb->update($hqdb->posts, $parent_data, $parent_where + array('post_type' => 'attachment')); $comment_ids = $hqdb->get_col($hqdb->prepare("SELECT comment_ID FROM {$hqdb->comments} WHERE comment_post_ID = %d", $postid)); foreach ($comment_ids as $comment_id) { hq_delete_comment($comment_id, true); } $post_meta_ids = $hqdb->get_col($hqdb->prepare("SELECT meta_id FROM {$hqdb->postmeta} WHERE post_id = %d ", $postid)); foreach ($post_meta_ids as $mid) { delete_metadata_by_mid('post', $mid); } /** * Fires immediately before a post is deleted from the database. * * @since 0.0.1 * * @param int $postid Post ID. */ do_action('delete_post', $postid); $result = $hqdb->delete($hqdb->posts, array('ID' => $postid)); if (!$result) { return false; } /** * Fires immediately after a post is deleted from the database. * * @since 0.0.1 * * @param int $postid Post ID. */ do_action('deleted_post', $postid); clean_post_cache($post); if (is_post_type_hierarchical($post->post_type) && $children) { foreach ($children as $child) { clean_post_cache($child); } } hq_clear_scheduled_hook('publish_future_post', array($postid)); /** * Fires after a post is deleted, at the conclusion of hq_delete_post(). * * @since 0.0.1 * * @see hq_delete_post() * * @param int $postid Post ID. */ do_action('after_delete_post', $postid); return $post; }