function sp_ev_update_videos($authors) { $current_videos = sp_ev_get_all_videos($authors); if (!$current_videos) { return 0; } // save new videos & determine list of all current video_ids $num_videos = 0; $video_ids = array(); foreach ($current_videos as $video) { array_push($video_ids, $video['video_id']); $is_new = sp_ev_save_video($video); if ($is_new) { $num_videos++; } } // remove deleted videos $del_videos = 0; $all_videos = new WP_Query(array('post_type' => 'external-videos', 'nopaging' => 1)); while ($all_videos->have_posts()) { $old_video = $all_videos->next_post(); $video_id = get_post_meta($old_video->ID, 'video_id', true); if (!in_array($video_id, $video_ids)) { wp_delete_post($old_video->ID, false); $del_videos += 1; } } if ($del_videos > 0) { echo sprintf(_n('Note: %d video was deleted on external host and thus removed from this collection.', 'Note: %d videos were deleted on external host and thus removed from this collection.', $del_videos, 'external-videos'), $del_videos); } return $num_videos; }
function sp_ev_update_videos($authors, $delete) { $current_videos = sp_ev_get_all_videos($authors); if (!$current_videos) { return 0; } // save new videos & determine list of all current video_ids $num_videos = 0; $video_ids = array(); foreach ($current_videos as $video) { array_push($video_ids, $video['video_id']); $is_new = sp_ev_save_video($video); if ($is_new) { $num_videos++; } } // want to delete externally deleted videos? if (!$delete) { return $num_videos; } // remove deleted videos $del_videos = 0; $all_videos = new WP_Query(array('post_type' => 'external-videos', 'nopaging' => 1)); while ($all_videos->have_posts()) { $old_video = $all_videos->next_post(); $video_id = get_post_meta($old_video->ID, 'video_id', true); if ($video_id != NULL && !in_array($video_id, $video_ids)) { $post = get_post($query_video->ID); $post->post_status = 'trash'; wp_update_post($post); // WP thinks it can just delete it and not move it to trash // if below worked, it could replace above three lines of code //wp_delete_post($old_video->ID, false); $del_videos += 1; } } if ($del_videos > 0) { echo sprintf(_n('Note: %d video was deleted on external host and moved to trash in this collection.', 'Note: %d videos were deleted on external host and moved to trash in this collection.', $del_videos, 'external-videos'), $del_videos); } return $num_videos; }