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