function mtphr_gallery_admin_render_youtube_field($resource, $pos = 0, $name_resources)
 {
     $id = isset($resource['id']) ? $resource['id'] : '';
     $title = isset($resource['title']) ? $resource['title'] : $id;
     $description = isset($resource['description']) ? $resource['description'] : '';
     $thumbnail = isset($resource['poster']) ? $resource['poster'] : '';
     $link = isset($resource['link']) ? $resource['link'] : $id;
     echo '<td class="mtphr-gallery-thumbnail mtphr-gallery-youtube-thumbnail">';
     echo '<div class="mtphr-gallery-thumbnail-contents">';
     echo '<input class="mtphr-galleries-id" type="hidden" name="' . $name_resources . '[' . $pos . '][id]" data-prefix="' . $name_resources . '" data-param="id" value="' . $id . '" />';
     echo '<input class="mtphr-galleries-title" type="hidden" name="' . $name_resources . '[' . $pos . '][title]" data-prefix="' . $name_resources . '" data-param="title" value="' . $title . '" />';
     echo '<input class="mtphr-galleries-description" type="hidden" name="' . $name_resources . '[' . $pos . '][description]" data-prefix="' . $name_resources . '" data-param="description" value="' . $description . '" />';
     echo '<input class="mtphr-galleries-poster" type="hidden" name="' . $name_resources . '[' . $pos . '][poster]" data-prefix="' . $name_resources . '" data-param="poster" value="' . $thumbnail . '" />';
     echo '<input class="mtphr-galleries-link" type="hidden" name="' . $name_resources . '[' . $pos . '][link]" data-prefix="' . $name_resources . '" data-param="link" value="' . $link . '" />';
     echo '<input class="mtphr-galleries-type" type="hidden" name="' . $name_resources . '[' . $pos . '][type]" data-prefix="' . $name_resources . '" data-param="type" value="youtube" />';
     echo '<div class="mtphr-galleries-admin-thumb">' . mtphr_gallery_admin_render_youtube_thumb($thumbnail) . '</div>';
     echo '<div class="mtphr-galleries-admin-thumb-title clearfix">';
     echo '<span>' . $title . '</span>';
     echo '<span class="mtphr-galleries-admin-thumb-title-type">' . __('YouTube', 'mtphr-galleries') . '</span>';
     echo mtphr_gallery_admin_preview_button($link);
     echo mtphr_gallery_admin_delete_button();
     echo '</div>';
     echo '</div>';
     echo '</td>';
 }
function mtphr_gallery_create_external_thumb_ajax()
{
    // Get access to the database
    global $wpdb;
    // Check the nonce
    check_ajax_referer('mtphr_galleries', 'security');
    // Get variables
    $type = $_POST['type'];
    $value = $_POST['value'];
    $poster = $_POST['poster'];
    $parent = $_POST['parent'];
    // Get the upload directory
    $upload_dir = wp_upload_dir();
    $gallery_upload_dir = $upload_dir['basedir'] . '/mtphr-galleries';
    $gallery_upload_url = $upload_dir['baseurl'] . '/mtphr-galleries';
    // Create the galleries directory
    if (!file_exists($gallery_upload_dir)) {
        mkdir($gallery_upload_dir);
    }
    // Display the files
    switch ($type) {
        case 'youtube':
            $response = wp_remote_get('http://youtube.com/get_video_info?video_id=' . $value);
            if ($response['response']['code'] == 200) {
                parse_str($response['body'], $ytarr);
                $url = $ytarr['iurlhq'];
                $title = $ytarr['title'];
                $file_path = $gallery_upload_dir . '/youtube-' . $value . '.jpg';
                $file_url = $gallery_upload_url . '/youtube-' . $value . '.jpg';
                if (mtphr_galleries_copy_poster($url, $file_path)) {
                    if ($id = mtphr_galleries_create_external_thumb_attachment($file_path, $file_url, $title, $poster, $parent)) {
                        sleep(2);
                        echo mtphr_gallery_admin_render_youtube_thumb($id);
                    }
                }
            }
            break;
        case 'vimeo':
            $response = wp_remote_get('http://vimeo.com/api/v2/video/' . $value . '.json');
            if ($response['response']['code'] == 200) {
                $body = json_decode($response['body'], true);
                $url = $body[0]['thumbnail_large'];
                $title = $body[0]['title'];
                $file_path = $gallery_upload_dir . '/vimeo-' . $value . '.jpg';
                $file_url = $gallery_upload_url . '/vimeo-' . $value . '.jpg';
                if (mtphr_galleries_copy_poster($url, $file_path)) {
                    if ($id = mtphr_galleries_create_external_thumb_attachment($file_path, $file_url, $title, $poster, $parent)) {
                        sleep(2);
                        echo mtphr_gallery_admin_render_vimeo_thumb($id);
                    }
                }
            }
            break;
    }
    die;
    // this is required to return a proper result
}