PluginProbe
Vimeography: Vimeo Video Gallery WordPress Plugin / 2.2
Vimeography: Vimeo Video Gallery WordPress Plugin v2.2
2.4.9 2.4.8 trunk 0.5.1 0.5.2 0.5.3 0.5.4 0.5.5 0.5.6 0.5.7 0.6 0.6.1 0.6.2 0.6.3 0.6.4 0.6.5 0.6.6 0.6.7 0.6.8 0.6.8.1 0.6.9 0.6.9.1 0.6.9.2 0.7 0.8 All 103 releases
vimeography / lib / shortcode.php

shortcode.php in Vimeography: Vimeo Video Gallery WordPress Plugin 2.2, at lib/shortcode.php

383 lines 10.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 '
203 SELECT *
204 FROM ' .
205 $wpdb->vimeography_gallery_meta .
206 ' AS meta
207 JOIN ' .
208 $wpdb->vimeography_gallery .
209 ' AS gallery
210 ON meta.gallery_id = gallery.id
211 WHERE meta.gallery_id = ' .
212 $id .
213 '
214 LIMIT 1;
215 '
216 );
217
218 if (empty($db_gallery_settings)) {
219 throw new Vimeography_Exception(
220 sprintf(
221 __(
222 'a Vimeography gallery with an ID of "%1$s" was not found.',
223 'vimeography'
224 ),
225 intval($id)
226 )
227 );
228 }
229
230 return $db_gallery_settings[0];
231 }
232
233 /**
234 * Verifies that the provided width setting is a valid CSS parameter
235 *
236 * @param string $width
237 * @return string A percentage, pixel-based, or empty width value.
238 */
239 private static function _validate_gallery_width($width)
240 {
241 if (!empty($width)) {
242 preg_match('/(\d*)(px|%?)/', $width, $matches);
243 // If a number value is set...
244 if (!empty($matches[1])) {
245 // If a '%' or 'px' is set...
246 if (!empty($matches[2])) {
247 // Accept the valid matching string
248 $width = $matches[0];
249 } else {
250 // Append a 'px' value to the matching number
251 $width = $matches[1] . 'px';
252 }
253 } else {
254 // Not a valid width
255 $width = '';
256 }
257 }
258 return $width;
259 }
260
261 /**
262 * Create a gallery_id token for any inline gallery that doesn't have an id.
263 *
264 * @return string A unique token representing the current gallery
265 */
266 private static function _get_inline_gallery_id($shortcode)
267 {
268 return substr(md5(serialize($shortcode)), 0, -24);
269 }
270
271 /**
272 * Load any custom CSS files that have been generated by the
273 * Vimegraphy theme customization tools.
274 *
275 * @return void
276 */
277 private function _vimeography_enqueue_custom_stylesheets()
278 {
279 $name = 'vimeography-gallery-' . $this->_gallery_id . '-custom';
280 $filename = $name . '.css';
281 $filepath = VIMEOGRAPHY_CUSTOMIZATIONS_PATH . $filename;
282 $file_url = VIMEOGRAPHY_CUSTOMIZATIONS_URL . $filename;
283
284 if (file_exists($filepath)) {
285 $stylesheet_key = "vimeography_gallery_" . $this->_gallery_id;
286 $css = file_get_contents($filepath);
287
288 // migrate local css file to db entry
289 if (function_exists('wp_update_custom_css_post')) {
290 $r = wp_update_custom_css_post($css, array(
291 'stylesheet' => $stylesheet_key // should be unique per gallery, copy on galleryduplicate
292 ));
293
294 unlink($filepath);
295 return;
296 }
297
298 $vimeography = Vimeography::get_instance();
299 $addons = $vimeography->addons->set_active_theme(
300 $this->_gallery_settings['theme']
301 );
302 $theme = $addons->active_theme;
303
304 $dependencies = array();
305
306 // Make sure the current theme's stylesheet handle is set as a dependency
307 if (
308 version_compare($theme['version'], '2.0', '<') ||
309 isset($theme['app_css'])
310 ) {
311 $dependencies = array(
312 'vimeography-' . strtolower($this->_gallery_settings['theme'])
313 );
314 }
315
316 wp_register_style(
317 $name,
318 $file_url,
319 $dependencies,
320 strval(filemtime($filepath))
321 );
322 wp_enqueue_style($name);
323 }
324 }
325
326 /**
327 * Loads the Vimeography engine and renderer and returns the rendered HTML for output.
328 *
329 * @return string
330 */
331 public function output()
332 {
333 $vimeography = Vimeography::get_instance();
334 $addons = $vimeography->addons->set_active_theme(
335 $this->_gallery_settings['theme']
336 );
337 $theme = $addons->active_theme;
338
339 try {
340 $engine = new \Vimeography\Engine();
341 return $engine
342 ->set_gallery_id($this->_gallery_id)
343 ->set_gallery_settings($this->_gallery_settings)
344 ->set_theme($theme)
345 ->load()
346 ->fetch()
347 ->post_process()
348 ->render();
349 } catch (Vimeography_Exception $e) {
350 $link = get_permalink();
351 $time = current_time('timestamp', true);
352 do_action('vimeography.exception.report', $e, $this, $link, $time);
353
354 ob_start();
355 ?>
356 <div class="vimeography-error">
357 <h2><?php _e(
358 'This video gallery couldn\'t be loaded.',
359 'vimeography'
360 ); ?></h2>
361 <p><?php echo $e->getMessage(); ?></p>
362 </div>
363
364 <style>
365 .vimeography-error {
366 background-color: rgba(255, 255, 255, 0.25);
367 max-width: 500px;
368 margin: 0 auto 2rem;
369 text-align: center;
370 border-radius: 4px;
371 padding: 1rem;
372 box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05);
373 }
374
375 .vimeography-error h2 {
376 font-size: 1.3rem;
377 }
378 </style>
379 <?php return ob_get_clean();
380 }
381 }
382 }
383