YouTube Channel - Version 2.1.0

Version Description

  • Change: iframe/HTML5 player inject with IFrame Player API https://developers.google.com/youtube/player_parameters#IFrame_Player_API
  • Change: use native WP remote file download function wp_remote_get to fetch feeds (prevent some permission errors on some hosts)
  • Change: removed height parameter so height is calculated by aspect ratio selection - 16:9 as default
  • Add: mute audio on autoplay if iframe/HTML5 is selected
  • Add: converter that will port pre-2.0.0 YTC widgets to 2.0.0+ version
  • Fix: playlist parser (now allowed dash and underscore in playlist ID)
Download this release

Release Info

Developer urkekg
Plugin Icon YouTube Channel
Version 2.1.0
Comparing to
See all releases

Code changes from version 2.0.0 to 2.1.0

Files changed (2) hide show
  1. readme.txt +15 -5
  2. youtube-channel.php +352 -134
readme.txt CHANGED
@@ -4,7 +4,7 @@ Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_i
4
  Tags: youtube, channel, playlist, single, widget, widgets, youtube player, flash player, rss, feed, video, thumbnail, embed, sidebar, chromeless, iframe, html5
5
  Requires at least: 3.7.0
6
  Tested up to: 3.8.1
7
- Stable tag: 2.0.0
8
  License: GPLv3
9
  License URI: http://www.gnu.org/licenses/gpl-3.0.html
10
 
@@ -23,8 +23,8 @@ If you like this extension and you find it useful, please rate it on the right s
23
  * Option to get random video from channel or playlist
24
  * Set custom widget title
25
  * Enhanced Privacy
26
- * Custom set of width and height of video thumbnail/embeded object (default 220x165 px)
27
- * Preferred aspect ratio relative to width (custom, 4:3, 16:10 and 16:9)
28
  * Choose to display video thumbnail, iframe (HTML5 video), object embed (Flash video) or chromeless video
29
  * Fix height for old and new YouTube embed and Chromeless video object taken by controls
30
  * Custom caching timeout
@@ -33,7 +33,7 @@ If you like this extension and you find it useful, please rate it on the right s
33
  * Option to hide video controls
34
  * Option to hide video info
35
  * Option to show video title on top of the video
36
- * Option to show video description below vide (experimental)
37
  * Option to hide annotations from video
38
  * Option to use light controls theme
39
  * Set custom text for link to channel
@@ -77,7 +77,6 @@ If you have to upgrade manually simply repeat the installation steps and re-enab
77
  == TODO ==
78
 
79
  * Add option to open video for thumbnail image in lightbox
80
- * Improve autoplay option to start playing only first video item
81
 
82
  == Frequently Asked Questions ==
83
 
