<?php
/**
 * Plugin Name:			Calluna Extension
 * Plugin URI:			http://zingersystems.com
 * Description:			Adapts the calluna theme for the updates on the awe booking plugin
 * Version:				1.0
 * Author:				Diyen Momjang, Zinger Systems
 * Author URI:			http://zingersystems.com
 * License:				GPL-2.0+
 * License URI:			http://www.gnu.org/licenses/gpl-2.0.txt
 */

function calluna_room_carousel_shortcode_extended($atts, $content = NULL) {

    extract(shortcode_atts(array(
        'items'			=> '3',
        'order'			=> 'ASC',
        'orderby'		=> 'price',
        'max_items'       => '8',
        'button_style'	 => 'style-1',
        'categories' 	=> 'all',
        'img_crop'		=> 'true',
        'img_width'		=> '420',
        'img_height'	=> '510',
        'excerpt_length' => '30',
        'button_price'   => 'yes'
    ), $atts));

    global $post;
    $type = 'room_type';
    if ( post_type_exists( 'apb_room_type' ) ) {
        $type = 'apb_room_type';
    }
    $post_ID = get_the_ID();
    $args = array(
        'post_type' => $type,
        'posts_per_page' => $max_items,
        'post__not_in' => array( $post_ID ),
        'order'          => $order,
        'orderby'        => $orderby,
        'post_status'    => 'publish',

    );
    if($orderby == 'price') {
        $args['orderby'] = 'meta_value_num';
        $args['meta_key'] = 'base_price';
    }
    if($categories != 'all'){
        $str = $categories;
        $arr = explode(',', $str); // string to array

        $args['tax_query'][] = array(
            'taxonomy'  => 'category',
            'field'   => 'slug',
            'terms'   => $arr
        );
    }

    wp_enqueue_style('calluna-carousel-style');
    wp_enqueue_script('calluna-carousel');

    ob_start();

    $wp_query = new WP_Query($args);

    if( $wp_query->have_posts() ) : ?>

        <div class="calluna-room-carousel">
            <div class="room2-carousel owl-carousel owl-theme" data-items="<?php echo esc_attr( $items ); ?>">

                <?php
                while ( $wp_query->have_posts() ) : $wp_query->the_post();

                    $featured_img_url = wp_get_attachment_url( get_post_thumbnail_id( $post->ID ) );
                    $featured_img     = wp_get_attachment_url( get_post_thumbnail_id( $post->ID ) );

                    //$price = AWE_function::apb_price(get_post_meta($post->ID,"base_price",true));
                    $price = '$300';
                    if ( has_excerpt( $post->ID ) ) {
                        $custom_excerpt = $post->post_excerpt;
                    } elseif(get_post_meta( $post->ID, 'room_desc', true ) != '') {
                        $custom_excerpt = get_post_meta( $post->ID, 'room_desc', true );

                    }
                    else {
                        $custom_excerpt = $post->post_content;
                    }
                    $custom_excerpt = wp_trim_words( strip_shortcodes( $custom_excerpt ), $excerpt_length);

                    // Crop featured images if necessary
                    if ( $img_crop != 'false' ) {
                        $thumbnail_hard_crop = $img_height == '9999' ? false : true;
                        $featured_img = calluna_shortcodes_img_resize( $featured_img_url, $img_width, $img_height, $thumbnail_hard_crop );
                    }
                    ?>
                    <div class="room-item">
                        <?php
                        if ( has_post_thumbnail( $post->ID ) ) { ?>

                            <a href="<?php echo esc_url(get_permalink()) ?>" title="<?php echo esc_attr(get_the_title()) ?>" class="room-pic">
                                <img src="<?php echo esc_url($featured_img)?>" alt="room-image" />
                                <div class="room-content-wrapper">
                                    <span class="overlay"></span>
                                    <div class="room-title">
                                        <h3><?php echo get_the_title() ?></h3>
                                    </div>
                                    <!--<div class="room-excerpt">
                                        <p><?php echo esc_attr($custom_excerpt) ?></p>
                                    </div>-->
                                    <div class="room-price-wrapper">
                                        <div class="room-price <?php echo esc_attr($button_style)?>">
                                            <?php $button_text = get_theme_mod('room_button_text', 'starting at');
                                            // WPML translations
                                            $button_text = calluna_translate_theme_mod( 'room_button_text', $button_text );

                                            ?>
                                            <span><?php echo esc_attr($button_text) ?></span>
                                            <?php
                                            if ($button_price != 'no') { ?>
                                                <span class="inline-price">
                                            <?php echo esc_attr($price); ?>
                                        </span>
                                            <?php } ?>

                                        </div>
                                    </div>
                                </div>
                            </a>

                        <?php } ?>

                    </div>

                <?php endwhile; ?>

            </div>

        </div><div class="clearfix"></div>

        <?php
        wp_reset_postdata();

    endif;

    $calluna_room_carousel_output = ob_get_contents();
    ob_end_clean();
    return $calluna_room_carousel_output;

}

add_shortcode('cl_room_carousel_extended', 'calluna_room_carousel_shortcode_extended');