PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.3.3
Post Views Counter v1.3.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 / columns.php
post-views-counter / includes Last commit date
ajax.php 7 years ago columns.php 5 years ago counter.php 5 years ago crawler-detect.php 6 years ago cron.php 6 years ago dashboard.php 5 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
columns.php
466 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 public function __construct() {
14 // actions
15 add_action( 'admin_init', array( $this, 'register_new_column' ) );
16 add_action( 'post_submitbox_misc_actions', array( $this, 'submitbox_views' ) );
17 add_action( 'attachment_submitbox_misc_actions', array( $this, 'submitbox_views' ) );
18 add_action( 'save_post', array( $this, 'save_post' ), 10, 2 );
19 add_action( 'edit_attachment', array( $this, 'save_post' ), 10 );
20 add_action( 'bulk_edit_custom_box', array( $this, 'quick_edit_custom_box' ), 10, 2 );
21 add_action( 'quick_edit_custom_box', array( $this, 'quick_edit_custom_box' ), 10, 2 );
22 add_action( 'wp_ajax_save_bulk_post_views', array( $this, 'save_bulk_post_views' ) );
23
24 // gutenberg
25 add_action( 'plugins_loaded', array( $this, 'init_gutemberg' ) );
26 }
27
28 /**
29 * Init Gutenberg
30 */
31 public function init_gutemberg() {
32 $block_editor = has_action( 'enqueue_block_assets' );
33 $gutenberg = function_exists( 'gutenberg_can_edit_post_type' );
34
35 if ( ! $block_editor && ! $gutenberg ) {
36 return;
37 }
38
39 add_action( 'add_meta_boxes', array( $this, 'gutenberg_add_meta_box' ) );
40 add_action( 'rest_api_init', array( $this, 'gutenberg_rest_api_init' ) );
41 add_action( 'enqueue_block_editor_assets', array( $this, 'gutenberg_enqueue_scripts' ) );
42 }
43
44 /**
45 * Register Gutenberg Metabox.
46 */
47 public function gutenberg_add_meta_box() {
48 add_meta_box( 'post_views_meta_box', __( 'Post Views', 'post-views-counter' ), '', 'post', '', '', array(
49 '__back_compat_meta_box' => false,
50 '__block_editor_compatible_meta_box' => true
51 ) );
52 }
53
54 /**
55 * Register REST API Gutenberg endpoints.
56 */
57 public function gutenberg_rest_api_init() {
58 // get views route
59 register_rest_route(
60 'post-views-counter',
61 '/update-post-views/',
62 array(
63 'methods' => array( 'POST' ),
64 'callback' => array( $this, 'gutenberg_update_callback' ),
65 'permission_callback' => array( $this, 'check_rest_route_permissions' ),
66 'args' => array(
67 'id' => array(
68 'sanitize_callback' => 'absint',
69 )
70 )
71 )
72 );
73 }
74
75 /**
76 * Check whether user has permissions to perform post views update in Gutenberg editor.
77 *
78 * @param object $request WP_REST_Request
79 * @return bool|WP_Error
80 */
81 public function check_rest_route_permissions( $request ) {
82 // break if current user can't edit this post
83 if ( ! current_user_can( 'edit_post', (int) $request->get_param( 'id' ) ) )
84 return new WP_Error( 'pvc-user-not-allowed', __( 'You are not allowed to edit this item.', 'post-views-counter' ) );
85
86 // break if views editing is restricted
87 if ( (bool) Post_Views_Counter()->options['general']['restrict_edit_views'] === true && ! current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) ) )
88 return new WP_Error( 'pvc-user-not-allowed', __( 'You are not allowed to edit this item.', 'post-views-counter' ) );
89
90 return true;
91 }
92
93 /**
94 * REST API Callback for Gutenberg endpoint.
95 *
96 * @param array $data
97 * @return array|int
98 */
99 public function gutenberg_update_callback( $data ) {
100 $post_id = ! empty( $data['id'] ) ? (int) $data['id'] : 0;
101 $post_views = ! empty( $data['post_views'] ) ? (int) $data['post_views'] : 0;
102
103 // get countable post types
104 $post_types = Post_Views_Counter()->options['general']['post_types_count'];
105
106 // check if post exists
107 $post = get_post( $post_id );
108
109 // whether to count this post type or not
110 if ( empty( $post_types ) || empty( $post ) || ! in_array( $post->post_type, $post_types, true ) )
111 return wp_send_json_error( __( 'Invalid post ID.', 'post-views-counter' ) );
112
113 // break if current user can't edit this post
114 if ( ! current_user_can( 'edit_post', $post_id ) )
115 return wp_send_json_error( __( 'You are not allowed to edit this item.', 'post-views-counter' ) );
116
117 // break if views editing is restricted
118 if ( (bool) Post_Views_Counter()->options['general']['restrict_edit_views'] === true && ! current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) ) )
119 return wp_send_json_error( __( 'You are not allowed to edit this item.', 'post-views-counter' ) );
120
121 global $wpdb;
122
123 pvc_update_post_views( $post_id, $post_views );
124
125 do_action( 'pvc_after_update_post_views_count', $post_id );
126
127 return $post_id;
128 }
129
130 /**
131 * Enqueue front end and editor JavaScript and CSS
132 */
133 public function gutenberg_enqueue_scripts() {
134 // enqueue the bundled block JS file
135 wp_enqueue_script(
136 'pvc-gutenberg',
137 POST_VIEWS_COUNTER_URL . '/js/gutenberg.min.js',
138 array( 'wp-i18n', 'wp-edit-post', 'wp-element', 'wp-editor', 'wp-components', 'wp-data', 'wp-plugins', 'wp-api' ),
139 Post_Views_Counter()->defaults['version']
140 );
141
142 // restrict editing
143 $restrict = (bool) Post_Views_Counter()->options['general']['restrict_edit_views'];
144 $can_edit = $restrict === false || ( $restrict === true && current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) ) );
145
146 $js_args = array(
147 'postID' => get_the_ID(),
148 'postViews' => pvc_get_post_views( get_the_ID() ),
149 'canEdit' => $can_edit,
150 'nonce' => wp_create_nonce( 'wp_rest' ),
151 'textPostViews' => __( 'Post Views', 'post-views-counter' ),
152 'textHelp' => __( 'Adjust the views count for this post.', 'post-views-counter' ),
153 'textCancel' => __( 'Cancel', 'post-views-counter' )
154 );
155
156 wp_localize_script(
157 'pvc-gutenberg',
158 'pvcEditorArgs',
159 $js_args
160 );
161
162 // enqueue frontend and editor block styles
163 wp_enqueue_style(
164 'pvc-gutenberg',
165 POST_VIEWS_COUNTER_URL . '/css/gutenberg.min.css', '',
166 Post_Views_Counter()->defaults['version']
167 );
168 }
169
170 /**
171 * Output post views for single post.
172 *
173 * @global object $post
174 * @return mixed
175 */
176 public function submitbox_views() {
177 global $post;
178
179 if ( ! in_array( $post->post_type, (array) Post_Views_Counter()->options['general']['post_types_count'] ) )
180 return;
181
182 // break if current user can't edit this post
183 if ( ! current_user_can( 'edit_post', $post->ID ) )
184 return;
185
186 // get total post views
187 $count = (int) pvc_get_post_views( $post->ID ); ?>
188
189 <div class="misc-pub-section" id="post-views">
190
191 <?php wp_nonce_field( 'post_views_count', 'pvc_nonce' ); ?>
192
193 <span id="post-views-display">
194 <?php echo __( 'Post Views', 'post-views-counter' ) . ': <b>' . number_format_i18n( $count ) . '</b>'; ?>
195 </span>
196
197 <?php
198 // restrict editing
199 $restrict = (bool) Post_Views_Counter()->options['general']['restrict_edit_views'];
200
201 if ( $restrict === false || ( $restrict === true && current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) ) ) ) {
202 ?>
203 <a href="#post-views" class="edit-post-views hide-if-no-js"><?php _e( 'Edit', 'post-views-counter' ); ?></a>
204
205 <div id="post-views-input-container" class="hide-if-js">
206
207 <p><?php _e( 'Adjust the views count for this post.', 'post-views-counter' ); ?></p>
208 <input type="hidden" name="current_post_views" id="post-views-current" value="<?php echo $count; ?>" />
209 <input type="text" name="post_views" id="post-views-input" value="<?php echo $count; ?>"/><br />
210 <p>
211 <a href="#post-views" class="save-post-views hide-if-no-js button"><?php _e( 'OK', 'post-views-counter' ); ?></a>
212 <a href="#post-views" class="cancel-post-views hide-if-no-js"><?php _e( 'Cancel', 'post-views-counter' ); ?></a>
213 </p>
214
215 </div>
216 <?php
217 }
218 ?>
219
220 </div>
221 <?php
222 }
223
224 /**
225 * Save post views data.
226 *
227 * @param int $post_id
228 * @param object $post
229 */
230 public function save_post( $post_id, $post = null ) {
231 if ( is_null( $post ) )
232 $post_type = get_post_type( $post_id );
233 else
234 $post_type = $post->post_type;
235
236 // break if doing autosave
237 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
238 return $post_id;
239
240 // break if current user can't edit this post
241 if ( ! current_user_can( 'edit_post', $post_id ) )
242 return $post_id;
243
244 // is post views set
245 if ( ! isset( $_POST['post_views'] ) )
246 return $post_id;
247
248 // cast numeric post views
249 $post_views = (int) $_POST['post_views'];
250
251 // unchanged post views value?
252 if ( isset( $_POST['current_post_views'] ) && $post_views === (int) $_POST['current_post_views'] )
253 return $post_id;
254
255 // break if post views in not one of the selected
256 $post_types = Post_Views_Counter()->options['general']['post_types_count'];
257
258 if ( ! in_array( $post_type, (array) $post_types ) )
259 return $post_id;
260
261 // break if views editing is restricted
262 $restrict = (bool) Post_Views_Counter()->options['general']['restrict_edit_views'];
263
264 if ( $restrict === true && ! current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) ) )
265 return $post_id;
266
267 // validate data
268 if ( ! isset( $_POST['pvc_nonce'] ) || ! wp_verify_nonce( $_POST['pvc_nonce'], 'post_views_count' ) )
269 return $post_id;
270
271 pvc_update_post_views( $post_id, $post_views );
272
273 do_action( 'pvc_after_update_post_views_count', $post_id );
274 }
275
276 /**
277 * Register post views column for specific post types
278 */
279 public function register_new_column() {
280 $post_types = Post_Views_Counter()->options['general']['post_types_count'];
281
282 if ( ! empty( $post_types ) ) {
283 foreach ( $post_types as $post_type ) {
284 if ( $post_type === 'attachment' ) {
285 // actions
286 add_action( 'manage_media_custom_column', array( $this, 'add_new_column_content' ), 10, 2 );
287
288 // filters
289 add_filter( 'manage_media_columns', array( $this, 'add_new_column' ) );
290 add_filter( 'manage_upload_sortable_columns', array( $this, 'register_sortable_custom_column' ) );
291 } else {
292 // actions
293 add_action( 'manage_' . $post_type . '_posts_custom_column', array( $this, 'add_new_column_content' ), 10, 2 );
294
295 // filters
296 add_filter( 'manage_' . $post_type . '_posts_columns', array( $this, 'add_new_column' ) );
297 add_filter( 'manage_edit-' . $post_type . '_sortable_columns', array( $this, 'register_sortable_custom_column' ) );
298
299 if ( class_exists( 'bbPress' ) ) {
300 if ( $post_type === 'forum' )
301 add_filter( 'bbp_admin_forums_column_headers', array( $this, 'add_new_column' ) );
302 elseif ( $post_type === 'topic' )
303 add_filter( 'bbp_admin_topics_column_headers', array( $this, 'add_new_column' ) );
304 }
305 }
306 }
307 }
308 }
309
310 /**
311 * Register sortable post views column.
312 *
313 * @param array $columns
314 * @return array
315 */
316 public function register_sortable_custom_column( $columns ) {
317 // add new sortable column
318 $columns['post_views'] = 'post_views';
319
320 return $columns;
321 }
322
323 /**
324 * Add post views column.
325 *
326 * @param array $columns
327 * @return array
328 */
329 public function add_new_column( $columns ) {
330 $offset = 0;
331
332 if ( isset( $columns['date'] ) )
333 $offset++;
334
335 if ( isset( $columns['comments'] ) )
336 $offset++;
337
338 if ( $offset > 0 ) {
339 $date = array_slice( $columns, -$offset, $offset, true );
340
341 foreach ( $date as $column => $name ) {
342 unset( $columns[$column] );
343 }
344
345 $columns['post_views'] = '<span class="dash-icon dashicons dashicons-chart-bar" title="' . __( 'Post Views', 'post-views-counter' ) . '"></span><span class="dash-title">' . __( 'Post Views', 'post-views-counter' ) . '</span>';
346
347 foreach ( $date as $column => $name ) {
348 $columns[$column] = $name;
349 }
350 } else
351 $columns['post_views'] = '<span class="dash-icon dashicons dashicons-chart-bar" title="' . __( 'Post Views', 'post-views-counter' ) . '"></span><span class="dash-title">' . __( 'Post Views', 'post-views-counter' ) . '</span>';
352
353 return $columns;
354 }
355
356 /**
357 * Add post views column content.
358 *
359 * @param string $column_name
360 * @param int $id
361 * @return muxed
362 */
363 public function add_new_column_content( $column_name, $id ) {
364 if ( $column_name === 'post_views' ) {
365 // get total post views
366 $count = pvc_get_post_views( $id );
367
368 echo $count;
369 }
370 }
371
372 /**
373 * Handle quick edit.
374 *
375 * @global string $pagenow
376 * @param string $column_name
377 * @return mixed
378 */
379 function quick_edit_custom_box( $column_name, $post_type ) {
380 global $pagenow, $post;
381
382 if ( $pagenow !== 'edit.php' )
383 return;
384
385 if ( $column_name !== 'post_views' )
386 return;
387
388 if ( ! Post_Views_Counter()->options['general']['post_views_column'] || ! in_array( $post_type, Post_Views_Counter()->options['general']['post_types_count'] ) )
389 return;
390
391 // break if views editing is restricted
392 $restrict = (bool) Post_Views_Counter()->options['general']['restrict_edit_views'];
393
394 if ( $restrict === true && ! current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) ) )
395 return;
396
397 ?>
398 <fieldset class="inline-edit-col-left">
399 <div id="inline-edit-post_views" class="inline-edit-col">
400 <label class="inline-edit-group">
401 <span class="title"><?php _e( 'Post Views', 'post-views-counter' ); ?></span>
402 <span class="input-text-wrap"><input type="text" name="post_views" class="title text" value=""></span>
403 <input type="hidden" name="current_post_views" value="" />
404 <?php wp_nonce_field( 'post_views_count', 'pvc_nonce' ); ?>
405 </label>
406 </div>
407 </fieldset>
408 <?php
409 }
410
411 /**
412 * Bulk save post views.
413 *
414 * @global object $wpdb;
415 * @return type
416 */
417 function save_bulk_post_views() {
418 if ( ! isset( $_POST['post_views'] ) )
419 $count = null;
420 else {
421 $count = trim( $_POST['post_views'] );
422
423 if ( is_numeric( $_POST['post_views'] ) ) {
424 $count = (int) $_POST['post_views'];
425
426 if ( $count < 0 )
427 $count = 0;
428 } else
429 $count = null;
430 }
431
432 $post_ids = ( ! empty( $_POST['post_ids'] ) && is_array( $_POST['post_ids'] ) ) ? array_map( 'absint', $_POST['post_ids'] ) : array();
433
434 if ( is_null( $count ) )
435 exit;
436
437 // break if views editing is restricted
438 $restrict = (bool) Post_Views_Counter()->options['general']['restrict_edit_views'];
439
440 if ( $restrict === true && ! current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) ) )
441 exit;
442
443 if ( ! empty( $post_ids ) ) {
444 foreach ( $post_ids as $post_id ) {
445
446 // break if current user can't edit this post
447 if ( ! current_user_can( 'edit_post', $post_id ) )
448 continue;
449
450 global $wpdb;
451
452 // insert or update db post views count
453 $wpdb->query(
454 $wpdb->prepare( "
455 INSERT INTO " . $wpdb->prefix . "post_views (id, type, period, count)
456 VALUES (%d, %d, %s, %d)
457 ON DUPLICATE KEY UPDATE count = %d", $post_id, 4, 'total', $count, $count
458 )
459 );
460 }
461 }
462
463 exit;
464 }
465 }
466