Exemple #1
0
/**
 * Hokey uninstall routine to remove all avatars
 *
 * @since 0.1.0
 */
function wp_user_avatars_uninstall()
{
    // Get users of blog
    $users = get_users(array('meta_key' => 'wp_user_avatars', 'fields' => 'ids'));
    // Delete all avatars
    foreach ($users as $user_id) {
        wp_user_avatars_delete_avatar($user_id);
    }
    // Cleanup options
    delete_option('wp_user_avatars');
}
Exemple #2
0
/**
 * Runs when a user clicks the Remove button for the avatar
 *
 * @since 0.1.0
 */
function wp_user_avatars_action_remove_avatars()
{
    // Bail if not our request
    if (empty($_GET['user_id']) || empty($_GET['_wpnonce'])) {
        return;
    }
    // Bail if nonce verification fails
    if (!wp_verify_nonce($_GET['_wpnonce'], 'remove_wp_user_avatars_nonce')) {
        return;
    }
    // Cast values
    $user_id = (int) $_GET['user_id'];
    // Bail if user cannot be edited
    if (!current_user_can('edit_avatar', $user_id)) {
        wp_die(esc_html__('You do not have permission to edit this user.', 'wp-user-avatars'));
    }
    // Delete the avatar
    wp_user_avatars_delete_avatar($user_id);
    // Output the default avatar
    if (defined('DOING_AJAX') && DOING_AJAX) {
        echo get_avatar($user_id, 90);
        die;
    }
}
/**
 * Saves avatar image to a user
 *
 * @since 0.1.0
 *
 * @param int        $user_id  ID of user to assign image to
 * @param int|string $media    Local URL for avatar or ID of attachment
 */
function wp_user_avatars_update_avatar($user_id, $media)
{
    // Delete old avatar
    wp_user_avatars_delete_avatar($user_id);
    // Setup empty meta array
    $meta_value = array();
    // Set the attachment URL
    if (is_int($media)) {
        $meta_value['media_id'] = $media;
        $meta_value['site_id'] = get_current_blog_id();
        $media = wp_get_attachment_url($media);
    }
    // Set full value to media URL
    $meta_value['full'] = esc_url_raw($media);
    // Update user metadata
    update_user_meta($user_id, 'wp_user_avatars', $meta_value);
}