(.+?):s'; /** * The ID of individual playlist. * * @var int */ public static $playlist_id = 0; /** * The removed shortcode callback. * * @var callable */ public $removed_shortcode_callback; /** * Registers the playlist shortcode. * * @global array $shortcode_tags * @return void */ public function register_embed() { global $shortcode_tags; if ( shortcode_exists( self::SHORTCODE ) ) { $this->removed_shortcode_callback = $shortcode_tags[ self::SHORTCODE ]; } add_shortcode( self::SHORTCODE, [ $this, 'shortcode' ] ); remove_action( 'wp_playlist_scripts', 'wp_playlist_scripts' ); } /** * Unregisters the playlist shortcode. * * @return void */ public function unregister_embed() { if ( isset( $this->removed_shortcode_callback ) ) { add_shortcode( self::SHORTCODE, $this->removed_shortcode_callback ); unset( $this->removed_shortcode_callback ); } add_action( 'wp_playlist_scripts', 'wp_playlist_scripts' ); } /** * Enqueues the playlist styling. * * @return void */ public function enqueue_styles() { wp_enqueue_style( 'amp-playlist-shortcode', amp_get_asset_url( 'css/amp-playlist-shortcode.css' ), [ 'wp-mediaelement' ], AMP__VERSION ); wp_styles()->add_data( 'amp-playlist-shortcode', 'rtl', 'replace' ); } /** * Gets AMP-compliant markup for the playlist shortcode. * * Uses the JSON that wp_playlist_shortcode() produces. * Gets the markup, based on the type of playlist. * * @param array $attr The playlist attributes. * @return string Playlist shortcode markup. */ public function shortcode( $attr ) { $data = $this->get_data( $attr ); if ( isset( $data['type'] ) && ( 'audio' === $data['type'] ) ) { return $this->audio_playlist( $data ); } if ( isset( $data['type'] ) && ( 'video' === $data['type'] ) ) { return $this->video_playlist( $data ); } return ''; } /** * Gets an AMP-compliant audio playlist. * * @param array $data Data. * @return string Playlist shortcode markup, or an empty string. */ public function audio_playlist( $data ) { if ( ! isset( $data['tracks'] ) ) { return ''; } self::$playlist_id++; $container_id = 'wpPlaylist' . self::$playlist_id . 'Carousel'; $state_id = 'wpPlaylist' . self::$playlist_id; $amp_state = [ 'selectedIndex' => 0, ]; $this->enqueue_styles(); ob_start(); ?>
get_title( $track ); $image_url = isset( $track['thumb']['src'] ) ? $track['thumb']['src'] : ''; $dimensions = $this->get_thumb_dimensions( $track ); ?>
print_tracks( $state_id, $data['tracks'] ); ?>
0, ]; foreach ( $data['tracks'] as $index => $track ) { $amp_state[ $index ] = [ 'videoUrl' => $track['src'], 'thumb' => isset( $track['thumb']['src'] ) ? $track['thumb']['src'] : '', ]; } $dimensions = isset( $data['tracks'][0]['dimensions']['resized'] ) ? $data['tracks'][0]['dimensions']['resized'] : null; $width = isset( $dimensions['width'] ) ? $dimensions['width'] : $content_width; $height = isset( $dimensions['height'] ) ? $dimensions['height'] : null; $src_bound = sprintf( '%s[%s.selectedIndex].videoUrl', $state_id, $state_id ); $this->enqueue_styles(); ob_start(); ?>
print_tracks( $state_id, $data['tracks'] ); ?>
self::THUMB_MAX_WIDTH ) { $ratio = $original_width / self::THUMB_MAX_WIDTH; $height = (int) ( $original_height / $ratio ); } else { $height = $original_height; } $width = min( self::THUMB_MAX_WIDTH, $original_width ); return compact( 'height', 'width' ); } /** * Outputs the playlist tracks, based on the type of playlist. * * These typically appear below the player. * Clicking a track triggers the player to appear with its src. * * @param string $state_id The ID of the container. * @param array $tracks Tracks. * @return void */ public function print_tracks( $state_id, $tracks ) { ?>
$track ) : ?> [ 'selectedIndex' => $index ] ] ) . ')'; $initial_class = 0 === $index ? 'wp-playlist-item wp-playlist-playing' : 'wp-playlist-item'; $bound_class = sprintf( '%d == %s.selectedIndex ? "wp-playlist-item wp-playlist-playing" : "wp-playlist-item"', $index, $state_id ); ?>
get_title( $track ) ); ?>