Advertisement
Guest User

productslider.php

a guest
Mar 2nd, 2016
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 18.10 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Post/Page Content
  4.  *
  5.  * Element is in Beta and by default disabled. Todo: test with layerslider elements. currently throws error bc layerslider is only included if layerslider element is detected which is not the case with the post/page element
  6.  */
  7.  
  8. if( !class_exists( 'woocommerce' ) )
  9. {
  10.     add_shortcode('av_productslider', 'avia_please_install_woo');
  11.     return;
  12. }
  13.  
  14.  
  15. if ( !class_exists( 'avia_sc_productslider' )  && class_exists( 'woocommerce' ) )
  16. {
  17.     class avia_sc_productslider extends aviaShortcodeTemplate
  18.     {
  19.  
  20.         /**
  21.          * Create the config array for the shortcode button
  22.          */
  23.         function shortcode_insert_button()
  24.         {
  25.             $this->config['name']       = __('Product Slider', 'avia_framework' );
  26.             $this->config['tab']        = __('Plugin Additions', 'avia_framework' );
  27.             $this->config['icon']       = AviaBuilder::$path['imagesURL']."sc-postslider.png";
  28.             $this->config['order']      = 30;
  29.             $this->config['target']     = 'avia-target-insert';
  30.             $this->config['shortcode']  = 'av_productslider';
  31.             $this->config['tooltip']    = __('Display a Slideshow of Product Entries', 'avia_framework' );
  32.             $this->config['drag-level'] = 3;
  33.         }
  34.  
  35.         /**
  36.          * Popup Elements
  37.          *
  38.          * If this function is defined in a child class the element automatically gets an edit button, that, when pressed
  39.          * opens a modal window that allows to edit the element properties
  40.          *
  41.          * @return void
  42.          */
  43.         function popup_elements()
  44.         {
  45.             $this->elements = array(
  46.  
  47.                 array(
  48.                         "name"  => __("Which Entries?", 'avia_framework' ),
  49.                         "desc"  => __("Select which entries should be displayed by selecting a taxonomy", 'avia_framework' ),
  50.                         "id"    => "categories",
  51.                         "type"  => "select",
  52.                         "taxonomy" => "product_cat",
  53.                         "subtype" => "cat",
  54.                         "multiple"  => 6
  55.                 ),
  56.  
  57.                 array(
  58.                         "name"  => __("Columns", 'avia_framework' ),
  59.                         "desc"  => __("How many columns should be displayed?", 'avia_framework' ),
  60.                         "id"    => "columns",
  61.                         "type"  => "select",
  62.                         "std"   => "3",
  63.                         "subtype" => array( __('2 Columns', 'avia_framework' )=>'2',
  64.                                             __('3 Columns', 'avia_framework' )=>'3',
  65.                                             __('4 Columns', 'avia_framework' )=>'4',
  66.                                             __('5 Columns', 'avia_framework' )=>'5',
  67.                                             )),
  68.                 array(
  69.                         "name"  => __("Entry Number", 'avia_framework' ),
  70.                         "desc"  => __("How many items should be displayed?", 'avia_framework' ),
  71.                         "id"    => "items",
  72.                         "type"  => "select",
  73.                         "std"   => "9",
  74.                         "subtype" => AviaHtmlHelper::number_array(1,100,1, array('All'=>'-1'))),
  75.  
  76.                 array(
  77.                     "name"  => __("Offset Number", 'avia_framework' ),
  78.                     "desc"  => __("The offset determines where the query begins pulling products. Useful if you want to remove a certain number of products because you already query them with another product slider. Attention: Use this option only if the product sorting of the product sliders match!", 'avia_framework' ),
  79.                     "id"    => "offset",
  80.                     "type"  => "select",
  81.                     "std"   => "0",
  82.                     "subtype" => AviaHtmlHelper::number_array(1,100,1, array(__('Deactivate offset','avia_framework')=>'0', __('Do not allow duplicate posts on the entire page (set offset automatically)', 'avia_framework' ) =>'no_duplicates'))),
  83.  
  84.  
  85.                 array(
  86.                         "name"  => __("Sorting Options", 'avia_framework' ),
  87.                         "desc"  => __("Here you can choose how to sort the products", 'avia_framework' ),
  88.                         "id"    => "sort",
  89.                         "type"  => "select",
  90.                         "std"   => "0",
  91.                         "no_first"=>true,
  92.                         "subtype" => array( /*__('Let user pick by displaying a dropdown with sort options (default value is defined at Woocommerce -> Settings -> Catalog)', 'avia_framework' )=>'dropdown', */
  93.                                             __('Use defaut (defined at Woocommerce -> Settings -> Catalog) ', 'avia_framework' ) =>'0',
  94.                                             __('Sort alphabetically', 'avia_framework' ) =>'title',
  95.                                             __('Sort by most recent', 'avia_framework' ) =>'date',
  96.                                             __('Sort by price', 'avia_framework' ) =>'price',
  97.                                             __('Sort by popularity', 'avia_framework' ) =>'popularity')),
  98.  
  99.                 array(
  100.                         "name"  => __("Autorotation active?",'avia_framework' ),
  101.                         "desc"  => __("Check if the slideshow should rotate by default",'avia_framework' ),
  102.                         "id"    => "autoplay",
  103.                         "type"  => "select",
  104.                         "std"   => "no",
  105.                         "subtype" => array(__('Yes','avia_framework' ) =>'yes',__('No','avia_framework' ) =>'no')),
  106.  
  107.                 array(
  108.                     "name"  => __("Slideshow autorotation duration",'avia_framework' ),
  109.                     "desc"  => __("Slideshow will rotate every X seconds",'avia_framework' ),
  110.                     "id"    => "interval",
  111.                     "type"  => "select",
  112.                     "std"   => "5",
  113.                     "required"  => array('autoplay','equals','yes'),
  114.                     "subtype" =>
  115.                     array('3'=>'3','4'=>'4','5'=>'5','6'=>'6','7'=>'7','8'=>'8','9'=>'9','10'=>'10','15'=>'15','20'=>'20','30'=>'30','40'=>'40','60'=>'60','100'=>'100')),
  116.  
  117.  
  118.                 );
  119.         }
  120.  
  121.         /**
  122.          * Editor Element - this function defines the visual appearance of an element on the AviaBuilder Canvas
  123.          * Most common usage is to define some markup in the $params['innerHtml'] which is then inserted into the drag and drop container
  124.          * Less often used: $params['data'] to add data attributes, $params['class'] to modify the className
  125.          *
  126.          *
  127.          * @param array $params this array holds the default values for $content and $args.
  128.          * @return $params the return array usually holds an innerHtml key that holds item specific markup.
  129.          */
  130.         function editor_element($params)
  131.         {
  132.             $params['innerHtml'] = "<img src='".$this->config['icon']."' title='".$this->config['name']."' />";
  133.             $params['innerHtml'].= "<div class='avia-element-label'>".$this->config['name']."</div>";
  134.             $params['content']   = NULL; //remove to allow content elements
  135.             return $params;
  136.         }
  137.  
  138.  
  139.  
  140.         /**
  141.          * Frontend Shortcode Handler
  142.          *
  143.          * @param array $atts array of attributes
  144.          * @param string $content text within enclosing form of shortcode element
  145.          * @param string $shortcodename the shortcode found, when == callback name
  146.          * @return string $output returns the modified html string
  147.          */
  148.         function shortcode_handler($atts, $content = "", $shortcodename = "", $meta = "")
  149.         {
  150.             $atts['class'] = $meta['el_class'];
  151.  
  152.             //fix for seo plugins which execute the do_shortcode() function before the WooCommerce plugin is loaded
  153.             global $woocommerce;
  154.             if(!is_object($woocommerce) || !is_object($woocommerce->query)) return;
  155.  
  156.             $slider = new avia_product_slider($atts);
  157.             $slider->query_entries();
  158.  
  159.             return $slider->html();
  160.         }
  161.  
  162.     }
  163. }
  164.  
  165.  
  166. if ( !class_exists( 'avia_product_slider' ) )
  167. {
  168.     class avia_product_slider
  169.     {
  170.         static  $slide = 0;
  171.         protected $atts;
  172.         protected $entries;
  173.  
  174.         function __construct($atts = array())
  175.         {
  176.  
  177.             $this->atts = shortcode_atts(array( 'type'      => 'slider', // can also be used as grid
  178.                                                 'style'     => '', //no_margin
  179.                                                 'columns'   => '4',
  180.                                                 'items'     => '16',
  181.                                                 'taxonomy'  => 'product_cat',
  182.                                                 'post_type' => 'product',
  183.                                                 'contents'  => 'excerpt',
  184.                                                 'autoplay'  => 'no',
  185.                                                 'animation' => 'fade',
  186.                                                 'paginate'  => 'no',
  187.                                                 'interval'  => 5,
  188.                                                 'class'     => '',
  189.                                                 'sort'      => '',
  190.                                                 'offset' => 0,
  191.                                                 'link_behavior' => '',
  192.                                                 'show_images'   => 'yes',
  193.                                                 'categories'=> array()
  194.                                                 ), $atts, 'av_productslider');
  195.         }
  196.  
  197.         public function html()
  198.         {
  199.             global $woocommerce, $woocommerce_loop, $avia_config;
  200.             $output = "";
  201.  
  202.             avia_post_slider::$slide ++;
  203.             extract($this->atts);
  204.  
  205.             $extraClass         = 'first';
  206.             $grid               = 'one_third';
  207.             $image_size         = 'portfolio';
  208.             $post_loop_count    = 1;
  209.             $loop_counter       = 1;
  210.             $autoplay           = $autoplay == "no" ? false : true;
  211.             $total              = $columns % 2 ? "odd" : "even";
  212.             $woocommerce_loop['columns'] = $columns;
  213.  
  214.             switch($columns)
  215.             {
  216.                 case "1": $grid = 'av_fullwidth';  $image_size = 'large'; break;
  217.                 case "2": $grid = 'av_one_half';   break;
  218.                 case "3": $grid = 'av_one_third';  break;
  219.                 case "4": $grid = 'av_one_fourth'; $image_size = 'portfolio_small'; break;
  220.                 case "5": $grid = 'av_one_fifth';  $image_size = 'portfolio_small'; break;
  221.             }
  222.  
  223.  
  224.             $data = AviaHelper::create_data_string(array('autoplay'=>$autoplay, 'interval'=>$interval, 'animation' =>$animation, 'hoverpause'=>1));
  225.  
  226.                 ob_start();
  227.  
  228.                 if ( have_posts() ) :
  229.  
  230.                 echo "<div {$data} class='template-shop avia-content-slider avia-content-{$type}-active avia-content-slider".avia_post_slider::$slide." avia-content-slider-{$total} {$class} shop_columns_{$columns}' >";
  231.  
  232.                 if($sort == "dropdown") avia_woocommerce_frontend_search_params();
  233.  
  234.                 echo    "<div class='avia-content-slider-inner'>";
  235.  
  236.                             if($type == 'grid') echo '<ul class="products">';
  237.  
  238.                             while ( have_posts() ) : the_post();
  239.                             if($loop_counter == 1 && $type == 'slider') echo '<ul class="products slide-entry-wrap">';
  240.  
  241.                                 woocommerce_get_template_part( 'content', 'product' );
  242.  
  243.                             $loop_counter ++;
  244.                             $post_loop_count ++;
  245.  
  246.                             if($loop_counter > $columns)
  247.                             {
  248.                                 $loop_counter = 1;
  249.                             }
  250.  
  251.                             if($loop_counter == 1 && $type == 'slider')
  252.                             {
  253.                                 echo '</ul>';
  254.                             }
  255.  
  256.                             endwhile; // end of the loop.
  257.  
  258.                             if($loop_counter != 1 || $type == 'grid')
  259.                             {
  260.                                 echo '</ul>';
  261.                             }
  262.  
  263.                 echo    "</div>";
  264.  
  265.                 if($post_loop_count -1 > $columns && $type == 'slider')
  266.                 {
  267.                     echo $this->slide_navigation_arrows();
  268.                 }
  269.  
  270.                 echo "</div>";
  271.  
  272.                 else :
  273.  
  274.                     if(function_exists('woocommerce_product_subcategories')) :
  275.  
  276.                         if ( ! woocommerce_product_subcategories( array( 'before' => '<ul class="products">', 'after' => '</ul>' ) ) ) :
  277.  
  278.                             echo "<p>".__( 'No products found which match your selection.', 'woocommerce' )."</p>";
  279.  
  280.                          endif;
  281.                     endif;
  282.                 endif;
  283.  
  284.             echo '<div class="clear"></div>';
  285.  
  286.             $products = ob_get_clean();
  287.             $output .= $products;
  288.  
  289.             if($paginate == "yes" && $avia_pagination = avia_pagination('', 'nav')) $output .= "<div class='pagination-wrap pagination-slider'>{$avia_pagination}</div>";
  290.  
  291.             wp_reset_query();
  292.             return $output;
  293.         }
  294.  
  295.  
  296.         public function html_list()
  297.         {
  298.             global $woocommerce, $avia_config, $wp_query;
  299.             $output = "";
  300.  
  301.             avia_post_slider::$slide ++;
  302.             extract($this->atts);
  303.  
  304.             $extraClass         = 'first';
  305.             $grid               = 'av_fullwidth';
  306.             $post_loop_count    = 0;
  307.             $loop_counter       = 0;
  308.             $total              = $columns % 2 ? "odd" : "even";
  309.             $posts_per_col      = ceil($wp_query->post_count / $columns);
  310.  
  311.             switch($columns)
  312.             {
  313.                 case "1": $grid = 'av_fullwidth';  break;
  314.                 case "2": $grid = 'av_one_half';   break;
  315.                 case "3": $grid = 'av_one_third';  break;
  316.                 case "4": $grid = 'av_one_fourth'; break;
  317.                 case "5": $grid = 'av_one_fifth';  break;
  318.             }
  319.  
  320.                 ob_start();
  321.  
  322.                 if ( have_posts() ) :
  323.  
  324.                 while ( have_posts() ) : the_post();
  325.  
  326.                 $post_loop_count ++;
  327.                 $loop_counter ++;
  328.                 if($loop_counter === 1)
  329.                 {
  330.                     echo "<div class='{$grid} {$extraClass} flex_column av-catalogue-column'>";
  331.                     echo "<div class='av-catalogue-container av-catalogue-container-woo' >";
  332.                     echo "<ul class='av-catalogue-list'>";
  333.                     $extraClass = "";
  334.                 }
  335.  
  336.                     global $product;
  337.  
  338.                     $link   =   $product->add_to_cart_url();
  339.                     $ajax_class = 'add_to_cart_button product_type_simple';
  340.                     $text   = "";
  341.                     $title  =   get_the_title();
  342.                     $content =  get_the_excerpt();
  343.                     $price =    $product->get_price_html();
  344.                     $rel   = "";
  345.  
  346.                     if(empty($link_behavior))
  347.                     {
  348.                         $cart_url = get_the_permalink();
  349.                         $ajax_class = "";
  350.                     }
  351.                     else
  352.                     {
  353.                         $cart_url = $product->add_to_cart_url();
  354.                         $ajax_class = $product->is_purchasable() ? "add_to_cart_button" : "";
  355.                         $rel = $product->is_purchasable() ? "rel='nofollow'" : "";
  356.                     }
  357.  
  358.                     $image = get_the_post_thumbnail($product->id, 'square', array('class'=>"av-catalogue-image av-cart-update-image av-catalogue-image-{$show_images}"));
  359.  
  360.                     $text .= $image;
  361.                     $text .= "<div class='av-catalogue-item-inner'>";
  362.                     $text .= "<div class='av-catalogue-title-container'><div class='av-catalogue-title av-cart-update-title'>{$title}</div><div class='av-catalogue-price av-cart-update-price'>{$price}</div></div>";
  363.                     $text .= "<div class='av-catalogue-content'>{$content}</div>";
  364.                     $text .= "</div>";
  365.  
  366.                     echo "<li>";
  367.  
  368.                     //coppied from templates/loop/add-to-cart.php - class and rel attr changed, as well as text
  369.  
  370.                     echo apply_filters( 'woocommerce_loop_add_to_cart_link',
  371.                         sprintf( '<a %s href="%s" data-product_id="%s" data-product_sku="%s" class="av-catalogue-item %s product_type_%s">%s</a>',
  372.                             $rel,
  373.                             esc_url( $cart_url ),
  374.                             esc_attr( $product->id ),
  375.                             esc_attr( $product->get_sku() ),
  376.                             $ajax_class,
  377.                             esc_attr( $product->product_type ),
  378.                             $text
  379.                         ),
  380.                     $product );
  381.  
  382.                     echo "</li>";
  383.  
  384.                 if($loop_counter == $posts_per_col || $post_loop_count == $wp_query->post_count)
  385.                 {
  386.                     echo "</ul>";
  387.                     echo "</div>";
  388.                     echo "</div>";
  389.                     $loop_counter = 0;
  390.                 }
  391.  
  392.                 endwhile; // end of the loop.
  393.  
  394.                 endif;
  395.  
  396.             $products = ob_get_clean();
  397.             $output .= $products;
  398.  
  399.             if($paginate == "yes" && $avia_pagination = avia_pagination('', 'nav')) $output .= "<div class='pagination-wrap pagination-slider'>{$avia_pagination}</div>";
  400.  
  401.             wp_reset_query();
  402.             return $output;
  403.         }
  404.  
  405.  
  406.  
  407.         protected function slide_navigation_arrows()
  408.         {
  409.             $html  = "";
  410.             $html .= "<div class='avia-slideshow-arrows avia-slideshow-controls'>";
  411.             $html .=    "<a href='#prev' class='prev-slide' ".av_icon_string('prev_big').">".__('Previous','avia_framework' )."</a>";
  412.             $html .=    "<a href='#next' class='next-slide' ".av_icon_string('next_big').">".__('Next','avia_framework' )."</a>";
  413.             $html .= "</div>";
  414.  
  415.             return $html;
  416.         }
  417.  
  418.         //fetch new entries
  419.         public function query_entries($params = array())
  420.         {
  421.             global $woocommerce, $avia_config;
  422.  
  423.             $query = array();
  424.             if(empty($params)) $params = $this->atts;
  425.  
  426.             if(!empty($params['categories']))
  427.             {
  428.                 //get the product categories
  429.                 $terms  = explode(',', $params['categories']);
  430.             }
  431.  
  432.             $page = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : get_query_var( 'page' );
  433.             if(!$page || $params['type'] == 'slider' || $params['paginate'] == 'no') $page = 1;
  434.  
  435.  
  436.             //if we find no terms for the taxonomy fetch all taxonomy terms
  437.             if(empty($terms[0]) || is_null($terms[0]) || $terms[0] === "null")
  438.             {
  439.                 $terms = array();
  440.                 $allTax = get_terms( $params['taxonomy']);
  441.                 foreach($allTax as $tax)
  442.                 {
  443.                     $terms[] = $tax->term_id;
  444.                 }
  445.             }
  446.  
  447.  
  448.             if($params['sort'] == 'dropdown')
  449.             {
  450.                 $avia_config['woocommerce']['default_posts_per_page'] = $params['items'];
  451.                 $ordering   = $woocommerce->query->get_catalog_ordering_args();
  452.                 $order      = $ordering['order'];
  453.                 $orderBY    = $ordering['orderby'];
  454.  
  455.                 if(!empty($avia_config['shop_overview_products_overwritten']))
  456.                     $params['items'] = $avia_config['shop_overview_products'];
  457.  
  458.             }
  459.             else
  460.             {
  461.                 $avia_config['woocommerce']['disable_sorting_options'] = true;
  462.  
  463.                 $order = "DESC";
  464.                 if(empty($params['sort']) || $params['sort'] == "0")
  465.                 {
  466.                     $ordering   = $woocommerce->query->get_catalog_ordering_args();
  467.                     $order      = $ordering['order'];
  468.                     $orderBY    = $ordering['orderby'];
  469.                 }
  470.                 else
  471.                 {
  472.                     $orderBY = $params['sort'];
  473.                 }
  474.  
  475.                 if(!$orderBY) $orderBY = "menu_order";
  476.  
  477.                 if($orderBY == 'price' || $orderBY == 'title'){ $order = "ASC"; }
  478.             }
  479.  
  480.  
  481.             if($params['offset'] == 'no_duplicates')
  482.             {
  483.                 $params['offset'] = 0;
  484.                 $no_duplicates = true;
  485.             }
  486.  
  487.             //wordpress 4.4 offset fix
  488.             if( $params['offset'] == 0 )
  489.             {
  490.                 $params['offset'] = false;
  491.             }
  492.             else
  493.             {
  494.                 //if the offset is set the paged param is ignored. therefore we need to factor in the page number
  495.                 $params['offset'] = $params['offset'] + ( ( $page - 1 ) * $params['items']);
  496.             }
  497.  
  498.  
  499.             // Meta query
  500.             $meta_query = array();
  501.             $meta_query[] = $woocommerce->query->visibility_meta_query();
  502.                 $meta_query[] = $woocommerce->query->stock_status_meta_query();
  503.             $meta_query   = array_filter( $meta_query );
  504.  
  505.             $ordering_args = $woocommerce->query->get_catalog_ordering_args( $orderBY, $order );
  506.  
  507.  
  508.             $query = array(
  509.                 'post_type'     => $params['post_type'],
  510.                 'post_status'   => 'publish',
  511.                 'ignore_sticky_posts'   => 1,
  512.                 'paged'         => $page,
  513.                         'offset'                => !(int)$params['offset'] ? "" : $params['offset'],
  514.                         'post__not_in' => (!empty($no_duplicates)) ? $avia_config['posts_on_current_page'] : array(),
  515.  
  516.                 'posts_per_page'    => $params['items'],
  517.                 'orderby'       => $ordering_args['orderby'],
  518.                 'order'         => $ordering_args['order'],
  519.                 'meta_query'        => $meta_query
  520.                                                 );
  521.             if(!empty($terms))
  522.             {
  523.                 $query['tax_query'] =  array(   array(  'taxonomy'  => $params['taxonomy'],
  524.                                                 'field'     => 'id',
  525.                                                 'terms'     => $terms,
  526.                                                 'operator'  => 'IN')
  527.                                                 );
  528.             }
  529.  
  530.  
  531.             if ( isset( $ordering_args['meta_key'] ) ) {
  532.                 $query['meta_key'] = $ordering_args['meta_key'];
  533.             }
  534.  
  535.             $query = apply_filters('avia_product_slide_query', $query, $params);
  536.  
  537.  
  538.             query_posts($query);
  539.  
  540.                 remove_filter( 'posts_clauses', array( $woocommerce->query, 'order_by_popularity_post_clauses' ) );
  541.             remove_filter( 'posts_clauses', array( $woocommerce->query, 'order_by_rating_post_clauses' ) );
  542.  
  543.             // store the queried post ids in
  544.             if( have_posts() )
  545.             {
  546.                 while( have_posts() )
  547.                 {
  548.                     the_post();
  549.                     $avia_config['posts_on_current_page'][] = get_the_ID();
  550.                 }
  551.             }
  552.         }
  553.     }
  554. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement