Automattic /
jetpack
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 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 | $control_ops = array( 'width' => 250 ); |
||
| 24 | |||
| 25 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) ); |
||
| 26 | |||
| 27 | parent::__construct( |
||
| 28 | 'gallery', |
||
| 29 | /** This filter is documented in modules/widgets/facebook-likebox.php */ |
||
| 30 | apply_filters( 'jetpack_widget_name', __( 'Gallery', 'jetpack' ) ), |
||
| 31 | $widget_ops, |
||
| 32 | $control_ops |
||
| 33 | ); |
||
| 34 | |||
| 35 | if ( is_customize_preview() ) { |
||
| 36 | add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend_scripts' ) ); |
||
| 37 | |||
| 38 | if ( class_exists( 'Jetpack_Tiled_Gallery' ) ) { |
||
| 39 | $tiled_gallery = new Jetpack_Tiled_Gallery(); |
||
| 40 | add_action( 'wp_enqueue_scripts', array( $tiled_gallery, 'default_scripts_and_styles' ) ); |
||
| 41 | } |
||
| 42 | |||
| 43 | if ( class_exists( 'Jetpack_Slideshow_Shortcode' ) ) { |
||
| 44 | $slideshow = new Jetpack_Slideshow_Shortcode(); |
||
| 45 | add_action( 'wp_enqueue_scripts', array( $slideshow, 'enqueue_scripts' ) ); |
||
| 46 | } |
||
| 47 | |||
| 48 | if ( class_exists( 'Jetpack_Carousel' ) ) { |
||
| 49 | $carousel = new Jetpack_Carousel(); |
||
| 50 | add_action( 'wp_enqueue_scripts', array( $carousel, 'enqueue_assets' ) ); |
||
| 51 | } |
||
| 52 | } |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @param array $args Display arguments including before_title, after_title, before_widget, and after_widget. |
||
| 57 | * @param array $instance The settings for the particular instance of the widget. |
||
| 58 | */ |
||
| 59 | public function widget( $args, $instance ) { |
||
| 60 | $instance = wp_parse_args( (array) $instance, $this->defaults() ); |
||
| 61 | |||
| 62 | $this->enqueue_frontend_scripts(); |
||
| 63 | |||
| 64 | extract( $args ); |
||
| 65 | |||
| 66 | $instance['attachments'] = $this->get_attachments( $instance ); |
||
| 67 | |||
| 68 | $classes = array(); |
||
| 69 | |||
| 70 | $classes[] = 'widget-gallery-' . $instance['type']; |
||
| 71 | |||
| 72 | // Due to a bug in the carousel plugin, carousels will be triggered for all tiled galleries that exist on a page |
||
| 73 | // with other tiled galleries, regardless of whether or not the widget was set to Carousel mode. The onClick selector |
||
| 74 | // is simply too broad, since it was not written with widgets in mind. This special class prevents that behavior, via |
||
| 75 | // an override handler in gallery.js |
||
| 76 | if( 'carousel' != $instance['link'] && 'slideshow' != $instance['type'] ) |
||
| 77 | $classes[] = 'no-carousel'; |
||
| 78 | else |
||
| 79 | $classes[] = 'carousel'; |
||
| 80 | |||
| 81 | $classes = implode( ' ', $classes ); |
||
| 82 | |||
| 83 | if ( 'carousel' == $instance['link'] ) { |
||
| 84 | require_once plugin_dir_path( realpath( dirname( __FILE__ ) . '/../carousel/jetpack-carousel.php' ) ) . 'jetpack-carousel.php'; |
||
| 85 | |||
| 86 | if ( class_exists( 'Jetpack_Carousel' ) ) { |
||
| 87 | // Create new carousel so we can use the enqueue_assets() method. Not ideal, but there is a decent amount |
||
| 88 | // of logic in that method that shouldn't be duplicated. |
||
| 89 | $carousel = new Jetpack_Carousel(); |
||
| 90 | |||
| 91 | // First parameter is $output, which comes from filters, and causes bypass of the asset enqueuing. Passing null is correct. |
||
| 92 | $carousel->enqueue_assets( null ); |
||
|
0 ignored issues
–
show
|
|||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | echo $before_widget . "\n"; |
||
| 97 | |||
| 98 | /** This filter is documented in core/src/wp-includes/default-widgets.php */ |
||
| 99 | $title = apply_filters( 'widget_title', $instance['title'] ); |
||
| 100 | |||
| 101 | if ( $title ) |
||
| 102 | echo $before_title . esc_html( $title ) . $after_title . "\n"; |
||
| 103 | |||
| 104 | echo '<div class="' . esc_attr( $classes ) . '">' . "\n"; |
||
| 105 | |||
| 106 | $method = $instance['type'] . '_widget'; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Allow the width of a gallery to be altered by themes or other code. |
||
| 110 | * |
||
| 111 | * @module widgets |
||
| 112 | * |
||
| 113 | * @since 2.5.0 |
||
| 114 | * |
||
| 115 | * @param int self::DEFAULT_WIDTH Default gallery width. Default is 265. |
||
| 116 | * @param string $args Display arguments including before_title, after_title, before_widget, and after_widget. |
||
| 117 | * @param array $instance The settings for the particular instance of the widget. |
||
| 118 | */ |
||
| 119 | $this->_instance_width = apply_filters( 'gallery_widget_content_width', self::DEFAULT_WIDTH, $args, $instance ); |
||
| 120 | |||
| 121 | // Register a filter to modify the tiled_gallery_content_width, so Jetpack_Tiled_Gallery |
||
| 122 | // can appropriately size the tiles. |
||
| 123 | add_filter( 'tiled_gallery_content_width', array( $this, 'tiled_gallery_content_width' ) ); |
||
| 124 | |||
| 125 | if ( method_exists( $this, $method ) ) |
||
| 126 | echo $this->$method( $args, $instance ); |
||
| 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( array( |
||
| 156 | 'post__in' => $ids, |
||
| 157 | 'post_status' => 'inherit', |
||
| 158 | 'post_type' => 'attachment', |
||
| 159 | 'post_mime_type' => 'image', |
||
| 160 | 'posts_per_page' => -1, |
||
| 161 | 'orderby' => 'post__in', |
||
| 162 | ) ); |
||
| 163 | |||
| 164 | $attachments = $attachments_query->get_posts(); |
||
| 165 | |||
| 166 | wp_reset_postdata(); |
||
| 167 | |||
| 168 | return $attachments; |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Generate HTML for a rectangular, tiled Widget |
||
| 173 | * |
||
| 174 | * @param array $args Display arguments including before_title, after_title, before_widget, and after_widget. |
||
| 175 | * @param array $instance The Widget instance to generate HTML for |
||
| 176 | * @return string String of HTML representing a rectangular gallery |
||
| 177 | */ |
||
| 178 | View Code Duplication | public function rectangular_widget( $args, $instance ) { |
|
| 179 | if ( ! class_exists( 'Jetpack_Tiled_Gallery' ) |
||
| 180 | && ! class_exists( 'Jetpack_Tiled_Gallery_Layout_Rectangular') ) { |
||
| 181 | return; |
||
| 182 | } |
||
| 183 | |||
| 184 | $widget_tiled_gallery = new Jetpack_Tiled_Gallery(); |
||
| 185 | $widget_tiled_gallery->default_scripts_and_styles(); |
||
| 186 | |||
| 187 | $layout = new Jetpack_Tiled_Gallery_Layout_Rectangular( $instance['attachments'], $instance['link'], false, 3 ); |
||
| 188 | return $layout->HTML(); |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Generate HTML for a square (grid style) Widget |
||
| 193 | * |
||
| 194 | * @param array $args Display arguments including before_title, after_title, before_widget, and after_widget. |
||
| 195 | * @param array $instance The Widget instance to generate HTML for |
||
| 196 | * @return string String of HTML representing a square gallery |
||
| 197 | */ |
||
| 198 | View Code Duplication | public function square_widget( $args, $instance ) { |
|
| 199 | if ( ! class_exists( 'Jetpack_Tiled_Gallery' ) |
||
| 200 | && ! class_exists( 'Jetpack_Tiled_Gallery_Layout_Square') ) { |
||
| 201 | return; |
||
| 202 | } |
||
| 203 | |||
| 204 | $widget_tiled_gallery = new Jetpack_Tiled_Gallery(); |
||
| 205 | $widget_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 | View Code Duplication | 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 | $widget_tiled_gallery = new Jetpack_Tiled_Gallery(); |
||
| 225 | $widget_tiled_gallery->default_scripts_and_styles(); |
||
| 226 | |||
| 227 | $layout = new Jetpack_Tiled_Gallery_Layout_Circle( $instance['attachments'], $instance['link'], false, 3 ); |
||
| 228 | return $layout->HTML(); |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Generate HTML for a slideshow Widget |
||
| 233 | * |
||
| 234 | * @param array $args Display arguments including before_title, after_title, before_widget, and after_widget. |
||
| 235 | * @param array $instance The Widget instance to generate HTML for |
||
| 236 | * @return string String of HTML representing a slideshow gallery |
||
| 237 | */ |
||
| 238 | public function slideshow_widget( $args, $instance ) { |
||
| 239 | global $content_width; |
||
| 240 | |||
| 241 | require_once plugin_dir_path( realpath( dirname( __FILE__ ) . '/../shortcodes/slideshow.php' ) ) . 'slideshow.php'; |
||
| 242 | |||
| 243 | if ( ! class_exists( 'Jetpack_Slideshow_Shortcode' ) ) |
||
| 244 | return; |
||
| 245 | |||
| 246 | if ( count( $instance['attachments'] ) < 1 ) |
||
| 247 | return; |
||
| 248 | |||
| 249 | $slideshow = new Jetpack_Slideshow_Shortcode(); |
||
| 250 | |||
| 251 | $slideshow->enqueue_scripts(); |
||
| 252 | |||
| 253 | $gallery_instance = "widget-" . $args['widget_id']; |
||
| 254 | |||
| 255 | $gallery = array(); |
||
| 256 | |||
| 257 | foreach ( $instance['attachments'] as $attachment ) { |
||
| 258 | $attachment_image_src = wp_get_attachment_image_src( $attachment->ID, 'full' ); |
||
| 259 | $attachment_image_src = jetpack_photon_url( $attachment_image_src[0], array( 'w' => $this->_instance_width ) ); // [url, width, height] |
||
| 260 | |||
| 261 | $caption = wptexturize( strip_tags( $attachment->post_excerpt ) ); |
||
| 262 | |||
| 263 | $gallery[] = (object) array( |
||
| 264 | 'src' => (string) esc_url_raw( $attachment_image_src ), |
||
| 265 | 'id' => (string) $attachment->ID, |
||
| 266 | 'caption' => (string) $caption, |
||
| 267 | ); |
||
| 268 | } |
||
| 269 | |||
| 270 | $max_width = intval( get_option( 'large_size_w' ) ); |
||
| 271 | $max_height = 175; |
||
| 272 | |||
| 273 | if ( intval( $content_width ) > 0 ) |
||
| 274 | $max_width = min( intval( $content_width ), $max_width ); |
||
| 275 | |||
| 276 | $color = Jetpack_Options::get_option( 'slideshow_background_color', 'black' ); |
||
| 277 | $autostart = isset( $attr['autostart'] ) ? $attr['autostart'] : true; |
||
| 278 | |||
| 279 | $js_attr = array( |
||
| 280 | 'gallery' => $gallery, |
||
| 281 | 'selector' => $gallery_instance, |
||
| 282 | 'width' => $max_width, |
||
| 283 | 'height' => $max_height, |
||
| 284 | 'trans' => 'fade', |
||
| 285 | 'color' => $color, |
||
| 286 | 'autostart' => $autostart, |
||
| 287 | ); |
||
| 288 | |||
| 289 | $html = $slideshow->slideshow_js( $js_attr ); |
||
| 290 | |||
| 291 | return $html; |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * tiled_gallery_content_width filter |
||
| 296 | * |
||
| 297 | * Used to adjust the content width of Jetpack_Tiled_Gallery's in sidebars |
||
| 298 | * |
||
| 299 | * $this->_instance_width is filtered in widget() and this filter is added then removed in widget() |
||
| 300 | * |
||
| 301 | * @param int $width int The original width value |
||
| 302 | * @return int The filtered width |
||
| 303 | */ |
||
| 304 | public function tiled_gallery_content_width( $width ) { |
||
| 305 | return $this->_instance_width; |
||
| 306 | } |
||
| 307 | |||
| 308 | public function form( $instance ) { |
||
| 309 | $defaults = $this->defaults(); |
||
| 310 | $allowed_values = $this->allowed_values(); |
||
| 311 | |||
| 312 | $instance = wp_parse_args( (array) $instance, $defaults ); |
||
| 313 | |||
| 314 | include dirname( __FILE__ ) . '/gallery/templates/form.php'; |
||
| 315 | } |
||
| 316 | |||
| 317 | public function update( $new_instance, $old_instance ) { |
||
| 318 | $instance = $this->sanitize( $new_instance ); |
||
| 319 | |||
| 320 | return $instance; |
||
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Sanitize the $instance's values to the set of allowed values. If a value is not acceptable, |
||
| 325 | * it is set to its default. |
||
| 326 | * |
||
| 327 | * Helps keep things nice and secure by whitelisting only allowed values |
||
| 328 | * |
||
| 329 | * @param array $instance The Widget instance to sanitize values for |
||
| 330 | * @return array $instance The Widget instance with values sanitized |
||
| 331 | */ |
||
| 332 | public function sanitize( $instance ) { |
||
| 333 | $allowed_values = $this->allowed_values(); |
||
| 334 | $defaults = $this->defaults(); |
||
| 335 | |||
| 336 | foreach ( $instance as $key => $value ) { |
||
| 337 | $value = trim( $value ); |
||
| 338 | |||
| 339 | if ( isset( $allowed_values[ $key ] ) && $allowed_values[ $key ] && ! array_key_exists( $value, $allowed_values[ $key ] ) ) { |
||
| 340 | $instance[ $key ] = $defaults[ $key ]; |
||
| 341 | } else { |
||
| 342 | $instance[ $key ] = sanitize_text_field( $value ); |
||
| 343 | } |
||
| 344 | } |
||
| 345 | |||
| 346 | return $instance; |
||
| 347 | } |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Return a multi-dimensional array of allowed values (and their labels) for all widget form |
||
| 351 | * elements |
||
| 352 | * |
||
| 353 | * To allow all values on an input, omit it from the returned array |
||
| 354 | * |
||
| 355 | * @return array Array of allowed values for each option |
||
| 356 | */ |
||
| 357 | public function allowed_values() { |
||
| 358 | $max_columns = 5; |
||
| 359 | |||
| 360 | // Create an associative array of allowed column values. This just automates the generation of |
||
| 361 | // column <option>s, from 1 to $max_columns |
||
| 362 | $allowed_columns = array_combine( range( 1, $max_columns ), range( 1, $max_columns ) ); |
||
| 363 | |||
| 364 | return array( |
||
| 365 | 'type' => array( |
||
| 366 | 'rectangular' => __( 'Tiles', 'jetpack' ), |
||
| 367 | 'square' => __( 'Square Tiles', 'jetpack' ), |
||
| 368 | 'circle' => __( 'Circles', 'jetpack' ), |
||
| 369 | 'slideshow' => __( 'Slideshow', 'jetpack' ), |
||
| 370 | ), |
||
| 371 | 'columns' => $allowed_columns, |
||
| 372 | 'link' => array( |
||
| 373 | 'carousel' => __( 'Carousel', 'jetpack' ), |
||
| 374 | 'post' => __( 'Attachment Page', 'jetpack' ), |
||
| 375 | 'file' => __( 'Media File', 'jetpack' ), |
||
| 376 | ) |
||
| 377 | ); |
||
| 378 | } |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Return an associative array of default values |
||
| 382 | * |
||
| 383 | * These values are used in new widgets as well as when sanitizing input. If a given value is not allowed, |
||
| 384 | * as defined in allowed_values(), that input is set to the default value defined here. |
||
| 385 | * |
||
| 386 | * @return array Array of default values for the Widget's options |
||
| 387 | */ |
||
| 388 | public function defaults() { |
||
| 389 | return array( |
||
| 390 | 'title' => '', |
||
| 391 | 'type' => 'rectangular', |
||
| 392 | 'ids' => '', |
||
| 393 | 'columns' => 3, |
||
| 394 | 'link' => 'carousel' |
||
| 395 | ); |
||
| 396 | } |
||
| 397 | |||
| 398 | public function enqueue_frontend_scripts() { |
||
| 399 | wp_register_script( 'gallery-widget', plugins_url( '/gallery/js/gallery.js', __FILE__ ) ); |
||
| 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( 'gallery-widget-admin', plugins_url( '/gallery/js/admin.js', __FILE__ ), array( |
||
| 411 | 'media-models', |
||
| 412 | 'media-views' |
||
| 413 | ), |
||
| 414 | '20150501' |
||
| 415 | ); |
||
| 416 | |||
| 417 | $js_settings = array( |
||
| 418 | 'thumbSize' => self::THUMB_SIZE |
||
| 419 | ); |
||
| 420 | |||
| 421 | wp_localize_script( 'gallery-widget-admin', '_wpGalleryWidgetAdminSettings', $js_settings ); |
||
| 422 | if( is_rtl() ) { |
||
| 423 | wp_enqueue_style( 'gallery-widget-admin', plugins_url( '/gallery/css/rtl/admin-rtl.css', __FILE__ ) ); |
||
| 424 | } else { |
||
| 425 | wp_enqueue_style( 'gallery-widget-admin', plugins_url( '/gallery/css/admin.css', __FILE__ ) ); |
||
| 426 | } |
||
| 427 | } |
||
| 428 | } |
||
| 429 | } |
||
| 430 | |||
| 431 | add_action( 'widgets_init', 'jetpack_gallery_widget_init' ); |
||
| 432 | |||
| 433 | function jetpack_gallery_widget_init() { |
||
| 434 | if ( ! method_exists( 'Jetpack', 'is_module_active' ) || Jetpack::is_module_active( 'tiled-gallery' ) ) |
||
| 435 | register_widget( 'Jetpack_Gallery_Widget' ); |
||
| 436 | } |
||
| 437 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignorePhpDoc annotation to the duplicate definition and it will be ignored.