gallery.php 14 KB

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