PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 5.0.3
Jetpack – WP Security, Backup, Speed, & Growth v5.0.3
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / modules / shortcodes / presentations.php
presentations.php
450 lines 13.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: Presentations
4 Plugin URI: http://automattic.com/wordpress-plugins/
5 Description: Presentations plugin based on the work done by <a href="http://darylkoop.com/">Daryl Koopersmith</a>. Powered by jmpress.js
6 Version: 0.2
7 Author: Automattic
8 Author URI: http://automattic.com/wordpress-plugins/
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25
26 /**
27 * Known issues:
28 *
29 * - IE 7/8 are not supported by jmpress and presentations will not work
30 * - IE 9 will not animate transitions at all, though it's possible to at least
31 * switch between slides.
32 * - Infinite Scroll themes will not load presentations properly unless the post
33 * happens to be on the first loaded page. The permalink page will function
34 * properly, however.
35 * - Exiting fullscreen mode will not properly reset the scroll locations in Safari
36 */
37
38
39 /*
40 HOW TO: How the plugin settings are organized and which features are supported.
41
42 The entire presentation should be wrapped with a [presentation] shortcode, and every
43 individual slide should be wrapped with a [slide] shortcode. Any settings supported
44 by [slide] can be set into [presentation], which will apply that setting for the entire
45 presentation unless overridden by individual slides.
46
47 - [presentation] only settings:
48 - duration: transition durations, default is one second.
49 - height: content height, default is 400px
50 - width: content width, default is 550px
51 - autoplay: delay between transitions in seconds, default 3s
52 when set the presentation will automatically transition between slides
53 as long as the presentation remains in focus
54
55 - [slide] settings:
56 - transition: specifies where the next slide will be placed relative
57 to the last one before it. Supported values are "up", "down"
58 "left", "right", or "none". Default value is "down".
59
60 - scale: scales the content relative to other slides, default value is one
61
62 - rotate: rotates the content by the specified degrees, default is zero
63
64 - fade: slides will fade in and out during transition. Values of "on" or
65 "true" will enable fading, while values of "no" or "false" will
66 disable it. Default value is "on"
67
68 - bgcolor: specifies a background color for the slides. Any CSS valid value
69 is permitted. Default color is transparent.
70
71 - bgimg: specifies an image url which will fill the background. Image is
72 set to fill the background 100% width and height
73
74 - fadebullets: any html <li> tags will start out with an opacity of 0 and any
75 subsequent slide transitions will show the bullets one by one
76 */
77
78 if ( ! class_exists( 'Presentations' ) ) :
79 class Presentations {
80
81 private $presentation_settings;
82 private $presentation_initialized;
83 private $scripts_and_style_included;
84
85 /**
86 * Constructor
87 */
88 function __construct() {
89 $this->presentation_initialized = false;
90 $this->scripts_and_style_included = false;
91
92 // Registers shortcodes
93 add_action( 'wp_head', array( &$this, 'add_scripts' ), 1 );
94
95 add_shortcode( 'presentation', array( &$this, 'presentation_shortcode' ) );
96 add_shortcode( 'slide', array( &$this, 'slide_shortcode' ) );
97 }
98
99 function add_scripts() {
100 $this->scripts_and_style_included = false;
101
102 if ( empty( $GLOBALS['posts'] ) || ! is_array( $GLOBALS['posts'] ) ) {
103 return;
104 }
105
106 foreach ( $GLOBALS['posts'] as $p ) {
107 if ( has_shortcode( $p->post_content, 'presentation' ) ) {
108 $this->scripts_and_style_included = true;
109 break;
110 }
111 }
112
113 if ( ! $this->scripts_and_style_included ) {
114 return;
115 }
116
117 $plugin = plugin_dir_url( __FILE__ );
118 // Add CSS
119 wp_enqueue_style( 'presentations', $plugin . 'css/style.css' );
120 // Add JavaScript
121 wp_enqueue_script( 'jquery' );
122 wp_enqueue_script( 'jmpress', $plugin . 'js/jmpress.min.js', array( 'jquery' ), '0.4.5', true );
123 wp_enqueue_script( 'presentations', $plugin . 'js/main.js', array( 'jquery', 'jmpress' ), false, true );
124 }
125
126 function presentation_shortcode( $atts, $content = '' ) {
127 // Mark that we've found a valid [presentation] shortcode
128 $this->presentation_initialized = true;
129
130 $atts = shortcode_atts(
131 array(
132 'duration' => '',
133 'height' => '',
134 'width' => '',
135 'bgcolor' => '',
136 'bgimg' => '',
137 'autoplay' => '',
138
139 // Settings
140 'transition' => '',
141 'scale' => '',
142 'rotate' => '',
143 'fade' => '',
144 'fadebullets' => '',
145 ), $atts, 'presentation'
146 );
147
148 $this->presentation_settings = array(
149 'transition' => 'down',
150 'scale' => 1,
151 'rotate' => 0,
152 'fade' => 'on',
153 'fadebullets' => 0,
154 'last' => array(
155 'x' => 0,
156 'y' => 0,
157 'scale' => 1,
158 'rotate' => 0,
159 ),
160 );
161
162 // Set the presentation-wide settings
163 if ( '' != trim( $atts['transition'] ) ) {
164 $this->presentation_settings['transition'] = $atts['transition'];
165 }
166
167 if ( '' != trim( $atts['scale'] ) ) {
168 $this->presentation_settings['scale'] = floatval( $atts['scale'] );
169 }
170
171 if ( '' != trim( $atts['rotate'] ) ) {
172 $this->presentation_settings['rotate'] = floatval( $atts['rotate'] );
173 }
174
175 if ( '' != trim( $atts['fade'] ) ) {
176 $this->presentation_settings['fade'] = $atts['fade'];
177 }
178
179 if ( '' != trim( $atts['fadebullets'] ) ) {
180 $this->presentation_settings['fadebullets'] = $atts['fadebullets'];
181 }
182
183 // Set any settings the slides don't care about
184 if ( '' != trim( $atts['duration'] ) ) {
185 $duration = floatval( $atts['duration'] ) . 's';
186 } else {
187 $duration = '1s';
188 }
189
190 // Autoplay durations are set in milliseconds
191 if ( '' != trim( $atts['autoplay'] ) ) {
192 $autoplay = floatval( $atts['autoplay'] ) * 1000;
193 } else {
194 $autoplay = 0;
195 } // No autoplay
196
197 // Set the presentation size as specified or with some nicely sized dimensions
198 if ( '' != trim( $atts['width'] ) ) {
199 $this->presentation_settings['width'] = intval( $atts['width'] );
200 } else {
201 $this->presentation_settings['width'] = 480;
202 }
203
204 if ( '' != trim( $atts['height'] ) ) {
205 $this->presentation_settings['height'] = intval( $atts['height'] );
206 } else {
207 $this->presentation_settings['height'] = 370;
208 }
209
210 // Hide the content by default in case the scripts fail
211 $style = 'display: none; width: ' . $this->presentation_settings['width'] . 'px; height: ' . $this->presentation_settings['height'] . 'px;';
212
213 // Check for background color XOR background image
214 // Use a white background if nothing specified
215 if ( preg_match( '/https?\:\/\/[^\'"\s]*/', $atts['bgimg'], $matches ) ) {
216 $style .= ' background-image: url("' . esc_url( $matches[0] ) . '");';
217 } else if ( '' != trim( $atts['bgcolor'] ) ) {
218 $style .= ' background-color: ' . esc_attr( $atts['bgcolor'] ) . ';';
219 } else {
220 $style .= ' background-color: #fff;';
221 }
222
223 // Not supported message style is inlined incase the style sheet doesn't get included
224 $out = "<section class='presentation-wrapper'>";
225 $out .= "<p class='not-supported-msg' style='display: inherit; padding: 25%; text-align: center;'>";
226 $out .= __( 'This slideshow could not be started. Try refreshing the page or viewing it in another browser.', 'jetpack' ) . '</p>';
227
228 // Bail out unless the scripts were added
229 if ( $this->scripts_and_style_included ) {
230 $out .= sprintf(
231 '<div class="presentation" duration="%s" data-autoplay="%s" style="%s">',
232 esc_attr( $duration ),
233 esc_attr( $autoplay ),
234 esc_attr( $style )
235 );
236 $out .= "<div class='nav-arrow-left'></div>";
237 $out .= "<div class='nav-arrow-right'></div>";
238 $out .= "<div class='nav-fullscreen-button'></div>";
239
240 if ( $autoplay ) {
241 $out .= '<div class="autoplay-overlay" style="display: none;"><p class="overlay-msg">';
242 $out .= __( 'Click to autoplay the presentation!', 'jetpack' );
243 $out .= '</p></div>';
244 }
245
246 $out .= do_shortcode( $content );
247 }
248
249 $out .= '</section>';
250
251 $this->presentation_initialized = false;
252
253 return $out;
254 }
255
256 function slide_shortcode( $atts, $content = '' ) {
257 // Bail out unless wrapped by a [presentation] shortcode
258 if ( ! $this->presentation_initialized ) {
259 return $content;
260 }
261
262 $atts = shortcode_atts(
263 array(
264 'transition' => '',
265 'scale' => '',
266 'rotate' => '',
267 'fade' => '',
268 'fadebullets' => '',
269 'bgcolor' => '',
270 'bgimg' => '',
271 ), $atts, 'slide'
272 );
273
274 // Determine positioning based on transition
275 if ( '' == trim( $atts['transition'] ) ) {
276 $atts['transition'] = $this->presentation_settings['transition'];
277 }
278
279 // Setting the content scale
280 if ( '' == trim( $atts['scale'] ) ) {
281 $atts['scale'] = $this->presentation_settings['scale'];
282 }
283
284 if ( '' == trim( $atts['scale'] ) ) {
285 $scale = 1;
286 } else {
287 $scale = floatval( $atts['scale'] );
288 }
289
290 if ( $scale < 0 ) {
291 $scale *= -1;
292 }
293
294 // Setting the content rotation
295 if ( '' == trim( $atts['rotate'] ) ) {
296 $atts['rotate'] = $this->presentation_settings['rotate'];
297 }
298
299 if ( '' == trim( $atts['rotate'] ) ) {
300 $rotate = 0;
301 } else {
302 $rotate = floatval( $atts['rotate'] );
303 }
304
305 // Setting if the content should fade
306 if ( '' == trim( $atts['fade'] ) ) {
307 $atts['fade'] = $this->presentation_settings['fade'];
308 }
309
310 if ( 'on' == $atts['fade'] || 'true' == $atts['fade'] ) {
311 $fade = 'fade';
312 } else {
313 $fade = '';
314 }
315
316 // Setting if bullets should fade on step changes
317 if ( '' == trim( $atts['fadebullets'] ) ) {
318 $atts['fadebullets'] = $this->presentation_settings['fadebullets'];
319 }
320
321 if ( 'on' == $atts['fadebullets'] || 'true' == $atts['fadebullets'] ) {
322 $fadebullets = 'fadebullets';
323 } else {
324 $fadebullets = '';
325 }
326
327 $coords = $this->get_coords(
328 array(
329 'transition' => $atts['transition'],
330 'scale' => $scale,
331 'rotate' => $rotate,
332 )
333 );
334
335 $x = $coords['x'];
336 $y = $coords['y'];
337
338 // Check for background color XOR background image
339 // Use a white background if nothing specified
340 if ( preg_match( '/https?\:\/\/[^\'"\s]*/', $atts['bgimg'], $matches ) ) {
341 $style = 'background-image: url("' . esc_url( $matches[0] ) . '");';
342 } else if ( '' != trim( $atts['bgcolor'] ) ) {
343 $style = 'background-color: ' . esc_attr( $atts['bgcolor'] ) . ';';
344 } else {
345 $style = '';
346 }
347
348 // Put everything together and let jmpress do the magic!
349 $out = sprintf(
350 '<div class="step %s %s" data-x="%s" data-y="%s" data-scale="%s" data-rotate="%s" style="%s">',
351 esc_attr( $fade ),
352 esc_attr( $fadebullets ),
353 esc_attr( $x ),
354 esc_attr( $y ),
355 esc_attr( $scale ),
356 esc_attr( $rotate ),
357 esc_attr( $style )
358 );
359
360 $out .= '<div class="slide-content">';
361 $out .= do_shortcode( $content );
362 $out .= '</div></div>';
363
364 return $out;
365 }
366
367 /**
368 * Determines the position of the next slide based on the position and scaling of the previous slide.
369 *
370 * @param array $args : an array with the following key-value pairs
371 * string $transition: the transition name, "up", "down", "left", or "right"
372 * float $scale: the scale of the next slide (used to determine the position of the slide after that)
373 *
374 * @return array with the 'x' and 'y' coordinates of the slide
375 */
376 function get_coords( $args ) {
377 if ( 0 == $args['scale'] ) {
378 $args['scale'] = 1;
379 }
380
381 $width = $this->presentation_settings['width'];
382 $height = $this->presentation_settings['height'];
383 $last = $this->presentation_settings['last'];
384 $scale = $last['scale'];
385
386 $next = array(
387 'x' => $last['x'],
388 'y' => $last['y'],
389 'scale' => $args['scale'],
390 'rotate' => $args['rotate'],
391 );
392
393 // All angles are measured from the vertical axis, so everything is backwards!
394 $diagAngle = atan2( $width, $height );
395 $diagonal = sqrt( pow( $width, 2 ) + pow( $height, 2 ) );
396
397 // We offset the angles by the angle formed by the diagonal so that
398 // we can multiply the sines directly against the diagonal length
399 $theta = deg2rad( $last['rotate'] ) - $diagAngle;
400 $phi = deg2rad( $next['rotate'] ) - $diagAngle;
401
402 // We start by displacing by the slide dimensions
403 $totalHorizDisp = $width * $scale;
404 $totalVertDisp = $height * $scale;
405
406 // If the previous slide was rotated, we add the incremental offset from the rotation
407 // Namely the difference between the regular dimension (no rotation) and the component
408 // of the diagonal for that angle
409 $totalHorizDisp += ( ( ( abs( sin( $theta ) ) * $diagonal ) - $width ) / 2 ) * $scale;
410 $totalVertDisp += ( ( ( abs( cos( $theta ) ) * $diagonal ) - $height ) / 2 ) * $scale;
411
412 // Similarly, we check if the current slide has been rotated and add whatever additional
413 // offset has been added. This is so that two rotated corners don't clash with each other.
414 // Note: we are checking the raw angle relative to the vertical axis, NOT the diagonal angle.
415 if ( 0 !== $next['rotate'] % 180 ) {
416 $totalHorizDisp += ( abs( ( sin( $phi ) * $diagonal ) - $width ) / 2 ) * $next['scale'];
417 $totalVertDisp += ( abs( ( cos( $phi ) * $diagonal ) - $height ) / 2 ) * $next['scale'];
418 }
419
420 switch ( trim( $args['transition'] ) ) {
421 case 'none':
422 break;
423
424 case 'left':
425 $next['x'] -= $totalHorizDisp;
426 break;
427
428 case 'right':
429 $next['x'] += $totalHorizDisp;
430 break;
431
432 case 'up':
433 $next['y'] -= $totalVertDisp;
434 break;
435
436 case 'down':
437 default:
438 $next['y'] += $totalVertDisp;
439 break;
440 }
441
442 $this->presentation_settings['last'] = $next;
443
444 return $next;
445 }
446 }
447
448 $GLOBALS['presentations'] = new Presentations();
449 endif;
450