<?php
/**
 * YP Recent Posts
 *
 * Example:
 * [yp_recent_posts style="1" post_type="post" ids="" exclude_ids="" custom_query="" count="5" pagination="false" boxed="false"]
 */
add_shortcode("yp_recent_posts", "yp_recent_posts");
function yp_recent_posts($atts, $content = null) {
    extract(shortcode_atts(array(
        "style"        => 1,
        "post_type"    => "post",
        "ids"          => "",
        "exclude_ids"  => "",
        "custom_query" => "",
        "count"        => 5,
        "pagination"   => false,
        "boxed"        => false,
        "class"        => ""
    ), $atts));

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

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

    $before = $after = '';

    if($style == 3) {
      $before = '<div class="isotope">';
      $after = '</div>';
      $class .= ' isotope-list news-grid row';
    }

    ob_start();
    echo wp_kses_post($before);
    ?> <div class="youplay-news <?php echo yp_sanitize_class($class); ?>"> <?php
      $paged = 0;
      if($pagination) {
        $paged = max( 1, get_query_var( 'page' ), get_query_var( 'paged' ) );
      }
      $query_opts = array(
        'showposts'     => intval($count),
        'posts_per_page'=> intval($count),
        'paged'         => $paged,
      );
      if($exclude_ids) {
        $query_opts['post__not_in'] = $exclude_ids;
      }
      if($post_type == 'ids') {
        $query_opts['post_type'] = 'any';
        $query_opts['post__in'] = $ids;
      } 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;
      }

      $yp_query = new WP_Query($query_opts);
      $counter = 0;
      while ($yp_query->have_posts()) : $yp_query->the_post();
        get_template_part( 'template-parts/content', $style );
      endwhile;

      echo wp_kses_post($after);
    ?> 
      <div class="clearfix"></div>
      <?php if($pagination) {
        yp_posts_navigation($yp_query);
      } ?>

    </div>

    <?php
    wp_reset_postdata();
    
    $result = ob_get_contents();
    ob_end_clean();
    return $result;
}



/* Add VC Shortcode */
add_action( "init", "vc_yp_recent_posts" );
function vc_yp_recent_posts() {
    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 Recent Posts", "youplay"),
           "base" => "yp_recent_posts",
           "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,
                    __("Style 3", "youplay") => 3
                 ),
                 "description" => ""
              ),
              array(
                "type"        => "dropdown",
                "heading"     => __( "Data source", "youplay" ),
                "param_name"  => "post_type",
                "value"       => $post_types_list,
                "std"         => "post",
                "description" => __( "Select content type", "youplay" )
              ),
              array(
                "type"        => "textfield",
                "heading"     => __( "IDs", "youplay" ),
                "param_name"  => "ids",
                "description" => __( "Type here the posts, pages, etc. IDs you want to use separated by coma. ex: 23,24,25", "youplay" ),
                "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"        => "textfield",
                 "heading"     => __("Recent Posts Count", "youplay"),
                 "param_name"  => "count",
                 "value"       => 5,
                 "description" => "",
              ),
              array(
                 "type"        => "checkbox",
                 "heading"     => __("Show Pagination", "youplay"),
                 "param_name"  => "pagination",
                 "value"       => array( __( "", "youplay" ) => true ),
                 "description" => "",
              ),
              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" => "",
              ),
           )
        ) );
    }
}