PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/fusion-core/shortcodes/class-woo-product-slider.php

https://gitlab.com/webkod3r/tripolis
PHP | 217 lines | 154 code | 46 blank | 17 comment | 31 complexity | 09c3b746061813753157d9ed91204e3b MD5 | raw file
  1. <?php
  2. class FusionSC_WooProductSlider {
  3. public static $args;
  4. /**
  5. * Initiate the shortcode
  6. */
  7. public function __construct() {
  8. add_filter( 'fusion_attr_woo-product-slider-shortcode', array( $this, 'attr' ) );
  9. add_filter( 'fusion_attr_woo-product-slider-shortcode-carousel', array( $this, 'carousel_attr' ) );
  10. add_filter( 'fusion_attr_woo-product-slider-shortcode-img-div', array( $this, 'img_div_attr' ) );
  11. add_shortcode( 'products_slider', array( $this, 'render' ) );
  12. }
  13. /**
  14. * Render the shortcode
  15. *
  16. * @param array $args Shortcode paramters
  17. * @param string $content Content between shortcode
  18. *
  19. * @return string HTML output
  20. */
  21. function render( $args, $content = '' ) {
  22. global $woocommerce, $smof_data;
  23. $defaults = FusionCore_Plugin::set_shortcode_defaults(
  24. array(
  25. 'class' => '',
  26. 'id' => '',
  27. 'autoplay' => 'no',
  28. 'carousel_layout' => 'title_on_rollover',
  29. 'cat_slug' => '',
  30. 'columns' => '5',
  31. 'column_spacing' => '13',
  32. 'mouse_scroll' => 'no',
  33. 'number_posts' => 10,
  34. 'picture_size' => 'fixed',
  35. 'scroll_items' => '',
  36. 'show_buttons' => 'yes',
  37. 'show_cats' => 'yes',
  38. 'show_nav' => 'yes',
  39. 'show_price' => 'yes',
  40. ), $args
  41. );
  42. ( $defaults['show_cats'] == "yes" ) ? ( $defaults['show_cats'] = 'enable' ) : ( $defaults['show_cats'] = 'disable' );
  43. ( $defaults['show_price'] == "yes" ) ? ( $defaults['show_price'] = true ) : ( $defaults['show_price'] = false );
  44. ( $defaults['show_buttons'] == "yes" ) ? ( $defaults['show_buttons'] = true ) : ( $defaults['show_buttons'] = false );
  45. extract( $defaults );
  46. self::$args = $defaults;
  47. $html = '';
  48. $buttons = '';
  49. if ( class_exists( 'Woocommerce' ) ) {
  50. $items_in_cart = array();
  51. if ( $woocommerce->cart && $woocommerce->cart->get_cart() && is_array( $woocommerce->cart->get_cart() ) ) {
  52. foreach ( $woocommerce->cart->get_cart() as $cart ) {
  53. $items_in_cart[] = $cart['product_id'];
  54. }
  55. }
  56. $design_class = 'fusion-' . Avada()->settings->get( 'woocommerce_product_box_design' ) . '-product-image-wrapper';
  57. $number_posts = (int) $number_posts;
  58. $args = array(
  59. 'post_type' => 'product',
  60. 'posts_per_page' => $number_posts,
  61. 'meta_query' => array(
  62. array(
  63. 'key' => '_thumbnail_id',
  64. 'compare' => '!=',
  65. 'value' => null
  66. )
  67. )
  68. );
  69. if ( $cat_slug ) {
  70. $cat_id = explode( '|', $cat_slug );
  71. $args['tax_query'] =
  72. array(
  73. array(
  74. 'taxonomy' => 'product_cat',
  75. 'field' => 'slug',
  76. 'terms' => $cat_id
  77. )
  78. );
  79. }
  80. if ( $picture_size == 'fixed' ) {
  81. $featured_image_size = 'related-img';
  82. } else {
  83. $featured_image_size = 'full';
  84. }
  85. $products = new WP_Query( $args );
  86. $product_list = '';
  87. if ( $products->have_posts() ) {
  88. while ( $products->have_posts() ) {
  89. $products->the_post();
  90. $id = get_the_ID();
  91. $in_cart = in_array( $id, $items_in_cart );
  92. $image = $price_tag = $terms = '';
  93. // Title on rollover layout
  94. if ( $carousel_layout == 'title_on_rollover' ) {
  95. $image = avada_render_first_featured_image_markup( get_the_ID(), $featured_image_size, get_permalink( get_the_ID() ), TRUE, $show_price, $show_buttons, $show_cats );
  96. // Title below image layout
  97. } else {
  98. if ( $show_buttons == 'yes' ) {
  99. $image = avada_render_first_featured_image_markup( get_the_ID(), $featured_image_size, get_permalink( get_the_ID() ), TRUE, FALSE, $show_buttons, 'disable', 'disable' );
  100. } else {
  101. $image = avada_render_first_featured_image_markup( get_the_ID(), $featured_image_size, get_permalink( get_the_ID() ), TRUE, FALSE, $show_buttons, 'disable', 'disable', '', '', 'no' );
  102. }
  103. // Get the post title
  104. $image .= sprintf( '<h4 %s><a href="%s" target="%s">%s</a></h4>', FusionCore_Plugin::attributes( 'fusion-carousel-title' ), get_permalink( get_the_ID() ), '_self', get_the_title() );
  105. $image .= '<div class="fusion-carousel-meta">';
  106. // Get the terms
  107. if ( $show_cats == 'enable' ) {
  108. $image .= get_the_term_list( get_the_ID(), 'product_cat', '', ', ', '' );
  109. }
  110. // Check if we should render the woo product price
  111. if ( $show_price ) {
  112. ob_start();
  113. woocommerce_get_template( 'loop/price.php' );
  114. $image .= sprintf( '<div class="fusion-carousel-price">%s</div>', ob_get_clean() );
  115. }
  116. $image .= '</div>';
  117. }
  118. if( $in_cart ) {
  119. $product_list .= sprintf( '<li %s><div class="%s"><div %s>%s</div></div></li>', FusionCore_Plugin::attributes( 'fusion-carousel-item' ), $design_class . ' fusion-item-in-cart', FusionCore_Plugin::attributes( 'fusion-carousel-item-wrapper' ), $image );
  120. } else {
  121. $product_list .= sprintf( '<li %s><div class="%s"><div %s>%s</div></div></li>', FusionCore_Plugin::attributes( 'fusion-carousel-item' ), $design_class, FusionCore_Plugin::attributes( 'fusion-carousel-item-wrapper' ), $image );
  122. }
  123. }
  124. }
  125. wp_reset_query();
  126. $html = sprintf( '<div %s>', FusionCore_Plugin::attributes( 'woo-product-slider-shortcode' ) );
  127. $html .= sprintf( '<div %s>', FusionCore_Plugin::attributes( 'woo-product-slider-shortcode-carousel' ) );
  128. $html .= sprintf( '<div %s>', FusionCore_Plugin::attributes( 'fusion-carousel-positioner' ) );
  129. $html .= sprintf( '<ul %s>', FusionCore_Plugin::attributes( 'fusion-carousel-holder' ) );
  130. $html .= $product_list;
  131. $html .= '</ul>';
  132. // Check if navigation should be shown
  133. if ( $show_nav == 'yes' ) {
  134. $html .= sprintf( '<div %s><span %s></span><span %s></span></div>', FusionCore_Plugin::attributes( 'fusion-carousel-nav' ),
  135. FusionCore_Plugin::attributes( 'fusion-nav-prev' ), FusionCore_Plugin::attributes( 'fusion-nav-next' ) );
  136. }
  137. $html .= '</div>';
  138. $html .= '</div>';
  139. $html .= '</div>';
  140. }
  141. return $html;
  142. }
  143. function attr() {
  144. $attr['class'] = 'fusion-woo-product-slider fusion-woo-slider';
  145. if ( self::$args['class'] ) {
  146. $attr['class'] .= ' ' . self::$args['class'];
  147. }
  148. if ( self::$args['id'] ) {
  149. $attr['id'] = self::$args['id'];
  150. }
  151. return $attr;
  152. }
  153. function carousel_attr() {
  154. $attr['class'] = 'fusion-carousel';
  155. if ( self::$args['carousel_layout'] == 'title_below_image' ) {
  156. $attr['class'] .= ' fusion-carousel-title-below-image';
  157. $attr['data-metacontent'] = 'yes';
  158. } else {
  159. $attr['class'] .= ' fusion-carousel-title-on-rollover';
  160. }
  161. $attr['data-autoplay'] = self::$args['autoplay'];
  162. $attr['data-columns'] = self::$args['columns'];
  163. $attr['data-itemmargin'] = self::$args['column_spacing'];
  164. $attr['data-itemwidth'] = 180;
  165. $attr['data-touchscroll'] = self::$args['mouse_scroll'];
  166. $attr['data-imagesize'] = self::$args['picture_size'];
  167. $attr['data-scrollitems'] = self::$args['scroll_items'];
  168. return $attr;
  169. }
  170. }
  171. new FusionSC_WooProductSlider();