Advertisement
cipher87

Masonry replace date with category

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