presentations.php
| 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( |
| 123 | 'jmpress', |
| 124 | Jetpack::get_file_url_for_environment( '_inc/build/shortcodes/js/jmpress.min.js', 'modules/shortcodes/js/jmpress.js' ), |
| 125 | array( 'jquery' ), |
| 126 | '0.4.5', |
| 127 | true |
| 128 | ); |
| 129 | wp_enqueue_script( |
| 130 | 'presentations', |
| 131 | Jetpack::get_file_url_for_environment( '_inc/build/shortcodes/js/main.min.js', 'modules/shortcodes/js/main.js' ), |
| 132 | array( 'jquery', 'jmpress' ), |
| 133 | false, |
| 134 | true |
| 135 | ); |
| 136 | } |
| 137 | |
| 138 | function presentation_shortcode( $atts, $content = '' ) { |
| 139 | // Mark that we've found a valid [presentation] shortcode |
| 140 | $this->presentation_initialized = true; |
| 141 | |
| 142 | $atts = shortcode_atts( |
| 143 | array( |
| 144 | 'duration' => '', |
| 145 | 'height' => '', |
| 146 | 'width' => '', |
| 147 | 'bgcolor' => '', |
| 148 | 'bgimg' => '', |
| 149 | 'autoplay' => '', |
| 150 | |
| 151 | // Settings |
| 152 | 'transition' => '', |
| 153 | 'scale' => '', |
| 154 | 'rotate' => '', |
| 155 | 'fade' => '', |
| 156 | 'fadebullets' => '', |
| 157 | ), |
| 158 | $atts, |
| 159 | 'presentation' |
| 160 | ); |
| 161 | |
| 162 | $this->presentation_settings = array( |
| 163 | 'transition' => 'down', |
| 164 | 'scale' => 1, |
| 165 | 'rotate' => 0, |
| 166 | 'fade' => 'on', |
| 167 | 'fadebullets' => 0, |
| 168 | 'last' => array( |
| 169 | 'x' => 0, |
| 170 | 'y' => 0, |
| 171 | 'scale' => 1, |
| 172 | 'rotate' => 0, |
| 173 | ), |
| 174 | ); |
| 175 | |
| 176 | // Set the presentation-wide settings |
| 177 | if ( '' != trim( $atts['transition'] ) ) { |
| 178 | $this->presentation_settings['transition'] = $atts['transition']; |
| 179 | } |
| 180 | |
| 181 | if ( '' != trim( $atts['scale'] ) ) { |
| 182 | $this->presentation_settings['scale'] = floatval( $atts['scale'] ); |
| 183 | } |
| 184 | |
| 185 | if ( '' != trim( $atts['rotate'] ) ) { |
| 186 | $this->presentation_settings['rotate'] = floatval( $atts['rotate'] ); |
| 187 | } |
| 188 | |
| 189 | if ( '' != trim( $atts['fade'] ) ) { |
| 190 | $this->presentation_settings['fade'] = $atts['fade']; |
| 191 | } |
| 192 | |
| 193 | if ( '' != trim( $atts['fadebullets'] ) ) { |
| 194 | $this->presentation_settings['fadebullets'] = $atts['fadebullets']; |
| 195 | } |
| 196 | |
| 197 | // Set any settings the slides don't care about |
| 198 | if ( '' != trim( $atts['duration'] ) ) { |
| 199 | $duration = floatval( $atts['duration'] ) . 's'; |
| 200 | } else { |
| 201 | $duration = '1s'; |
| 202 | } |
| 203 | |
| 204 | // Autoplay durations are set in milliseconds |
| 205 | if ( '' != trim( $atts['autoplay'] ) ) { |
| 206 | $autoplay = floatval( $atts['autoplay'] ) * 1000; |
| 207 | } else { |
| 208 | $autoplay = 0; |
| 209 | } // No autoplay |
| 210 | |
| 211 | // Set the presentation size as specified or with some nicely sized dimensions |
| 212 | if ( '' != trim( $atts['width'] ) ) { |
| 213 | $this->presentation_settings['width'] = intval( $atts['width'] ); |
| 214 | } else { |
| 215 | $this->presentation_settings['width'] = 480; |
| 216 | } |
| 217 | |
| 218 | if ( '' != trim( $atts['height'] ) ) { |
| 219 | $this->presentation_settings['height'] = intval( $atts['height'] ); |
| 220 | } else { |
| 221 | $this->presentation_settings['height'] = 370; |
| 222 | } |
| 223 | |
| 224 | // Hide the content by default in case the scripts fail |
| 225 | $style = 'display: none; width: ' . $this->presentation_settings['width'] . 'px; height: ' . $this->presentation_settings['height'] . 'px;'; |
| 226 | |
| 227 | // Check for background color XOR background image |
| 228 | // Use a white background if nothing specified |
| 229 | if ( preg_match( '/https?\:\/\/[^\'"\s]*/', $atts['bgimg'], $matches ) ) { |
| 230 | $style .= ' background-image: url("' . esc_url( $matches[0] ) . '");'; |
| 231 | } elseif ( '' != trim( $atts['bgcolor'] ) ) { |
| 232 | $style .= ' background-color: ' . esc_attr( $atts['bgcolor'] ) . ';'; |
| 233 | } else { |
| 234 | $style .= ' background-color: #fff;'; |
| 235 | } |
| 236 | |
| 237 | // Not supported message style is inlined incase the style sheet doesn't get included |
| 238 | $out = "<section class='presentation-wrapper'>"; |
| 239 | $out .= "<p class='not-supported-msg' style='display: inherit; padding: 25%; text-align: center;'>"; |
| 240 | $out .= __( 'This slideshow could not be started. Try refreshing the page or viewing it in another browser.', 'jetpack' ) . '</p>'; |
| 241 | |
| 242 | // Bail out unless the scripts were added |
| 243 | if ( $this->scripts_and_style_included ) { |
| 244 | $out .= sprintf( |
| 245 | '<div class="presentation" duration="%s" data-autoplay="%s" style="%s">', |
| 246 | esc_attr( $duration ), |
| 247 | esc_attr( $autoplay ), |
| 248 | esc_attr( $style ) |
| 249 | ); |
| 250 | $out .= "<div class='nav-arrow-left'></div>"; |
| 251 | $out .= "<div class='nav-arrow-right'></div>"; |
| 252 | $out .= "<div class='nav-fullscreen-button'></div>"; |
| 253 | |
| 254 | if ( $autoplay ) { |
| 255 | $out .= '<div class="autoplay-overlay" style="display: none;"><p class="overlay-msg">'; |
| 256 | $out .= __( 'Click to autoplay the presentation!', 'jetpack' ); |
| 257 | $out .= '</p></div>'; |
| 258 | } |
| 259 | |
| 260 | $out .= do_shortcode( $content ); |
| 261 | } |
| 262 | |
| 263 | $out .= '</section>'; |
| 264 | |
| 265 | $this->presentation_initialized = false; |
| 266 | |
| 267 | return $out; |
| 268 | } |
| 269 | |
| 270 | function slide_shortcode( $atts, $content = '' ) { |
| 271 | // Bail out unless wrapped by a [presentation] shortcode |
| 272 | if ( ! $this->presentation_initialized ) { |
| 273 | return $content; |
| 274 | } |
| 275 | |
| 276 | $atts = shortcode_atts( |
| 277 | array( |
| 278 | 'transition' => '', |
| 279 | 'scale' => '', |
| 280 | 'rotate' => '', |
| 281 | 'fade' => '', |
| 282 | 'fadebullets' => '', |
| 283 | 'bgcolor' => '', |
| 284 | 'bgimg' => '', |
| 285 | ), |
| 286 | $atts, |
| 287 | 'slide' |
| 288 | ); |
| 289 | |
| 290 | // Determine positioning based on transition |
| 291 | if ( '' == trim( $atts['transition'] ) ) { |
| 292 | $atts['transition'] = $this->presentation_settings['transition']; |
| 293 | } |
| 294 | |
| 295 | // Setting the content scale |
| 296 | if ( '' == trim( $atts['scale'] ) ) { |
| 297 | $atts['scale'] = $this->presentation_settings['scale']; |
| 298 | } |
| 299 | |
| 300 | if ( '' == trim( $atts['scale'] ) ) { |
| 301 | $scale = 1; |
| 302 | } else { |
| 303 | $scale = floatval( $atts['scale'] ); |
| 304 | } |
| 305 | |
| 306 | if ( $scale < 0 ) { |
| 307 | $scale *= -1; |
| 308 | } |
| 309 | |
| 310 | // Setting the content rotation |
| 311 | if ( '' == trim( $atts['rotate'] ) ) { |
| 312 | $atts['rotate'] = $this->presentation_settings['rotate']; |
| 313 | } |
| 314 | |
| 315 | if ( '' == trim( $atts['rotate'] ) ) { |
| 316 | $rotate = 0; |
| 317 | } else { |
| 318 | $rotate = floatval( $atts['rotate'] ); |
| 319 | } |
| 320 | |
| 321 | // Setting if the content should fade |
| 322 | if ( '' == trim( $atts['fade'] ) ) { |
| 323 | $atts['fade'] = $this->presentation_settings['fade']; |
| 324 | } |
| 325 | |
| 326 | if ( 'on' == $atts['fade'] || 'true' == $atts['fade'] ) { |
| 327 | $fade = 'fade'; |
| 328 | } else { |
| 329 | $fade = ''; |
| 330 | } |
| 331 | |
| 332 | // Setting if bullets should fade on step changes |
| 333 | if ( '' == trim( $atts['fadebullets'] ) ) { |
| 334 | $atts['fadebullets'] = $this->presentation_settings['fadebullets']; |
| 335 | } |
| 336 | |
| 337 | if ( 'on' == $atts['fadebullets'] || 'true' == $atts['fadebullets'] ) { |
| 338 | $fadebullets = 'fadebullets'; |
| 339 | } else { |
| 340 | $fadebullets = ''; |
| 341 | } |
| 342 | |
| 343 | $coords = $this->get_coords( |
| 344 | array( |
| 345 | 'transition' => $atts['transition'], |
| 346 | 'scale' => $scale, |
| 347 | 'rotate' => $rotate, |
| 348 | ) |
| 349 | ); |
| 350 | |
| 351 | $x = $coords['x']; |
| 352 | $y = $coords['y']; |
| 353 | |
| 354 | // Check for background color XOR background image |
| 355 | // Use a white background if nothing specified |
| 356 | if ( preg_match( '/https?\:\/\/[^\'"\s]*/', $atts['bgimg'], $matches ) ) { |
| 357 | $style = 'background-image: url("' . esc_url( $matches[0] ) . '");'; |
| 358 | } elseif ( '' != trim( $atts['bgcolor'] ) ) { |
| 359 | $style = 'background-color: ' . esc_attr( $atts['bgcolor'] ) . ';'; |
| 360 | } else { |
| 361 | $style = ''; |
| 362 | } |
| 363 | |
| 364 | // Put everything together and let jmpress do the magic! |
| 365 | $out = sprintf( |
| 366 | '<div class="step %s %s" data-x="%s" data-y="%s" data-scale="%s" data-rotate="%s" style="%s">', |
| 367 | esc_attr( $fade ), |
| 368 | esc_attr( $fadebullets ), |
| 369 | esc_attr( $x ), |
| 370 | esc_attr( $y ), |
| 371 | esc_attr( $scale ), |
| 372 | esc_attr( $rotate ), |
| 373 | esc_attr( $style ) |
| 374 | ); |
| 375 | |
| 376 | $out .= '<div class="slide-content">'; |
| 377 | $out .= do_shortcode( $content ); |
| 378 | $out .= '</div></div>'; |
| 379 | |
| 380 | return $out; |
| 381 | } |
| 382 | |
| 383 | /** |
| 384 | * Determines the position of the next slide based on the position and scaling of the previous slide. |
| 385 | * |
| 386 | * @param array $args : an array with the following key-value pairs |
| 387 | * string $transition: the transition name, "up", "down", "left", or "right" |
| 388 | * float $scale: the scale of the next slide (used to determine the position of the slide after that) |
| 389 | * |
| 390 | * @return array with the 'x' and 'y' coordinates of the slide |
| 391 | */ |
| 392 | function get_coords( $args ) { |
| 393 | if ( 0 == $args['scale'] ) { |
| 394 | $args['scale'] = 1; |
| 395 | } |
| 396 | |
| 397 | $width = $this->presentation_settings['width']; |
| 398 | $height = $this->presentation_settings['height']; |
| 399 | $last = $this->presentation_settings['last']; |
| 400 | $scale = $last['scale']; |
| 401 | |
| 402 | $next = array( |
| 403 | 'x' => $last['x'], |
| 404 | 'y' => $last['y'], |
| 405 | 'scale' => $args['scale'], |
| 406 | 'rotate' => $args['rotate'], |
| 407 | ); |
| 408 | |
| 409 | // All angles are measured from the vertical axis, so everything is backwards! |
| 410 | $diagAngle = atan2( $width, $height ); |
| 411 | $diagonal = sqrt( pow( $width, 2 ) + pow( $height, 2 ) ); |
| 412 | |
| 413 | // We offset the angles by the angle formed by the diagonal so that |
| 414 | // we can multiply the sines directly against the diagonal length |
| 415 | $theta = deg2rad( $last['rotate'] ) - $diagAngle; |
| 416 | $phi = deg2rad( $next['rotate'] ) - $diagAngle; |
| 417 | |
| 418 | // We start by displacing by the slide dimensions |
| 419 | $totalHorizDisp = $width * $scale; |
| 420 | $totalVertDisp = $height * $scale; |
| 421 | |
| 422 | // If the previous slide was rotated, we add the incremental offset from the rotation |
| 423 | // Namely the difference between the regular dimension (no rotation) and the component |
| 424 | // of the diagonal for that angle |
| 425 | $totalHorizDisp += ( ( ( abs( sin( $theta ) ) * $diagonal ) - $width ) / 2 ) * $scale; |
| 426 | $totalVertDisp += ( ( ( abs( cos( $theta ) ) * $diagonal ) - $height ) / 2 ) * $scale; |
| 427 | |
| 428 | // Similarly, we check if the current slide has been rotated and add whatever additional |
| 429 | // offset has been added. This is so that two rotated corners don't clash with each other. |
| 430 | // Note: we are checking the raw angle relative to the vertical axis, NOT the diagonal angle. |
| 431 | if ( 0 !== $next['rotate'] % 180 ) { |
| 432 | $totalHorizDisp += ( abs( ( sin( $phi ) * $diagonal ) - $width ) / 2 ) * $next['scale']; |
| 433 | $totalVertDisp += ( abs( ( cos( $phi ) * $diagonal ) - $height ) / 2 ) * $next['scale']; |
| 434 | } |
| 435 | |
| 436 | switch ( trim( $args['transition'] ) ) { |
| 437 | case 'none': |
| 438 | break; |
| 439 | |
| 440 | case 'left': |
| 441 | $next['x'] -= $totalHorizDisp; |
| 442 | break; |
| 443 | |
| 444 | case 'right': |
| 445 | $next['x'] += $totalHorizDisp; |
| 446 | break; |
| 447 | |
| 448 | case 'up': |
| 449 | $next['y'] -= $totalVertDisp; |
| 450 | break; |
| 451 | |
| 452 | case 'down': |
| 453 | default: |
| 454 | $next['y'] += $totalVertDisp; |
| 455 | break; |
| 456 | } |
| 457 | |
| 458 | $this->presentation_settings['last'] = $next; |
| 459 | |
| 460 | return $next; |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | $GLOBALS['presentations'] = new Presentations(); |
| 465 | endif; |
| 466 |