PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 2.7.5
Jetpack – WP Security, Backup, Speed, & Growth v2.7.5
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 / slideshow.php
slideshow.php
209 lines 6.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Slideshow shortcode usage: [gallery type="slideshow"] or the older [slideshow]
5 */
6 class Jetpack_Slideshow_Shortcode {
7 public $instance_count = 0;
8
9 function __construct() {
10 global $shortcode_tags;
11
12 $needs_scripts = false;
13
14 // Only if the slideshow shortcode has not already been defined.
15 if ( ! array_key_exists( 'slideshow', $shortcode_tags ) ) {
16 add_shortcode( 'slideshow', array( $this, 'shortcode_callback' ) );
17 $needs_scripts = true;
18 }
19
20 // Only if the gallery shortcode has not been redefined.
21 if ( isset( $shortcode_tags['gallery'] ) && $shortcode_tags['gallery'] == 'gallery_shortcode' ) {
22 add_filter( 'post_gallery', array( $this, 'post_gallery' ), 1002, 2 );
23 add_filter( 'jetpack_gallery_types', array( $this, 'add_gallery_type' ), 10 );
24 $needs_scripts = true;
25 }
26
27 if ( $needs_scripts )
28 add_action( 'wp_enqueue_scripts', array( $this, 'maybe_enqueue_scripts' ), 1 );
29 }
30
31 /**
32 * Responds to the [gallery] shortcode, but not an actual shortcode callback.
33 *
34 * @param $value An empty string if nothing has modified the gallery output, the output html otherwise
35 * @param $attr The shortcode attributes array
36 *
37 * @return string The (un)modified $value
38 */
39 function post_gallery( $value, $attr ) {
40 // Bail if somebody else has done something
41 if ( ! empty( $value ) )
42 return $value;
43
44 // If [gallery type="slideshow"] have it behave just like [slideshow]
45 if ( ! empty( $attr['type'] ) && 'slideshow' == $attr['type'] )
46 return $this->shortcode_callback( $attr );
47
48 return $value;
49 }
50
51 /**
52 * Add the Slideshow type to gallery settings
53 *
54 * @param $types An array of types where the key is the value, and the value is the caption.
55 * @see Jetpack_Tiled_Gallery::media_ui_print_templates
56 */
57 function add_gallery_type( $types = array() ) {
58 $types['slideshow'] = esc_html__( 'Slideshow', 'jetpack' );
59 return $types;
60 }
61
62 function shortcode_callback( $attr, $content = null ) {
63 global $post, $content_width;
64
65 $attr = shortcode_atts( array(
66 'trans' => 'fade',
67 'order' => 'ASC',
68 'orderby' => 'menu_order ID',
69 'id' => $post->ID,
70 'include' => '',
71 'exclude' => '',
72 ), $attr );
73
74 if ( 'rand' == strtolower( $attr['order'] ) )
75 $attr['orderby'] = 'none';
76
77 $attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
78 if ( ! $attr['orderby'] )
79 $attr['orderby'] = 'menu_order ID';
80
81 // Don't restrict to the current post if include
82 $post_parent = ( empty( $attr['include'] ) ) ? intval( $attr['id'] ) : null;
83
84 $attachments = get_posts( array(
85 'post_status' => 'inherit',
86 'post_type' => 'attachment',
87 'post_mime_type' => 'image',
88 'posts_per_page' => -1,
89 'post_parent' => $post_parent,
90 'order' => $attr['order'],
91 'orderby' => $attr['orderby'],
92 'include' => $attr['include'],
93 'exclude' => $attr['exclude'],
94 ) );
95
96 if ( count( $attachments ) < 2 )
97 return;
98
99 $gallery_instance = sprintf( "gallery-%d-%d", $attr['id'], ++$this->instance_count );
100
101 $gallery = array();
102 foreach ( $attachments as $attachment ) {
103 $attachment_image_src = wp_get_attachment_image_src( $attachment->ID, 'full' );
104 $attachment_image_src = $attachment_image_src[0]; // [url, width, height]
105 $caption = apply_filters( 'jetpack_slideshow_slide_caption', wptexturize( strip_tags( $attachment->post_excerpt ) ), $attachment->ID );
106
107 $gallery[] = (object) array(
108 'src' => (string) esc_url_raw( $attachment_image_src ),
109 'id' => (string) $attachment->ID,
110 'caption' => (string) $caption,
111 );
112 }
113
114 $max_width = intval( get_option( 'large_size_w' ) );
115 $max_height = 450;
116 if ( intval( $content_width ) > 0 )
117 $max_width = min( intval( $content_width ), $max_width );
118
119 $js_attr = array(
120 'gallery' => $gallery,
121 'selector' => $gallery_instance,
122 'width' => $max_width,
123 'height' => $max_height,
124 'trans' => $attr['trans'] ? $attr['trans'] : 'fade',
125 );
126
127 // Show a link to the gallery in feeds.
128 if ( is_feed() )
129 return sprintf( '<a href="%s">%s</a>',
130 esc_url( get_permalink( $post->ID ) . '#' . $gallery_instance . '-slideshow' ),
131 esc_html__( 'Click to view slideshow.', 'jetpack' )
132 );
133
134 return $this->slideshow_js( $js_attr );
135 }
136
137 /**
138 * Render the slideshow js
139 *
140 * Returns the necessary markup and js to fire a slideshow.
141 *
142 * @uses $this->enqueue_scripts()
143 */
144 function slideshow_js( $attr ) {
145 // Enqueue scripts
146 $this->enqueue_scripts();
147
148 if ( $attr['width'] <= 100 )
149 $attr['width'] = 450;
150
151 if ( $attr['height'] <= 100 )
152 $attr['height'] = 450;
153
154 // 40px padding
155 $attr['width'] -= 40;
156 $attr['height'] -= 40;
157
158 $output = '';
159
160 $output .= '<p class="jetpack-slideshow-noscript robots-nocontent">' . esc_html__( 'This slideshow requires JavaScript.', 'jetpack' ) . '</p>';
161 $output .= '<div id="' . esc_attr( $attr['selector'] . '-slideshow' ) . '" class="slideshow-window jetpack-slideshow" data-width="' . esc_attr( $attr['width'] ) . '" data-height="' . esc_attr( $attr['height'] ) . '" data-trans="' . esc_attr( $attr['trans'] ) . '" data-gallery="' . esc_attr( json_encode( $attr['gallery'] ) ) . '"></div>';
162
163 $output .= "
164 <style>
165 #{$attr['selector']}-slideshow .slideshow-slide img {
166 max-height: " . intval( $attr['height'] ) ."px;
167 /* Emulate max-height in IE 6 */
168 _height: expression(this.scrollHeight >= " . intval( $attr['height'] ) . " ? '" . intval( $attr['height'] ) . "px' : 'auto');
169 }
170 </style>
171 ";
172
173 return $output;
174 }
175
176 /**
177 * Infinite Scroll needs the scripts to be present at all times
178 */
179 function maybe_enqueue_scripts() {
180 if ( is_home() && current_theme_supports( 'infinite-scroll' ) )
181 $this->enqueue_scripts();
182 }
183
184 /**
185 * Actually enqueues the scripts and styles.
186 */
187 function enqueue_scripts() {
188 static $enqueued = false;
189
190 if ( $enqueued )
191 return;
192
193 wp_enqueue_script( 'jquery-cycle', plugins_url( '/js/jquery.cycle.js', __FILE__ ) , array( 'jquery' ), '2.9999.8', true );
194 wp_enqueue_script( 'jetpack-slideshow', plugins_url( '/js/slideshow-shortcode.js', __FILE__ ), array( 'jquery-cycle' ), '20121214.1', true );
195 wp_enqueue_style( 'jetpack-slideshow', plugins_url( '/css/slideshow-shortcode.css', __FILE__ ) );
196
197 wp_localize_script( 'jetpack-slideshow', 'jetpackSlideshowSettings', apply_filters( 'jetpack_js_slideshow_settings', array(
198 'spinner' => plugins_url( '/img/slideshow-loader.gif', __FILE__ ),
199 ) ) );
200
201 $enqueued = true;
202 }
203
204 public static function init() {
205 $gallery = new Jetpack_Slideshow_Shortcode;
206 }
207 }
208 add_action( 'init', array( 'Jetpack_Slideshow_Shortcode', 'init' ) );
209