示例#1
0
/**
 * Display a playlist.
 *
 * @since 1.0.0
 * @todo Add an arg to specify a template path that doesn't exist in the /cue directory.
 *
 * @param mixed $post A post ID, WP_Post object or post slug.
 * @param array $args
 */
function cue_playlist($post, $args = array())
{
    if (is_string($post) && !is_numeric($post)) {
        // Get a playlist by its slug.
        $post = get_page_by_path($post, OBJECT, 'cue_playlist');
    } else {
        $post = get_post($post);
    }
    if (!$post || 'cue_playlist' !== get_post_type($post)) {
        return;
    }
    $tracks = get_cue_playlist_tracks($post);
    if (empty($tracks)) {
        return;
    }
    if (!isset($args['enqueue']) || $args['enqueue']) {
        Cue::enqueue_assets();
    }
    $template_names = array("playlist-{$post->ID}.php", "playlist-{$post->post_name}.php", "playlist.php");
    // Prepend custom templates.
    if (!empty($args['template'])) {
        $add_templates = array_filter((array) $args['template']);
        $template_names = array_merge($add_templates, $template_names);
    }
    $template_loader = new Cue_Template_Loader();
    $template = $template_loader->locate_template($template_names);
    $classes = array('cue-playlist');
    $classes[] = isset($args['show_playlist']) && false == $args['show_playlist'] ? 'is-playlist-hidden' : '';
    $classes = implode(' ', array_filter($classes));
    echo '<div class="cue-playlist-container">';
    do_action('cue_before_playlist', $post, $tracks, $args);
    include $template;
    do_action('cue_after_playlist', $post, $tracks, $args);
    echo '</div>';
}