PluginProbe
PowerPress Podcasting plugin by Blubrry / trunk
PowerPress Podcasting plugin by Blubrry vtrunk
11.17.9 11.17.8 11.17.7 11.17.6 11.17.4 11.17.3 11.17.2 11.17.1 11.17 11.16.11 11.16.10 11.16.9 11.16.8 11.16.7 11.16.6 11.16.5 11.16.4 11.16.3 11.16.2 11.16.1 11.9.13 11.9.14 11.9.15 11.9.16 11.9.17 All 383 releases
powerpress / powerpress-playlist.php

powerpress-playlist.php in PowerPress Podcasting plugin by Blubrry trunk, at powerpress-playlist.php

552 lines 17.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // powerpress-playlist.php
3
4
5 function powerpress_get_term_by_ttid($ttid)
6 {
7 global $wpdb;
8 $ttid = intval($ttid);
9 $term_info = $wpdb->get_results("SELECT term_id, taxonomy FROM {$wpdb->term_taxonomy} WHERE term_taxonomy_id = {$ttid} LIMIT 1", ARRAY_A);
10 if( empty( $term_info[0]['term_id']) )
11 return false;
12
13 $term_ID = $term_info[0]['term_id'];
14 $taxonomy_type = $term_info[0]['taxonomy'];
15 return get_term_by('id', $term_ID, $taxonomy_type);
16 }
17
18 function powepress_get_program_title_by_term_taxonomy_id($ttid)
19 {
20 $FeedSettings = powerpress_get_settings('powerpress_taxonomy_'.$ttid);
21 if( !empty($FeedSettings['title']) )
22 return $FeedSettings['title'];
23 return;
24 }
25
26 function powerpress_get_program_title_by_taxonomy($term_id, $taxonomy = 'category')
27 {
28 $General = get_option('powerpress_general');
29 // Efficiently get the taxonomy program titles from WordPress
30 if( !isset($GLOBALS['powerpress'][$taxonomy]) )
31 {
32 $GLOBALS['powerpress'][$taxonomy] = array();
33
34 // SELECT all the caegory podcasting / taxonomy podasting feeds...
35
36 if( $taxonomy == 'category' && isset($General['custom_cat_feeds']) )
37 {
38 $Feeds = $General['custom_cat_feeds'];
39 // Walk through the categories...
40 foreach( $Feeds as $null=> $cat_id )
41 {
42 $FeedSettings = get_option('powerpress_cat_feed_'.$cat_id);
43 if( !empty($FeedSettings['title']) )
44 $GLOBALS['powerpress'][$taxonomy][ $cat_id ] = $FeedSettings['title'];
45 }
46 }
47 else
48 {
49 $PowerPressTaxonomies = get_option('powerpress_taxonomy_podcasting');
50 if( !empty($PowerPressTaxonomies) )
51 {
52 $query_in = '';
53 foreach( $PowerPressTaxonomies as $tt_id=> $null )
54 {
55 if( !empty($query_in) )
56 $query_in .= ',';
57 $query_in .= intval($tt_id); // sanitize for sql
58 }
59
60 if( !empty($query_in) )
61 {
62 $terms = $wpdb->get_results("SELECT term_taxonomy_id, term_id, taxonomy FROM {$wpdb->term_taxonomy} WHERE term_taxonomy_id IN ($query_in)", ARRAY_A);
63
64 foreach( $terms as $index=> $term_info )
65 {
66 // TODO: We need to get term by the term_id and taxonomy
67 $FeedSettings = powerpress_get_settings('powerpress_taxonomy_'.$term_info['term_taxonomy_id']);
68 if( !empty($FeedSettings['title']) )
69 $GLOBALS['powerpress'][ $term_info['taxonomy'] ][ $term_info['term_id'] ] = $FeedSettings['title'];
70 }
71 }
72 }
73 }
74 }
75
76 if( !empty($GLOBALS['powerpress'][$taxonomy][$term_id]) )
77 return $GLOBALS['powerpress'][$taxonomy][$term_id];
78 if( !empty($General['program_title']) )
79 return $General['program_title']; // Get the default podcast title
80 return ''; // REturn the blog title last resort
81 }
82
83 function powerpress_get_title_by_post_type($post_type, $slug='podcast')
84 {
85
86 }
87
88 function powerpress_playlist_episodes($args)
89 {
90 global $wpdb;
91 $return = array();
92
93 $defaults = array(
94 'limit' => 10,
95 'slug' => 'podcast',
96 'post_type'=>'post',
97 'category'=>'',
98 'taxonomy'=>'',
99 'tax_term'=>'',
100 'term_taxonomy_id'=>'',
101 'ids'=>'',
102 'order'=>''
103 );
104 $args = wp_parse_args( $args, $defaults );
105
106 /* handle taxonomy and post id's*/
107 if( empty($args['taxonomy']) && empty($args['tax_term']) && !empty($args['category']) )
108 {
109 $args['taxonomy'] = 'category';
110 $args['tax_term'] = $args['category'];
111 }
112
113 $TaxonomyObj = false;
114 if( !empty($args['term_taxonomy_id']) )
115 {
116 $TaxonomyObj = powerpress_get_term_by_ttid( $args['term_taxonomy_id'] );
117 }
118
119 if( empty($TaxonomyObj) && !empty($args['taxonomy']) && !empty($args['tax_term']) )
120 {
121 if( preg_match('/^[0-9]*$/', $args['tax_term']) ) // If it is a numeric ID, lets try finding it by ID first...
122 $TaxonomyObj = get_term_by('id', $args['tax_term'], $args['taxonomy']);
123 if( empty($TaxonomyObj) )
124 $TaxonomyObj = get_term_by('name', $args['tax_term'], $args['taxonomy']);
125 if( empty($TaxonomyObj) )
126 $TaxonomyObj = get_term_by('slug', $args['tax_term'], $args['taxonomy']);
127 }
128
129 // Start the SQL query
130 $query = "SELECT p.ID, p.post_title, p.post_date, pm.meta_value ";
131 $query .= "FROM {$wpdb->posts} AS p ";
132 $query .= apply_filters( 'powerpress_join', "INNER JOIN {$wpdb->postmeta} AS pm ON p.ID = pm.post_id " );
133
134 if( !empty($TaxonomyObj->term_taxonomy_id) )
135 $query .= "INNER JOIN {$wpdb->term_relationships} AS tr ON p.ID = tr.object_id ";
136
137 $query .= apply_filters( 'powerpress_where', "WHERE (pm.meta_key = %s) " );
138 $query .= "AND p.post_type = %s ";
139 $query .= "AND p.post_status = 'publish' ";
140 if( !empty($TaxonomyObj->term_taxonomy_id) ) {
141 $query .= "AND tr.term_taxonomy_id = '". $TaxonomyObj->term_taxonomy_id ."' ";
142 }
143 if ( apply_filters( 'wpml_setting', false, 'setup_complete' ) ) {
144 add_filter( 'powerpress_join', function( $join ) {
145 global $wpdb;
146
147 $join .= "JOIN {$wpdb->prefix}icl_translations ON element_type = CONCAT('post_', p.post_type) AND element_id = p.ID ";
148
149 return $join;
150 } );
151 add_filter( 'powerpress_where', function( $where ) {
152 $lang = apply_filters( 'wpml_current_language', false );
153 $where .= "AND language_code = '$lang' ";
154 $where .= "AND pm.meta_value NOT LIKE 'no%' ";
155
156 return $where;
157 } );
158 }
159
160 $for_query = '';
161 if( !empty( $args['ids'] ) ) {
162 // First santity check make sure we are only working with numbers....
163 if( preg_match('/^[0-9,\s]*$/', $args['ids']) ) {
164 $ids = explode(',', preg_replace('/(\s)/', '', $args['ids']) );
165 foreach( $ids as $index=> $id ) {
166 if( empty($id) )
167 continue;
168 if( !empty($for_query) )
169 $for_query .= ', ';
170 $for_query .= $id;
171 }
172
173 if( !empty($for_query) ) {
174 $query .= "AND p.ID IN ($for_query) ";
175 }
176 }
177 }
178
179 $query .= "GROUP BY p.ID ";
180 if( !empty($for_query) ) {
181 $query .= "ORDER BY FIELD('id', $for_query) ";
182 } else if ( !empty($args['order']) && strtolower($args['order']) == 'asc' ) {
183 $query .= "ORDER BY p.post_date ASC ";
184 } else {
185 $query .= "ORDER BY p.post_date DESC ";
186 }
187 $query .= "LIMIT 0, %d";
188
189 $query = $wpdb->prepare($query, ($args['slug'] == 'podcast'?'enclosure': '_'.$args['slug'].':enclosure'), $args['post_type'], $args['limit'] );
190 $results_data = $wpdb->get_results($query, ARRAY_A);
191 if( $results_data )
192 {
193 foreach( $results_data as $null=> $row )
194 {
195 if( empty($row['meta_value']) )
196 continue;
197
198 $EnclosureData = powerpress_get_enclosure_data($row['ID'], $args['slug'], $row['meta_value']);
199 $return[ $row['ID'] ] = array();
200 $return[ $row['ID'] ]['ID'] = $row['ID'];
201 $return[ $row['ID'] ]['post_title'] = $row['post_title'];
202 $return[ $row['ID'] ]['post_date'] = $row['post_date'];
203 $return[ $row['ID'] ]['enclosure'] = $EnclosureData;
204 }
205 }
206 return $return;
207 }
208
209 /**
210 * Output the templates used by playlists.
211 *
212 */
213 function powerpress_underscore_playlist_templates() {
214 ?>
215 <script type="text/html" id="tmpl-wp-playlist-current-item">
216 <# if ( data.poster ) { #>
217 <img src="{{ data.poster.src }}" />
218 <# } #>
219 <div class="wp-playlist-caption">
220 <# if ( data.meta.program_title ) { #>
221 <span class="wp-playlist-item-meta wp-playlist-item-title">{{ data.meta.program_title }}</span>
222 <# } #>
223 <# if ( data.meta.link ) { #>
224 <span class="wp-playlist-item-meta wp-playlist-item-title"><a href="{{ data.meta.link }}" rel="nofollow">{{ data.title }}</a></span>
225 <# } else if ( data.title ) { #>
226 <span class="wp-playlist-item-meta wp-playlist-item-title">{{ data.title }}</span>
227 <# } #>
228 <# if ( data.meta.date ) { #><span class="wp-playlist-item-meta wp-playlist-item-artist">{{ data.meta.date }}</span><# } #>
229 </div>
230 </script>
231 <script type="text/html" id="tmpl-wp-playlist-item">
232 <div class="wp-playlist-item">
233 <a class="wp-playlist-caption" href="{{ data.src }}">
234 <!-- <span class="wp-playlist-item-title">&#8220;{{{ data.title }}}&#8221;</span> -->
235 <# if ( data.title ) { #>
236 <span class="wp-playlist-item-title">{{ data.title }}</span>
237 <# } #>
238 <# if ( data.meta.date ) { #>
239 <span class="wp-playlist-item-artist"> &mdash; {{ data.meta.date }}</span>
240 <# } #>
241 </a>
242 <# if ( data.meta.length_formatted ) { #>
243 <div class="wp-playlist-item-length">{{ data.meta.length_formatted }}</div>
244 <# } #>
245 </div>
246 </script>
247 <?php
248 }
249
250 /**
251 * Output and enqueue default scripts and styles for playlists.
252 *
253 * @since 6.0
254 *
255 * @param string $type Type of playlist. Accepts 'audio' or 'video'.
256 */
257 function powerpress_playlist_scripts( $type ) {
258 wp_enqueue_style( 'wp-mediaelement' ); // Use the playlist built into WordPress
259 wp_enqueue_script( 'wp-playlist' ); // Use the playlist built into WordPress
260 ?>
261 <!--[if lt IE 9]><script>document.createElement('<?php echo esc_js( $type ) ?>');</script><![endif]-->
262 <?php
263 add_action( 'wp_footer', 'powerpress_underscore_playlist_templates', 0 );
264 add_action( 'admin_footer', 'powerpress_underscore_playlist_templates', 0 );
265 }
266 add_action( 'powerpress_playlist_scripts', 'powerpress_playlist_scripts' );
267
268 /**
269 * The playlist shortcode.
270 *
271 * This implements the functionality of the playlist shortcode for displaying
272 * a collection of podcast episodes in a post.
273 *
274 * @since 6.0
275 *
276 * @param array $attr Playlist shortcode attributes.
277 * @return string Playlist output. Empty string if the passed type is unsupported.
278 */
279 function powerpress_playlist_shortcode( $attr ) {
280 global $content_width;
281
282 if ( is_feed() ) {
283 return '';
284 }
285
286 static $instance = 0;
287 $instance++;
288
289 /*
290 if ( ! empty( $attr['ids'] ) ) {
291 // 'ids' is explicitly ordered, unless you specify otherwise.
292 if ( empty( $attr['orderby'] ) ) {
293 $attr['orderby'] = 'post__in';
294 }
295 $attr['include'] = $attr['ids'];
296 }
297 */
298
299
300 extract( shortcode_atts( array(
301 'type' => 'audio', // Already defined by the first episode type
302 'style' => 'light', /* */
303 'tracklist' => true, /* always true */
304 'tracknumbers' => true, /* always false for podcasting */
305 'images' => true, // Used for PowerPress Playlist
306 'image' => '', // Used for PowerPress Playlist (specific image URL for default poster artwork
307 'artists' => true, // display the artist / author / talent name (Future use)
308 'category'=>'', // Used for PowerPress Playlist (specify category ID, name or slug)
309 'term_id'=>'', // Used for PowerPress Playlist (specify term ID, name or slug)
310 'taxonomy'=>'', // Used for PowerPress Playlist (specify taxonomy name)
311 'term_taxonomy_id'=>'', // Used for PowerPress Playlist (specify term_taxonomy_id)
312 'program_titles_by_taxonomy'=>'', // e.g. category
313 'date' => true, // Display the date
314 'title' => true, // Dislay the title of episode
315 'links'=>true, // Link to episode page
316 'slug' => '', // Used for PowerPress Playlist
317 'feed' => '', // Used for PowerPress Playlist
318 'channel'=>'', // Used for PowerPress Playlist
319 'post_type' => 'post', // Used for PowerPress Playlist
320 'limit'=>10, // Used for PowerPress Playlist
321 'ids'=>'', // Used to specify specific post ids to assemble a player with specific episodes
322 'order'=>'' // Order of episodes, descending by default
323 ), $attr, 'powerpressplaylist' ) );
324
325
326 if ( $type !== 'audio' ) {
327 $type = 'video';
328 }
329
330
331 $tracknumbers = false;
332 //$images = true;
333 $artists = true; // Program title
334
335 $images = filter_var( $images, FILTER_VALIDATE_BOOLEAN );
336 $links = filter_var( $links, FILTER_VALIDATE_BOOLEAN );
337 $episode_title = filter_var( $title, FILTER_VALIDATE_BOOLEAN );
338 $date = filter_var( $date, FILTER_VALIDATE_BOOLEAN );
339
340 if( empty($slug) && !empty($feed) )
341 $slug = $feed;
342 if( empty($slug) && !empty($channel) )
343 $slug = $channel;
344 if( empty($slug) )
345 $slug = 'podcast';
346
347 $args = array(
348 'limit' => $limit,
349 'slug' => $slug,
350 'post_type'=>$post_type,
351 'category'=>$category,
352 'term_id'=>'',
353 'taxonomy'=>'',
354 'term_taxonomy_id'=>$term_taxonomy_id,
355 'ids'=>$ids,
356 'order'=>$order
357 );
358
359 $episodes = powerpress_playlist_episodes( $args );
360
361 if ( empty( $episodes ) ) {
362 return '';
363 }
364
365 $ProgramSettings = false;
366 // Get Podcast Settings...ssss
367 if( !empty($post_type) )
368 {
369 $PostTypeSettingsArray = get_option('powerpress_posttype_'.$post_type);
370 if( isset($PostTypeSettingsArray[ $slug ]) && is_array($PostTypeSettingsArray[ $slug ] ) )
371 {
372 $ProgramSettings = $PostTypeSettingsArray[ $slug ];
373 }
374 }
375 if( !empty($slug) && !$ProgramSettings )
376 $ProgramSettings = get_option('powerpress_feed_'.$slug);
377 if( !empty($term_taxonomy_id) && !$ProgramSettings )
378 $ProgramSettings = get_option('powerpress_taxonomy_'.$term_taxonomy_id);
379 if( empty($ProgramSettings) )
380 $ProgramSettings = get_option('powerpress_general');
381
382 $outer = 22; // default padding and border of wrapper
383
384 $default_width = 640;
385 $default_height = 360;
386
387 $theme_width = empty( $content_width ) ? $default_width : ( $content_width - $outer );
388 $theme_height = empty( $content_width ) ? $default_height : round( ( $default_height * $theme_width ) / $default_width );
389
390 $data = compact( 'type' );
391
392 if( !empty($images) && empty($image) ) // If they specified images but did not specify a specific image in the shortcode...
393 {
394 if( !empty($ProgramSettings['itunes_image']) )
395 $image = $ProgramSettings['itunes_image'];
396 }
397
398 // don't pass strings to JSON, will be truthy in JS
399 // foreach ( array( 'tracklist', 'tracknumbers', 'images', 'artists' ) as $key ) {
400 foreach ( array( 'tracklist', 'tracknumbers', 'images', 'artists', 'date' ) as $key ) {
401 $data[$key] = filter_var( $$key, FILTER_VALIDATE_BOOLEAN );
402 }
403
404 // Set a global poster image
405 if( !empty($image) ) {
406 $data['poster'] = array( 'src'=>$image, 'width'=>'144', 'height'=>'144' );
407 }
408
409 $tracks = array();
410 foreach ( $episodes as $episode ) {
411 //$url = wp_get_attachment_url( $attachment->ID );
412 $url = $episode['enclosure']['url'];
413 //$ftype = wp_check_filetype( $url, wp_get_mime_types() );
414 $track = array(
415 'src' => $url,
416 'type' => $episode['enclosure']['type'],
417 'title' => $episode['post_title'],
418 'caption' => $episode['post_title'],
419 'description' => $episode['post_title']
420 );
421
422 if( empty($episode_title) ) {
423 $track['title'] = '';
424 }
425
426 //$image = false;
427 $episode_image = $image;
428 if( $images && !empty($episode['enclosure']['image']) )
429 {
430 $episode_image = $episode['enclosure']['image'];
431 }
432 else if( $images && !empty($episode['enclosure']['itunes_image']) )
433 {
434 $episode_image = $episode['enclosure']['itunes_image'];
435 }
436 // enclosure
437
438 $track['meta'] = array();
439
440 $track['meta']['artist'] = ('Talent Name'); // TODO
441 $track['meta']['album'] = ('Podcast Title here'); // TODO
442 if( $program_titles_by_taxonomy )
443 {
444 $ObjectTerms = wp_get_object_terms( $episode['ID'], $program_titles_by_taxonomy);
445 if(!empty($ObjectTerms) && !is_wp_error( $ObjectTerms ) && count($ObjectTerms) == 1 )
446 {
447 $track['meta']['program_title'] = powerpress_get_program_title_by_taxonomy($ObjectTerms[0]->term_id, $program_titles_by_taxonomy);
448 }
449 }
450 if( !empty($episode_title) )
451 $track['meta']['title'] = $episode['post_title'];
452 $track['meta']['genre'] = 'Podcast';
453 $track['meta']['year'] = mysql2date( 'Y', $episode['post_date'] ); // Episode year
454 if( !empty($date) )
455 $track['meta']['date'] = mysql2date( get_option( 'date_format' ), $episode['post_date'] );// Get episode date
456 $track['meta']['length_formatted'] = powerpress_readable_duration($episode['enclosure']['duration']); // $episode['enclosure']['duration'];
457 if( $track['meta']['length_formatted'] == '0:00' )
458 $track['meta']['length_formatted'] = ''; // Make it empty
459 if( !empty($links) )
460 $track['meta']['link'] = get_permalink( $episode['ID'] ); // 'http://www.google.com/';
461
462 if( $type == 'video' )
463 {
464 $track['dimensions'] = array(
465 'original' => array('width'=>'100%', 'height'=>'100%' ),
466 'resized' => array('width'=>'100%', 'height'=>'100%' )
467 );
468 }
469 /*
470 //$meta = wp_get_attachment_metadata( $attachment->ID );
471 $meta = false;
472 if ( ! empty( $meta ) ) {
473
474 foreach ( wp_get_attachment_id3_keys( $attachment ) as $key => $label ) {
475 if ( ! empty( $meta[ $key ] ) ) {
476 $track['meta'][ $key ] = $meta[ $key ];
477 }
478 }
479
480 if ( 'video' === $type ) {
481 if ( ! empty( $meta['width'] ) && ! empty( $meta['height'] ) ) {
482 $width = $meta['width'];
483 $height = $meta['height'];
484 $theme_height = round( ( $height * $theme_width ) / $width );
485 } else {
486 $width = $default_width;
487 $height = $default_height;
488 }
489
490 $track['dimensions'] = array(
491 'original' => compact( 'width', 'height' ),
492 'resized' => array(
493 'width' => $theme_width,
494 'height' => $theme_height
495 )
496 );
497 }
498 }
499 */
500
501 if( !empty($episode_image) ) // !empty($image) )
502 {
503 $src = $episode_image;
504 $width = 144;
505 $height = 144;
506 if( $type == 'video' ) // image onlyl used for video
507 $track['image'] = array('src'=>$src, 'width'=>'100%', 'height'=>'100%');
508 //$track['thumb'] = compact( 'src', 'width', 'height' );
509 $track['poster'] = compact( 'src', 'width', 'height' );
510 }
511
512 $tracks[] = $track;
513 }
514 $data['tracks'] = $tracks;
515
516 $safe_type = esc_attr( $type );
517 $safe_style = esc_attr( $style );
518
519 ob_start();
520
521 if ( 1 === $instance ) {
522 do_action( 'powerpress_playlist_scripts', $type, $style );
523 } ?>
524 <div class="wp-playlist wp-<?php echo $safe_type ?>-playlist wp-playlist-<?php echo $safe_style ?>">
525 <?php if ( 'audio' === $type ): ?>
526 <div class="wp-playlist-current-item"></div>
527 <?php endif ?>
528 <<?php echo $safe_type ?> controls="controls" preload="none" width="<?php
529 echo (int) $theme_width;
530 ?>"<?php if ( 'video' === $safe_type ):
531 echo ' height="', (int) $theme_height, '"';
532 endif; ?>></<?php echo $safe_type ?>>
533 <div class="wp-playlist-next"></div>
534 <div class="wp-playlist-prev"></div>
535 <noscript>
536 <ol><?php // TODO
537 //foreach ( $attachments as $att_id => $attachment ) {
538 // printf( '<li>%s</li>', wp_get_attachment_link( $att_id ) );
539 //}
540 ?></ol>
541 </noscript>
542 <script type="application/json" class="wp-playlist-script"><?php echo json_encode( $data ) ?></script>
543 </div>
544 <?php
545 return ob_get_clean();
546 }
547
548 add_shortcode( 'podcastlist', 'powerpress_playlist_shortcode' );
549 add_shortcode( 'podcastplaylist', 'powerpress_playlist_shortcode' );
550 add_shortcode( 'powerpressplaylist', 'powerpress_playlist_shortcode' );
551 add_shortcode( 'powerpress_playlist', 'powerpress_playlist_shortcode' );
552