File: /mnt/data/dreamstour-wp/wp-content/plugins/dreamstour-widgets/widgets/class-top-rated-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;
if ( ! defined( 'ABSPATH' ) ) exit;
class DTS_top_rated_tours extends Widget_Base {
public function get_name() {
return 'dreamstour_elementor_top_rated_tours';
}
public function get_title() {
return __( 'Dreams Top Rated Tours', 'dreamstour_elementor' );
}
public function get_icon() {
return 'eicon-rating';
}
public function get_categories() {
return [ 'dreamstourelemetortheme' ];
}
protected function register_controls() {
$this->start_controls_section(
'content_section',
[ 'label' => __( 'Content', 'dreamstour_elementor' ) ]
);
$this->add_control(
'badge_text',
[
'label' => __( 'Badge Text', 'dreamstour_elementor' ),
'type' => Controls_Manager::TEXT,
'default' => __( 'Top Rated Tours', 'dreamstour_elementor' ),
]
);
$this->add_control(
'heading',
[
'label' => __( 'Heading', 'dreamstour_elementor' ),
'type' => Controls_Manager::TEXT,
'default' => __( 'Popular Tours Around the World', '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 Categories',
]
);
$this->add_control(
'view_all_link',
[
'label' => __( 'View All Button Link', 'dreamstour_elementor' ),
'type' => Controls_Manager::URL,
'default' => [ 'url' => '#' ],
]
);
$this->end_controls_section();
}
protected function render() {
$s = $this->get_settings();
$badge_text = esc_html( $s['badge_text'] );
$heading = esc_html( $s['heading'] );
$view_all_text = esc_html( $s['view_all_text'] );
$view_all_link = $s['view_all_link']['url'] ? esc_url($s['view_all_link']['url']) : '#';
$posts_per_page = ! empty( $s['posts_per_page'] ) ? intval( $s['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">
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8 wow fadeInUp" data-wow-delay="0.2s">
<div class="section-header-six">
<span class="badge badge-soft-primary rounded-pill mb-1"><?php echo esc_html($badge_text); ?></span>
<h2><?php echo esc_html($heading); ?><span class="text-primary">.</span></h2>
</div>
</div>
<div class="col-md-4">
<div class="text-end text-end-left-mobile">
<a href="<?php echo esc_url($view_all_link); ?>" class="btn btn-dark sec-head-btn"><?php echo esc_html($view_all_text); ?><i class="isax isax-arrow-right-3 ms-2"></i></a>
</div>
</div>
</div>
<div class="cars-slider owl-dot-bottom owl-carousel wow fadeInUp" data-wow-delay="1.5">
<?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);
$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);
$tour_price = get_post_meta($tour_id, 'tour_price', true);
$tour_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_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('Top Rated','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>
<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 : ?>
<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>
</section>
<?php
}
}