Advertisement
Guest User

av-helper-masonry.php

a guest
Jan 15th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 26.84 KB | None | 0 0
  1. <?php
  2.  
  3. if ( !class_exists( 'avia_masonry' ) )
  4. {
  5.     class avia_masonry
  6.     {
  7.         static  $element = 0;
  8.         protected $atts;
  9.         protected $entries;
  10.  
  11.         function __construct($atts = array())
  12.         {
  13.             self::$element += 1;
  14.            
  15.             $this->screen_options = AviaHelper::av_mobile_sizes($atts);
  16.             $this->atts = shortcode_atts(array( 'ids'   => false,
  17.                                                 'action'=> false,
  18.                                                 'link'  => 'category',
  19.                                                 'post_type'=> get_post_types(),
  20.                                                 'items' => 24,
  21.                                                 'size'  => 'fixed',
  22.                                                 'gap'   => '1px',
  23.                                                 'overlay_fx'        => 'active',
  24.                                                 'offset'            => 0,
  25.                                                 'container_links'   => true,
  26.                                                 'container_class'   => "",
  27.                                                 'paginate'          => 'paginate',
  28.                                                 'caption_elements'  => 'title excerpt',
  29.                                                 'caption_display'   => 'always',
  30.                                                 'caption_styling'   => '',
  31.                                                 'wc_prod_visible'   => '',
  32.                                                 'prod_order_by'     => '',
  33.                                                 'prod_order'        => '',
  34.                                                 'sort'              => 'no',
  35.                                                 'columns'           => 'automatic',
  36.                                                 'auto_ratio'        => 1.7, //equals a 16:9 ratio
  37.                                                 'set_breadcrumb'    => true, //no shortcode option for this, modifies the breadcrumb nav, must be false on taxonomy overview
  38.                                                 'custom_markup'     => '',
  39.                                                 'query_orderby'     => 'date',
  40.                                                 'query_order'       => 'DESC',
  41.                                                 'color'             => '',
  42.                                                 'custom_bg'         => '',
  43.                                                 'orientation'       => '',
  44.                                                 ), $atts, 'av_masonry_entries');
  45.            
  46.            
  47.             if($this->atts['caption_elements'] == 'none')
  48.             {
  49.                 $this->atts['caption_styling'] = "";
  50.             }
  51.                                                    
  52.             $this->atts = apply_filters('avf_masonry_settings', $this->atts, self::$element);
  53.         }
  54.        
  55.         //ajax function to load additional items
  56.         static function load_more()
  57.         {
  58.             if(check_ajax_referer('av-masonry-nonce', 'avno'));
  59.            
  60.             //increase the post items by one to fetch an additional item. this item is later removed by the javascript but it tells the script if there are more items to load or not
  61.             $_POST['items'] = empty($_POST['items']) ? 1 : $_POST['items'] + 1;
  62.        
  63.             $masonry    = new avia_masonry($_POST);
  64.             $ajax       = true;
  65.            
  66.             if(!empty($_POST['ids']))
  67.             {
  68.                 $masonry->query_entries_by_id(array(), $ajax);
  69.             }
  70.             else
  71.             {
  72.                 $masonry->extract_terms();
  73.                 $masonry->query_entries(array(), $ajax);
  74.             }
  75.            
  76.            
  77.             $output = $masonry->html( );
  78.                    
  79.             echo '{av-masonry-loaded}'.$output;
  80.             exit();
  81.         }
  82.        
  83.        
  84.         function extract_terms()
  85.         {
  86.             if(isset($this->atts['link']))
  87.             {
  88.                 $this->atts['link'] = explode(',', $this->atts['link'], 2 );
  89.                 $this->atts['taxonomy'] = $this->atts['link'][0];
  90.  
  91.                 if(isset($this->atts['link'][1]))
  92.                 {
  93.                     $this->atts['categories'] = $this->atts['link'][1];
  94.                 }
  95.                 else
  96.                 {
  97.                     $this->atts['categories'] = array();
  98.                 }
  99.             }
  100.         }
  101.        
  102.         function sort_buttons()
  103.         {
  104.             $sort_terms = get_terms( $this->atts['taxonomy'] , array('hide_empty'=>true) );
  105.            
  106.             $current_page_terms = array();
  107.             $term_count         = array();
  108.             $display_terms      = is_array($this->atts['categories']) ? $this->atts['categories'] : array_filter(explode(',',$this->atts['categories']));
  109.  
  110.             foreach ($this->loop as $entry)
  111.             {
  112.                 if($current_item_terms = get_the_terms( $entry['ID'], $this->atts['taxonomy'] ))
  113.                 {
  114.                     if(!empty($current_item_terms))
  115.                     {
  116.                         foreach($current_item_terms as $current_item_term)
  117.                         {
  118.                             if(empty($display_terms) || in_array($current_item_term->term_id, $display_terms))
  119.                             {
  120.                                 $current_page_terms[$current_item_term->term_id] = $current_item_term->term_id;
  121.  
  122.                                 if(!isset($term_count[$current_item_term->term_id] ))
  123.                                 {
  124.                                     $term_count[$current_item_term->term_id] = 0;
  125.                                 }
  126.  
  127.                                 $term_count[$current_item_term->term_id] ++;
  128.                             }
  129.                         }
  130.                     }
  131.                 }
  132.             }
  133.            
  134.            
  135.             $hide   = count($display_terms) <= 1 ? "hidden" : "";
  136.             $output = "";
  137.            
  138.             if(empty($hide))
  139.             {
  140.                 $output  = "<div class='av-masonry-sort main_color av-sort-".$this->atts['sort']."' data-masonry-id='".self::$element."' >";
  141.                 //$output .= "<div class='container'>";
  142.                
  143.                 $first_item_name = apply_filters('avf_masonry_sort_first_label', __('All','avia_framework' ), $this->atts);
  144.                 $first_item_html = '<span class="inner_sort_button"><span>'.$first_item_name.'</span><small class="avia-term-count"> '.count($this->loop).' </small></span>';
  145.                
  146.                 $output .= apply_filters('avf_masonry_sort_heading', "", $this->atts);
  147.                
  148.                 if(strpos($this->atts['sort'], 'tax') !== false) $output .= "<div class='av-current-sort-title'>{$first_item_html}</div>";
  149.                
  150.                 $sort_loop = "";
  151.                 $allowed_terms = array();
  152.                
  153.                 foreach($sort_terms as $term)
  154.                 {
  155.                     $show_item = in_array($term->term_id, $current_page_terms) ? 'avia_show_sort' : 'avia_hide_sort';
  156.                    
  157.                     if(!isset($term_count[$term->term_id])) $term_count[$term->term_id] = 0;
  158.                    
  159.                     $term->slug = str_replace('%', '', $term->slug);
  160.                    
  161.                     if( empty($display_terms) || in_array($term->term_id, $display_terms))
  162.                     {
  163.                         $allowed_terms[] = $term->slug.'_sort';
  164.                     }
  165.                    
  166.                     $sort_loop .=   "<span class='text-sep {$term->slug}_sort_sep {$show_item}'>/</span>";
  167.                     $sort_loop .=   '<a href="#" data-filter="'.$term->slug.'_sort" class="'.$term->slug.'_sort_button '.$show_item.'" ><span class="inner_sort_button">';
  168.                     $sort_loop .=       "<span>".esc_html(trim($term->name))."</span>";
  169.                     $sort_loop .=       "<small class='avia-term-count'> ".$term_count[$term->term_id]." </small></span>";
  170.                     $sort_loop .=   "</a>";
  171.                 }
  172.                
  173.                 $allowed_terms = json_encode($allowed_terms);
  174.                 $output .= "<div class='av-sort-by-term {$hide} ' data-av-allowed-sort='{$allowed_terms}' >";
  175.                 $output .= '<a href="#" data-filter="all_sort" class="all_sort_button active_sort">'.$first_item_html.'</a>';
  176.                 $output .= $sort_loop;
  177.                 $output .= "</div></div>";
  178.             }
  179.            
  180.             return $output;
  181.  
  182.            
  183.         }
  184.        
  185.         //get the categories for each post and create a string that serves as classes so the javascript can sort by those classes
  186.         function sort_array($the_id)
  187.         {
  188.             $sort_classes   = array("all_sort");
  189.             $item_terms     = get_the_terms( $the_id, $this->atts['taxonomy']);
  190.  
  191.             if(is_object($item_terms) || is_array($item_terms))
  192.             {
  193.                 foreach ($item_terms as $term)
  194.                 {
  195.                     $term->slug = str_replace('%', '', $term->slug);
  196.                     $sort_classes[] = $term->slug.'_sort ';
  197.                 }
  198.             }
  199.  
  200.             return $sort_classes;
  201.         }
  202.  
  203.        
  204.        
  205.         function html()
  206.         {
  207.             if(empty($this->loop)) return;
  208.            
  209.             extract($this->screen_options); //return $av_font_classes, $av_title_font_classes, $av_display_classes and $av_column_classes
  210.            
  211.             $output     = "";
  212.             $items      = "";
  213.             $size       = strpos($this->atts['size'], 'fixed') !== false ? 'fixed' : "flex";
  214.             $auto       = strpos($this->atts['size'], 'masonry') !== false ? true : false;
  215.             $manually   = strpos($this->atts['size'], 'manually') !== false ? true : false;
  216.             $defaults   = array('ID'=>'',
  217.                                 'thumb_ID'=>'',
  218.                                 'title' =>'',
  219.                                 'url' => '',  
  220.                                 'class' => array(),  
  221.                                 'date' => '',
  222.                                 'excerpt' => '',
  223.                                 'data' => '',
  224.                                 'attachment'=> array(),
  225.                                 'attachment_overlay' => array(),
  226.                                 'bg' => "",
  227.                                 'before_content'=>'', // if set replaces the whole bg part
  228.                                 'text_before'=>'',
  229.                                 'text_after'=>'',
  230.                                 'img_before'=>'');
  231.            
  232.            
  233.             $style = "";
  234.            
  235.             if( !empty( $this->atts['color'] ) )
  236.             {
  237.                 $style .= AviaHelper::style_string( $this->atts, 'custom_bg', 'background-color' );
  238.                 $style  = AviaHelper::style_string( $style );
  239.             }
  240.            
  241.             $orientation = $this->atts['size'] == "fixed" ? $this->atts['orientation'] : "";
  242.            
  243.             $output .= "<div id='av-masonry-".self::$element."' class='av-masonry noHover av-{$size}-size av-{$this->atts['gap']}-gap av-hover-overlay-{$this->atts['overlay_fx']} av-masonry-col-{$this->atts['columns']} av-caption-{$this->atts['caption_display']} av-caption-style-{$this->atts['caption_styling']} {$this->atts['container_class']} {$orientation} {$av_display_classes} {$av_column_classes}' {$style} >";
  244.            
  245.             $output .= $this->atts['sort'] != "no" ? $this->sort_buttons() : "";
  246.            
  247.             $output .= "<div class='av-masonry-container isotope av-js-disabled ' >";
  248.             $all_sorts  = array();
  249.             $sort_array = array();
  250.             foreach($this->loop as $entry)
  251.             {
  252.                 extract(array_merge($defaults, $entry));
  253.                 $img_html       = "";
  254.                 $img_style      = "";
  255.                 if($this->atts['sort'] != "no")
  256.                 {
  257.                     $sort_array     = $this->sort_array($entry['ID']);
  258.                 }
  259.                 $class_string   = implode(' ', $class).' '.implode(' ', $sort_array);
  260.                 $all_sorts      = array_merge($all_sorts, $sort_array);
  261.                
  262.                 if(!empty($attachment))
  263.                 {
  264.                     $alt = get_post_meta($thumb_ID, '_wp_attachment_image_alt', true);
  265.                     $alt = !empty($alt) ? esc_attr($alt) : '';
  266.                     $title = esc_attr(get_the_title($thumb_ID));
  267.  
  268.                     if(isset($attachment[0]))
  269.                     {
  270.                         $img_html  = '<img src="'.$attachment[0].'" title="'.$title.'" alt="'.$alt.'" />';
  271.                         $img_style = 'style="background-image: url('.$attachment[0].');"';
  272.                         $class_string .= " av-masonry-item-with-image";
  273.                     }
  274.                    
  275.                     if(isset($attachment_overlay[0]))
  276.                     {
  277.                         $over_html  = '<img src="'.$attachment_overlay[0].'" title="'.$title.'" alt="'.$alt.'" />';
  278.                         $over_style = 'style="background-image: url('.$attachment_overlay[0].');"';
  279.                         $img_before = '<div class="av-masonry-image-container av-masonry-overlay" '.$over_style.'>'.$over_html.'</div>';
  280.                     }
  281.                    
  282.                     $bg = '<div class="av-masonry-outerimage-container">'.$img_before.'<div class="av-masonry-image-container" '.$img_style.'>'.$img_html.'</div></div>';
  283.                    
  284.                 }
  285.                 else
  286.                 {
  287.                     $class_string .= " av-masonry-item-no-image";
  288.                 }
  289.                
  290.                
  291.                 if($size == 'fixed')
  292.                 {
  293.                     if(!empty($attachment) || !empty($before_content))
  294.                     {
  295.                         if($auto)
  296.                             $class_string .= $this->ratio_check_by_image_size($attachment);
  297.                            
  298.                         if($manually)
  299.                             $class_string .= $this->ratio_check_by_tag($entry['tags']);
  300.                     }
  301.                 }
  302.                
  303.                 $linktitle = "";
  304.                
  305.                 if($post_type == 'attachment' && strpos($html_tags[0], 'a href=') !== false)
  306.                 {
  307.                     $linktitle = 'title="'.esc_attr($description).'"';
  308.                 }
  309.                 else if(strpos($html_tags[0], 'a href=') !== false)
  310.                 {
  311.                     $linktitle = 'title="'.esc_attr($the_title).'"';
  312.                 }
  313.                 $markup = ($post_type == 'attachment') ? avia_markup_helper(array('context' => 'image_url','echo'=>false, 'id'=>$entry['ID'], 'custom_markup'=>$this->atts['custom_markup'])) : avia_markup_helper(array('context' => 'entry','echo'=>false, 'id'=>$entry['ID'], 'custom_markup'=>$this->atts['custom_markup']));
  314.  
  315.                 $items .=   "<{$html_tags[0]} id='av-masonry-".self::$element."-item-".$entry['ID']."' data-av-masonry-item='".$entry['ID']."' class='{$class_string}' {$linktitle} {$markup}>";
  316.                 $items .=       "<div class='av-inner-masonry-sizer'></div>"; //responsible for the size
  317.                 $items .=       "<figure class='av-inner-masonry main_color'>";
  318.                 $items .=           $bg;
  319.                
  320.                 //title and excerpt
  321.                 if($this->atts['caption_elements'] != 'none' || !empty($text_add))
  322.                 {
  323.                     $items .=   "<figcaption class='av-inner-masonry-content site-background'><div class='av-inner-masonry-content-pos'><div class='av-inner-masonry-content-pos-content'><div class='avia-arrow'></div>".$text_before;
  324.                    
  325.                     if(strpos($this->atts['caption_elements'], 'title') !== false){
  326.                         $markup = avia_markup_helper(array('context' => 'entry_title','echo'=>false, 'id'=>$entry['ID'], 'custom_markup'=>$this->atts['custom_markup']));
  327.                         $items .=   "<h3 class='av-masonry-entry-title entry-title' {$markup}>{$the_title}</h3>";
  328.                     }
  329.  
  330.                     if(strpos($this->atts['caption_elements'], 'excerpt') !== false && !empty($content)){
  331.                         $markup = avia_markup_helper(array('context' => 'entry_content','echo'=>false, 'id'=>$entry['ID'], 'custom_markup'=>$this->atts['custom_markup']));
  332.                         $items .=   "<div class='av-masonry-entry-content entry-content' {$markup}>{$content}</div>";
  333.                     }
  334.                     $items .=   $text_after."</div></div></figcaption>";
  335.                 }
  336.                 $items .=       "</figure>";
  337.                 $items .=   "</{$html_tags[1]}><!--end av-masonry entry-->";                   
  338.             }
  339.            
  340.             //if its an ajax call return the items only without container
  341.             if(isset($this->atts['action']) && $this->atts['action'] == 'avia_ajax_masonry_more')
  342.             {
  343.                
  344.                 return $items;
  345.             }
  346.            
  347.             // if its no ajax load prepend an empty invisible element as the first element. this is used for calculating the correct width of a default element.
  348.             // in theory this is not necessary because the masonry can detect that with an extra js parameter but sorting becomes slugish if that param is set
  349.            
  350.             $all_sort_string = implode(' ', array_unique($all_sorts));
  351.             $items = "<div class='av-masonry-entry isotope-item av-masonry-item-no-image {$all_sort_string}'></div>".$items;
  352.            
  353.             $output .= $items;
  354.             $output .=  "</div>";
  355.            
  356.            
  357.             //append pagination
  358.             if($this->atts['paginate'] == "pagination" && $avia_pagination = avia_pagination($this->entries->max_num_pages, 'nav'))
  359.             {
  360.                 $output .= "<div class='av-masonry-pagination av-masonry-pagination-{$this->atts['paginate']}'>{$avia_pagination}</div>";
  361.             }
  362.             else if($this->atts['paginate'] == "load_more" && $this->entries->max_num_pages > count($this->entries))
  363.             {
  364.                 $output .= $this->load_more_button();
  365.             }
  366.            
  367.             $output .= "</div>";
  368.            
  369.             return $output;
  370.         }
  371.        
  372.                
  373.         function load_more_button()
  374.         {
  375.             $data_string  = AviaHelper::create_data_string($this->atts);
  376.             $data_string .= " data-avno='".wp_create_nonce( 'av-masonry-nonce' )."'";
  377.             $output  = "";
  378.             $output .=      "<a class='av-masonry-pagination av-masonry-load-more' href='#load-more' {$data_string}>".__('Load more','avia_framework')."</a>";
  379.            
  380.             return $output;
  381.         }  
  382.        
  383.         function ratio_check_by_image_size($attachment)
  384.         {
  385.             $img_size = ' av-grid-img';
  386.            
  387.             if(!empty($attachment[1]) && !empty($attachment[2]))
  388.             {
  389.                 if($attachment[1] > $attachment[2]) //landscape
  390.                 {
  391.                     //only consider it landscape if its 1.7 times wider than high
  392.                     if($attachment[1] / $attachment[2] > $this->atts['auto_ratio']) $img_size = ' av-landscape-img';
  393.                 }
  394.                 else //same check with portrait
  395.                 {
  396.                     if($attachment[2] / $attachment[1] > $this->atts['auto_ratio']) $img_size = ' av-portrait-img';
  397.                 }
  398.             }
  399.            
  400.             return $img_size;
  401.         }
  402.        
  403.         function ratio_check_by_tag($tags)
  404.         {
  405.             $img_size = '';
  406.            
  407.             if(is_array($tags))
  408.             {  
  409.                 $tag_values = apply_filters('avf_ratio_check_by_tag_values', array('portrait' => 'portrait', 'landscape' => 'landscape'));
  410.  
  411.                 if(in_array($tag_values['portrait'], $tags)) { $img_size .= ' av-portrait-img'; }
  412.                 if(in_array($tag_values['landscape'], $tags)){ $img_size .= ' av-landscape-img'; }
  413.             }
  414.            
  415.             if(empty($img_size))  $img_size = ' av-grid-img';
  416.            
  417.             return $img_size;
  418.            
  419.         }
  420.        
  421.        
  422.         function prepare_loop_from_entries( $ajax = false )
  423.         {
  424.             $this->loop = array();
  425.             if(empty($this->entries) || empty($this->entries->posts)) return;
  426.             $tagTax = "post_tag";
  427.             $date_format = get_option('date_format');
  428.            
  429.            
  430.             foreach($this->entries->posts as $key => $entry)
  431.             {  
  432.                 $overlay_img = $custom_url          = false;
  433.                 $img_size                           = 'masonry';
  434.                 $author = apply_filters('avf_author_name', get_the_author_meta('display_name', $entry->post_author), $entry->post_author);
  435.                        
  436.                 $this->loop[$key]['text_before']    = "";
  437.                 $this->loop[$key]['text_after']     = "";
  438.                 $this->loop[$key]['ID'] = $id       = $entry->ID;
  439.                 $this->loop[$key]['post_type']      = $entry->post_type;
  440.                 $this->loop[$key]['thumb_ID']       = get_post_thumbnail_id($id);
  441.                 $this->loop[$key]['the_title']      = get_the_title($id);
  442.                 $this->loop[$key]['url']            = get_permalink($id);
  443.                 $this->loop[$key]['date']           = "<span class='av-masonry-date meta-color updated'>".get_the_time($date_format, $id)."</span>";
  444.                 $this->loop[$key]['author']         = "<span class='av-masonry-author meta-color vcard author'><span class='fn'>". __('by','avia_framework') .' '. $author."</span></span>";
  445.                 $this->loop[$key]['class']          = get_post_class("av-masonry-entry isotope-item", $id);
  446.                 $this->loop[$key]['content']        = strip_tags( $entry->post_excerpt );
  447.                 $this->loop[$key]['description']    = !empty($entry->post_content) ? $entry->post_content : $entry->post_excerpt;
  448.                
  449.                 if(empty($this->loop[$key]['content']))
  450.                 {
  451.                     if($ajax)
  452.                     {
  453.                         $entry->post_content = preg_replace("!\[.*?\]!", "", $entry->post_content);
  454.                     }
  455.                    
  456.                     $this->loop[$key]['content']    = avia_backend_truncate($entry->post_content, apply_filters( 'avf_masonry_excerpt_length' , 60) , apply_filters( 'avf_masonry_excerpt_delimiter' , " "), "…", true, '');
  457.                 }
  458.                
  459.                 $this->loop[$key]['content'] = nl2br( trim($this->loop[$key]['content']) );
  460.                
  461.                 //post type specific
  462.                 switch($entry->post_type)
  463.                 {
  464.                     case 'post':
  465.                    
  466.                     $post_format        = get_post_format($id) ? get_post_format($id) : 'standard';
  467.                     $this->loop[$key]   = apply_filters( 'post-format-'.$post_format, $this->loop[$key] );
  468.                     $this->loop[$key]['text_after'] .= $this->loop[$key]['date'];
  469.                     $this->loop[$key]['text_after'] .= '<span class="av-masonry-text-sep text-sep-author">/</span>';
  470.                     $this->loop[$key]['text_after'] .= $this->loop[$key]['author'];
  471.                    
  472.                         switch($post_format)
  473.                         {
  474.                             case 'quote' :
  475.                             case 'link' :
  476.                             case 'image' :
  477.                             case 'gallery' :
  478.                                 if(!$this->loop[$key]['thumb_ID'])
  479.                                 {
  480.                                     $this->loop[$key]['text_before'] = av_icon_display($post_format);
  481.                                 }
  482.                             break;
  483.                            
  484.                             case 'audio' :
  485.                             case 'video' :
  486.                                 if(!$this->loop[$key]['thumb_ID'])
  487.                                 {
  488.                                     $this->loop[$key]['text_before'] = av_icon_display($post_format);
  489.                                 }
  490.                                 else
  491.                                 {
  492.                                     $this->loop[$key]['text_before'] = av_icon_display($post_format, 'av-masonry-media');
  493.                                 }
  494.                             break;
  495.                         }
  496.                    
  497.                    
  498.                    
  499.                     break;
  500.                    
  501.                     case 'portfolio':
  502.                    
  503.                     //set portfolio breadcrumb navigation
  504.                     if($this->atts['set_breadcrumb'] && is_page()) $_SESSION["avia_{$entry->post_type}"] = get_the_ID();
  505.                    
  506.                     //check if the user has set up a custom link
  507.                     if(!post_password_required($id)){
  508.                         $custom_link = get_post_meta( $id ,'_portfolio_custom_link', true) != "" ? get_post_meta( $id ,'_portfolio_custom_link_url', true) : false;
  509.                         if($custom_link) $this->loop[$key]['url'] = $custom_link;
  510.                     }
  511.                     break;
  512.                    
  513.                    
  514.                     case 'attachment':
  515.                    
  516.                     $custom_url = get_post_meta( $id, 'av-custom-link', true );
  517.                     $this->loop[$key]['thumb_ID'] = $id;
  518.                     $this->loop[$key]['content']        = $entry->post_excerpt;
  519.                    
  520.                     if($custom_url)
  521.                     {
  522.                         $this->loop[$key]['url'] = $custom_url;
  523.                     }
  524.                     else
  525.                     {
  526.                         $this->loop[$key]['url'] = wp_get_attachment_image_src($id, apply_filters('avf_avia_builder_masonry_lightbox_img_size','large'));
  527.                         $this->loop[$key]['url'] = reset($this->loop[$key]['url']);
  528.                     }
  529.  
  530.                    
  531.                     break;
  532.                    
  533.                     case 'product':
  534.                     //check if woocommerce is enabled in the first place so we can use woocommerce functions
  535.                     if(function_exists('avia_woocommerce_enabled') && avia_woocommerce_enabled())
  536.                     {
  537.                         $tagTax         = "product_tag";
  538.                         $product        = function_exists('wc_get_product') ? wc_get_product($id) : get_product( $id );
  539.                         $overlay_img    = avia_woocommerce_gallery_first_thumbnail($id, $img_size, true);
  540.  
  541.                         $this->loop[$key]['text_after'] .= '<span class="av-masonry-price price">'.$product->get_price_html()."</span>";
  542.                         if($product->is_on_sale( )) $this->loop[$key]['text_after'] .= '<span class="onsale">'.__( 'Sale!', 'avia_framework' ).'</span>';
  543.                     }
  544.                     break;
  545.                 }
  546.                
  547.                
  548.                 //check if post is password protected
  549.                 if(post_password_required($id))
  550.                 {
  551.                     $this->loop[$key]['content']        = "";
  552.                     $this->loop[$key]['class'][]        = "entry-protected";
  553.                     $this->loop[$key]['thumb_ID']       = "";
  554.                     $this->loop[$key]['text_before']    = av_icon_display('closed');
  555.                     $this->loop[$key]['text_after']     = $this->loop[$key]['date'];
  556.                 }
  557.                
  558.                
  559.                
  560.                 //set the html tags. depending on the link settings use either an a tag or a div tag
  561.                 if(!empty($this->atts['container_links']) || !empty($custom_url))
  562.                 {
  563.                     $this->loop[$key]['html_tags'] = array('a href="'.$this->loop[$key]['url'].'"','a'); //opening and closing tag for the masonry container
  564.                 }
  565.                 else
  566.                 {
  567.                     $this->loop[$key]['html_tags'] = array('div','div');
  568.                 }
  569.                
  570.                
  571.                 //get post tags
  572.                 $this->loop[$key]['tags']       = wp_get_post_terms($id, $tagTax, array( 'fields' => 'slugs' ));
  573.                
  574.                 //check if the image got landscape as well as portrait class applied. in that case use a bigger image size
  575.                 if(strlen($this->ratio_check_by_tag($this->loop[$key]['tags'])) > 20) $img_size = 'extra_large';
  576.                
  577.                 //get attachment data
  578.                 $this->loop[$key]['attachment'] = !empty($this->loop[$key]['thumb_ID']) ? wp_get_attachment_image_src($this->loop[$key]['thumb_ID'], $img_size) : "";
  579.                
  580.                 //get overlay attachment in case the overlay is set
  581.                 $this->loop[$key]['attachment_overlay'] = !empty($overlay_img) ? wp_get_attachment_image_src($overlay_img, $img_size) : "";
  582.                
  583.                 //apply filter for other post types, in case we want to use them and display additional/different information
  584.                 $this->loop[$key] = apply_filters('avf_masonry_loop_prepare', $this->loop[$key], $this->entries);
  585.             }
  586.         }
  587.        
  588.        
  589.         //fetch new entries
  590.         public function query_entries($params = array(), $ajax = false)
  591.         {
  592.            
  593.             global $avia_config;
  594.  
  595.             if(empty($params)) $params = $this->atts;
  596.  
  597.             if(empty($params['custom_query']))
  598.             {
  599.                 $query = array();
  600.                 $avialable_terms = array();
  601.                
  602.                 if(!empty($params['categories']))
  603.                 {
  604.                     //get the portfolio categories
  605.                     $terms  = explode(',', $params['categories']);
  606.                 }
  607.  
  608.                 $page = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : get_query_var( 'page' );
  609.                 if(!$page || $params['paginate'] == 'no') $page = 1;
  610.  
  611.                
  612.                 //if we find no terms for the taxonomy fetch all taxonomy terms
  613.                 if(empty($terms[0]) || is_null($terms[0]) || $terms[0] === "null")
  614.                 {
  615.                     $terms = array();
  616.                     $allTax = get_terms( $params['taxonomy'] );
  617.                     foreach($allTax as $tax)
  618.                     {
  619.                         if( is_object($tax) )
  620.                         {
  621.                             $terms[] = $tax->term_id;
  622.                         }
  623.                     }
  624.                 }
  625.                
  626.                
  627.                
  628.                 if(!empty($params['taxonomy']))
  629.                 {
  630.                     $allTax = get_terms( $params['taxonomy'] );
  631.                     foreach($allTax as $tax)
  632.                     {
  633.                         if( is_object($tax) )
  634.                         {
  635.                             $avialable_terms[] = $tax->term_id;
  636.                         }
  637.                     }
  638.                 }
  639.                
  640.                
  641.                 //check if any of the terms passed are valid. if not all existing terms are used
  642.                 $valid_terms = array();
  643.                 foreach($terms as $term)
  644.                 {
  645.                     if(in_array($term, $avialable_terms))
  646.                     {
  647.                         $valid_terms[] = $term;
  648.                     }
  649.                 }
  650.                
  651.                 if(!empty($valid_terms))
  652.                 {
  653.                     $terms = $valid_terms;
  654.                     $this->atts['categories'] = implode(",", $terms);
  655.                 }
  656.                 else
  657.                 {
  658.                     $terms = $avialable_terms;
  659.                     $this->atts['categories'] = implode(",", $terms);
  660.                 }
  661.                
  662.                 if(empty($params['post_type'])) $params['post_type'] = get_post_types();
  663.                 if(is_string($params['post_type'])) $params['post_type'] = explode(',', $params['post_type']);
  664.  
  665.  
  666.                 //wordpress 4.4 offset fix. only necessary for ajax loading, therefore we ignore the page param
  667.                 if( $params['offset'] == 0 )
  668.                 {
  669.                     $params['offset'] = false;
  670.                 }
  671.                      
  672.            
  673.                
  674.                 // Meta query - replaced by Tax query in WC 3.0.0
  675.                 $meta_query = array();
  676.                 $tax_query = array();
  677.  
  678.                 // check if taxonomy are set to product or product attributes
  679.                 $tax = get_taxonomy( $params['taxonomy'] );
  680.                
  681.                 if( is_object( $tax ) && isset( $tax->object_type ) && in_array( 'product', (array) $tax->object_type ) && class_exists( 'woocommerce' ) )
  682.                 {
  683.                     $avia_config['woocommerce']['disable_sorting_options'] = true;
  684.                    
  685.                     avia_wc_set_out_of_stock_query_params( $meta_query, $tax_query, $params['wc_prod_visible'] );
  686.                    
  687.                         //  sets filter hooks !!
  688.                     $ordering_args = avia_wc_get_product_query_order_args( $params['prod_order_by'], $params['prod_order'] );
  689.                            
  690.                     $params['query_orderby'] = $ordering_args['orderby'];
  691.                     $params['query_order'] = $ordering_args['order'];
  692.                 }
  693.  
  694.                 if( ! empty( $terms ) )
  695.                 {
  696.                     $tax_query[] =  array(
  697.                                         'taxonomy'  =>  $params['taxonomy'],
  698.                                         'field'     =>  'id',
  699.                                         'terms'     =>  $terms,
  700.                                         'operator'  =>  'IN'
  701.                                 );
  702.                 }
  703.  
  704.                 $query = array( 'orderby'       =>  $params['query_orderby'],
  705.                                 'order'         =>  $params['query_order'],
  706.                                 'paged'         =>  $page,
  707.                                 'post_type'     =>  $params['post_type'],
  708.                                 'post_status'   =>  'publish',
  709.                                 'offset'        =>  $params['offset'],
  710.                                 'posts_per_page' => $params['items'],
  711.                                 'meta_query'    =>  $meta_query,
  712.                                 'tax_query'     =>  $tax_query
  713.                             );
  714.  
  715.  
  716.  
  717.                 if($params['query_orderby'] == 'rand' && isset($_POST['loaded']))
  718.                 {
  719.                     $query['post__not_in'] = $_POST['loaded'];
  720.                     $query['offset'] = false;
  721.                 }                                          
  722.                    
  723.             }
  724.             else
  725.             {
  726.                 $query = $params['custom_query'];
  727.             }
  728.  
  729.  
  730.             $query = apply_filters('avia_masonry_entries_query', $query, $params);
  731.  
  732.             $this->entries = new WP_Query( $query );
  733.             $this->prepare_loop_from_entries( $ajax );
  734.            
  735.             if( function_exists( 'WC' ) )
  736.             {
  737.                 avia_wc_clear_catalog_ordering_args_filters();
  738.                 $avia_config['woocommerce']['disable_sorting_options'] = false;
  739.             }
  740.  
  741.         }
  742.        
  743.        
  744.         public function query_entries_by_id($params = array(), $ajax = false)
  745.         {
  746.             global $avia_config;
  747.  
  748.             if(empty($params)) $params = $this->atts;
  749.            
  750.             $ids = is_array($this->atts['ids']) ? $this->atts['ids'] : array_filter(explode(',',$this->atts['ids']));
  751.            
  752.             $page = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : get_query_var( 'page' );
  753.             if(!$page) $page = 1;
  754.            
  755.             if( $params['offset'] == 0 )
  756.             {
  757.                 $params['offset'] = false;
  758.             }
  759.            
  760.             $query = array(
  761.                 'post__in' => $ids,
  762.                 'post_status' => 'inherit',
  763.                 'post_type' => 'attachment',
  764.                 'post_mime_type' => 'image',
  765.                 'paged'     => $page,
  766.                 'order' => 'ASC',
  767.                 'offset'    => $params['offset'],
  768.                 'posts_per_page' => $params['items'],
  769.                 'orderby' => 'post__in'
  770.             );
  771.            
  772.            
  773.             $query = apply_filters('avia_masonry_entries_query', $query, $params);
  774.  
  775.             $this->entries = new WP_Query( $query );
  776.             $this->prepare_loop_from_entries( $ajax );
  777.            
  778.            
  779.         }
  780.     }
  781. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement