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