| 1 |
<?php |
| 2 |
|
| 3 |
namespace Vimeography; |
| 4 |
|
| 5 |
/** |
| 6 |
* \Vimeography\Engine class |
| 7 |
* |
| 8 |
* @since 2.0 |
| 9 |
*/ |
| 10 |
class Engine { |
| 11 |
|
| 12 |
/** |
| 13 |
* The gallery id for the current gallery, if any. |
| 14 |
* Used to pull the associated cache file for a gallery |
| 15 |
* request, to look up gallery settings in the database, |
| 16 |
* and to namespace the gallery DOM element with an informational classname. |
| 17 |
* |
| 18 |
* @var integer |
| 19 |
*/ |
| 20 |
public $gallery_id; |
| 21 |
|
| 22 |
|
| 23 |
/** |
| 24 |
* Used to determine various aspects about the display preferences |
| 25 |
* for the current gallery request, including gallery width, |
| 26 |
* featured video, resource, cache expiration etc. |
| 27 |
* |
| 28 |
* @var array |
| 29 |
*/ |
| 30 |
public $gallery_settings; |
| 31 |
|
| 32 |
|
| 33 |
/** |
| 34 |
* Header metadata for the active theme |
| 35 |
* |
| 36 |
* Note: always be sure to set at least the $theme['version'] |
| 37 |
* in the requests so that we know which version of the renderer to load. |
| 38 |
* |
| 39 |
* @var array |
| 40 |
*/ |
| 41 |
public $theme; |
| 42 |
|
| 43 |
|
| 44 |
/** |
| 45 |
* The core Vimeography request processor |
| 46 |
* This varies based on whether or not Vimeography Pro is installed. |
| 47 |
* |
| 48 |
* @var class object |
| 49 |
*/ |
| 50 |
public $core; |
| 51 |
|
| 52 |
|
| 53 |
/** |
| 54 |
* The Vimeography renderer |
| 55 |
* |
| 56 |
* Depending on which version of Vimeography we have installed, this will |
| 57 |
* either build the initial Vuex state tree and provide a mounting point |
| 58 |
* for the gallery to be injected (v2.x), or, will use Mustache.php to get |
| 59 |
* the contents of a mustache template and render it serverside (1.x) |
| 60 |
* |
| 61 |
* @var class |
| 62 |
*/ |
| 63 |
public $renderer; |
| 64 |
|
| 65 |
|
| 66 |
/** |
| 67 |
* Sets the gallery ID associated with the current request. |
| 68 |
* |
| 69 |
* @param integer $id gallery ID |
| 70 |
*/ |
| 71 |
public function set_gallery_id( $id ) { |
| 72 |
$this->gallery_id = $id; |
| 73 |
return $this; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Sets the gallery settings. |
| 78 |
* |
| 79 |
* @param [type] $settings [description] |
| 80 |
*/ |
| 81 |
public function set_gallery_settings( $settings ) { |
| 82 |
$this->gallery_settings = $settings; |
| 83 |
return $this; |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
/** |
| 88 |
* Sets the header metadata for the active theme |
| 89 |
* |
| 90 |
* @param array $theme |
| 91 |
*/ |
| 92 |
public function set_theme( $theme ) { |
| 93 |
$this->theme = $theme; |
| 94 |
return $this; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
/** |
| 99 |
* Load up the correct core and renderer based on the |
| 100 |
* user's installed plugins. |
| 101 |
* |
| 102 |
* This needs to be called after the set_gallery_settings() |
| 103 |
* has been called. |
| 104 |
*/ |
| 105 |
public function load() { |
| 106 |
/** |
| 107 |
* We will decide which of these to use below based on |
| 108 |
* the versions of Vimeography Pro (if any) and the gallery theme |
| 109 |
* we have installed. |
| 110 |
*/ |
| 111 |
require_once VIMEOGRAPHY_PATH . 'lib/core.php'; |
| 112 |
require_once VIMEOGRAPHY_PATH . 'lib/core/basic.php'; |
| 113 |
require_once VIMEOGRAPHY_PATH . 'lib/renderer.php'; |
| 114 |
|
| 115 |
require_once VIMEOGRAPHY_PATH . 'lib/deprecated/core.php'; |
| 116 |
require_once VIMEOGRAPHY_PATH . 'lib/deprecated/core/basic.php'; |
| 117 |
require_once VIMEOGRAPHY_PATH . 'lib/deprecated/renderer.php'; |
| 118 |
|
| 119 |
if ( class_exists( '\Vimeography_Pro' ) ) { |
| 120 |
|
| 121 |
do_action('vimeography/load_pro'); |
| 122 |
|
| 123 |
if ( |
| 124 |
version_compare( VIMEOGRAPHY_PRO_VERSION, '2.0', '>=' ) && |
| 125 |
version_compare( $this->theme['version'], '2.0', '>=' ) |
| 126 |
) { |
| 127 |
$this->core = new Pro\Core( $this ); |
| 128 |
$this->renderer = new Pro\Renderer( $this ); // load 2.0 pro renderer |
| 129 |
} else { |
| 130 |
|
| 131 |
if ( version_compare( $this->theme['version'], '2.0', '>=' ) ) { |
| 132 |
$keys = get_site_option('vimeography_activation_keys'); |
| 133 |
|
| 134 |
if ( ! empty( $keys ) ) { |
| 135 |
foreach ( $keys as $key ) { |
| 136 |
if ( $key->plugin_name === 'vimeography-pro' ) { |
| 137 |
$link = sprintf( "https://vimeography.com/checkout/?edd_license_key=%s&download_id=36", $key->activation_key ); |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
$message = sprintf( __('The <strong>%s</strong> gallery theme is only compatible with <strong>Vimeography Pro 2.</strong><br /> You\'re currently using <strong>Vimeography Pro version %s.</strong> Please try updating to the latest version of Vimeography Pro to use this theme. ', 'vimeography'), $this->theme['name'], VIMEOGRAPHY_PRO_VERSION ); |
| 143 |
|
| 144 |
if ( isset( $link ) ) { |
| 145 |
$message .= '<br /><br />'; |
| 146 |
$message .= sprintf( __('If your Vimeography Pro license key is expired, you can gain access to <strong>another year of updates and support</strong> by <a href="%s">renewing your license key on this page.</a>', 'vimeography'), $link ); |
| 147 |
} |
| 148 |
|
| 149 |
throw new \Vimeography_Exception( wp_kses_post($message) ); |
| 150 |
} |
| 151 |
|
| 152 |
$this->core = new \Vimeography_Core_Pro( $this->gallery_settings ); |
| 153 |
$this->renderer = new \Vimeography_Pro_Renderer( $this->gallery_settings, $this->gallery_id ); // load deprecated 1.x pro renderer |
| 154 |
} |
| 155 |
|
| 156 |
} else { |
| 157 |
if ( version_compare( $this->theme['version'], '2.0', '>=' ) ) { |
| 158 |
$this->core = new Basic\Core( $this ); |
| 159 |
$this->renderer = new Renderer( $this ); // load 2.0 renderer |
| 160 |
} else { |
| 161 |
$this->core = new \Vimeography_Core_Basic( $this->gallery_settings ); |
| 162 |
$this->renderer = new \Vimeography_Renderer( $this->gallery_settings, $this->gallery_id ); // load deprecated 1.x renderer |
| 163 |
} |
| 164 |
|
| 165 |
} |
| 166 |
|
| 167 |
return $this; |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
/** |
| 172 |
* Instruct the core class to bypass checking and setting the video cache. |
| 173 |
* Note: must call load() before using this method! |
| 174 |
* |
| 175 |
* @return $this |
| 176 |
*/ |
| 177 |
public function skip_cache() { |
| 178 |
$this->core->skip_cache = true; |
| 179 |
return $this; |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
/** |
| 184 |
* Retrieve the videos from either the remote API or the cached |
| 185 |
* video file based on our cache expiration and request settings. |
| 186 |
* |
| 187 |
* Note: cache can be entirely bypassed by adding ?vimeography_nocache=1 to |
| 188 |
* the $_GET parameters of the request. |
| 189 |
* |
| 190 |
* @return $this |
| 191 |
*/ |
| 192 |
public function fetch() { |
| 193 |
// choose a method based on version determined during load |
| 194 |
if ( isset( $this->core->version ) && version_compare( $this->core->version, '2.0', '>=' ) ) { |
| 195 |
$result = $this->core->get_videos(); |
| 196 |
} else { |
| 197 |
$result = $this->core->get_videos( $this->gallery_settings['cache'], $this->gallery_id ); |
| 198 |
} |
| 199 |
|
| 200 |
if ( empty( $result->video_set ) ) { |
| 201 |
throw new \Vimeography_Exception( wp_kses_post(__('the Vimeo source for this gallery does not have any videos.', 'vimeography')) ); |
| 202 |
} |
| 203 |
|
| 204 |
$this->data = $result; |
| 205 |
return $this; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
/** |
| 210 |
* Formats the remote API response data |
| 211 |
* to better fit our templating needs. |
| 212 |
* |
| 213 |
* @return $this |
| 214 |
*/ |
| 215 |
public function post_process() { |
| 216 |
|
| 217 |
$videos = $this->data->video_set; |
| 218 |
|
| 219 |
require_once VIMEOGRAPHY_PATH . 'lib/helpers.php'; |
| 220 |
$helpers = new \Vimeography_Helpers; |
| 221 |
|
| 222 |
/** |
| 223 |
* If the user prefers to limit the videos displayed to a subset |
| 224 |
* of the total videos returned during the request, truncate |
| 225 |
* them here. |
| 226 |
*/ |
| 227 |
if ( isset( $this->gallery_settings['limit'] ) ) { |
| 228 |
$videos = $helpers->limit_video_set( $videos, $this->gallery_settings['limit'] ); |
| 229 |
} |
| 230 |
|
| 231 |
$videos = apply_filters('vimeography.pro.post_process', $videos); |
| 232 |
|
| 233 |
/** |
| 234 |
* Vimeography themes used to include this functionality |
| 235 |
* in their constructor file. Since 2.0, we'll just include |
| 236 |
* it here and remove it from all of the theme constructors. |
| 237 |
* |
| 238 |
* @todo |
| 239 |
*/ |
| 240 |
$videos = $helpers->apply_common_formatting( $videos ); |
| 241 |
|
| 242 |
$this->data->video_set = $videos; |
| 243 |
return $this; |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
/** |
| 248 |
* [to_json description] |
| 249 |
* @return [type] [description] |
| 250 |
*/ |
| 251 |
public function to_json() { |
| 252 |
return json_encode( $this->data ); |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
/** |
| 257 |
* Output the expected data based on the version combination |
| 258 |
* of Vimeography Pro and the gallery theme. |
| 259 |
* |
| 260 |
* We'll typically only call render() on the initial page load via shortcode. |
| 261 |
* Subsequent search and pagination requests will skip this and return |
| 262 |
* the JSON-encoded data directly via a separate method. |
| 263 |
* |
| 264 |
* - 2.0: Builds the Vuex store state and and mounts the Vue app |
| 265 |
* - 1.x: Fetch the Mustache template and populate it with video data |
| 266 |
* |
| 267 |
* @return string|html |
| 268 |
*/ |
| 269 |
public function render() { |
| 270 |
// choose a method based on version determined during load |
| 271 |
if ( isset( $this->renderer->version ) && version_compare( $this->renderer->version, '2.0', '>=' ) ) { |
| 272 |
return $this->renderer->prepare( $this->data )->render(); |
| 273 |
} else { |
| 274 |
$this->renderer->load_theme(); |
| 275 |
$renderer = apply_filters('vimeography/deprecated/reload-pro-renderer', $this->renderer, $this->gallery_settings, $this->gallery_id ); |
| 276 |
return $renderer->render( $this->data ); |
| 277 |
} |
| 278 |
} |
| 279 |
} |
| 280 |
|