HEX
Server: nginx/1.24.0
System: Linux DGT-WORDPRESS-VM-SERVER 6.14.0-1017-azure #17~24.04.1-Ubuntu SMP Mon Dec 1 20:10:50 UTC 2025 x86_64
User: ubuntu (1000)
PHP: 8.4.12
Disabled: NONE
Upload Files
File: /mnt/data/dreamstour-wp/wp-content/plugins/dreamstour-widgets/widgets/class-trending-tours.php
<?php
namespace Dreamstourelementor\Widgets;

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use WP_Query;
use function get_the_ID;
use function get_comments;
use function get_comment_meta;
use function wp_reset_postdata;
use function wp_kses_post;
use function esc_html;
use function get_the_title;
use function get_permalink;
use function has_post_thumbnail;
use function get_the_post_thumbnail_url;
use function get_template_directory_uri;
use function get_post_meta;
use function get_the_terms;
use function is_wp_error;
use function esc_url;
use function esc_attr;
use function get_post_type_archive_link;

if ( ! defined( 'ABSPATH' ) ) exit;

class DTS_trending_tours extends Widget_Base {

    public function get_name() {
        return 'dreamstour_elementor_trending_tours';
    }

    public function get_title() {
        return __( 'Dreams Tour Trending Tours', 'dreamstour_elementor' );
    }

    public function get_icon() {
        return 'eicon-star';
    }

    public function get_categories() {
        return [ 'dreamstourelemetortheme' ];
    }

    protected function register_controls() {
        $this->start_controls_section(
            'content_section',
            [
                'label' => __( 'Content', 'dreamstour_elementor' ),
            ]
        );

        $this->add_control(
            'heading',
            [
                'label' => __( 'Heading', 'dreamstour_elementor' ),
                'type' => Controls_Manager::TEXT,
                'default' => __( 'Our <span class="text-primary text-decoration-underline">Trending</span> Places', 'dreamstour_elementor' ),
            ]
        );

        $this->add_control(
            'sub_heading',
            [
                'label' => __( 'Sub Heading', 'dreamstour_elementor' ),
                'type' => Controls_Manager::TEXTAREA,
                'default' => __( 'Here are some famous tourist places around the world that are known for their historical significance, natural beauty, or cultural impact', 'dreamstour_elementor' ),
            ]
        );

        $this->add_control(
            'posts_per_page',
            [
                'label' => __( 'Number of Tours', 'dreamstour_elementor' ),
                'type' => Controls_Manager::NUMBER,
                'default' => 8,
                'min' => 1,
                'max' => 24,
            ]
        );


        
		$this->add_control(
            'view_all_text',
            [
                'label' => __( 'View All Button Text', 'dreamstour_elementor' ),
                'type' => Controls_Manager::TEXT,
                'default' => 'View All Places',
            ]
        );

        $this->add_control(
            'view_all_link',
            [
                'label' => __( 'View All Button Link', 'dreamstour_elementor' ),
                'type' => Controls_Manager::URL,
                'default' => [ 'url' => '#' ],
            ]
        );


        

        $this->add_control(
            'show_nav',
            [
                'label' => __( 'Show Slider Nav', 'dreamstour_elementor' ),
                'type' => Controls_Manager::SWITCHER,
                'default' => 'yes',
            ]
        );

        $this->end_controls_section();
    }

