YouTube Channel - Version 3.0.7.2

Version Description

(2015-05-24) = * Add: Error report if we have broken feed on record * Add: Report about failed HTTP connections and other problems ocurred when we try to fetch feed * Add: DRY of visible errors for Administrator and visitors (Oops message)

Download this release

Release Info

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

Code changes from version 3.0.7.1 to 3.0.7.2

Files changed (2) hide show
  1. readme.txt +12 -22
  2. youtube-channel.php +110 -60
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, feed, video, thumbnail, embed, sidebar, iframe, html5, responsive
5
  Requires at least: 3.9.0
6
  Tested up to: 4.2.2
7
- Stable tag: 3.0.7.1
8
  License: GPLv3
9
  License URI: http://www.gnu.org/licenses/gpl-3.0.html
10
 
@@ -182,33 +182,17 @@ Please folllow [Installation](https://wordpress.org/plugins/youtube-channel/inst
182
  Right click that message on your website, click *Inspect Element*, expand tag with class *youtube_channel* and look for HTML comment below it, message will look like this:
183
 
184
  `<!-- YTC ERROR:
185
- domain: youtube.playlistItem
186
- reason: playlistNotFound
187
- message: Not Found
188
- tip: Please check did you set proper Channel ID. We should display videos from channel videos, but YouTube does not recognize your channel ID UCkVThLIHAZVsJc7f28tIsIws as existing and public resource.
189
  -->`
190
 
191
- Do exactly what tip says - check and correct Channel ID in default settings/widget/shortcode.
192
 
193
  `<!-- YTC ERROR:
194
- domain: usageLimits
195
- reason: keyInvalid
196
- message: Bad Request
197
  -->`
198
 
199
- If you followed [Installation](https://wordpress.org/plugins/youtube-channel/installation/) instructions, double check **YouTube Data API Key** entered to Global settings. Do you have correct key generated by you and allowed to be used on your server?
200
-
201
- `<!-- YTC ERROR:
202
- domain: usageLimits
203
- reason: ipRefererBlocked
204
- message: There is a per-IP or per-Referer restriction configured on your API key and the request does not match these restrictions. Please use the Google Developers Console to update your API key configuration if request from this IP or referer should be allowed.
205
- -->`
206
-
207
- We still strugling with that Google's restrictions. If you get final solution, please inform us asap. Try with following possible solutions:
208
-
209
- 1. If you are using **Jetpack** plugin with enabled **Protect** sub plugin, try to disable it. (kudos to [yudhita](https://wordpress.org/support/profile/yudhita) for tip)
210
  1. Try to remove restrictions by referer or IP in your **YouTube Data API Key** and refresh page after couple minutes.
211
- 1. If that does not help, please try to create new API Key for Server w/o restrictions (not to regenerate existing one).
212
 
213
  If there is no `YTC ERROR` code in HTML source, visit [Google API Explorer](https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.playlistItems.list?part=snippet&maxResults=5&playlistId=) and append:
214
 
@@ -220,7 +204,7 @@ If there is no `YTC ERROR` code in HTML source, visit [Google API Explorer](http
220
  Note that all four resources are *playlists* (including channel), so append mentioned ID to field **playlistId** (not to **id**), and click **Execute** button at the bottom of that page.
221
 
222
  1. If you receive some error, fix settings.
223
- 1. If there is no error but you do not get any video in results - contact Google Support
224
  1. If there are video results but not displayed with YouTube Channel plugin - [contact us](https://wordpress.org/support/plugin/youtube-channel)
225
 
226
  = Where to find correct Channel ID and/or Vanity custom Name? =
@@ -289,6 +273,12 @@ If you really need that missing feature ASAP, feel free to [contact me](urosevic
289
  If you don't wish to pay for enhancements (then you don't care would that be implemented in a week, month, year or so), then send new [Support topic](https://wordpress.org/support/plugin/youtube-channel) with *Topic title* in format **[Feature Request] ...**
290
 
291
  == Changelog ==
 
 
 
 
 
 
292
  = 3.0.7.1 (2015-05-17/18) =
293
  * Fix: Plugin version number not updated in DB
294
  * Fix: Magnific Popup appear under header on Twenty Eleven theme
4
  Tags: youtube, channel, playlist, single, widget, widgets, youtube player, feed, video, thumbnail, embed, sidebar, iframe, html5, responsive
5
  Requires at least: 3.9.0
6
  Tested up to: 4.2.2
7
+ Stable tag: 3.0.7.2
8
  License: GPLv3
9
  License URI: http://www.gnu.org/licenses/gpl-3.0.html
10
 
182
  Right click that message on your website, click *Inspect Element*, expand tag with class *youtube_channel* and look for HTML comment below it, message will look like this:
183
 
184
  `<!-- YTC ERROR:
185
+ Please check did you set proper Channel ID. You set to show videos from channel, but YouTube does not recognize MyCoolLegacyName as existing and public channel.
 
 
 
186
  -->`
187
 
188
+ Do exactly what message says - check and correct Channel ID in default settings/widget/shortcode.
189
 
190
  `<!-- YTC ERROR:
191
+ Check YouTube Data API Key restrictions, empty cache if enabled by appending in browser address bar parameter ?ytc_force_recache=1
 
 
192
  -->`
193
 
 
 
 
 
 
 
 
 
 
 
 
194
  1. Try to remove restrictions by referer or IP in your **YouTube Data API Key** and refresh page after couple minutes.
195
+ 1. If that does not help, please try to create new API Key for Server w/o restrictions (not to regenerate existing one).
196
 
197
  If there is no `YTC ERROR` code in HTML source, visit [Google API Explorer](https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.playlistItems.list?part=snippet&maxResults=5&playlistId=) and append:
198
 
204
  Note that all four resources are *playlists* (including channel), so append mentioned ID to field **playlistId** (not to **id**), and click **Execute** button at the bottom of that page.
205
 
206
  1. If you receive some error, fix settings.
207
+ 1. If there is no error but you do not get any video in results - contact Google Support.
208
  1. If there are video results but not displayed with YouTube Channel plugin - [contact us](https://wordpress.org/support/plugin/youtube-channel)
209
 
210
  = Where to find correct Channel ID and/or Vanity custom Name? =
273
  If you don't wish to pay for enhancements (then you don't care would that be implemented in a week, month, year or so), then send new [Support topic](https://wordpress.org/support/plugin/youtube-channel) with *Topic title* in format **[Feature Request] ...**
274
 
275
  == Changelog ==
276
+
277
+ = 3.0.7.2 (2015-05-24) =
278
+ * Add: Error report if we have broken feed on record
279
+ * Add: Report about failed HTTP connections and other problems ocurred when we try to fetch feed
280
+ * Add: DRY of visible errors for Administrator and visitors (Oops message)
281
+
282
  = 3.0.7.1 (2015-05-17/18) =
283
  * Fix: Plugin version number not updated in DB
284
  * Fix: Magnific Popup appear under header on Twenty Eleven theme
youtube-channel.php CHANGED
@@ -4,7 +4,7 @@ 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 or iframe (HTML5) video from YouTube Channel, Liked Videos, Favourites or Playlist.
6
  Author: Aleksandar Urošević
7
- Version: 3.0.7.1
8
  Author URI: http://urosevic.net/
9
  */
10
 
@@ -17,7 +17,7 @@ if ( !class_exists('WPAU_YOUTUBE_CHANNEL') )
17
  {
18
 
19
  const DB_VER = 8;
20
- const VER = '3.0.7.1';
21
 
22
  public $plugin_name = "YouTube Channel";
23
  public $plugin_slug = "youtube-channel";
@@ -297,6 +297,9 @@ if ( !class_exists('WPAU_YOUTUBE_CHANNEL') )
297
 
298
  }
299
 
 
 
 
300
  function enqueue_scripts() {
301
  wp_enqueue_style( 'youtube-channel', plugins_url('assets/css/youtube-channel.min.css', __FILE__), array(), self::VER );
302
 
@@ -448,23 +451,18 @@ function ytc_mute(event){
448
  // Print out YTC block
449
  public function output($instance) {
450
 
451
- // print info about API key to admins
452
- // and "Coming soon..." for visitors
453
  if ( empty($this->defaults['apikey']) ) {
454
- if ( current_user_can('manage_options') ) {
455
- $output[] = sprintf(
456
- __('<strong>%s</strong> version 3+ requires <strong>YouTube DATA API Key</strong> to work. <a href="%s" target="_blank">Learn more here</a>.', 'youtube-channel'),
457
- $this->plugin_name,
458
- 'http://urosevic.net/wordpress/plugins/youtube-channel/#youtube_data_api_key'
459
- );
460
 
461
- } else {
462
- $output[] = "Coming soon...";
463
- $output[] = "<!-- YTC ERROR:\n";
464
- $output[] = "tip: No YouTube Data API Key provided!\n";
465
- $output[] = "-->\n";
466
- }
 
467
  return $output;
 
468
  }
469
 
470
  // 1) Get resource from widget/shortcode
@@ -493,10 +491,7 @@ function ytc_mute(event){
493
  }
494
  // Now check has Playlist ID set or throw error
495
  if ( $playlist == "" ) {
496
- $output[] = "Comming soon...";
497
- $output[] = "<!-- YTC ERROR:\n";
498
- $output[] = "tip: Playlist selected as resource but no Playlist ID provided!\n";
499
- $output[] = "-->\n";
500
  return $output;
501
  }
502
  break;
@@ -514,13 +509,11 @@ function ytc_mute(event){
514
  }
515
  // Now check is Channel ID set or throw error
516
  if ( $channel == '' ) {
517
- $output[] = "Comming soon...";
518
- $output[] = "<!-- YTC ERROR:\n";
519
  if ( $resource == 1 ) { $resource_name = 'Favourited videos'; }
520
  elseif ( $resource == 3 ) { $resource_name = 'Liked videos'; }
521
  else { $resource_name = 'Channel (User uploads)'; }
522
- $output[] = sprintf('tip: %s selected as resource but no Channel ID provided!', $resource_name) . "\n";
523
- $output[] = "-->\n";
524
  return $output;
525
  }
526
 
@@ -549,7 +542,7 @@ function ytc_mute(event){
549
  break;
550
  default: // Channel
551
  $resource_name = 'channel';
552
- $resource_id = preg_replace('/^UC/', 'UU', $channel); //$channel;
553
  }
554
 
555
  // Start output array
@@ -623,9 +616,19 @@ function ytc_mute(event){
623
  }
624
  }
625
 
 
 
 
 
 
 
 
 
 
 
626
  // Predefine `max_items` to prevent undefined notices
627
  $max_items = 0;
628
- if ( ! is_wp_error($json_output) && is_object($json_output) && !empty($json_output->items) ) {
629
  // Sort by date uploaded
630
  $json_entry = $json_output->items;
631
 
@@ -643,39 +646,41 @@ function ytc_mute(event){
643
 
644
  if ($max_items == 0) {
645
 
646
- // is this WP error?
647
- if ( is_wp_error($json_output) ) {
648
- $error_string = $json_output->get_error_message();
649
- $output[] = $error_string;
650
- unset($error_string);
651
- } else {
652
- $output[] = __("Oops, something went wrong.", 'youtube-channel');
653
- // append YouTube DATA API error reason as comment
654
- if ( ! empty($json_output) && is_object($json_output) && !empty($json_output->error->errors) ) {
655
- $output[] = "<!-- YTC ERROR:\n";
656
- $output[] = 'domain: ' . $json_output->error->errors[0]->domain . "\n";
657
- $output[] = 'reason: ' . $json_output->error->errors[0]->reason . "\n";
658
- $output[] = 'message: ' . $json_output->error->errors[0]->message . "\n";
659
-
660
- if ( $json_output->error->errors[0]->reason == 'playlistNotFound' ) {
661
- if ( $resource_name == 'playlist' ) {
662
- $output[] = "tip: Please check did you set existing Playlist ID. We should display videos from {$resource_name} videos, but YouTube does not recognize {$resource_id} as existing and public playlist.\n";
663
- } else {
664
- $output[] = "tip: Please check did you set proper Channel ID. We should display videos from {$resource_name} videos, but YouTube does not recognize your channel ID {$channel} as existing and public resource.\n";
665
- }
666
- }
667
- elseif ( $json_output->error->errors[0]->reason == 'keyInvalid' ) {
668
- $output[] = "tip: Double check YouTube Data API Key on General plugin tab and make sure it`s correct. Check https://wordpress.org/plugins/youtube-channel/installation/\n";
669
- }
670
- elseif ( $json_output->error->errors[0]->reason == 'ipRefererBlocked' ) {
671
- $output[] = "tip: Check YouTube Data API Key restrictions, empty cache if enabled and append in browser address bar parameter ?ytc_force_recache=1\n";
672
- }
673
- elseif ( $json_output->error->errors[0]->reason == 'invalidChannelId' ) {
674
- $output[] = "tip: You have set wrong Channel ID. Fix that in General plugin settings, Widget and/or shortcode. Check https://wordpress.org/plugins/youtube-channel/faq/\n";
675
  }
676
- $output[] = "-->\n";
677
  }
678
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
679
 
680
  } else { // ELSE if ($max_items == 0)
681
 
@@ -751,17 +756,62 @@ function ytc_mute(event){
751
  $wprga = array(
752
  'timeout' => 5 // five seconds only
753
  );
 
754
  $response = wp_remote_get($feed_url, $wprga);
755
- $json = wp_remote_retrieve_body( $response );
756
 
757
- // free some memory
 
 
 
 
 
 
 
 
 
 
 
 
 
758
  unset($response);
759
 
760
  return $json;
761
 
762
  } // END function fetch_youtube_feed($resource_id, $items)
763
 
764
- // function to calculate height by width and ratio
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
765
  function height_ratio($width=306, $ratio) {
766
 
767
  switch ($ratio)
4
  Plugin URI: http://urosevic.net/wordpress/plugins/youtube-channel/
5
  Description: <a href="widgets.php">Widget</a> that display latest video thumbnail or iframe (HTML5) video from YouTube Channel, Liked Videos, Favourites or Playlist.
6
  Author: Aleksandar Urošević
7
+ Version: 3.0.7.2
8
  Author URI: http://urosevic.net/
9
  */
10
 
17
  {
18
 
19
  const DB_VER = 8;
20
+ const VER = '3.0.7.2';
21
 
22
  public $plugin_name = "YouTube Channel";
23
  public $plugin_slug = "youtube-channel";
297
 
298
  }
299
 
300
+ /**
301
+ * Enqueue frontend scripts and styles
302
+ */
303
  function enqueue_scripts() {
304
  wp_enqueue_style( 'youtube-channel', plugins_url('assets/css/youtube-channel.min.css', __FILE__), array(), self::VER );
305
 
451
  // Print out YTC block
452
  public function output($instance) {
453
 
454
+ // Error message if no YouTube Data API Key
 
455
  if ( empty($this->defaults['apikey']) ) {
 
 
 
 
 
 
456
 
457
+ $error_msg = sprintf(
458
+ __('<strong>%s</strong> version 3+ requires <strong>YouTube DATA API Key</strong> to work. <a href="%s" target="_blank">Learn more here</a>.', 'youtube-channel'),
459
+ $this->plugin_name,
460
+ 'http://urosevic.net/wordpress/plugins/youtube-channel/#youtube_data_api_key'
461
+ );
462
+
463
+ $output[] = $this->front_debug($error_msg);
464
  return $output;
465
+
466
  }
467
 
468
  // 1) Get resource from widget/shortcode
491
  }
492
  // Now check has Playlist ID set or throw error
493
  if ( $playlist == "" ) {
494
+ $output[] = $this->front_debug("Playlist selected as resource but no Playlist ID provided!");
 
 
 
495
  return $output;
496
  }
497
  break;
509
  }
510
  // Now check is Channel ID set or throw error
511
  if ( $channel == '' ) {
 
 
512
  if ( $resource == 1 ) { $resource_name = 'Favourited videos'; }
513
  elseif ( $resource == 3 ) { $resource_name = 'Liked videos'; }
514
  else { $resource_name = 'Channel (User uploads)'; }
515
+ $error_msg = sprintf('%s selected as resource but no Channel ID provided!', $resource_name);
516
+ $output[] = $this->front_debug($error_msg);
517
  return $output;
518
  }
519
 
542
  break;
543
  default: // Channel
544
  $resource_name = 'channel';
545
+ $resource_id = preg_replace('/^UC/', 'UU', $channel);
546
  }
547
 
548
  // Start output array
616
  }
617
  }
618
 
619
+ // Prevent further checks if we have WP Error or empty record even after fallback
620
+ if ( is_wp_error($json_output) ) {
621
+ $output[] = $this->front_debug( $json_output->get_error_message() );
622
+ return $output;
623
+ }
624
+ elseif ( empty($json_output) ) {
625
+ $output[] = $this->front_debug(sprintf(__('We have empty record for this feed. Please read <a href="%s" target="_blank">FAQ</a> and if that does not help, contact <a href="%s" target="_blank">support</a>.'), 'https://wordpress.org/plugins/youtube-channel/faq/', 'https://wordpress.org/support/plugin/youtube-channel'));
626
+ return $output;
627
+ }
628
+
629
  // Predefine `max_items` to prevent undefined notices
630
  $max_items = 0;
631
+ if ( is_object($json_output) && ! empty($json_output->items) ) {
632
  // Sort by date uploaded
633
  $json_entry = $json_output->items;
634
 
646
 
647
  if ($max_items == 0) {
648
 
649
+ // append YouTube DATA API error reason as comment
650
+ if ( ! empty($json_output) && is_object($json_output) && ! empty($json_output->error->errors) ) {
651
+
652
+ // Error went in fetch_youtube_feed()
653
+ if ( $json_output->error->errors[0]->reason == 'wpError' ) {
654
+ $error_msg = $json_output->error->errors[0]->message;
655
+ }
656
+ // Playlist error from Google API
657
+ elseif ( $json_output->error->errors[0]->reason == 'playlistNotFound' ) {
658
+ if ( $resource_name == 'playlist' ) {
659
+ $error_msg = "Please check did you set existing <em>Playlist ID</em>. You set to show videos from {$resource_name}, but YouTube does not recognize <strong>{$resource_id}</strong> as existing and public playlist.";
660
+ } else {
661
+ $error_msg = "Please check did you set proper <em>Channel ID</em>. You set to show videos from {$resource_name}, but YouTube does not recognize <strong>{$channel}</strong> as existing and public channel.";
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
662
  }
 
663
  }
664
+ // Invalid YouTube Data API Key
665
+ elseif ( $json_output->error->errors[0]->reason == 'keyInvalid' ) {
666
+ $error_msg = sprintf(__("Double check <em>YouTube Data API Key</em> on <em>General</em> plugin tab and make sure it's correct. Read <a href=\"%s\" target=\"_blank\">Installation</a> document."), 'https://wordpress.org/plugins/youtube-channel/installation/');
667
+ }
668
+ // Restricted access YouTube Data API Key
669
+ elseif ( $json_output->error->errors[0]->reason == 'ipRefererBlocked' ) {
670
+ $error_msg = "Check <em>YouTube Data API Key</em> restrictions, empty cache if enabled by appending in browser address bar parameter <em>?ytc_force_recache=1</em>";
671
+ }
672
+ // (deprecated?) Non existing Channel ID set
673
+ elseif ( $json_output->error->errors[0]->reason == 'invalidChannelId' ) {
674
+ $error_msg = sprintf(__("You have set wrong Channel ID. Fix that in General plugin settings, Widget and/or shortcode. Read <a href=\"%s\" target=\"_blank\">FAQ</a> document."), 'https://wordpress.org/plugins/youtube-channel/faq/');
675
+ }
676
+
677
+ } else { // ELSE ! empty($json_output->error->errors)
678
+
679
+ $error_msg = "Unrecognized error experienced.";
680
+
681
+ } // END ! empty($json_output->error->errors)
682
+
683
+ $output[] = $this->front_debug($error_msg);
684
 
685
  } else { // ELSE if ($max_items == 0)
686
 
756
  $wprga = array(
757
  'timeout' => 5 // five seconds only
758
  );
759
+
760
  $response = wp_remote_get($feed_url, $wprga);
 
761
 
762
+ // If we have WP error, make JSON with error
763
+ if ( is_wp_error($response) ) {
764
+
765
+ $json = "{\"error\":{\"errors\":[{\"reason\":\"wpError\",";
766
+ $json .= "\"message\":\"{$response->get_error_message()}\",";
767
+ $json .= "\"domain\":\"wpRemoteGet\"}]}}";
768
+
769
+ } else {
770
+
771
+ $json = wp_remote_retrieve_body( $response );
772
+
773
+ }
774
+
775
+ // Mree some memory
776
  unset($response);
777
 
778
  return $json;
779
 
780
  } // END function fetch_youtube_feed($resource_id, $items)
781
 
782
+
783
+ /**
784
+ * Print explanation of error for administrators (users with capability manage_options)
785
+ * and hidden message for lower users and visitors
786
+ * @param string $message Error message
787
+ * @return string FOrmatted message for error
788
+ */
789
+ function front_debug($message) {
790
+
791
+ // Show visible error to admin, Oops message to visitors and lower members
792
+ if ( is_user_logged_in() && current_user_can('manage_options') ) {
793
+
794
+ $output = "<p class=\"ytc_error\"><strong>YTC ERROR:</strong> $message</p>";
795
+
796
+ } else {
797
+
798
+ $output = __("Oops, something went wrong.", 'youtube-channel');
799
+ $output .= "<!-- YTC ERROR:\n";
800
+ $output .= strip_tags($message);
801
+ $output .= "\n-->\n";
802
+
803
+ }
804
+
805
+ return $output;
806
+
807
+ } // END function debug($message)
808
+
809
+ /**
810
+ * Calculate height by provided width and aspect ratio
811
+ * @param integer $width Width in pixels
812
+ * @param integer $ratio Selected aspect ratio (1 for 4:3, other for 16:9)
813
+ * @return integer Calculated height in pixels
814
+ */
815
  function height_ratio($width=306, $ratio) {
816
 
817
  switch ($ratio)