Jetpack_Tiled_Gallery

The Jetpack by WordPress.com Jetpack Tiled Gallery class.

Defined (1)

The class is defined in the following location(s).

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