Exemple #1
0
function largo_save_avatar_field($user_id)
{
    if (has_files_to_upload(LARGO_AVATAR_INPUT_NAME)) {
        if (isset($_FILES[LARGO_AVATAR_INPUT_NAME])) {
            $file = wp_upload_bits($_FILES[LARGO_AVATAR_INPUT_NAME]['name'], null, @file_get_contents($_FILES[LARGO_AVATAR_INPUT_NAME]['tmp_name']));
            if (FALSE === $file['error']) {
                $mime_type = wp_check_filetype($file['file']);
                $args = array('guid' => $file['url'], 'post_mime_type' => $mime_type['type'], 'post_title' => preg_replace('/\\.[^.]+$/', '', basename($file['file'])), 'post_content' => '', 'post_status' => 'inherit');
                $id = wp_insert_attachment($args, $file['file']);
                if (!is_wp_error($id)) {
                    require_once ABSPATH . 'wp-admin/includes/image.php';
                    // Generate the metadata for the attachment, and update the database record.
                    $metadata = wp_generate_attachment_metadata($id, $file['file']);
                    $update = wp_update_attachment_metadata($id, $metadata);
                    largo_associate_avatar_with_user($user_id, $id);
                }
            }
        }
    }
}
 function test_largo_remove_avatar()
 {
     // Should also work if we're editing another user's profile
     largo_associate_avatar_with_user($this->user_id, $this->avatar_id);
     // We can simulate editing another user's profile by setting the $_POST['user_id'] key.
     $_POST['user_id'] = $this->user_id;
     try {
         $this->_handleAjax("largo_remove_avatar");
     } catch (WPAjaxDieContinueException $e) {
         // Retrieve the $avatar_id.
         $retrieved = largo_get_user_avatar_id($this->user_id);
         // $retrieved should be empty at this point
         $this->assertEmpty($retrieved);
     }
 }