post_playlist

最后更新于:2021-11-27 17:11:20

apply_filters( ‘post_playlist’, string $output, array $attr, int $instance )

Filters the playlist output.

参数

$output

(string)
Playlist output. Default empty.

$attr

(array)
An array of shortcode attributes.

$instance

(int)
Unique numeric ID of this playlist shortcode instance.

源文件

文件: gc-includes/media.php

View on Trac

/**
 * Passing a non-empty value to the filter will short-circuit the default output.
 *
 * @param string $output   Playlist output. Default empty.
 * @param array  $attr     An array of shortcode attributes.
 * @param int    $instance Unique numeric ID of this playlist shortcode instance.
 */
function gcdocs_mytheme_custom_playlist( $output, $attr, $instance ) {

	if ( 'false' == $attr['images'] || 'video' == $attr['type'] ) {
		return;
	}

	$cover_size = $attr['cover_size'] || 'thumbnail';
	$post = get_post();

	if ( ! empty( $attr['ids'] ) ) {
		if ( empty( $attr['orderby'] ) ) {
			$attr['orderby'] = 'post__in';
		}
		$attr['include'] = $attr['ids'];
	}

	$atts = shortcode_atts(
		array(
			'type'         => 'audio',
			'order'        => 'ASC',
			'orderby'      => 'menu_order ID',
			'id'           => $post ? $post->ID : 0,
			'include'      => '',
			'exclude'      => '',
			'style'        => 'light',
			'tracklist'    => true,
			'tracknumbers' => true,
			'images'       => true,
			'artists'      => true,
		),
		$attr,
		'playlist'
	);

	$id = intval( $atts['id'] );

	$args = array(
		'post_status'    => 'inherit',
		'post_type'      => 'attachment',
		'post_mime_type' => $atts['type'],
		'order'          => $atts['order'],
		'orderby'        => $atts['orderby'],
	);

	if ( ! empty( $atts['include'] ) ) {
		$args['include'] = $atts['include'];
		$_attachments    = get_posts( $args );

		$attachments = array();
		foreach ( $_attachments as $key => $val ) {
			$attachments[ $val->ID ] = $_attachments[ $key ];
		}
	} elseif ( ! empty( $atts['exclude'] ) ) {
		$args['post_parent'] = $id;
		$args['exclude']     = $atts['exclude'];
		$attachments         = get_children( $args );
	} else {
		$args['post_parent'] = $id;
		$attachments         = get_children( $args );
	}

	if ( empty( $attachments ) ) {
		return '';
	}

	if ( is_feed() ) {
		$output = "n";
		foreach ( $attachments as $att_id => $attachment ) {
			$output .= gc_get_attachment_link( $att_id ) . "n";
		}
		return $output;
	}

	$data = array(
		'type'         => $atts['type'],
		'tracklist'    => gc_validate_boolean( $atts['tracklist'] ),
		'tracknumbers' => gc_validate_boolean( $atts['tracknumbers'] ),
		'images'       => gc_validate_boolean( $atts['images'] ),
		'artists'      => gc_validate_boolean( $atts['artists'] ),
	);

	$tracks = array();
	foreach ( $attachments as $attachment ) {
		$url   = gc_get_attachment_url( $attachment->ID );
		$ftype = gc_check_filetype( $url, gc_get_mime_types() );
		$track = array(
			'src'         => $url,
			'type'        => $ftype['type'],
			'title'       => $attachment->post_title,
			'caption'     => $attachment->post_excerpt,
			'description' => $attachment->post_content,
		);

		$track['meta'] = array();
		$meta          = gc_get_attachment_metadata( $attachment->ID );
		if ( ! empty( $meta ) ) {
			foreach ( gc_get_attachment_id3_keys( $attachment ) as $key => $label ) {
				if ( ! empty( $meta[ $key ] ) ) {
					$track['meta'][ $key ] = $meta[ $key ];
				}
			}
		}

		if ( $atts['images'] ) {
			$thumb_id = get_post_thumbnail_id( $attachment->ID );
			if ( ! empty( $thumb_id ) ) {
				list( $src, $width, $height ) = gc_get_attachment_image_src( $thumb_id, 'full' );
				$track['image']               = compact( 'src', 'width', 'height' );
				// use our custom size
				list( $src, $width, $height ) = gc_get_attachment_image_src( $thumb_id, $cover_size );
				$track['thumb']               = compact( 'src', 'width', 'height' );
			} else {
				$src            = gc_mime_type_icon( $attachment->ID );
				$width          = 48;
				$height         = 64;
				$track['image'] = compact( 'src', 'width', 'height' );
				$track['thumb'] = compact( 'src', 'width', 'height' );
			}
		}

		$tracks[] = $track;
	}
	$data['tracks'] = $tracks;

	$safe_type  = esc_attr( $atts['type'] );
	$safe_style = esc_attr( $atts['style'] );

	// begin output
	ob_start();

	if ( 1 === $instance ) {
		// Prints and enqueues playlist scripts, styles, and JavaScript templates.
		do_action( 'gc_playlist_scripts', $atts['type'], $atts['style'] );
	}
	?>
	<div class="gc-playlist gc-<?php echo $safe_type; ?>-playlist gc-playlist-<?php echo $safe_style; ?>">
		<?php if ( 'audio' === $atts['type'] ) : ?>
		<div class="gc-playlist-current-item"></div>
		<?php endif ?>
		<<?php echo $safe_type; ?> controls="controls" preload="none"></<?php echo $safe_type; ?>>
		<div class="gc-playlist-next"></div>
		<div class="gc-playlist-prev"></div>
		<noscript>
		<ol>
		<?php
		foreach ( $attachments as $att_id => $attachment ) {
			printf( '<li>%s</li>', gc_get_attachment_link( $att_id ) );
		}
		?>
		</ol>
		</noscript>
		<script type="application/json" class="gc-playlist-script"><?php echo gc_json_encode( $data ); ?></script>
	</div>
	<?php
	return ob_get_clean();
}
add_filter( 'post_playlist', 'mytheme_custom_playlist', 10, 3 );