<?php
/**
 * YP Carousel
 *
 * Example:
 * [yp_posts_carousel style="1" post_type="ids" posts="1,2,3" exclude_ids="" custom_query="" show_price="true" show_rating="true" show_discount_badges="true" badges_always_show="false" boxed="false"]
 */
add_shortcode("yp_posts_carousel", "yp_posts_carousel");
function yp_posts_carousel($atts, $content = null) {
    extract(shortcode_atts(array(
        "style"               => 1,
        "post_type"           => "ids",
        "posts"               => "",
        "exclude_ids"         => "",
        "custom_query"        => "",
        "show_price"          => true,
        "show_rating"         => true,
        "show_discount_badges"=> true,
        "badges_always_show"  => false,
        "boxed"               => false,
        "class"               => ""
    ), $atts));

    $posts = explode( ",", $posts );
    $exclude_ids = explode( ",", $exclude_ids );

    if(empty($posts)) {
      return "";
    }
    
    if(yp_check($boxed)) {
      $class .= " container";
    }

    $query_opts = array();

    if($exclude_ids) {
      $query_opts['post__not_in'] = $exclude_ids;
    }

    if($post_type == 'ids') {
      $query_opts['post_type'] = 'any';
      $query_opts['post__in'] = $posts;
    } else if($post_type == 'custom_query') {
      $tmp_arr = array();
      parse_str( html_entity_decode($custom_query), $tmp_arr );
      $query_opts = array_merge($query_opts, $tmp_arr);
    } else {
      $query_opts['post_type'] = $post_type;
    }

    $result_items = '';
    $yp_query = new WP_Query($query_opts);

    while ($yp_query->have_posts()) : $yp_query->the_post();
      global $product;

      $ID = get_the_ID();

      $title = "<h4>" . get_the_title() . "</h4>";
      $rating = '';
      $price = '';
      $badge = '';

      $img_src = get_post_thumbnail_id( $ID );
      $img_src = wp_get_attachment_image_src( $img_src, '500x375' );
      $img_src = $img_src[0];

      // use no-image
      if(!$img_src) {
        $img_src = yp_opts('single_post_noimage');
      }

      if ($product) {
        if(yp_check($show_rating)) {
          $rating = yp_get_rating( $product->get_average_rating() );
        }

        if(yp_check($show_discount_badges) && function_exists('yp_woo_discount_badge')) {
          $badge = yp_woo_discount_badge($product, yp_check($badges_always_show));
        }

        if(yp_check($show_price) && $price = $product->get_price_html()) {
          $price = '<div class="price">' . $price . '</div>';
        }
      }

      $item_content = '';
      if($style == 1) {
        $item_content = 
          '<a class="angled-img" href="' . esc_url(get_permalink()) . '">
            <div class="img">
              <img src="' . esc_url($img_src) . '" alt="">
              ' . $badge . '
            </div>
            <div class="over-info">
              <div>
                <div>
                  ' . $title . '
                  ' . $rating . '
                  ' . $price . '
                </div>
              </div>
            </div>
          </a>';
      } else {
        $description = '';
        if( yp_check($price) && yp_check($rating) ) {
          $description =
            '<div class="row">
              <div class="col-xs-6">
                ' . $rating . '
              </div>
              <div class="col-xs-6">
                ' . $price . '
              </div>
            </div>';
        } else if( yp_check($price) ) {
          $description = $price;
        } else if( yp_check($rating) ) {
          $description = $rating;
        }

        $item_content = 
          '<a class="angled-img" href="' . esc_url(get_permalink()) . '">
            <div class="img img-offset">
              <img src="' . esc_url($img_src) . '" alt="">
              ' . $badge . '
            </div>
            <div class="bottom-info">
              ' . $title . '
              ' . $description . '
            </div>
          </a>';
      }
      
      $result_items .= $item_content;

    endwhile;

    wp_reset_postdata();

    return '<div class="youplay-carousel ' . yp_sanitize_class($class) . '">' . $result_items . '</div>';
}


