C:\xampp\htdocs\landing\wp-content\plugins\amp\includes\embeds\class-amp-playlist-embed-handler.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<?php
/**
 * Class AMP_Playlist_Embed_Handler
 *
 * @package AMP
 * @since 0.7
 */

/**
 * Class AMP_Playlist_Embed_Handler
 *
 * Creates AMP-compatible markup for the WordPress 'playlist' shortcode.
 *
 * @package AMP
 * @internal
 */
class AMP_Playlist_Embed_Handler extends AMP_Base_Embed_Handler {

    
/**
     * The tag of the shortcode.
     *
     * @var string
     */
    
const SHORTCODE 'playlist';

    
/**
     * The default height of the thumbnail image for 'audio' playlist tracks.
     *
     * @var int
     */
    
const DEFAULT_THUMB_HEIGHT 64;

    
/**
     * The default width of the thumbnail image for 'audio' playlist tracks.
     *
     * @var int
     */
    
const DEFAULT_THUMB_WIDTH 48;

    
/**
     * The max width of the audio thumbnail image.
     *
     * This corresponds to the max-width in wp-mediaelement.css:
     * .wp-playlist .wp-playlist-current-item img
     *
     * @var int
     */
    
const THUMB_MAX_WIDTH 60;

    
/**
     * The height of the carousel.
     *
     * @var int
     */
    
const CAROUSEL_HEIGHT 160;

    
/**
     * The pattern to get the playlist data.
     *
     * @var string
     */
    
const PLAYLIST_REGEX ':<script type="application/json" class="wp-playlist-script">(.+?)</script>: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_existsself::SHORTCODE ) ) {
            
$this->removed_shortcode_callback $shortcode_tagsself::SHORTCODE ];
        }
        
add_shortcodeself::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_shortcodeself::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();
        
?>
        <div class="wp-playlist wp-audio-playlist wp-playlist-light">
            <amp-state id="<?php echo esc_attr$state_id ); ?>">
                <script type="application/json"><?php echo wp_json_encode$amp_state ); ?></script>
            </amp-state>
            <amp-carousel id="<?php echo esc_attr$container_id ); ?>" [slide]="<?php echo esc_attr$state_id '.selectedIndex' ); ?>" height="<?php echo esc_attrself::CAROUSEL_HEIGHT ); ?>" width="auto" type="slides">
                <?php
                
foreach ( $data['tracks'] as $track ) :
                    
$title      $this->get_title$track );
                    
$image_url  = isset( $track['thumb']['src'] ) ? $track['thumb']['src'] : '';
                    
$dimensions $this->get_thumb_dimensions$track );
                    
?>
                    <div>
                        <div class="wp-playlist-current-item">
                            <?php if ( $image_url ) : ?>
                                <amp-img src="<?php echo esc_url$image_url ); ?>" height="<?php echo esc_attr$dimensions['height'] ); ?>" width="<?php echo esc_attr$dimensions['width'] ); ?>"></amp-img>
                            <?php endif; ?>
                            <div class="wp-playlist-caption">
                                <span class="wp-playlist-item-meta wp-playlist-item-title"><?php echo esc_html$title ); ?></span>
                            </div>
                        </div>
                        <amp-audio width="auto" height="50" src="<?php echo esc_url$track['src'] ); ?>"></amp-audio>
                    </div>
                <?php endforeach; ?>
            </amp-carousel>
            <?php $this->print_tracks$state_id$data['tracks'] ); ?>
        </div>
        <?php
        
return ob_get_clean();
    }

    
/**
     * Gets an AMP-compliant video playlist.
     *
     * This uses similar markup to the native playlist shortcode output.
     * So the styles from wp-mediaelement.min.css will apply to it.
     *
     * @global int $content_width
     * @param array $data Data.
     * @return string $video_playlist Markup for the video playlist.
     */
    
