cipher87

Gallery with Custom Links

Sep 14th, 2019
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 14.50 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Gallery
  4.  *
  5.  * Shortcode that allows to create a gallery based on images selected from the media library
  6.  */
  7. if ( ! defined( 'ABSPATH' ) ) {  exit;  }    // Exit if accessed directly
  8.  
  9.  
  10. if ( ! class_exists( 'avia_sc_gallery' ) )
  11. {
  12.     class avia_sc_gallery extends aviaShortcodeTemplate
  13.     {
  14.             /**
  15.              *
  16.              * @var int
  17.              */
  18.             static public $gallery = 0;
  19.            
  20.             /**
  21.              *
  22.              * @var string
  23.              */
  24.             public $extra_style = "";
  25.            
  26.             /**
  27.              *
  28.              * @var string
  29.              */
  30.             public $non_ajax_style = "";
  31.  
  32.             /**
  33.              * Create the config array for the shortcode button
  34.              */
  35.             function shortcode_insert_button()
  36.             {
  37.                 $this->config['self_closing']   =   'yes';
  38.                
  39.                 $this->config['name']           = __( 'Gallery', 'avia_framework' );
  40.                 $this->config['tab']            = __( 'Media Elements', 'avia_framework' );
  41.                 $this->config['icon']           = AviaBuilder::$path['imagesURL']."sc-gallery.png";
  42.                 $this->config['order']          = 6;
  43.                 $this->config['target']         = 'avia-target-insert';
  44.                 $this->config['shortcode']      = 'av_gallery';
  45.                 $this->config['modal_data']     = array( 'modal_class' => 'mediumscreen' );
  46.                 $this->config['tooltip']        = __( 'Creates a custom gallery', 'avia_framework' );
  47.                 $this->config['preview']        = 1;
  48.                 $this->config['disabling_allowed'] = 'manually'; //only allowed manually since the default [gallery shortcode] is also affected
  49.                 $this->config['id_name']        = 'id';
  50.                 $this->config['id_show']        = 'yes';
  51.             }
  52.            
  53.             function extra_assets()
  54.             {
  55.                 //load css
  56.                 wp_enqueue_style( 'avia-module-gallery' , AviaBuilder::$path['pluginUrlRoot'].'avia-shortcodes/gallery/gallery.css' , array('avia-layout'), false );
  57.                
  58.                 wp_enqueue_script( 'avia-module-gallery' , AviaBuilder::$path['pluginUrlRoot'].'avia-shortcodes/gallery/gallery.js' , array('avia-shortcodes'), false, true );
  59.  
  60.             }
  61.  
  62.             /**
  63.              * Popup Elements
  64.              *
  65.              * If this function is defined in a child class the element automatically gets an edit button, that, when pressed
  66.              * opens a modal window that allows to edit the element properties
  67.              *
  68.              * @return void
  69.              */
  70.             function popup_elements()
  71.             {
  72.                 $this->elements = array(
  73.                     array(
  74.                         "type"  => "tab_container", 'nodescription' => true
  75.                     ),
  76.                    
  77.                     array(
  78.                         "type"  => "tab",
  79.                         "name"  => __("Content" , 'avia_framework'),
  80.                         'nodescription' => true
  81.                     ),
  82.                    
  83.                    
  84.                     array(
  85.                             "name"  => __("Edit Gallery",'avia_framework' ),
  86.                             "desc"  => __("Create a new Gallery by selecting existing or uploading new images",'avia_framework' ),
  87.                             "id"    => "ids",
  88.                             "type"  => "gallery",
  89.                             "modal_class" => 'av-show-image-custom-link',
  90.                             "title" => __("Add/Edit Gallery",'avia_framework' ),
  91.                             "button" => __("Insert Images",'avia_framework' ),
  92.                             "std"   => ""),
  93.  
  94.                     array(
  95.                             "name"  => __("Gallery Style", 'avia_framework' ),
  96.                             "desc"  => __("Choose the layout of your Gallery", 'avia_framework' ),
  97.                             "id"    => "style",
  98.                             "type"  => "select",
  99.                             "std"   => "thumbnails",
  100.                             "subtype" => array(
  101.                                                 __('Small Thumbnails',  'avia_framework' ) =>'thumbnails',
  102.                                                 __('Big image with thumbnails below', 'avia_framework' ) =>'big_thumb',
  103.                                                 __('Big image only, other images can be accessed via lightbox', 'avia_framework' ) =>'big_thumb lightbox_gallery',
  104.                                                 )
  105.                             ),
  106.  
  107.                     array(
  108.                             "name"  => __("Gallery Big Preview Image Size", 'avia_framework' ),
  109.                             "desc"  => __("Choose image size for the Big Preview Image", 'avia_framework' ),
  110.                             "id"    => "preview_size",
  111.                             "type"  => "select",
  112.                             "std"   => "portfolio",
  113.                             "required"  => array('style','contains','big_thumb'),
  114.                             "subtype" =>  AviaHelper::get_registered_image_sizes(array('logo'))
  115.                             ),
  116.  
  117.                     array(
  118.                             "name"  => __("Force same size for all big preview images?", 'avia_framework' ),
  119.                             "desc"  => __("Depending on the size you selected above, preview images might differ in size. Should the theme force them to display at exactly the same size?", 'avia_framework' ),
  120.                             "id"    => "crop_big_preview_thumbnail",
  121.                             "type"  => "select",
  122.                             "std"   => "yes",
  123.                             "required"  => array('style','equals','big_thumb'),
  124.                             "subtype" =>  array(__('Yes, force same size on all Big Preview images, even if they use a different aspect ratio', 'avia_framework') => 'avia-gallery-big-crop-thumb', __('No, do not force the same size', 'avia_framework') => 'avia-gallery-big-no-crop-thumb')),
  125.  
  126.                     array(
  127.                         "name"  => __("Gallery Preview Image Size", 'avia_framework' ),
  128.                         "desc"  => __("Choose image size for the small preview thumbnails", 'avia_framework' ),
  129.                         "id"    => "thumb_size",
  130.                         "type"  => "select",
  131.                         "std"   => "portfolio",
  132.                             "required"  => array('style','not','big_thumb lightbox_gallery'),
  133.                         "subtype" =>  AviaHelper::get_registered_image_sizes(array('logo'))
  134.                     ),
  135.  
  136.                     array(
  137.                             "name"  => __("Thumbnail Columns", 'avia_framework' ),
  138.                             "desc"  => __("Choose the column count of your Gallery", 'avia_framework' ),
  139.                             "id"    => "columns",
  140.                             "type"  => "select",
  141.                             "std"   => "5",
  142.                             "required"  => array('style','not','big_thumb lightbox_gallery'),
  143.                             "subtype" => AviaHtmlHelper::number_array(1,12,1)
  144.                             ),
  145.  
  146.                     array(
  147.                             "name"  => __("Use Lighbox", 'avia_framework' ),
  148.                             "desc"  => __("Do you want to activate the lightbox", 'avia_framework' ),
  149.                             "id"    => "imagelink",
  150.                             "type"  => "select",
  151.                             "std"   => "5",
  152.                             "required"  => array('style','not','big_thumb lightbox_gallery'),
  153.                             "subtype" => array(
  154.                                 __('Yes',  'avia_framework' ) =>'lightbox',
  155.                                 __('No, open the images in the browser window', 'avia_framework' ) =>'aviaopeninbrowser noLightbox',
  156.                                 __('No, open the images in a new browser window/tab', 'avia_framework' ) =>'aviaopeninbrowser aviablank noLightbox',
  157.                                 __('No, don\'t add a link to the images at all', 'avia_framework' ) =>'avianolink noLightbox')
  158.                             ),
  159.  
  160.                             array(
  161.                                 "name"  => __("Thumbnail fade in effect", 'avia_framework' ),
  162.                                 "desc"  => __("You can set when the gallery thumbnail animation starts", 'avia_framework' ),
  163.                                 "id"    => "lazyload",
  164.                                 "type"  => "select",
  165.                                 "std"   => "avia_lazyload",
  166.                                 "required"  => array('style','not','big_thumb lightbox_gallery'),
  167.                                 "subtype" => array(
  168.                                     __('Disable all animations',  'avia_framework' ) =>'animations_off',
  169.                                     __('Show the animation when user scrolls to the gallery',  'avia_framework' ) =>'avia_lazyload',
  170.                                     __('Activate animation on page load (might be preferable on large galleries)', 'avia_framework' ) =>'deactivate_avia_lazyload')
  171.                             ),
  172.  
  173.  
  174.                            
  175.                             array(
  176.                                     "type"  => "close_div",
  177.                                     'nodescription' => true
  178.                                 ), 
  179.                    
  180.                             array( 
  181.                                     'type'          => 'template',
  182.                                     'template_id'   => 'screen_options_tab'
  183.                                 ),
  184.                        
  185.                    
  186.                         array(
  187.                             "type"  => "close_div",
  188.                             'nodescription' => true
  189.                         ), 
  190.                        
  191.  
  192.                         );
  193.  
  194.             }
  195.  
  196.             /**
  197.              * Editor Element - this function defines the visual appearance of an element on the AviaBuilder Canvas
  198.              * Most common usage is to define some markup in the $params['innerHtml'] which is then inserted into the drag and drop container
  199.              * Less often used: $params['data'] to add data attributes, $params['class'] to modify the className
  200.              *
  201.              *
  202.              * @param array $params this array holds the default values for $content and $args.
  203.              * @return $params the return array usually holds an innerHtml key that holds item specific markup.
  204.              */
  205.             function editor_element($params)
  206.             {
  207.                 $params['innerHtml'] = "<img src='".$this->config['icon']."' title='".$this->config['name']."' />";
  208.                 $params['innerHtml'].= "<div class='avia-element-label'>".$this->config['name']."</div>";
  209.                 $params['content']   = null; //remove to allow content elements
  210.                 return $params;
  211.             }
  212.  
  213.             /**
  214.              * Frontend Shortcode Handler
  215.              *
  216.              * @param array $atts array of attributes
  217.              * @param string $content text within enclosing form of shortcode element
  218.              * @param string $shortcodename the shortcode found, when == callback name
  219.              * @return string $output returns the modified html string
  220.              */
  221.             function shortcode_handler( $atts, $content = "", $shortcodename = "", $meta = "" )
  222.             {
  223.                
  224.                 extract( AviaHelper::av_mobile_sizes( $atts ) ); //return $av_font_classes, $av_title_font_classes and $av_display_classes
  225.                
  226.                 $output  = "";
  227.                 $first   = true;
  228.                
  229.                 if(empty($atts['columns']) && isset($atts['ids']))
  230.                 {
  231.                     $atts['columns'] = count(explode(",", $atts['ids']));
  232.                     if($atts['columns'] > 10) { $atts['columns'] = 10; }
  233.                 }
  234.                
  235.                 extract( shortcode_atts( array(
  236.                             'order'         => 'ASC',
  237.                             'thumb_size'    => 'thumbnail',
  238.                             'size'          => '',
  239.                             'lightbox_size' => 'large',
  240.                             'preview_size'  => 'portfolio',
  241.                             'ids'           => '',
  242.                             'ajax_request'  => false,
  243.                             'imagelink'     => 'lightbox',
  244.                             'style'         => 'thumbnails',
  245.                             'columns'       => 5,
  246.                             'lazyload'      => 'avia_lazyload',
  247.                             'crop_big_preview_thumbnail' => 'avia-gallery-big-crop-thumb'
  248.                         ), $atts, $this->config['shortcode'] ) );
  249.                    
  250.  
  251.                 $attachments = get_posts( array(
  252.                                     'include'       => $ids,
  253.                                     'post_status'   => 'inherit',
  254.                                     'post_type'     => 'attachment',
  255.                                     'post_mime_type' => 'image',
  256.                                     'order'         => $order,
  257.                                     'orderby'       => 'post__in'
  258.                                     )
  259.                             );
  260.                
  261.                
  262.                 //compatibility mode for default wp galleries
  263.                 if(!empty($size)) $thumb_size = $size;
  264.                
  265.                
  266.                 if('big_thumb lightbox_gallery' == $style)
  267.                 {
  268.                     $imagelink = 'lightbox';
  269.                     $lazyload  = 'deactivate_avia_lazyload';
  270.                     $meta['el_class'] .= " av-hide-gallery-thumbs";
  271.                 }
  272.  
  273.  
  274.                 // animation
  275.                 $animation_class = "";
  276.                 if ($lazyload !== 'animations_off'){
  277.                     $animation_class = 'avia-gallery-animate';
  278.                 }
  279.  
  280.  
  281.                 if( ! empty( $attachments ) && is_array( $attachments ) )
  282.                 {
  283.                     self::$gallery++;
  284.                     $thumb_width = round(100 / $columns, 4);
  285.  
  286.                     $markup = avia_markup_helper(array('context' => 'image','echo'=>false, 'custom_markup'=>$meta['custom_markup']));
  287.                     $output .= "<div {$meta['custom_el_id']} class='avia-gallery {$av_display_classes} avia-gallery-".self::$gallery." ".$lazyload." ".$animation_class." avia_animate_when_visible ".$meta['el_class']."' $markup>";
  288.                     $thumbs = "";
  289.                     $counter = 0;
  290.  
  291.                     foreach($attachments as $attachment)
  292.                     {
  293.                         $link    =  apply_filters('avf_avia_builder_gallery_image_link', wp_get_attachment_image_src($attachment->ID, $lightbox_size), $attachment, $atts, $meta);
  294.                         if ($imagelink === 'aviaopeninbrowser noLightbox' || $imagelink === 'aviaopeninbrowser aviablank noLightbox') {
  295.                             $link[0] = get_post_meta($attachment->ID, 'av-custom-link', true);
  296.                         }
  297.                        
  298.                         $custom_link_class = !empty($link['custom_link_class']) ? $link['custom_link_class'] : '';
  299.                         $class   = $counter++ % $columns ? "class='$imagelink $custom_link_class'" : "class='first_thumb $imagelink $custom_link_class'";
  300.                         $img     = wp_get_attachment_image_src($attachment->ID, $thumb_size);
  301.                         $prev    = wp_get_attachment_image_src($attachment->ID, $preview_size);
  302.  
  303.                         $caption = trim($attachment->post_excerpt) ? wptexturize($attachment->post_excerpt) : "";
  304.                         $tooltip = $caption ? "data-avia-tooltip='".$caption."'" : "";
  305.  
  306.                         $alt = get_post_meta($attachment->ID, '_wp_attachment_image_alt', true);
  307.                         $alt = !empty($alt) ? esc_attr($alt) : '';
  308.                         $title = trim($attachment->post_title) ? esc_attr($attachment->post_title) : "";
  309.                         $description = trim($attachment->post_content) ? esc_attr($attachment->post_content) : esc_attr(trim($attachment->post_excerpt));
  310.  
  311.                         $markup_url = avia_markup_helper(array('context' => 'image_url','echo'=>false, 'id'=>$attachment->ID, 'custom_markup'=>$meta['custom_markup']));
  312.  
  313.                         if( strpos($style, "big_thumb") !== false && $first)
  314.                         {
  315.                             $output .= "<a class='avia-gallery-big fakeLightbox $imagelink $crop_big_preview_thumbnail $custom_link_class' href='".$link[0]."'  data-onclick='1' title='".$description."' ><span class='avia-gallery-big-inner' $markup_url>";
  316.                             $output .= "    <img width='".$prev[1]."' height='".$prev[2]."' src='".$prev[0]."' title='".$title."' alt='".$alt."' />";
  317.                if($caption) $output .= "    <span class='avia-gallery-caption'>{$caption}</span>";
  318.                             $output .= "</span></a>";
  319.                         }
  320.  
  321.                         $thumbs .= " <a href='".$link[0]."' data-rel='gallery-".self::$gallery."' data-prev-img='".$prev[0]."' {$class} data-onclick='{$counter}' title='".$description."' $markup_url><img {$tooltip} src='".$img[0]."' width='".$img[1]."' height='".$img[2]."'  title='".$title."' alt='".$alt."' /></a>";
  322.                         $first = false;
  323.                     }
  324.  
  325.                     $output .= "<div class='avia-gallery-thumb'>{$thumbs}</div>";
  326.                     $output .= "</div>";
  327.                    
  328.                     $selector = !empty($atts['ajax_request']) ? ".ajax_slide" : "";
  329.                    
  330.                     //generate thumb width based on columns
  331.                     $this->extra_style .= "<style type='text/css'>";
  332.                     $this->extra_style .= "#top #wrap_all {$selector} .avia-gallery-".self::$gallery." .avia-gallery-thumb a{width:{$thumb_width}%;}";
  333.                     $this->extra_style .= "</style>";
  334.                    
  335.                     if(!empty($this->extra_style))
  336.                     {
  337.                        
  338.                         if(!empty($atts['ajax_request']) || !empty($_POST['avia_request']))
  339.                         {
  340.                             $output .= $this->extra_style;
  341.                             $this->extra_style = "";
  342.                         }
  343.                         else
  344.                         {
  345.                             $this->non_ajax_style = $this->extra_style;
  346.                             add_action('wp_footer', array($this, 'print_extra_style'));
  347.                         }
  348.                     }
  349.  
  350.                 }
  351.  
  352.                 return $output;
  353.             }
  354.  
  355.  
  356.             function print_extra_style()
  357.             {
  358.                 echo $this->non_ajax_style;
  359.             }
  360.  
  361.     }
  362. }
Add Comment
Please, Sign In to add comment