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