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