    protected function render() {
        $settings = $this->get_settings();
        $view_all_text = esc_html( $settings['view_all_text'] );
        $view_all_link = $settings['view_all_link']['url'] ? esc_url($settings['view_all_link']['url']) : '#';
        $posts_per_page = ! empty( $settings['posts_per_page'] ) ? intval( $settings['posts_per_page'] ) : 8;
        $query = new WP_Query([
            'post_type' => 'tour',
            'posts_per_page' => -1,
            'post_status' => 'publish',
        ]);
        $tours = [];
        if ( $query->have_posts() ) {
            while ( $query->have_posts() ) {
                $query->the_post();
                $tour_id = get_the_ID();
                $comments = get_comments([
                    'post_id' => $tour_id,
                    'status'  => 'approve',
                ]);
                $tour_reviews = is_array($comments) ? count($comments) : 0;
                $sum_ratings = 0; $rated_count = 0;
                if ($comments) {
                    foreach ($comments as $c) {
                        $r = get_comment_meta($c->comment_ID, 'rating', true);
                        if ($r) { $sum_ratings += intval($r); $rated_count++; }
                    }
                }
                $tour_rating = $rated_count > 0 ? round($sum_ratings / $rated_count, 2) : 0;
                $tours[] = [
                    'ID' => $tour_id,
                    'rating' => $tour_rating,
                    'reviews' => $tour_reviews,
                ];
            }
            wp_reset_postdata();
        }
        usort($tours, function($a, $b) {
            return $b['rating'] <=> $a['rating'];
        });
        $tours = array_slice($tours, 0, $posts_per_page);
        ?>
        <section class="section place-section bg-white">
            <div class="container">
                <div class="row justify-content-center">
                    <div class="col-xl-6 col-lg-10 text-center wow fadeInUp" data-wow-delay="0.2s">
                        <div class="section-header mb-4 text-center">
                            <h2 class="mb-2"><?php echo wp_kses_post( $settings['heading'] ); ?></h2>
                            <p class="sub-title"><?php echo esc_html( $settings['sub_heading'] ); ?></p>
                        </div>
                    </div>
                </div>
                <div class="tab-content">
                    <div class="tab-pane fade active show" id="Tour-list">
                        <div class="owl-carousel place-slider <?php echo ( $settings['show_nav'] === 'yes' ) ? 'nav-center' : ''; ?>">
                            <?php foreach ( $tours as $tour ) :
                                $tour_id = $tour['ID'];
                                $rating = $tour['rating'];
                                $reviews = $tour['reviews'];
                                $title = get_the_title($tour_id);
                                $permalink = get_permalink($tour_id);
                                $gallery = get_post_meta($tour_id, 'tour_gallery', true);
                                $img = '';
                                $gallery_imgs = [];
                                if ($gallery && is_array($gallery)) {
                                    foreach ($gallery as $img_id) {
                                        $img_url = wp_get_attachment_image_url($img_id, 'large');
                                        if ($img_url) $gallery_imgs[] = $img_url;
                                    }
                                }
                                if (empty($gallery_imgs)) {
                                    if (has_post_thumbnail($tour_id)) {
                                        $gallery_imgs[] = get_the_post_thumbnail_url($tour_id, 'large');
                                    } else {
                                        $gallery_imgs[] = get_template_directory_uri() . '/assets/images/placeholder.webp';
                                    }
                                }
                                $guests = get_post_meta($tour_id, 'tour_people_limit', true);
                                $days_terms = get_the_terms($tour_id, 'tour_duration_days');
                                $nights_terms = get_the_terms($tour_id, 'tour_duration_nights');
                                $days = ($days_terms && ! is_wp_error($days_terms)) ? $days_terms[0]->name : '';
                                $nights = ($nights_terms && ! is_wp_error($nights_terms)) ? $nights_terms[0]->name : '';
                                $location = get_post_meta($tour_id, 'tour_destination', true);
                                $price = get_post_meta($tour_id, 'tour_price', true);
                                $offer_price = get_post_meta($tour_id, 'tour_offer_price', true);
                                $author_id = get_the_author_meta('ID');
                                $author_avatar = get_avatar_url($author_id, array('size' => 96));

                                $tour_price = get_post_meta($tour_id, 'tour_price', true);
                                $tour_offer_price = get_post_meta($tour_id, 'tour_offer_price', true);
                                $tour_types = get_the_terms($tour_id, 'tour_category');

                                $author_id_post = get_the_author_meta('ID');
                                $author_tours_page = function_exists('dreamstour_fl_framework_getoptions') ? dreamstour_fl_framework_getoptions('author_tours_page') : 0;
                                $author_tours_page_url = $author_tours_page ? get_permalink($author_tours_page) : '';
                                $author_link = $author_tours_page_url ? add_query_arg(array(
                                    'author_id' => (int) $author_id_post,
                                ), $author_tours_page_url) : 'javascript:void(0);';
                                
                                ?>
                                <div class="place-item mb-4 flex-fill">
                                    <div class="place-img">
                                        <div class="img-slider image-slide owl-carousel nav-center">
                                            <?php foreach ($gallery_imgs as $img_url): ?>
                                                <div class="slide-images">
                                                    <a href="<?php echo esc_url($permalink); ?>">
                                                        <img src="<?php echo esc_url($img_url); ?>" class="img-fluid" alt="<?php echo esc_attr($title); ?>">
                                                    </a>
                                                </div>
                                            <?php endforeach; ?>
                                        </div>
                                        <div class="fav-item">
                                           <?php $wishlist_active = is_user_logged_in() ? (function_exists('dreamstour_is_in_wishlist') ? dreamstour_is_in_wishlist(get_current_user_id(), $tour_id) : false) : false; ?>
                                            <a href="javascript:void(0);" id="dt-wishlist-btn" data-tour-id="<?php echo (int) $tour_id; ?>" data-tour-title="<?php echo esc_attr(get_the_title($tour_id)); ?>" class="fav-icon <?php echo $wishlist_active ? 'selected' : ''; ?>">
                                                <i class="isax isax-heart5"></i>
                                                
                                            </a>
                                            <span class="badge bg-info d-inline-flex align-items-center"><i class="isax isax-ranking me-1"></i><?php esc_html_e('Trending','dreamstour_elementor');?></span>
                                        </div>
                                    </div>
                                    <div class="place-content">
                                        <div class="d-flex align-items-center justify-content-between mb-1">
                                            <div class="d-flex flex-wrap align-items-center">
                                                <span class="me-1"><i class="ti ti-receipt text-primary"></i></span>
                                                <p class="fs-14 text-gray-9"><?php
                                                    if ($tour_types && !is_wp_error($tour_types)) {
                                                        echo esc_html($tour_types[0]->name);
                                                    } else {
                                                        esc_html_e('Tour','dreams-tour'); 
                                                    }
                                                    ?></p>
                                            </div>
                                            <span class="d-inline-block border vertical-splits">
                                                <span class="bg-light text-light d-flex align-items-center justify-content-center"></span>
                                            </span>
                                            <div class="d-flex align-items-center flex-wrap">
                                                <span class="badge badge-warning badge-xs text-gray-9 fs-13 fw-medium me-1"><?php echo esc_html( number_format( (float) $rating, 1 ) ); ?>
                                                </span>
                                                <p class="fs-14">(<?php echo intval($reviews); ?> <?php esc_html_e('Reviews','dreamstour_elementor');?>)</p>
                                            </div>
                                        </div>
                                        <h5 class="mb-1 text-truncate"><a href="<?php echo esc_url($permalink); ?>"><?php echo esc_html($title); ?></a></h5>
                                        <p class="d-flex align-items-center mb-3"><i class="isax isax-location5 me-2"></i><?php echo esc_html($location); ?></p>
                                        <div class="mb-3">
                                        <h6 class="d-flex align-items-center text-gray-6 fs-14 fw-normal border-end pe-2 me-2">
                                                <?php esc_html_e('Starts From','dreams-tour');?>
                                                <?php if ($tour_offer_price) : ?>
                                                <span class="ms-1 fs-18 fw-semibold text-primary">
                                                <?php echo get_woocommerce_currency_symbol(); ?><?php echo esc_html($tour_offer_price); ?>
                                                </span>
                                                <!-- Original Price (struck through) -->
                                                <span class="ms-1 fs-18 fw-semibold text-gray-3 text-decoration-line-through">
                                                <?php echo get_woocommerce_currency_symbol(); ?><?php echo esc_html($tour_price); ?>
                                                </span>
                                            <?php else : ?>
                                                <!-- Regular Price only -->
                                                <span class="ms-1 fs-18 fw-semibold text-primary">
                                                <?php echo get_woocommerce_currency_symbol(); ?><?php echo esc_html($tour_price ? $tour_price : '0'); ?>
                                                </span>
                                            <?php endif; ?>

                                            </h6>
                                        </div>
                                        <div class="d-flex align-items-center justify-content-between border-top pt-3">
                                            <div class="d-flex flex-wrap align-items-center me-2">
                                                <span class="me-1"><i class="isax isax-calendar-tick text-gray-6"></i></span>
                                                <p class="fs-14 text-gray-9"><?php echo esc_html($days ? $days : '0 Day'); ?><?php if ($days && $nights) echo ', '; ?><?php echo esc_html($nights ? $nights : '0 Night'); ?></p>
                                            </div>
                                            <span class="d-inline-block border vertical-splits">
                                                <span class="bg-light text-light d-flex align-items-center justify-content-center"></span>
                                            </span>
                                            <div class="ms-2 d-flex align-items-center">
                                                <p class="fs-14 text-gray-9 mb-0 text-truncate d-flex align-items-center">
                                                    <i class="isax isax-profile-2user me-1"></i><?php echo esc_html($guests ? $guests : '0'); ?> Guests
                                                </p>
                                                <a href="<?php echo esc_url($author_link); ?>" class="avatar avatar-sm ms-3">
                                                <img src="<?php echo esc_url($author_avatar); ?>" class="rounded-circle" alt="<?php the_author(); ?>">
                                                </a>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            <?php endforeach; ?>
                        </div>
                    </div>
                </div>
                <div class="text-center view-all wow fadeInUp">
                    <a href="<?php echo esc_html($view_all_link); ?>" class="btn btn-dark d-inline-flex align-items-center"><?php echo esc_html($view_all_text); ?><i class="isax isax-arrow-right-3 ms-2"></i></a>
                </div>
            </div>
        </section>
        <?php
    }
}