示例#1
0
/**
 * Delete auto-drafts for new posts that are > 7 days old.
 *
 * @since 0.0.1
 *
 * @global hqdb $hqdb HiveQueen database abstraction object.
 */
function hq_delete_auto_drafts()
{
    global $hqdb;
    // Cleanup old auto-drafts more than 7 days old.
    $old_posts = $hqdb->get_col("SELECT ID FROM {$hqdb->posts} WHERE post_status = 'auto-draft' AND DATE_SUB( NOW(), INTERVAL 7 DAY ) > post_date");
    foreach ((array) $old_posts as $delete) {
        // Force delete.
        hq_delete_post($delete, true);
    }
}
示例#2
0
/**
 * Callback for handling a menu item when its original object is deleted.
 *
 * @since 0.0.1
 * @access private
 *
 * @param int $object_id The ID of the original object being trashed.
 *
 */
function _hq_delete_tax_menu_item($object_id = 0, $tt_id, $taxonomy)
{
    $object_id = (int) $object_id;
    $menu_item_ids = hq_get_associated_nav_menu_items($object_id, 'taxonomy', $taxonomy);
    foreach ((array) $menu_item_ids as $menu_item_id) {
        hq_delete_post($menu_item_id, true);
    }
}
示例#3
0
/**
 * Remove user and optionally reassign posts and links to another user.
 *
 * If the $reassign parameter is not assigned to a User ID, then all posts will
 * be deleted of that user. The action 'delete_user' that is passed the User ID
 * being deleted will be run after the posts are either reassigned or deleted.
 * The user meta will also be deleted that are for that User ID.
 *
 * @since 0.0.1
 *
 * @global hqdb $hqdb
 *
 * @param int $id User ID.
 * @param int $reassign Optional. Reassign posts and links to new User ID.
 * @return bool True when finished.
 */
function hq_delete_user($id, $reassign = null)
{
    global $hqdb;
    $id = (int) $id;
    $user = new HQ_User($id);
    if (!$user->exists()) {
        return false;
    }
    // Normalize $reassign to null or a user ID. 'novalue' was an older default.
    if ('novalue' === $reassign) {
        $reassign = null;
    } elseif (null !== $reassign) {
        $reassign = (int) $reassign;
    }
    /**
     * Fires immediately before a user is deleted from the database.
     *
     * @since 0.0.1
     *
     * @param int      $id       ID of the user to delete.
     * @param int|null $reassign ID of the user to reassign posts and links to.
     *                           Default null, for no reassignment.
     */
    do_action('delete_user', $id, $reassign);
    if (null === $reassign) {
        $post_types_to_delete = array();
        foreach (get_post_types(array(), 'objects') as $post_type) {
            if ($post_type->delete_with_user) {
                $post_types_to_delete[] = $post_type->name;
            } elseif (null === $post_type->delete_with_user && post_type_supports($post_type->name, 'author')) {
                $post_types_to_delete[] = $post_type->name;
            }
        }
        /**
         * Filter the list of post types to delete with a user.
         *
         * @since 0.0.1
         *
         * @param array $post_types_to_delete Post types to delete.
         * @param int   $id                   User ID.
         */
        $post_types_to_delete = apply_filters('post_types_to_delete_with_user', $post_types_to_delete, $id);
        $post_types_to_delete = implode("', '", $post_types_to_delete);
        $post_ids = $hqdb->get_col($hqdb->prepare("SELECT ID FROM {$hqdb->posts} WHERE post_author = %d AND post_type IN ('{$post_types_to_delete}')", $id));
        if ($post_ids) {
            foreach ($post_ids as $post_id) {
                hq_delete_post($post_id);
            }
        }
        // Clean links
        $link_ids = $hqdb->get_col($hqdb->prepare("SELECT link_id FROM {$hqdb->links} WHERE link_owner = %d", $id));
        if ($link_ids) {
            foreach ($link_ids as $link_id) {
                hq_delete_link($link_id);
            }
        }
    } else {
        $post_ids = $hqdb->get_col($hqdb->prepare("SELECT ID FROM {$hqdb->posts} WHERE post_author = %d", $id));
        $hqdb->update($hqdb->posts, array('post_author' => $reassign), array('post_author' => $id));
        if (!empty($post_ids)) {
            foreach ($post_ids as $post_id) {
                clean_post_cache($post_id);
            }
        }
        $link_ids = $hqdb->get_col($hqdb->prepare("SELECT link_id FROM {$hqdb->links} WHERE link_owner = %d", $id));
        $hqdb->update($hqdb->links, array('link_owner' => $reassign), array('link_owner' => $id));
        if (!empty($link_ids)) {
            foreach ($link_ids as $link_id) {
                clean_bookmark_cache($link_id);
            }
        }
    }
    // FINALLY, delete user
    if (is_multisite()) {
        remove_user_from_blog($id, get_current_blog_id());
    } else {
        $meta = $hqdb->get_col($hqdb->prepare("SELECT umeta_id FROM {$hqdb->usermeta} WHERE user_id = %d", $id));
        foreach ($meta as $mid) {
            delete_metadata_by_mid('user', $mid);
        }
        $hqdb->delete($hqdb->users, array('ID' => $id));
    }
    clean_user_cache($user);
    /**
     * Fires immediately after a user is deleted from the database.
     *
     * @since 0.0.1
     *
     * @param int      $id       ID of the deleted user.
     * @param int|null $reassign ID of the user to reassign posts and links to.
     *                           Default null, for no reassignment.
     */
    do_action('deleted_user', $id, $reassign);
    return true;
}