Example #1
0
 /**
  * Checks if a rate is to be deleted
  *
  * @package WP Idea Stream
  * @subpackage admin/admin
  *
  * @since 2.0.0
  *
  * @uses   wp_idea_stream_is_admin() to check if on an IdeaStream Administration screen
  * @uses   wp_idea_stream_user_can() to check for user's capabilities
  * @uses   check_admin_referer() to check the request was made on the site
  * @uses   wp_idea_stream_delete_rate() to delete a rate
  * @uses   add_query_arg() to add query vars to an url
  * @uses   get_edit_post_link() to get the idea edit link
  * @uses   wp_safe_redirect() to safely redirect the user
  */
 public function maybe_delete_rate()
 {
     if (!wp_idea_stream_is_admin()) {
         return;
     }
     if (!wp_idea_stream_user_can('edit_ideas')) {
         return;
     }
     if (empty($_GET['remove_vote']) || empty($_GET['post']) || empty($_GET['action'])) {
         return;
     }
     $idea_id = absint($_GET['post']);
     $user_id = absint($_GET['remove_vote']);
     // nonce check
     check_admin_referer('idea_remove_vote_' . $user_id);
     if (false !== wp_idea_stream_delete_rate($idea_id, $user_id)) {
         $message = 11;
     } else {
         $message = 12;
     }
     // Utimate and not necessary check...
     if (!empty($_GET['remove_vote'])) {
         $redirect = add_query_arg('message', $message, get_edit_post_link($idea_id, 'url'));
         wp_safe_redirect($redirect);
         exit;
     }
 }
Example #2
0
/**
 * Hooks to deleted_user to perform additional actions
 *
 * When a user is deleted, we need to be sure the ideas he shared are also
 * deleted to avoid troubles in edit screens as the post author field will found
 * no user. I also remove rates.
 *
 * The main problem here (excepting error notices) is ownership of the idea. To avoid any
 * troubles, deleting when user leaves seems to be the safest. If you have a different point
 * of view, you can remove_action( 'deleted_user', 'wp_idea_stream_users_delete_user_data', 10, 1 )
 * and use a different way of managing this. I advise you to make sure ideas are reattributed to
 * an existing user ID. About rates, there's no problem if a non existing user ID is in the rating
 * list of an idea.
 *
 * @package WP Idea Stream
 * @subpackage users/functions
 *
 * @since 2.0.0
 *
 * @uses add_filter() to temporarly include all post status
 * @uses wp_idea_stream_ideas_get_ideas() to get all user's ideas and rates
 * @uses remove_filter() to remove the filter
 * @uses apply_filters() Calls 'wp_idea_stream_users_delete_user_force_delete' to override
 * @uses do_action() Calls 'wp_idea_stream_users_before_trash_user_data' to perform actions before idea is trashed
 *                   Calls 'wp_idea_stream_users_before_delete_user_data' to perform actions before idea is deleted
 * @uses wp_delete_post() to peramanently delete (forces flag on) these ideas
 * @uses wp_idea_stream_is_rating_disabled() to check if rating functionality is available
 * @uses wp_idea_stream_delete_rate() to delete user's rates
 * @uses do_action() Calls 'wp_idea_stream_delete_user_rates' to perform actions once user is deleted
 */
function wp_idea_stream_users_delete_user_data($user_id = 0)
{
    if (empty($user_id)) {
        return;
    }
    // Make sure we don't miss any ideas
    add_filter('wp_idea_stream_ideas_get_status', 'wp_idea_stream_ideas_get_all_status', 10, 1);
    // Get user's ideas, in case of multisite
    $user_ideas = wp_idea_stream_ideas_get_ideas(array('per_page' => -1, 'author' => $user_id));
    // remove asap
    remove_filter('wp_idea_stream_ideas_get_status', 'wp_idea_stream_ideas_get_all_status', 10, 1);
    /**
     * We're forcing ideas to be deleted definitively
     * Using this filter you can set it to only be trashed
     *
     * Internally use in case user has been spammed (BuddyPress functionnality)
     * @see  buddypress/functions
     *
     * @param bool   $force_delete true to permanently delete, false to trash
     */
    $force_delete = apply_filters('wp_idea_stream_users_delete_user_force_delete', true);
    // If any delete them
    if (!empty($user_ideas['ideas'])) {
        foreach ($user_ideas['ideas'] as $user_idea) {
            /**
             * WordPress is using a check on native post types
             * so we can't just pass $force_delete to wp_delete_post().
             */
            if (empty($force_delete)) {
                /**
                 * @param  int ID of the idea being trashed
                 * @param  int $user_id the user id
                 */
                do_action('wp_idea_stream_users_before_trash_user_data', $user_idea->ID, $user_id);
                wp_trash_post($user_idea->ID);
            } else {
                /**
                 * @param  int ID of the idea being trashed
                 * @param  int $user_id the user id
                 */
                do_action('wp_idea_stream_users_before_delete_user_data', $user_idea->ID, $user_id);
                wp_delete_post($user_idea->ID, true);
            }
        }
    }
    // Ratings are on, try to delete them.
    if (!wp_idea_stream_is_rating_disabled()) {
        // Make sure we don't miss any ideas
        add_filter('wp_idea_stream_ideas_get_status', 'wp_idea_stream_ideas_get_all_status', 10, 1);
        // Get user's rates
        $rated_ideas = wp_idea_stream_ideas_get_ideas(array('per_page' => -1, 'meta_query' => array(array('key' => '_ideastream_rates', 'value' => ';i:' . $user_id . ';', 'compare' => 'LIKE'))));
        // remove asap
        remove_filter('wp_idea_stream_ideas_get_status', 'wp_idea_stream_ideas_get_all_status', 10, 1);
        // If any delete them.
        if (!empty($rated_ideas['ideas'])) {
            foreach ($rated_ideas['ideas'] as $idea) {
                wp_idea_stream_delete_rate($idea->ID, $user_id);
            }
            /**
             * Internally used in BuddyPress part of the plugin to delete notifications
             * generated by the deleted user.
             * @see buddypress/notifications part
             *
             * @param int $user_id the user ID
             */
            do_action('wp_idea_stream_delete_user_rates', $user_id);
        }
    }
    /**
     * @param int $user_id the user ID
     */
    do_action('wp_idea_stream_users_deleted_user_data', $user_id);
}