PageRenderTime 26ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/jetpack/modules/widgets/gallery.php

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