PluginProbe
Expanding Archives / trunk
Expanding Archives vtrunk
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.5 1.1.0 1.1.1 2.0 2.0.1 2.0.2 2.1.0 2.1.1
expanding-archives / src / Widget.php

Widget.php in Expanding Archives trunk, at src/Widget.php

147 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Widget.php
4 *
5 * @package expanding-archives
6 * @copyright Copyright (c) 2026, Ashley Gibson
7 * @license GPL2+
8 * @since 2.0
9 */
10
11 namespace Ashleyfae\ExpandingArchives;
12
13 use Ashleyfae\ExpandingArchives\Helpers\ArchiveRenderer;
14
15 class Widget extends \WP_Widget
16 {
17
18 /**
19 * Constructor.
20 */
21 public function __construct()
22 {
23 parent::__construct(
24 'ng_expanding_archives',
25 __('Expanding Archives', 'expanding-archives'),
26 [
27 'description' => __('Adds expandable archives of your old posts.', 'expanding-archives'),
28 ]
29 );
30 }
31
32 /**
33 * Parses the instance values by merging in the defaults.
34 *
35 * @since 2.0
36 *
37 * @param array|mixed $instance
38 *
39 * @return array
40 */
41 private function getInstanceValues($instance): array
42 {
43 if (! is_array($instance)) {
44 $instance = [];
45 }
46
47 return wp_parse_args($instance, [
48 'title' => '',
49 'expand_current' => true,
50 ]);
51 }
52
53 /**
54 * Displays the front-end of the widget.
55 *
56 * @since 2.0
57 *
58 * @param array $args Widget arguments.
59 * @param array $instance Saved values from database.
60 *
61 * @return void
62 */
63 public function widget($args, $instance)
64 {
65 $instance = $this->getInstanceValues($instance);
66
67 echo $args['before_widget'] ?? '';
68
69 $title = apply_filters('widget_title', $instance['title'] ?? '');
70
71 //If title section is filled out, display title. If not, display nothing.
72 if (! empty($title)) {
73 echo $args['before_title'].$title.$args['after_title'];
74 }
75
76 $displayer = new ArchiveRenderer();
77 if (! empty($instance['expand_current'])) {
78 $displayer->expandCurrent();
79 }
80
81 $displayer->render();
82
83 echo $args['after_widget'] ?? '';
84 }
85
86 /**
87 * Displays the admin widget form.
88 *
89 * @since 2.0
90 *
91 * @see WP_Widget::form()
92 *
93 * @param array $instance Saved values from database.
94 */
95 public function form($instance)
96 {
97 $instance = $this->getInstanceValues($instance);
98 ?>
99 <p>
100 <label for="<?php echo $this->get_field_id('title'); ?>">
101 <?php esc_html_e('Title:'); ?>
102 </label>
103 <input
104 type="text"
105 id="<?php echo $this->get_field_id('title'); ?>"
106 class="widefat"
107 name="<?php echo $this->get_field_name('title'); ?>"
108 value="<?php echo esc_attr($instance['title']); ?>"
109 >
110 </p>
111
112 <p>
113 <input
114 type="checkbox"
115 id="<?php echo $this->get_field_id('expand_current'); ?>"
116 name="<?php echo $this->get_field_name('expand_current'); ?>"
117 value="1" <?php checked($instance['expand_current']); ?>
118 >
119 <label for="<?php echo $this->get_field_id('expand_current'); ?>">
120 <?php esc_html_e('Automatically expand current month'); ?>
121 </label>
122 </p>
123 <?php
124 }
125
126 /**
127 * Sanitizes the widget form values as they are saved.
128 *
129 * @since 2.0
130 *
131 * @see WP_Widget::update()
132 *
133 * @param array $new_instance Values just sent to be saved.
134 * @param array $old_instance Previously saved values from database.
135 *
136 * @return array Updated safe values to be saved.
137 */
138 public function update($new_instance, $old_instance)
139 {
140 return [
141 'title' => sanitize_text_field(strip_tags($new_instance['title'] ?? '')),
142 'expand_current' => ! empty($new_instance['expand_current']),
143 ];
144 }
145
146 }
147