Guest User

TPML Gallery

a guest
Sep 8th, 2017
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.81 KB | None | 0 0
  1. <?php
  2. // Filters the default gallery shortcode output.
  3. // If the filtered output isnโ€™t empty, it will be used instead of generating the default gallery template.
  4.  
  5. add_filter( 'post_gallery', 'vam_post_gallery', 10, 3 );
  6.  
  7. function vam_post_gallery( $output = '', $atts, $instance ) {
  8.  
  9.     $post = get_post();
  10.  
  11.     static $instance = 0;
  12.     $instance++;
  13.  
  14.     $defaults = array(
  15.         'id'      => $post ? $post->ID : 0,
  16.         'order'   => 'ASC',
  17.         'orderby' => 'menu_order ID',
  18.         'columns' => 3,
  19.         'size'    => 'full',
  20.         'type'  => 'grid',
  21.     );
  22.  
  23.     $atts = wp_parse_args( $atts, $defaults );
  24.  
  25.     $id = intval( $atts['id'] );
  26.  
  27.     if ( ! empty( $atts['ids'] ) ) {
  28.         // 'ids' is explicitly ordered, unless you specify otherwise.
  29.         if ( empty( $atts['orderby'] ) ) {
  30.             $atts['orderby'] = 'post__in';
  31.         }
  32.         $atts['include'] = $atts['ids'];
  33.     }
  34.  
  35.     // We're trusting author input, so let's at least make sure it looks like a valid orderby statement
  36.     if ( isset( $attr['orderby'] ) ) {
  37.         $attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
  38.         if ( ! $attr['orderby'] ) {
  39.             unset( $attr['orderby'] );
  40.         }
  41.     }
  42.  
  43.     if ( ! empty( $atts['include'] ) ) {
  44.         $post_attachments = get_posts(
  45.             array(
  46.                 'include'        => $atts['include'],
  47.                 'post_status'    => 'inherit',
  48.                 'post_type'      => 'attachment',
  49.                 'post_mime_type' => 'image',
  50.                 'order'          => $atts['order'],
  51.                 'orderby'        => $atts['orderby'],
  52.             )
  53.         );
  54.         $attachments = array();
  55.         foreach ( $post_attachments as $key => $val ) {
  56.             $attachments[ $val->ID ] = $post_attachments[ $key ];
  57.         }
  58.     } elseif ( ! empty( $atts['exclude'] ) ) {
  59.         $attachments = get_children(
  60.             array(
  61.                 'post_parent'    => $id,
  62.                 'exclude'        => $atts['exclude'],
  63.                 'post_status'    => 'inherit',
  64.                 'post_type'      => 'attachment',
  65.                 'post_mime_type' => 'image',
  66.                 'order'          => $atts['order'],
  67.                 'orderby'        => $atts['orderby'],
  68.             )
  69.         );
  70.     } else {
  71.         $attachments = get_children(
  72.             array(
  73.                 'post_parent'    => $id,
  74.                 'post_status'    => 'inherit',
  75.                 'post_type'      => 'attachment',
  76.                 'post_mime_type' => 'image',
  77.                 'order'          => $atts['order'],
  78.                 'orderby'        => $atts['orderby'],
  79.             )
  80.         );
  81.     }
  82.     if ( empty( $attachments ) ) {
  83.         return '';
  84.     }
  85.  
  86.     $selector = "gallery-{$instance}";
  87.  
  88.     if ( isset( $atts['type'] ) ) {
  89.         $type = $atts['type'];
  90.     }
  91.  
  92.     if ( ! empty( $type ) ) {
  93.         // Justified
  94.         if ( 'justified' === $type ) {
  95.             $output = '<div class="gallery-justified">';
  96.             foreach ( $attachments as $id => $attachment ) {
  97.                 $attr = ( trim( $attachment->post_excerpt ) ) ?
  98.                 array(
  99.                     'aria-describedby' => "$selector-$id",
  100.                 ) : '';
  101.  
  102.                 if ( ! empty( $atts['link'] ) && 'file' === $atts['link'] ) {
  103.                     $image_output = wp_get_attachment_link( $id, $atts['size'], false, false, false, $attr );
  104.                 } elseif ( ! empty( $atts['link'] ) && 'none' === $atts['link'] ) {
  105.                     $image_output = wp_get_attachment_image( $id, $atts['size'], false, $attr );
  106.                 } else {
  107.                     $image_output = wp_get_attachment_link( $id, $atts['size'], true, false, false, $attr );
  108.                 }
  109.                 $output .= '<figure>' . $image_output . '</figure>';
  110.             }
  111.             $output .= '</div>';
  112.         }
  113.  
  114.         if ( 'slider' === $type ) {
  115.             do_action( 'vam_theme_owlslider', get_the_ID() );
  116.             if ( ! empty( $attachments ) ) {
  117.                 $output .= '<div class="owl-container gallery-slider">';
  118.                 $output .= '<div class="owl-carousel" id="vam_postformat_gallery">';
  119.                 foreach ( $attachments as $id => $attachment ) {
  120.                     $output .= '<div class="owl-item">' . wp_get_attachment_image( $id, 'full' ) . '</div>';
  121.                 }
  122.                 $output .= '</div>';//.owl-carousel
  123.                 $output .= '</div><div class="clear"></div>';
  124.             }
  125.         }
  126.  
  127.         if ( 'grid' === $type ) {
  128.             // Grid
  129.  
  130.             $columns = $atts['columns'];
  131.  
  132.             if ( $columns == 5 ) {
  133.                 $columns = 4;
  134.             } elseif ( $columns == 7 ) {
  135.                 $columns = 6;
  136.             } elseif ( $columns == 8 ) {
  137.                 $columns = 6;
  138.             } elseif ( $columns == 9 ) {
  139.                 $columns = 12;
  140.             }
  141.             $i = 0;
  142.             $output = '<div class="gallery-grid">';
  143.             $output .= '<div class="row">';
  144.             foreach ( $attachments as $id => $attachment ) {
  145.                 $attr = ( trim( $attachment->post_excerpt ) ) ? array( 'aria-describedby' => "$selector-$id" ) : '';
  146.                 if ( ! empty( $atts['link'] ) && 'file' === $atts['link'] ) {
  147.                     $image_output = wp_get_attachment_link( $id, $atts['size'], false, false, false, $attr );
  148.                 } elseif ( ! empty( $atts['link'] ) && 'none' === $atts['link'] ) {
  149.                     $image_output = wp_get_attachment_image( $id, $atts['size'], false, $attr );
  150.                 } else {
  151.                     $image_output = wp_get_attachment_link( $id, $atts['size'], true, false, false, $attr );
  152.                 }
  153.                 $output .= "<div class='item col-md-" . 12 / $columns . "'>";
  154.                 $output .= '<figure>';
  155.                 $output .= $image_output;
  156.                 $output .= '</figure>';
  157.                 $output .= '</div>';
  158.                 $current = ++$i;
  159.                 if ( $columns > 0 && $current % 0 === $columns && $current !== count( $attachments ) ) {
  160.                     $output .= '</div><div class="row">';
  161.                 }
  162.                 if ( $current == count( $attachments ) ) {
  163.                     $output .= '</div>';
  164.                 }
  165.             }
  166.             $output .= '</div>';
  167.         }
  168.     }
  169.     return $output;
  170. }
  171.  
  172. add_action('print_media_templates', function() {
  173.     // define your backbone template;
  174.     // the "tmpl-" prefix is required,
  175.     // and your input field should have a data-setting attribute
  176.     // matching the shortcode name
  177.  
  178.     $gallery_types = apply_filters('print_media_templates_gallery_settings_types',
  179.         array(
  180.             'justified' => esc_html__( 'Justified','vam' ),
  181.             'grid'      => esc_html__( 'Grid','vam' ),
  182.             'slider'    => esc_html__( 'Slider','vam' ),
  183.             'default_val'    => esc_html__( 'Default','vam' ),
  184.         )
  185.     );
  186.     ?>
  187.     <script type="text/html" id="tmpl-custom-gallery-type-setting">
  188.         <label class="setting">
  189.             <span><?php esc_html_e( 'Layout Type' ); ?></span>
  190.             <select data-setting="type">
  191.             <?php
  192.             foreach ( $gallery_types as $key => $value ) {
  193.                 echo "<option value=\"$key\">$value</option>";
  194.             }
  195.             ?>
  196.             </select>
  197.         </label>
  198.     </script>
  199.  
  200.   <script>
  201.     jQuery(document).ready(function(){
  202.  
  203.         // add your shortcode attribute and its default value to the
  204.         // gallery settings list; $.extend should work as well...
  205.         _.extend(wp.media.gallery.defaults, {
  206.             type: 'default_val'
  207.         });
  208.  
  209.         // join default gallery settings template with yours -- store in list
  210.         if (!wp.media.gallery.templates) wp.media.gallery.templates = ['gallery-settings'];
  211.         wp.media.gallery.templates.push('custom-gallery-type-setting');
  212.  
  213.         // merge default gallery settings template with yours
  214.         wp.media.view.Settings.Gallery = wp.media.view.Settings.Gallery.extend({
  215.             template: function (view) {
  216.                 var output = '';
  217.                 for (var i in wp.media.gallery.templates) {
  218.                     output += wp.media.template(wp.media.gallery.templates[i])(view);
  219.                 }
  220.                 return output;
  221.             }
  222.         });
  223.     });
  224.   </script>
  225.     <?php
  226. });
Add Comment
Please, Sign In to add comment