PageRenderTime 24ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

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

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