PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.2.5
Post Views Counter v1.2.5
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 crawler-detect.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
292 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
88 // break if doing autosave
89 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
90 return $post_id;
91
92 // break if current user can't edit this post
93 if ( ! current_user_can( 'edit_post', $post_id ) )
94 return $post_id;
95
96 // is post views set
97 if ( ! isset( $_POST['post_views'] ) )
98 return $post_id;
99
100 // break if post views in not one of the selected
101 $post_types = Post_Views_Counter()->options['general']['post_types_count'];
102
103 if ( ! in_array( $post->post_type, (array) $post_types ) )
104 return $post_id;
105
106 // break if views editing is restricted
107 $restrict = (bool) Post_Views_Counter()->options['general']['restrict_edit_views'];
108
109 if ( $restrict === true && ! current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) ) )
110 return $post_id;
111
112 // validate data
113 if ( ! isset( $_POST['pvc_nonce'] ) || ! wp_verify_nonce( $_POST['pvc_nonce'], 'post_views_count' ) )
114 return $post_id;
115
116 global $wpdb;
117
118 $count = apply_filters( 'pvc_update_post_views_count', absint( $_POST['post_views'] ), $post_id );
119
120 // insert or update db post views count
121 $wpdb->query(
122 $wpdb->prepare( "
123 INSERT INTO " . $wpdb->prefix . "post_views (id, type, period, count)
124 VALUES (%d, %d, %s, %d)
125 ON DUPLICATE KEY UPDATE count = %d", $post_id, 4, 'total', $count, $count
126 )
127 );
128
129 do_action( 'pvc_after_update_post_views_count', $post_id );
130 }
131
132 /**
133 * Register post views column for specific post types
134 */
135 public function register_new_column() {
136 $post_types = Post_Views_Counter()->options['general']['post_types_count'];
137
138 if ( ! empty( $post_types ) ) {
139 foreach ( $post_types as $post_type ) {
140 // actions
141 add_action( 'manage_' . $post_type . '_posts_custom_column', array( $this, 'add_new_column_content' ), 10, 2 );
142
143 // filters
144 add_filter( 'manage_' . $post_type . '_posts_columns', array( $this, 'add_new_column' ) );
145 add_filter( 'manage_edit-' . $post_type . '_sortable_columns', array( $this, 'register_sortable_custom_column' ) );
146 }
147 }
148 }
149
150 /**
151 * Register sortable post views column.
152 *
153 * @param array $columns
154 * @return array
155 */
156 public function register_sortable_custom_column( $columns ) {
157 // add new sortable column
158 $columns['post_views'] = 'post_views';
159
160 return $columns;
161 }
162
163 /**
164 * Add post views column.
165 *
166 * @param array $columns
167 * @return array
168 */
169 public function add_new_column( $columns ) {
170 $offset = 0;
171
172 if ( isset( $columns['date'] ) )
173 $offset ++;
174
175 if ( isset( $columns['comments'] ) )
176 $offset ++;
177
178 if ( $offset > 0 ) {
179 $date = array_slice( $columns, -$offset, $offset, true );
180
181 foreach ( $date as $column => $name ) {
182 unset( $columns[$column] );
183 }
184
185 $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>';
186
187 foreach ( $date as $column => $name ) {
188 $columns[$column] = $name;
189 }
190 } else
191 $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>';
192
193 return $columns;
194 }
195
196 /**
197 * Add post views column content.
198 *
199 * @param string $column_name
200 * @param int $id
201 * @return muxed
202 */
203 public function add_new_column_content( $column_name, $id ) {
204 if ( $column_name === 'post_views' ) {
205 // get total post views
206 $count = pvc_get_post_views( $id );
207
208 echo $count;
209 }
210 }
211
212 /**
213 * Handle quick edit.
214 *
215 * @global string $pagenow
216 * @param string $column_name
217 * @return mixed
218 */
219 function quick_edit_custom_box( $column_name, $post_type ) {
220 global $pagenow, $post;
221
222 if ( $pagenow !== 'edit.php' )
223 return;
224
225 if ( $column_name != 'post_views' )
226 return;
227
228 if ( ! Post_Views_Counter()->options['general']['post_views_column'] || ! in_array( $post_type, Post_Views_Counter()->options['general']['post_types_count'] ) )
229 return;
230
231 // break if views editing is restricted
232 $restrict = (bool) Post_Views_Counter()->options['general']['restrict_edit_views'];
233
234 if ( $restrict === true && ! current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) ) )
235 return;
236
237 // get total post views
238 $count = $count = pvc_get_post_views( $post->ID );
239 ?>
240 <fieldset class="inline-edit-col-left">
241 <div id="inline-edit-post_views" class="inline-edit-col">
242 <label class="inline-edit-group">
243 <span class="title"><?php _e( 'Post Views', 'post-views-counter' ); ?></span>
244 <span class="input-text-wrap"><input type="text" name="post_views" class="title text" value="<?php echo absint( $count ); ?>"></span>
245 <?php wp_nonce_field( 'post_views_count', 'pvc_nonce' ); ?>
246 </label>
247 </div>
248 </fieldset>
249 <?php
250 }
251
252 /**
253 * Bulk save post views.
254 *
255 * @global object $wpdb;
256 * @return type
257 */
258 function save_bulk_post_views() {
259
260 $post_ids = ( ! empty( $_POST['post_ids'] ) && is_array( $_POST['post_ids'] ) ) ? array_map( 'absint', $_POST['post_ids'] ) : array();
261 $count = ( ! empty( $_POST['post_views'] ) ) ? absint( $_POST['post_views'] ) : null;
262
263 // break if views editing is restricted
264 $restrict = (bool) Post_Views_Counter()->options['general']['restrict_edit_views'];
265
266 if ( $restrict === true && ! current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) ) )
267 die();
268
269 if ( ! empty( $post_ids ) ) {
270 foreach ( $post_ids as $post_id ) {
271
272 // break if current user can't edit this post
273 if ( ! current_user_can( 'edit_post', $post_id ) )
274 continue;
275
276 global $wpdb;
277
278 // insert or update db post views count
279 $wpdb->query(
280 $wpdb->prepare( "
281 INSERT INTO " . $wpdb->prefix . "post_views (id, type, period, count)
282 VALUES (%d, %d, %s, %d)
283 ON DUPLICATE KEY UPDATE count = %d", $post_id, 4, 'total', $count, $count
284 )
285 );
286 }
287 }
288 die();
289 }
290
291 }
292