| 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 |
* For the moment, comment out the setting for v2.8. |
| 32 |
* The remainder should work as it always has. |
| 33 |
* See: https://github.com/Automattic/jetpack/pull/85/files |
| 34 |
*/ |
| 35 |
// add_action( 'admin_init', array( $this, 'register_settings' ), 5 ); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Responds to the [gallery] shortcode, but not an actual shortcode callback. |
| 40 |
* |
| 41 |
* @param $value An empty string if nothing has modified the gallery output, the output html otherwise |
| 42 |
* @param $attr The shortcode attributes array |
| 43 |
* |
| 44 |
* @return string The (un)modified $value |
| 45 |
*/ |
| 46 |
function post_gallery( $value, $attr ) { |
| 47 |
// Bail if somebody else has done something |
| 48 |
if ( ! empty( $value ) ) |
| 49 |
return $value; |
| 50 |
|
| 51 |
// If [gallery type="slideshow"] have it behave just like [slideshow] |
| 52 |
if ( ! empty( $attr['type'] ) && 'slideshow' == $attr['type'] ) |
| 53 |
return $this->shortcode_callback( $attr ); |
| 54 |
|
| 55 |
return $value; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Add the Slideshow type to gallery settings |
| 60 |
* |
| 61 |
* @param $types An array of types where the key is the value, and the value is the caption. |
| 62 |
* @see Jetpack_Tiled_Gallery::media_ui_print_templates |
| 63 |
*/ |
| 64 |
function add_gallery_type( $types = array() ) { |
| 65 |
$types['slideshow'] = esc_html__( 'Slideshow', 'jetpack' ); |
| 66 |
return $types; |
| 67 |
} |
| 68 |
|
| 69 |
function register_settings() { |
| 70 |
add_settings_section( 'slideshow_section', __( 'Image Gallery Slideshow', 'jetpack' ), '__return_empty_string', 'media' ); |
| 71 |
|
| 72 |
add_settings_field( 'jetpack_slideshow_background_color', __( 'Background color', 'jetpack' ), array( $this, 'slideshow_background_color_callback' ), 'media', 'slideshow_section' ); |
| 73 |
|
| 74 |
register_setting( 'media', 'jetpack_slideshow_background_color', array( $this, 'slideshow_background_color_sanitize' ) ); |
| 75 |
} |
| 76 |
|
| 77 |
function slideshow_background_color_callback() { |
| 78 |
$options = array( |
| 79 |
'black' => __( 'Black', 'jetpack' ), |
| 80 |
'white' => __( 'White', 'jetpack' ), |
| 81 |
); |
| 82 |
$this->settings_select( 'jetpack_slideshow_background_color', $options ); |
| 83 |
} |
| 84 |
|
| 85 |
function settings_select( $name, $values, $extra_text = '' ) { |
| 86 |
if ( empty( $name ) || empty( $values ) || ! is_array( $values ) ) { |
| 87 |
return; |
| 88 |
} |
| 89 |
$option = get_option( $name ); |
| 90 |
?> |
| 91 |
<fieldset> |
| 92 |
<select name="<?php echo esc_attr( $name ); ?>" id="<?php esc_attr( $name ); ?>"> |
| 93 |
<?php foreach ( $values as $key => $value ) : ?> |
| 94 |
<option value="<?php echo esc_attr( $key ); ?>" <?php selected( $key, $option ); ?>> |
| 95 |
<?php echo esc_html( $value ); ?> |
| 96 |
</option> |
| 97 |
<?php endforeach; ?> |
| 98 |
</select> |
| 99 |
<?php if ( ! empty( $extra_text ) ) : ?> |
| 100 |
<p class="description"><?php echo esc_html( $extra_text ); ?></p> |
| 101 |
<?php endif; ?> |
| 102 |
</fieldset> |
| 103 |
<?php |
| 104 |
} |
| 105 |
|
| 106 |
function slideshow_background_color_sanitize( $value ) { |
| 107 |
return ( 'white' == $value ) ? 'white' : 'black'; |
| 108 |
} |
| 109 |
|
| 110 |
function shortcode_callback( $attr, $content = null ) { |
| 111 |
global $post, $content_width; |
| 112 |
|
| 113 |
$attr = shortcode_atts( array( |
| 114 |
'trans' => 'fade', |
| 115 |
'order' => 'ASC', |
| 116 |
'orderby' => 'menu_order ID', |
| 117 |
'id' => $post->ID, |
| 118 |
'include' => '', |
| 119 |
'exclude' => '', |
| 120 |
), $attr, 'slideshow' ); |
| 121 |
|
| 122 |
if ( 'rand' == strtolower( $attr['order'] ) ) |
| 123 |
$attr['orderby'] = 'none'; |
| 124 |
|
| 125 |
$attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] ); |
| 126 |
if ( ! $attr['orderby'] ) |
| 127 |
$attr['orderby'] = 'menu_order ID'; |
| 128 |
|
| 129 |
// Don't restrict to the current post if include |
| 130 |
$post_parent = ( empty( $attr['include'] ) ) ? intval( $attr['id'] ) : null; |
| 131 |
|
| 132 |
$attachments = get_posts( array( |
| 133 |
'post_status' => 'inherit', |
| 134 |
'post_type' => 'attachment', |
| 135 |
'post_mime_type' => 'image', |
| 136 |
'posts_per_page' => -1, |
| 137 |
'post_parent' => $post_parent, |
| 138 |
'order' => $attr['order'], |
| 139 |
'orderby' => $attr['orderby'], |
| 140 |
'include' => $attr['include'], |
| 141 |
'exclude' => $attr['exclude'], |
| 142 |
) ); |
| 143 |
|
| 144 |
if ( count( $attachments ) < 1 ) |
| 145 |
return; |
| 146 |
|
| 147 |
$gallery_instance = sprintf( "gallery-%d-%d", $attr['id'], ++$this->instance_count ); |
| 148 |
|
| 149 |
$gallery = array(); |
| 150 |
foreach ( $attachments as $attachment ) { |
| 151 |
$attachment_image_src = wp_get_attachment_image_src( $attachment->ID, 'full' ); |
| 152 |
$attachment_image_src = $attachment_image_src[0]; // [url, width, height] |
| 153 |
$caption = apply_filters( 'jetpack_slideshow_slide_caption', wptexturize( strip_tags( $attachment->post_excerpt ) ), $attachment->ID ); |
| 154 |
|
| 155 |
$gallery[] = (object) array( |
| 156 |
'src' => (string) esc_url_raw( $attachment_image_src ), |
| 157 |
'id' => (string) $attachment->ID, |
| 158 |
'caption' => (string) $caption, |
| 159 |
); |
| 160 |
} |
| 161 |
|
| 162 |
$max_width = intval( get_option( 'large_size_w' ) ); |
| 163 |
$max_height = 450; |
| 164 |
if ( intval( $content_width ) > 0 ) |
| 165 |
$max_width = min( intval( $content_width ), $max_width ); |
| 166 |
|
| 167 |
$color = Jetpack_Options::get_option( 'slideshow_background_color', 'black' ); |
| 168 |
|
| 169 |
$js_attr = array( |
| 170 |
'gallery' => $gallery, |
| 171 |
'selector' => $gallery_instance, |
| 172 |
'width' => $max_width, |
| 173 |
'height' => $max_height, |
| 174 |
'trans' => $attr['trans'] ? $attr['trans'] : 'fade', |
| 175 |
'color' => $color, |
| 176 |
); |
| 177 |
|
| 178 |
// Show a link to the gallery in feeds. |
| 179 |
if ( is_feed() ) |
| 180 |
return sprintf( '<a href="%s">%s</a>', |
| 181 |
esc_url( get_permalink( $post->ID ) . '#' . $gallery_instance . '-slideshow' ), |
| 182 |
esc_html__( 'Click to view slideshow.', 'jetpack' ) |
| 183 |
); |
| 184 |
|
| 185 |
return $this->slideshow_js( $js_attr ); |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Render the slideshow js |
| 190 |
* |
| 191 |
* Returns the necessary markup and js to fire a slideshow. |
| 192 |
* |
| 193 |
* @uses $this->enqueue_scripts() |
| 194 |
*/ |
| 195 |
function slideshow_js( $attr ) { |
| 196 |
// Enqueue scripts |
| 197 |
$this->enqueue_scripts(); |
| 198 |
|
| 199 |
if ( $attr['width'] <= 100 ) |
| 200 |
$attr['width'] = 450; |
| 201 |
|
| 202 |
if ( $attr['height'] <= 100 ) |
| 203 |
$attr['height'] = 450; |
| 204 |
|
| 205 |
// 40px padding |
| 206 |
$attr['width'] -= 40; |
| 207 |
$attr['height'] -= 40; |
| 208 |
|
| 209 |
$output = ''; |
| 210 |
|
| 211 |
if ( defined( 'JSON_HEX_AMP' ) ) { |
| 212 |
// This is nice to have, but not strictly necessary since we use _wp_specialchars() below |
| 213 |
$gallery = json_encode( $attr['gallery'], JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT ); |
| 214 |
} else { |
| 215 |
$gallery = json_encode( $attr['gallery'] ); |
| 216 |
} |
| 217 |
|
| 218 |
$output .= '<p class="jetpack-slideshow-noscript robots-nocontent">' . esc_html__( 'This slideshow requires JavaScript.', 'jetpack' ) . '</p>'; |
| 219 |
$output .= sprintf( '<div id="%s" class="slideshow-window jetpack-slideshow slideshow-%s" data-width="%s" data-height="%s" data-trans="%s" data-gallery="%s"></div>', |
| 220 |
esc_attr( $attr['selector'] . '-slideshow' ), |
| 221 |
esc_attr( $attr['color'] ), |
| 222 |
esc_attr( $attr['width'] ), |
| 223 |
esc_attr( $attr['height'] ), |
| 224 |
esc_attr( $attr['trans'] ), |
| 225 |
/* |
| 226 |
* The input to json_encode() above can contain '"'. |
| 227 |
* |
| 228 |
* For calls to json_encode() lacking the JSON_HEX_AMP option, |
| 229 |
* that '"' is left unaltered. Running '"' through esc_attr() |
| 230 |
* also leaves it unaltered since esc_attr() does not double-encode. |
| 231 |
* |
| 232 |
* This means we end up with an attribute like |
| 233 |
* `data-gallery="{"foo":"""}"`, |
| 234 |
* which is interpreted by the browser as `{"foo":"""}`, |
| 235 |
* which cannot be JSON decoded. |
| 236 |
* |
| 237 |
* The preferred workaround is to include the JSON_HEX_AMP (and friends) |
| 238 |
* options, but these are not available until 5.3.0. |
| 239 |
* Alternatively, we can use _wp_specialchars( , , , true ) instead of |
| 240 |
* esc_attr(), which will double-encode. |
| 241 |
* |
| 242 |
* Since we can't rely on JSON_HEX_AMP, we do both. |
| 243 |
*/ |
| 244 |
_wp_specialchars( wp_check_invalid_utf8( $gallery ), ENT_QUOTES, false, true ) |
| 245 |
); |
| 246 |
|
| 247 |
$output .= " |
| 248 |
<style> |
| 249 |
#{$attr['selector']}-slideshow .slideshow-slide img { |
| 250 |
max-height: " . intval( $attr['height'] ) ."px; |
| 251 |
/* Emulate max-height in IE 6 */ |
| 252 |
_height: expression(this.scrollHeight >= " . intval( $attr['height'] ) . " ? '" . intval( $attr['height'] ) . "px' : 'auto'); |
| 253 |
} |
| 254 |
</style> |
| 255 |
"; |
| 256 |
|
| 257 |
return $output; |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Infinite Scroll needs the scripts to be present at all times |
| 262 |
*/ |
| 263 |
function maybe_enqueue_scripts() { |
| 264 |
if ( is_home() && current_theme_supports( 'infinite-scroll' ) ) |
| 265 |
$this->enqueue_scripts(); |
| 266 |
} |
| 267 |
|
| 268 |
/** |
| 269 |
* Actually enqueues the scripts and styles. |
| 270 |
*/ |
| 271 |
function enqueue_scripts() { |
| 272 |
static $enqueued = false; |
| 273 |
|
| 274 |
if ( $enqueued ) |
| 275 |
return; |
| 276 |
|
| 277 |
wp_enqueue_script( 'jquery-cycle', plugins_url( '/js/jquery.cycle.js', __FILE__ ) , array( 'jquery' ), '2.9999.8', true ); |
| 278 |
wp_enqueue_script( 'jetpack-slideshow', plugins_url( '/js/slideshow-shortcode.js', __FILE__ ), array( 'jquery-cycle' ), '20121214.1', true ); |
| 279 |
if( is_rtl() ) { |
| 280 |
wp_enqueue_style( 'jetpack-slideshow', plugins_url( '/css/rtl/slideshow-shortcode-rtl.css', __FILE__ ) ); |
| 281 |
} else { |
| 282 |
wp_enqueue_style( 'jetpack-slideshow', plugins_url( '/css/slideshow-shortcode.css', __FILE__ ) ); |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
wp_localize_script( 'jetpack-slideshow', 'jetpackSlideshowSettings', apply_filters( 'jetpack_js_slideshow_settings', array( |
| 287 |
'spinner' => plugins_url( '/img/slideshow-loader.gif', __FILE__ ), |
| 288 |
) ) ); |
| 289 |
|
| 290 |
$enqueued = true; |
| 291 |
} |
| 292 |
|
| 293 |
public static function init() { |
| 294 |
$gallery = new Jetpack_Slideshow_Shortcode; |
| 295 |
} |
| 296 |
} |
| 297 |
add_action( 'init', array( 'Jetpack_Slideshow_Shortcode', 'init' ) ); |
| 298 |
|