Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

28 it is sowing php errors #29

Merged
merged 3 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use Docker To use this Plugin setup. (Not Compulasory)
$ cd wp-post-views
$ docker-compose up -d
```
Goto : [localhost:9999/](http://localhost:9999/)
Goto : [localhost:8080/](http://localhost:8080/)

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

Expand Down
18 changes: 18 additions & 0 deletions assets/js/ajax.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
jQuery(document).ready(function ($) {
if( $('body').hasClass('archive')){
return;
}
$.ajax({
url: wp_post_views_ajax_object.ajaxurl, // this is the object instantiated in wp_localize_script function
type: 'POST',
data: {
action: 'wppv_counter', // this is the function in your functions.php that will be triggered
post_id: wp_post_views_ajax_object.post_id,
nonce: wp_post_views_ajax_object.nonce,
},
success: function (data) {
//Do something with the result from server
// console.log(data);
}
});
});
37 changes: 17 additions & 20 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,26 @@ services:
wordpress:
image: wordpress
ports:
- 9999:80
- 8080:80
environment:
WORDPRESS_DB_PASSWORD: example
WORDPRESS_CONFIG_EXTRA:
define( 'WP_DEBUG', true );


WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: exampleuser
WORDPRESS_DB_PASSWORD: examplepass
WORDPRESS_DB_NAME: exampledb
WORDPRESS_DEBUG: '1'
volumes:
- ./:/var/www/html/wp-content/plugins/wp-post-views

mysql:
image: mysql:5.7
db:
image: mysql:8.0
environment:
MYSQL_ROOT_PASSWORD: example
MYSQL_DATABASE: exampledb
MYSQL_USER: exampleuser
MYSQL_PASSWORD: examplepass
MYSQL_RANDOM_ROOT_PASSWORD: '1'
volumes:
- db:/var/lib/mysql

# phpmyadmin
phpmyadmin:
depends_on:
- mysql
image: phpmyadmin/phpmyadmin
restart: always
ports:
- '9998:80'
environment:
PMA_HOST: mysql
MYSQL_ROOT_PASSWORD: example
volumes:
wordpress:
db:
227 changes: 227 additions & 0 deletions includes/counter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
<?php
/**
* Counter Functions.
*/

class WP_Post_Views_Counter_Functions {
public $options;
public $meta_key;
public $total_views_transient_key;
public $total_views_transient_expiration;

public function __construct()
{
$this->load();
}
/**
* Initialize the plugin.
*
* @return void
*/
public function load() {

$this->options = get_option( 'wppv_api_settings' );
$this->meta_key = 'entry_views';
$this->total_views_transient_key = 'wppv_post_total_views';
$this->total_views_transient_expiration = 1 * MINUTE_IN_SECONDS;

// add_action( 'wp_head', array( $this, 'counter' ), 10, 1 );
add_filter( 'manage_posts_columns', array( $this, 'wppv_posts_column_views' ) );
add_filter( 'manage_pages_columns', array( $this, 'wppv_posts_column_views' ) );
add_action( 'manage_posts_custom_column', array( $this, 'wppv_posts_custom_column_views' ) );
add_action( 'manage_pages_custom_column', array( $this, 'wppv_posts_custom_column_views' ) );
add_action( "wp_ajax_wppv_counter", array( $this, 'ajax_functions' ) );
add_action( "wp_ajax_nopriv_wppv_counter", array( $this, 'ajax_functions' ) );
add_action( "wp_enqueue_scripts", array( $this, 'register_scripts') );
}

public function wppv_posts_column_views( $columns ) {

if ( ! empty( $this->options['wppv_api_text_field_0'] ) ) {
$columns['post_views'] = 'Views';
}
return $columns;
}

public function wppv_posts_custom_column_views( $column ) {
$this->options = get_option( 'wppv_api_settings' );
if ( !empty($this->options['wppv_api_text_field_0']) ) {
if ( $column === 'post_views') {
$view_post_meta = get_post_meta(get_the_ID(), 'entry_views', true);
echo esc_html( $view_post_meta );
}
}

}

public function get_ip_address()
{
// Check for shared internet/ISP IP
if (isset($_SERVER['HTTP_CLIENT_IP'])) {
$client_ip = filter_var($_SERVER['HTTP_CLIENT_IP'], FILTER_VALIDATE_IP);
if (!empty($client_ip) && $this->validate_ip($client_ip)) {
return $client_ip;
}
}

// Sanitize HTTP_X_FORWARDED_FOR variable
$x_forwarded_for = filter_input(INPUT_SERVER, 'HTTP_X_FORWARDED_FOR', FILTER_SANITIZE_SPECIAL_CHARS);
if ($x_forwarded_for !== null) {
$iplist = explode(',', $x_forwarded_for);
foreach ($iplist as $ip) {
$ip = trim($ip); // Remove any leading/trailing spaces
if ($this->validate_ip($ip))
return $ip;
}
}

// Check for IPs passing through proxies
$proxy_vars = array(
'HTTP_X_FORWARDED',
'HTTP_X_CLUSTER_CLIENT_IP',
'HTTP_FORWARDED_FOR',
'HTTP_FORWARDED'
);

foreach ($proxy_vars as $var) {
if (!empty($_SERVER[$var])) {
$ip = filter_var($_SERVER[$var], FILTER_VALIDATE_IP);
if ($ip !== false && $this->validate_ip($ip))
return $ip;
}
}

// Sanitize and validate REMOTE_ADDR variable
if (isset($_SERVER['REMOTE_ADDR'])) {
$remote_addr = filter_var($_SERVER['REMOTE_ADDR'], FILTER_VALIDATE_IP);
if ($remote_addr !== false && $this->validate_ip($remote_addr)) {
return $remote_addr;
}
}

// Return unreliable IP since all else failed
return '';
}


public function validate_ip($ip) {
if (
filter_var( $ip,
FILTER_VALIDATE_IP,
FILTER_FLAG_IPV4 |
FILTER_FLAG_IPV6 |
FILTER_FLAG_NO_PRIV_RANGE |
FILTER_FLAG_NO_RES_RANGE
) === false
) {
return false;
}
return true;
}

public function counter( $post_id ){
$post = get_post($post_id);
$stored_ip_addresses = 0;
$selected_type = array();
isset($this->options['wppv_api_post_checkbox_1']) ? $selected_type = $this->options['wppv_api_post_checkbox_1'] : '';

if( is_object($post) && in_array($post->post_type , $selected_type)){
if ( !empty($this->options['wppv_api_text_field_1']) ) {
$stored_ip_addresses = get_post_meta($post->ID,'view_ip',true);

$current_ip = $this->get_ip_address();
if( $stored_ip_addresses )
{
if(!in_array($current_ip, $stored_ip_addresses))
{
$view_post_meta = get_post_meta($post->ID, $this->meta_key, true);
$new_viewed_count = intval($view_post_meta) + 1;
update_post_meta($post->ID, $this->meta_key, $new_viewed_count);
$stored_ip_addresses[] = $current_ip;
update_post_meta($post->ID,'view_ip',$stored_ip_addresses);
}
} else {
$stored_ip_addresses = array();
$view_post_meta = get_post_meta($post->ID, $this->meta_key, true);
$new_viewed_count = intval($view_post_meta) + 1;
update_post_meta($post->ID, $this->meta_key, $new_viewed_count);
$stored_ip_addresses[] = $current_ip;
update_post_meta($post->ID,'view_ip',$stored_ip_addresses);
}
} else {
$view_post_meta = get_post_meta($post->ID, $this->meta_key, true);
$new_viewed_count = intval($view_post_meta) + 1;
update_post_meta($post->ID, $this->meta_key, $new_viewed_count);
}
}

}

private function count_total_view( $post_type = 'post' ) {
$total = 0;

if( $total = get_transient( $this->total_views_transient_key.$post_type ) ) {
return $total;
}

$arguments = array(
'post_type' => $post_type,
'posts_per_page' => '-1',
'status' => 'publish',
);
$total_count_query = new WP_Query( $arguments );

if( $total_count_query->have_posts() ){
while( $total_count_query->have_posts() ) {
$total_count_query->the_post();
$view_post_meta = get_post_meta(get_the_ID(), $this->meta_key, true);
$total += $view_post_meta;
}
}
set_transient( $this->total_views_transient_key.$post_type, $total, $this->total_views_transient_expiration );

return $total;
}

public function get_total_views( $post_type = 'post' ) {
return $this->count_total_view($post_type);
}

public function register_scripts(){
wp_register_script(
'wp-posts-view-script',
WP_POST_VIEW_URL.'/assets/js/ajax.js',
array('jquery'),
false,
true
);
wp_enqueue_script('wp-posts-view-script');

$localised_array = array(
'ajaxurl' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('wp-post-view-nonce'),
);

if( get_the_ID() && in_array( get_post_type( get_the_ID() ), get_post_types() )){
$localised_array['post_id'] = get_the_ID();
}

wp_localize_script(
'wp-posts-view-script',
'wp_post_views_ajax_object',
$localised_array,
);
}

public function ajax_functions(){

$post_id = intval(( $_POST['post_id'] ));

if ( ! wp_verify_nonce( $_POST['nonce'], 'wp-post-view-nonce' ) ) {
wp_send_json_success('1');
}
$this->counter($post_id);
wp_send_json_success('1');
// wp_die(); // ajax call must die to avoid trailing 0 in your response
}
}
6 changes: 3 additions & 3 deletions includes/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ public static function select_post_type_callback( ) {
/* BUILT IN POST TYPE PAGE AND POST OPTION */
?>
<label for="">Posts </label>
<input type='checkbox' name='wppv_api_settings[wppv_api_post_checkbox_1][post]' value="post" <?php checked( ( 'post' == @$checkbox_val['post']), true ); ?>>
<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 ); ?>>
<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 ); ?>>
<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 ); ?>>
<?php
foreach($post_types as $post_type){
?>
<label for=""><?php echo esc_html( $post_type->label );?> </label>
<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 ); ?>>
<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 ); ?>>
<?php
}
}
Expand Down
17 changes: 11 additions & 6 deletions readme.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
=== Wp Post Views - Wordpress Post views counter ===
Contributors: vanpariyar, ankitatanti, Brijeshdhanani, piyushmultidots, kajalgohel
Tags: post views, count wordpress site views, show post views, post view counter, WP Post Views, post view count based on ip
Requires at least: 5.0
Requires PHP: 5.3
Tested up to: 6.3
Requires at least: 5.4
Requires PHP: 7.4
Tested up to: 6.4
Stable tag: 1.14
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Expand All @@ -29,14 +29,15 @@ Use this shortcode.

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


### Tutorial

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

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

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

== Changelog ==

= 1.15 - 12/03/2024 =
- Complete architecture Changed on How we count views.
- 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.
- code changes can be viewed in GitHub -: https://github.com/vanpariyar/wp-post-views/compare/master...28-it-is-sowing-php-errors

= 1.14 - 21/09/2023 =
- Version Bump to 1.13
- Merged new changes for PHPCS and PHPCBF. Thanks @kajalgohel for Contributions
- Version Bump to 1.14

= 1.13 - 16/06/2023 =
- Version Bump to 1.13
Expand Down
Loading