| 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 |
* Build the endpoint url based on the provided information. |
| 64 |
* |
| 65 |
* @access protected |
| 66 |
* @param mixed $data |
| 67 |
* @return void |
| 68 |
*/ |
| 69 |
protected function _build_url($source) |
| 70 |
{ |
| 71 |
switch ($source) |
| 72 |
{ |
| 73 |
case 'album': |
| 74 |
$result = 'album/'.$this->_named.'/'.$this->_type; |
| 75 |
break; |
| 76 |
case 'channel': |
| 77 |
$result = 'channel/'.$this->_named.'/'.$this->_type; |
| 78 |
break; |
| 79 |
case 'group': |
| 80 |
$result = $this->_named.'/'.$this->_type; |
| 81 |
break; |
| 82 |
case 'user': |
| 83 |
$result = $this->_named.'/'.$this->_type; |
| 84 |
break; |
| 85 |
case 'video': |
| 86 |
$result = 'video/'.$this->_named; |
| 87 |
break; |
| 88 |
default: |
| 89 |
if (empty($source)) |
| 90 |
{ |
| 91 |
throw new Vimeography_Exception('You must provide a source from where to retrieve Vimeo videos.'); |
| 92 |
} |
| 93 |
else |
| 94 |
{ |
| 95 |
throw new Vimeography_Exception($source.' is not a valid Vimeo source parameter.'); |
| 96 |
} |
| 97 |
break; |
| 98 |
} |
| 99 |
|
| 100 |
return self::ENDPOINT.$result.self::FORMAT; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Retrieves the requested data from Vimeo API. |
| 105 |
* |
| 106 |
* TODO: This could potentially return a 404 page, and we don't want that, nor do we want to show it in the exception. |
| 107 |
* @access protected |
| 108 |
* @param mixed $data |
| 109 |
* @return void |
| 110 |
*/ |
| 111 |
protected function _retrieve() |
| 112 |
{ |
| 113 |
$urls = array(); |
| 114 |
$urls[] = $this->_build_url($this->_source); |
| 115 |
|
| 116 |
if (!empty($this->_featured)) |
| 117 |
$urls[] = self::ENDPOINT.'video/'.$this->_featured.self::FORMAT; |
| 118 |
|
| 119 |
$result = array(); |
| 120 |
$videos = ''; |
| 121 |
$main_request = TRUE; |
| 122 |
|
| 123 |
foreach ($urls as $url) |
| 124 |
{ |
| 125 |
if ($main_request == TRUE) |
| 126 |
{ |
| 127 |
$number_of_requests = ceil($this->_limit / 20); |
| 128 |
|
| 129 |
for ($video_page = 1; $video_page <= $number_of_requests; $video_page++) |
| 130 |
{ |
| 131 |
$response = $this->_make_vimeo_request($url, $video_page); |
| 132 |
|
| 133 |
if ($video_page == 1) |
| 134 |
{ |
| 135 |
// If the limit is less than the total videos, we need to remove some videos. |
| 136 |
if ($this->_limit < count(json_decode($response))) |
| 137 |
{ |
| 138 |
$video_object = json_decode($response); |
| 139 |
|
| 140 |
for ($video_to_delete = (count($video_object) - 1); $video_to_delete >= $this->_limit; $video_to_delete--) |
| 141 |
{ |
| 142 |
unset($video_object[$video_to_delete]); |
| 143 |
} |
| 144 |
$response = json_encode($video_object); |
| 145 |
} |
| 146 |
|
| 147 |
$videos .= $response; |
| 148 |
} |
| 149 |
else |
| 150 |
{ |
| 151 |
$videos = str_replace(']', ',', $videos); |
| 152 |
|
| 153 |
if ($this->_limit < ($video_page * 20)) |
| 154 |
{ |
| 155 |
$video_object = json_decode($response); |
| 156 |
|
| 157 |
for ($video_to_delete = count($video_object) - 1; $video_to_delete >= $this->_limit - (($video_page * 20) / 2); $video_to_delete--) |
| 158 |
{ |
| 159 |
unset($video_object[$video_to_delete]); |
| 160 |
} |
| 161 |
$response = json_encode($video_object); |
| 162 |
} |
| 163 |
|
| 164 |
$response = str_replace('[', ' ', $response); |
| 165 |
$videos = $videos.$response; |
| 166 |
} |
| 167 |
|
| 168 |
// 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 |
| 169 |
if (count(json_decode($videos)) < 20) break; |
| 170 |
|
| 171 |
} |
| 172 |
$main_request = FALSE; |
| 173 |
$result[] = $videos; |
| 174 |
} |
| 175 |
else |
| 176 |
{ |
| 177 |
// Remove the last video in the array to make room for the featured video . NOTE: Don't want to do this when the featured video exists in the video_object |
| 178 |
/*$video_object = json_decode($result[0]); |
| 179 |
unset($video_object[count($video_object) - 1]); |
| 180 |
$result[0] = json_encode($video_object);*/ |
| 181 |
|
| 182 |
// Make featured vid request here. |
| 183 |
$featured_video = $this->_make_vimeo_request($url, 1); |
| 184 |
$result[] = $featured_video; |
| 185 |
} |
| 186 |
|
| 187 |
} |
| 188 |
return $result; |
| 189 |
} |
| 190 |
|
| 191 |
private function _make_vimeo_request($url, $page) |
| 192 |
{ |
| 193 |
$response = wp_remote_get($url.'?page='.$page); |
| 194 |
|
| 195 |
if (isset($response->errors)) |
| 196 |
{ |
| 197 |
foreach ($response->errors as $error) |
| 198 |
{ |
| 199 |
throw new Vimeography_Exception('the plugin did not retrieve data from the Vimeo API! '. $error[0]); |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
if (strpos($response['body'], 'not found')) |
| 204 |
throw new Vimeography_Exception('the plugin could not retrieve data from the Vimeo API! '. $response['body']); |
| 205 |
|
| 206 |
return $response['body']; |
| 207 |
} |
| 208 |
|
| 209 |
public function render($data) |
| 210 |
{ |
| 211 |
if (! $this->_debug) |
| 212 |
{ |
| 213 |
if (! isset($this->_theme)) |
| 214 |
throw new Vimeography_Exception('You must specify a theme in either the admin panel or the shortcode.'); |
| 215 |
|
| 216 |
if (!@require_once(VIMEOGRAPHY_THEME_PATH . $this->_theme . '/'.$this->_theme.'.php')) |
| 217 |
throw new Vimeography_Exception('The "'.$this->_theme.'" theme does not exist or is improperly structured.'); |
| 218 |
|
| 219 |
$class = 'Vimeography_Themes_'.ucfirst($this->_theme); |
| 220 |
|
| 221 |
if (!class_exists($class)) |
| 222 |
throw new Vimeography_Exception('The "'.$this->_theme.'" theme class does not exist or is improperly structured.'); |
| 223 |
|
| 224 |
$mustache = new $class; |
| 225 |
$theme = $this->_load_theme($this->_theme); |
| 226 |
|
| 227 |
$mustache->data = json_decode($data[0]); |
| 228 |
|
| 229 |
if (isset($data[1])) |
| 230 |
{ |
| 231 |
// featured video option is set |
| 232 |
$featured = json_decode($data[1]); |
| 233 |
|
| 234 |
// check if featured video is in the source array, and if so, remove it to avoid duplicates. |
| 235 |
$i = 0; |
| 236 |
foreach ($mustache->data as $video) |
| 237 |
{ |
| 238 |
if ($video->id === $featured[0]->id) |
| 239 |
unset($mustache->data[$i]); |
| 240 |
$i++; |
| 241 |
} |
| 242 |
} |
| 243 |
else |
| 244 |
{ |
| 245 |
$data = json_decode($data[0]); |
| 246 |
$featured = $data[0]; |
| 247 |
} |
| 248 |
|
| 249 |
$mustache->featured = $featured; |
| 250 |
$mustache->gallery_id = $this->_gallery_id; |
| 251 |
|
| 252 |
return $mustache->render($theme); |
| 253 |
} |
| 254 |
else |
| 255 |
{ |
| 256 |
echo '<h1>Vimeography Debug</h1>'; |
| 257 |
echo '<pre>'; |
| 258 |
print_r(json_decode($data)); |
| 259 |
echo '</pre>'; |
| 260 |
die; |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
protected function _load_theme($name) |
| 265 |
{ |
| 266 |
$path = VIMEOGRAPHY_THEME_PATH . $name . '/videos.mustache'; |
| 267 |
if (! $result = @file_get_contents($path)) |
| 268 |
throw new Vimeography_Exception('The gallery template for the "'.$name.'" theme cannot be found.'); |
| 269 |
return $result; |
| 270 |
} |
| 271 |
} |