| 1 |
<?php |
| 2 |
|
| 3 |
// Exit if accessed directly |
| 4 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 5 |
|
| 6 |
class Vimeography_Renderer { |
| 7 |
/** |
| 8 |
* The settings to be used when rendering the gallery. |
| 9 |
* |
| 10 |
* @var array |
| 11 |
*/ |
| 12 |
protected $_settings; |
| 13 |
|
| 14 |
/** |
| 15 |
* The ID of the Vimeography gallery being rendered. |
| 16 |
* |
| 17 |
* @var int |
| 18 |
*/ |
| 19 |
protected $_gallery_id; |
| 20 |
|
| 21 |
/** |
| 22 |
* The meta headers for the active Vimeography theme |
| 23 |
* |
| 24 |
* @var array |
| 25 |
*/ |
| 26 |
protected $_active_theme; |
| 27 |
|
| 28 |
/** |
| 29 |
* The theme class instance for the current active Vimeography theme |
| 30 |
* |
| 31 |
* @var object |
| 32 |
*/ |
| 33 |
protected $_view; |
| 34 |
|
| 35 |
/** |
| 36 |
* The Mustache template being used to render the Vimeo data. |
| 37 |
* |
| 38 |
* @var [type] A mustache template or partial |
| 39 |
*/ |
| 40 |
protected $_template; |
| 41 |
|
| 42 |
/** |
| 43 |
* Backwards compatibility variable for Vimeography Pro < 0.6.3 |
| 44 |
* This is an instanceof $_template |
| 45 |
* |
| 46 |
* @var [type] |
| 47 |
*/ |
| 48 |
protected $_theme; |
| 49 |
|
| 50 |
/** |
| 51 |
* Creates the rendering engine, called from the Vimeography Shortcode. |
| 52 |
* |
| 53 |
* $settings should contain at least |
| 54 |
* - theme = theme name to use |
| 55 |
* optionals are: |
| 56 |
* - partial = if not full theme would be rendered |
| 57 |
* |
| 58 |
* @param array $settings The shortcode gallery settings |
| 59 |
* @param int $gallery_id The ID of the Vimeography gallery being loaded. |
| 60 |
* |
| 61 |
* @throws Vimeography_Exception |
| 62 |
*/ |
| 63 |
public function __construct($settings, $gallery_id) { |
| 64 |
$this->_settings = $settings; |
| 65 |
$this->_gallery_id = $gallery_id; |
| 66 |
|
| 67 |
if (! $this->_view) { |
| 68 |
$this->_view = new stdClass(); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Retrieve and set the meta headers for the current active theme. |
| 74 |
* |
| 75 |
* @param array Gallery settings |
| 76 |
*/ |
| 77 |
protected function _set_active_theme($settings) { |
| 78 |
if ( ! isset( $settings['theme'] ) ) { |
| 79 |
throw new Vimeography_Exception( |
| 80 |
__('You must specify a theme in either the admin panel or the shortcode.', 'vimeography') |
| 81 |
); |
| 82 |
} |
| 83 |
|
| 84 |
$vimeography = Vimeography::get_instance(); |
| 85 |
$vimeography->addons->set_active_theme( $settings['theme'] ); |
| 86 |
|
| 87 |
$this->_active_theme = $vimeography->addons->active_theme; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Loads the theme class for the current active theme. |
| 92 |
* |
| 93 |
* @param array $theme Meta headers for the active theme |
| 94 |
* @return void |
| 95 |
*/ |
| 96 |
protected function _load_theme_class($theme) { |
| 97 |
|
| 98 |
// If the theme class exists, require it. |
| 99 |
if ( file_exists( $theme['file_path'] ) ) { |
| 100 |
require_once $theme['file_path']; |
| 101 |
} else { |
| 102 |
// If it doesn't exist, throw an appropriate error message. |
| 103 |
if ( empty( $theme['name'] ) ) { |
| 104 |
throw new Vimeography_Exception( |
| 105 |
__('This Vimeography gallery does not have a theme assigned to it.', 'vimeography') |
| 106 |
); |
| 107 |
} else { |
| 108 |
throw new Vimeography_Exception( |
| 109 |
sprintf( |
| 110 |
__('The "%s" theme does not exist or is improperly structured.', 'vimeography'), |
| 111 |
$theme['name'] |
| 112 |
) |
| 113 |
); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
// Build the conventional theme class name |
| 118 |
$class = 'Vimeography_Themes_' . ucfirst( $theme['name'] ); |
| 119 |
|
| 120 |
if ( ! class_exists( $class ) ) { |
| 121 |
throw new Vimeography_Exception( |
| 122 |
sprintf( |
| 123 |
__('The "%s" theme class does not exist or is improperly named.', 'vimeography'), |
| 124 |
$theme['name'] |
| 125 |
) |
| 126 |
); |
| 127 |
} |
| 128 |
|
| 129 |
$this->_view = new $class; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Loads the Mustache template or partial for the active theme. |
| 134 |
* |
| 135 |
* @param array $theme Meta headers for the active theme |
| 136 |
* @param array $settings Gallery shortcode settings |
| 137 |
* @return void |
| 138 |
*/ |
| 139 |
protected function _load_theme_template($theme, $settings) { |
| 140 |
$loaders = array_merge( |
| 141 |
self::_load_theme_template_override( $theme['plugin_override_path'] ), |
| 142 |
array( new Mustache_Loader_FilesystemLoader( $theme['plugin_path'] ) ) |
| 143 |
); |
| 144 |
|
| 145 |
$partial_loaders = array_merge( |
| 146 |
self::_load_theme_template_override( $theme['partials_override_path'] ), |
| 147 |
array( new Mustache_Loader_FilesystemLoader( $theme['partials_path'] ) ) |
| 148 |
); |
| 149 |
|
| 150 |
$mustache = new Mustache_Engine( array( |
| 151 |
'loader' => new Mustache_Loader_CascadingLoader( $loaders ), |
| 152 |
'partials_loader' => new Mustache_Loader_CascadingLoader( $partial_loaders ), |
| 153 |
) ); |
| 154 |
|
| 155 |
$this->_template = ( isset( $settings['partial'] ) ) ? |
| 156 |
$mustache->loadPartial( strtolower( $settings['partial'] ) ) : |
| 157 |
$mustache->loadTemplate( strtolower( $theme['name'] ) ); |
| 158 |
|
| 159 |
// Backwards-Compatibility for Vimeography Pro < 0.7 |
| 160 |
$this->_theme = $this->_template; |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Attempts to load a theme template override, if it exists. |
| 165 |
* |
| 166 |
* @param string $path path to the theme template override, eg. 'wp-content/themes/bones/vimeography/bugsauce/' |
| 167 |
* @return array |
| 168 |
*/ |
| 169 |
protected static function _load_theme_template_override($override_path) { |
| 170 |
try { |
| 171 |
return array( new Mustache_Loader_FilesystemLoader( $override_path ) ); |
| 172 |
} catch (Mustache_Exception_RuntimeException $e) { |
| 173 |
return array(); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Enables the active theme to hook into the _enqueue_scripts actions. |
| 179 |
* |
| 180 |
* This is what allows each theme to be able to load all of its own |
| 181 |
* asset dependencies, including custom javascripts and stylesheets. |
| 182 |
* |
| 183 |
* @param object $view The theme class instance |
| 184 |
* @return void |
| 185 |
*/ |
| 186 |
protected function _load_theme_dependencies($view) { |
| 187 |
add_action('wp_enqueue_scripts', array( get_class( $view ), 'load_scripts' ) ); |
| 188 |
add_action('admin_enqueue_scripts', array( get_class( $view ), 'load_scripts' ) ); |
| 189 |
|
| 190 |
if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) { |
| 191 |
// Action has already been run, we're late to the party. |
| 192 |
is_admin() ? do_action('admin_enqueue_scripts') : do_action('wp_enqueue_scripts'); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* [load_theme description] |
| 198 |
* @return [type] [description] |
| 199 |
*/ |
| 200 |
public function load_theme() { |
| 201 |
self::_set_active_theme( $this->_settings ); |
| 202 |
self::_load_theme_class( $this->_active_theme ); |
| 203 |
self::_load_theme_template( $this->_active_theme, $this->_settings ); |
| 204 |
self::_load_theme_dependencies( $this->_view ); |
| 205 |
$this->_view->gallery_id = $this->_gallery_id; |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Renders the data inside of the theme's Mustache template. |
| 210 |
* We did it! |
| 211 |
* |
| 212 |
* @param array $data Video set data from Vimeo |
| 213 |
* @return string | html |
| 214 |
*/ |
| 215 |
public function render($result) { |
| 216 |
// Set remaining view variables and render away |
| 217 |
if ( isset( $this->_settings['width'] ) ) { |
| 218 |
$this->_view->gallery_width = $this->_settings['width']; |
| 219 |
} |
| 220 |
|
| 221 |
$this->_view->data = $result->video_set; |
| 222 |
|
| 223 |
if (! empty( $this->_view->data[0] ) ) { |
| 224 |
$this->_view->featured = clone $this->_view->data[0]; |
| 225 |
} |
| 226 |
|
| 227 |
// Set theme javascript variables |
| 228 |
$this->set_theme_vars(); |
| 229 |
|
| 230 |
return $this->_template->render($this->_view); |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
/** |
| 235 |
* [set_theme_vars description] |
| 236 |
*/ |
| 237 |
public function set_theme_vars() { |
| 238 |
|
| 239 |
$theme_name = strtolower( $this->_active_theme['name'] ); |
| 240 |
|
| 241 |
$data = array( |
| 242 |
'gallery_id' => $this->_view->gallery_id |
| 243 |
); |
| 244 |
|
| 245 |
$reshuffled_data = array( |
| 246 |
'l10n_print_after' => sprintf('vimeography.galleries.%1$s["%2$s"] = %3$s', |
| 247 |
$theme_name, |
| 248 |
$this->_view->gallery_id, |
| 249 |
json_encode( $data ) |
| 250 |
) |
| 251 |
); |
| 252 |
|
| 253 |
wp_localize_script("vimeography-{$theme_name}", |
| 254 |
"vimeography = window.vimeography || {}; |
| 255 |
window.vimeography.galleries = window.vimeography.galleries || {}; |
| 256 |
window.vimeography.galleries.{$theme_name} = window.vimeography.galleries.{$theme_name} || {}; |
| 257 |
vimeography.unused", |
| 258 |
$reshuffled_data); |
| 259 |
} |
| 260 |
|
| 261 |
/** |
| 262 |
* Allows for examination of the data currently being sent to the gallery. |
| 263 |
* |
| 264 |
* @param string $data A JSON string of Vimeo data. |
| 265 |
* @return string Prints the string. |
| 266 |
*/ |
| 267 |
protected function debug($data) |
| 268 |
{ |
| 269 |
echo '<h1>Vimeography Debug</h1>'; |
| 270 |
echo '<pre>'; |
| 271 |
print_r($data); |
| 272 |
echo '</pre>'; |
| 273 |
die; |
| 274 |
} |
| 275 |
|
| 276 |
} |
| 277 |
|