PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.4.6
Post Views Counter v1.4.6
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 2 years ago class-columns.php 2 years ago class-counter.php 2 years ago class-crawler-detect.php 2 years ago class-cron.php 2 years ago class-dashboard.php 2 years ago class-frontend.php 2 years ago class-functions.php 2 years ago class-query.php 2 years ago class-settings-api.php 2 years ago class-settings.php 2 years ago class-update.php 2 years ago class-widgets.php 2 years ago functions.php 2 years ago
class-widgets.php
251 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 private $pvc_list_types;
43 private $pvc_image_sizes;
44
45 /**
46 * Class constructor.
47 *
48 * @return void
49 */
50 public function __construct() {
51 // call parent
52 parent::__construct(
53 'Post_Views_Counter_List_Widget',
54 __( 'Most Viewed Posts', 'post-views-counter' ),
55 [
56 'description' => __( 'Displays a list of the most viewed posts', 'post-views-counter' )
57 ]
58 );
59
60 // default settings
61 $this->pvc_defaults = [
62 'title' => __( 'Most Viewed Posts', 'post-views-counter' ),
63 'number_of_posts' => 5,
64 'thumbnail_size' => 'thumbnail',
65 'post_type' => [],
66 'order' => 'desc',
67 'list_type' => 'unordered',
68 'show_post_views' => true,
69 'show_post_thumbnail' => false,
70 'show_post_excerpt' => false,
71 'show_post_author' => false,
72 'no_posts_message' => __( 'No most viewed posts found', 'post-views-counter' )
73 ];
74
75 // order types
76 $this->pvc_order_types = [
77 'asc' => __( 'Ascending', 'post-views-counter' ),
78 'desc' => __( 'Descending', 'post-views-counter' )
79 ];
80
81 // list types
82 $this->pvc_list_types = [
83 'unordered' => __( 'Unordered list', 'post-views-counter' ),
84 'ordered' => __( 'Ordered list', 'post-views-counter' )
85 ];
86
87 // image sizes
88 $this->pvc_image_sizes = array_merge( [ 'full' ], get_intermediate_image_sizes() );
89
90 // sort image sizes by name, ascending
91 sort( $this->pvc_image_sizes, SORT_STRING );
92 }
93
94 /**
95 * Display widget.
96 *
97 * @param array $args
98 * @param array $instance
99 * @return void
100 */
101 public function widget( $args, $instance ) {
102 // empty title?
103 if ( empty( $instance['title'] ) )
104 $instance['title'] = $this->pvc_defaults['title'];
105
106 // filter title
107 $instance['title'] = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
108
109 $html = $args['before_widget'] . ( ! empty( $instance['title'] ) ? $args['before_title'] . esc_html( $instance['title'] ) . $args['after_title'] : '' );
110 $html .= pvc_most_viewed_posts( $instance, false );
111 $html .= $args['after_widget'];
112
113 echo $html;
114 }
115
116 /** Render widget form.
117 *
118 * @param array $instance
119 * @return void
120 */
121 public function form( $instance ) {
122 $html = '
123 <p>
124 <label for="' . esc_attr( $this->get_field_id( 'title' ) ) . '">' . esc_html__( 'Title', 'post-views-counter' ) . ':</label>
125 <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'] ) . '" />
126 </p>
127 <p>
128 <label>' . esc_html__( 'Post Types', 'post-views-counter' ) . ':</label><br />';
129
130 // post types
131 foreach ( Post_Views_Counter()->functions->get_post_types() as $post_type => $post_type_name ) {
132 $html .= '
133 <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>';
134 }
135
136 $show_post_thumbnail = isset( $instance['show_post_thumbnail'] ) ? $instance['show_post_thumbnail'] : $this->pvc_defaults['show_post_thumbnail'];
137
138 $html .= '
139 </p>
140 <p>
141 <label for="' . esc_attr( $this->get_field_id( 'number_of_posts' ) ) . '">' . esc_html__( 'Number of posts to show', 'post-views-counter' ) . ':</label>
142 <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'] ) . '" />
143 </p>
144 <p>
145 <label for="' . esc_attr( $this->get_field_id( 'no_posts_message' ) ) . '">' . esc_html__( 'No posts message', 'post-views-counter' ) . ':</label>
146 <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'] ) . '" />
147 </p>
148 <p>
149 <label for="' . esc_attr( $this->get_field_id( 'order' ) ) . '">' . esc_html__( 'Order', 'post-views-counter' ) . ':</label>
150 <select id="' . esc_attr( $this->get_field_id( 'order' ) ) . '" name="' . esc_attr( $this->get_field_name( 'order' ) ) . '">';
151
152 // order types
153 foreach ( $this->pvc_order_types as $id => $order ) {
154 $html .= '
155 <option value="' . esc_attr( $id ) . '" ' . selected( $id, ( isset( $instance['order'] ) ? $instance['order'] : $this->pvc_defaults['order'] ), false ) . '>' . esc_html( $order ) . '</option>';
156 }
157
158 $html .= '
159 </select>
160 </p>
161 <p>
162 <label for="' . esc_attr( $this->get_field_id( 'list_type' ) ) . '">' . esc_html__( 'Display Style', 'post-views-counter' ) . ':</label>
163 <select id="' . esc_attr( $this->get_field_id( 'list_type' ) ) . '" name="' . esc_attr( $this->get_field_name( 'list_type' ) ) . '">';
164
165 // list types
166 foreach ( $this->pvc_list_types as $id => $list_type ) {
167 $html .= '
168 <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>';
169 }
170
171 $html .= '
172 </select>
173 </p>
174 <p>
175 <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>
176 <br />
177 <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>
178 <br />
179 <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>
180 <br />
181 <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>
182 </p>
183 <p class="pvc-post-thumbnail-size"' . ( $show_post_thumbnail ? '' : ' style="display: none;"' ) . '>
184 <label for="' . esc_attr( $this->get_field_id( 'thumbnail_size' ) ) . '">' . esc_html__( 'Thumbnail size', 'post-views-counter' ) . ':</label>
185 <select id="' . esc_attr( $this->get_field_id( 'thumbnail_size' ) ) . '" name="' . esc_attr( $this->get_field_name( 'thumbnail_size' ) ) . '">';
186
187 $size_type = isset( $instance['thumbnail_size'] ) ? $instance['thumbnail_size'] : $this->pvc_defaults['thumbnail_size'];
188
189 // image sizes
190 foreach ( $this->pvc_image_sizes as $size ) {
191 $html .= '
192 <option value="' . esc_attr( $size ) . '" ' . selected( $size, $size_type, false ) . '>' . esc_html( $size ) . '</option>';
193 }
194
195 $html .= '
196 </select>
197 </p>';
198
199 echo $html;
200 }
201
202 /**
203 * Save widget form.
204 *
205 * @param array $new_instance
206 * @param array $old_instance
207 * @return array
208 */
209 public function update( $new_instance, $old_instance ) {
210 // number of posts
211 $old_instance['number_of_posts'] = (int) (isset( $new_instance['number_of_posts'] ) ? $new_instance['number_of_posts'] : $this->pvc_defaults['number_of_posts']);
212
213 // order
214 $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'];
215
216 // list type
217 $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'];
218
219 // thumbnail size
220 $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'];
221
222 // booleans
223 $old_instance['show_post_views'] = ! empty( $new_instance['show_post_views'] );
224 $old_instance['show_post_thumbnail'] = ! empty( $new_instance['show_post_thumbnail'] );
225 $old_instance['show_post_excerpt'] = ! empty( $new_instance['show_post_excerpt'] );
226 $old_instance['show_post_author'] = ! empty( $new_instance['show_post_author'] );
227
228 // texts
229 $old_instance['title'] = sanitize_text_field( isset( $new_instance['title'] ) ? $new_instance['title'] : $this->pvc_defaults['title'] );
230 $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'] );
231
232 // post types
233 if ( isset( $new_instance['post_type'] ) ) {
234 $post_types = [];
235
236 // get post types
237 $_post_types = Post_Views_Counter()->functions->get_post_types();
238
239 foreach ( $new_instance['post_type'] as $post_type ) {
240 if ( isset( $_post_types[$post_type] ) )
241 $post_types[] = $post_type;
242 }
243
244 $old_instance['post_type'] = array_unique( $post_types );
245 } else
246 $old_instance['post_type'] = [ 'post' ];
247
248 return $old_instance;
249 }
250 }
251