/* Add VC Shortcode */
add_action( "init", "vc_youplay_posts_carousel" );
function vc_youplay_posts_carousel() {
    if(function_exists("vc_map")) {

        $post_types = get_post_types( array() );
        $post_types_list = array();
        if ( is_array( $post_types ) && ! empty( $post_types ) ) {
          foreach ( $post_types as $post_type ) {
            if ( $post_type !== "revision" && $post_type !== "nav_menu_item"/* && $post_type !== "attachment"*/ ) {
              $label = ucfirst( $post_type );
              $post_types_list[] = array( $post_type, __( $label, "youplay" ) );
            }
          }
        }
        $post_types_list[] = array( "custom_query", __( "Custom Query", "youplay" ) );
        $post_types_list[] = array( "ids", __( "List of IDs", "youplay" ) );

        /* Register shortcode with Visual Composer */
        vc_map( array(
           "name"     => __("YP Posts Carousel", "youplay"),
           "base"     => "yp_posts_carousel",
           "controls" => "full",
           "category" => "Youplay",
           "icon"     => "icon-youplay",
           "params"   => array(
              array(
                 "type"       => "dropdown",
                 "heading"    => __("Style", "youplay"),
                 "param_name" => "style",
                 "value"      => array(
                    __("Style 1", "youplay") => 1,
                    __("Style 2", "youplay") => 2
                 ),
                 "description" => ""
              ),
              array(
                "type"        => "dropdown",
                "heading"     => __( "Data source", "youplay" ),
                "param_name"  => "post_type",
                "value"       => $post_types_list,
                "std"         => "ids",
                "description" => __( "Select content type", "youplay" )
              ),
              array(
                 "type"        => "textfield",
                 "heading"     => __("IDs", "youplay"),
                 "param_name"  => "posts",
                 "value"       => __("", "youplay"),
                 "description" => "Type here the posts, pages, etc. IDs you want to use separated by coma. ex: 23,24,25",
                "dependency"  => array(
                  "element"   => "post_type",
                  "value"     => array( "ids" ),
                ),
              ),
              array(
                "type"        => "textarea_safe",
                "heading"     => __( "Custom Query", "youplay" ),
                "param_name"  => "custom_query",
                "description" => __( "Build custom query according to <a href='http://codex.wordpress.org/Function_Reference/query_posts'>WordPress Codex</a>.", "youplay" ),
                "dependency"  => array(
                  "element"   => "post_type",
                  "value"     => array( "custom_query" ),
                ),
              ),
              array(
                "type"        => "textfield",
                "heading"     => __( "Exclude IDs", "youplay" ),
                "param_name"  => "exclude_ids",
                "description" => __( "Type here the posts, pages, etc. IDs you want to use separated by coma. ex: 23,24,25", "youplay" ),
              ),
              array(
                 "type"        => "checkbox",
                 "heading"     => __("Show Price", "youplay"),
                 "param_name"  => "show_price",
                 "value"       => array( __( "", "youplay" ) => true ),
                 "description" => "",
              ),
              array(
                 "type"        => "checkbox",
                 "heading"     => __("Show Rating", "youplay"),
                 "param_name"  => "show_rating",
                 "value"       => array( __( "", "youplay" ) => true ),
                 "description" => "",
              ),
              array(
                 "type"        => "checkbox",
                 "heading"     => __("Show Discount Badges", "youplay"),
                 "param_name"  => "show_discount_badges",
                 "value"       => array( __( "", "youplay" ) => true ),
                 "description" => "",
              ),
              array(
                 "type"        => "checkbox",
                 "heading"     => __("Badges Always Show", "youplay"),
                 "param_name"  => "badges_always_show",
                 "value"       => array( __( "", "youplay" ) => true ),
                 "description" => "When unchecked - show only on mouse over",
              ),
              array(
                 "type"        => "checkbox",
                 "heading"     => __("Boxed", "youplay"),
                 "param_name"  => "boxed",
                 "value"       => array( __( "", "youplay" ) => true ),
                 "description" => "Use it when your page content boxed disabled",
              ),
              array(
                 "type"        => "textfield",
                 "heading"     => __("Custom Classes", "youplay"),
                 "param_name"  => "class",
                 "value"       => __("", "youplay"),
                 "description" => "",
              ),
           )
        ) );
    }
}