| 1 |
<?php |
| 2 |
|
| 3 |
// Exit if accessed directly |
| 4 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 5 |
|
| 6 |
abstract class Vimeography_Core { |
| 7 |
|
| 8 |
const ENDPOINT = 'https://api.vimeo.com/'; |
| 9 |
|
| 10 |
/** |
| 11 |
* Vimeo library instance |
| 12 |
* |
| 13 |
* @var instance |
| 14 |
*/ |
| 15 |
protected $_vimeo; |
| 16 |
|
| 17 |
/** |
| 18 |
* [$_auth description] |
| 19 |
* |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
protected $_auth; |
| 23 |
|
| 24 |
/** |
| 25 |
* The parameters to send in the Vimeo request. |
| 26 |
* |
| 27 |
* @var array |
| 28 |
*/ |
| 29 |
protected $_params = array(); |
| 30 |
|
| 31 |
/** |
| 32 |
* The gallery id associated with the current request, if any. |
| 33 |
* Used to look up the corresponding cache file. |
| 34 |
* |
| 35 |
* @since 2.0 |
| 36 |
* @var [type] |
| 37 |
*/ |
| 38 |
protected $_gallery_id; |
| 39 |
|
| 40 |
/** |
| 41 |
* [$_endpoint description] |
| 42 |
* |
| 43 |
* @var string |
| 44 |
*/ |
| 45 |
protected $_endpoint; |
| 46 |
|
| 47 |
/** |
| 48 |
* Limit a gallery to show only this amount of videos. |
| 49 |
* |
| 50 |
* @var int |
| 51 |
*/ |
| 52 |
protected $_limit = 0; |
| 53 |
|
| 54 |
/** |
| 55 |
* An optional resource string pointing to the video that |
| 56 |
* should be featured in the gallery. |
| 57 |
* |
| 58 |
* @var string |
| 59 |
*/ |
| 60 |
protected $_featured; |
| 61 |
|
| 62 |
|
| 63 |
/** |
| 64 |
* Request the following fields to be returned from the Vimeo API. |
| 65 |
* |
| 66 |
* @var array |
| 67 |
*/ |
| 68 |
public $fields = array( |
| 69 |
'name', |
| 70 |
'uri', |
| 71 |
'link', |
| 72 |
'description', |
| 73 |
'duration', |
| 74 |
'width', |
| 75 |
'height', |
| 76 |
'embed', |
| 77 |
'tags.name', |
| 78 |
'tags.canonical', |
| 79 |
'created_time', |
| 80 |
'stats', |
| 81 |
'pictures', |
| 82 |
'status', |
| 83 |
); |
| 84 |
|
| 85 |
/** |
| 86 |
* Set the class properties from the provided |
| 87 |
* shortcode settings array. |
| 88 |
* |
| 89 |
* @param array $settings |
| 90 |
*/ |
| 91 |
public function __construct($settings) { |
| 92 |
$this->_endpoint = $settings['source']; |
| 93 |
|
| 94 |
if ( isset( $settings['limit'] ) ) { |
| 95 |
$this->_limit = $settings['limit']; |
| 96 |
} |
| 97 |
|
| 98 |
if ( isset( $settings['featured'] ) && ! empty( $settings['featured'] ) ) { |
| 99 |
$this->_featured = '/videos/' . preg_replace( "/[^0-9]/", '', $settings['featured'] ); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* [_verify_vimeo_endpoint description] |
| 105 |
* @param [type] $resource [description] |
| 106 |
* @return [type] [description] |
| 107 |
*/ |
| 108 |
abstract protected function _verify_vimeo_endpoint($resource); |
| 109 |
|
| 110 |
/** |
| 111 |
* Gets the videos for the gallery. |
| 112 |
* |
| 113 |
* @param int $expiration Length that the cache is valid, in seconds |
| 114 |
* @param int $gallery_id |
| 115 |
* @return [type] [description] |
| 116 |
*/ |
| 117 |
public function get_videos($expiration, $gallery_id) { |
| 118 |
|
| 119 |
if ( isset( $_GET['vimeography_nocache'] ) && $_GET['vimeography_nocache'] == 1 ) { |
| 120 |
return $this->fetch(); |
| 121 |
} |
| 122 |
|
| 123 |
require_once VIMEOGRAPHY_PATH . 'lib/deprecated/cache.php'; |
| 124 |
$cache = new Vimeography_Cache($gallery_id, $expiration); |
| 125 |
|
| 126 |
// If the cache file exists, |
| 127 |
if ( $cache->exists() ) { |
| 128 |
|
| 129 |
// and the cache file is expired, |
| 130 |
if ( ( $last_modified = $cache->expired() ) !== false) { |
| 131 |
|
| 132 |
// make the request with a last modified header. |
| 133 |
$result = $this->fetch( $last_modified ); |
| 134 |
|
| 135 |
// Here is where we need to check if $video_set exists, or if it |
| 136 |
// returned a 304, in which case, we can safely update the |
| 137 |
// cache's last modified and return it. |
| 138 |
if ( $result === null ) { |
| 139 |
$result = $cache->renew()->get(); |
| 140 |
} else { |
| 141 |
// Cache the updated results. |
| 142 |
if ( intval( $expiration ) !== 0) { |
| 143 |
$cache->set( $result ); |
| 144 |
} |
| 145 |
} |
| 146 |
} else { |
| 147 |
|
| 148 |
// If it isn't expired, return it. |
| 149 |
$result = $cache->get(); |
| 150 |
} |
| 151 |
} else { |
| 152 |
// If a cache doesn't exist, go get the videos, dude. |
| 153 |
$result = $this->fetch(); |
| 154 |
|
| 155 |
// Cache the results. |
| 156 |
if ( intval( $expiration ) !== 0 && ( ! empty( $result->video_set ) ) ) { |
| 157 |
$result = apply_filters( 'vimeography/cache-videos', $result, $gallery_id ); |
| 158 |
$cache->set( $result ); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
return $result; |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Fetch the videos to be displayed in the Vimeography Gallery. |
| 167 |
* |
| 168 |
* @param $last_modified |
| 169 |
* @return string $response Modified response from Vimeo. |
| 170 |
*/ |
| 171 |
public function fetch( $last_modified = NULL ) { |
| 172 |
|
| 173 |
if ( ! $this->_verify_vimeo_endpoint( $this->_endpoint ) ) { |
| 174 |
throw new Vimeography_Exception( sprintf( __('Endpoint %s is not valid.', 'vimeography'), $this->_endpoint ) ); |
| 175 |
} |
| 176 |
|
| 177 |
$response = $this->_make_vimeo_request($this->_endpoint, $this->_params, $last_modified); |
| 178 |
|
| 179 |
// If 304 not modified, return |
| 180 |
if ( $response == NULL ) { |
| 181 |
return $response; |
| 182 |
} |
| 183 |
|
| 184 |
$video_set = $response->data; |
| 185 |
|
| 186 |
if ( ! empty( $this->_featured ) ) { |
| 187 |
$featured_video = $this->_make_vimeo_request( $this->_featured ); |
| 188 |
$result_set = $this->_arrange_featured_video( $video_set, $featured_video ); |
| 189 |
} else { |
| 190 |
$result_set = $video_set; |
| 191 |
} |
| 192 |
|
| 193 |
unset($response->data); |
| 194 |
$response->video_set = $result_set; |
| 195 |
|
| 196 |
return $response; |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Send a cURL Wordpress request to retrieve the requested data from the Vimeo API. |
| 201 |
* |
| 202 |
* @param string $endpoint Vimeo API endpoint |
| 203 |
* @return array Response Body |
| 204 |
*/ |
| 205 |
private function _make_vimeo_request($endpoint, $params = array(), $last_modified = null) { |
| 206 |
try { |
| 207 |
|
| 208 |
/** |
| 209 |
* Only add request parameters if they don't already exist within |
| 210 |
* a query string in the $endpoint |
| 211 |
*/ |
| 212 |
$query = parse_url($endpoint, PHP_URL_QUERY); |
| 213 |
|
| 214 |
if ( empty( $query ) ) { |
| 215 |
|
| 216 |
// Limit the request to return only the fields that |
| 217 |
// Vimeography themes actually use. |
| 218 |
$fields = apply_filters( 'vimeography.request.fields', $this->fields ); |
| 219 |
|
| 220 |
// Add parameters which are common to all requests |
| 221 |
$params = array_merge( array( |
| 222 |
'fields' => implode( $fields, ',' ), |
| 223 |
'filter' => 'embeddable', |
| 224 |
'filter_embeddable' => 'true', |
| 225 |
), $params ); |
| 226 |
|
| 227 |
} |
| 228 |
|
| 229 |
$headers = array( |
| 230 |
'User-Agent' => sprintf( 'Vimeography loves you (%s)', home_url() ), |
| 231 |
); |
| 232 |
|
| 233 |
if ( $last_modified !== null ) { |
| 234 |
$headers['If-Modified-Since'] = $last_modified; |
| 235 |
} |
| 236 |
|
| 237 |
$response = $this->_vimeo->request( $endpoint, $params, 'GET', true, $headers ); |
| 238 |
|
| 239 |
if ( isset( $response['headers']['X-RateLimit-Limit'] ) ) { |
| 240 |
$this->rate_limit = array( |
| 241 |
'limit' => $response['headers']['X-RateLimit-Limit'], |
| 242 |
'remaining' => $response['headers']['X-RateLimit-Remaining'], |
| 243 |
'reset' => new \DateTime( $response['headers']['X-RateLimit-Reset'] ) |
| 244 |
); |
| 245 |
} |
| 246 |
|
| 247 |
switch ( $response['status'] ) { |
| 248 |
case 200: |
| 249 |
return $response['body']; |
| 250 |
case 304: |
| 251 |
return null; |
| 252 |
case 400: |
| 253 |
throw new Vimeography_Exception( |
| 254 |
__('a bad request made was made. ', 'vimeography') . $response['body']->error |
| 255 |
); |
| 256 |
case 401: |
| 257 |
throw new Vimeography_Exception( |
| 258 |
__('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.', 'vimeography') |
| 259 |
); |
| 260 |
case 403: |
| 261 |
$error_code = $response['body']->error_code; |
| 262 |
$developer_message = $response['body']->developer_message; |
| 263 |
|
| 264 |
$error = sprintf( __('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)', 'vimeography'), $response['headers']['X-Banned-IP'] ); |
| 265 |
$error .= sprintf( __('<br /><br />Error #%1$d: %2$s', 'vimeography'), $error_code, $developer_message ); |
| 266 |
|
| 267 |
throw new Vimeography_Exception( $error ); |
| 268 |
case 404: |
| 269 |
throw new Vimeography_Exception( |
| 270 |
__('the plugin could not retrieve data from the Vimeo API! ', 'vimeography') . $response['body']->error |
| 271 |
); |
| 272 |
case 500: case 503: |
| 273 |
throw new Vimeography_Exception( |
| 274 |
__('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.', 'vimeography') |
| 275 |
); |
| 276 |
default: |
| 277 |
throw new Vimeography_Exception(sprintf(__('Unknown response status %1$d, %2$s', 'vimeography'), $response['status'], $response['body']->error ) ); |
| 278 |
} |
| 279 |
} catch (Exception $e) { |
| 280 |
throw new Vimeography_Exception( |
| 281 |
__('the request to Vimeo failed. ', 'vimeography') . $e->getMessage() |
| 282 |
); |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
/** |
| 288 |
* Arrange the video set to contain the video to be featured at the beginning of the set. |
| 289 |
* |
| 290 |
* @param array $video_set Vimeo Videos |
| 291 |
* @param array $featured_video a Vimeo Video |
| 292 |
* @return string $video_set Arranged array of Vimeo Videos |
| 293 |
*/ |
| 294 |
private function _arrange_featured_video($video_set, $featured_video) { |
| 295 |
// Does the featured video exist in the set? |
| 296 |
// If so, remove it from the set and place at front. |
| 297 |
$found = false; |
| 298 |
|
| 299 |
// We have to do this because if the featured video |
| 300 |
// exists in the collection as a contextual video, |
| 301 |
// it would not be removed if we only compared resouce urls |
| 302 |
// since they would not match. |
| 303 |
$featured_id = str_replace('/', '', strrchr($featured_video->link, '/')); |
| 304 |
|
| 305 |
foreach ($video_set as $key => $video) { |
| 306 |
if (strpos($video->uri, $featured_id) !== false) { |
| 307 |
unset($video_set[$key]); |
| 308 |
$found = true; |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
// If it does not exist, we need to remove the last video in the |
| 313 |
// video set and place the featured video up front. |
| 314 |
if ( $found == false && $this->_limit == count($video_set) ) { |
| 315 |
array_pop($video_set); |
| 316 |
} |
| 317 |
|
| 318 |
// Add the featured video to the front. |
| 319 |
array_unshift( $video_set, $featured_video ); |
| 320 |
|
| 321 |
return array_values( $video_set ); |
| 322 |
} |
| 323 |
} |
| 324 |
|