PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.5.7
Post Views Counter v1.5.7
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 8 months ago class-columns.php 8 months ago class-counter.php 8 months ago class-crawler-detect.php 8 months ago class-cron.php 8 months ago class-dashboard.php 8 months ago class-frontend.php 8 months ago class-functions.php 8 months ago class-query.php 8 months ago class-settings-api.php 8 months ago class-settings.php 8 months ago class-update.php 8 months ago class-widgets.php 8 months ago functions.php 8 months ago
class-widgets.php
274 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 * Display widget.
103 *
104 * @param array $args
105 * @param array $instance
106 * @return void
107 */
108 public function widget( $args, $instance ) {
109 // empty title?
110 if ( empty( $instance['title'] ) )
111 $instance['title'] = $this->pvc_defaults['title'];
112
113 // filter title
114 $instance['title'] = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
115
116 $html = $args['before_widget'] . ( ! empty( $instance['title'] ) ? $args['before_title'] . esc_html( $instance['title'] ) . $args['after_title'] : '' );
117 $html .= pvc_most_viewed_posts( $instance, false );
118 $html .= $args['after_widget'];
119
120 echo $html;
121 }
122
123 /** Render widget form.
124 *
125 * @param array $instance
126 * @return void
127 */
128 public function form( $instance ) {
129 $html = '
130 <p>
131 <label for="' . esc_attr( $this->get_field_id( 'title' ) ) . '">' . esc_html__( 'Title', 'post-views-counter' ) . ':</label>
132 <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'] ) . '" />
133 </p>
134 <p>
135 <label>' . esc_html__( 'Post Types', 'post-views-counter' ) . ':</label><br />';
136
137 // post types
138 foreach ( Post_Views_Counter()->functions->get_post_types() as $post_type => $post_type_name ) {
139 $html .= '
140 <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>';
141 }
142
143 $html .= '
144 </p>
145 <p>
146 <label for="' . esc_attr( $this->get_field_id( 'period' ) ) . '">' . esc_html__( 'Views Period', 'post-views-counter' ) . ':</label>
147 <select id="' . esc_attr( $this->get_field_id( 'period' ) ) . '" name="' . esc_attr( $this->get_field_name( 'period' ) ) . '">';
148
149 // periods
150 foreach ( $this->pvc_periods as $period => $label ) {
151 $html .= '
152 <option value="' . esc_attr( $period ) . '" ' . selected( $period, ( isset( $instance['period'] ) ? $instance['period'] : $this->pvc_defaults['period'] ), false ) . '>' . esc_html( $label ) . '</option>';
153 }
154
155 $show_post_thumbnail = isset( $instance['show_post_thumbnail'] ) ? $instance['show_post_thumbnail'] : $this->pvc_defaults['show_post_thumbnail'];
156
157 $html .= '
158 </select>
159 </p>
160 <p>
161 <label for="' . esc_attr( $this->get_field_id( 'number_of_posts' ) ) . '">' . esc_html__( 'Number of posts to show', 'post-views-counter' ) . ':</label>
162 <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'] ) . '" />
163 </p>
164 <p>
165 <label for="' . esc_attr( $this->get_field_id( 'no_posts_message' ) ) . '">' . esc_html__( 'No posts message', 'post-views-counter' ) . ':</label>
166 <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'] ) . '" />
167 </p>
168 <p>
169 <label for="' . esc_attr( $this->get_field_id( 'order' ) ) . '">' . esc_html__( 'Order', 'post-views-counter' ) . ':</label>
170 <select id="' . esc_attr( $this->get_field_id( 'order' ) ) . '" name="' . esc_attr( $this->get_field_name( 'order' ) ) . '">';
171
172 // order types
173 foreach ( $this->pvc_order_types as $id => $order ) {
174 $html .= '
175 <option value="' . esc_attr( $id ) . '" ' . selected( $id, ( isset( $instance['order'] ) ? $instance['order'] : $this->pvc_defaults['order'] ), false ) . '>' . esc_html( $order ) . '</option>';
176 }
177
178 $html .= '
179 </select>
180 </p>
181 <p>
182 <label for="' . esc_attr( $this->get_field_id( 'list_type' ) ) . '">' . esc_html__( 'Display Style', 'post-views-counter' ) . ':</label>
183 <select id="' . esc_attr( $this->get_field_id( 'list_type' ) ) . '" name="' . esc_attr( $this->get_field_name( 'list_type' ) ) . '">';
184
185 // list types
186 foreach ( $this->pvc_list_types as $id => $list_type ) {
187 $html .= '
188 <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>';
189 }
190
191 $html .= '
192 </select>
193 </p>
194 <p>
195 <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>
196 <br />
197 <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>
198 <br />
199 <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>
200 <br />
201 <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>
202 </p>
203 <p class="pvc-post-thumbnail-size"' . ( $show_post_thumbnail ? '' : ' style="display: none;"' ) . '>
204 <label for="' . esc_attr( $this->get_field_id( 'thumbnail_size' ) ) . '">' . esc_html__( 'Thumbnail size', 'post-views-counter' ) . ':</label>
205 <select id="' . esc_attr( $this->get_field_id( 'thumbnail_size' ) ) . '" name="' . esc_attr( $this->get_field_name( 'thumbnail_size' ) ) . '">';
206
207 $size_type = isset( $instance['thumbnail_size'] ) ? $instance['thumbnail_size'] : $this->pvc_defaults['thumbnail_size'];
208
209 // image sizes
210 foreach ( $this->pvc_image_sizes as $size ) {
211 $html .= '
212 <option value="' . esc_attr( $size ) . '" ' . selected( $size, $size_type, false ) . '>' . esc_html( $size ) . '</option>';
213 }
214
215 $html .= '
216 </select>
217 </p>';
218
219 echo $html;
220 }
221
222 /**
223 * Save widget form.
224 *
225 * @param array $new_instance
226 * @param array $old_instance
227 * @return array
228 */
229 public function update( $new_instance, $old_instance ) {
230 // number of posts
231 $old_instance['number_of_posts'] = (int) (isset( $new_instance['number_of_posts'] ) ? $new_instance['number_of_posts'] : $this->pvc_defaults['number_of_posts']);
232
233 // order
234 $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'];
235
236 // period
237 $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'];
238
239 // list type
240 $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'];
241
242 // thumbnail size
243 $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'];
244
245 // booleans
246 $old_instance['show_post_views'] = ! empty( $new_instance['show_post_views'] );
247 $old_instance['show_post_thumbnail'] = ! empty( $new_instance['show_post_thumbnail'] );
248 $old_instance['show_post_excerpt'] = ! empty( $new_instance['show_post_excerpt'] );
249 $old_instance['show_post_author'] = ! empty( $new_instance['show_post_author'] );
250
251 // texts
252 $old_instance['title'] = sanitize_text_field( isset( $new_instance['title'] ) ? $new_instance['title'] : $this->pvc_defaults['title'] );
253 $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'] );
254
255 // post types
256 if ( isset( $new_instance['post_type'] ) ) {
257 $post_types = [];
258
259 // get post types
260 $_post_types = Post_Views_Counter()->functions->get_post_types();
261
262 foreach ( $new_instance['post_type'] as $post_type ) {
263 if ( isset( $_post_types[$post_type] ) )
264 $post_types[] = $post_type;
265 }
266
267 $old_instance['post_type'] = array_unique( $post_types );
268 } else
269 $old_instance['post_type'] = [ 'post' ];
270
271 return $old_instance;
272 }
273 }
274