protected function syncPlaylistIds(Google_Service_YouTube $youtube, $remoteId, array $playlistIds)
 {
     $playlistsResponseList = $youtube->playlists->listPlaylists('id,snippet', array('mine' => true));
     foreach ($playlistsResponseList->getItems() as $playlist) {
         $playlistId = $playlist['id'];
         if (!in_array($playlistId, $playlistIds)) {
             $playlistsItemsListResponse = $youtube->playlistItems->listPlaylistItems('snippet', array('playlistId' => $playlistId, 'videoId' => $remoteId));
             foreach ($playlistsItemsListResponse->getItems() as $playlistItem) {
                 $youtube->playlistItems->delete($playlistItem['id']);
             }
         }
     }
     foreach ($playlistIds as $playlistId) {
         if (!$playlistId) {
             continue;
         }
         $playlistsItemsListResponse = $youtube->playlistItems->listPlaylistItems('snippet', array('playlistId' => $playlistId, 'videoId' => $remoteId));
         if (count($playlistsItemsListResponse->getItems())) {
             continue;
         }
         $resourceId = new Google_Service_YouTube_ResourceId();
         $resourceId->setKind('youtube#video');
         $resourceId->setVideoId($remoteId);
         $snippet = new Google_Service_YouTube_PlaylistItemSnippet();
         $snippet->setPlaylistId($playlistId);
         $snippet->setResourceId($resourceId);
         $playlistItem = new Google_Service_YouTube_PlaylistItem();
         $playlistItem->setSnippet($snippet);
         $youtube->playlistItems->insert('snippet', $playlistItem);
     }
 }
 function addVideoToWatchLaterList($watch_later_list_id, $video_id)
 {
     // include the required php files - containers client_id and client_secret
     include_once YT4WP_PATH . 'lib/google_api_wrapper_clientid_clientsecret.php';
     try {
         $resourceId = new Google_Service_YouTube_ResourceId();
         $resourceId->setVideoId($video_id);
         $resourceId->setKind('youtube#video');
         $playlistItemSnippet = new Google_Service_YouTube_PlaylistItemSnippet();
         $playlistItemSnippet->setPlaylistId($watch_later_list_id);
         $playlistItemSnippet->setResourceId($resourceId);
         $playlistItem = new Google_Service_YouTube_PlaylistItem();
         $playlistItem->setSnippet($playlistItemSnippet);
         $insertVideoToWatchLaterPlaylist = $youtube->playlistItems->insert('snippet,contentDetails', $playlistItem, array());
         if ($insertVideoToWatchLaterPlaylist) {
             echo 'Success : Video added to Watch Later playlist';
         }
     } catch (Google_ServiceException $e) {
         echo $e->getMessage();
     } catch (Google_Exception $e) {
         if (strpos($e->getMessage(), '(403) Playlist contains maximum number of items.') !== false) {
             echo 'Error : Video already exists in Watch Later playlist';
         }
     }
 }
Ejemplo n.º 3
0
 $youTubePlaylist->setSnippet($playlistSnippet);
 $youTubePlaylist->setStatus($playlistStatus);
 // 4. Call the playlists.insert method to create the playlist. The API
 // response will contain information about the new playlist.
 $playlistResponse = $youtube->playlists->insert('snippet,status', $youTubePlaylist, array());
 $playlistId = $playlistResponse['id'];
 // 5. Add a video to the playlist. First, define the resource being added
 // to the playlist by setting its video ID and kind.
 $resourceId = new Google_Service_YouTube_ResourceId();
 $resourceId->setVideoId('SZj6rAYkYOg');
 $resourceId->setKind('youtube#video');
 // Then define a snippet for the playlist item. Set the playlist item's
 // title if you want to display a different value than the title of the
 // video being added. Add the resource ID and the playlist ID retrieved
 // in step 4 to the snippet as well.
 $playlistItemSnippet = new Google_Service_YouTube_PlaylistItemSnippet();
 $playlistItemSnippet->setTitle('First video in the test playlist');
 $playlistItemSnippet->setPlaylistId($playlistId);
 $playlistItemSnippet->setResourceId($resourceId);
 // Finally, create a playlistItem resource and add the snippet to the
 // resource, then call the playlistItems.insert method to add the playlist
 // item.
 $playlistItem = new Google_Service_YouTube_PlaylistItem();
 $playlistItem->setSnippet($playlistItemSnippet);
 $playlistItemResponse = $youtube->playlistItems->insert('snippet,contentDetails', $playlistItem, array());
 $htmlBody .= "<h3>New Playlist</h3><ul>";
 $htmlBody .= sprintf('<li>%s (%s)</li>', $playlistResponse['snippet']['title'], $playlistResponse['id']);
 $htmlBody .= '</ul>';
 $htmlBody .= "<h3>New PlaylistItem</h3><ul>";
 $htmlBody .= sprintf('<li>%s (%s)</li>', $playlistItemResponse['snippet']['title'], $playlistItemResponse['id']);
 $htmlBody .= '</ul>';
     }
     fclose($handle);
     // If you want to make other calls after the file upload, set setDefer back to false
     $client->setDefer(false);
     if ($screen_base != 'toplevel_page_youtube-for-wordpress') {
         $insert_button = '<a class="button button-secondary insert_video_button">insert</a>';
     } else {
         $insert_button = '';
     }
     // if the user selected to upload to a playlist
     // else we just skip it
     if ($playlist_id != '') {
         $resourceId = new Google_Service_YouTube_ResourceId();
         $resourceId->setVideoId($status['id']);
         $resourceId->setKind('youtube#video');
         $playlistItemSnippet = new Google_Service_YouTube_PlaylistItemSnippet();
         $playlistItemSnippet->setTitle($_POST['video-title']);
         $playlistItemSnippet->setPlaylistId($playlist_id);
         $playlistItemSnippet->setResourceId($resourceId);
         $playlistItem = new Google_Service_YouTube_PlaylistItem();
         $playlistItem->setSnippet($playlistItemSnippet);
         $insertToPlaylistRequest = $youtube->playlistItems->insert('snippet,contentDetails', $playlistItem, array());
     }
     $htmlBody .= '<h3>Content Successfuly Uploaded</h3><p>Your new content should now be viewable from within the <a style="margin-top:-6px;" class="button-secondary" href="?page=youtube-for-wordpress&amp;tab=youtube_plus_browse">browse</a> tab, but may be unavailable until it completes processing.</p><br /><a href="#" class="upload_another_video button-secondary">Upload Another</a>';
     // print_r($status);
     $htmlBody .= '</ul>';
     // testing returned data from
     // YouTube API
     /******************************/
     // $htmlBody .= 'The Privacy Setting Is ...... '. $_POST['video-privacy-settings'];
 } catch (Google_ServiceException $e) {