gallery.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. class Vamtam_Gallery {
  3. public function __construct() {
  4. add_shortcode( 'vamtam_gallery', array( __CLASS__, 'gallery' ) );
  5. add_shortcode( 'vamtam_gallery_lightbox', array( __CLASS__, 'gallery_lightbox' ) );
  6. }
  7. public static function gallery( $attr ) {
  8. // Allow plugins/child themes to override the default gallery template.
  9. $output = apply_filters( 'vamtam_post_gallery', '', $attr );
  10. if ( '' != $output ) {
  11. return $output;
  12. }
  13. $attachments = self::get_attachments( $attr );
  14. if ( empty( $attachments ) )
  15. return '';
  16. if ( is_feed() ) {
  17. $output = "\n";
  18. foreach ( $attachments as $att_id => $attachment )
  19. $output .= wp_get_attachment_link( $att_id, $size, true ) . "\n";
  20. return $output;
  21. }
  22. $inside_cube = isset( $GLOBALS['vamtam_inside_cube'] ) && $GLOBALS['vamtam_inside_cube'];
  23. if ( $inside_cube ) {
  24. $output .= '<div class="cbp-slider-inline">';
  25. $output .= '<div class="cbp-slider-wrapper">';
  26. VamtamOverrides::unlimited_image_sizes(); // check for matching VamtamOverrides::limit_image_sizes(); below
  27. } else {
  28. if ( VamtamTemplates::early_cube_load() ) {
  29. wp_enqueue_script( 'cubeportfolio' );
  30. }
  31. wp_enqueue_style( 'cubeportfolio' );
  32. $slider_options = array(
  33. 'layoutMode' => 'slider',
  34. 'drag' => true,
  35. 'auto' => false,
  36. 'autoTimeout' => 5000,
  37. 'autoPauseOnHover' => true,
  38. 'showNavigation' => true,
  39. 'showPagination' => true,
  40. 'rewindNav' => true,
  41. 'gridAdjustment' => 'responsive',
  42. 'mediaQueries' => array(
  43. array(
  44. 'width' => 1,
  45. 'cols' => 1,
  46. ),
  47. ),
  48. 'gapHorizontal' => 0,
  49. 'gapVertical' => 0,
  50. 'caption' => '',
  51. 'displayType' => 'default',
  52. );
  53. $output .= '<div class="vamtam-cubeportfolio cbp cbp-slider-edge" data-options="' . esc_attr( json_encode( $slider_options ) ) . '">';
  54. }
  55. foreach ( $attachments as $id => $attachment ) {
  56. $image = wp_get_attachment_image( $id, $attr['size'] );
  57. if ( ! empty( $image ) ) {
  58. if ( $inside_cube ) {
  59. $output .= '<div class="cbp-slider-item cbp-slider-item--active">';
  60. $output .= '<a href="' . esc_url( get_permalink() ) . '" title="' . the_title_attribute( array(
  61. 'echo' => false,
  62. ) ) . '">';
  63. $output .= $image;
  64. $output .= '</a>';
  65. $output .= '</div>';
  66. } else {
  67. $output .= '<div class="cbp-item">';
  68. $output .= $image;
  69. $output .= '</div>';
  70. }
  71. }
  72. }
  73. if ( $inside_cube ) {
  74. VamtamOverrides::limit_image_sizes();
  75. $output .= '</div>
  76. <div class="cbp-slider-controls">
  77. <div class="cbp-slider-prev"></div>
  78. <div class="cbp-slider-next"></div>
  79. </div>
  80. </div>';
  81. } else {
  82. $output .= '</div>';
  83. }
  84. return $output;
  85. }
  86. public static function gallery_lightbox( $attr ) {
  87. // Allow plugins/child themes to override the default gallery template.
  88. $output = apply_filters( 'vamtam_post_gallery_lightbox', '', $attr );
  89. if ( '' != $output ) {
  90. return $output;
  91. }
  92. $attachments = self::get_attachments( $attr );
  93. if ( empty( $attachments ) )
  94. return '';
  95. if ( is_feed() ) {
  96. return '';
  97. }
  98. foreach ( $attachments as $id => $attachment ) {
  99. $image_src = wp_get_attachment_image_src( $id, 'full' );
  100. if ( ! empty( $image_src ) && ! empty( $image_src[0] ) ) {
  101. $output .= '<a href="' . esc_url( $image_src[0] ) . '" title="' . esc_attr__( 'View Gallery Item', 'wpv' ) . '" class="cbp-lightbox vamtam-lightbox-gallery" data-title="' . esc_attr( get_the_title() ) . '"></a>';
  102. }
  103. }
  104. return $output;
  105. }
  106. public static function get_attachments( $attr ) {
  107. extract( self::process_atts( $attr ) );
  108. $id = intval( $id );
  109. if ( 'RAND' == $order ) {
  110. $orderby = 'none';
  111. }
  112. if ( ! empty( $include ) ) {
  113. $_attachments = get_posts( array(
  114. 'include' => $include,
  115. 'post_status' => 'inherit',
  116. 'post_type' => 'attachment',
  117. 'post_mime_type' => 'image',
  118. 'order' => $order,
  119. 'orderby' => $orderby,
  120. ) );
  121. $attachments = array();
  122. foreach ( $_attachments as $key => $val ) {
  123. $attachments[ $val->ID ] = $_attachments[ $key ];
  124. }
  125. } elseif ( ! empty( $exclude ) ) {
  126. $attachments = get_children( array(
  127. 'post_parent' => $id,
  128. 'exclude' => $exclude,
  129. 'post_status' => 'inherit',
  130. 'post_type' => 'attachment',
  131. 'post_mime_type' => 'image',
  132. 'order' => $order,
  133. 'orderby' => $orderby,
  134. ) );
  135. } else {
  136. $attachments = get_children( array(
  137. 'post_parent' => $id,
  138. 'post_status' => 'inherit',
  139. 'post_type' => 'attachment',
  140. 'post_mime_type' => 'image',
  141. 'order' => $order,
  142. 'orderby' => $orderby,
  143. ) );
  144. }
  145. return $attachments;
  146. }
  147. public static function process_atts( $attr ) {
  148. $post = get_post();
  149. if ( ! empty( $attr['ids'] ) ) {
  150. // 'ids' is explicitly ordered, unless you specify otherwise.
  151. if ( empty( $attr['orderby'] ) ) {
  152. $attr['orderby'] = 'post__in';
  153. }
  154. $attr['include'] = $attr['ids'];
  155. }
  156. // We're trusting author input, so let's at least make sure it looks like a valid orderby statement
  157. if ( isset( $attr['orderby'] ) ) {
  158. $attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
  159. if ( ! $attr['orderby'] ) {
  160. unset( $attr['orderby'] );
  161. }
  162. }
  163. return shortcode_atts(array(
  164. 'order' => 'ASC',
  165. 'orderby' => 'menu_order ID',
  166. 'id' => $post->ID,
  167. 'itemtag' => 'dl',
  168. 'icontag' => 'dt',
  169. 'captiontag' => 'dd',
  170. 'columns' => 3,
  171. 'size' => 'thumbnail',
  172. 'include' => '',
  173. 'exclude' => '',
  174. 'pausetime' => 3000,
  175. 'direction' => 'none',
  176. 'where' => 'single',
  177. ), $attr, 'gallery');
  178. }
  179. }
  180. new Vamtam_Gallery;