| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
Plugin Name: Gallery |
| 5 |
Description: Gallery widget |
| 6 |
Author: Automattic Inc. |
| 7 |
Version: 1.0 |
| 8 |
Author URI: http://automattic.com |
| 9 |
*/ |
| 10 |
|
| 11 |
class Jetpack_Gallery_Widget extends WP_Widget { |
| 12 |
const THUMB_SIZE = 45; |
| 13 |
const DEFAULT_WIDTH = 265; |
| 14 |
|
| 15 |
protected $_instance_width; |
| 16 |
|
| 17 |
public function __construct() { |
| 18 |
$widget_ops = array( |
| 19 |
'classname' => 'widget-gallery', |
| 20 |
'description' => __( 'Display a photo gallery or slideshow', 'jetpack' ), |
| 21 |
'customize_selective_refresh' => true, |
| 22 |
); |
| 23 |
|
| 24 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) ); |
| 25 |
|
| 26 |
parent::__construct( |
| 27 |
'gallery', |
| 28 |
/** This filter is documented in modules/widgets/facebook-likebox.php */ |
| 29 |
apply_filters( 'jetpack_widget_name', __( 'Gallery', 'jetpack' ) ), |
| 30 |
$widget_ops |
| 31 |
); |
| 32 |
|
| 33 |
if ( is_customize_preview() ) { |
| 34 |
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend_scripts' ) ); |
| 35 |
|
| 36 |
if ( class_exists( 'Jetpack_Tiled_Gallery' ) ) { |
| 37 |
add_action( 'wp_enqueue_scripts', array( 'Jetpack_Tiled_Gallery', 'default_scripts_and_styles' ) ); |
| 38 |
} |
| 39 |
|
| 40 |
if ( class_exists( 'Jetpack_Slideshow_Shortcode' ) ) { |
| 41 |
$slideshow = new Jetpack_Slideshow_Shortcode(); |
| 42 |
add_action( 'wp_enqueue_scripts', array( $slideshow, 'enqueue_scripts' ) ); |
| 43 |
} |
| 44 |
|
| 45 |
if ( class_exists( 'Jetpack_Carousel' ) ) { |
| 46 |
$carousel = new Jetpack_Carousel(); |
| 47 |
add_action( 'wp_enqueue_scripts', array( $carousel, 'enqueue_assets' ) ); |
| 48 |
} |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* @param array $args Display arguments including before_title, after_title, before_widget, and after_widget. |
| 54 |
* @param array $instance The settings for the particular instance of the widget. |
| 55 |
*/ |
| 56 |
public function widget( $args, $instance ) { |
| 57 |
$instance = wp_parse_args( (array) $instance, $this->defaults() ); |
| 58 |
|
| 59 |
$this->enqueue_frontend_scripts(); |
| 60 |
|
| 61 |
extract( $args ); |
| 62 |
|
| 63 |
$instance['attachments'] = $this->get_attachments( $instance ); |
| 64 |
|
| 65 |
$classes = array(); |
| 66 |
|
| 67 |
$classes[] = 'widget-gallery-' . $instance['type']; |
| 68 |
|
| 69 |
// Due to a bug in the carousel plugin, carousels will be triggered for all tiled galleries that exist on a page |
| 70 |
// with other tiled galleries, regardless of whether or not the widget was set to Carousel mode. The onClick selector |
| 71 |
// is simply too broad, since it was not written with widgets in mind. This special class prevents that behavior, via |
| 72 |
// an override handler in gallery.js |
| 73 |
if ( 'carousel' != $instance['link'] && 'slideshow' != $instance['type'] ) { |
| 74 |
$classes[] = 'no-carousel'; |
| 75 |
} else { |
| 76 |
$classes[] = 'carousel'; |
| 77 |
} |
| 78 |
|
| 79 |
$classes = implode( ' ', $classes ); |
| 80 |
|
| 81 |
if ( 'carousel' == $instance['link'] ) { |
| 82 |
require_once plugin_dir_path( realpath( dirname( __FILE__ ) . '/../carousel/jetpack-carousel.php' ) ) . 'jetpack-carousel.php'; |
| 83 |
|
| 84 |
if ( class_exists( 'Jetpack_Carousel' ) ) { |
| 85 |
// Create new carousel so we can use the enqueue_assets() method. Not ideal, but there is a decent amount |
| 86 |
// of logic in that method that shouldn't be duplicated. |
| 87 |
$carousel = new Jetpack_Carousel(); |
| 88 |
|
| 89 |
// First parameter is $output, which comes from filters, and causes bypass of the asset enqueuing. Passing null is correct. |
| 90 |
$carousel->enqueue_assets( null ); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
echo $before_widget . "\n"; |
| 95 |
|
| 96 |
/** This filter is documented in core/src/wp-includes/default-widgets.php */ |
| 97 |
$title = apply_filters( 'widget_title', $instance['title'] ); |
| 98 |
|
| 99 |
if ( $title ) { |
| 100 |
echo $before_title . esc_html( $title ) . $after_title . "\n"; |
| 101 |
} |
| 102 |
|
| 103 |
echo '<div class="' . esc_attr( $classes ) . '">' . "\n"; |
| 104 |
|
| 105 |
$method = $instance['type'] . '_widget'; |
| 106 |
|
| 107 |
/** |
| 108 |
* Allow the width of a gallery to be altered by themes or other code. |
| 109 |
* |
| 110 |
* @module widgets |
| 111 |
* |
| 112 |
* @since 2.5.0 |
| 113 |
* |
| 114 |
* @param int self::DEFAULT_WIDTH Default gallery width. Default is 265. |
| 115 |
* @param string $args Display arguments including before_title, after_title, before_widget, and after_widget. |
| 116 |
* @param array $instance The settings for the particular instance of the widget. |
| 117 |
*/ |
| 118 |
$this->_instance_width = apply_filters( 'gallery_widget_content_width', self::DEFAULT_WIDTH, $args, $instance ); |
| 119 |
|
| 120 |
// Register a filter to modify the tiled_gallery_content_width, so Jetpack_Tiled_Gallery |
| 121 |
// can appropriately size the tiles. |
| 122 |
add_filter( 'tiled_gallery_content_width', array( $this, 'tiled_gallery_content_width' ) ); |
| 123 |
|
| 124 |
if ( method_exists( $this, $method ) ) { |
| 125 |
echo $this->$method( $args, $instance ); |
| 126 |
} |
| 127 |
|
| 128 |
// Remove the stored $_instance_width, as it is no longer needed |
| 129 |
$this->_instance_width = null; |
| 130 |
|
| 131 |
// Remove the filter, so any Jetpack_Tiled_Gallery in a post is not affected |
| 132 |
remove_filter( 'tiled_gallery_content_width', array( $this, 'tiled_gallery_content_width' ) ); |
| 133 |
|
| 134 |
echo "\n" . '</div>'; // .widget-gallery-$type |
| 135 |
|
| 136 |
echo "\n" . $after_widget; |
| 137 |
|
| 138 |
/** This action is documented in modules/widgets/gravatar-profile.php */ |
| 139 |
do_action( 'jetpack_stats_extra', 'widget_view', 'gallery' ); |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Fetch the images attached to the gallery Widget |
| 144 |
* |
| 145 |
* @param array $instance The Widget instance for which you'd like attachments |
| 146 |
* @return array Array of attachment objects for the Widget in $instance |
| 147 |
*/ |
| 148 |
public function get_attachments( $instance ) { |
| 149 |
$ids = explode( ',', $instance['ids'] ); |
| 150 |
|
| 151 |
if ( isset( $instance['random'] ) && 'on' == $instance['random'] ) { |
| 152 |
shuffle( $ids ); |
| 153 |
} |
| 154 |
|
| 155 |
$attachments_query = new WP_Query( |
| 156 |
array( |
| 157 |
'post__in' => $ids, |
| 158 |
'post_status' => 'inherit', |
| 159 |
'post_type' => 'attachment', |
| 160 |
'post_mime_type' => 'image', |
| 161 |
'posts_per_page' => -1, |
| 162 |
'orderby' => 'post__in', |
| 163 |
) |
| 164 |
); |
| 165 |
|
| 166 |
$attachments = $attachments_query->get_posts(); |
| 167 |
|
| 168 |
wp_reset_postdata(); |
| 169 |
|
| 170 |
return $attachments; |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Generate HTML for a rectangular, tiled Widget |
| 175 |
* |
| 176 |
* @param array $args Display arguments including before_title, after_title, before_widget, and after_widget. |
| 177 |
* @param array $instance The Widget instance to generate HTML for |
| 178 |
* @return string String of HTML representing a rectangular gallery |
| 179 |
*/ |
| 180 |
public function rectangular_widget( $args, $instance ) { |
| 181 |
if ( ! class_exists( 'Jetpack_Tiled_Gallery' ) |
| 182 |
&& ! class_exists( 'Jetpack_Tiled_Gallery_Layout_Rectangular' ) ) { |
| 183 |
return; |
| 184 |
} |
| 185 |
|
| 186 |
Jetpack_Tiled_Gallery::default_scripts_and_styles(); |
| 187 |
|
| 188 |
$layout = new Jetpack_Tiled_Gallery_Layout_Rectangular( $instance['attachments'], $instance['link'], false, 3 ); |
| 189 |
return $layout->HTML(); |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Generate HTML for a square (grid style) Widget |
| 194 |
* |
| 195 |
* @param array $args Display arguments including before_title, after_title, before_widget, and after_widget. |
| 196 |
* @param array $instance The Widget instance to generate HTML for |
| 197 |
* @return string String of HTML representing a square gallery |
| 198 |
*/ |
| 199 |
public function square_widget( $args, $instance ) { |
| 200 |
if ( ! class_exists( 'Jetpack_Tiled_Gallery' ) |
| 201 |
&& ! class_exists( 'Jetpack_Tiled_Gallery_Layout_Square' ) ) { |
| 202 |
return; |
| 203 |
} |
| 204 |
|
| 205 |
Jetpack_Tiled_Gallery::default_scripts_and_styles(); |
| 206 |
|
| 207 |
$layout = new Jetpack_Tiled_Gallery_Layout_Square( $instance['attachments'], $instance['link'], false, 3 ); |
| 208 |
return $layout->HTML(); |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Generate HTML for a circular (grid style) Widget |
| 213 |
* |
| 214 |
* @param array $args Display arguments including before_title, after_title, before_widget, and after_widget. |
| 215 |
* @param array $instance The Widget instance to generate HTML for |
| 216 |
* @return string String of HTML representing a circular gallery |
| 217 |
*/ |
| 218 |
public function circle_widget( $args, $instance ) { |
| 219 |
if ( ! class_exists( 'Jetpack_Tiled_Gallery' ) |
| 220 |
&& ! class_exists( 'Jetpack_Tiled_Gallery_Layout_Circle' ) ) { |
| 221 |
return; |
| 222 |
} |
| 223 |
|
| 224 |
Jetpack_Tiled_Gallery::default_scripts_and_styles(); |
| 225 |
|
| 226 |
$layout = new Jetpack_Tiled_Gallery_Layout_Circle( $instance['attachments'], $instance['link'], false, 3 ); |
| 227 |
return $layout->HTML(); |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Generate HTML for a slideshow Widget |
| 232 |
* |
| 233 |
* @param array $args Display arguments including before_title, after_title, before_widget, and after_widget. |
| 234 |
* @param array $instance The Widget instance to generate HTML for |
| 235 |
* @return string String of HTML representing a slideshow gallery |
| 236 |
*/ |
| 237 |
public function slideshow_widget( $args, $instance ) { |
| 238 |
global $content_width; |
| 239 |
|
| 240 |
require_once plugin_dir_path( realpath( dirname( __FILE__ ) . '/../shortcodes/slideshow.php' ) ) . 'slideshow.php'; |
| 241 |
|
| 242 |
if ( ! class_exists( 'Jetpack_Slideshow_Shortcode' ) ) { |
| 243 |
return; |
| 244 |
} |
| 245 |
|
| 246 |
if ( count( $instance['attachments'] ) < 1 ) { |
| 247 |
return; |
| 248 |
} |
| 249 |
|
| 250 |
$slideshow = new Jetpack_Slideshow_Shortcode(); |
| 251 |
|
| 252 |
$slideshow->enqueue_scripts(); |
| 253 |
|
| 254 |
$gallery_instance = 'widget-' . $args['widget_id']; |
| 255 |
|
| 256 |
$gallery = array(); |
| 257 |
|
| 258 |
foreach ( $instance['attachments'] as $attachment ) { |
| 259 |
$attachment_image_src = wp_get_attachment_image_src( $attachment->ID, 'full' ); |
| 260 |
$attachment_image_src = jetpack_photon_url( $attachment_image_src[0], array( 'w' => $this->_instance_width ) ); // [url, width, height] |
| 261 |
|
| 262 |
$caption = wptexturize( strip_tags( $attachment->post_excerpt ) ); |
| 263 |
|
| 264 |
$gallery[] = (object) array( |
| 265 |
'src' => (string) esc_url_raw( $attachment_image_src ), |
| 266 |
'id' => (string) $attachment->ID, |
| 267 |
'caption' => (string) $caption, |
| 268 |
); |
| 269 |
} |
| 270 |
|
| 271 |
$max_width = intval( get_option( 'large_size_w' ) ); |
| 272 |
$max_height = 175; |
| 273 |
|
| 274 |
if ( intval( $content_width ) > 0 ) { |
| 275 |
$max_width = min( intval( $content_width ), $max_width ); |
| 276 |
} |
| 277 |
|
| 278 |
$color = Jetpack_Options::get_option( 'slideshow_background_color', 'black' ); |
| 279 |
$autostart = isset( $attr['autostart'] ) ? $attr['autostart'] : true; |
| 280 |
|
| 281 |
$js_attr = array( |
| 282 |
'gallery' => $gallery, |
| 283 |
'selector' => $gallery_instance, |
| 284 |
'width' => $max_width, |
| 285 |
'height' => $max_height, |
| 286 |
'trans' => 'fade', |
| 287 |
'color' => $color, |
| 288 |
'autostart' => $autostart, |
| 289 |
); |
| 290 |
|
| 291 |
$html = $slideshow->slideshow_js( $js_attr ); |
| 292 |
|
| 293 |
return $html; |
| 294 |
} |
| 295 |
|
| 296 |
/** |
| 297 |
* tiled_gallery_content_width filter |
| 298 |
* |
| 299 |
* Used to adjust the content width of Jetpack_Tiled_Gallery's in sidebars |
| 300 |
* |
| 301 |
* $this->_instance_width is filtered in widget() and this filter is added then removed in widget() |
| 302 |
* |
| 303 |
* @param int $width int The original width value |
| 304 |
* @return int The filtered width |
| 305 |
*/ |
| 306 |
public function tiled_gallery_content_width( $width ) { |
| 307 |
return $this->_instance_width; |
| 308 |
} |
| 309 |
|
| 310 |
public function form( $instance ) { |
| 311 |
$defaults = $this->defaults(); |
| 312 |
$allowed_values = $this->allowed_values(); |
| 313 |
|
| 314 |
$instance = wp_parse_args( (array) $instance, $defaults ); |
| 315 |
|
| 316 |
include dirname( __FILE__ ) . '/gallery/templates/form.php'; |
| 317 |
} |
| 318 |
|
| 319 |
public function update( $new_instance, $old_instance ) { |
| 320 |
$instance = $this->sanitize( $new_instance ); |
| 321 |
|
| 322 |
return $instance; |
| 323 |
} |
| 324 |
|
| 325 |
/** |
| 326 |
* Sanitize the $instance's values to the set of allowed values. If a value is not acceptable, |
| 327 |
* it is set to its default. |
| 328 |
* |
| 329 |
* Helps keep things nice and secure by whitelisting only allowed values |
| 330 |
* |
| 331 |
* @param array $instance The Widget instance to sanitize values for |
| 332 |
* @return array $instance The Widget instance with values sanitized |
| 333 |
*/ |
| 334 |
public function sanitize( $instance ) { |
| 335 |
$allowed_values = $this->allowed_values(); |
| 336 |
$defaults = $this->defaults(); |
| 337 |
|
| 338 |
foreach ( $instance as $key => $value ) { |
| 339 |
$value = trim( $value ); |
| 340 |
|
| 341 |
if ( isset( $allowed_values[ $key ] ) && $allowed_values[ $key ] && ! array_key_exists( $value, $allowed_values[ $key ] ) ) { |
| 342 |
$instance[ $key ] = $defaults[ $key ]; |
| 343 |
} else { |
| 344 |
$instance[ $key ] = sanitize_text_field( $value ); |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
return $instance; |
| 349 |
} |
| 350 |
|
| 351 |
/** |
| 352 |
* Return a multi-dimensional array of allowed values (and their labels) for all widget form |
| 353 |
* elements |
| 354 |
* |
| 355 |
* To allow all values on an input, omit it from the returned array |
| 356 |
* |
| 357 |
* @return array Array of allowed values for each option |
| 358 |
*/ |
| 359 |
public function allowed_values() { |
| 360 |
$max_columns = 5; |
| 361 |
|
| 362 |
// Create an associative array of allowed column values. This just automates the generation of |
| 363 |
// column <option>s, from 1 to $max_columns |
| 364 |
$allowed_columns = array_combine( range( 1, $max_columns ), range( 1, $max_columns ) ); |
| 365 |
|
| 366 |
return array( |
| 367 |
'type' => array( |
| 368 |
'rectangular' => __( 'Tiles', 'jetpack' ), |
| 369 |
'square' => __( 'Square Tiles', 'jetpack' ), |
| 370 |
'circle' => __( 'Circles', 'jetpack' ), |
| 371 |
'slideshow' => __( 'Slideshow', 'jetpack' ), |
| 372 |
), |
| 373 |
'columns' => $allowed_columns, |
| 374 |
'link' => array( |
| 375 |
'carousel' => __( 'Carousel', 'jetpack' ), |
| 376 |
'post' => __( 'Attachment Page', 'jetpack' ), |
| 377 |
'file' => __( 'Media File', 'jetpack' ), |
| 378 |
), |
| 379 |
); |
| 380 |
} |
| 381 |
|
| 382 |
/** |
| 383 |
* Return an associative array of default values |
| 384 |
* |
| 385 |
* These values are used in new widgets as well as when sanitizing input. If a given value is not allowed, |
| 386 |
* as defined in allowed_values(), that input is set to the default value defined here. |
| 387 |
* |
| 388 |
* @return array Array of default values for the Widget's options |
| 389 |
*/ |
| 390 |
public function defaults() { |
| 391 |
return array( |
| 392 |
'title' => '', |
| 393 |
'type' => 'rectangular', |
| 394 |
'ids' => '', |
| 395 |
'columns' => 3, |
| 396 |
'link' => 'carousel', |
| 397 |
); |
| 398 |
} |
| 399 |
|
| 400 |
public function enqueue_frontend_scripts() { |
| 401 |
wp_register_script( |
| 402 |
'gallery-widget', |
| 403 |
Jetpack::get_file_url_for_environment( |
| 404 |
'_inc/build/widgets/gallery/js/gallery.min.js', |
| 405 |
'modules/widgets/gallery/js/gallery.js' |
| 406 |
) |
| 407 |
); |
| 408 |
|
| 409 |
wp_enqueue_script( 'gallery-widget' ); |
| 410 |
} |
| 411 |
|
| 412 |
public function enqueue_admin_scripts() { |
| 413 |
global $pagenow; |
| 414 |
|
| 415 |
if ( 'widgets.php' == $pagenow || 'customize.php' == $pagenow ) { |
| 416 |
wp_enqueue_media(); |
| 417 |
|
| 418 |
wp_enqueue_script( |
| 419 |
'gallery-widget-admin', |
| 420 |
Jetpack::get_file_url_for_environment( |
| 421 |
'_inc/build/widgets/gallery/js/admin.min.js', |
| 422 |
'modules/widgets/gallery/js/admin.js' |
| 423 |
), |
| 424 |
array( |
| 425 |
'media-models', |
| 426 |
'media-views', |
| 427 |
), |
| 428 |
'20150501' |
| 429 |
); |
| 430 |
|
| 431 |
$js_settings = array( |
| 432 |
'thumbSize' => self::THUMB_SIZE, |
| 433 |
); |
| 434 |
|
| 435 |
wp_localize_script( 'gallery-widget-admin', '_wpGalleryWidgetAdminSettings', $js_settings ); |
| 436 |
wp_enqueue_style( 'gallery-widget-admin', plugins_url( '/gallery/css/admin.css', __FILE__ ) ); |
| 437 |
wp_style_add_data( 'gallery-widget-admin', 'rtl', 'replace' ); |
| 438 |
} |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
add_action( 'widgets_init', 'jetpack_gallery_widget_init' ); |
| 443 |
|
| 444 |
function jetpack_gallery_widget_init() { |
| 445 |
/** |
| 446 |
* Allow the Gallery Widget to be enabled even when Core supports the Media Gallery Widget |
| 447 |
* |
| 448 |
* @module widgets |
| 449 |
* |
| 450 |
* @since 5.5.0 |
| 451 |
* |
| 452 |
* @param bool false Whether to force-enable the gallery widget |
| 453 |
*/ |
| 454 |
if ( |
| 455 |
! apply_filters( 'jetpack_force_enable_gallery_widget', false ) |
| 456 |
&& class_exists( 'WP_Widget_Media_Gallery' ) |
| 457 |
&& Jetpack_Options::get_option( 'gallery_widget_migration' ) |
| 458 |
) { |
| 459 |
return; |
| 460 |
} |
| 461 |
if ( ! method_exists( 'Jetpack', 'is_module_active' ) || Jetpack::is_module_active( 'tiled-gallery' ) ) { |
| 462 |
register_widget( 'Jetpack_Gallery_Widget' ); |
| 463 |
} |
| 464 |
} |
| 465 |
|