/**
  * New post load action for videos.
  * Will first display a form to query for the video.
  */
 public function post_new_onload()
 {
     if (!isset($_REQUEST['post_type']) || $this->post_type !== $_REQUEST['post_type']) {
         return;
     }
     global $CVM_NEW_VIDEO;
     if (isset($_POST['wp_nonce'])) {
         if (check_admin_referer('cvm_query_new_video', 'wp_nonce')) {
             if (empty($_POST['cvm_video_id'])) {
                 wp_redirect('post-new.php?post_type=' . $this->post_type);
                 die;
             }
             // search if video already exists
             $posts = get_posts(array('post_type' => $this->post_type, 'meta_key' => '__cvm_video_id', 'meta_value' => $_POST['cvm_video_id'], 'post_status' => array('publish', 'pending', 'draft', 'future', 'private')));
             if ($posts) {
                 wp_redirect('post.php?post=' . $posts[0]->ID . '&action=edit&message=102&video_id=' . $_POST['cvm_video_id']);
                 die;
             }
             $request = cvm_query_video($_POST['cvm_video_id']);
             if ($request && 200 == $request['response']['code']) {
                 $data = json_decode($request['body'], true);
                 $CVM_NEW_VIDEO = cvm_format_video_entry($data[0]);
                 add_filter('default_content', array($this, 'default_content'), 999, 2);
                 add_filter('default_title', array($this, 'default_title'), 999, 2);
                 add_filter('default_excerpt', array($this, 'default_excerpt'), 999, 2);
                 // add video player for video preview on post
                 cvm_enqueue_player();
             }
         } else {
             wp_die("Cheatin' uh?");
         }
     }
     // if vidoe query not started, display the form
     if (!$CVM_NEW_VIDEO) {
         wp_enqueue_script('cvm-new-video-js', CVM_URL . 'assets/back-end/js/video-new.js', array('jquery'), '1.0');
         $post_type_object = get_post_type_object($this->post_type);
         $title = $post_type_object->labels->add_new_item;
         include ABSPATH . 'wp-admin/admin-header.php';
         include CVM_PATH . 'views/new_video.php';
         include ABSPATH . 'wp-admin/admin-footer.php';
         die;
     }
 }
function cvm_output_playlist($videos = 'latest', $results = 5, $theme = 'default', $player_settings = array())
{
    global $CVM_POST_TYPE;
    $args = array('post_type' => $CVM_POST_TYPE->get_post_type(), 'posts_per_page' => absint($results), 'numberposts' => absint($results), 'post_status' => 'publish', 'supress_filters' => true);
    // if $videos is array, the function was called with an array of video ids
    if (is_array($videos)) {
        $ids = array();
        foreach ($videos as $video_id) {
            $ids[] = absint($video_id);
        }
        $args['include'] = $ids;
        $args['posts_per_page'] = count($ids);
        $args['numberposts'] = count($ids);
    } elseif (is_string($videos)) {
        $found = false;
        switch ($videos) {
            case 'latest':
                $args['orderby'] = 'post_date';
                $args['order'] = 'DESC';
                $found = true;
                break;
        }
        if (!$found) {
            return;
        }
    } else {
        // if $videos is anything else other than array or string, bail out
        return;
    }
    // get video posts
    $posts = get_posts($args);
    if (!$posts) {
        return;
    }
    $videos = array();
    foreach ($posts as $post_key => $post) {
        if (isset($ids)) {
            $key = array_search($post->ID, $ids);
        } else {
            $key = $post_key;
        }
        if (is_numeric($key)) {
            $videos[$key] = array('ID' => $post->ID, 'title' => $post->post_title, 'video_data' => get_post_meta($post->ID, '__cvm_video_data', true));
        }
    }
    ksort($videos);
    ob_start();
    // set custom player settings if any
    global $CVM_PLAYER_SETTINGS;
    if ($player_settings && is_array($player_settings)) {
        $defaults = cvm_get_player_settings();
        foreach ($defaults as $setting => $value) {
            if (isset($player_settings[$setting]) && !empty($player_settings[$setting])) {
                $defaults[$setting] = $player_settings[$setting];
            }
        }
        $CVM_PLAYER_SETTINGS = $defaults;
    }
    global $cvm_video;
    if (!array_key_exists($theme, cvm_playlist_themes())) {
        $theme = 'default';
    }
    $file = CVM_PATH . 'themes/' . $theme . '/player.php';
    if (!is_file($file)) {
        $theme = 'default';
    }
    include CVM_PATH . 'themes/' . $theme . '/player.php';
    $content = ob_get_contents();
    ob_end_clean();
    cvm_enqueue_player();
    wp_enqueue_script('cvm-vim-player-' . $theme, CVM_URL . 'themes/' . $theme . '/assets/script.js', array('cvm-video-player'), '1.0');
    wp_enqueue_style('cvm-vim-player-' . $theme, CVM_URL . 'themes/' . $theme . '/assets/stylesheet.css', false, '1.0');
    // remove custom player settings
    $CVM_PLAYER_SETTINGS = false;
    return $content;
}