public function video_playlist$data ) {
        global 
$content_width;
        if ( ! isset( 
$data['tracks'][0]['src'] ) ) {
            return 
'';
        }
        
self::$playlist_id++;
        
$state_id  'wpPlaylist' self::$playlist_id;
        
$amp_state = [
            
'selectedIndex' => 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();
        
?>
        <div class="wp-playlist wp-video-playlist wp-playlist-light">
            <amp-state id="<?php echo esc_attr$state_id ); ?>">
                <script type="application/json"><?php echo wp_json_encode$amp_state ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></script>
            </amp-state>
            <amp-video id="amp-video" src="<?php echo esc_url$data['tracks'][0]['src'] ); ?>" [src]="<?php echo esc_attr$src_bound ); ?>" width="<?php echo esc_attr$width ); ?>" height="<?php echo esc_attr$height ); ?>" controls></amp-video>
            <?php $this->print_tracks$state_id$data['tracks'] ); ?>
        </div>
        <?php
        
return ob_get_clean(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
    
}

    
/**
     * Gets the thumbnail image dimensions, including height and width.
     *
     * If the width is higher than the maximum width,
     * reduces it to the maximum width.
     * And it proportionally reduces the height.
     *
     * @param array $track The data for the track.
     * @return array {
     *     Dimensions.
     *
     *     @type int $height Image height.
     *     @type int $width  Image width.
     * }
     */
    
public function get_thumb_dimensions$track ) {
        
$original_height = isset( $track['thumb']['height'] ) ? (int) $track['thumb']['height'] : self::DEFAULT_THUMB_HEIGHT;
        
$original_width  = isset( $track['thumb']['width'] ) ? (int) $track['thumb']['width'] : self::DEFAULT_THUMB_WIDTH;
        if ( 
$original_width self::THUMB_MAX_WIDTH ) {
            
$ratio  $original_width self::THUMB_MAX_WIDTH;
            
$height = (int) ( $original_height $ratio );
        } else {
            
$height $original_height;
        }
        
$width minself::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 ) {
        
?>
        <div class="wp-playlist-tracks">
            <?php foreach ( $tracks as $index => $track ) : ?>
                <?php
                $on            
'tap:AMP.setState(' wp_json_encode( [ $state_id => [ 'selectedIndex' => $index ] ] ) . ')';
                
$initial_class === $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 );
                
?>
                <div class="<?php echo esc_attr$initial_class ); ?>" [class]="<?php echo esc_attr$bound_class ); ?>" >
                    <a class="wp-playlist-caption" on="<?php echo esc_attr$on ); ?>">
                        <?php echo esc_html( ( $index ) . '.' ); ?> <span class="wp-playlist-item-title"><?php echo esc_html$this->get_title$track ) ); ?></span>
                    </a>
                    <?php if ( isset( $track['meta']['length_formatted'] ) ) : ?>
                        <div class="wp-playlist-item-length"><?php echo esc_html$track['meta']['length_formatted'] ); ?></div>
                    <?php endif; ?>
                </div>
            <?php endforeach; ?>
        </div>
        <?php
    
}

    
/**
     * Gets the data for the playlist.
     *
     * @see wp_playlist_shortcode()
     * @param array $attr The shortcode attributes.
     * @return array $data The data for the playlist.
     */
    
public function get_data$attr ) {
        
$markup wp_playlist_shortcode$attr );
        
preg_matchself::PLAYLIST_REGEX$markup$matches );
        if ( empty( 
$matches[1] ) ) {
            return [];
        }
        return 
json_decode$matches[1], true );
    }

    
/**
     * Gets the title for the track.
     *
     * @param array $track The track data.
     * @return string $title The title of the track.
     */
    
public function get_title$track ) {
        if ( ! empty( 
$track['caption'] ) ) {
            return 
$track['caption'];
        }

        if ( ! empty( 
$track['title'] ) ) {
            return 
$track['title'];
        }

        return 
'';
    }
}
x

Windows NT KPTV 6.2 build 9200 (Windows Server 2012 Datacenter Edition) i586