Advertisement
Guest User

av-helper-masonry.php

a guest
Mar 9th, 2021
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 28.68 KB | None | 0 0
  1. <?php
  2. /**
  3. * Helper for masonry
  4. *
  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. if($size == 'flex') $img_html = '<img src="'.$attachment[0].'" title="'.$title.'" alt="'.$alt.'" />';
  330. if($size == 'fixed') $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}>".__('Εμφανίστε περισσότερα','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. $this->loop[$key]['text_before'] = "";
  496. $this->loop[$key]['text_after'] = "";
  497. $this->loop[$key]['ID'] = $id = $entry->ID;
  498. $this->loop[$key]['post_type'] = $entry->post_type;
  499. $this->loop[$key]['thumb_ID'] = get_post_thumbnail_id($id);
  500. $this->loop[$key]['the_title'] = get_the_title($id);
  501. $this->loop[$key]['url'] = get_permalink($id);
  502. $this->loop[$key]['date'] = "<span class='av-masonry-date meta-color updated'>".get_the_time($date_format, $id)."</span>";
  503. $this->loop[$key]['author'] = "<span class='av-masonry-author meta-color vcard author'><span class='fn'>". __('by','avia_framework') .' '. $author."</span></span>";
  504. $this->loop[$key]['class'] = get_post_class("av-masonry-entry isotope-item", $id);
  505. $this->loop[$key]['content'] = strip_tags( $entry->post_excerpt );
  506. $this->loop[$key]['description'] = !empty($entry->post_content) ? $entry->post_content : $entry->post_excerpt;
  507.  
  508. if(empty($this->loop[$key]['content']))
  509. {
  510. if($ajax)
  511. {
  512. $entry->post_content = preg_replace("!\[.*?\]!", "", $entry->post_content);
  513. }
  514.  
  515. $this->loop[$key]['content'] = avia_backend_truncate($entry->post_content, apply_filters( 'avf_masonry_excerpt_length' , 60) , apply_filters( 'avf_masonry_excerpt_delimiter' , " "), "…", true, '');
  516. }
  517.  
  518. $this->loop[$key]['content'] = nl2br( trim($this->loop[$key]['content']) );
  519.  
  520. //post type specific
  521. switch($entry->post_type)
  522. {
  523. case 'post':
  524.  
  525. $post_format = get_post_format($id) ? get_post_format($id) : 'standard';
  526. $this->loop[$key] = apply_filters( 'post-format-'.$post_format, $this->loop[$key] );
  527. $this->loop[$key]['text_after'] .= $this->loop[$key]['date'];
  528. $this->loop[$key]['text_after'] .= '<span class="av-masonry-text-sep text-sep-author">/</span>';
  529. $this->loop[$key]['text_after'] .= $this->loop[$key]['author'];
  530.  
  531. switch($post_format)
  532. {
  533. case 'quote' :
  534. case 'link' :
  535. case 'image' :
  536. case 'gallery' :
  537. if(!$this->loop[$key]['thumb_ID'])
  538. {
  539. $this->loop[$key]['text_before'] = av_icon_display($post_format);
  540. }
  541. break;
  542.  
  543. case 'audio' :
  544. case 'video' :
  545. if(!$this->loop[$key]['thumb_ID'])
  546. {
  547. $this->loop[$key]['text_before'] = av_icon_display($post_format);
  548. }
  549. else
  550. {
  551. $this->loop[$key]['text_before'] = av_icon_display($post_format, 'av-masonry-media');
  552. }
  553. break;
  554. }
  555.  
  556.  
  557.  
  558. break;
  559.  
  560. case 'portfolio':
  561.  
  562. //set portfolio breadcrumb navigation
  563. if($this->atts['set_breadcrumb'] && is_page()) $_SESSION["avia_{$entry->post_type}"] = get_the_ID();
  564.  
  565. //check if the user has set up a custom link
  566. if(!post_password_required($id)){
  567. $custom_link = get_post_meta( $id ,'_portfolio_custom_link', true) != "" ? get_post_meta( $id ,'_portfolio_custom_link_url', true) : false;
  568. if($custom_link) $this->loop[$key]['url'] = $custom_link;
  569. }
  570. break;
  571.  
  572.  
  573. case 'attachment':
  574.  
  575. $custom_url = get_post_meta( $id, 'av-custom-link', true );
  576. $this->loop[$key]['thumb_ID'] = $id;
  577. $this->loop[$key]['content'] = $entry->post_excerpt;
  578.  
  579. if($custom_url)
  580. {
  581. $this->loop[$key]['url'] = $custom_url;
  582. }
  583. else
  584. {
  585. $this->loop[$key]['url'] = wp_get_attachment_image_src($id, apply_filters('avf_avia_builder_masonry_lightbox_img_size','large'));
  586. $this->loop[$key]['url'] = reset($this->loop[$key]['url']);
  587. }
  588.  
  589.  
  590. break;
  591.  
  592. case 'product':
  593. //check if woocommerce is enabled in the first place so we can use woocommerce functions
  594. if(function_exists('avia_woocommerce_enabled') && avia_woocommerce_enabled())
  595. {
  596. $tagTax = "product_tag";
  597. $product = function_exists('wc_get_product') ? wc_get_product($id) : get_product( $id );
  598. $overlay_img = avia_woocommerce_gallery_first_thumbnail($id, $img_size, true);
  599.  
  600. $this->loop[$key]['text_after'] .= '<span class="av-masonry-price price">'.$product->get_price_html()."</span>";
  601. if($product->is_on_sale( )) $this->loop[$key]['text_after'] .= '<span class="onsale">'.__( 'Sale!', 'avia_framework' ).'</span>';
  602. }
  603. break;
  604. }
  605.  
  606.  
  607. //check if post is password protected
  608. if(post_password_required($id))
  609. {
  610. $this->loop[$key]['content'] = "";
  611. $this->loop[$key]['class'][] = "entry-protected";
  612. $this->loop[$key]['thumb_ID'] = "";
  613. $this->loop[$key]['text_before'] = av_icon_display('closed');
  614. $this->loop[$key]['text_after'] = $this->loop[$key]['date'];
  615. }
  616.  
  617.  
  618.  
  619. //set the html tags. depending on the link settings use either an a tag or a div tag
  620. if(!empty($this->atts['container_links']) || !empty($custom_url))
  621. {
  622. $this->loop[$key]['html_tags'] = array('a href="'.$this->loop[$key]['url'].'"','a'); //opening and closing tag for the masonry container
  623. }
  624. else
  625. {
  626. $this->loop[$key]['html_tags'] = array('div','div');
  627. }
  628.  
  629.  
  630. //get post tags
  631. $this->loop[$key]['tags'] = wp_get_post_terms($id, $tagTax, array( 'fields' => 'slugs' ));
  632.  
  633. //check if the image got landscape as well as portrait class applied. in that case use a bigger image size
  634. if(strlen($this->ratio_check_by_tag($this->loop[$key]['tags'])) > 20) $img_size = 'extra_large';
  635.  
  636. //get attachment data
  637. $this->loop[$key]['attachment'] = !empty($this->loop[$key]['thumb_ID']) ? wp_get_attachment_image_src($this->loop[$key]['thumb_ID'], $img_size) : "";
  638.  
  639. //get overlay attachment in case the overlay is set
  640. $this->loop[$key]['attachment_overlay'] = !empty($overlay_img) ? wp_get_attachment_image_src($overlay_img, $img_size) : "";
  641.  
  642. //apply filter for other post types, in case we want to use them and display additional/different information
  643. $this->loop[$key] = apply_filters('avf_masonry_loop_prepare', $this->loop[$key], $this->entries);
  644. }
  645. }
  646.  
  647.  
  648. //fetch new entries
  649. public function query_entries($params = array(), $ajax = false)
  650. {
  651.  
  652. global $avia_config;
  653.  
  654. if(empty($params)) $params = $this->atts;
  655.  
  656. if(empty($params['custom_query']))
  657. {
  658. $query = array();
  659. $avialable_terms = array();
  660.  
  661. if(!empty($params['categories']))
  662. {
  663. //get the portfolio categories
  664. $terms = explode(',', $params['categories']);
  665. }
  666.  
  667. $page = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : get_query_var( 'page' );
  668. if(!$page || $params['paginate'] == 'no') $page = 1;
  669.  
  670.  
  671. //if we find no terms for the taxonomy fetch all taxonomy terms
  672. if(empty($terms[0]) || is_null($terms[0]) || $terms[0] === "null")
  673. {
  674. $terms = array();
  675. $allTax = get_terms( $params['taxonomy'] );
  676. foreach($allTax as $tax)
  677. {
  678. if( is_object($tax) )
  679. {
  680. $terms[] = $tax->term_id;
  681. }
  682. }
  683. }
  684.  
  685.  
  686.  
  687. if(!empty($params['taxonomy']))
  688. {
  689. $allTax = get_terms( $params['taxonomy'] );
  690. foreach($allTax as $tax)
  691. {
  692. if( is_object($tax) )
  693. {
  694. $avialable_terms[] = $tax->term_id;
  695. }
  696. }
  697. }
  698.  
  699.  
  700. //check if any of the terms passed are valid. if not all existing terms are used
  701. $valid_terms = array();
  702. foreach($terms as $term)
  703. {
  704. if(in_array($term, $avialable_terms))
  705. {
  706. $valid_terms[] = $term;
  707. }
  708. }
  709.  
  710. if(!empty($valid_terms))
  711. {
  712. $terms = $valid_terms;
  713. $this->atts['categories'] = implode(",", $terms);
  714. }
  715. else
  716. {
  717. $terms = $avialable_terms;
  718. $this->atts['categories'] = implode(",", $terms);
  719. }
  720.  
  721. if(empty($params['post_type'])) $params['post_type'] = get_post_types();
  722. if(is_string($params['post_type'])) $params['post_type'] = explode(',', $params['post_type']);
  723.  
  724.  
  725. //wordpress 4.4 offset fix. only necessary for ajax loading, therefore we ignore the page param
  726. if( $params['offset'] == 0 )
  727. {
  728. $params['offset'] = false;
  729. }
  730.  
  731.  
  732.  
  733. // Meta query - replaced by Tax query in WC 3.0.0
  734. $meta_query = array();
  735. $tax_query = array();
  736.  
  737. // check if taxonomy are set to product or product attributes
  738. $tax = get_taxonomy( $params['taxonomy'] );
  739.  
  740. if( class_exists( 'WooCommerce' ) && is_object( $tax ) && isset( $tax->object_type ) && in_array( 'product', (array) $tax->object_type ) )
  741. {
  742. $avia_config['woocommerce']['disable_sorting_options'] = true;
  743.  
  744. avia_wc_set_out_of_stock_query_params( $meta_query, $tax_query, $params['wc_prod_visible'] );
  745.  
  746. // sets filter hooks !!
  747. $ordering_args = avia_wc_get_product_query_order_args( $params['prod_order_by'], $params['prod_order'] );
  748.  
  749. $params['query_orderby'] = $ordering_args['orderby'];
  750. $params['query_order'] = $ordering_args['order'];
  751. }
  752.  
  753. if( ! empty( $terms ) )
  754. {
  755. $tax_query[] = array(
  756. 'taxonomy' => $params['taxonomy'],
  757. 'field' => 'id',
  758. 'terms' => $terms,
  759. 'operator' => 'IN'
  760. );
  761. }
  762.  
  763. $query = array( 'orderby' => $params['query_orderby'],
  764. 'order' => $params['query_order'],
  765. 'paged' => $page,
  766. 'post_type' => $params['post_type'],
  767. 'post_status' => 'publish',
  768. 'offset' => $params['offset'],
  769. 'posts_per_page' => $params['items'],
  770. 'meta_query' => $meta_query,
  771. 'tax_query' => $tax_query
  772. );
  773.  
  774.  
  775.  
  776. if($params['query_orderby'] == 'rand' && isset($_POST['loaded']))
  777. {
  778. $query['post__not_in'] = $_POST['loaded'];
  779. $query['offset'] = false;
  780. }
  781.  
  782. }
  783. else
  784. {
  785. $query = $params['custom_query'];
  786. }
  787.  
  788.  
  789. /**
  790. * @used_by avia_remove_bbpress_post_type_from_query 10 (bbPress)
  791. * avia_translate_ids_from_query 10 (WPML)
  792. * avia_events_modify_recurring_event_query 10 (Tribe Events Pro)
  793. */
  794. $query = apply_filters('avia_masonry_entries_query', $query, $params);
  795.  
  796. $this->entries = new WP_Query( $query );
  797.  
  798. /**
  799. * @used_by avia_events_modify_recurring_event_query 10 (Tribe Events Pro)
  800. *
  801. * @added_by Günter
  802. * @since 4.2.4
  803. */
  804. do_action( 'ava_after_masonry_entries_query' );
  805.  
  806. $this->prepare_loop_from_entries( $ajax );
  807.  
  808. if( function_exists( 'WC' ) )
  809. {
  810. avia_wc_clear_catalog_ordering_args_filters();
  811. $avia_config['woocommerce']['disable_sorting_options'] = false;
  812. }
  813.  
  814. }
  815.  
  816.  
  817. public function query_entries_by_id($params = array(), $ajax = false)
  818. {
  819. global $avia_config;
  820.  
  821. if(empty($params)) $params = $this->atts;
  822.  
  823. $ids = is_array($this->atts['ids']) ? $this->atts['ids'] : array_filter(explode(',',$this->atts['ids']));
  824.  
  825. $page = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : get_query_var( 'page' );
  826. if(!$page) $page = 1;
  827.  
  828. if( $params['offset'] == 0 )
  829. {
  830. $params['offset'] = false;
  831. }
  832.  
  833. $query = array(
  834. 'post__in' => $ids,
  835. 'post_status' => 'inherit',
  836. 'post_type' => 'attachment',
  837. 'post_mime_type' => 'image',
  838. 'paged' => $page,
  839. 'order' => 'ASC',
  840. 'offset' => $params['offset'],
  841. 'posts_per_page' => $params['items'],
  842. 'orderby' => 'post__in'
  843. );
  844.  
  845. /**
  846. * @used_by avia_remove_bbpress_post_type_from_query 10 (bbPress)
  847. * avia_translate_ids_from_query 10 (WPML)
  848. * avia_events_modify_recurring_event_query 10 (Tribe Events Pro)
  849. */
  850. $query = apply_filters('avia_masonry_entries_query', $query, $params);
  851.  
  852. $this->entries = new WP_Query( $query );
  853.  
  854. /**
  855. * @used_by avia_events_modify_recurring_event_query 10 (Tribe Events Pro)
  856. *
  857. * @added_by Günter
  858. * @since 4.2.4
  859. */
  860. do_action( 'ava_after_masonry_entries_query' );
  861.  
  862. $this->prepare_loop_from_entries( $ajax );
  863.  
  864.  
  865. }
  866. }
  867. }
  868.  
  869.  
  870.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement