PluginProbe
Vimeography: Vimeo Video Gallery WordPress Plugin / 0.7
Vimeography: Vimeo Video Gallery WordPress Plugin v0.7
2.4.9 2.4.8 trunk 0.5.1 0.5.2 0.5.3 0.5.4 0.5.5 0.5.6 0.5.7 0.6 0.6.1 0.6.2 0.6.3 0.6.4 0.6.5 0.6.6 0.6.7 0.6.8 0.6.8.1 0.6.9 0.6.9.1 0.6.9.2 0.7 0.8 All 103 releases
vimeography / lib / core.php

core.php in Vimeography: Vimeo Video Gallery WordPress Plugin 0.7, at lib/core.php

325 lines 8.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // Require Mustache.php
4 if (! class_exists('Mustache'))
5 require_once(VIMEOGRAPHY_PATH . '/vendor/mustache/Mustache.php');
6
7 class Vimeography_Core extends Vimeography
8 {
9 const ENDPOINT = 'http://vimeo.com/api/v2/';
10 const FORMAT = '.json';
11
12 protected $_source;
13 protected $_type;
14
15 protected $_theme;
16 protected $_limit;
17 protected $_featured;
18 protected $_gallery_id;
19 protected $_width;
20
21 protected $_debug = FALSE;
22
23 public static function factory($class, $settings)
24 {
25 require_once(VIMEOGRAPHY_PATH .'lib/'. $class . '.php');
26 $class_name = 'Vimeography_'. ucfirst($class);
27
28 if (class_exists($class_name))
29 {
30 return new $class_name($settings);
31 }
32 else
33 {
34 throw new Vimeography_Exception('Class not found: '.$class_name);
35 }
36 }
37
38 public function __construct($settings)
39 {
40 require_once(VIMEOGRAPHY_PATH .'lib/exception.php');
41
42 $this->_theme = $settings['theme'];
43 $this->_featured = $settings['featured'];
44 $this->_source = $settings['source'];
45 $this->_limit = $settings['limit'];
46 $this->_gallery_id = $settings['id'];
47 $this->_width = $settings['width'];
48 }
49
50 /**
51 * Overload the constructed debugger to print the data instead of render it.
52 *
53 * @access public
54 * @param mixed $debug (default: TRUE)
55 * @return void
56 */
57 public function debug($debug = TRUE)
58 {
59 $this->_debug = $debug;
60 return $this;
61 }
62
63 /**
64 * Retrieves the requested data from Vimeo API.
65 * This is called by the class loaded in the factory method.
66 *
67 * TODO: This could potentially return a 404 page, and we don't want that, nor do we want to show it in the exception.
68 * @access protected
69 * @param mixed $data
70 * @return void
71 */
72 protected function _retrieve()
73 {
74 $urls = array();
75 $urls[] = $this->_build_url($this->_source);
76
77 if (!empty($this->_featured))
78 $urls[] = self::ENDPOINT.'video/'.$this->_featured.self::FORMAT;
79
80 $result = array();
81 $videos = '';
82 $main_request = TRUE;
83
84 foreach ($urls as $url)
85 {
86 if ($main_request == TRUE)
87 {
88 $number_of_requests = ceil($this->_limit / 20);
89
90 for ($video_page = 1; $video_page <= $number_of_requests; $video_page++)
91 {
92 $response = $this->_make_vimeo_request($url, $video_page);
93
94 if ($video_page == 1)
95 {
96 // If the limit is less than the total videos, we need to remove some videos.
97 if ($this->_limit < count(json_decode($response)))
98 {
99 $video_object = json_decode($response);
100
101 for ($video_to_delete = (count($video_object) - 1); $video_to_delete >= $this->_limit; $video_to_delete--)
102 {
103 unset($video_object[$video_to_delete]);
104 }
105 $response = json_encode($video_object);
106 }
107
108 $videos .= $response;
109 }
110 else
111 {
112 $videos = str_replace(']', ',', $videos);
113
114 if ($this->_limit < ($video_page * 20))
115 {
116 $video_object = json_decode($response);
117
118 for ($video_to_delete = count($video_object) - 1; $video_to_delete >= $this->_limit - (($video_page * 20) / 2); $video_to_delete--)
119 {
120 unset($video_object[$video_to_delete]);
121 }
122 $response = json_encode($video_object);
123 }
124
125 $response = str_replace('[', ' ', $response);
126 $videos = $videos.$response;
127 }
128
129 // This stops the next request from occuring if the total
130 // video count on the Vimeo source is less than what the
131 // user specified that they would like to see
132 if (count(json_decode($videos)) < 20) break;
133
134 }
135 $main_request = FALSE;
136 $result[] = $videos;
137 }
138 else
139 {
140 // A featured video was set, let's get it.
141 $featured_video = $this->_make_vimeo_request($url, 1);
142
143 // Now, we need to check if the featured video already exists in
144 // the main video request object.
145 $video_to_check = json_decode($featured_video, true);
146 $video_object = json_decode($result[0], true);
147 $video_found = FALSE;
148
149 // If it does exist, we should remove it.
150 foreach ($video_object as $video_key => $video_data)
151 {
152 if ($video_data['id'] === $video_to_check[0]['id'])
153 {
154 unset($video_object[$video_key]);
155 $video_found = TRUE;
156 }
157 }
158
159 // If it does not exist, we need to remove the last video in the
160 // array to make room for the featured video.
161 if ($video_found == FALSE)
162 unset($video_object[count($video_object) - 1]);
163
164 $result[0] = json_encode(array_values($video_object));
165 $result[] = $featured_video;
166 }
167
168 }
169 return $result;
170 }
171
172 /**
173 * Build the endpoint url based on the provided Vimeo URL.
174 *
175 * @access private
176 * @param mixed $data
177 * @return void
178 */
179 private function _build_url($source)
180 {
181
182 $scheme = parse_url($source);
183 if (empty($scheme['scheme'])) $source = 'https://' . $source;
184
185 if ((($url = parse_url($source)) !== FALSE) && (preg_match('~vimeo[.]com$~', $url['host']) > 0))
186 {
187 // The URL is a valid Vimeo URL. Break it into an array containing the URL parts
188 $url = array_filter(explode('/', $url['path']), 'strlen');
189
190 // If the array doesn't contain one of the following strings, it
191 // must be either a user or a video
192 if (in_array($url[1], array('album', 'channels', 'groups')) !== TRUE)
193 {
194 if (is_numeric($url[1]))
195 {
196 array_unshift($url, 'video');
197 }
198 else
199 {
200 array_unshift($url, 'users');
201 }
202 }
203
204 // Put the URL parts into a conventional array
205 $parts = array('type' => rtrim(array_shift($url), 's'), 'name' => array_shift($url));
206 }
207 else
208 {
209 throw new Vimeography_Exception('You must provide a valid Vimeo source from where to retrieve Vimeo videos.');
210 }
211
212 switch ($parts['type'])
213 {
214 case 'album':
215 $result = 'album/'.$parts['name'].'/'.$this->_type;
216 break;
217 case 'channel':
218 $result = 'channel/'.$parts['name'].'/'.$this->_type;
219 break;
220 case 'group':
221 $result = 'group/'.$parts['name'].'/'.$this->_type;
222 break;
223 case 'user':
224 $result = $parts['name'].'/'.$this->_type;
225 break;
226 case 'video':
227 $result = 'video/'.$parts['name'];
228 break;
229 default:
230 throw new Vimeography_Exception($parts['type'].' is not a valid Vimeo source parameter.');
231 break;
232 }
233
234 return self::ENDPOINT.$result.self::FORMAT;
235 }
236
237 /**
238 * Send a cURL Wordpress request to retrieve the requested data from the Vimeo simple API.
239 *
240 * @access private
241 * @param mixed $url
242 * @param mixed $page
243 * @return void
244 */
245 private function _make_vimeo_request($url, $page)
246 {
247 $response = wp_remote_get($url.'?page='.$page);
248
249 if (isset($response->errors))
250 {
251 foreach ($response->errors as $error)
252 {
253 throw new Vimeography_Exception('the plugin did not retrieve data from the Vimeo API! '. $error[0]);
254 }
255 }
256
257 if (strpos($response['body'], 'not found'))
258 throw new Vimeography_Exception('the plugin could not retrieve data from the Vimeo API! '. $response['body']);
259
260 return $response['body'];
261 }
262
263 public function render($data)
264 {
265 if (! $this->_debug)
266 {
267 if (! isset($this->_theme))
268 throw new Vimeography_Exception('You must specify a theme in either the admin panel or the shortcode.');
269
270 if (!@require_once(VIMEOGRAPHY_THEME_PATH . $this->_theme . '/'.$this->_theme.'.php'))
271 throw new Vimeography_Exception('The "'.$this->_theme.'" theme does not exist or is improperly structured.');
272
273 $class = 'Vimeography_Themes_'.ucfirst($this->_theme);
274
275 if (!class_exists($class))
276 throw new Vimeography_Exception('The "'.$this->_theme.'" theme class does not exist or is improperly structured.');
277
278 $mustache = new $class;
279 $theme = $this->_load_theme($this->_theme);
280
281 $mustache->data = json_decode($data[0]);
282
283 if (isset($data[1]))
284 {
285 // featured video option is set
286 $featured = json_decode($data[1]);
287 }
288 else
289 {
290 $data = json_decode($data[0]);
291 $featured = $data[0];
292 }
293
294 $mustache->featured = $featured;
295 $mustache->gallery_id = $this->_gallery_id;
296 $mustache->gallery_width = $this->_width;
297
298 return $mustache->render($theme);
299 }
300 else
301 {
302 echo '<h1>Vimeography Debug</h1>';
303 echo '<pre>';
304 print_r(json_decode($data));
305 echo '</pre>';
306 die;
307 }
308 }
309
310 /**
311 * Retrieves the contents of a Vimeography theme's mustache template.
312 *
313 * @access private
314 * @static
315 * @param mixed $name
316 * @return void
317 */
318 private static function _load_theme($name)
319 {
320 $path = VIMEOGRAPHY_THEME_PATH . $name . '/videos.mustache';
321 if (! $result = @file_get_contents($path))
322 throw new Vimeography_Exception('The gallery template for the "'.$name.'" theme cannot be found.');
323 return $result;
324 }
325 }