PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.0.12
Post Views Counter v1.0.12
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 11 years ago counter.php 11 years ago cron.php 11 years ago frontend.php 11 years ago functions.php 11 years ago query.php 11 years ago settings.php 11 years ago update.php 11 years ago widgets.php 11 years ago
columns.php
227 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( 'current_screen', 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 }
15
16 /**
17 * Output post views for single post.
18 */
19 public function submitbox_views() {
20 global $post;
21
22 $post_types = Post_Views_Counter()->get_attribute( 'options', 'general', 'post_types_count' );
23
24 if ( ! in_array( $post->post_type, (array) $post_types ) )
25 return;
26
27 // break if current user can't edit this post
28 if ( ! current_user_can( 'edit_post', $post->ID ) )
29 return;
30
31 global $wpdb;
32
33 // get total post views
34 $views = $wpdb->get_var(
35 $wpdb->prepare( "
36 SELECT count
37 FROM " . $wpdb->prefix . "post_views
38 WHERE id = %d AND type = 4", absint( $post->ID )
39 )
40 );
41 ?>
42
43 <div class="misc-pub-section" id="post-views">
44
45 <?php wp_nonce_field( 'post_views_count', 'pvc_nonce' ); ?>
46
47 <span id="post-views-display">
48
49 <?php echo __( 'Post Views', 'post-views-counter' ) . ': <b>' . number_format_i18n( (int) $views ) . '</b>'; ?>
50
51 </span>
52
53 <?php // restrict editing
54 $restrict = (bool) Post_Views_Counter()->get_attribute( 'options', 'general', 'restrict_edit_views' );
55
56 if ( $restrict === false || ( $restrict === true && current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) ) ) ) {
57 ?>
58 <a href="#post-views" class="edit-post-views hide-if-no-js"><?php _e( 'Edit', 'post-views-counter' ); ?></a>
59
60 <div id="post-views-input-container" class="hide-if-js">
61
62 <p><?php _e( 'Adjust the views count for this post.', 'post-views-counter' ); ?></p>
63 <input type="hidden" name="current_post_views" id="post-views-current" value="<?php echo (int) $views; ?>" />
64 <input type="text" name="post_views" id="post-views-input" value="<?php echo (int) $views; ?>"/><br />
65 <p>
66 <a href="#post-views" class="save-post-views hide-if-no-js button"><?php _e( 'OK', 'post-views-counter' ); ?></a>
67 <a href="#post-views" class="cancel-post-views hide-if-no-js"><?php _e( 'Cancel', 'post-views-counter' ); ?></a>
68 </p>
69
70 </div>
71 <?php
72 }
73 ?>
74
75 </div>
76 <?php
77 }
78
79 /**
80 * Save post views data
81 */
82 public function save_post( $post_id, $post ) {
83
84 // break if doing autosave
85 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
86 return $post_id;
87
88 // break if current user can't edit this post
89 if ( ! current_user_can( 'edit_post', $post_id ) )
90 return $post_id;
91
92 // is post views set
93 if ( ! isset( $_POST['post_views'] ) )
94 return $post_id;
95
96 // break if post views in not one of the selected
97 $post_types = Post_Views_Counter()->get_attribute( 'options', 'general', 'post_types_count' );
98
99 if ( ! in_array( $post->post_type, (array) $post_types ) )
100 return $post_id;
101
102 // break if views editing is restricted
103 $restrict = (bool) Post_Views_Counter()->get_attribute( 'options', 'general', 'restrict_edit_views' );
104
105 if ( $restrict === true && ! current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) ) )
106 return $post_id;
107
108 // validate data
109 if ( ! isset( $_POST['pvc_nonce'] ) || ! wp_verify_nonce( $_POST['pvc_nonce'], 'post_views_count' ) )
110 return $post_id;
111
112 global $wpdb;
113
114 $count = apply_filters( 'pvc_update_post_views_count', absint( $_POST['post_views'] ), $post_id );
115
116 // insert or update db post views count
117 $wpdb->query(
118 $wpdb->prepare( "
119 INSERT INTO " . $wpdb->prefix . "post_views (id, type, period, count)
120 VALUES (%d, %d, %s, %d)
121 ON DUPLICATE KEY UPDATE count = %d", $post_id, 4, 'total', $count, $count
122 )
123 );
124
125 do_action( 'pvc_after_update_post_views_count', $post_id );
126 }
127
128 /**
129 * Register post views column for specific post types
130 */
131 public function register_new_column() {
132 $screen = get_current_screen();
133
134 if ( Post_Views_Counter()->get_attribute( 'options', 'general', 'post_views_column' ) && ($screen->base == 'edit' && in_array( $screen->post_type, Post_Views_Counter()->get_attribute( 'options', 'general', 'post_types_count' ) )) ) {
135
136 foreach ( Post_Views_Counter()->get_attribute( 'options', 'general', 'post_types_count' ) as $post_type ) {
137
138 if ( $post_type === 'page' && $screen->post_type === 'page' ) {
139 // actions
140 add_action( 'manage_pages_custom_column', array( &$this, 'add_new_column_content' ), 10, 2 );
141
142 // filters
143 add_filter( 'manage_pages_columns', array( &$this, 'add_new_column' ) );
144 add_filter( 'manage_edit-page_sortable_columns', array( &$this, 'register_sortable_custom_column' ) );
145 } elseif ( $post_type === 'post' && $screen->post_type === 'post' ) {
146 // actions
147 add_action( 'manage_posts_custom_column', array( &$this, 'add_new_column_content' ), 10, 2 );
148
149 // filters
150 add_filter( 'manage_posts_columns', array( &$this, 'add_new_column' ) );
151 add_filter( 'manage_edit-post_sortable_columns', array( &$this, 'register_sortable_custom_column' ) );
152 } elseif ( $screen->post_type === $post_type ) {
153 // actions
154 add_action( 'manage_' . $post_type . '_posts_custom_column', array( &$this, 'add_new_column_content' ), 10, 2 );
155
156 // filters
157 add_filter( 'manage_' . $post_type . '_posts_columns', array( &$this, 'add_new_column' ) );
158 add_filter( 'manage_edit-' . $post_type . '_sortable_columns', array( &$this, 'register_sortable_custom_column' ) );
159 }
160 }
161 }
162 }
163
164 /**
165 * Register sortable post views column
166 */
167 public function register_sortable_custom_column( $columns ) {
168 // add new sortable column
169 $columns['post_views'] = 'post_views';
170
171 return $columns;
172 }
173
174 /**
175 * Add post views column
176 */
177 public function add_new_column( $columns ) {
178 $offset = 0;
179
180 if ( isset( $columns['date'] ) )
181 $offset ++;
182
183 if ( isset( $columns['comments'] ) )
184 $offset ++;
185
186 if ( $offset > 0 ) {
187 $date = array_slice( $columns, -$offset, $offset, true );
188
189 foreach ( $date as $column => $name ) {
190 unset( $columns[$column] );
191 }
192
193 $columns['post_views'] = __( 'Post Views', 'post-views-counter' );
194
195 foreach ( $date as $column => $name ) {
196 $columns[$column] = $name;
197 }
198 } else
199 $columns['post_views'] = __( 'Post Views', 'post-views-counter' );
200
201 return $columns;
202 }
203
204 /**
205 * Add post views column content
206 */
207 public function add_new_column_content( $column_name, $id ) {
208
209 if ( $column_name === 'post_views' ) {
210
211 global $wpdb;
212
213 // get total post views
214 $views = $wpdb->get_var(
215 $wpdb->prepare( "
216 SELECT count
217 FROM " . $wpdb->prefix . "post_views
218 WHERE id = %d AND type = 4", $id
219 )
220 );
221
222 echo number_format_i18n( (int) $views );
223 }
224 }
225
226 }
227