PluginProbe
Vimeography: Vimeo Video Gallery WordPress Plugin / 0.5.7
Vimeography: Vimeo Video Gallery WordPress Plugin v0.5.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.5.7, at lib/core.php

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