@@ -113,6 +112,13 @@ Video feed for YTC has been retreived with standard youtube feed [uploads by spe
113
  If you does not see your latest video in your uplaods feed (which you can access at https://gdata.youtube.com/feeds/api/users/YOUR_YT_USERID/uploads by replacing YOUR_YT_USERID with your real youtube user ID), then YTC will not see it too.
114
 
115
  == Changelog ==
 
 
 
 
 
 
 
116
 
117
  = 2.0.0 =
118
  * Fix: undefined vars notices
@@ -225,6 +231,10 @@ Just try it and rate it. Only initial release is available right now.
225
 
226
  == Upgrade Notice ==
227
 
 
 
 
 
228
  = 2.0.0 =
229
 
230
  This version introduces caching, optimized feeds, JSON, and better user experience.
4
  Tags: youtube, channel, playlist, single, widget, widgets, youtube player, flash player, rss, feed, video, thumbnail, embed, sidebar, chromeless, iframe, html5
5
  Requires at least: 3.7.0
6
  Tested up to: 3.8.1
7
+ Stable tag: 2.1.0
8
  License: GPLv3
9
  License URI: http://www.gnu.org/licenses/gpl-3.0.html
10
 
23
  * Option to get random video from channel or playlist
24
  * Set custom widget title
25
  * Enhanced Privacy
26
+ * Custom width for video thumbnail/embeded object (default is 220px)
27
+ * Preferred aspect ratio relative to width (16:9, 16:10 and 4:3)
28
  * Choose to display video thumbnail, iframe (HTML5 video), object embed (Flash video) or chromeless video
29
  * Fix height for old and new YouTube embed and Chromeless video object taken by controls
30
  * Custom caching timeout
33
  * Option to hide video controls
34
  * Option to hide video info
35
  * Option to show video title on top of the video
36
+ * Option to show video description below video
37
  * Option to hide annotations from video
38
  * Option to use light controls theme
39
  * Set custom text for link to channel
77
  == TODO ==
78
 
79
  * Add option to open video for thumbnail image in lightbox
 
80
 
81
  == Frequently Asked Questions ==
82
 
112
  If you does not see your latest video in your uplaods feed (which you can access at https://gdata.youtube.com/feeds/api/users/YOUR_YT_USERID/uploads by replacing YOUR_YT_USERID with your real youtube user ID), then YTC will not see it too.
113
 
114
  == Changelog ==
115
+ = 2.1.0 =
116
+ * Change: iframe/HTML5 player inject with IFrame Player API https://developers.google.com/youtube/player_parameters#IFrame_Player_API
117
+ * Change: use native WP remote file download function wp_remote_get to fetch feeds (prevent some permission errors on some hosts)
118
+ * Change: removed height parameter so height is calculated by aspect ratio selection - 16:9 as default
119
+ * Add: mute audio on autoplay if iframe/HTML5 is selected
120
+ * Add: converter that will port pre-2.0.0 YTC widgets to 2.0.0+ version
121
+ * Fix: playlist parser (now allowed dash and underscore in playlist ID)
122
 
123
  = 2.0.0 =
124
  * Fix: undefined vars notices
231
 
232
  == Upgrade Notice ==
233
 
234
+ = 2.1.0 =
235
+
236
+ Before you upgrade to this version, BACKUP DATABASE because experimental transition code converts YouTube Channel widgets made in versions prior to 2.0.0 to new format. You can lose all predefined YTC widgets.
237
+
238
  = 2.0.0 =
239
 
240
  This version introduces caching, optimized feeds, JSON, and better user experience.
youtube-channel.php CHANGED
@@ -4,21 +4,15 @@ Plugin Name: YouTube Channel
4
  Plugin URI: http://urosevic.net/wordpress/plugins/youtube-channel/
5
  Description: <a href="widgets.php">Widget</a> that display latest video thumbnail, iframe (HTML5 video), object (Flash video) or chromeless video from YouTube Channel or Playlist.
6
  Author: Aleksandar Urošević
7
- Version: 2.0.0
8
  Author URI: http://urosevic.net/
9
  */
10
- define( 'YTCVER', '2.0.0' );
11
  define( 'YOUTUBE_CHANNEL_URL', plugin_dir_url(__FILE__) );
12
  define( 'YTCPLID', 'PLEC850BE962234400' );
13
  define( 'YTCUID', 'urkekg' );
14
  define( 'YTCTDOM', 'youtube-channel' );
15
 
16
- /* Load plugin's textdomain */
17
- add_action( 'init', 'youtube_channel_init' );
18
- function youtube_channel_init() {
19
- load_plugin_textdomain( YTCTDOM, false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
20
- }
21
-
22
  /* youtube widget */
23
  class YouTube_Channel_Widget extends WP_Widget {
24
 
@@ -30,40 +24,139 @@ class YouTube_Channel_Widget extends WP_Widget {
30
  );
31
  }
32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  public function form($instance) {
34
  // outputs the options form on admin
35
- $title = (!empty($instance['title'])) ? esc_attr($instance['title']) : '';
36
- $channel = (!empty($instance['channel'])) ? esc_attr($instance['channel']) : '';
37
- $vidqty = (!empty($instance['vidqty'])) ? esc_attr($instance['vidqty']) : 1; // number of items to show
38
- $playlist = (!empty($instance['playlist'])) ? esc_attr($instance['playlist']) : '';
39
- $use_res = (!empty($instance['use_res'])) ? esc_attr($instance['use_res']) : 0; // resource to use: channel, favorites, playlis : ''t
40
- $only_pl = (!empty($instance['only_pl'])) ? esc_attr($instance['only_pl']) : '';
41
- $cache_time = (!empty($instance['cache_time'])) ? esc_attr($instance['cache_time']) : '';
42
- $getrnd = (!empty($instance['getrnd'])) ? esc_attr($instance['getrnd']) : '';
43
- $maxrnd = (!empty($instance['maxrnd'])) ? esc_attr($instance['maxrnd']) : 25; // items to fetch
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
 
45
- $goto_txt = (!empty($instance['goto_txt'])) ? esc_attr($instance['goto_txt']) : '';
46
- $showgoto = (!empty($instance['showgoto'])) ? esc_attr($instance['showgoto']) : '';
47
- $popup_goto = (!empty($instance['popup_goto'])) ? esc_attr($instance['popup_goto']) : '';
48
-
49
- $showtitle = (!empty($instance['showtitle'])) ? esc_attr($instance['showtitle']) : '';
50
- $showvidesc = (!empty($instance['showvidesc'])) ? esc_attr($instance['showvidesc']) : '';
51
- $videsclen = (!empty($instance['videsclen'])) ? esc_attr($instance['videsclen']) : 0;
52
- $descappend = (!empty($instance['descappend'])) ? esc_attr($instance['descappend']) : '&hellip;';
53
- $width = (!empty($instance['width'])) ? esc_attr($instance['width']) : 220;
54
- $height = (!empty($instance['height'])) ? esc_attr($instance['height']) : '';
55
- $to_show = (!empty($instance['to_show'])) ? esc_attr($instance['to_show']) : '';
56
- $autoplay = (!empty($instance['autoplay'])) ? esc_attr($instance['autoplay']) : '';
57
- $controls = (!empty($instance['controls'])) ? esc_attr($instance['controls']) : '';
58
- $fixnoitem = (!empty($instance['fixnoitem'])) ? esc_attr($instance['fixnoitem']) : '';
59
- $ratio = (!empty($instance['ratio'])) ? esc_attr($instance['ratio']) : '';
60
- $fixyt = (!empty($instance['fixyt'])) ? esc_attr($instance['fixyt']) : '';
61
- $hideinfo = (!empty($instance['hideinfo'])) ? esc_attr($instance['hideinfo']) : '';
62
- $hideanno = (!empty($instance['hideanno'])) ? esc_attr($instance['hideanno']) : '';
63
- $themelight = (!empty($instance['themelight'])) ? esc_attr($instance['themelight']) : '';
64
- $debugon = (!empty($instance['debugon'])) ? esc_attr($instance['debugon']) : '';
65
- $userchan = (!empty($instance['userchan'])) ? esc_attr($instance['userchan']) : '';
66
- $enhprivacy = (!empty($instance['enhprivacy'])) ? esc_attr($instance['enhprivacy']) : '';
67
  ?>
68
  <p>
69
  <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Widget Title:', YTCTDOM); ?><input type="text" class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $title; ?>" title="<?php _e('Title for widget', YTCTDOM); ?>" /></label>
@@ -105,26 +198,67 @@ class YouTube_Channel_Widget extends WP_Widget {
105
  <select class="widefat" id="<?php echo $this->get_field_id( 'cache_time' ); ?>" name="<?php echo $this->get_field_name( 'cache_time' ); ?>">
106
 
107
  <option value="0"<?php selected( $cache_time, 0 ); ?>><?php _e('Do not chache', YTCTDOM); ?></option>
108
- <?php $tt = 60; ?><option value="<?=$tt?>"<?php selected( $cache_time, $tt ); ?>><?php _e('1 minute', YTCTDOM); ?></option>
109
- <?php $tt = 60 * 5; ?><option value="<?=$tt?>"<?php selected( $cache_time, $tt ); ?>><?php _e('5 minutes', YTCTDOM); ?></option>
110
- <?php $tt = 60 * 15; ?><option value="<?=$tt?>"<?php selected( $cache_time, $tt ); ?>><?php _e('15 minutes', YTCTDOM); ?></option>
111
- <?php $tt = 60 * 30; ?><option value="<?=$tt?>"<?php selected( $cache_time, $tt ); ?>><?php _e('30 minutes', YTCTDOM); ?></option>
112
- <?php $tt = 60 * 60; ?><option value="<?=$tt?>"<?php selected( $cache_time, $tt ); ?>><?php _e('1 hour', YTCTDOM); ?></option>
113
- <?php $tt = 60 * 60 * 2; ?><option value="<?=$tt?>"<?php selected( $cache_time, $tt ); ?>><?php _e('2 hours', YTCTDOM); ?></option>
114
- <?php $tt = 60 * 60 * 5; ?><option value="<?=$tt?>"<?php selected( $cache_time, $tt ); ?>><?php _e('5 hours', YTCTDOM); ?></option>
115
- <?php $tt = 60 * 60 * 10; ?><option value="<?=$tt?>"<?php selected( $cache_time, $tt ); ?>><?php _e('10 hours', YTCTDOM); ?></option>
116
- <?php $tt = 60 * 60 * 12; ?><option value="<?=$tt?>"<?php selected( $cache_time, $tt ); ?>><?php _e('12 hours', YTCTDOM); ?></option>
117
- <?php $tt = 60 * 60 * 18; ?><option value="<?=$tt?>"<?php selected( $cache_time, $tt ); ?>><?php _e('18 hours', YTCTDOM); ?></option>
118
- <?php $tt = 60 * 60 * 24; ?><option value="<?=$tt?>"<?php selected( $cache_time, $tt ); ?>><?php _e('1 day', YTCTDOM); ?></option>
119
- <?php $tt = 60 * 60 * 24 * 2; ?><option value="<?=$tt?>"<?php selected( $cache_time, $tt ); ?>><?php _e('2 days', YTCTDOM); ?></option>
120
- <?php $tt = 60 * 60 * 24 * 3; ?><option value="<?=$tt?>"<?php selected( $cache_time, $tt ); ?>><?php _e('3 days', YTCTDOM); ?></option>
121
- <?php $tt = 60 * 60 * 24 * 4; ?><option value="<?=$tt?>"<?php selected( $cache_time, $tt ); ?>><?php _e('4 days', YTCTDOM); ?></option>
122
- <?php $tt = 60 * 60 * 24 * 5; ?><option value="<?=$tt?>"<?php selected( $cache_time, $tt ); ?>><?php _e('5 days', YTCTDOM); ?></option>
123
- <?php $tt = 60 * 60 * 24 * 6; ?><option value="<?=$tt?>"<?php selected( $cache_time, $tt ); ?>><?php _e('6 days', YTCTDOM); ?></option>
124
- <?php $tt = 60 * 60 * 24 * 7; ?><option value="<?=$tt?>"<?php selected( $cache_time, $tt ); ?>><?php _e('1 week', YTCTDOM); ?></option>
125
- <?php $tt = 60 * 60 * 24 * 7 * 2; ?><option value="<?=$tt?>"<?php selected( $cache_time, $tt ); ?>><?php _e('2 weeks', YTCTDOM); ?></option>
126
- <?php $tt = 60 * 60 * 24 * 7 * 3; ?><option value="<?=$tt?>"<?php selected( $cache_time, $tt ); ?>><?php _e('3 weeks', YTCTDOM); ?></option>
127
- <?php $tt = 60 * 60 * 24 * 7 * 4; ?><option value="<?=$tt?>"<?php selected( $cache_time, $tt ); ?>><?php _e('1 month', YTCTDOM); ?></option>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
128
  </select>
129
  </p>
130
  <p>
@@ -141,19 +275,21 @@ class YouTube_Channel_Widget extends WP_Widget {
141
  </p>
142
 
143
  <h4><?php _e('Video Settings', YTCTDOM); ?></h4>
144
- <p>
145
- <label for="<?php echo $this->get_field_id('width'); ?>"><?php _e('Width', YTCTDOM); ?>:</label> <input class="small-text" id="<?php echo $this->get_field_id('width'); ?>" name="<?php echo $this->get_field_name('width'); ?>" type="number" min="32" value="<?php echo $width; ?>" title="<?php _e('Set video width in pixels', YTCTDOM); ?>" /> px (<?php _e('default', YTCTDOM); ?> 220)
146
- <br />
147
- <label for="<?php echo $this->get_field_id('height'); ?>"><?php _e('Height', YTCTDOM); ?>:</label> <input class="small-text" id="<?php echo $this->get_field_id('height'); ?>" name="<?php echo $this->get_field_name('height'); ?>" type="number" min="32" value="<?php echo $height; ?>" title="<?php _e('Set video height in pixels', YTCTDOM); ?>" /> px (<?php _e('default', YTCTDOM); ?> 165)
148
- </p>
149
- <p><label for="<?php echo $this->get_field_id('ratio'); ?>"><?php _e('Aspect ratio (relative to width):', YTCTDOM); ?></label>
150
  <select class="widefat" id="<?php echo $this->get_field_id( 'ratio' ); ?>" name="<?php echo $this->get_field_name( 'ratio' ); ?>">
151
- <option value="0"<?php selected( $ratio, 0 ); ?>><?php _e('Custom (as set above)', YTCTDOM); ?></option>
152
- <option value="1"<?php selected( $ratio, 1 ); ?>>4:3</option>
153
- <option value="2"<?php selected( $ratio, 2 ); ?>>16:10</option>
154
  <option value="3"<?php selected( $ratio, 3 ); ?>>16:9</option>
 
 
155
  </select>
156
  </p>
 
 
 
 
 
 
 
157
  <p>
158
  <label for="<?php echo $this->get_field_id('to_show'); ?>"><?php _e('What to show?', YTCTDOM); ?></label>
159
  <select class="widefat" id="<?php echo $this->get_field_id( 'to_show' ); ?>" name="<?php echo $this->get_field_name( 'to_show' ); ?>">
@@ -165,7 +301,8 @@ class YouTube_Channel_Widget extends WP_Widget {
165
  <input class="checkbox" type="checkbox" <?php checked( (bool) $themelight, true ); ?> id="<?php echo $this->get_field_id( 'themelight' ); ?>" name="<?php echo $this->get_field_name( 'themelight' ); ?>" /> <label for="<?php echo $this->get_field_id( 'themelight' ); ?>"><?php _e('Use light theme (default is dark)', YTCTDOM); ?></label><br />
166
  <input class="checkbox" type="checkbox" <?php checked( (bool) $controls, true ); ?> id="<?php echo $this->get_field_id( 'controls' ); ?>" name="<?php echo $this->get_field_name( 'controls' ); ?>" /> <label for="<?php echo $this->get_field_id( 'controls' ); ?>"><?php _e('Hide player controls', YTCTDOM); ?></label><br />
167
  <input class="checkbox" type="checkbox" <?php checked( (bool) $fixyt, true ); ?> id="<?php echo $this->get_field_id( 'fixyt' ); ?>" name="<?php echo $this->get_field_name( 'fixyt' ); ?>" /> <label for="<?php echo $this->get_field_id( 'fixyt' ); ?>"><?php _e('Fix height taken by controls', YTCTDOM); ?></label><br />
168
- <input class="checkbox" type="checkbox" <?php checked( (bool) $autoplay, true ); ?> id="<?php echo $this->get_field_id( 'autoplay' ); ?>" name="<?php echo $this->get_field_name( 'autoplay' ); ?>" /> <label for="<?php echo $this->get_field_id( 'autoplay' ); ?>"><?php _e('Autoplay video or playlist', YTCTDOM); ?></label>
 
169
  </p>
170
 
171
  <h4><?php _e('Content Layout', YTCTDOM); ?></h4>
@@ -198,11 +335,12 @@ class YouTube_Channel_Widget extends WP_Widget {
198
 
199
  <?php
200
  if ( $debugon == 'on' ) {
 
201
  $debug_arr = array_merge(
202
  array(
203
  'server' => $_SERVER["SERVER_SOFTWARE"],
204
- 'php' => phpversion(),
205
- 'wp' => get_bloginfo('version'),
206
  'ytc' => YTCVER,
207
  'url' => get_site_url()
208
  ),
@@ -210,7 +348,7 @@ if ( $debugon == 'on' ) {
210
  ?>
211
 
212
  <textarea name="debug" class="widefat" style="height: 100px;"><?php echo au_ytc_dbg($debug_arr); ?></textarea><br />
213
- Insert debug data to <a href="http://wordpress.org/support/plugin/youtube-channel" target="_support">support forum</a>.
214
  <?php } ?>
215
  </p>
216
 
@@ -223,39 +361,41 @@ if ( $debugon == 'on' ) {
223
 
224
  public function update($new_instance, $old_instance) {
225
  // processes widget options to be saved
226
- $instance = $old_instance;
227
- $instance['title'] = strip_tags($new_instance['title']);
228
- $instance['channel'] = strip_tags($new_instance['channel']);
229
- $instance['vidqty'] = $new_instance['vidqty'];
230
- $instance['playlist'] = strip_tags($new_instance['playlist']);
231
- $instance['use_res'] = $new_instance['use_res'];
232
- $instance['cache_time'] = $new_instance['cache_time'];
233
- $instance['only_pl'] = $new_instance['only_pl'];
234
- $instance['getrnd'] = $new_instance['getrnd'];
235
- $instance['maxrnd'] = $new_instance['maxrnd'];
236
 
237
- $instance['goto_txt'] = strip_tags($new_instance['goto_txt']);
238
- $instance['showgoto'] = $new_instance['showgoto'];
239
- $instance['popup_goto'] = $new_instance['popup_goto'];
240
 
241
- $instance['showtitle'] = $new_instance['showtitle'];
242
- $instance['showvidesc'] = $new_instance['showvidesc'];
243
- $instance['descappend'] = strip_tags($new_instance['descappend']);
244
- $instance['videsclen'] = strip_tags($new_instance['videsclen']);
245
- $instance['width'] = strip_tags($new_instance['width']);
246
- $instance['height'] = strip_tags($new_instance['height']);
247
- $instance['to_show'] = strip_tags($new_instance['to_show']);
248
- $instance['autoplay'] = $new_instance['autoplay'];
249
- $instance['controls'] = $new_instance['controls'];
250
- $instance['fixnoitem'] = $new_instance['fixnoitem'];
251
- $instance['ratio'] = strip_tags($new_instance['ratio']);
252
- $instance['fixyt'] = $new_instance['fixyt'];
253
- $instance['hideinfo'] = $new_instance['hideinfo'];
254
- $instance['hideanno'] = $new_instance['hideanno'];
255
- $instance['themelight'] = $new_instance['themelight'];
256
- $instance['debugon'] = $new_instance['debugon'];
257
- $instance['userchan'] = $new_instance['userchan'];
258
- $instance['enhprivacy'] = $new_instance['enhprivacy'];
 
 
259
 
260
  return $instance;
261
  }
@@ -324,7 +464,13 @@ if ( $debugon == 'on' ) {
324
  // get/set transient cache
325
  if ( false === ($json = get_transient($cache_key)) ) {
326
  // no cached JSON, get new
327
- $json = file_get_contents($feed_url,0,null,null);
 
 
 
 
 
 
328
  // set decoded JSON to transient cache_key
329
  set_transient($cache_key, json_decode($json), $instance['cache_time']);
330
  } else {
@@ -333,13 +479,20 @@ if ( $debugon == 'on' ) {
333
  }
334
  } else {
335
  // just get fresh feed if cache disabled
336
- $json = file_get_contents($feed_url,0,null,null);
 
 
 
 
 
337
  }
338
 
339
  // decode JSON data
340
  $json_output = json_decode($json);
341
 
342
- if ( !is_wp_error($json_output) ) {
 
 
343
  // sort by date uploaded
344
  $json_entry = $json_output->feed->entry;
345
 
@@ -360,7 +513,8 @@ if ( $debugon == 'on' ) {
360
  }
361
 
362
  if ($maxitems == 0) {
363
- $output[] = __( 'No items' , YTCTDOM );
 
364
  } else {
365
 
366
  if ( $getrnd ) $rnd_used = array(); // set array for unique random item
@@ -393,6 +547,40 @@ if ( $debugon == 'on' ) {
393
 
394
  }
395
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
396
  }
397
 
398
  /* function to print standard playlist embed code */
@@ -405,7 +593,8 @@ function ytc_only_pl($instance) {
405
  if ( empty($playlist) )
406
  $playlist = YTCPLID;
407
 
408
- $height = height_ratio($width, $instance['height'], $instance['ratio']);
 
409
 
410
  $height += ($instance['fixyt']) ? 25 : 0;
411
 
@@ -428,15 +617,16 @@ function ytc_print_video($item, $instance, $y) {
428
 
429
  // get hideinfo, autoplay and controls settings
430
  // where this is used?
431
- $hideinfo = $instance['hideinfo'];
432
- $autoplay = $instance['autoplay'];
433
- $controls = $instance['controls'];
 
434
 
435
  // set width and height
436
  $width = $instance['width'];
437
- if ( empty($width) )
438
- $width = 220;
439
- $height = height_ratio($width, $instance['height'], $instance['ratio']);
440
 
441
  // calculate image height based on width for 4:3 thumbnail
442
  $imgfixedheight = $width / 4 * 3;
@@ -510,14 +700,37 @@ function ytc_print_video($item, $instance, $y) {
510
  } else if ( $to_show == "iframe" ) {
511
  if ( empty($usepl) ) $yt_url = $yt_id;
512
 
513
- $output[] = '<iframe title="YouTube video player" width="'.$width.'" height="'.$height.'" src="//'.$yt_domain.'/embed/'.$yt_url.'?wmode=opaque'; //&enablejsapi=1';
514
- if ( $controls ) $output[] = "&amp;controls=0";
515
- if ( $hideinfo ) $output[] = "&amp;showinfo=0";
516
- if ( $autoplay ) $output[] = "&amp;autoplay=1";
517
- if ( $hideanno ) $output[] = "&amp;iv_load_policy=3";
518
- if ( $themelight ) $output[] = "&amp;theme=light";
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
519
 
520
- $output[] = '" style="border: 0;" allowfullscreen id="'.$ytc_vid.'"></iframe>';
521
  } else { // default is object
522
  $obj_url = '//'.$yt_domain.'/'.$yt_url.'?version=3';
523
  $obj_url .= ( $controls ) ? '&amp;controls=0' : '';
@@ -580,10 +793,23 @@ function ytc_print_video($item, $instance, $y) {
580
  }
581
 
582
  // function to calculate height by width and ratio
583
- function height_ratio($width, $height=0, $ratio) {
584
- if ( $width == "" )
585
- $width = 220;
586
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
587
  if ( $ratio == 1 ) { // 4:3
588
  $height = round(($width / 4 ) * 3);
589
  } elseif ( $ratio == 2 ) { // 16:10
@@ -595,6 +821,7 @@ function height_ratio($width, $height=0, $ratio) {
595
  if ( $height == "" || $height == 0 )
596
  $height = 165;
597
  }
 
598
  return $height;
599
  }
600
 
@@ -639,7 +866,7 @@ function ytc_channel_link($instance) {
639
  function ytc_clean_playlist_id($playlist) {
640
  if ( substr($playlist,0,4) == "http" ) {
641
  // if URL provided, extract playlist ID
642
- $playlist = preg_replace('/.*list=PL([A-Za-z0-9]*).*/','$1', $playlist);
643
  } else if ( substr($playlist,0,2) == 'PL' ) {
644
  $playlist = substr($playlist,2);
645
  }
@@ -670,12 +897,3 @@ function yt_domain($instance) {
670
  $yt_domain = ( !empty($instance['enhprivacy']) ) ? 'www.youtube-nocookie.com' : 'www.youtube.com';
671
  return $yt_domain;
672
  }
673
-
674
- function adbg($i){
675
- if ( is_array($i) )
676
- $o = '<pre>'.print_r($i,1).'</pre>';
677
- else
678
- $o = $i;
679
-
680
- apply_filters('debug', $o);
681
- }
4
  Plugin URI: http://urosevic.net/wordpress/plugins/youtube-channel/
5
  Description: <a href="widgets.php">Widget</a> that display latest video thumbnail, iframe (HTML5 video), object (Flash video) or chromeless video from YouTube Channel or Playlist.
6
  Author: Aleksandar Urošević
7
+ Version: 2.1.0
8
  Author URI: http://urosevic.net/
9
  */
10
+ define( 'YTCVER', '2.1.0' );
11
  define( 'YOUTUBE_CHANNEL_URL', plugin_dir_url(__FILE__) );
12
  define( 'YTCPLID', 'PLEC850BE962234400' );
13
  define( 'YTCUID', 'urkekg' );
14
  define( 'YTCTDOM', 'youtube-channel' );
15
 
 
 
 
 
 
 
16
  /* youtube widget */
17
  class YouTube_Channel_Widget extends WP_Widget {
18
 
24
  );
25
  }
26
 
27
+
28
+ /**
29
+ * Activate the plugin
30
+ */
31
+ public static function activate()
32
+ {
33
+ // Transit old settings to new format
34
+ // get pre-2.0.0 YTC widgets, and if exist, convert to 2.0.0+ version
35
+ if ( $old = get_option('widget_youtube_channel_widget') ) {
36
+ // if we have pre-2.0.0 YTC widgets, merge them to new version
37
+
38
+ // get new YTC widgets
39
+ $new = get_option('widget_youtube-channel');
40
+
41
+ // get all widget areas
42
+ $widget_areas = get_option('sidebars_widgets');
43
+
44
+ // update options to 2.0.0+ version
45
+ foreach ($old as $k=>$v) {
46
+
47
+ if ( $k !== "_multiwidget" ){
48
+ // option for resource
49
+ $v['use_res'] = 0;
50
+ if ( $v['usepl'] == "on" ) {
51
+ $v['use_res'] = 2;
52
+ }
53
+
54
+ $v['popup_goto'] = 0;
55
+ if ( $v['popupgoto'] == "on" ) {
56
+ $v['popup_goto'] = 1;
57
+ } else if ($v['target'] == "on") {
58
+ $v['popup_goto'] = 2;
59
+ }
60
+ unset($v['usepl'], $v['popupgoto'], $v['target']);
61
+
62
+ $v['cache_time'] = 0;
63
+ $v['userchan'] = 0;
64
+ $v['enhprivacy'] = 0;
65
+ $v['autoplay_mute'] = 0;
66
+
67
+ // add old YTC widget to new set
68
+ // but append at the end if YTC widget with same ID already exist
69
+ // in new set (created in version 2.0.0)
70
+ if ( is_array($new[$k]) ) {
71
+ // populate at the end
72
+ array_push($new, $v);
73
+ $ytc_widget_id = "youtube-channel-".end(array_keys($new));
74
+ } else {
75
+ // set as current widget ID
76
+ $new[$k] = $v;
77
+ $ytc_widget_id = "youtube-channel-$k";
78
+ }
79
+
80
+ $ytc_widget_added = 0;
81
+ foreach ( $widget_areas as $wak => $wav ) {
82
+ // check if here we have this widget
83
+ if ( is_array($wav) && in_array($ytc_widget_id,$wav) )
84
+ $ytc_widget_added++;
85
+ }
86
+ // if YTC widget has not present in any widget area, add it to inactive widgets ;)
87
+ if ( $ytc_widget_added == 0 )
88
+ array_push($widget_areas['wp_inactive_widgets'], $ytc_widget_id);
89
+
90
+ }
91
+ // add to inactive widgets if don't belong to any widget area
92
+
93
+ } // foreach widget option
94
+
95
+ // update widget areas set
96
+ update_option('sidebars_widgets',$widget_areas);
97
+
98
+ // update new YTC widgets
99
+ update_option('widget_youtube-channel',$new);
100
+
101
+ // remove old YTC widgets entry
102
+ delete_option('widget_youtube_channel_widget');
103
+
104
+ // clear temporary vars
105
+ unset ($old,$new);
106
+
107
+ } // if we have old YTC widgets
108
+
109
+ } // END public static function activate
110
+
111
+ // TODO: Form code
112
  public function form($instance) {
113
  // outputs the options form on admin
114
+ // General Options
115
+ $title = (!empty($instance['title'])) ? esc_attr($instance['title']) : '';
116
+ $channel = (!empty($instance['channel'])) ? esc_attr($instance['channel']) : '';
117
+ $playlist = (!empty($instance['playlist'])) ? esc_attr($instance['playlist']) : '';
118
+
119
+ $use_res = (!empty($instance['use_res'])) ? esc_attr($instance['use_res']) : 0; // resource to use: channel, favorites, playlis : ''t
120
+ $only_pl = (!empty($instance['only_pl'])) ? esc_attr($instance['only_pl']) : '';
121
+
122
+ $cache_time = (!empty($instance['cache_time'])) ? esc_attr($instance['cache_time']) : '';
123
+
124
+ $maxrnd = (!empty($instance['maxrnd'])) ? esc_attr($instance['maxrnd']) : 25; // items to fetch
125
+ $vidqty = (!empty($instance['vidqty'])) ? esc_attr($instance['vidqty']) : 1; // number of items to show
126
+
127
+ $enhprivacy = (!empty($instance['enhprivacy'])) ? esc_attr($instance['enhprivacy']) : '';
128
+ $fixnoitem = (!empty($instance['fixnoitem'])) ? esc_attr($instance['fixnoitem']) : '';
129
+ $getrnd = (!empty($instance['getrnd'])) ? esc_attr($instance['getrnd']) : '';
130
+
131
+ // Video Settings
132
+ $ratio = (!empty($instance['ratio'])) ? esc_attr($instance['ratio']) : 3;
133
+ $width = (!empty($instance['width'])) ? esc_attr($instance['width']) : 220;
134
+ // $height = (!empty($instance['height'])) ? esc_attr($instance['height']) : '';
135
+
136
+ $to_show = (!empty($instance['to_show'])) ? esc_attr($instance['to_show']) : '';
137
+ $themelight = (!empty($instance['themelight'])) ? esc_attr($instance['themelight']) : '';
138
+ $controls = (!empty($instance['controls'])) ? esc_attr($instance['controls']) : '';
139
+ $fixyt = (!empty($instance['fixyt'])) ? esc_attr($instance['fixyt']) : '';
140
+ $autoplay = (!empty($instance['autoplay'])) ? esc_attr($instance['autoplay']) : '';
141
+ $autoplay_mute = (!empty($instance['autoplay_mute'])) ? esc_attr($instance['autoplay_mute']) : '';
142
+
143
+ // Content Layout
144
+ $showtitle = (!empty($instance['showtitle'])) ? esc_attr($instance['showtitle']) : '';
145
+ $showvidesc = (!empty($instance['showvidesc'])) ? esc_attr($instance['showvidesc']) : '';
146
+ $videsclen = (!empty($instance['videsclen'])) ? esc_attr($instance['videsclen']) : 0;
147
+ $descappend = (!empty($instance['descappend'])) ? esc_attr($instance['descappend']) : '&hellip;';
148
+
149
+ $hideanno = (!empty($instance['hideanno'])) ? esc_attr($instance['hideanno']) : '';
150
+ $hideinfo = (!empty($instance['hideinfo'])) ? esc_attr($instance['hideinfo']) : '';
151
+
152
+ // Link to Channel
153
+ $goto_txt = (!empty($instance['goto_txt'])) ? esc_attr($instance['goto_txt']) : '';
154
+ $showgoto = (!empty($instance['showgoto'])) ? esc_attr($instance['showgoto']) : '';
155
+ $popup_goto = (!empty($instance['popup_goto'])) ? esc_attr($instance['popup_goto']) : '';
156
+ $userchan = (!empty($instance['userchan'])) ? esc_attr($instance['userchan']) : '';
157
 
158
+ // Debug YTC
159
+ $debugon = (!empty($instance['debugon'])) ? esc_attr($instance['debugon']) : '';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
160
  ?>
161
  <p>
162
  <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Widget Title:', YTCTDOM); ?><input type="text" class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $title; ?>" title="<?php _e('Title for widget', YTCTDOM); ?>" /></label>
198
  <select class="widefat" id="<?php echo $this->get_field_id( 'cache_time' ); ?>" name="<?php echo $this->get_field_name( 'cache_time' ); ?>">
199
 
200
  <option value="0"<?php selected( $cache_time, 0 ); ?>><?php _e('Do not chache', YTCTDOM); ?></option>
201
+ <?php
202
+ $times = array(
203
+ 'minute' => array(
204
+ 1 => "1 minute",
205
+ 5 => "5 minutes",
206
+ 15 => "15 minutes",
207
+ 30 => "30 minutes"
208
+ ),
209
+ 'hour' => array(
210
+ 1 => "1 hour",
211
+ 2 => "2 hours",
212
+ 5 => "5 hours",
213
+ 10 => "10 hours",
214
+ 12 => "12 hours",
215
+ 18 => "18 hours"
216
+ ),
217
+ 'day' => array(
218
+ 1 => "1 day",
219
+ 2 => "2 days",
220
+ 3 => "3 days",
221
+ 4 => "4 days",
222
+ 5 => "5 days",
223
+ 6 => "6 days"
224
+ ),
225
+ 'week' => array(
226
+ 1 => "1 week",
227
+ 2 => "2 weeks",
228
+ 3 => "3 weeks",
229
+ 4 => "1 month"
230
+ )
231
+ );
232
+
233
+ $out = "";
234
+ foreach ($times as $period => $timeset)
235
+ {
236
+ switch ($period)
237
+ {
238
+ case 'minute':
239
+ $sc = MINUTE_IN_SECONDS;
240
+ break;
241
+ case 'hour':
242
+ $sc = HOUR_IN_SECONDS;
243
+ break;
244
+ case 'day':
245
+ $sc = DAY_IN_SECONDS;
246
+ break;
247
+ case 'week':
248
+ $sc = WEEK_IN_SECONDS;
249
+ break;
250
+ }
251
+
252
+ foreach ($timeset as $n => $s)
253
+ {
254
+ $sec = $sc * $n;
255
+ $out .='<option value="'.$sec.'" '. selected( $cache_time, $sec ).'>'.__($s, YTCTDOM).'</option>';
256
+ unset($sec);
257
+ }
258
+ }
259
+ echo $out;
260
+ unset($out);
261
+ ?>
262
  </select>
263
  </p>
264
  <p>
275
  </p>
276
 
277
  <h4><?php _e('Video Settings', YTCTDOM); ?></h4>
278
+ <p><label for="<?php echo $this->get_field_id('ratio'); ?>"><?php _e('Aspect ratio', YTCTDOM); ?>:</label>
 
 
 
 
 
279
  <select class="widefat" id="<?php echo $this->get_field_id( 'ratio' ); ?>" name="<?php echo $this->get_field_name( 'ratio' ); ?>">
280
+ <?php /* <option value="0"<?php selected( $ratio, 0 ); ?>><?php _e('Custom (as set above)', YTCTDOM); ?></option> */ ?>
 
 
281
  <option value="3"<?php selected( $ratio, 3 ); ?>>16:9</option>
282
+ <option value="2"<?php selected( $ratio, 2 ); ?>>16:10</option>
283
+ <option value="1"<?php selected( $ratio, 1 ); ?>>4:3</option>
284
  </select>
285
  </p>
286
+ <p>
287
+ <label for="<?php echo $this->get_field_id('width'); ?>"><?php _e('Width', YTCTDOM); ?>:</label> <input class="small-text" id="<?php echo $this->get_field_id('width'); ?>" name="<?php echo $this->get_field_name('width'); ?>" type="number" min="32" value="<?php echo $width; ?>" title="<?php _e('Set video width in pixels', YTCTDOM); ?>" /> px (<?php _e('default', YTCTDOM); ?> 220)
288
+ <?php /*
289
+ <br />
290
+ <label for="< ?php echo $this->get_field_id('height'); ?>">< ?php _e('Height', YTCTDOM); ?>:</label> <input class="small-text" id="< ?php echo $this->get_field_id('height'); ?>" name="< ?php echo $this->get_field_name('height'); ?>" type="number" min="32" value="< ?php echo $height; ?>" title="< ?php _e('Set video height in pixels', YTCTDOM); ?>" /> px (< ?php _e('default', YTCTDOM); ?> 165)
291
+ */ ?>
292
+ </p>
293
  <p>
294
  <label for="<?php echo $this->get_field_id('to_show'); ?>"><?php _e('What to show?', YTCTDOM); ?></label>
295
  <select class="widefat" id="<?php echo $this->get_field_id( 'to_show' ); ?>" name="<?php echo $this->get_field_name( 'to_show' ); ?>">
301
  <input class="checkbox" type="checkbox" <?php checked( (bool) $themelight, true ); ?> id="<?php echo $this->get_field_id( 'themelight' ); ?>" name="<?php echo $this->get_field_name( 'themelight' ); ?>" /> <label for="<?php echo $this->get_field_id( 'themelight' ); ?>"><?php _e('Use light theme (default is dark)', YTCTDOM); ?></label><br />
302
  <input class="checkbox" type="checkbox" <?php checked( (bool) $controls, true ); ?> id="<?php echo $this->get_field_id( 'controls' ); ?>" name="<?php echo $this->get_field_name( 'controls' ); ?>" /> <label for="<?php echo $this->get_field_id( 'controls' ); ?>"><?php _e('Hide player controls', YTCTDOM); ?></label><br />
303
  <input class="checkbox" type="checkbox" <?php checked( (bool) $fixyt, true ); ?> id="<?php echo $this->get_field_id( 'fixyt' ); ?>" name="<?php echo $this->get_field_name( 'fixyt' ); ?>" /> <label for="<?php echo $this->get_field_id( 'fixyt' ); ?>"><?php _e('Fix height taken by controls', YTCTDOM); ?></label><br />
304
+ <input class="checkbox" type="checkbox" <?php checked( (bool) $autoplay, true ); ?> id="<?php echo $this->get_field_id( 'autoplay' ); ?>" name="<?php echo $this->get_field_name( 'autoplay' ); ?>" /> <label for="<?php echo $this->get_field_id( 'autoplay' ); ?>"><?php _e('Autoplay video or playlist', YTCTDOM); ?></label><br />
305
+ <input class="checkbox" type="checkbox" <?php checked( (bool) $autoplay_mute, true ); ?> id="<?php echo $this->get_field_id( 'autoplay_mute' ); ?>" name="<?php echo $this->get_field_name( 'autoplay_mute' ); ?>" /> <label for="<?php echo $this->get_field_id( 'autoplay_mute' ); ?>"><?php _e('Mute video on autoplay', YTCTDOM); ?></label>
306
  </p>
307
 
308
  <h4><?php _e('Content Layout', YTCTDOM); ?></h4>
335
 
336
  <?php
337
  if ( $debugon == 'on' ) {
338
+ global $wp_version;
339
  $debug_arr = array_merge(
340
  array(
341
  'server' => $_SERVER["SERVER_SOFTWARE"],
342
+ 'php' => PHP_VERSION, //phpversion(),
343
+ 'wp' => $wp_version, // get_bloginfo('version'),
344
  'ytc' => YTCVER,
345
  'url' => get_site_url()
346
  ),
348
  ?>
349
 
350
  <textarea name="debug" class="widefat" style="height: 100px;"><?php echo au_ytc_dbg($debug_arr); ?></textarea><br />
351
+ <small>Insert debug data to <a href="http://wordpress.org/support/plugin/youtube-channel" target="_support">support forum</a>.<br />Please do not remove channel and playlist ID's. If you are concerned about privacy, send this debug log to email <a href="mailto:urke.kg@gmail.com?subject=YTC%20debug%20log">urke.kg@gmail.com</a></small>
352
  <?php } ?>
353
  </p>
354
 
361
 
362
  public function update($new_instance, $old_instance) {
363
  // processes widget options to be saved
364
+ $instance = $old_instance;
365
+ $instance['title'] = strip_tags($new_instance['title']);
366
+ $instance['channel'] = strip_tags($new_instance['channel']);
367
+ $instance['vidqty'] = $new_instance['vidqty'];
368
+ $instance['playlist'] = strip_tags($new_instance['playlist']);
369
+ $instance['use_res'] = $new_instance['use_res'];
370
+ $instance['cache_time'] = $new_instance['cache_time'];
371
+ $instance['only_pl'] = $new_instance['only_pl'];
372
+ $instance['getrnd'] = $new_instance['getrnd'];
373
+ $instance['maxrnd'] = $new_instance['maxrnd'];
374
 
375
+ $instance['goto_txt'] = strip_tags($new_instance['goto_txt']);
376
+ $instance['showgoto'] = $new_instance['showgoto'];
377
+ $instance['popup_goto'] = $new_instance['popup_goto'];
378
 
379
+ $instance['showtitle'] = $new_instance['showtitle'];
380
+ $instance['showvidesc'] = $new_instance['showvidesc'];
381
+ $instance['descappend'] = strip_tags($new_instance['descappend']);
382
+ $instance['videsclen'] = strip_tags($new_instance['videsclen']);
383
+ $instance['width'] = strip_tags($new_instance['width']);
384
+ // $instance['height'] = strip_tags($new_instance['height']);
385
+ $instance['to_show'] = strip_tags($new_instance['to_show']);
386
+ $instance['autoplay'] = $new_instance['autoplay'];
387
+ $instance['autoplay_mute'] = $new_instance['autoplay_mute'];
388
+
389
+ $instance['controls'] = $new_instance['controls'];
390
+ $instance['fixnoitem'] = $new_instance['fixnoitem'];
391
+ $instance['ratio'] = strip_tags($new_instance['ratio']);
392
+ $instance['fixyt'] = $new_instance['fixyt'];
393
+ $instance['hideinfo'] = $new_instance['hideinfo'];
394
+ $instance['hideanno'] = $new_instance['hideanno'];
395
+ $instance['themelight'] = $new_instance['themelight'];
396
+ $instance['debugon'] = $new_instance['debugon'];
397
+ $instance['userchan'] = $new_instance['userchan'];
398
+ $instance['enhprivacy'] = $new_instance['enhprivacy'];
399
 
400
  return $instance;
401
  }
464
  // get/set transient cache
465
  if ( false === ($json = get_transient($cache_key)) ) {
466
  // no cached JSON, get new
467
+ $wprga = array(
468
+ 'timeout' => 2 // two seconds only
469
+ );
470
+ $response = wp_remote_get($feed_url, $wprga);
471
+ $json = wp_remote_retrieve_body( $response );
472
+
473
+ // $json = file_get_contents($feed_url,0,null,null);
474
  // set decoded JSON to transient cache_key
475
  set_transient($cache_key, json_decode($json), $instance['cache_time']);
476
  } else {
479
  }
480
  } else {
481
  // just get fresh feed if cache disabled
482
+ // $json = file_get_contents($feed_url,0,null,null);
483
+ $wprga = array(
484
+ 'timeout' => 2 // two seconds only
485
+ );
486
+ $response = wp_remote_get($feed_url, $wprga);
487
+ $json = wp_remote_retrieve_body( $response );
488
  }
489
 
490
  // decode JSON data
491
  $json_output = json_decode($json);
492
 
493
+ // predefine maxitems to prevent undefined notices
494
+ $maxitems = 0;
495
+ if ( !is_wp_error($json_output) && is_object($json_output) ) {
496
  // sort by date uploaded
497
  $json_entry = $json_output->feed->entry;
498
 
513
  }
514
 
515
  if ($maxitems == 0) {
516
+ // $output[] = __( 'No items' , YTCTDOM );
517
+ $output[] = __("No items", YTCTDOM).' [<a href="'.$feed_url.'" target="_blank">'.__("Check here why",YTCTDOM).'</a>]';
518
  } else {
519
 
520
  if ( $getrnd ) $rnd_used = array(); // set array for unique random item
547
 
548
  }
549
 
550
+ } // class YouTube_Channel_Widget()
551
+
552
+ if( class_exists('YouTube_Channel_Widget'))
553
+ {
554
+ // Installation and uninstallation hooks
555
+ register_activation_hook(__FILE__, array('YouTube_Channel_Widget', 'activate'));
556
+
557
+
558
+ /* Load plugin's textdomain */
559
+ add_action( 'init', 'youtube_channel_init' ); /*TODO: move inside class*/
560
+ function youtube_channel_init() {
561
+ load_plugin_textdomain( YTCTDOM, false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
562
+ }
563
+
564
+ /* Load YTC player script */
565
+ function ytc_enqueue_scripts() {
566
+ wp_enqueue_script( 'ytc', 'https://www.youtube.com/player_api', array(), '3.0.0', true );
567
+ }
568
+ add_action( 'wp_enqueue_scripts', 'ytc_enqueue_scripts' );
569
+
570
+ function ytc_footer_js() {
571
+ ?>
572
+ <script type="text/javascript">
573
+ function onYouTubePlayerAPIReady() {
574
+ <?php echo $_SESSION['ytc_html5_js']; ?>
575
+ }
576
+ function ytc_mute(event){
577
+ event.target.mute();
578
+ }
579
+ </script>
580
+ <?php
581
+ }
582
+ add_action( 'wp_footer', 'ytc_footer_js' );
583
+
584
  }
585
 
586
  /* function to print standard playlist embed code */
593
  if ( empty($playlist) )
594
  $playlist = YTCPLID;
595
 
596
+ $height = height_ratio($width, $instance['ratio']);
597
+ // $height = height_ratio($width, $instance['height'], $instance['ratio']);
598
 
599
  $height += ($instance['fixyt']) ? 25 : 0;
600
 
617
 
618
  // get hideinfo, autoplay and controls settings
619
  // where this is used?
620
+ $hideinfo = $instance['hideinfo'];
621
+ $autoplay = $instance['autoplay'];
622
+ $autoplay_mute = $instance['autoplay_mute'];
623
+ $controls = $instance['controls'];
624
 
625
  // set width and height
626
  $width = $instance['width'];
627
+ if ( empty($width) ) $width = 220;
628
+ $height = height_ratio($width, $instance['ratio']);
629
+ // $height = height_ratio($width, $instance['height'], $instance['ratio']);
630
 
631
  // calculate image height based on width for 4:3 thumbnail
632
  $imgfixedheight = $width / 4 * 3;
700
  } else if ( $to_show == "iframe" ) {
701
  if ( empty($usepl) ) $yt_url = $yt_id;
702
 
703
+ $js_controls = ( $controls ) ? "controls: 0," : '';
704
+ $js_showinfo = ( $hideinfo ) ? "showinfo: 0," : '';
705
+ $js_autoplay = ( $autoplay ) ? "autoplay: 1," : '';
706
+ $js_iv_load_policy = ( $hideanno ) ? "iv_load_policy: 3," : '';
707
+ $js_theme = ( $themelight ) ? "theme: 'light'," : '';
708
+ $js_autoplay_mute = ( $autoplay && $autoplay_mute ) ? "events: {'onReady': ytc_mute}" : '';
709
+ $js_player_id = str_replace('-', '_', $yt_url);
710
+
711
+ $output[] = '<div id="ytc_player_'.$js_player_id.'"></div>';
712
+ $site_domain = $_SERVER['HTTP_HOST'];
713
+ $ytc_html5_js = <<<JS
714
+ var ytc_player_$js_player_id;
715
+ ytc_player_$js_player_id = new YT.Player('ytc_player_$js_player_id', {
716
+ height: '$height',
717
+ width: '$width',
718
+ videoId: '$yt_url',
719
+ enablejsapi: 1,
720
+ playerVars: {
721
+ $js_autoplay $js_showinfo $js_controls $js_theme wmmode: 'opaque'
722
+ },
723
+ origin: '$site_domain',
724
+ $js_iv_load_policy $js_autoplay_mute
725
+ });
726
+ JS;
727
+
728
+ // prepare JS for footer
729
+ if ( empty($_SESSION['ytc_html5_js']) )
730
+ $_SESSION['ytc_html5_js'] = $ytc_html5_js;
731
+ else
732
+ $_SESSION['ytc_html5_js'] .= $ytc_html5_js;
733
 
 
734
  } else { // default is object
735
  $obj_url = '//'.$yt_domain.'/'.$yt_url.'?version=3';
736
  $obj_url .= ( $controls ) ? '&amp;controls=0' : '';
793
  }
794
 
795
  // function to calculate height by width and ratio
796
+ function height_ratio($width=220, $ratio) {
797
+ // if ( $width == "" ) $width = 220;
 
798
 
799
+ switch ($ratio)
800
+ {
801
+ case 1:
802
+ $height = round(($width / 4 ) * 3);
803
+ break;
804
+ case 2:
805
+ $height = round(($width / 16 ) * 10);
806
+ break;
807
+ case 3:
808
+ default:
809
+ $height = round(($width / 16 ) * 9);
810
+ }
811
+
812
+ /*
813
  if ( $ratio == 1 ) { // 4:3
814
  $height = round(($width / 4 ) * 3);
815
  } elseif ( $ratio == 2 ) { // 16:10
821
  if ( $height == "" || $height == 0 )
822
  $height = 165;
823
  }
824
+ */
825
  return $height;
826
  }
827
 
866
  function ytc_clean_playlist_id($playlist) {
867
  if ( substr($playlist,0,4) == "http" ) {
868
  // if URL provided, extract playlist ID
869
+ $playlist = preg_replace('/.*list=PL([A-Za-z0-9\-\_]*).*/','$1', $playlist);
870
  } else if ( substr($playlist,0,2) == 'PL' ) {
871
  $playlist = substr($playlist,2);
872
  }
897
  $yt_domain = ( !empty($instance['enhprivacy']) ) ? 'www.youtube-nocookie.com' : 'www.youtube.com';
898
  return $yt_domain;
899
  }