File: /mnt/data/dreamstour-wp/wp-content/plugins/dreams-tour/includes/recent-posts-widgets.php
<?php
/**
* Widget API: WP_Widget_Recent_Posts class
*
* @package WordPress
* @subpackage Widgets
* @since 4.4.0
*/
/**
* Core class used to implement a Recent Posts widget.
*
* @since 2.8.0
*
* @see WP_Widget
*/
class Dreamstour_Widget_Recent_Posts extends WP_Widget
{
/**
* Sets up a new Recent Posts widget instance.
*
* @since 2.8.0
*/
public function __construct()
{
$this->load_localisation();
add_action( 'init', array( $this, 'load_localisation' ), 99 );
$widget_ops = array(
'classname' => 'widget_dreamstour_recent_entries',
'description' => __('Your site most recent Posts.', 'dreams-tour'),
'customize_selective_refresh' => true,
);
parent::__construct('dreamstour-recent-posts', __('Dreams Tour Recent Posts', 'dreams-tour'), $widget_ops);
$this->alt_option_name = 'widget_dreamstour_recent_entries';
}
public function load_localisation() {
load_plugin_textdomain( 'dreams-tour', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
/**
* Outputs the content for the current Recent Posts widget instance.
*
* @param array $args Display arguments including 'before_title', 'after_title',
* 'before_widget', and 'after_widget'.
* @param array $instance Settings for the current Recent Posts widget instance.
* @since 2.8.0
*
*/
public function widget($args, $instance)
{
if (!isset($args['widget_id'])) {
$args['widget_id'] = $this->id;
}
$title = (!empty($instance['title'])) ? $instance['title'] : esc_html__('Related Posts', 'dreams-tour');
/** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
$title = apply_filters('widget_title', $title, $instance, $this->id_base);
$number = (!empty($instance['number'])) ? absint($instance['number']) : 5;
if (!$number) {
$number = 5;
}
$show_date = isset($instance['show_date']) ? $instance['show_date'] : false;
$show_image = isset($instance['show_image']) ? $instance['show_image'] : false;
$show_category = isset($instance['show_category']) ? $instance['show_category'] : false;
$show_author = isset($instance['show_author']) ? $instance['show_author'] : false;
$style = isset($instance['style']) ? $instance['style'] : 'style-1';
/**
* Filters the arguments for the Recent Posts widget.
*
* @param array $args An array of arguments used to retrieve the recent posts.
* @param array $instance Array of settings for the current widget.
* @see WP_Query::get_posts()
*
* @since 3.4.0
* @since 4.9.0 Added the `$instance` parameter.
*
*/
$r = new WP_Query(
apply_filters(
'dreamstour_widget_posts_args',
array(
'posts_per_page' => $number,
'no_found_rows' => true,
'post_status' => 'publish',
'ignore_sticky_posts' => true,
),
$instance
)
);
if (!$r->have_posts()) {
return;
}
?>
<?php echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
<?php // Title handled inside custom card header ?>
<div class="card-header border-0 pb-0">
<div class="pb-3 border-bottom">
<h5><i class="ti ti-brand-blogger text-primary fs-16 me-2"></i><?php echo esc_html( $title ); ?></h5>
</div>
</div>
<div class="card-body pt-3">
<?php $total_posts = is_array($r->posts) ? count($r->posts) : 0; ?>
<?php foreach ($r->posts as $index => $recent_post) : ?>
<?php
$post_title = get_the_title($recent_post->ID);
$item_title = (!empty($post_title)) ? $post_title : esc_html__('(no title)', 'dreams-tour');
$aria_current = '';
if (get_queried_object_id() === $recent_post->ID) {
$aria_current = ' aria-current="page"';
}
$thumb_url = '';
if($show_image) {
if (has_post_thumbnail($recent_post->ID)) {
$thumb_url = get_the_post_thumbnail_url($recent_post->ID, 'medium');
} else {
$thumb_url = get_template_directory_uri() . '/assets/images/blog-list-01.jpg';
}
}
$author_id = get_post_field('post_author', $recent_post->ID);
$author_name = get_the_author_meta('display_name', $author_id);
$author_url = get_author_posts_url($author_id);
$author_avatar = get_avatar_url($author_id, array('size' => 48));
$is_last = ($index === $total_posts - 1);
$post_item_class = 'blog-post ' . ($is_last ? 'mb-0' : 'mb-3');
?>
<div class="<?php echo esc_attr($post_item_class); ?>">
<div class="d-flex align-items-center">
<?php if ($show_image && !empty($thumb_url)) : ?>
<a href="<?php echo esc_url(get_permalink($recent_post->ID)); ?>" class="avatar avatar-xxl flex-shrink-0 me-2">
<img src="<?php echo esc_url($thumb_url); ?>" class="rounded" alt="<?php echo esc_attr($item_title); ?>">
</a>
<?php endif; ?>
<div>
<a href="<?php echo esc_url(get_permalink($recent_post->ID)); ?>" class="two-line-ellipsis fs-14 fw-medium"<?php echo $aria_current; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>><?php echo esc_html( wp_trim_words( $item_title, 14 ) ); ?></a>
<div class="d-flex align-items-center mt-2">
<?php if ($show_author) : ?>
<a href="<?php echo esc_url($author_url); ?>" class="d-flex align-items-center border-end pe-2 me-2">
<span class="avatar avatar-xs me-1">
<img src="<?php echo esc_url($author_avatar); ?>" class="blog-user-img rounded-circle border border-light" alt="<?php echo esc_attr($author_name); ?>">
</span>
<p class="fs-14 text-truncate mb-0 text-capitalize"><?php echo esc_html($author_name); ?></p>
</a>
<?php endif; ?>
<?php if ($show_date) : ?>
<p class="fs-14 text-truncate mb-0"><i class="isax isax-calendar-2 me-2"></i><?php echo esc_html( get_the_date('j M Y', $recent_post->ID) ); ?></p>
<?php endif; ?>
</div>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
<?php
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
* Handles updating the settings for the current Recent Posts widget instance.
*
* @param array $new_instance New settings for this instance as input by the user via
* WP_Widget::form().
* @param array $old_instance Old settings for this instance.
* @return array Updated settings to save.
* @since 2.8.0
*
*/
public function update($new_instance, $old_instance)
{
$instance = $old_instance;
$instance['title'] = sanitize_text_field($new_instance['title']);
$instance['number'] = (int)$new_instance['number'];
$instance['show_date'] = isset($new_instance['show_date']) ? (bool)$new_instance['show_date'] : false;
$instance['show_image'] = isset($new_instance['show_image']) ? (bool)$new_instance['show_image'] : false;
$instance['show_category'] = isset($new_instance['show_category']) ? (bool)$new_instance['show_category'] : false;
$instance['show_author'] = isset($new_instance['show_author']) ? (bool)$new_instance['show_author'] : false;
$instance['style'] = isset($new_instance['style']) ? $new_instance['style'] : 'style-1';
return $instance;
}
/**
* Outputs the settings form for the Recent Posts widget.
*
* @param array $instance Current settings.
* @since 2.8.0
*
*/
public function form($instance)
{
$title = isset($instance['title']) ? esc_attr($instance['title']) : '';
$number = isset($instance['number']) ? absint($instance['number']) : 5;
$show_date = isset($instance['show_date']) ? (bool)$instance['show_date'] : false;
$show_image = isset($instance['show_image']) ? (bool)$instance['show_image'] : false;
$show_category = isset($instance['show_category']) ? (bool)$instance['show_category'] : false;
$show_author = isset($instance['show_author']) ? (bool)$instance['show_author'] : false;
$style = isset($instance['style']) ? esc_attr($instance['style']) : 'style-1';
?>
<p><label for="<?php echo esc_html($this->get_field_id('text')); ?>"><?php esc_html_e('Style', 'dreams-tour'); ?>
<select class='widefat' id="<?php echo esc_html($this->get_field_id('style')); ?>"
name="<?php echo esc_html($this->get_field_name('style')); ?>" type="text">
<option value='style-1'<?php echo esc_html(($style == 'style-1')) ? 'selected' : ''; ?>>
<?php esc_html_e('Style 1', 'dreams-tour'); ?>
</option>
</select>
</label>
</p>
<p>
<label for="<?php echo esc_attr($this->get_field_id('title')); ?>"><?php esc_html_e('Title:', 'dreams-tour'); ?></label>
<input class="widefat" id="<?php echo esc_attr($this->get_field_id('title')); ?>"
name="<?php echo esc_attr($this->get_field_name('title')); ?>" type="text"
value="<?php echo esc_attr($title); ?>"/></p>
<p>
<label for="<?php echo esc_attr($this->get_field_id('number')); ?>"><?php esc_html_e('Number of posts to show:', 'dreams-tour'); ?></label>
<input class="tiny-text" id="<?php echo esc_attr($this->get_field_id('number')); ?>"
name="<?php echo esc_attr($this->get_field_name('number')); ?>" type="number" step="1" min="1"
value="<?php echo esc_attr($number); ?>" size="3"/></p>
<p><input class="checkbox" type="checkbox"<?php checked($show_date); ?>
id="<?php echo esc_attr($this->get_field_id('show_date')); ?>"
name="<?php echo esc_attr($this->get_field_name('show_date')); ?>"/>
<label for="<?php echo esc_attr($this->get_field_id('show_date')); ?>"><?php esc_html_e('Display post date?', 'dreams-tour'); ?></label>
</p>
<p><input class="checkbox" type="checkbox"<?php checked($show_image); ?>
id="<?php echo esc_attr($this->get_field_id('show_image')); ?>"
name="<?php echo esc_attr($this->get_field_name('show_image')); ?>"/>
<label for="<?php echo esc_attr($this->get_field_id('show_image')); ?>"><?php esc_html_e('Display post image?', 'dreams-tour'); ?></label>
</p>
<p><input class="checkbox" type="checkbox"<?php checked($show_author); ?>
id="<?php echo esc_attr($this->get_field_id('show_author')); ?>"
name="<?php echo esc_attr($this->get_field_name('show_author')); ?>"/>
<label for="<?php echo esc_attr($this->get_field_id('show_author')); ?>"><?php esc_html_e('Display author details?', 'dreams-tour'); ?></label>
</p>
<?php
}
}
function dreamstour_register_widget() {
register_widget( 'Dreamstour_Widget_Recent_Posts' );
}
add_action( 'widgets_init', 'dreamstour_register_widget' );