PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / trunk
Post Views Counter vtrunk
1.7.13 1.7.12 1.7.11 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.2.0 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3 1.3.1 1.3.10 1.3.11 1.3.12 1.3.13 1.3.2 1.3.2.1 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.7.0 1.7.1 1.7.10 1.7.2 1.7.3 1.7.4 1.7.5 1.7.6 1.7.7 1.7.8 1.7.9
post-views-counter / includes / class-widgets.php
post-views-counter / includes Last commit date
class-admin.php 3 months ago class-columns-modal.php 1 month ago class-columns.php 2 weeks ago class-counter.php 2 months ago class-crawler-detect.php 7 months ago class-cron.php 1 month ago class-dashboard.php 1 month ago class-emails-mailer.php 1 month ago class-emails-period.php 1 month ago class-emails-query.php 1 month ago class-emails-scheduler.php 1 month ago class-emails-template.php 1 month ago class-emails.php 1 month ago class-frontend.php 2 weeks ago class-functions.php 1 year ago class-import.php 2 months ago class-integration-gutenberg.php 6 months ago class-integrations.php 2 months ago class-query.php 2 months ago class-settings-api.php 1 month ago class-settings-display.php 4 months ago class-settings-emails.php 1 month ago class-settings-general.php 1 month ago class-settings-integrations.php 5 months ago class-settings-other.php 1 month ago class-settings-reports.php 1 month ago class-settings.php 1 month ago class-toolbar.php 3 months ago class-traffic-signals.php 2 months ago class-update.php 1 month ago class-widgets.php 1 month ago functions.php 1 month ago
class-widgets.php
300 lines
1 <?php
2 // exit if accessed directly
3 if ( ! defined( 'ABSPATH' ) )
4 exit;
5
6 /**
7 * Post_Views_Counter_Widgets class.
8 *
9 * @class Post_Views_Counter_Widgets
10 */
11 class Post_Views_Counter_Widgets {
12
13 /**
14 * Class constructor.
15 *
16 * @return void
17 */
18 public function __construct() {
19 // actions
20 add_action( 'widgets_init', [ $this, 'register_widgets' ] );
21 }
22
23 /**
24 * Register widgets.
25 *
26 * @return void
27 */
28 public function register_widgets() {
29 register_widget( 'Post_Views_Counter_List_Widget' );
30 }
31 }
32
33 /**
34 * Post_Views_Counter_List_Widget class.
35 *
36 * @class Post_Views_Counter_List_Widget
37 */
38 class Post_Views_Counter_List_Widget extends WP_Widget {
39
40 private $pvc_defaults;
41 private $pvc_order_types;
42 public $pvc_periods;
43 private $pvc_list_types;
44 private $pvc_image_sizes;
45
46 /**
47 * Class constructor.
48 *
49 * @return void
50 */
51 public function __construct() {
52 // call parent
53 parent::__construct(
54 'Post_Views_Counter_List_Widget',
55 __( 'Most Viewed Posts', 'post-views-counter' ),
56 [
57 'description' => __( 'Displays a list of the most viewed posts', 'post-views-counter' )
58 ]
59 );
60
61 // default settings
62 $this->pvc_defaults = [
63 'title' => __( 'Most Viewed Posts', 'post-views-counter' ),
64 'number_of_posts' => 5,
65 'period' => 'total',
66 'thumbnail_size' => 'thumbnail',
67 'post_type' => [],
68 'order' => 'desc',
69 'list_type' => 'unordered',
70 'show_post_views' => true,
71 'show_post_thumbnail' => false,
72 'show_post_excerpt' => false,
73 'show_post_author' => false,
74 'no_posts_message' => __( 'No most viewed posts found', 'post-views-counter' )
75 ];
76
77 // order types
78 $this->pvc_order_types = [
79 'asc' => __( 'Ascending', 'post-views-counter' ),
80 'desc' => __( 'Descending', 'post-views-counter' )
81 ];
82
83 // periods
84 $this->pvc_periods = [
85 'total' => __( 'Total Views', 'post-views-counter' )
86 ];
87
88 // list types
89 $this->pvc_list_types = [
90 'unordered' => __( 'Unordered list', 'post-views-counter' ),
91 'ordered' => __( 'Ordered list', 'post-views-counter' )
92 ];
93
94 // image sizes
95 $this->pvc_image_sizes = array_merge( [ 'full' ], get_intermediate_image_sizes() );
96
97 // sort image sizes by name, ascending
98 sort( $this->pvc_image_sizes, SORT_STRING );
99 }
100
101 /**
102 * Ensure template helper functions are available for early widget renders.
103 *
104 * @return bool
105 */
106 private function maybe_load_template_functions() {
107 if ( function_exists( 'pvc_most_viewed_posts' ) )
108 return true;
109
110 if ( defined( 'POST_VIEWS_COUNTER_PATH' ) )
111 include_once( POST_VIEWS_COUNTER_PATH . 'includes/functions.php' );
112
113 return function_exists( 'pvc_most_viewed_posts' );
114 }
115
116 /**
117 * Display widget.
118 *
119 * @param array $args
120 * @param array $instance
121 * @return void
122 */
123 public function widget( $args, $instance ) {
124 // empty title?
125 if ( empty( $instance['title'] ) )
126 $instance['title'] = $this->pvc_defaults['title'];
127
128 // filter title
129 $instance['title'] = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
130
131 $html = $args['before_widget'] . ( ! empty( $instance['title'] ) ? $args['before_title'] . esc_html( $instance['title'] ) . $args['after_title'] : '' );
132
133 if ( ! $this->maybe_load_template_functions() ) {
134 $html .= ! empty( $instance['no_posts_message'] ) ? $instance['no_posts_message'] : $this->pvc_defaults['no_posts_message'];
135 $html .= $args['after_widget'];
136
137 echo wp_kses_post( $html );
138
139 return;
140 }
141
142 $html .= pvc_most_viewed_posts( $instance, false );
143 $html .= $args['after_widget'];
144
145 echo wp_kses_post( $html );
146 }
147
148 /** Render widget form.
149 *
150 * @param array $instance
151 * @return void
152 */
153 public function form( $instance ) {
154 $html = '
155 <p>
156 <label for="' . esc_attr( $this->get_field_id( 'title' ) ) . '">' . esc_html__( 'Title', 'post-views-counter' ) . ':</label>
157 <input id="' . esc_attr( $this->get_field_id( 'title' ) ) . '" class="widefat" name="' . esc_attr( $this->get_field_name( 'title' ) ) . '" type="text" value="' . esc_attr( isset( $instance['title'] ) ? $instance['title'] : $this->pvc_defaults['title'] ) . '" />
158 </p>
159 <p>
160 <label>' . esc_html__( 'Post Types', 'post-views-counter' ) . ':</label><br />';
161
162 // post types
163 foreach ( Post_Views_Counter()->functions->get_post_types() as $post_type => $post_type_name ) {
164 $html .= '
165 <input id="' . esc_attr( $this->get_field_id( 'post_type' ) . '-' . $post_type ) . '" type="checkbox" name="' . esc_attr( $this->get_field_name( 'post_type' ) ) . '[]" value="' . esc_attr( $post_type ) . '" ' . checked( ( ! isset( $instance['post_type'] ) ? true : in_array( $post_type, $instance['post_type'], true ) ), true, false ) . '><label for="' . esc_attr( $this->get_field_id( 'post_type' ) . '-' . $post_type ) . '">' . esc_html( $post_type_name ) . '</label>';
166 }
167
168 $html .= '
169 </p>
170 <p>
171 <label for="' . esc_attr( $this->get_field_id( 'period' ) ) . '">' . esc_html__( 'Views Period', 'post-views-counter' ) . ':</label>
172 <select id="' . esc_attr( $this->get_field_id( 'period' ) ) . '" name="' . esc_attr( $this->get_field_name( 'period' ) ) . '">';
173
174 // periods
175 foreach ( $this->pvc_periods as $period => $label ) {
176 $html .= '
177 <option value="' . esc_attr( $period ) . '" ' . selected( $period, ( isset( $instance['period'] ) ? $instance['period'] : $this->pvc_defaults['period'] ), false ) . '>' . esc_html( $label ) . '</option>';
178 }
179
180 $show_post_thumbnail = isset( $instance['show_post_thumbnail'] ) ? $instance['show_post_thumbnail'] : $this->pvc_defaults['show_post_thumbnail'];
181
182 $html .= '
183 </select>
184 </p>
185 <p>
186 <label for="' . esc_attr( $this->get_field_id( 'number_of_posts' ) ) . '">' . esc_html__( 'Number of posts to show', 'post-views-counter' ) . ':</label>
187 <input id="' . esc_attr( $this->get_field_id( 'number_of_posts' ) ) . '" name="' . esc_attr( $this->get_field_name( 'number_of_posts' ) ) . '" type="number" size="3" min="0" max="100" value="' . esc_attr( isset( $instance['number_of_posts'] ) ? $instance['number_of_posts'] : $this->pvc_defaults['number_of_posts'] ) . '" />
188 </p>
189 <p>
190 <label for="' . esc_attr( $this->get_field_id( 'no_posts_message' ) ) . '">' . esc_html__( 'No posts message', 'post-views-counter' ) . ':</label>
191 <input id="' . esc_attr( $this->get_field_id( 'no_posts_message' ) ) . '" class="widefat" type="text" name="' . esc_attr( $this->get_field_name( 'no_posts_message' ) ) . '" value="' . esc_attr( isset( $instance['no_posts_message'] ) ? $instance['no_posts_message'] : $this->pvc_defaults['no_posts_message'] ) . '" />
192 </p>
193 <p>
194 <label for="' . esc_attr( $this->get_field_id( 'order' ) ) . '">' . esc_html__( 'Order', 'post-views-counter' ) . ':</label>
195 <select id="' . esc_attr( $this->get_field_id( 'order' ) ) . '" name="' . esc_attr( $this->get_field_name( 'order' ) ) . '">';
196
197 // order types
198 foreach ( $this->pvc_order_types as $id => $order ) {
199 $html .= '
200 <option value="' . esc_attr( $id ) . '" ' . selected( $id, ( isset( $instance['order'] ) ? $instance['order'] : $this->pvc_defaults['order'] ), false ) . '>' . esc_html( $order ) . '</option>';
201 }
202
203 $html .= '
204 </select>
205 </p>
206 <p>
207 <label for="' . esc_attr( $this->get_field_id( 'list_type' ) ) . '">' . esc_html__( 'Display Style', 'post-views-counter' ) . ':</label>
208 <select id="' . esc_attr( $this->get_field_id( 'list_type' ) ) . '" name="' . esc_attr( $this->get_field_name( 'list_type' ) ) . '">';
209
210 // list types
211 foreach ( $this->pvc_list_types as $id => $list_type ) {
212 $html .= '
213 <option value="' . esc_attr( $id ) . '" ' . selected( $id, ( isset( $instance['list_type'] ) ? $instance['list_type'] : $this->pvc_defaults['list_type'] ), false ) . '>' . esc_html( $list_type ) . '</option>';
214 }
215
216 $html .= '
217 </select>
218 </p>
219 <p>
220 <input id="' . esc_attr( $this->get_field_id( 'show_post_views' ) ) . '" type="checkbox" name="' . esc_attr( $this->get_field_name( 'show_post_views' ) ) . '" value="show_post_views" ' . checked( true, ( isset( $instance['show_post_views'] ) ? $instance['show_post_views'] : $this->pvc_defaults['show_post_views'] ), false ) . ' /> <label for="' . esc_attr( $this->get_field_id( 'show_post_views' ) ) . '">' . esc_html__( 'Display post views?', 'post-views-counter' ) . '</label>
221 <br />
222 <input id="' . esc_attr( $this->get_field_id( 'show_post_excerpt' ) ) . '" type="checkbox" name="' . esc_attr( $this->get_field_name( 'show_post_excerpt' ) ) . '" value="show_post_excerpt" ' . checked( true, ( isset( $instance['show_post_excerpt'] ) ? $instance['show_post_excerpt'] : $this->pvc_defaults['show_post_excerpt'] ), false ) . ' /> <label for="' . esc_attr( $this->get_field_id( 'show_post_excerpt' ) ) . '">' . esc_html__( 'Display post excerpt?', 'post-views-counter' ) . '</label>
223 <br />
224 <input id="' . esc_attr( $this->get_field_id( 'show_post_author' ) ) . '" type="checkbox" name="' . esc_attr( $this->get_field_name( 'show_post_author' ) ) . '" value="show_post_author" ' . checked( true, ( isset( $instance['show_post_author'] ) ? $instance['show_post_author'] : $this->pvc_defaults['show_post_author'] ), false ) . ' /> <label for="' . esc_attr( $this->get_field_id( 'show_post_author' ) ) . '">' . esc_html__( 'Display post author?', 'post-views-counter' ) . '</label>
225 <br />
226 <input id="' . esc_attr( $this->get_field_id( 'show_post_thumbnail' ) ) . '" class="pvc-show-post-thumbnail" type="checkbox" name="' . esc_attr( $this->get_field_name( 'show_post_thumbnail' ) ) . '" value="show_post_thumbnail" ' . checked( true, $show_post_thumbnail, false ) . ' /> <label for="' . esc_attr( $this->get_field_id( 'show_post_thumbnail' ) ) . '">' . esc_html__( 'Display post thumbnail?', 'post-views-counter' ) . '</label>
227 </p>
228 <p class="pvc-post-thumbnail-size"' . ( $show_post_thumbnail ? '' : ' style="display: none;"' ) . '>
229 <label for="' . esc_attr( $this->get_field_id( 'thumbnail_size' ) ) . '">' . esc_html__( 'Thumbnail size', 'post-views-counter' ) . ':</label>
230 <select id="' . esc_attr( $this->get_field_id( 'thumbnail_size' ) ) . '" name="' . esc_attr( $this->get_field_name( 'thumbnail_size' ) ) . '">';
231
232 $size_type = isset( $instance['thumbnail_size'] ) ? $instance['thumbnail_size'] : $this->pvc_defaults['thumbnail_size'];
233
234 // image sizes
235 foreach ( $this->pvc_image_sizes as $size ) {
236 $html .= '
237 <option value="' . esc_attr( $size ) . '" ' . selected( $size, $size_type, false ) . '>' . esc_html( $size ) . '</option>';
238 }
239
240 $html .= '
241 </select>
242 </p>';
243
244 // Form output doesn't need wp_kses_post - it's admin-only and inputs are already escaped
245 echo $html;
246 }
247
248 /**
249 * Save widget form.
250 *
251 * @param array $new_instance
252 * @param array $old_instance
253 * @return array
254 */
255 public function update( $new_instance, $old_instance ) {
256 // number of posts
257 $old_instance['number_of_posts'] = (int) (isset( $new_instance['number_of_posts'] ) ? $new_instance['number_of_posts'] : $this->pvc_defaults['number_of_posts']);
258
259 // order
260 $old_instance['order'] = isset( $new_instance['order'] ) && in_array( $new_instance['order'], array_keys( $this->pvc_order_types ), true ) ? $new_instance['order'] : $this->pvc_defaults['order'];
261
262 // period
263 $old_instance['period'] = isset( $new_instance['period'] ) && in_array( $new_instance['period'], array_keys( $this->pvc_periods ), true ) ? $new_instance['period'] : $this->pvc_defaults['period'];
264
265 // list type
266 $old_instance['list_type'] = isset( $new_instance['list_type'] ) && in_array( $new_instance['list_type'], array_keys( $this->pvc_list_types ), true ) ? $new_instance['list_type'] : $this->pvc_defaults['list_type'];
267
268 // thumbnail size
269 $old_instance['thumbnail_size'] = isset( $new_instance['thumbnail_size'] ) && in_array( $new_instance['thumbnail_size'], $this->pvc_image_sizes, true ) ? $new_instance['thumbnail_size'] : $this->pvc_defaults['thumbnail_size'];
270
271 // booleans
272 $old_instance['show_post_views'] = ! empty( $new_instance['show_post_views'] );
273 $old_instance['show_post_thumbnail'] = ! empty( $new_instance['show_post_thumbnail'] );
274 $old_instance['show_post_excerpt'] = ! empty( $new_instance['show_post_excerpt'] );
275 $old_instance['show_post_author'] = ! empty( $new_instance['show_post_author'] );
276
277 // texts
278 $old_instance['title'] = sanitize_text_field( isset( $new_instance['title'] ) ? $new_instance['title'] : $this->pvc_defaults['title'] );
279 $old_instance['no_posts_message'] = sanitize_text_field( isset( $new_instance['no_posts_message'] ) ? $new_instance['no_posts_message'] : $this->pvc_defaults['no_posts_message'] );
280
281 // post types
282 if ( isset( $new_instance['post_type'] ) ) {
283 $post_types = [];
284
285 // get post types
286 $_post_types = Post_Views_Counter()->functions->get_post_types();
287
288 foreach ( $new_instance['post_type'] as $post_type ) {
289 if ( isset( $_post_types[$post_type] ) )
290 $post_types[] = $post_type;
291 }
292
293 $old_instance['post_type'] = array_unique( $post_types );
294 } else
295 $old_instance['post_type'] = [ 'post' ];
296
297 return $old_instance;
298 }
299 }
300