PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.2.3
Post Views Counter v1.2.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 9 years ago counter.php 9 years ago cron.php 9 years ago dashboard.php 9 years ago frontend.php 9 years ago functions.php 9 years ago query.php 9 years ago settings.php 9 years ago update.php 9 years ago widgets.php 9 years ago
columns.php
321 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 // actions
150 add_action( 'manage_' . $post_type . '_posts_custom_column', array( $this, 'add_new_column_content' ), 10, 2 );
151
152 // filters
153 add_filter( 'manage_' . $post_type . '_posts_columns', array( $this, 'add_new_column' ) );
154 add_filter( 'manage_edit-' . $post_type . '_sortable_columns', array( $this, 'register_sortable_custom_column' ) );
155 }
156 }
157 }
158
159 /**
160 * Register sortable post views column.
161 *
162 * @param array $columns
163 * @return array
164 */
165 public function register_sortable_custom_column( $columns ) {
166 // add new sortable column
167 $columns['post_views'] = 'post_views';
168
169 return $columns;
170 }
171
172 /**
173 * Add post views column.
174 *
175 * @param array $columns
176 * @return array
177 */
178 public function add_new_column( $columns ) {
179 $offset = 0;
180
181 if ( isset( $columns['date'] ) )
182 $offset ++;
183
184 if ( isset( $columns['comments'] ) )
185 $offset ++;
186
187 if ( $offset > 0 ) {
188 $date = array_slice( $columns, -$offset, $offset, true );
189
190 foreach ( $date as $column => $name ) {
191 unset( $columns[$column] );
192 }
193
194 $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>';
195
196 foreach ( $date as $column => $name ) {
197 $columns[$column] = $name;
198 }
199 } else
200 $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>';
201
202 return $columns;
203 }
204
205 /**
206 * Add post views column content.
207 *
208 * @global object $wpdb
209 * @param string $column_name
210 * @param int $id
211 * @return muxed
212 */
213 public function add_new_column_content( $column_name, $id ) {
214
215 if ( $column_name === 'post_views' ) {
216
217 global $wpdb;
218
219 // get total post views
220 $count = $wpdb->get_var(
221 $wpdb->prepare( "
222 SELECT count
223 FROM " . $wpdb->prefix . "post_views
224 WHERE id = %d AND type = 4", $id
225 )
226 );
227
228 echo (int) $count;
229 }
230 }
231
232 /**
233 * Handle quick edit.
234 *
235 * @global string $pagenow
236 * @global object $wpdb
237 * @param string $column_name
238 * @return mixed
239 */
240 function quick_edit_custom_box( $column_name, $post_type ) {
241 global $pagenow, $post;
242
243 if ( $pagenow !== 'edit.php' )
244 return;
245
246 if ( ! Post_Views_Counter()->options['general']['post_views_column'] || ! in_array( $post_type, Post_Views_Counter()->options['general']['post_types_count'] ) )
247 return;
248
249 // break if views editing is restricted
250 $restrict = (bool) Post_Views_Counter()->options['general']['restrict_edit_views'];
251
252 if ( $restrict === true && ! current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) ) )
253 return;
254
255 if ( $column_name != 'post_views' )
256 return;
257
258 global $wpdb;
259
260 // get total post views
261 $count = $wpdb->get_var(
262 $wpdb->prepare( "
263 SELECT count
264 FROM " . $wpdb->prefix . "post_views
265 WHERE id = %d AND type = 4", $post->ID
266 )
267 );
268 ?>
269 <fieldset class="inline-edit-col-left">
270 <div id="inline-edit-post_views" class="inline-edit-col">
271 <label class="inline-edit-group">
272 <span class="title"><?php _e( 'Post Views', 'post-views-counter' ); ?></span>
273 <span class="input-text-wrap"><input type="text" name="post_views" class="title text" value="<?php echo absint( $count ); ?>"></span>
274 <?php wp_nonce_field( 'post_views_count', 'pvc_nonce' ); ?>
275 </label>
276 </div>
277 </fieldset>
278 <?php
279 }
280
281 /**
282 * Bulk save post views.
283 *
284 * @global object $wpdb;
285 * @return type
286 */
287 function save_bulk_post_views() {
288
289 $post_ids = ( ! empty( $_POST[ 'post_ids' ] ) && is_array( $_POST['post_ids'] ) ) ? array_map( 'absint', $_POST[ 'post_ids' ] ) : array();
290 $count = ( ! empty( $_POST[ 'post_views' ] ) ) ? absint( $_POST[ 'post_views' ] ) : null;
291
292 // break if views editing is restricted
293 $restrict = (bool) Post_Views_Counter()->options['general']['restrict_edit_views'];
294
295 if ( $restrict === true && ! current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) ) )
296 die();
297
298 if ( ! empty( $post_ids ) ) {
299 foreach ( $post_ids as $post_id ) {
300
301 // break if current user can't edit this post
302 if ( ! current_user_can( 'edit_post', $post_id ) )
303 continue;
304
305 global $wpdb;
306
307 // insert or update db post views count
308 $wpdb->query(
309 $wpdb->prepare( "
310 INSERT INTO " . $wpdb->prefix . "post_views (id, type, period, count)
311 VALUES (%d, %d, %s, %d)
312 ON DUPLICATE KEY UPDATE count = %d", $post_id, 4, 'total', $count, $count
313 )
314 );
315 }
316 }
317 die();
318 }
319
320 }
321