Jetpack_Gallery_Widget

Plugin Name: Gallery Description: Gallery widget Author: Automattic Inc.

Defined (1)

The class is defined in the following location(s).

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