Skip to content

Commit

Permalink
Merge pull request #21 from vanpariyar/feature/18-hi-i-want-to-build-…
Browse files Browse the repository at this point in the history
…statistics-and-take-all-the-views-on-one-page

Feature/18 hi i want to build statistics and take all the views on one page
  • Loading branch information
Antontokarchuk0302 committed Oct 19, 2022
2 parents 60275f2 + e2e5ee4 commit 7cfa03c
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 70 deletions.
2 changes: 1 addition & 1 deletion includes/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static function wppv_activation_hook(){
if(empty(get_option( 'wppv_api_settings' ))){
$options = array(
'wppv_api_text_field_0' => '1' ,
'wppv_api_text_field_1' => '1',
'wppv_api_text_field_1' => '0',
'wppv_api_post_checkbox_1'=> array(
'post' => 'post',
'page' => 'page'
Expand Down
27 changes: 24 additions & 3 deletions includes/shortcodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
function wppv_add_custom_shortcode() {
/**
* @param $post which is Post id ( Optional )
* @author of shortcode Ronak Vanpariya.
* @author Ronak Vanpariya.
* @desc Get Post Count For the Blog.
*/

Expand All @@ -13,7 +13,28 @@ function wppv_current_post_view_callback($atts = array() , $content = ''){
$view_post_meta = get_post_meta(get_the_ID(), $meta_key, true);
return $view_post_meta;
}
if(!shortcode_exists( 'WPPV-TOTAL-VIEWS' )){
if( ! shortcode_exists( 'WPPV-TOTAL-VIEWS' )){
add_shortcode( 'WPPV-TOTAL-VIEWS', 'wppv_current_post_view_callback' );
}
}

/**
* @param $post_type which is post ( Default )
* @author Ronak Vanpariya.
* @desc Get Post Total Count For the Blog.
*/

function wppv_current_post_view_per_post_type_callback($atts = array() , $content = ''){
global $wp_post_views;

$parsed = wp_parse_args(
$atts,
array(
'post_type' => 'post',
)
);
return $wp_post_views->get_total_views( $parsed['post_type'] );
}
if( ! shortcode_exists( 'WPPV-TOTAL-VIEWS-PER-POST-TYPE' )){
add_shortcode( 'WPPV-TOTAL-VIEWS-PER-POST-TYPE', 'wppv_current_post_view_per_post_type_callback' );
}
}
7 changes: 6 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Tags: post views, count wordpress site views, show post views, post view counter
Requires at least: 5.0
Requires PHP: 5.3
Tested up to: 6.0
Stable tag: 1.10
Stable tag: 1.11
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html

Expand All @@ -25,6 +25,11 @@ Use this shortcode.

[WPPV-TOTAL-VIEWS]


TO get site wide count of your post type ( Refresh Hourly due to performance reason ).
[WPPV-TOTAL-VIEWS-PER-POST-TYPE post_type="post"]


### Tutorial

[youtube https://youtu.be/11NH5xOBs68]
Expand Down
169 changes: 104 additions & 65 deletions wp-post-views.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* Plugin Name: WP Post Views
* Plugin URI: https://github.com/vanpariyar/wp-post-views
* Description: WP Post Views.
* Version: 1.9
* Version: 1.11ß
* Requires at least: 5.0
* Requires PHP: 5.3
* Author: Ronak J Vanpariya
Expand All @@ -37,11 +37,11 @@

/* Plugin Constants */
if (!defined('WP_POST_VIEW_URL')) {
define('WP_POST_VIEW_URL', plugin_dir_url(__FILE__));
define('WP_POST_VIEW_URL', plugin_dir_url(__FILE__));
}

if (!defined('WP_POST_VIEW_PLUGIN_PATH')) {
define('WP_POST_VIEW_PLUGIN_PATH', plugin_dir_path(__FILE__));
define('WP_POST_VIEW_PLUGIN_PATH', plugin_dir_path(__FILE__));
}

require_once (WP_POST_VIEW_PLUGIN_PATH . '/includes/settings.php');
Expand All @@ -53,16 +53,25 @@
/**
* MAIN CLASS
*/
class WP_Post_Views
{
function __construct()
{
add_action( 'init', array( $this ,'load_textdomain' ) );
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') );
class WP_Post_Views {
/**
* Initialize the plugin.
*
* @return void
*/
public function __construct() {

$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( 'init', array( $this, 'load_textdomain' ) );
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' ) );
Wp_post_view_settings::settings_init();
}

Expand All @@ -72,104 +81,134 @@ function load_textdomain() {

public function wppv_posts_column_views( $columns ) {

$options = get_option( 'wppv_api_settings' );

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

public function wppv_posts_custom_column_views( $column ) {
$options = get_option( 'wppv_api_settings' );
if ( !empty($options['wppv_api_text_field_0']) ) {
$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 $view_post_meta;
}
}
}
}

public function get_ip_address()
{
// check for shared internet/ISP IP
if (!empty($_SERVER['HTTP_CLIENT_IP']) && $this->validate_ip($_SERVER['HTTP_CLIENT_IP']))
return $_SERVER['HTTP_CLIENT_IP'];
// check for IPs passing through proxies
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
// check if multiple ips exist in var
$iplist = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
foreach ($iplist as $ip) {
if ($this->validate_ip($ip))
return $ip;
}
}
if (!empty($_SERVER['HTTP_X_FORWARDED']) && $this->validate_ip($_SERVER['HTTP_X_FORWARDED']))
return $_SERVER['HTTP_X_FORWARDED'];
if (!empty($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']) && $this->validate_ip($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']))
return $_SERVER['HTTP_X_CLUSTER_CLIENT_IP'];
if (!empty($_SERVER['HTTP_FORWARDED_FOR']) && $this->validate_ip($_SERVER['HTTP_FORWARDED_FOR']))
return $_SERVER['HTTP_FORWARDED_FOR'];
if (!empty($_SERVER['HTTP_FORWARDED']) && $this->validate_ip($_SERVER['HTTP_FORWARDED']))
return $_SERVER['HTTP_FORWARDED'];
// return unreliable ip since all else failed
return $_SERVER['REMOTE_ADDR'];
// check for shared internet/ISP IP
if (!empty($_SERVER['HTTP_CLIENT_IP']) && $this->validate_ip($_SERVER['HTTP_CLIENT_IP']))
return $_SERVER['HTTP_CLIENT_IP'];
// check for IPs passing through proxies
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
// check if multiple ips exist in var
$iplist = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
foreach ($iplist as $ip) {
if ($this->validate_ip($ip))
return $ip;
}
}
if (!empty($_SERVER['HTTP_X_FORWARDED']) && $this->validate_ip($_SERVER['HTTP_X_FORWARDED']))
return $_SERVER['HTTP_X_FORWARDED'];
if (!empty($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']) && $this->validate_ip($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']))
return $_SERVER['HTTP_X_CLUSTER_CLIENT_IP'];
if (!empty($_SERVER['HTTP_FORWARDED_FOR']) && $this->validate_ip($_SERVER['HTTP_FORWARDED_FOR']))
return $_SERVER['HTTP_FORWARDED_FOR'];
if (!empty($_SERVER['HTTP_FORWARDED']) && $this->validate_ip($_SERVER['HTTP_FORWARDED']))
return $_SERVER['HTTP_FORWARDED'];
// return unreliable ip since all else failed
return $_SERVER['REMOTE_ADDR'];
}

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;
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(){
global $post;
$stored_ip_addresses = 0;
$options = get_option( 'wppv_api_settings' );
$selected_type = array();
isset($options['wppv_api_post_checkbox_1']) ? $selected_type = $options['wppv_api_post_checkbox_1'] : '';
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($options['wppv_api_text_field_1']) ) {
if ( !empty($this->options['wppv_api_text_field_1']) ) {
$stored_ip_addresses = get_post_meta(get_the_ID(),'view_ip',true);

$current_ip = $this->get_ip_address();
if( $stored_ip_addresses )
{
if(!in_array($current_ip, $stored_ip_addresses))
{
$meta_key = 'entry_views';
$view_post_meta = get_post_meta(get_the_ID(), $meta_key, true);
$view_post_meta = get_post_meta(get_the_ID(), $this->meta_key, true);
$new_viewed_count = intval($view_post_meta) + 1;
update_post_meta(get_the_ID(), $meta_key, $new_viewed_count);
update_post_meta(get_the_ID(), $this->meta_key, $new_viewed_count);
$stored_ip_addresses[] = $current_ip;
update_post_meta(get_the_ID(),'view_ip',$stored_ip_addresses);
}
} else {
$stored_ip_addresses = array();
$meta_key = 'entry_views';
$view_post_meta = get_post_meta(get_the_ID(), $meta_key, true);
$view_post_meta = get_post_meta(get_the_ID(), $this->meta_key, true);
$new_viewed_count = intval($view_post_meta) + 1;
update_post_meta(get_the_ID(), $meta_key, $new_viewed_count);
update_post_meta(get_the_ID(), $this->meta_key, $new_viewed_count);
$stored_ip_addresses[] = $current_ip;
update_post_meta(get_the_ID(),'view_ip',$stored_ip_addresses);
}
} else {
$meta_key = 'entry_views';
$view_post_meta = get_post_meta(get_the_ID(), $meta_key, true);
$view_post_meta = get_post_meta(get_the_ID(), $this->meta_key, true);
$new_viewed_count = intval($view_post_meta) + 1;
update_post_meta(get_the_ID(), $meta_key, $new_viewed_count);
update_post_meta(get_the_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);
}

}

$post_view = new WP_Post_Views();
global $wp_post_views;

$wp_post_views = new WP_Post_Views();

0 comments on commit 7cfa03c

Please sign in to comment.