PluginProbe
Essential Widgets / 3.2
Essential Widgets v3.2
3.2.1 3.3 3.2 trunk 1.0.0 1.0.1 1.1 1.2 1.2.1 1.2.2 1.3 1.4 1.4.1 1.5 1.6 1.7 1.7.1 1.7.2 1.7.3 1.7.4 1.8 1.9 2.0 2.1 2.2 All 31 releases
essential-widgets / includes / widgets / class-ew-archives.php

class-ew-archives.php in Essential Widgets 3.2, at includes/widgets/class-ew-archives.php

247 lines 9.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 exit; // Exit if accessed directly.
4 }
5
6 if ( ! class_exists( 'EW_Archives' ) ) :
7 class EW_Archives extends WP_Widget // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound -- EW_ is this plugin's abbreviated prefix; wrapped in class_exists() guard.
8 {
9
10 protected $defaults;
11
12 public function __construct() {
13 $this->defaults = array(
14 'title' => esc_attr__( 'Archives', 'essential-widgets' ),
15 'limit' => 10,
16 'type' => 'monthly',
17 'post_type' => 'post',
18 'order' => 'DESC',
19 'format' => 'html',
20 'before' => '',
21 'after' => '',
22 'show_post_count' => false,
23 );
24
25 parent::__construct(
26 'ew-archive',
27 __( 'EW: Archives', 'essential-widgets' ),
28 array(
29 'classname' => 'essential-widgets widget_archive ew-archive ewarchive',
30 'description' => esc_html__( 'Displays a list of categories', 'essential-widgets' ),
31 ),
32 array(
33 'id_base' => 'ew-archive',
34 )
35 );
36 }
37
38 public function form( $instance ) {
39 $instance = wp_parse_args( (array) $instance, $this->defaults );
40
41 $post_types = get_post_types( array( 'name' => 'post' ), 'objects' );
42 $post_types = array_merge(
43 $post_types,
44 get_post_types( array( 'publicly_queryable' => true, '_builtin' => false ), 'objects' )
45 );
46
47 $type = array(
48 'alpha' => esc_attr__( 'Alphabetical', 'essential-widgets' ),
49 'daily' => esc_attr__( 'Daily', 'essential-widgets' ),
50 'monthly' => esc_attr__( 'Monthly', 'essential-widgets' ),
51 'postbypost' => esc_attr__( 'Post By Post', 'essential-widgets' ),
52 'weekly' => esc_attr__( 'Weekly', 'essential-widgets' ),
53 'yearly' => esc_attr__( 'Yearly', 'essential-widgets' ),
54 );
55
56 $order = array(
57 'ASC' => esc_attr__( 'Ascending', 'essential-widgets' ),
58 'DESC' => esc_attr__( 'Descending', 'essential-widgets' ),
59 );
60
61 $format = array(
62 'custom' => esc_attr__( 'Plain', 'essential-widgets' ),
63 'html' => esc_attr__( 'List', 'essential-widgets' ),
64 'option' => esc_attr__( 'Dropdown', 'essential-widgets' ),
65 );
66
67 ?>
68 <p>
69 <label><?php esc_html_e( 'Title:', 'essential-widgets' ); ?>
70 <input type="text" class="widefat" id="<?php echo esc_attr( $this->get_field_id('title') ); ?>" name="<?php echo esc_attr( $this->get_field_name('title') ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" placeholder="<?php echo esc_attr( $this->defaults['title'] ); ?>" />
71 </label>
72 </p>
73
74 <p>
75 <label><?php esc_html_e( 'Limit:', 'essential-widgets' ); ?>
76 <input type="number" class="widefat" min="0" name="<?php echo esc_attr( $this->get_field_name('limit') ); ?>" value="<?php echo intval($instance['limit']); ?>" />
77 </label>
78 </p>
79
80 <p>
81 <label><?php esc_html_e( 'Type:', 'essential-widgets' ); ?>
82 <select class="widefat" name="<?php echo esc_attr( $this->get_field_name('type') ); ?>">
83 <?php foreach ( $type as $option_value => $option_label ) : ?>
84 <option value="<?php echo esc_attr( $option_value ); ?>" <?php selected( $instance['type'], $option_value ); ?>><?php echo esc_html( $option_label ); ?></option>
85 <?php endforeach; ?>
86 </select>
87 </label>
88 </p>
89
90 <p>
91 <label><?php esc_html_e( 'Post Type:', 'essential-widgets' ); ?>
92 <select class="widefat" name="<?php echo esc_attr( $this->get_field_name('post_type') ); ?>">
93 <?php foreach ( $post_types as $post_type ) : ?>
94 <option value="<?php echo esc_attr( $post_type->name ); ?>" <?php selected( $instance['post_type'], $post_type->name ); ?>><?php echo esc_html( $post_type->labels->singular_name ); ?></option>
95 <?php endforeach; ?>
96 </select>
97 </label>
98 </p>
99
100 <p>
101 <label><?php esc_html_e( 'Order:', 'essential-widgets' ); ?>
102 <select class="widefat" name="<?php echo esc_attr( $this->get_field_name('order') ); ?>">
103 <?php foreach ( $order as $option_value => $option_label ) : ?>
104 <option value="<?php echo esc_attr( $option_value ); ?>" <?php selected( $instance['order'], $option_value ); ?>><?php echo esc_html( $option_label ); ?></option>
105 <?php endforeach; ?>
106 </select>
107 </label>
108 </p>
109
110 <p>
111 <label><?php esc_html_e( 'Display as:', 'essential-widgets' ); ?>
112 <select class="widefat" name="<?php echo esc_attr( $this->get_field_name('format') ); ?>">
113 <?php foreach ( $format as $option_value => $option_label ) : ?>
114 <option value="<?php echo esc_attr( $option_value ); ?>" <?php selected( $instance['format'], $option_value ); ?>><?php echo esc_html( $option_label ); ?></option>
115 <?php endforeach; ?>
116 </select>
117 </label>
118 </p>
119
120 <p>
121 <label><?php esc_html_e( 'Before:', 'essential-widgets' ); ?>
122 <input type="text" class="widefat" name="<?php echo esc_attr( $this->get_field_name('before') ); ?>" value="<?php echo esc_attr( $instance['before'] ); ?>" />
123 </label>
124 </p>
125
126 <p>
127 <label><?php esc_html_e( 'After:', 'essential-widgets' ); ?>
128 <input type="text" class="widefat" name="<?php echo esc_attr( $this->get_field_name('after') ); ?>" value="<?php echo esc_attr( $instance['after'] ); ?>" />
129 </label>
130 </p>
131
132 <p>
133 <label>
134 <input type="checkbox" <?php checked( $instance['show_post_count'], true ); ?> name="<?php echo esc_attr( $this->get_field_name('show_post_count') ); ?>" />
135 <?php esc_html_e( 'Show post count?', 'essential-widgets' ); ?>
136 </label>
137 </p>
138 <?php
139 }
140
141 public function update( $new_instance, $old_instance ) {
142 $instance = array();
143
144 $instance['title'] = sanitize_text_field( $new_instance['title'] );
145 $instance['post_type'] = sanitize_key( $new_instance['post_type'] );
146
147 $allowed_types = array( 'alpha', 'daily', 'monthly', 'postbypost', 'weekly', 'yearly' );
148 $allowed_orders = array( 'ASC', 'DESC' );
149 $allowed_formats = array( 'custom', 'html', 'option' );
150
151 $instance['type'] = in_array( $new_instance['type'], $allowed_types, true ) ? $new_instance['type'] : 'monthly';
152 $instance['order'] = in_array( $new_instance['order'], $allowed_orders, true ) ? $new_instance['order'] : 'DESC';
153 $instance['format'] = in_array( $new_instance['format'], $allowed_formats, true ) ? $new_instance['format'] : 'html';
154
155 $instance['limit'] = intval( $new_instance['limit'] );
156 $instance['limit'] = max( 0, $instance['limit'] ); // ensure non-negative integer
157
158 $instance['before'] = current_user_can( 'unfiltered_html' ) ? $new_instance['before'] : wp_kses_post( $new_instance['before'] );
159 $instance['after'] = current_user_can( 'unfiltered_html' ) ? $new_instance['after'] : wp_kses_post( $new_instance['after'] );
160
161 $instance['show_post_count'] = ! empty( $new_instance['show_post_count'] ) ? 1 : 0;
162
163 return $instance;
164 }
165
166 public function widget( $args, $instance ) {
167 $instance = wp_parse_args( $instance, $this->defaults );
168
169 echo wp_kses_post( $args['before_widget'] );
170
171 if ( ! empty( $instance['title'] ) ) {
172 echo wp_kses_post( $args['before_title'] );
173 $title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
174 echo esc_html( $title );
175 echo wp_kses_post( $args['after_title'] );
176 }
177
178 echo wp_kses_post( $this->shortcode( $instance ) );
179
180 echo wp_kses_post( $args['after_widget'] );
181 }
182
183 public function shortcode( $atts ) {
184 $atts['echo'] = false;
185
186 // Ensure limit is integer and not zero
187 $atts_copy = $atts;
188 $atts_copy['limit'] = intval( $atts_copy['limit'] ?? 0 );
189 if ( $atts_copy['limit'] <= 0 ) {
190 unset( $atts_copy['limit'] ); // prevent SQL error
191 }
192
193 $atts_copy['post_type'] = sanitize_key( $atts_copy['post_type'] ?? 'post' );
194
195 $archives = str_replace( array("\r","\n","\t"), '', wp_get_archives( $atts_copy ) );
196 $archives = str_replace( '</a>&nbsp;(', '</a> <span>(', $archives );
197 $archives = str_replace( ')', ')</span>', $archives );
198
199 $dropdown_id = esc_attr( uniqid( 'wp-block-archives-' ) );
200 $ew_archives = '';
201
202 if ( ! empty( $atts['is_block'] ) && ! empty( $atts['title'] ) ) {
203 $ew_archives .= '<h2 class="ew-archive-block-title">' . esc_html( $atts['title'] ) . '</h2>';
204 }
205
206 if ( ! empty( $atts['before'] ) ) {
207 $ew_archives .= wp_kses_post( $atts['before'] );
208 }
209
210 if ( 'option' === $atts['format'] ) {
211 $class = 'wp-block-archives-dropdown';
212 switch ( $atts['type'] ) {
213 case 'yearly': $label = __( 'Select Year', 'essential-widgets' ); break;
214 case 'monthly': $label = __( 'Select Month', 'essential-widgets' ); break;
215 case 'daily': $label = __( 'Select Day', 'essential-widgets' ); break;
216 case 'weekly': $label = __( 'Select Week', 'essential-widgets' ); break;
217 default: $label = __( 'Select Post', 'essential-widgets' ); break;
218 }
219 $label = esc_html( $label );
220
221 $block_content = '<label class="screen-reader-text" for="' . $dropdown_id . '">' . esc_html( $atts['title'] ) . '</label>
222 <select id="' . $dropdown_id . '" name="archive-dropdown" onchange="document.location.href=this.options[this.selectedIndex].value;">
223 <option value="">' . $label . '</option>' . $archives . '</select>';
224
225 $ew_archives .= '<div class="' . esc_attr( $class ) . '">' . wp_kses_post( $block_content ) . '</div>';
226 } elseif ( 'html' === $atts['format'] ) {
227 $ew_archives .= '<ul class="ew-archives">' . $archives . '</ul>';
228 } elseif ( 'custom' === $atts['format'] ) {
229 $ew_archives .= $archives;
230 }
231
232 if ( ! empty( $atts['after'] ) ) {
233 $ew_archives .= wp_kses_post( $atts['after'] );
234 }
235
236 return $ew_archives;
237 }
238 }
239 endif;
240
241 if ( ! function_exists( 'ew_archives_register' ) ) :
242 function ew_archives_register() { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- ew_ is this plugin's abbreviated prefix; wrapped in function_exists() guard.
243 register_widget( 'EW_Archives' );
244 }
245 add_action( 'widgets_init', 'ew_archives_register' );
246 endif;
247