|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace Includes\Api; |
| 5 | + |
| 6 | +use WP_Widget; |
| 7 | +use WP_Error; |
| 8 | +use WP_Query; |
| 9 | + |
| 10 | +defined('ABSPATH') || exit; |
| 11 | + |
| 12 | +class LatestPostsWidget extends WP_Widget |
| 13 | +{ |
| 14 | + public function __construct() |
| 15 | + { |
| 16 | + parent::__construct( |
| 17 | + |
| 18 | + // Base ID |
| 19 | + 'latest-post-widget', |
| 20 | + |
| 21 | + // Widget name to show in the UI |
| 22 | + __('Latest Post Widget', 'latestpostwidget'), |
| 23 | + |
| 24 | + // Description |
| 25 | + ['description' => __('Displays recent posts from a selected category.', 'latestpostwidget')] |
| 26 | + ); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Front-end display of widget. |
| 31 | + * |
| 32 | + * @see WP_Widget::widget() |
| 33 | + * |
| 34 | + * @param array $args Widget arguments. |
| 35 | + * @param array $instance Saved values from database. |
| 36 | + */ |
| 37 | + public function widget($args, $instance) |
| 38 | + { |
| 39 | + // extract($args); |
| 40 | + echo $args['before_widget']; |
| 41 | + |
| 42 | + $defaultTitle = __('Latest Posts from Category'); |
| 43 | + $title = !empty($instance['title']) ? $instance['title'] : $defaultTitle; |
| 44 | + |
| 45 | + $title = apply_filters('widget_title', $title, $instance); |
| 46 | + $number = (int) ($instance['number'] ?? 5); |
| 47 | + $category = $instance['category'] ?? ''; |
| 48 | + |
| 49 | + if (!empty($title)) { |
| 50 | + echo $args['before_title'] . esc_html($title) . $args['after_title']; |
| 51 | + } |
| 52 | + |
| 53 | + if (empty($category)) { |
| 54 | + echo '<p>' . esc_html__('No category selected', 'paktoluspostwidget') . '</p>'; |
| 55 | + echo $args['after_widget']; |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + $query = new WP_Query( |
| 60 | + [ |
| 61 | + 'cat' => (int) $category, |
| 62 | + 'posts_per_page' => $number, |
| 63 | + 'post_status' => 'publish' |
| 64 | + ] |
| 65 | + ); |
| 66 | + |
| 67 | + |
| 68 | + if ($query->have_posts()): |
| 69 | + echo '<ul>'; |
| 70 | + while ($query->have_posts()): |
| 71 | + $query->the_post(); |
| 72 | + echo '<li><a href="' . esc_url(get_permalink()) . '">' . esc_html(get_the_title()) . ' </a></li>'; |
| 73 | + endwhile; |
| 74 | + echo '</ul>'; |
| 75 | + else: |
| 76 | + echo '<p>' . esc_html__('No posts found', 'paktoluspostwidget') . '</p>'; |
| 77 | + endif; |
| 78 | + |
| 79 | + |
| 80 | + wp_reset_postdata(); |
| 81 | + echo $args['after_widget']; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Back-end widget form. |
| 86 | + * |
| 87 | + * @see WP_Widget::form() |
| 88 | + * |
| 89 | + * @param array $instance Previously saved values from database. |
| 90 | + */ |
| 91 | + public function form($instance) |
| 92 | + { |
| 93 | + $title = esc_attr($instance['title'] ?? __('Latest Posts from Category', 'paktoluspostwidget')); |
| 94 | + $number = esc_attr($instance['number'] ?? 5); |
| 95 | + $category = $instance['category'] ?? ''; |
| 96 | + |
| 97 | + $categories = get_categories(['hide_empty' => false]); |
| 98 | + |
| 99 | + ?> |
| 100 | + <p> |
| 101 | + <label |
| 102 | + for="<?= esc_attr($this->get_field_id('title')); ?>"><?= esc_html__('Title:', 'paktoluspostwidget'); ?></label> |
| 103 | + <input class="widefat" id="<?= esc_attr($this->get_field_id('title')); ?>" |
| 104 | + name="<?= esc_attr($this->get_field_name('title')); ?>" type="text" value="<?= $title; ?>"> |
| 105 | + </p> |
| 106 | + <p> |
| 107 | + <label |
| 108 | + for="<?= esc_attr($this->get_field_id('number')); ?>"><?= esc_html__('Number of posts to show:', 'paktoluspostwidget'); ?> |
| 109 | + </label> |
| 110 | + <input class="tiny-text" id="<?= esc_attr($this->get_field_id('number')); ?>" |
| 111 | + name="<?= esc_attr($this->get_field_name('number')); ?>" type="number" step="1" min="1" value="<?= $number; ?>" |
| 112 | + size="3"> |
| 113 | + </p> |
| 114 | + <p> |
| 115 | + <label for="<?= esc_attr($this->get_field_id('category')); ?>"><?= esc_html__('Category:', 'paktoluspostwidget'); ?> |
| 116 | + </label> |
| 117 | + <select class="widefat" id="<?= esc_attr($this->get_field_id('category')); ?>" |
| 118 | + name="<?= esc_attr($this->get_field_name('category')); ?>"> |
| 119 | + <option value=""><?= esc_html__('Select a Category', 'paktoluspostwidget'); ?></option> |
| 120 | + <?php foreach ($categories as $cat): ?> |
| 121 | + <option value="<?= esc_attr($cat->term_id); ?>" <?= selected((int) $category, $cat->term_id, false); ?>> |
| 122 | + <?= esc_html($cat->name); ?> |
| 123 | + </option> |
| 124 | + <?php endforeach; ?> |
| 125 | + </select> |
| 126 | + </p> |
| 127 | + <?php |
| 128 | + } |
| 129 | + /** |
| 130 | + * Sanitize widget form values as they are saved. |
| 131 | + * |
| 132 | + * @see WP_Widget::update() |
| 133 | + * |
| 134 | + * @param array $new_instance Values just sent to be saved. |
| 135 | + * @param array $old_instance Previously saved values from database. |
| 136 | + * |
| 137 | + * @return array Updated safe values to be saved. |
| 138 | + */ |
| 139 | + public function update($new_instance, $old_instance) |
| 140 | + { |
| 141 | + $instance = []; |
| 142 | + |
| 143 | + $instance['title'] = sanitize_text_field($new_instance['title'] ?? ''); |
| 144 | + $instance['number'] = (int) ($new_instance['number'] ?? 5); |
| 145 | + $instance['category'] = (int) ($new_instance['category'] ?? 0); |
| 146 | + |
| 147 | + if ($instance['number'] <= 0) { |
| 148 | + $instance['number'] = 5; |
| 149 | + } |
| 150 | + |
| 151 | + return $instance; |
| 152 | + } |
| 153 | +} |
0 commit comments