PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.2.14
Post Views Counter v1.2.14
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 7 years ago counter.php 7 years ago crawler-detect.php 7 years ago cron.php 7 years ago dashboard.php 7 years ago frontend.php 7 years ago functions.php 7 years ago query.php 7 years ago settings.php 7 years ago update.php 7 years ago widgets.php 7 years ago
columns.php
299 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
23 /**
24 * Output post views for single post.
25 *
26 * @global object $post
27 * @return mixed
28 */
29 public function submitbox_views() {
30 global $post;
31
32 $post_types = Post_Views_Counter()->options['general']['post_types_count'];
33
34 if ( ! in_array( $post->post_type, (array) $post_types ) )
35 return;
36
37 // break if current user can't edit this post
38 if ( ! current_user_can( 'edit_post', $post->ID ) )
39 return;
40
41 // get total post views
42 $count = pvc_get_post_views( $post->ID );
43 ?>
44
45 <div class="misc-pub-section" id="post-views">
46
47 <?php wp_nonce_field( 'post_views_count', 'pvc_nonce' ); ?>
48
49 <span id="post-views-display">
50 <?php echo __( 'Post Views', 'post-views-counter' ) . ': <b>' . number_format_i18n( (int) $count ) . '</b>'; ?>
51 </span>
52
53 <?php
54 // restrict editing
55 $restrict = (bool) Post_Views_Counter()->options['general']['restrict_edit_views'];
56
57 if ( $restrict === false || ( $restrict === true && current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) ) ) ) {
58 ?>
59 <a href="#post-views" class="edit-post-views hide-if-no-js"><?php _e( 'Edit', 'post-views-counter' ); ?></a>
60
61 <div id="post-views-input-container" class="hide-if-js">
62
63 <p><?php _e( 'Adjust the views count for this post.', 'post-views-counter' ); ?></p>
64 <input type="hidden" name="current_post_views" id="post-views-current" value="<?php echo (int) $count; ?>" />
65 <input type="text" name="post_views" id="post-views-input" value="<?php echo (int) $count; ?>"/><br />
66 <p>
67 <a href="#post-views" class="save-post-views hide-if-no-js button"><?php _e( 'OK', 'post-views-counter' ); ?></a>
68 <a href="#post-views" class="cancel-post-views hide-if-no-js"><?php _e( 'Cancel', 'post-views-counter' ); ?></a>
69 </p>
70
71 </div>
72 <?php
73 }
74 ?>
75
76 </div>
77 <?php
78 }
79
80 /**
81 * Save post views data.
82 *
83 * @param int $post_id
84 * @param object $post
85 */
86 public function save_post( $post_id, $post ) {
87 // break if doing autosave
88 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
89 return $post_id;
90
91 // break if current user can't edit this post
92 if ( ! current_user_can( 'edit_post', $post_id ) )
93 return $post_id;
94
95 // is post views set
96 if ( ! isset( $_POST['post_views'] ) )
97 return $post_id;
98
99 // break if post views in not one of the selected
100 $post_types = Post_Views_Counter()->options['general']['post_types_count'];
101
102 if ( ! in_array( $post->post_type, (array) $post_types ) )
103 return $post_id;
104
105 // break if views editing is restricted
106 $restrict = (bool) Post_Views_Counter()->options['general']['restrict_edit_views'];
107
108 if ( $restrict === true && ! current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) ) )
109 return $post_id;
110
111 // validate data
112 if ( ! isset( $_POST['pvc_nonce'] ) || ! wp_verify_nonce( $_POST['pvc_nonce'], 'post_views_count' ) )
113 return $post_id;
114
115 global $wpdb;
116
117 $count = apply_filters( 'pvc_update_post_views_count', absint( $_POST['post_views'] ), $post_id );
118
119 // insert or update db post views count
120 $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 ) );
121
122 do_action( 'pvc_after_update_post_views_count', $post_id );
123 }
124
125 /**
126 * Register post views column for specific post types
127 */
128 public function register_new_column() {
129 $post_types = Post_Views_Counter()->options['general']['post_types_count'];
130
131 if ( ! empty( $post_types ) ) {
132 foreach ( $post_types as $post_type ) {
133 // actions
134 add_action( 'manage_' . $post_type . '_posts_custom_column', array( $this, 'add_new_column_content' ), 10, 2 );
135
136 // filters
137 add_filter( 'manage_' . $post_type . '_posts_columns', array( $this, 'add_new_column' ) );
138 add_filter( 'manage_edit-' . $post_type . '_sortable_columns', array( $this, 'register_sortable_custom_column' ) );
139 }
140 }
141 }
142
143 /**
144 * Register sortable post views column.
145 *
146 * @param array $columns
147 * @return array
148 */
149 public function register_sortable_custom_column( $columns ) {
150 // add new sortable column
151 $columns['post_views'] = 'post_views';
152
153 return $columns;
154 }
155
156 /**
157 * Add post views column.
158 *
159 * @param array $columns
160 * @return array
161 */
162 public function add_new_column( $columns ) {
163 $offset = 0;
164
165 if ( isset( $columns['date'] ) )
166 $offset ++;
167
168 if ( isset( $columns['comments'] ) )
169 $offset ++;
170
171 if ( $offset > 0 ) {
172 $date = array_slice( $columns, -$offset, $offset, true );
173
174 foreach ( $date as $column => $name ) {
175 unset( $columns[$column] );
176 }
177
178 $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>';
179
180 foreach ( $date as $column => $name ) {
181 $columns[$column] = $name;
182 }
183 } else
184 $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>';
185
186 return $columns;
187 }
188
189 /**
190 * Add post views column content.
191 *
192 * @param string $column_name
193 * @param int $id
194 * @return muxed
195 */
196 public function add_new_column_content( $column_name, $id ) {
197 if ( $column_name === 'post_views' ) {
198 // get total post views
199 $count = pvc_get_post_views( $id );
200
201 echo $count;
202 }
203 }
204
205 /**
206 * Handle quick edit.
207 *
208 * @global string $pagenow
209 * @param string $column_name
210 * @return mixed
211 */
212 function quick_edit_custom_box( $column_name, $post_type ) {
213 global $pagenow, $post;
214
215 if ( $pagenow !== 'edit.php' )
216 return;
217
218 if ( $column_name != 'post_views' )
219 return;
220
221 if ( ! Post_Views_Counter()->options['general']['post_views_column'] || ! in_array( $post_type, Post_Views_Counter()->options['general']['post_types_count'] ) )
222 return;
223
224 // break if views editing is restricted
225 $restrict = (bool) Post_Views_Counter()->options['general']['restrict_edit_views'];
226
227 if ( $restrict === true && ! current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) ) )
228 return;
229
230 ?>
231 <fieldset class="inline-edit-col-left">
232 <div id="inline-edit-post_views" class="inline-edit-col">
233 <label class="inline-edit-group">
234 <span class="title"><?php _e( 'Post Views', 'post-views-counter' ); ?></span>
235 <span class="input-text-wrap"><input type="text" name="post_views" class="title text" value=""></span>
236 <?php wp_nonce_field( 'post_views_count', 'pvc_nonce' ); ?>
237 </label>
238 </div>
239 </fieldset>
240 <?php
241 }
242
243 /**
244 * Bulk save post views.
245 *
246 * @global object $wpdb;
247 * @return type
248 */
249 function save_bulk_post_views() {
250 if ( ! isset( $_POST['post_views'] ) ) {
251 $count = null;
252 } else {
253 $count = trim( $_POST['post_views'] );
254
255 if ( is_numeric( $_POST['post_views'] ) ) {
256 $count = (int) $_POST['post_views'];
257
258 if ( $count < 0 )
259 $count = 0;
260 } else {
261 $count = null;
262 }
263 }
264
265 $post_ids = ( ! empty( $_POST['post_ids'] ) && is_array( $_POST['post_ids'] ) ) ? array_map( 'absint', $_POST['post_ids'] ) : array();
266
267 if ( is_null( $count ) )
268 exit;
269
270 // break if views editing is restricted
271 $restrict = (bool) Post_Views_Counter()->options['general']['restrict_edit_views'];
272
273 if ( $restrict === true && ! current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) ) )
274 exit;
275
276 if ( ! empty( $post_ids ) ) {
277 foreach ( $post_ids as $post_id ) {
278
279 // break if current user can't edit this post
280 if ( ! current_user_can( 'edit_post', $post_id ) )
281 continue;
282
283 global $wpdb;
284
285 // insert or update db post views count
286 $wpdb->query(
287 $wpdb->prepare( "
288 INSERT INTO " . $wpdb->prefix . "post_views (id, type, period, count)
289 VALUES (%d, %d, %s, %d)
290 ON DUPLICATE KEY UPDATE count = %d", $post_id, 4, 'total', $count, $count
291 )
292 );
293 }
294 }
295
296 exit;
297 }
298 }
299