PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.7.10
Post Views Counter v1.7.10
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-columns.php
post-views-counter / includes Last commit date
class-admin.php 2 months ago class-columns-modal.php 2 months ago class-columns.php 2 months ago class-counter.php 2 months ago class-crawler-detect.php 2 months ago class-cron.php 2 months ago class-dashboard.php 2 months ago class-frontend.php 2 months ago class-functions.php 2 months ago class-import.php 2 months ago class-integration-gutenberg.php 2 months ago class-integrations.php 2 months ago class-query.php 2 months ago class-settings-api.php 2 months ago class-settings-display.php 2 months ago class-settings-general.php 2 months ago class-settings-integrations.php 2 months ago class-settings-other.php 2 months ago class-settings-reports.php 2 months ago class-settings.php 2 months ago class-toolbar.php 2 months ago class-traffic-signals.php 2 months ago class-update.php 2 months ago class-widgets.php 2 months ago functions.php 2 months ago
class-columns.php
418 lines
1 <?php
2 // exit if accessed directly
3 if ( ! defined( 'ABSPATH' ) )
4 exit;
5
6 /**
7 * Post_Views_Counter_Columns class.
8 *
9 * @class Post_Views_Counter_Columns
10 */
11 class Post_Views_Counter_Columns {
12
13 /**
14 * Class constructor.
15 *
16 * @return void
17 */
18 public function __construct() {
19 // actions
20 add_action( 'admin_init', [ $this, 'register_new_column' ] );
21 add_action( 'post_submitbox_misc_actions', [ $this, 'submitbox_views' ] );
22 add_action( 'attachment_submitbox_misc_actions', [ $this, 'submitbox_views' ] );
23 add_action( 'save_post', [ $this, 'save_post' ], 10, 2 );
24 add_action( 'edit_attachment', [ $this, 'save_post' ], 10 );
25 add_action( 'bulk_edit_custom_box', [ $this, 'quick_edit_custom_box' ], 10, 2 );
26 add_action( 'quick_edit_custom_box', [ $this, 'quick_edit_custom_box' ], 10, 2 );
27 add_action( 'wp_ajax_save_bulk_post_views', [ $this, 'save_bulk_post_views' ] );
28 }
29 /**
30 * Output post views for single post.
31 *
32 * @global object $post
33 *
34 * @return void
35 */
36 public function submitbox_views() {
37 global $post;
38
39 // get main instance
40 $pvc = Post_Views_Counter();
41 $post_types = (array) $pvc->options['general']['post_types_count'];
42
43 // break if display is not allowed
44 if ( ! $pvc->options['display']['post_views_column'] || ! in_array( $post->post_type, $post_types, true ) )
45 return;
46
47 // check if user can see post stats
48 if ( apply_filters( 'pvc_admin_display_post_views', true, $post->ID ) === false )
49 return;
50
51 // get total post views
52 $count = (int) pvc_get_post_views( $post->ID ); ?>
53
54 <div class="misc-pub-section" id="post-views">
55
56 <?php wp_nonce_field( 'post_views_count', 'pvc_nonce' ); ?>
57
58 <span id="post-views-display">
59 <?php echo __( 'Post Views', 'post-views-counter' ) . ': <b>' . number_format_i18n( $count ) . '</b>'; ?>
60 </span>
61
62 <?php
63 // allow editing
64 $allow_edit = (bool) $pvc->options['display']['restrict_edit_views'];
65
66 // allow editing condition
67 $allow_edit_condition = (bool) current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) );
68
69 if ( $allow_edit === true && $allow_edit_condition === true ) {
70 ?>
71 <a href="#post-views" class="edit-post-views hide-if-no-js"><?php _e( 'Edit', 'post-views-counter' ); ?></a>
72
73 <div id="post-views-input-container" class="hide-if-js">
74
75 <p><?php _e( 'Adjust the views count for this post.', 'post-views-counter' ); ?></p>
76 <input type="hidden" name="current_post_views" id="post-views-current" value="<?php echo esc_attr( $count ); ?>" />
77 <input type="text" name="post_views" id="post-views-input" value="<?php echo esc_attr( $count ); ?>"/><br />
78 <p>
79 <a href="#post-views" class="save-post-views hide-if-no-js button"><?php _e( 'OK', 'post-views-counter' ); ?></a>
80 <a href="#post-views" class="cancel-post-views hide-if-no-js"><?php _e( 'Cancel', 'post-views-counter' ); ?></a>
81 </p>
82
83 </div>
84 <?php
85 }
86 ?>
87
88 </div>
89 <?php
90 }
91
92 /**
93 * Save post views data.
94 *
95 * @param int $post_id
96 * @param object $post
97 * @return void
98 */
99 public function save_post( $post_id, $post = null ) {
100 // break if doing autosave
101 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
102 return;
103
104 // break if current user can't edit this post
105 if ( ! current_user_can( 'edit_post', $post_id ) )
106 return;
107
108 // is post views set
109 if ( ! isset( $_POST['post_views'] ) )
110 return;
111
112 // cast numeric post views
113 $post_views = (int) $_POST['post_views'];
114
115 // unchanged post views value?
116 if ( isset( $_POST['current_post_views'] ) && $post_views === (int) $_POST['current_post_views'] )
117 return;
118
119 // get main instance
120 $pvc = Post_Views_Counter();
121
122 // break if post views in not one of the selected
123 $post_types = (array) $pvc->options['general']['post_types_count'];
124
125 // get post type
126 if ( is_null( $post ) )
127 $post_type = get_post_type( $post_id );
128 else
129 $post_type = $post->post_type;
130
131 // invalid post type?
132 if ( ! in_array( $post_type, $post_types, true ) )
133 return;
134
135 // allow editing
136 $allow_edit = (bool) $pvc->options['display']['restrict_edit_views'];
137
138 // allow editing condition
139 $allow_edit_condition = (bool) current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) );
140
141 // break if views editing not allowed or editing condition not met
142 if ( $allow_edit === false || $allow_edit_condition === false )
143 return;
144
145 // validate data
146 if ( ! isset( $_POST['pvc_nonce'] ) || ! wp_verify_nonce( $_POST['pvc_nonce'], 'post_views_count' ) )
147 return;
148
149 // update post views
150 pvc_update_post_views( $post_id, $post_views );
151
152 do_action( 'pvc_after_update_post_views_count', $post_id );
153 }
154
155 /**
156 * Register post views column for specific post types.
157 *
158 * @return void
159 */
160 public function register_new_column() {
161 // get main instance
162 $pvc = Post_Views_Counter();
163
164 // is posts views column active?
165 if ( ! $pvc->options['display']['post_views_column'] )
166 return false;
167
168 // get post types
169 $post_types = $pvc->options['general']['post_types_count'];
170
171 // any post types?
172 if ( ! empty( $post_types ) ) {
173 foreach ( $post_types as $post_type ) {
174 if ( $post_type === 'attachment' ) {
175 // actions
176 add_action( 'manage_media_custom_column', [ $this, 'add_new_column_content' ], 10, 2 );
177
178 // filters
179 add_filter( 'manage_media_columns', [ $this, 'add_new_column' ] );
180 add_filter( 'manage_upload_sortable_columns', [ $this, 'register_sortable_custom_column' ] );
181 } else {
182 // actions
183 add_action( 'manage_' . $post_type . '_posts_custom_column', [ $this, 'add_new_column_content' ], 10, 2 );
184
185 // filters
186 add_filter( 'manage_' . $post_type . '_posts_columns', [ $this, 'add_new_column' ] );
187 add_filter( 'manage_edit-' . $post_type . '_columns', [ $this, 'add_new_column' ], 20 );
188 add_filter( 'manage_edit-' . $post_type . '_sortable_columns', [ $this, 'register_sortable_custom_column' ] );
189
190 // bbPress?
191 if ( class_exists( 'bbPress' ) ) {
192 if ( $post_type === 'forum' )
193 add_filter( 'bbp_admin_forums_column_headers', [ $this, 'add_new_column' ] );
194 elseif ( $post_type === 'topic' )
195 add_filter( 'bbp_admin_topics_column_headers', [ $this, 'add_new_column' ] );
196 }
197 }
198 }
199 }
200 }
201
202 /**
203 * Register sortable post views column.
204 *
205 * @param array $columns
206 * @return array
207 */
208 public function register_sortable_custom_column( $columns ) {
209 global $post_type;
210
211 // get main instance
212 $pvc = Post_Views_Counter();
213 $post_types = (array) $pvc->options['general']['post_types_count'];
214
215 // break if display is disabled
216 if ( ! $pvc->options['display']['post_views_column'] || ! in_array( $post_type, $post_types, true ) )
217 return $columns;
218
219 // check if user can see stats
220 if ( apply_filters( 'pvc_admin_display_post_views', true ) === false )
221 return $columns;
222
223 // add new sortable column
224 $columns['post_views'] = 'post_views';
225
226 return $columns;
227 }
228
229 /**
230 * Add post views column.
231 *
232 * @param array $columns
233 * @return array
234 */
235 public function add_new_column( $columns ) {
236 // date column exists?
237 if ( isset( $columns['date'] ) ) {
238 // store date column
239 $date = $columns['date'];
240
241 // unset date column
242 unset( $columns['date'] );
243 }
244
245 // comments column exists?
246 if ( isset( $columns['comments'] ) ) {
247 // store comments column
248 $comments = $columns['comments'];
249
250 // unset comments column
251 unset( $columns['comments'] );
252 }
253
254 // add post views column
255 $columns['post_views'] = '<span class="pvc-views-header" title="' . esc_attr__( 'Post Views', 'post-views-counter' ) . '"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" fill="currentColor"><path d="M12 2a1 1 0 0 0-1 1v10a1 1 0 0 0 1 1h1a1 1 0 0 0 1-1V3a1 1 0 0 0-1-1h-1ZM6.5 6a1 1 0 0 1 1-1h1a1 1 0 0 1 1 1v7a1 1 0 0 1-1 1h-1a1 1 0 0 1-1-1V6ZM2 9a1 1 0 0 1 1-1h1a1 1 0 0 1 1 1v4a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V9Z" /></svg><span class="screen-reader-text">' . esc_attr__( 'Post Views', 'post-views-counter' ) . '</span></span>';
256
257 // restore date column
258 if ( isset( $date ) )
259 $columns['date'] = $date;
260
261 // restore comments column
262 if ( isset( $comments ) )
263 $columns['comments'] = $comments;
264
265 return $columns;
266 }
267
268 /**
269 * Add post views column content.
270 *
271 * @param string $column_name
272 * @param int $id
273 * @return void
274 */
275 public function add_new_column_content( $column_name, $id ) {
276 if ( $column_name === 'post_views' ) {
277 // get total post views
278 $count = pvc_get_post_views( $id );
279
280 // check if user can see stats
281 if ( apply_filters( 'pvc_admin_display_post_views', true, $id ) === false ) {
282 echo '';
283 return;
284 }
285
286 // get post title
287 $post_title = get_the_title( $id );
288
289 if ( $post_title === '' )
290 $post_title = __( '(no title)', 'post-views-counter' );
291
292 // get post type labels
293 $post_type_object = get_post_type_object( get_post_type( $id ) );
294
295 if ( $post_type_object ) {
296 $post_type_labels = get_post_type_labels( $post_type_object );
297 }
298
299 if ( $post_type_labels ) {
300 $post_title = $post_type_labels->singular_name . ': ' . $post_title;
301 }
302
303 // clickable link (modal opening handled via JavaScript)
304 echo '<a href="#" class="pvc-view-chart" data-post-id="' . esc_attr( $id ) . '" data-post-title="' . esc_attr( $post_title ) . '">' . esc_html( $count ) . '</a>';
305 }
306 }
307
308 /**
309 * Handle quick edit.
310 *
311 * @global string $pagenow
312 *
313 * @param string $column_name
314 * @param string $post_type
315 * @return void
316 */
317 function quick_edit_custom_box( $column_name, $post_type ) {
318 global $pagenow, $post;
319
320 if ( $pagenow !== 'edit.php' )
321 return;
322
323 if ( $column_name !== 'post_views' )
324 return;
325
326 if ( ! $post )
327 return;
328
329 // get main instance
330 $pvc = Post_Views_Counter();
331 $post_types = (array) $pvc->options['general']['post_types_count'];
332
333 // break if display is not allowed
334 if ( ! $pvc->options['display']['post_views_column'] || ! in_array( $post_type, $post_types, true ) )
335 return;
336
337 // check if user can see stats
338 if ( apply_filters( 'pvc_admin_display_post_views', true, $post->ID ) === false )
339 return;
340
341 // allow editing
342 $allow_edit = (bool) $pvc->options['display']['restrict_edit_views'];
343
344 // allow editing condition
345 $allow_edit_condition = (bool) current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) );
346 ?>
347 <fieldset class="inline-edit-col-left">
348 <div id="inline-edit-post_views" class="inline-edit-col">
349 <label class="inline-edit-group">
350 <span class="title"><?php _e( 'Post Views', 'post-views-counter' ); ?></span>
351 <?php if ( $allow_edit === true && $allow_edit_condition === true ) { ?>
352 <span class="input-text-wrap"><input type="text" name="post_views" class="title text" value=""></span>
353 <input type="hidden" name="current_post_views" value="" />
354 <?php wp_nonce_field( 'post_views_count', 'pvc_nonce' ); ?>
355 <?php } else { ?>
356 <span class="input-text-wrap"><input type="text" name="post_views" class="title text" value="" disabled readonly /></span>
357 <?php } ?>
358 </label>
359 </div>
360 </fieldset>
361 <?php
362 }
363
364 /**
365 * Bulk save post views.
366 *
367 * @global object $wpdb
368 *
369 * @return void
370 */
371 function save_bulk_post_views() {
372 global $wpdb;
373
374 // check nonce
375 if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'pvc_save_bulk_post_views' ) )
376 exit;
377
378 $count = null;
379
380 if ( isset( $_POST['post_views'] ) && is_numeric( trim( $_POST['post_views'] ) ) ) {
381 $count = (int) $_POST['post_views'];
382
383 if ( $count < 0 )
384 $count = 0;
385 }
386
387 // check post ids
388 $post_ids = ( ! empty( $_POST['post_ids'] ) && is_array( $_POST['post_ids'] ) ) ? array_map( 'absint', $_POST['post_ids'] ) : [];
389
390 if ( is_null( $count ) )
391 exit;
392
393 // allow editing
394 $allow_edit = (bool) $pvc->options['display']['restrict_edit_views'];
395
396 // allow editing condition
397 $allow_edit_condition = (bool) current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) );
398
399 // break if views editing not allowed or editing condition not met
400 if ( $allow_edit === false || $allow_edit_condition === false )
401 exit;
402
403 // any post ids?
404 if ( ! empty( $post_ids ) ) {
405 foreach ( $post_ids as $post_id ) {
406 // break if current user can't edit this post
407 if ( ! current_user_can( 'edit_post', $post_id ) )
408 continue;
409
410 // insert or update db post views count
411 $wpdb->query( $wpdb->prepare( "INSERT INTO " . $wpdb->prefix . "post_views (id, type, period, count) VALUES (%d, %d, %s, %d) ON DUPLICATE KEY UPDATE count = %d", $post_id, 4, 'total', $count, $count ) );
412 }
413 }
414
415 exit;
416 }
417 }
418