1
+ <?php
2
+ /**
3
+ * Counter Functions.
4
+ */
5
+
6
+ class WP_Post_Views_Counter_Functions {
7
+ public $ options ;
8
+ public $ meta_key ;
9
+ public $ total_views_transient_key ;
10
+ public $ total_views_transient_expiration ;
11
+
12
+ public function __construct ()
13
+ {
14
+ $ this ->load ();
15
+ }
16
+ /**
17
+ * Initialize the plugin.
18
+ *
19
+ * @return void
20
+ */
21
+ public function load () {
22
+
23
+ $ this ->options = get_option ( 'wppv_api_settings ' );
24
+ $ this ->meta_key = 'entry_views ' ;
25
+ $ this ->total_views_transient_key = 'wppv_post_total_views ' ;
26
+ $ this ->total_views_transient_expiration = 1 * MINUTE_IN_SECONDS ;
27
+
28
+ // add_action( 'wp_head', array( $this, 'counter' ), 10, 1 );
29
+ add_filter ( 'manage_posts_columns ' , array ( $ this , 'wppv_posts_column_views ' ) );
30
+ add_filter ( 'manage_pages_columns ' , array ( $ this , 'wppv_posts_column_views ' ) );
31
+ add_action ( 'manage_posts_custom_column ' , array ( $ this , 'wppv_posts_custom_column_views ' ) );
32
+ add_action ( 'manage_pages_custom_column ' , array ( $ this , 'wppv_posts_custom_column_views ' ) );
33
+ add_action ( "wp_ajax_wppv_counter " , array ( $ this , 'ajax_functions ' ) );
34
+ add_action ( "wp_ajax_nopriv_wppv_counter " , array ( $ this , 'ajax_functions ' ) );
35
+ add_action ( "wp_enqueue_scripts " , array ( $ this , 'register_scripts ' ) );
36
+ }
37
+
38
+ public function wppv_posts_column_views ( $ columns ) {
39
+
40
+ if ( ! empty ( $ this ->options ['wppv_api_text_field_0 ' ] ) ) {
41
+ $ columns ['post_views ' ] = 'Views ' ;
42
+ }
43
+ return $ columns ;
44
+ }
45
+
46
+ public function wppv_posts_custom_column_views ( $ column ) {
47
+ $ this ->options = get_option ( 'wppv_api_settings ' );
48
+ if ( !empty ($ this ->options ['wppv_api_text_field_0 ' ]) ) {
49
+ if ( $ column === 'post_views ' ) {
50
+ $ view_post_meta = get_post_meta (get_the_ID (), 'entry_views ' , true );
51
+ echo esc_html ( $ view_post_meta );
52
+ }
53
+ }
54
+
55
+ }
56
+
57
+ public function get_ip_address ()
58
+ {
59
+ // Check for shared internet/ISP IP
60
+ if (isset ($ _SERVER ['HTTP_CLIENT_IP ' ])) {
61
+ $ client_ip = filter_var ($ _SERVER ['HTTP_CLIENT_IP ' ], FILTER_VALIDATE_IP );
62
+ if (!empty ($ client_ip ) && $ this ->validate_ip ($ client_ip )) {
63
+ return $ client_ip ;
64
+ }
65
+ }
66
+
67
+ // Sanitize HTTP_X_FORWARDED_FOR variable
68
+ $ x_forwarded_for = filter_input (INPUT_SERVER , 'HTTP_X_FORWARDED_FOR ' , FILTER_SANITIZE_SPECIAL_CHARS );
69
+ if ($ x_forwarded_for !== null ) {
70
+ $ iplist = explode (', ' , $ x_forwarded_for );
71
+ foreach ($ iplist as $ ip ) {
72
+ $ ip = trim ($ ip ); // Remove any leading/trailing spaces
73
+ if ($ this ->validate_ip ($ ip ))
74
+ return $ ip ;
75
+ }
76
+ }
77
+
78
+ // Check for IPs passing through proxies
79
+ $ proxy_vars = array (
80
+ 'HTTP_X_FORWARDED ' ,
81
+ 'HTTP_X_CLUSTER_CLIENT_IP ' ,
82
+ 'HTTP_FORWARDED_FOR ' ,
83
+ 'HTTP_FORWARDED '
84
+ );
85
+
86
+ foreach ($ proxy_vars as $ var ) {
87
+ if (!empty ($ _SERVER [$ var ])) {
88
+ $ ip = filter_var ($ _SERVER [$ var ], FILTER_VALIDATE_IP );
89
+ if ($ ip !== false && $ this ->validate_ip ($ ip ))
90
+ return $ ip ;
91
+ }
92
+ }
93
+
94
+ // Sanitize and validate REMOTE_ADDR variable
95
+ if (isset ($ _SERVER ['REMOTE_ADDR ' ])) {
96
+ $ remote_addr = filter_var ($ _SERVER ['REMOTE_ADDR ' ], FILTER_VALIDATE_IP );
97
+ if ($ remote_addr !== false && $ this ->validate_ip ($ remote_addr )) {
98
+ return $ remote_addr ;
99
+ }
100
+ }
101
+
102
+ // Return unreliable IP since all else failed
103
+ return '' ;
104
+ }
105
+
106
+
107
+ public function validate_ip ($ ip ) {
108
+ if (
109
+ filter_var ( $ ip ,
110
+ FILTER_VALIDATE_IP ,
111
+ FILTER_FLAG_IPV4 |
112
+ FILTER_FLAG_IPV6 |
113
+ FILTER_FLAG_NO_PRIV_RANGE |
114
+ FILTER_FLAG_NO_RES_RANGE
115
+ ) === false
116
+ ) {
117
+ return false ;
118
+ }
119
+ return true ;
120
+ }
121
+
122
+ public function counter ( $ post_id ){
123
+ $ post = get_post ($ post_id );
124
+ $ stored_ip_addresses = 0 ;
125
+ $ selected_type = array ();
126
+ isset ($ this ->options ['wppv_api_post_checkbox_1 ' ]) ? $ selected_type = $ this ->options ['wppv_api_post_checkbox_1 ' ] : '' ;
127
+
128
+ if ( is_object ($ post ) && in_array ($ post ->post_type , $ selected_type )){
129
+ if ( !empty ($ this ->options ['wppv_api_text_field_1 ' ]) ) {
130
+ $ stored_ip_addresses = get_post_meta ($ post ->ID ,'view_ip ' ,true );
131
+
132
+ $ current_ip = $ this ->get_ip_address ();
133
+ if ( $ stored_ip_addresses )
134
+ {
135
+ if (!in_array ($ current_ip , $ stored_ip_addresses ))
136
+ {
137
+ $ view_post_meta = get_post_meta ($ post ->ID , $ this ->meta_key , true );
138
+ $ new_viewed_count = intval ($ view_post_meta ) + 1 ;
139
+ update_post_meta ($ post ->ID , $ this ->meta_key , $ new_viewed_count );
140
+ $ stored_ip_addresses [] = $ current_ip ;
141
+ update_post_meta ($ post ->ID ,'view_ip ' ,$ stored_ip_addresses );
142
+ }
143
+ } else {
144
+ $ stored_ip_addresses = array ();
145
+ $ view_post_meta = get_post_meta ($ post ->ID , $ this ->meta_key , true );
146
+ $ new_viewed_count = intval ($ view_post_meta ) + 1 ;
147
+ update_post_meta ($ post ->ID , $ this ->meta_key , $ new_viewed_count );
148
+ $ stored_ip_addresses [] = $ current_ip ;
149
+ update_post_meta ($ post ->ID ,'view_ip ' ,$ stored_ip_addresses );
150
+ }
151
+ } else {
152
+ $ view_post_meta = get_post_meta ($ post ->ID , $ this ->meta_key , true );
153
+ $ new_viewed_count = intval ($ view_post_meta ) + 1 ;
154
+ update_post_meta ($ post ->ID , $ this ->meta_key , $ new_viewed_count );
155
+ }
156
+ }
157
+
158
+ }
159
+
160
+ private function count_total_view ( $ post_type = 'post ' ) {
161
+ $ total = 0 ;
162
+
163
+ if ( $ total = get_transient ( $ this ->total_views_transient_key .$ post_type ) ) {
164
+ return $ total ;
165
+ }
166
+
167
+ $ arguments = array (
168
+ 'post_type ' => $ post_type ,
169
+ 'posts_per_page ' => '-1 ' ,
170
+ 'status ' => 'publish ' ,
171
+ );
172
+ $ total_count_query = new WP_Query ( $ arguments );
173
+
174
+ if ( $ total_count_query ->have_posts () ){
175
+ while ( $ total_count_query ->have_posts () ) {
176
+ $ total_count_query ->the_post ();
177
+ $ view_post_meta = get_post_meta (get_the_ID (), $ this ->meta_key , true );
178
+ $ total += $ view_post_meta ;
179
+ }
180
+ }
181
+ set_transient ( $ this ->total_views_transient_key .$ post_type , $ total , $ this ->total_views_transient_expiration );
182
+
183
+ return $ total ;
184
+ }
185
+
186
+ public function get_total_views ( $ post_type = 'post ' ) {
187
+ return $ this ->count_total_view ($ post_type );
188
+ }
189
+
190
+ public function register_scripts (){
191
+ wp_register_script (
192
+ 'wp-posts-view-script ' ,
193
+ WP_POST_VIEW_URL .'/assets/js/ajax.js ' ,
194
+ array ('jquery ' ),
195
+ false ,
196
+ true
197
+ );
198
+ wp_enqueue_script ('wp-posts-view-script ' );
199
+
200
+ $ localised_array = array (
201
+ 'ajaxurl ' => admin_url ('admin-ajax.php ' ),
202
+ 'nonce ' => wp_create_nonce ('wp-post-view-nonce ' ),
203
+ );
204
+
205
+ if ( get_the_ID () && in_array ( get_post_type ( get_the_ID () ), get_post_types () )){
206
+ $ localised_array ['post_id ' ] = get_the_ID ();
207
+ }
208
+
209
+ wp_localize_script (
210
+ 'wp-posts-view-script ' ,
211
+ 'wp_post_views_ajax_object ' ,
212
+ $ localised_array ,
213
+ );
214
+ }
215
+
216
+ public function ajax_functions (){
217
+
218
+ $ post_id = intval (( $ _POST ['post_id ' ] ));
219
+
220
+ if ( ! wp_verify_nonce ( $ _POST ['nonce ' ], 'wp-post-view-nonce ' ) ) {
221
+ wp_send_json_success ('1 ' );
222
+ }
223
+ $ this ->counter ($ post_id );
224
+ wp_send_json_success ('1 ' );
225
+ // wp_die(); // ajax call must die to avoid trailing 0 in your response
226
+ }
227
+ }
0 commit comments