PluginProbe
Vimeography: Vimeo Video Gallery WordPress Plugin / 2.2
Vimeography: Vimeo Video Gallery WordPress Plugin v2.2
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 2.2, at lib/core.php

479 lines 12.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Vimeography;
4
5 abstract class Core
6 {
7 public $version = '2.0';
8
9 protected $_endpoint = 'https://api.vimeo.com/';
10
11 /**
12 * Vimeo library instance
13 *
14 * @var instance
15 */
16 protected $_vimeo;
17
18 /**
19 * Access token to send along with the Vimeo request.
20 *
21 * @var string
22 */
23 protected $_auth;
24
25 /**
26 * The parameters to send in the Vimeo request.
27 *
28 * @var array
29 */
30 protected $_params = array();
31
32 /**
33 * The resource to request in the Vimeo request
34 *
35 * example: `/channels/staffpicks/videos`
36 *
37 * @var string
38 */
39 protected $_resource;
40
41 /**
42 * Limit a gallery to show only this amount of videos.
43 *
44 * @var int
45 */
46 protected $_limit = 0;
47
48 /**
49 * An optional resource string pointing to the video that
50 * should be featured in the gallery.
51 *
52 * @var string
53 */
54 protected $_featured;
55
56 /**
57 * [$_cache description]
58 * @var [type]
59 */
60 protected $_cache;
61
62 /**
63 * Whether or not to bypass checking and setting
64 * the cache with relevant video data.
65 *
66 * Can be set with $engine->skip_cache()->fetch();
67 *
68 * @var bool
69 */
70 public $skip_cache = false;
71
72 /**
73 * Length of time that a cache file is good for,
74 * in seconds.
75 *
76 * @var integer
77 */
78 private $_expiration = 3600;
79
80 /**
81 * Request the following fields to be returned from the Vimeo API.
82 *
83 * @var array
84 */
85 public $fields = array(
86 'name',
87 'uri',
88 'link',
89 'description',
90 'duration',
91 'width',
92 'height',
93 'embed',
94 'tags.name',
95 'tags.canonical',
96 'created_time',
97 'stats',
98 'pictures',
99 'status',
100 'user.account'
101 );
102
103 /**
104 * Set the class properties from the provided
105 * shortcode settings array.
106 */
107 public function __construct($engine)
108 {
109 $this->gallery_id = $engine->gallery_id;
110 $this->gallery_settings = $engine->gallery_settings;
111
112 $this->_resource = $this->gallery_settings['source'];
113
114 if (isset($this->gallery_settings['limit'])) {
115 $this->_limit = $this->gallery_settings['limit'];
116 }
117
118 if (
119 isset($this->gallery_settings['featured']) &&
120 !empty($this->gallery_settings['featured'])
121 ) {
122 $this->_featured =
123 '/videos/' .
124 preg_replace("/[^0-9]/", '', $this->gallery_settings['featured']);
125 }
126
127 if (isset($engine->gallery_settings['cache'])) {
128 $this->_expiration = intval($engine->gallery_settings['cache']);
129 }
130
131 require_once VIMEOGRAPHY_PATH . 'lib/cache.php';
132 $this->_cache = new Cache($engine);
133 }
134
135 /**
136 * [_verify_vimeo_resource description]
137 * @param [type] $resource [description]
138 * @return [type] [description]
139 */
140 abstract protected function _verify_vimeo_resource($resource);
141
142 /**
143 * Retrieves the videos for the gallery.
144 *
145 * @return [type] [description]
146 */
147 public function get_videos()
148 {
149 if (isset($_GET['vimeography_nocache']) || $this->skip_cache === true) {
150 return $this->fetch();
151 }
152
153 // If the cache file exists,
154 if ($this->_cache->exists()) {
155 // and the cache file is expired,
156 if ($this->_cache->expired() === true) {
157 // make the request with a last modified header.
158 $result = $this->fetch($this->_cache->last_modified);
159
160 // Here is where we need to check if $video_set exists, or if it
161 // returned a 304, in which case, we can safely update the
162 // cache's last modified and return it.
163 if ($result === null) {
164 $result = $this->_cache->renew()->get();
165 } else {
166 // Cache the updated results if we can.
167 $this->_set_cache($result);
168 }
169 } else {
170 // If it isn't expired, return it.
171 $result = $this->_cache->get();
172
173 $result = apply_filters(
174 'vimeography.pro.paginate',
175 $result,
176 $this->gallery_id
177 );
178 }
179 } else {
180 // If a cache doesn't exist, go get the videos, dude.
181 $result = $this->fetch();
182
183 // Cache the results if we can.
184 $this->_set_cache($result);
185 }
186
187 return $result;
188 }
189
190 /**
191 * Fetch the videos to be displayed in the Vimeography Gallery.
192 *
193 * @param $last_modified
194 * @return string $response Modified response from Vimeo.
195 */
196 public function fetch($last_modified = null)
197 {
198 if (!$this->_verify_vimeo_resource($this->_resource)) {
199 throw new \Vimeography_Exception(
200 sprintf(
201 __('The "%s" resource is not valid.', 'vimeography'),
202 $this->_resource
203 )
204 );
205 }
206
207 $response = $this->_make_vimeo_request(
208 $this->_resource,
209 $this->_params,
210 $last_modified
211 );
212
213 // If 304 not modified, return
214 if ($response === null) {
215 return $response;
216 }
217
218 $video_set = $response->data;
219
220 if (!empty($this->_featured)) {
221 $featured_video = $this->_make_vimeo_request($this->_featured);
222 $result_set = $this->_arrange_featured_video($video_set, $featured_video);
223 } else {
224 $result_set = $video_set;
225 }
226
227 unset($response->data);
228 $response->video_set = $result_set;
229
230 return $response;
231 }
232
233 /**
234 * Send a cURL Wordpress request to retrieve the requested data from the Vimeo API.
235 *
236 * @param string $endpoint Vimeo API endpoint
237 * @return array Response Body
238 */
239 private function _make_vimeo_request(
240 $endpoint,
241 $params = array(),
242 $last_modified = null
243 ) {
244 try {
245 /**
246 * Limit the request to return only the fields that
247 * Vimeography themes actually use.
248 *
249 * @var [type]
250 */
251 $fields = apply_filters('vimeography.request.fields', $this->fields);
252
253 /**
254 * Add parameters which are common to all requests
255 * Note: Signature changed in PHP7.4.0, but we still support 5.3+
256 */
257 if (version_compare(phpversion(), '7.4.0', '<')) {
258 // php version isn't high enough
259 $imploded_fields = implode($fields, ',');
260 } else {
261 $imploded_fields = implode(',', $fields);
262 }
263
264 $params = array_merge(
265 array(
266 'fields' => $imploded_fields
267 ),
268 $params
269 );
270
271 /**
272 * Allows Vimeography to disclude videos in the request response
273 * that cannot be embedded due to their privacy settings or domain restrictions.
274 */
275 $filter = apply_filters(
276 'vimeography.request.privacy.filter',
277 'embeddable',
278 $this->gallery_id,
279 $this->gallery_settings
280 );
281
282 if ($filter === 'embeddable') {
283 $params['filter'] = 'embeddable';
284 $params['filter_embeddable'] = 'true';
285 }
286
287 /**
288 * Set the headers to send along with the Vimeo request.
289 */
290 $headers = array(
291 'User-Agent' => sprintf('Vimeography loves you (%s)', home_url())
292 );
293
294 if ($last_modified !== null) {
295 $headers['If-Modified-Since'] = $last_modified;
296 }
297
298 $headers = apply_filters(
299 'vimeography.request.headers',
300 $headers,
301 $this->gallery_id,
302 $this->gallery_settings
303 );
304
305 /**
306 * Perform the request.
307 */
308 $response = $this->_vimeo->request(
309 $endpoint,
310 $params,
311 'GET',
312 true,
313 $headers
314 );
315
316 do_action(
317 'vimeography.request.response',
318 $response,
319 $this->gallery_id,
320 $this->gallery_settings,
321 $endpoint,
322 $params,
323 $headers
324 );
325
326 // if ( isset( $response['headers']['X-RateLimit-Limit'] ) ) {
327 // $reset_date = new \DateTime();
328 // $reset_date->setTimestamp( $response['headers']['X-RateLimit-Reset'] );
329
330 // $this->rate_limit = array(
331 // 'limit' => $response['headers']['X-RateLimit-Limit'],
332 // 'remaining' => $response['headers']['X-RateLimit-Remaining'],
333 // 'reset' => $reset_date,
334 // );
335 // }
336
337 switch ($response['status']) {
338 case 200:
339 return $response['body'];
340 case 304:
341 return null;
342 case 400:
343 throw new \Vimeography_Exception(
344 __('a bad request made was made. ', 'vimeography') .
345 $response['body']->error
346 );
347 case 401:
348 throw new \Vimeography_Exception(
349 __(
350 'an invalid token was used for the API request. Try removing your Vimeo token on the Vimeography Pro page and following the steps again to create a Vimeo app.',
351 'vimeography'
352 )
353 );
354 case 403:
355 if ($response['body']->error_code) {
356 $error_code = $response['body']->error_code;
357 $developer_message = $response['body']->developer_message;
358
359 $error = sprintf(
360 __(
361 'Your server\'s IP address (%1$s) is currently banned from using the Vimeo API. Please contact Vimeo support at https://vimeo.com/help/contact for more information. Make sure you include your server IP address in your support request. (%1$s)',
362 'vimeography'
363 ),
364 $response['headers']['X-Banned-IP']
365 );
366 $error .= sprintf(
367 __('<br /><br />Error #%1$d: %2$s', 'vimeography'),
368 $error_code,
369 $developer_message
370 );
371 } else {
372 $error = __(
373 'This site does not have the proper permissions to access that Vimeo collection.',
374 'vimeography'
375 );
376 }
377
378 throw new \Vimeography_Exception($error);
379 case 404:
380 throw new \Vimeography_Exception(
381 __(
382 'the plugin could not retrieve data from the Vimeo API! ',
383 'vimeography'
384 ) . $response['body']->error
385 );
386 case 429:
387 throw new \Vimeography_Exception(
388 __(
389 'too many requests to Vimeo, please wait a moment and try again.',
390 'vimeography'
391 )
392 );
393 case 500:
394 case 503:
395 throw new \Vimeography_Exception(
396 __(
397 'looks like Vimeo is having some API issues. Try reloading, or, check back in a few minutes. You can also check http://vimeostatus.com for live updates.',
398 'vimeography'
399 )
400 );
401 default:
402 throw new \Vimeography_Exception(
403 sprintf(
404 __('Unknown response status %1$d, %2$s', 'vimeography'),
405 $response['status'],
406 $response['body']->error
407 )
408 );
409 }
410 } catch (Exception $e) {
411 throw new \Vimeography_Exception(
412 __('the request to Vimeo failed. ', 'vimeography') . $e->getMessage()
413 );
414 }
415 }
416
417 /**
418 * Arrange the video set to contain the video to be featured at the beginning of the set.
419 *
420 * @param array $video_set Vimeo Videos
421 * @param array $featured_video a Vimeo Video
422 * @return string $video_set Arranged array of Vimeo Videos
423 */
424 private function _arrange_featured_video($video_set, $featured_video)
425 {
426 // Does the featured video exist in the set?
427 // If so, remove it from the set and place at front.
428 $found = false;
429
430 // We have to do this because if the featured video
431 // exists in the collection as a contextual video,
432 // it would not be removed if we only compared resouce urls
433 // since they would not match.
434 $featured_id = str_replace('/', '', strrchr($featured_video->link, '/'));
435
436 foreach ($video_set as $key => $video) {
437 if (strpos($video->uri, $featured_id) !== false) {
438 unset($video_set[$key]);
439 $found = true;
440 }
441 }
442
443 // If it does not exist, we need to remove the last video in the
444 // video set and place the featured video up front.
445 if ($found == false && $this->_limit == count($video_set)) {
446 array_pop($video_set);
447 }
448
449 // Add the featured video to the front.
450 array_unshift($video_set, $featured_video);
451
452 return array_values($video_set);
453 }
454
455 /**
456 * Set the cache file contents if our
457 * gallery settings and response data meet all
458 * of the required criteria.
459 *
460 * @param array $data
461 */
462 private function _set_cache($data)
463 {
464 if (
465 !empty($data->video_set) &&
466 $data->page === 1 &&
467 $this->_expiration !== 0 &&
468 isset($this->gallery_id)
469 ) {
470 $data = apply_filters(
471 'vimeography/cache-videos',
472 $data,
473 $this->gallery_id
474 );
475 $this->_cache->set($data);
476 }
477 }
478 }
479