forked from wp-plugins/powerpress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
powerpress-playlist.php
522 lines (454 loc) · 17.5 KB
/
powerpress-playlist.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
<?php
// powerpress-playlist.php
function powerpress_get_term_by_ttid($ttid, $output = OBJECT, $filter = 'raw')
{
global $wpdb;
$value = intval($ttid);
$field = 'tt.term_taxonomy_id';
$term = $wpdb->get_row( $wpdb->prepare( "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE $field = %s LIMIT 1", $value) );
if ( !$term )
return false;
$taxonomy = $term->taxonomy;
wp_cache_add($term->term_id, $term, $taxonomy);
/** This filter is documented in wp-includes/taxonomy.php */
$term = apply_filters( 'get_term', $term, $taxonomy );
/** This filter is documented in wp-includes/taxonomy.php */
$term = apply_filters( "get_$taxonomy", $term, $taxonomy );
$term = sanitize_term($term, $taxonomy, $filter);
if ( $output == OBJECT ) {
return $term;
} elseif ( $output == ARRAY_A ) {
return get_object_vars($term);
} elseif ( $output == ARRAY_N ) {
return array_values(get_object_vars($term));
} else {
return $term;
}
}
function powepress_get_program_title_by_term_taxonomy_id($ttid)
{
$FeedSettings = powerpress_get_settings('powerpress_taxonomy_'.$ttid);
if( !empty($FeedSettings['title']) )
return $FeedSettings['title'];
return;
}
function powerpress_get_program_title_by_taxonomy($term_id, $taxonomy = 'category')
{
$General = get_option('powerpress_general');
// Efficiently get the taxonomy program titles from WordPress
if( !isset($GLOBALS['powerpress'][$taxonomy]) )
{
$GLOBALS['powerpress'][$taxonomy] = array();
// SELECT all the caegory podcasting / taxonomy podasting feeds...
if( $taxonomy == 'category' && isset($General['custom_cat_feeds']) )
{
$Feeds = $General['custom_cat_feeds'];
// Walk through the categories...
while( list($null, $cat_id) = each($Feeds) )
{
$FeedSettings = get_option('powerpress_cat_feed_'.$cat_id);
if( !empty($FeedSettings['title']) )
$GLOBALS['powerpress'][$taxonomy][ $cat_id ] = $FeedSettings['title'];
}
}
else
{
$PowerPressTaxonomies = get_option('powerpress_taxonomy_podcasting');
if( !empty($PowerPressTaxonomies) )
{
$query_in = '';
while( list($tt_id, $null) = each($PowerPressTaxonomies) )
{
if( !empty($query_in) )
$query_in .= ',';
$query_in .= $tt_id;
}
if( !empty($query_in) )
{
$terms = $wpdb->get_results("SELECT term_taxonomy_id, term_id, taxonomy FROM {$wpdb->term_taxonomy} WHERE term_taxonomy_id IN ($query_in)", ARRAY_A);
while( list($index,$term_info) = each($terms) )
{
// TODO: We need to get term by the term_id and taxonomy
$FeedSettings = powerpress_get_settings('powerpress_taxonomy_'.$term_info['term_taxonomy_id']);
if( !empty($FeedSettings['title']) )
$GLOBALS['powerpress'][ $term_info['taxonomy'] ][ $term_info['term_id'] ] = $FeedSettings['title'];
}
}
}
}
}
if( !empty($GLOBALS['powerpress'][$taxonomy][$term_id]) )
return $GLOBALS['powerpress'][$taxonomy][$term_id];
if( !empty($General['program_title']) )
return $General['program_title']; // Get the default podcast title
return ''; // REturn the blog title last resort
}
function powerpress_get_title_by_post_type($post_type, $slug='podcast')
{
}
function powerpress_playlist_episodes($args)
{
global $wpdb;
$return = array();
$defaults = array(
'limit' => 10,
'slug' => 'podcast',
'post_type'=>'post',
'category'=>'',
'taxonomy'=>'',
'tax_term'=>'',
'term_taxonomy_id'=>''
);
$args = wp_parse_args( $args, $defaults );
/* handle taxonomy and post id's*/
if( empty($args['taxonomy']) && empty($args['tax_term']) && !empty($args['category']) )
{
$args['taxonomy'] = 'category';
$args['tax_term'] = $args['category'];
}
$TaxonomyObj = false;
if( !empty($args['term_taxonomy_id']) )
{
$TaxonomyObj = powerpress_get_term_by_ttid( $args['term_taxonomy_id'] );
}
if( empty($TaxonomyObj) && !empty($args['taxonomy']) && !empty($args['tax_term']) )
{
if( preg_match('/^[0-9]*$/', $args['tax_term']) ) // If it is a numeric ID, lets try finding it by ID first...
$TaxonomyObj = get_term_by('id', $args['tax_term'], $args['taxonomy']);
if( empty($TaxonomyObj) )
$TaxonomyObj = get_term_by('name', $args['tax_term'], $args['taxonomy']);
if( empty($TaxonomyObj) )
$TaxonomyObj = get_term_by('slug', $args['tax_term'], $args['taxonomy']);
}
// Start the SQL query
$query = "SELECT p.ID, p.post_title, p.post_date, pm.meta_value ";
$query .= "FROM {$wpdb->posts} AS p ";
$query .= "INNER JOIN {$wpdb->postmeta} AS pm ON p.ID = pm.post_id ";
if( !empty($TaxonomyObj->term_taxonomy_id) )
$query .= "INNER JOIN {$wpdb->term_relationships} AS tr ON p.ID = tr.object_id ";
$query .= "WHERE (pm.meta_key = %s) ";
$query .= "AND p.post_type = %s ";
$query .= "AND p.post_status = 'publish' ";
if( !empty($TaxonomyObj->term_taxonomy_id) )
$query .= "AND tr.term_taxonomy_id = '". $TaxonomyObj->term_taxonomy_id ."' ";
$query .= "GROUP BY p.ID ";
$query .= "ORDER BY p.post_date DESC ";
$query .= "LIMIT 0, %d";
$query = $wpdb->prepare($query, ($args['slug'] == 'podcast'?'enclosure': '_'.$args['slug'].':enclosure'), $args['post_type'], $args['limit'] );
$results_data = $wpdb->get_results($query, ARRAY_A);
if( $results_data )
{
while( list($null,$row) = each($results_data) )
{
if( empty($row['meta_value']) )
continue;
$EnclosureData = powerpress_get_enclosure_data($row['ID'], $args['slug'], $row['meta_value']);
$return[ $row['ID'] ] = array();
$return[ $row['ID'] ]['ID'] = $row['ID'];
$return[ $row['ID'] ]['post_title'] = $row['post_title'];
$return[ $row['ID'] ]['post_date'] = $row['post_date'];
$return[ $row['ID'] ]['enclosure'] = $EnclosureData;
}
}
return $return;
}
/**
* Output the templates used by playlists.
*
*/
function powerpress_underscore_playlist_templates() {
?>
<script type="text/html" id="tmpl-wp-playlist-current-item">
<# if ( data.poster ) { #>
<img src="{{ data.poster.src }}" />
<# } #>
<div class="wp-playlist-caption">
<# if ( data.meta.program_title ) { #>
<span class="wp-playlist-item-meta wp-playlist-item-title">{{ data.meta.program_title }}</span>
<# } #>
<# if ( data.meta.link ) { #>
<span class="wp-playlist-item-meta wp-playlist-item-title"><a href="{{ data.meta.link }}" rel="nofollow">{{ data.title }}</a></span>
<# } else { #>
<span class="wp-playlist-item-meta wp-playlist-item-title">{{ data.title }}</span>
<# } #>
<# if ( data.meta.date ) { #><span class="wp-playlist-item-meta wp-playlist-item-artist">{{ data.meta.date }}</span><# } #>
<# if ( data.meta.itunes_subtitle ) { #><span class="wp-playlist-item-meta wp-playlist-item-album">{{ data.meta.itunes_subtitle }}</span><# } #>
</div>
</script>
<script type="text/html" id="tmpl-wp-playlist-item">
<div class="wp-playlist-item">
<a class="wp-playlist-caption" href="{{ data.src }}">
<!-- <span class="wp-playlist-item-title">“{{{ data.title }}}”</span> -->
<span class="wp-playlist-item-title">{{ data.title }}</span>
<# if ( data.meta.date ) { #>
<span class="wp-playlist-item-artist"> — {{ data.meta.date }}</span>
<# } #>
</a>
<# if ( data.meta.length_formatted ) { #>
<div class="wp-playlist-item-length">{{ data.meta.length_formatted }}</div>
<# } #>
</div>
</script>
<?php
}
/**
* Output and enqueue default scripts and styles for playlists.
*
* @since 6.0
*
* @param string $type Type of playlist. Accepts 'audio' or 'video'.
*/
function powerpress_playlist_scripts( $type ) {
wp_enqueue_style( 'wp-mediaelement' ); // Use the playlist built into WordPress
wp_enqueue_script( 'wp-playlist' ); // Use the playlist built into WordPress
?>
<!--[if lt IE 9]><script>document.createElement('<?php echo esc_js( $type ) ?>');</script><![endif]-->
<?php
add_action( 'wp_footer', 'powerpress_underscore_playlist_templates', 0 );
add_action( 'admin_footer', 'powerpress_underscore_playlist_templates', 0 );
}
add_action( 'powerpress_playlist_scripts', 'powerpress_playlist_scripts' );
/**
* The playlist shortcode.
*
* This implements the functionality of the playlist shortcode for displaying
* a collection of podcast episodes in a post.
*
* @since 6.0
*
* @param array $attr Playlist shortcode attributes.
* @return string Playlist output. Empty string if the passed type is unsupported.
*/
function powerpress_playlist_shortcode( $attr ) {
global $content_width;
if ( is_feed() ) {
return '';
}
static $instance = 0;
$instance++;
/*
if ( ! empty( $attr['ids'] ) ) {
// 'ids' is explicitly ordered, unless you specify otherwise.
if ( empty( $attr['orderby'] ) ) {
$attr['orderby'] = 'post__in';
}
$attr['include'] = $attr['ids'];
}
*/
extract( shortcode_atts( array(
'type' => 'audio', // Already defined by the first episode type
'style' => 'light', /* */
'tracklist' => true, /* always true */
'tracknumbers' => true, /* always false for podcasting */
'images' => true, // Used for PowerPress Playlist
'image' => '', // Used for PowerPress Playlist (specific image URL for default poster artwork
'artists' => true, // display the artist / author / talent name (Future use)
'itunes_subtitle'=>false,
'category'=>'', // Used for PowerPress Playlist (specify category ID, name or slug)
'term_id'=>'', // Used for PowerPress Playlist (specify term ID, name or slug)
'taxonomy'=>'', // Used for PowerPress Playlist (specify taxonomy name)
'term_taxonomy_id'=>'', // Used for PowerPress Playlist (specify term_taxonomy_id)
'program_titles_by_taxonomy'=>'', // e.g. category
'date' => true, // Display the date
'title' => true, // Dislay the title of program
'links'=>true, // Link to episode page
'slug' => '', // Used for PowerPress Playlist
'feed' => '', // Used for PowerPress Playlist
'channel'=>'', // Used for PowerPress Playlist
'post_type' => 'post', // Used for PowerPress Playlist
'limit'=>10 // Used for PowerPress Playlist
), $attr, 'powerpressplaylist' ) );
if ( $type !== 'audio' ) {
$type = 'video';
}
$tracknumbers = false;
//$images = true;
$artists = true; // Program title
$images = filter_var( $images, FILTER_VALIDATE_BOOLEAN );
$links = filter_var( $links, FILTER_VALIDATE_BOOLEAN );
$itunes_subtitle = filter_var( $itunes_subtitle, FILTER_VALIDATE_BOOLEAN );
$date = filter_var( $date, FILTER_VALIDATE_BOOLEAN );
if( empty($slug) && !empty($feed) )
$slug = $feed;
if( empty($slug) && !empty($channel) )
$slug = $channel;
if( empty($slug) )
$slug = 'podcast';
$args = array(
'limit' => $limit,
'slug' => $slug,
'post_type'=>$post_type,
'category'=>$category,
'term_id'=>'',
'taxonomy'=>'',
'term_taxonomy_id'=>$term_taxonomy_id
);
$episodes = powerpress_playlist_episodes( $args );
if ( empty( $episodes ) ) {
return '';
}
$ProgramSettings = false;
// Get Podcast Settings...ssss
if( !empty($post_type) )
{
$PostTypeSettingsArray = get_option('powerpress_posttype_'.$post_type);
if( is_array($PostTypeSettingsArray[ $slug ] ) )
{
$ProgramSettings = $PostTypeSettingsArray[ $slug ];
}
}
if( !empty($slug) && !$ProgramSettings )
$ProgramSettings = get_option('powerpress_feed_'.$slug);
if( !empty($term_taxonomy_id) && !$ProgramSettings )
$ProgramSettings = get_option('powerpress_taxonomy_'.$term_taxonomy_id);
if( empty($ProgramSettings) )
$ProgramSettings = get_option('powerpress_general');
$outer = 22; // default padding and border of wrapper
$default_width = 640;
$default_height = 360;
$theme_width = empty( $content_width ) ? $default_width : ( $content_width - $outer );
$theme_height = empty( $content_width ) ? $default_height : round( ( $default_height * $theme_width ) / $default_width );
$data = compact( 'type' );
if( !empty($images) && empty($image) ) // If they specified images but did not specify a specific image in the shortcode...
{
if( !empty($ProgramSettings['rss2_image']) )
$image = $ProgramSettings['rss2_image'];
else if( !empty($ProgramSettings['itunes_image']) )
$image = $ProgramSettings['itunes_image'];
}
// don't pass strings to JSON, will be truthy in JS
// foreach ( array( 'tracklist', 'tracknumbers', 'images', 'artists' ) as $key ) {
foreach ( array( 'tracklist', 'tracknumbers', 'images', 'artists', 'date', 'itunes_subtitle' ) as $key ) {
$data[$key] = filter_var( $$key, FILTER_VALIDATE_BOOLEAN );
}
// Set a global poster image
if( !empty($image) ) {
$data['poster'] = array( 'src'=>$image, 'width'=>'144', 'height'=>'144' );
}
$tracks = array();
foreach ( $episodes as $episode ) {
//$url = wp_get_attachment_url( $attachment->ID );
$url = $episode['enclosure']['url'];
//$ftype = wp_check_filetype( $url, wp_get_mime_types() );
$track = array(
'src' => $url,
'type' => $episode['enclosure']['type'],
'title' => $episode['post_title'],
'caption' => $episode['post_title'],
'description' => $episode['post_title']
);
//$image = false;
$episode_image = $image;
if( $images && !empty($episode['enclosure']['image']) )
{
$episode_image = $episode['enclosure']['image'];
}
else if( $images && !empty($episode['enclosure']['itunes_image']) )
{
$episode_image = $episode['enclosure']['itunes_image'];
}
// enclosure
$track['meta'] = array();
$track['meta']['artist'] = ('Talent Name'); // TODO
$track['meta']['album'] = ('Podcast Title here'); // TODO
if( $program_titles_by_taxonomy )
{
$ObjectTerms = wp_get_object_terms( $episode['ID'], $program_titles_by_taxonomy);
if(!empty($ObjectTerms) && !is_wp_error( $ObjectTerms ) && count($ObjectTerms) == 1 )
{
$track['meta']['program_title'] = powerpress_get_program_title_by_taxonomy($ObjectTerms[0]->term_id, $program_titles_by_taxonomy);
}
}
$track['meta']['title'] = $episode['post_title'];
if( !empty($itunes_subtitle) && !empty($episode['enclosure']['subtitle']) )
$track['meta']['itunes_subtitle'] = $episode['enclosure']['subtitle'];
$track['meta']['genre'] = 'Podcast';
$track['meta']['year'] = mysql2date( 'Y', $episode['post_date'] ); // Episode year
if( !empty($date) )
$track['meta']['date'] = mysql2date( get_option( 'date_format' ), $episode['post_date'] );// Get episode date
$track['meta']['length_formatted'] = powerpress_readable_duration($episode['enclosure']['duration']); // $episode['enclosure']['duration'];
if( $track['meta']['length_formatted'] == '0:00' )
$track['meta']['length_formatted'] = ''; // Make it empty
if( !empty($links) )
$track['meta']['link'] = get_permalink( $episode['ID'] ); // 'http://www.google.com/';
if( $type == 'video' )
{
$track['dimensions'] = array(
'original' => array('width'=>'100%', 'height'=>'100%' ),
'resized' => array('width'=>'100%', 'height'=>'100%' )
);
}
/*
//$meta = wp_get_attachment_metadata( $attachment->ID );
$meta = false;
if ( ! empty( $meta ) ) {
foreach ( wp_get_attachment_id3_keys( $attachment ) as $key => $label ) {
if ( ! empty( $meta[ $key ] ) ) {
$track['meta'][ $key ] = $meta[ $key ];
}
}
if ( 'video' === $type ) {
if ( ! empty( $meta['width'] ) && ! empty( $meta['height'] ) ) {
$width = $meta['width'];
$height = $meta['height'];
$theme_height = round( ( $height * $theme_width ) / $width );
} else {
$width = $default_width;
$height = $default_height;
}
$track['dimensions'] = array(
'original' => compact( 'width', 'height' ),
'resized' => array(
'width' => $theme_width,
'height' => $theme_height
)
);
}
}
*/
if( !empty($episode_image) ) // !empty($image) )
{
$src = $episode_image;
$width = 144;
$height = 144;
if( $type == 'video' ) // image onlyl used for video
$track['image'] = array('src'=>$src, 'width'=>'100%', 'height'=>'100%');
//$track['thumb'] = compact( 'src', 'width', 'height' );
$track['poster'] = compact( 'src', 'width', 'height' );
}
$tracks[] = $track;
}
$data['tracks'] = $tracks;
$safe_type = esc_attr( $type );
$safe_style = esc_attr( $style );
ob_start();
if ( 1 === $instance ) {
do_action( 'powerpress_playlist_scripts', $type, $style );
} ?>
<div class="wp-playlist wp-<?php echo $safe_type ?>-playlist wp-playlist-<?php echo $safe_style ?>">
<?php if ( 'audio' === $type ): ?>
<div class="wp-playlist-current-item"></div>
<?php endif ?>
<<?php echo $safe_type ?> controls="controls" preload="none" width="<?php
echo (int) $theme_width;
?>"<?php if ( 'video' === $safe_type ):
echo ' height="', (int) $theme_height, '"';
endif; ?>></<?php echo $safe_type ?>>
<div class="wp-playlist-next"></div>
<div class="wp-playlist-prev"></div>
<noscript>
<ol><?php // TODO
//foreach ( $attachments as $att_id => $attachment ) {
// printf( '<li>%s</li>', wp_get_attachment_link( $att_id ) );
//}
?></ol>
</noscript>
<script type="application/json" class="wp-playlist-script"><?php echo json_encode( $data ) ?></script>
</div>
<?php
return ob_get_clean();
}
add_shortcode( 'podcastlist', 'powerpress_playlist_shortcode' );
add_shortcode( 'podcastplaylist', 'powerpress_playlist_shortcode' );
add_shortcode( 'powerpressplaylist', 'powerpress_playlist_shortcode' );
add_shortcode( 'powerpress_playlist', 'powerpress_playlist_shortcode' );