Last active
February 28, 2017 07:27
-
-
Save hannit/e87a7abe33a99d0fbf7ec01eab8b5e71 to your computer and use it in GitHub Desktop.
Import youtube playlist and videos as CPT - Needs optimization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Get all playlists in the channel | |
function bio_youtube_get_playlists() { | |
$lists = array(); | |
$key = 'Youtube-API-Key'; | |
$channel = 'Channel-ID'; | |
$url = "https://www.googleapis.com/youtube/v3/playlists?key=$key&channelId=$channel&part=snippet,id&order=date&maxResults=20"; | |
$data = file_get_contents($url); | |
if (!empty($data)) { | |
$data_struct = json_decode($data); | |
if (is_array($data_struct->items)) { | |
foreach ($data_struct->items as $list) { | |
$title = $list->snippet->title; | |
$id = $list->id; | |
$videos = bio_get_list_videos($id); | |
$lists[] = array('title' => $title, 'id' => $id, 'videos' => $videos); | |
} | |
} | |
} | |
return $lists; | |
} | |
// get all videos for playlist | |
function bio_get_list_videos($list_id) { | |
$videos = array(); | |
$key = 'Youtube-API-Key'; | |
$channel = 'Channel-ID'; | |
$url = "https://www.googleapis.com/youtube/v3/playlistItems?key=$key&playlistId=$list_id&part=snippet,id&maxResults=20"; | |
$data = file_get_contents($url); | |
$i = 0; | |
if (!empty($data)) { | |
$data_struct = json_decode($data); | |
if (is_array($data_struct->items)) { | |
foreach ($data_struct->items as $vid) { $i++; | |
$videos[$i]['title'] = $vid->snippet->title; | |
$videos[$i]['description'] = $vid->snippet->description; | |
$videos[$i]['thumb'] = $vid->snippet->thumbnails->medium->url; | |
$videos[$i]['id'] = $vid->snippet->resourceId->videoId; | |
} | |
} | |
} | |
return $videos; | |
} | |
function bio_import_videos() { | |
$playlist_tax = 'playlist'; | |
$video_cpt = 'video'; | |
$data = bio_youtube_get_playlists(); | |
foreach ($data as $pl) { | |
$term = term_exists($pl['title'], 'playlist'); | |
if (!$term) { | |
$term = wp_insert_term($pl['title'],'playlist', array()); | |
} | |
$tid = $term['term_id']; | |
foreach ($pl['videos'] as $vid) { | |
// check if already imported | |
if (get_page_by_title($vid['title'], 'OBJECT', $video_cpt )) | |
continue; | |
$my_post = array( | |
'post_title' => wp_strip_all_tags($vid['title']), | |
'post_content' => $vid['description'], | |
'post_status' => 'publish', | |
'post_author' => 1, | |
'post_type' => $video_cpt | |
); | |
$pid = wp_insert_post( $my_post ); | |
if (!is_wp_error($pid )) { | |
wp_set_post_terms( $pid, array($tid), $playlist_tax); | |
// exists in a different gist | |
import_featured_image($vid['thumb'], $vid['title'], $pid); | |
} | |
} | |
} | |
} | |
/* | |
* Run youtube import weekly | |
*/ | |
function custom_cron_job_recurrence( $schedules ) { | |
$schedules['weekly'] = array( | |
'display' => __( 'once weekly', 'bio' ), | |
'interval' => 604800, | |
); | |
return $schedules; | |
} | |
add_filter( 'cron_schedules', 'custom_cron_job_recurrence' ); | |
// Schedule Cron Job Event | |
function custom_cron_job() { | |
if ( ! wp_next_scheduled( 'import_youtube_videos' ) ) { | |
wp_schedule_event( current_time( 'timestamp' ), 'weekly', 'import_youtube_videos' ); | |
} | |
} | |
add_action( 'wp', 'custom_cron_job' ); | |
add_action('import_youtube_videos', 'bio_import_videos'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment