1 | <?php
|
---|
2 | /*
|
---|
3 | Plugin Name: TubeMatic
|
---|
4 | Plugin URI: http://www.griido.it/tubematic-plugin-wordpress/
|
---|
5 | Description: This plugin is made for automatically embedding videos into a blog post or page.
|
---|
6 | Author: Redazione di Griido.it
|
---|
7 | Version: 1.0
|
---|
8 | Author URI: http://www.griido.it/
|
---|
9 | */
|
---|
10 |
|
---|
11 | require_once("config.php");
|
---|
12 |
|
---|
13 | function shortcode_tubematic($attr = array(), $content = NULL) {
|
---|
14 | global $tm_views_label, $tm_duration_label, $tm_datetime_filer, $tm_by_label;
|
---|
15 | $showtitle=TRUE;
|
---|
16 | $showdate=TRUE;
|
---|
17 | $showcount=TRUE;
|
---|
18 | $showuser=TRUE;
|
---|
19 | $type="";
|
---|
20 | $limit=6;
|
---|
21 | $spanrow=3;
|
---|
22 | $lr='';
|
---|
23 |
|
---|
24 | if (isset($attr['query'])){$q = ereg_replace('[[:space:]]+', '/', trim(strtolower($attr['query'])));}
|
---|
25 | if (isset($attr['limit'])){$limit = (integer) $attr['limit'];}
|
---|
26 | if (isset($attr['spanrow'])){$spanrow = (integer) $attr['spanrow'];}
|
---|
27 | if (isset($attr['type'])){$type = strtolower($attr['type']);}
|
---|
28 | if (isset($attr['orderby'])){$orderby = strtolower($attr['orderby']);}
|
---|
29 | if (isset($attr['keys'])){$keys = urlencode(trim(strtolower($attr['keys'])));}
|
---|
30 | if (isset($attr['showtitle'])){if(strtolower($attr['showtitle'])=='n'){$showtitle=FALSE;}}
|
---|
31 | if (isset($attr['showdate'])){if(strtolower($attr['showdate'])=='n'){$showdate=FALSE;}}
|
---|
32 | if (isset($attr['showcount'])){if(strtolower($attr['showcount'])=='n'){$showcount=FALSE;}}
|
---|
33 | if (isset($attr['showuser'])){if(strtolower($attr['showuser'])=='n'){$showuser=FALSE;}}
|
---|
34 | if (isset($attr['lr'])){$lr = strtolower($attr['lr']);}
|
---|
35 |
|
---|
36 | if ($spanrow>0) $spanrow--; else $spanrow=2;
|
---|
37 | if ($limit<=0) $limit=6;
|
---|
38 |
|
---|
39 | switch ($type) {
|
---|
40 | //The latest videos from a channel or only the videos that match your keywords
|
---|
41 | case "uploads":
|
---|
42 | $feedURL=tubematic_composeurl("http://gdata.youtube.com/feeds/api/users/$q/uploads",$limit,$orderby,$lr);
|
---|
43 | /*
|
---|
44 | use this parameter to create a filter for a feed. Obtain only the videos that contain your query in the metadata (title, tags, description).
|
---|
45 | */
|
---|
46 | if (!empty($keys)) $feedURL.="&q=$keys";
|
---|
47 | break;
|
---|
48 | // Search in a category
|
---|
49 | case "category":
|
---|
50 | $feedURL = tubematic_composeurl("http://gdata.youtube.com/feeds/api/videos/-/{$q}",$limit,$orderby,$lr);
|
---|
51 | break;
|
---|
52 | // Search feeds
|
---|
53 | case "searchfeed":
|
---|
54 | $feedURL = tubematic_composeurl("http://gdata.youtube.com/feeds/api/videos",$limit,$orderby,$lr);
|
---|
55 | /*
|
---|
56 | use this parameter to create a filter for a feed. Obtain only the videos that contain your query in the metadata (title, tags, description).
|
---|
57 | */
|
---|
58 | if (!empty($keys)) $feedURL.="&q=$keys";
|
---|
59 | break;
|
---|
60 | default:
|
---|
61 | $feedURL="";
|
---|
62 | break;
|
---|
63 | }
|
---|
64 |
|
---|
65 | if (empty($feedURL)) return ""; // nothing to do
|
---|
66 |
|
---|
67 | // read feed into SimpleXML object
|
---|
68 | $sxml = @simplexml_load_file($feedURL);
|
---|
69 | //echo $sxml->title;
|
---|
70 | //iterate over entries in feed
|
---|
71 | $span=0;
|
---|
72 | $vg.="<table>";
|
---|
73 |
|
---|
74 | if (!$sxml) return ""; // uhm.. something was bad
|
---|
75 |
|
---|
76 | foreach ($sxml->entry as $entry) {
|
---|
77 |
|
---|
78 | // get date
|
---|
79 | $published=$entry->published;
|
---|
80 |
|
---|
81 | // get nodes in media: namespace for media information
|
---|
82 | $media = $entry->children('http://search.yahoo.com/mrss/');
|
---|
83 |
|
---|
84 | // get video player URL
|
---|
85 | $attrs = $media->group->player->attributes();
|
---|
86 | $watch = $attrs['url'];
|
---|
87 |
|
---|
88 | // get video thumbnail
|
---|
89 | $attrs = $media->group->thumbnail[0]->attributes();
|
---|
90 | $thumbnail = $attrs['url'];
|
---|
91 |
|
---|
92 | // get <yt:duration> node for video length
|
---|
93 | $yt = $media->children('http://gdata.youtube.com/schemas/2007');
|
---|
94 | $attrs = $yt->duration->attributes();
|
---|
95 | $length = $attrs['seconds'];
|
---|
96 |
|
---|
97 | // get <yt:stats> node for viewer statistics
|
---|
98 | $yt = $entry->children('http://gdata.youtube.com/schemas/2007');
|
---|
99 | $attrs = $yt->statistics->attributes();
|
---|
100 |
|
---|
101 | //if (!in_array($orderby,array("published","updated"))) $viewCount = $attrs->viewCount;
|
---|
102 | //else $showcount=false;
|
---|
103 | $viewCount = @$attrs->viewCount;
|
---|
104 | if (!$viewCount) $showcount=false;
|
---|
105 |
|
---|
106 | // get <gd:rating> node for video ratings
|
---|
107 | /*$gd = $entry->children('http://schemas.google.com/g/2005');
|
---|
108 | if ($gd->rating) {
|
---|
109 | $attrs = $gd->rating->attributes();
|
---|
110 | $rating = $attrs['average'];
|
---|
111 | } else {
|
---|
112 | $rating = 0;
|
---|
113 | }*/
|
---|
114 |
|
---|
115 | if ($span==0) $vg.="<tr>";
|
---|
116 | $vg .= '<td style="border:1px dashed gray;"><table>
|
---|
117 | ';
|
---|
118 | $vg.='<tr><td valign="top" style="padding:3px"><a href="'.$watch.'" target="youtube"><img src="'.$thumbnail.'" /></a></td><td valign="top"><table cellspacing="0" cellpadding="0">';
|
---|
119 | if($showtitle){$vg.='<tr><td><span style="font-size:10px; font-weight:bold;">'.$media->group->title.'</span></td></tr>';}
|
---|
120 | if($showuser){$vg.='<tr><td><span style="font-size:10px; font-weight:normal;">'.$tm_by_label.'<a href="http://www.youtube.com/user/'.$entry->author->name.'">'.$entry->author->name.'</a></span></td></tr>';}
|
---|
121 | if($showdate){$vg.='<tr><td><span style="font-size:10px;">'.strftime($tm_datetime_filer,strtotime($published)).'</span></td></tr>';}
|
---|
122 | if($showcount){$vg.='<tr><td><span style="font-size:10px;">'.$tm_views_label.$viewCount.'</span></td></tr>';}
|
---|
123 | $vg.='<tr><td><span style="font-size:10px;">'.$tm_duration_label.$length.'s</span></td></tr>';
|
---|
124 | $vg.='</table></td></tr></table>
|
---|
125 | ';
|
---|
126 | if ($span==$spanrow) { $vg.="</td></tr>"; $span=0; }
|
---|
127 | else $span++;
|
---|
128 | }
|
---|
129 | $vg.="</table>";
|
---|
130 | return $vg;
|
---|
131 | }
|
---|
132 |
|
---|
133 | function tubematic_composeurl($feedURL,$limit,$orderby,$lr='') {
|
---|
134 | /*
|
---|
135 | orderby={updated, viewCount, rating, relevance}: sort the items from feed by upload date, number of views, rating or relevance
|
---|
136 | */
|
---|
137 | if (!in_array($orderby,array("updated","rating","relevance","published"))) $orderby="viewCount";
|
---|
138 | $feedURL.="?orderby=$orderby";
|
---|
139 | /*
|
---|
140 | the maximum number of items from a feed (by default, a feed includes only 25 items)
|
---|
141 | */
|
---|
142 | $feedURL.="&max-results=$limit";
|
---|
143 | /*
|
---|
144 | the lr parameter restricts the search to videos that have a title, description or keywords in a specific language. Valid values for the lr parameter are ISO 639-1 two-letter language codes. You can also use the values zh-Hans for simplified Chinese and zh-Hant for traditional Chinese.
|
---|
145 | */
|
---|
146 | if (!empty($lr)) $feedURL.="&lr=$lr";
|
---|
147 | return $feedURL;
|
---|
148 | }
|
---|
149 |
|
---|
150 | add_shortcode('tubematic', 'shortcode_tubematic');
|
---|
151 | ?>
|
---|