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