tiled-gallery.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. // Include the class file containing methods for rounding constrained array elements.
  3. // Here the constrained array element is the dimension of a row, group or an image in the tiled gallery.
  4. include_once dirname( __FILE__ ) . '/math/class-constrained-array-rounding.php';
  5. // Layouts
  6. include_once dirname( __FILE__ ) . '/tiled-gallery/tiled-gallery-rectangular.php';
  7. include_once dirname( __FILE__ ) . '/tiled-gallery/tiled-gallery-square.php';
  8. include_once dirname( __FILE__ ) . '/tiled-gallery/tiled-gallery-circle.php';
  9. class Jetpack_Tiled_Gallery {
  10. private static $talaveras = array( 'rectangular', 'square', 'circle', 'rectangle', 'columns' );
  11. public function __construct() {
  12. add_action( 'admin_init', array( $this, 'settings_api_init' ) );
  13. add_filter( 'jetpack_gallery_types', array( $this, 'jetpack_gallery_types' ), 9 );
  14. add_filter( 'jetpack_default_gallery_type', array( $this, 'jetpack_default_gallery_type' ) );
  15. }
  16. public function tiles_enabled() {
  17. // Check the setting status
  18. return '' != Jetpack_Options::get_option_and_ensure_autoload( 'tiled_galleries', '' );
  19. }
  20. public function set_atts( $atts ) {
  21. global $post;
  22. $this->atts = shortcode_atts( array(
  23. 'order' => 'ASC',
  24. 'orderby' => 'menu_order ID',
  25. 'id' => isset( $post->ID ) ? $post->ID : 0,
  26. 'include' => '',
  27. 'exclude' => '',
  28. 'type' => '',
  29. 'grayscale' => false,
  30. 'link' => '',
  31. 'columns' => 3
  32. ), $atts, 'gallery' );
  33. $this->atts['id'] = (int) $this->atts['id'];
  34. $this->float = is_rtl() ? 'right' : 'left';
  35. // Default to rectangular is tiled galleries are checked
  36. if ( $this->tiles_enabled() && ( ! $this->atts['type'] || 'default' == $this->atts['type'] ) ) {
  37. /** This filter is already documented in functions.gallery.php */
  38. $this->atts['type'] = apply_filters( 'jetpack_default_gallery_type', 'rectangular' );
  39. }
  40. if ( !$this->atts['orderby'] ) {
  41. $this->atts['orderby'] = sanitize_sql_orderby( $this->atts['orderby'] );
  42. if ( !$this->atts['orderby'] )
  43. $this->atts['orderby'] = 'menu_order ID';
  44. }
  45. if ( 'rand' == strtolower( $this->atts['order'] ) ) {
  46. $this->atts['orderby'] = 'rand';
  47. }
  48. // We shouldn't have more than 20 columns.
  49. if ( ! is_numeric( $this->atts['columns'] ) || 20 < $this->atts['columns'] ) {
  50. $this->atts['columns'] = 3;
  51. }
  52. }
  53. public function get_attachments() {
  54. extract( $this->atts );
  55. if ( !empty( $include ) ) {
  56. $include = preg_replace( '/[^0-9,]+/', '', $include );
  57. $_attachments = get_posts( array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby, 'suppress_filters' => false) );
  58. $attachments = array();
  59. foreach ( $_attachments as $key => $val ) {
  60. $attachments[$val->ID] = $_attachments[$key];
  61. }
  62. } elseif ( 0 == $id ) {
  63. // Should NEVER Happen but infinite_scroll_load_other_plugins_scripts means it does
  64. // Querying with post_parent == 0 can generate stupidly memcache sets on sites with 10000's of unattached attachments as get_children puts every post in the cache.
  65. // TODO Fix this properly
  66. $attachments = array();
  67. } elseif ( !empty( $exclude ) ) {
  68. $exclude = preg_replace( '/[^0-9,]+/', '', $exclude );
  69. $attachments = get_children( array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby, 'suppress_filters' => false) );
  70. } else {
  71. $attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby, 'suppress_filters' => false ) );
  72. }
  73. return $attachments;
  74. }
  75. public static function default_scripts_and_styles() {
  76. wp_enqueue_script(
  77. 'tiled-gallery',
  78. Jetpack::get_file_url_for_environment(
  79. '_inc/build/tiled-gallery/tiled-gallery/tiled-gallery.min.js',
  80. 'modules/tiled-gallery/tiled-gallery/tiled-gallery.js'
  81. ),
  82. array( 'jquery' )
  83. );
  84. wp_enqueue_style( 'tiled-gallery', plugins_url( 'tiled-gallery/tiled-gallery.css', __FILE__ ), array(), '2012-09-21' );
  85. wp_style_add_data( 'tiled-gallery', 'rtl', 'replace' );
  86. }
  87. public function gallery_shortcode( $val, $atts ) {
  88. if ( ! empty( $val ) ) // something else is overriding post_gallery, like a custom VIP shortcode
  89. return $val;
  90. global $post;
  91. $this->set_atts( $atts );
  92. $attachments = $this->get_attachments();
  93. if ( empty( $attachments ) )
  94. return '';
  95. if ( is_feed() || defined( 'IS_HTML_EMAIL' ) ) {
  96. return '';
  97. }
  98. if (
  99. in_array(
  100. $this->atts['type'],
  101. /**
  102. * Filters the permissible Tiled Gallery types.
  103. *
  104. * @module tiled-gallery
  105. *
  106. * @since 3.7.0
  107. *
  108. * @param array Array of allowed types. Default: 'rectangular', 'square', 'circle', 'rectangle', 'columns'.
  109. */
  110. $talaveras = apply_filters( 'jetpack_tiled_gallery_types', self::$talaveras )
  111. )
  112. ) {
  113. // Enqueue styles and scripts
  114. self::default_scripts_and_styles();
  115. // Generate gallery HTML
  116. $gallery_class = 'Jetpack_Tiled_Gallery_Layout_' . ucfirst( $this->atts['type'] );
  117. $gallery = new $gallery_class( $attachments, $this->atts['link'], $this->atts['grayscale'], (int) $this->atts['columns'] );
  118. $gallery_html = $gallery->HTML();
  119. if ( $gallery_html && class_exists( 'Jetpack' ) && class_exists( 'Jetpack_Photon' ) ) {
  120. // Tiled Galleries in Jetpack require that Photon be active.
  121. // If it's not active, run it just on the gallery output.
  122. if ( ! in_array( 'photon', Jetpack::get_active_modules() ) && ! Jetpack::is_development_mode() )
  123. $gallery_html = Jetpack_Photon::filter_the_content( $gallery_html );
  124. }
  125. return trim( preg_replace( '/\s+/', ' ', $gallery_html ) ); // remove any new lines from the output so that the reader parses it better
  126. }
  127. return '';
  128. }
  129. public static function gallery_already_redefined() {
  130. global $shortcode_tags;
  131. $redefined = false;
  132. if ( ! isset( $shortcode_tags[ 'gallery' ] ) || $shortcode_tags[ 'gallery' ] !== 'gallery_shortcode' ) {
  133. $redefined = true;
  134. }
  135. /**
  136. * Filter the output of the check for another plugin or theme affecting WordPress galleries.
  137. *
  138. * This will let folks that replace core’s shortcode confirm feature parity with it, so Jetpack's Tiled Galleries can still work.
  139. *
  140. * @module tiled-gallery
  141. *
  142. * @since 3.1.0
  143. *
  144. * @param bool $redefined Does another plugin or theme already redefines the default WordPress gallery?
  145. */
  146. return apply_filters( 'jetpack_tiled_gallery_shortcode_redefined', $redefined );
  147. }
  148. public static function init() {
  149. if ( self::gallery_already_redefined() )
  150. return;
  151. $gallery = new Jetpack_Tiled_Gallery;
  152. add_filter( 'post_gallery', array( $gallery, 'gallery_shortcode' ), 1001, 2 );
  153. }
  154. public static function get_content_width() {
  155. $tiled_gallery_content_width = Jetpack::get_content_width();
  156. if ( ! $tiled_gallery_content_width )
  157. $tiled_gallery_content_width = 500;
  158. /**
  159. * Filter overwriting the default content width.
  160. *
  161. * @module tiled-gallery
  162. *
  163. * @since 2.1.0
  164. *
  165. * @param string $tiled_gallery_content_width Default Tiled Gallery content width.
  166. */
  167. return apply_filters( 'tiled_gallery_content_width', $tiled_gallery_content_width );
  168. }
  169. /**
  170. * Media UI integration
  171. */
  172. function jetpack_gallery_types( $types ) {
  173. if ( get_option( 'tiled_galleries' ) && isset( $types['default'] ) ) {
  174. // Tiled is set as the default, meaning that type='default'
  175. // will still display the mosaic.
  176. $types['thumbnails'] = $types['default'];
  177. unset( $types['default'] );
  178. }
  179. $types['rectangular'] = __( 'Tiled Mosaic', 'jetpack' );
  180. $types['square'] = __( 'Square Tiles', 'jetpack' );
  181. $types['circle'] = __( 'Circles', 'jetpack' );
  182. $types['columns'] = __( 'Tiled Columns', 'jetpack' );
  183. return $types;
  184. }
  185. function jetpack_default_gallery_type() {
  186. return ( get_option( 'tiled_galleries' ) ? 'rectangular' : 'default' );
  187. }
  188. static function get_talaveras() {
  189. return self::$talaveras;
  190. }
  191. /**
  192. * Add a checkbox field to the Carousel section in Settings > Media
  193. * for setting tiled galleries as the default.
  194. */
  195. function settings_api_init() {
  196. global $wp_settings_sections;
  197. // Add the setting field [tiled_galleries] and place it in Settings > Media
  198. if ( isset( $wp_settings_sections['media']['carousel_section'] ) )
  199. $section = 'carousel_section';
  200. else
  201. $section = 'default';
  202. add_settings_field( 'tiled_galleries', __( 'Tiled Galleries', 'jetpack' ), array( $this, 'setting_html' ), 'media', $section );
  203. register_setting( 'media', 'tiled_galleries', 'esc_attr' );
  204. }
  205. function setting_html() {
  206. echo '<label><input name="tiled_galleries" type="checkbox" value="1" ' .
  207. checked( 1, '' != get_option( 'tiled_galleries' ), false ) . ' /> ' .
  208. __( 'Display all your gallery pictures in a cool mosaic.', 'jetpack' ) . '</br></label>';
  209. }
  210. }
  211. add_action( 'init', array( 'Jetpack_Tiled_Gallery', 'init' ) );