Advertisement
Guest User

Untitled

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