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