| 1 |
<?php |
| 2 |
|
| 3 |
// Exit if accessed directly |
| 4 |
if (!defined('ABSPATH')) { |
| 5 |
exit(); |
| 6 |
} |
| 7 |
|
| 8 |
class Vimeography_Shortcode extends Vimeography |
| 9 |
{ |
| 10 |
/** |
| 11 |
* The shortcode tag attributes applied by the user on a page or post. |
| 12 |
* |
| 13 |
* @var array |
| 14 |
*/ |
| 15 |
private $_atts; |
| 16 |
|
| 17 |
/** |
| 18 |
* The content located inside of the shortcode tag applied by the |
| 19 |
* user on a page or post. |
| 20 |
* |
| 21 |
* @var string |
| 22 |
*/ |
| 23 |
private $_content = null; |
| 24 |
|
| 25 |
/** |
| 26 |
* The id number associated with a gallery entry in the database. |
| 27 |
* |
| 28 |
* @var int or tokenized string |
| 29 |
*/ |
| 30 |
protected $_gallery_id; |
| 31 |
|
| 32 |
/** |
| 33 |
* The settings being used to render the current gallery. |
| 34 |
* This includes the theme name, resource URI, featured video, cache settings, |
| 35 |
* and gallery width parameter. |
| 36 |
* |
| 37 |
* @var array |
| 38 |
*/ |
| 39 |
private $_gallery_settings; |
| 40 |
|
| 41 |
/** |
| 42 |
* Hook into the Vimeography Shortcode and |
| 43 |
* add shortcode support for widgets. |
| 44 |
*/ |
| 45 |
public function __construct() |
| 46 |
{ |
| 47 |
add_filter('widget_text', 'do_shortcode'); |
| 48 |
add_shortcode('vimeography', array($this, 'vimeography_shortcode')); |
| 49 |
|
| 50 |
if (function_exists('register_block_type')) { |
| 51 |
register_block_type('vimeography/gallery', array( |
| 52 |
'editor_script' => 'vimeography-gallery-block-editor', |
| 53 |
'editor_style' => 'vimeography-gallery-block-editor', |
| 54 |
'style' => 'vimeography-gallery-block', |
| 55 |
'render_callback' => array($this, 'vimeography_shortcode') |
| 56 |
)); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Loads the gallery settings, generates any custom CSS, and creates the gallery token. |
| 62 |
* |
| 63 |
* @param array $atts The shortcode tag attributes applied by the user on a page or post. |
| 64 |
* @param string $content The content located inside of the shortcode tag. |
| 65 |
*/ |
| 66 |
public function vimeography_shortcode($atts, $content = null) |
| 67 |
{ |
| 68 |
$this->_atts = $atts; |
| 69 |
$this->_content = $content; |
| 70 |
|
| 71 |
try { |
| 72 |
$this->_gallery_settings = self::_apply_shortcode_gallery_settings( |
| 73 |
$this->_atts |
| 74 |
); |
| 75 |
$this->_gallery_id = isset($atts['id']) |
| 76 |
? intval($atts['id']) |
| 77 |
: self::_get_inline_gallery_id($this->_gallery_settings); |
| 78 |
|
| 79 |
$this->_vimeography_enqueue_custom_stylesheets(); |
| 80 |
return $this->output(); |
| 81 |
} catch (Vimeography_Exception $e) { |
| 82 |
return __("Vimeography Error: ", 'vimeography') . $e->getMessage(); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Determines which gallery settings to use based on the provided |
| 88 |
* shortcode settings, the existing gallery db settings, and the |
| 89 |
* fallback gallery settings. |
| 90 |
* |
| 91 |
* @return array The gallery settings to be used to render the current gallery. |
| 92 |
*/ |
| 93 |
private static function _apply_shortcode_gallery_settings($atts) |
| 94 |
{ |
| 95 |
if (!empty($atts['id'])) { |
| 96 |
$db_gallery_settings = self::_get_db_gallery_settings( |
| 97 |
intval($atts['id']) |
| 98 |
); |
| 99 |
} |
| 100 |
|
| 101 |
// Get admin panel options |
| 102 |
$default_settings = get_option('vimeography_default_settings'); |
| 103 |
|
| 104 |
$fallback_gallery_settings = array(); |
| 105 |
$fallback_gallery_settings['theme'] = isset( |
| 106 |
$db_gallery_settings->theme_name |
| 107 |
) |
| 108 |
? $db_gallery_settings->theme_name |
| 109 |
: $default_settings['theme_name']; |
| 110 |
$fallback_gallery_settings['featured'] = isset( |
| 111 |
$db_gallery_settings->featured_video |
| 112 |
) |
| 113 |
? $db_gallery_settings->featured_video |
| 114 |
: $default_settings['featured_video']; |
| 115 |
$fallback_gallery_settings['endpoint'] = isset( |
| 116 |
$db_gallery_settings->resource_uri |
| 117 |
) |
| 118 |
? $db_gallery_settings->resource_uri |
| 119 |
: $default_settings['resource_uri']; |
| 120 |
$fallback_gallery_settings['limit'] = isset( |
| 121 |
$db_gallery_settings->video_limit |
| 122 |
) |
| 123 |
? $db_gallery_settings->video_limit |
| 124 |
: $default_settings['video_limit']; |
| 125 |
$fallback_gallery_settings['cache'] = isset( |
| 126 |
$db_gallery_settings->cache_timeout |
| 127 |
) |
| 128 |
? $db_gallery_settings->cache_timeout |
| 129 |
: $default_settings['cache_timeout']; |
| 130 |
$fallback_gallery_settings['width'] = isset( |
| 131 |
$db_gallery_settings->gallery_width |
| 132 |
) |
| 133 |
? $db_gallery_settings->gallery_width |
| 134 |
: ''; |
| 135 |
|
| 136 |
$fallback_gallery_settings = apply_filters( |
| 137 |
'vimeography.gallery.settings', |
| 138 |
$fallback_gallery_settings, |
| 139 |
$atts |
| 140 |
); |
| 141 |
|
| 142 |
// Get shortcode attributes |
| 143 |
$shortcode_gallery_settings = shortcode_atts( |
| 144 |
array( |
| 145 |
'theme' => $fallback_gallery_settings['theme'], |
| 146 |
'featured' => $fallback_gallery_settings['featured'], |
| 147 |
'source' => $fallback_gallery_settings['endpoint'], |
| 148 |
'limit' => $fallback_gallery_settings['limit'], |
| 149 |
'cache' => $fallback_gallery_settings['cache'], |
| 150 |
'width' => $fallback_gallery_settings['width'] |
| 151 |
), |
| 152 |
$atts, |
| 153 |
'vimeography' |
| 154 |
); |
| 155 |
|
| 156 |
// Remove this line once 3.6 is the minimum supported version. |
| 157 |
$shortcode_gallery_settings = apply_filters( |
| 158 |
'vimeography-pro/do-shortcode', |
| 159 |
$shortcode_gallery_settings, |
| 160 |
'', |
| 161 |
$atts |
| 162 |
); |
| 163 |
|
| 164 |
$shortcode_gallery_settings['width'] = self::_validate_gallery_width( |
| 165 |
$shortcode_gallery_settings['width'] |
| 166 |
); |
| 167 |
|
| 168 |
if ( |
| 169 |
$shortcode_gallery_settings['source'] != |
| 170 |
$fallback_gallery_settings['endpoint'] |
| 171 |
) { |
| 172 |
$shortcode_gallery_settings[ |
| 173 |
'source' |
| 174 |
] = Vimeography::validate_vimeo_source( |
| 175 |
$shortcode_gallery_settings['source'] |
| 176 |
); |
| 177 |
} |
| 178 |
|
| 179 |
// Hot swap the showcases source for albums until showcases are supported via the API |
| 180 |
$shortcode_gallery_settings['source'] = str_replace( |
| 181 |
"showcases", |
| 182 |
"albums", |
| 183 |
$shortcode_gallery_settings['source'] |
| 184 |
); |
| 185 |
|
| 186 |
$shortcode_gallery_settings['source'] = |
| 187 |
$shortcode_gallery_settings['source'] . '/videos'; |
| 188 |
|
| 189 |
return $shortcode_gallery_settings; |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Retrieves the gallery data for the provided gallery ID. |
| 194 |
* |
| 195 |
* @return object The settings associated with the gallery in the database. |
| 196 |
*/ |
| 197 |
private static function _get_db_gallery_settings($id) |
| 198 |
{ |
| 199 |
global $wpdb; |
| 200 |
|
| 201 |
$db_gallery_settings = $wpdb->get_results( |
| 202 |
$wpdb->prepare( |
| 203 |
' |
| 204 |
SELECT * |
| 205 |
FROM ' . $wpdb->vimeography_gallery_meta . ' AS meta |
| 206 |
JOIN ' . $wpdb->vimeography_gallery . ' AS gallery |
| 207 |
ON meta.gallery_id = gallery.id |
| 208 |
WHERE meta.gallery_id = %d |
| 209 |
LIMIT 1; |
| 210 |
', |
| 211 |
$id |
| 212 |
) |
| 213 |
); |
| 214 |
|
| 215 |
if (empty($db_gallery_settings)) { |
| 216 |
throw new Vimeography_Exception( |
| 217 |
sprintf( |
| 218 |
wp_kses_post(__( |
| 219 |
'a Vimeography gallery with an ID of "%1$s" was not found.', |
| 220 |
'vimeography' |
| 221 |
)), |
| 222 |
intval($id) |
| 223 |
) |
| 224 |
); |
| 225 |
} |
| 226 |
|
| 227 |
return $db_gallery_settings[0]; |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Verifies that the provided width setting is a valid CSS parameter |
| 232 |
* |
| 233 |
* @param string $width |
| 234 |
* @return string A percentage, pixel-based, or empty width value. |
| 235 |
*/ |
| 236 |
private static function _validate_gallery_width($width) |
| 237 |
{ |
| 238 |
if (!empty($width)) { |
| 239 |
preg_match('/(\d*)(px|%?)/', $width, $matches); |
| 240 |
// If a number value is set... |
| 241 |
if (!empty($matches[1])) { |
| 242 |
// If a '%' or 'px' is set... |
| 243 |
if (!empty($matches[2])) { |
| 244 |
// Accept the valid matching string |
| 245 |
$width = $matches[0]; |
| 246 |
} else { |
| 247 |
// Append a 'px' value to the matching number |
| 248 |
$width = $matches[1] . 'px'; |
| 249 |
} |
| 250 |
} else { |
| 251 |
// Not a valid width |
| 252 |
$width = ''; |
| 253 |
} |
| 254 |
} |
| 255 |
return $width; |
| 256 |
} |
| 257 |
|
| 258 |
/** |
| 259 |
* Create a gallery_id token for any inline gallery that doesn't have an id. |
| 260 |
* |
| 261 |
* @return string A unique token representing the current gallery |
| 262 |
*/ |
| 263 |
private static function _get_inline_gallery_id($shortcode) |
| 264 |
{ |
| 265 |
return substr(md5(serialize($shortcode)), 0, -24); |
| 266 |
} |
| 267 |
|
| 268 |
/** |
| 269 |
* Load any custom CSS files that have been generated by the |
| 270 |
* Vimegraphy theme customization tools. |
| 271 |
* |
| 272 |
* @return void |
| 273 |
*/ |
| 274 |
private function _vimeography_enqueue_custom_stylesheets() |
| 275 |
{ |
| 276 |
$name = 'vimeography-gallery-' . $this->_gallery_id . '-custom'; |
| 277 |
$filename = $name . '.css'; |
| 278 |
$filepath = VIMEOGRAPHY_CUSTOMIZATIONS_PATH . $filename; |
| 279 |
$file_url = VIMEOGRAPHY_CUSTOMIZATIONS_URL . $filename; |
| 280 |
|
| 281 |
if (file_exists($filepath)) { |
| 282 |
$stylesheet_key = "vimeography_gallery_" . $this->_gallery_id; |
| 283 |
$css = file_get_contents($filepath); |
| 284 |
|
| 285 |
// migrate local css file to db entry |
| 286 |
if (function_exists('wp_update_custom_css_post')) { |
| 287 |
$r = wp_update_custom_css_post($css, array( |
| 288 |
'stylesheet' => $stylesheet_key // should be unique per gallery, copy on galleryduplicate |
| 289 |
)); |
| 290 |
|
| 291 |
unlink($filepath); |
| 292 |
return; |
| 293 |
} |
| 294 |
|
| 295 |
$vimeography = Vimeography::get_instance(); |
| 296 |
$addons = $vimeography->addons->set_active_theme( |
| 297 |
$this->_gallery_settings['theme'] |
| 298 |
); |
| 299 |
$theme = $addons->active_theme; |
| 300 |
|
| 301 |
$dependencies = array(); |
| 302 |
|
| 303 |
// Make sure the current theme's stylesheet handle is set as a dependency |
| 304 |
if ( |
| 305 |
version_compare($theme['version'], '2.0', '<') || |
| 306 |
isset($theme['app_css']) |
| 307 |
) { |
| 308 |
$dependencies = array( |
| 309 |
'vimeography-' . strtolower($this->_gallery_settings['theme']) |
| 310 |
); |
| 311 |
} |
| 312 |
|
| 313 |
wp_register_style( |
| 314 |
$name, |
| 315 |
$file_url, |
| 316 |
$dependencies, |
| 317 |
strval(filemtime($filepath)) |
| 318 |
); |
| 319 |
wp_enqueue_style($name); |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
/** |
| 324 |
* Loads the Vimeography engine and renderer and returns the rendered HTML for output. |
| 325 |
* |
| 326 |
* @return string |
| 327 |
*/ |
| 328 |
public function output() |
| 329 |
{ |
| 330 |
$vimeography = Vimeography::get_instance(); |
| 331 |
$addons = $vimeography->addons->set_active_theme( |
| 332 |
$this->_gallery_settings['theme'] |
| 333 |
); |
| 334 |
$theme = $addons->active_theme; |
| 335 |
|
| 336 |
try { |
| 337 |
$engine = new \Vimeography\Engine(); |
| 338 |
return $engine |
| 339 |
->set_gallery_id($this->_gallery_id) |
| 340 |
->set_gallery_settings($this->_gallery_settings) |
| 341 |
->set_theme($theme) |
| 342 |
->load() |
| 343 |
->fetch() |
| 344 |
->post_process() |
| 345 |
->render(); |
| 346 |
} catch (Vimeography_Exception $e) { |
| 347 |
$link = get_permalink(); |
| 348 |
$time = current_time('timestamp', true); |
| 349 |
do_action('vimeography.exception.report', $e, $this, $link, $time); |
| 350 |
|
| 351 |
ob_start(); |
| 352 |
?> |
| 353 |
<div class="vimeography-error"> |
| 354 |
<h2><?php wp_kses_post(_e( |
| 355 |
'This video gallery couldn\'t be loaded.', |
| 356 |
'vimeography' |
| 357 |
)); ?></h2> |
| 358 |
<p><?php echo wp_kses_post($e->getMessage()); ?></p> |
| 359 |
</div> |
| 360 |
|
| 361 |
<style> |
| 362 |
.vimeography-error { |
| 363 |
background-color: rgba(255, 255, 255, 0.25); |
| 364 |
max-width: 500px; |
| 365 |
margin: 0 auto 2rem; |
| 366 |
text-align: center; |
| 367 |
border-radius: 4px; |
| 368 |
padding: 1rem; |
| 369 |
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05); |
| 370 |
} |
| 371 |
|
| 372 |
.vimeography-error h2 { |
| 373 |
font-size: 1.3rem; |
| 374 |
} |
| 375 |
</style> |
| 376 |
<?php return ob_get_clean(); |
| 377 |
} |
| 378 |
} |
| 379 |
} |
| 380 |
|