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