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