暫無描述

gallery.php 15KB

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