Skip to content

Commit ffb59b0

Browse files
authored
Merge pull request #29 from vanpariyar/28-it-is-sowing-php-errors
28 it is sowing php errors
2 parents 071b233 + c801a28 commit ffb59b0

File tree

7 files changed

+284
-196
lines changed

7 files changed

+284
-196
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use Docker To use this Plugin setup. (Not Compulasory)
99
$ cd wp-post-views
1010
$ docker-compose up -d
1111
```
12-
Goto : [localhost:9999/](http://localhost:9999/)
12+
Goto : [localhost:8080/](http://localhost:8080/)
1313

1414
Live URL: https://wordpress.org/plugins/wp-post-views/
1515

assets/js/ajax.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
jQuery(document).ready(function ($) {
2+
if( $('body').hasClass('archive')){
3+
return;
4+
}
5+
$.ajax({
6+
url: wp_post_views_ajax_object.ajaxurl, // this is the object instantiated in wp_localize_script function
7+
type: 'POST',
8+
data: {
9+
action: 'wppv_counter', // this is the function in your functions.php that will be triggered
10+
post_id: wp_post_views_ajax_object.post_id,
11+
nonce: wp_post_views_ajax_object.nonce,
12+
},
13+
success: function (data) {
14+
//Do something with the result from server
15+
// console.log(data);
16+
}
17+
});
18+
});

docker-compose.yml

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,26 @@ services:
55
wordpress:
66
image: wordpress
77
ports:
8-
- 9999:80
8+
- 8080:80
99
environment:
10-
WORDPRESS_DB_PASSWORD: example
11-
WORDPRESS_CONFIG_EXTRA:
12-
define( 'WP_DEBUG', true );
13-
14-
10+
WORDPRESS_DB_HOST: db
11+
WORDPRESS_DB_USER: exampleuser
12+
WORDPRESS_DB_PASSWORD: examplepass
13+
WORDPRESS_DB_NAME: exampledb
14+
WORDPRESS_DEBUG: '1'
1515
volumes:
1616
- ./:/var/www/html/wp-content/plugins/wp-post-views
1717

18-
mysql:
19-
image: mysql:5.7
18+
db:
19+
image: mysql:8.0
2020
environment:
21-
MYSQL_ROOT_PASSWORD: example
21+
MYSQL_DATABASE: exampledb
22+
MYSQL_USER: exampleuser
23+
MYSQL_PASSWORD: examplepass
24+
MYSQL_RANDOM_ROOT_PASSWORD: '1'
25+
volumes:
26+
- db:/var/lib/mysql
2227

23-
# phpmyadmin
24-
phpmyadmin:
25-
depends_on:
26-
- mysql
27-
image: phpmyadmin/phpmyadmin
28-
restart: always
29-
ports:
30-
- '9998:80'
31-
environment:
32-
PMA_HOST: mysql
33-
MYSQL_ROOT_PASSWORD: example
28+
volumes:
29+
wordpress:
30+
db:

includes/counter.php

Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
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+
}

includes/settings.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,13 @@ public static function select_post_type_callback( ) {
8585
/* BUILT IN POST TYPE PAGE AND POST OPTION */
8686
?>
8787
<label for="">Posts </label>
88-
<input type='checkbox' name='wppv_api_settings[wppv_api_post_checkbox_1][post]' value="post" <?php checked( ( 'post' == @$checkbox_val['post']), true ); ?>>
89-
<label for="">Pages </label><input type='checkbox' name='wppv_api_settings[wppv_api_post_checkbox_1][page]' value="page" <?php checked( ( 'page' == @$checkbox_val['page']), true ); ?>>
88+
<input type='checkbox' name='wppv_api_settings[wppv_api_post_checkbox_1][post]' value="post" <?php checked( ( isset($checkbox_val['post']) && 'post' == @$checkbox_val['post']), true ); ?>>
89+
<label for="">Pages </label><input type='checkbox' name='wppv_api_settings[wppv_api_post_checkbox_1][page]' value="page" <?php checked( ( isset($checkbox_val['page']) && 'page' == @$checkbox_val['page']), true ); ?>>
9090
<?php
9191
foreach($post_types as $post_type){
9292
?>
9393
<label for=""><?php echo esc_html( $post_type->label );?> </label>
94-
<input type="checkbox" name="wppv_api_settings[wppv_api_post_checkbox_1][<?php echo esc_attr( $post_type->name ); ?>]" value="<?php echo esc_attr( $post_type->name ); ?>" <?php checked( ( $post_type->name == @$checkbox_val[$post_type->name] ), true ); ?>>
94+
<input type="checkbox" name="wppv_api_settings[wppv_api_post_checkbox_1][<?php echo esc_attr( $post_type->name ); ?>]" value="<?php echo esc_attr( $post_type->name ); ?>" <?php checked( ( isset($checkbox_val[$post_type->name]) && $post_type->name == @$checkbox_val[$post_type->name] ), true ); ?>>
9595
<?php
9696
}
9797
}

readme.txt

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
=== Wp Post Views - Wordpress Post views counter ===
22
Contributors: vanpariyar, ankitatanti, Brijeshdhanani, piyushmultidots, kajalgohel
33
Tags: post views, count wordpress site views, show post views, post view counter, WP Post Views, post view count based on ip
4-
Requires at least: 5.0
5-
Requires PHP: 5.3
6-
Tested up to: 6.3
4+
Requires at least: 5.4
5+
Requires PHP: 7.4
6+
Tested up to: 6.4
77
Stable tag: 1.14
88
License: GPLv2 or later
99
License URI: http://www.gnu.org/licenses/gpl-2.0.html
@@ -29,14 +29,15 @@ Use this shortcode.
2929

3030
TO get site wide count of your post type ( Refresh Hourly due to performance reason ).
3131
[WPPV-TOTAL-VIEWS-PER-POST-TYPE post_type="post"]
32+
The total view shortcode not working well with large sites.
3233

3334

3435
### Tutorial
3536

3637
[youtube https://youtu.be/11NH5xOBs68]
3738

3839
### Development
39-
* Development happening on GitHub :- https://github.com/vanpariyar/wp-post-views
40+
* Development happening on GitHub :- [WP Post Views Github](https://github.com/vanpariyar/wp-post-views)
4041
* Create issue on the GitHub OR Pull request for new feature when new tag added it will automatically deployed.
4142

4243
== Installation ==
@@ -56,9 +57,13 @@ TO get site wide count of your post type ( Refresh Hourly due to performance rea
5657

5758
== Changelog ==
5859

60+
= 1.15 - 12/03/2024 =
61+
- Complete architecture Changed on How we count views.
62+
- We are now using ajax to count views. This will ensure the views getting logged even on the caching set. Please while using cache allow ajax function to run.
63+
- code changes can be viewed in GitHub -: https://github.com/vanpariyar/wp-post-views/compare/master...28-it-is-sowing-php-errors
64+
5965
= 1.14 - 21/09/2023 =
60-
- Version Bump to 1.13
61-
- Merged new changes for PHPCS and PHPCBF. Thanks @kajalgohel for Contributions
66+
- Version Bump to 1.14
6267

6368
= 1.13 - 16/06/2023 =
6469
- Version Bump to 1.13

0 commit comments

Comments
 (0)