Plugin Directory

Changeset 2801279

Timestamp:
10/19/2022 01:44:25 PM (22 months ago)
Author:
vanpariyar
Message:

Update to version 1.11 from GitHub

Location:
wp-post-views
Files:
8 edited
1 copied

Legend:

Unmodified
Added
Removed
  • wp-post-views/tags/1.11/includes/settings.php

    r2757872 r2801279  
    1919            $options = array(
    2020                'wppv_api_text_field_0' => '1' ,
    21                 'wppv_api_text_field_1' => '1',
     21                'wppv_api_text_field_1' => '',
    2222                'wppv_api_post_checkbox_1'=> array(
    2323                    'post' => 'post',
  • wp-post-views/tags/1.11/includes/shortcodes.php

    r2646429 r2801279  
    55    /**
    66     * @param $post which is Post id ( Optional )
    7      * @author of shortcode Ronak Vanpariya.
     7     * @author Ronak Vanpariya.
    88     * @desc Get Post Count For the Blog.
    99     */
     
    1414        return $view_post_meta;
    1515    }
    16     if(!shortcode_exists( 'WPPV-TOTAL-VIEWS' )){
     16    if(shortcode_exists( 'WPPV-TOTAL-VIEWS' )){
    1717        add_shortcode( 'WPPV-TOTAL-VIEWS', 'wppv_current_post_view_callback' );
    18     }   
     18    }
     19
     20    /**
     21     * @param $post_type which is post ( Default )
     22     * @author Ronak Vanpariya.
     23     * @desc Get Post Total Count For the Blog.
     24     */
     25
     26    function wppv_current_post_view_per_post_type_callback($atts = array() , $content = ''){
     27        global $wp_post_views;
     28
     29        $parsed = wp_parse_args(
     30            $atts,
     31            array(
     32                'post_type' => 'post',
     33            )
     34        );
     35        return $wp_post_views->get_total_views( $parsed['post_type'] );
     36    }
     37    if( ! shortcode_exists( 'WPPV-TOTAL-VIEWS-PER-POST-TYPE' )){
     38        add_shortcode( 'WPPV-TOTAL-VIEWS-PER-POST-TYPE', 'wppv_current_post_view_per_post_type_callback' );
     39    }
    1940}
  • wp-post-views/tags/1.11/readme.txt

    r2757872 r2801279  
    11=== Wp Post Views - Wordpress Post views counter ===
    2 Contributors: vanpariyar, ankitatanti, Brijeshdhanani,
     2Contributors: vanpariyar, ankitatanti, Brijeshdhanani,
    33Tags: post views, count wordpress site views, show post views, post view counter, WP Post Views, post view count based on ip 
    44Requires at least: 5.0
    55Requires PHP: 5.3
    6 Tested up to: 6.0
    7 Stable tag: 1.10
     6Tested up to: 6.
     7Stable tag: 1.1
    88License: GPLv2 or later
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    2525
    2626[WPPV-TOTAL-VIEWS]
     27
     28
     29
     30
     31
    2732
    2833### Tutorial
     
    5055
    5156== Changelog ==
     57
     58
     59
     60
    5261
    5362= 1.10 - 18/07/2022 =
  • wp-post-views/tags/1.11/wp-post-views.php

    r2757872 r2801279  
    1212 * Plugin URI:        https://github.com/vanpariyar/wp-post-views
    1313 * Description:       WP Post Views.
    14  * Version:           1.9
     14 * Version:           1.
    1515 * Requires at least: 5.0
    1616 * Requires PHP:      5.3
     
    2121 * License:           GPL v2 or later
    2222 * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
    23 
    24  WP Post Views is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 2 of the License, or any later version.
    25  
    26  WP Post Views is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    27  
    28  You should have received a copy of the GNU General Public License along with WP Post Views. If not, see  * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
    29 
    3023*/
    3124
     
    3831/* Plugin Constants */
    3932if (!defined('WP_POST_VIEW_URL')) {
    40     define('WP_POST_VIEW_URL', plugin_dir_url(__FILE__));
     33define('WP_POST_VIEW_URL', plugin_dir_url(__FILE__));
    4134}
    4235
    4336if (!defined('WP_POST_VIEW_PLUGIN_PATH')) {
    44     define('WP_POST_VIEW_PLUGIN_PATH', plugin_dir_path(__FILE__));
     37define('WP_POST_VIEW_PLUGIN_PATH', plugin_dir_path(__FILE__));
    4538}
    4639
     
    5447 * MAIN CLASS
    5548 */
    56 class WP_Post_Views
    57 {
    58     function __construct()
    59     {
    60         add_action( 'init', array( $this ,'load_textdomain' ) );
    61         add_action( 'wp_head', array( $this , 'counter'), 10, 1 );
    62         add_filter( 'manage_posts_columns', array( $this,'wppv_posts_column_views') );
    63         add_filter( 'manage_pages_columns', array( $this,'wppv_posts_column_views') );
    64         add_action( 'manage_posts_custom_column', array( $this,'wppv_posts_custom_column_views') );
    65         add_action( 'manage_pages_custom_column', array( $this,'wppv_posts_custom_column_views') );
     49class WP_Post_Views {
     50    /**
     51     * Initialize the plugin.
     52     *
     53     * @return void
     54     */
     55    public function __construct() {
     56       
     57        $this->options = get_option( 'wppv_api_settings' );
     58        $this->meta_key         = 'entry_views';
     59        $this->total_views_transient_key = 'wppv_post_total_views';
     60        $this->total_views_transient_expiration = 1 * MINUTE_IN_SECONDS;
     61
     62        add_action( 'init', array( $this, 'load_textdomain' ) );
     63        add_action( 'wp_head', array( $this, 'counter' ), 10, 1 );
     64        add_filter( 'manage_posts_columns', array( $this, 'wppv_posts_column_views' ) );
     65        add_filter( 'manage_pages_columns', array( $this, 'wppv_posts_column_views' ) );
     66        add_action( 'manage_posts_custom_column', array( $this, 'wppv_posts_custom_column_views' ) );
     67        add_action( 'manage_pages_custom_column', array( $this, 'wppv_posts_custom_column_views' ) );
    6668        Wp_post_view_settings::settings_init();
    6769    }
     
    7375    public function wppv_posts_column_views( $columns ) {
    7476
    75         $options = get_option( 'wppv_api_settings' );
    76        
    77         //$options['wppv_api_text_field_0'];
    78         if ( !empty($options['wppv_api_text_field_0']) ) { 
     77        if ( ! empty( $this->options['wppv_api_text_field_0'] ) ) {
    7978            $columns['post_views'] = 'Views';
    8079        }
    81         return $columns;
     80    return $columns;
    8281    }
    8382
    8483    public function wppv_posts_custom_column_views( $column ) {
    85         $options = get_option( 'wppv_api_settings' );
    86         if ( !empty($options['wppv_api_text_field_0']) ) {
     84        $options = get_option( 'wppv_api_settings' );
     85        if ( !empty($options['wppv_api_text_field_0']) ) {
    8786            if ( $column === 'post_views') {
    8887                $view_post_meta = get_post_meta(get_the_ID(), 'entry_views', true);
    8988                echo $view_post_meta;
    90             }
    91         }
    92        
     89        }
     90        }
     91   
    9392    }
    9493
    9594    public function get_ip_address()
    9695    {
    97         // check for shared internet/ISP IP
    98         if (!empty($_SERVER['HTTP_CLIENT_IP']) && $this->validate_ip($_SERVER['HTTP_CLIENT_IP']))
    99             return $_SERVER['HTTP_CLIENT_IP'];
    100         // check for IPs passing through proxies
    101         if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    102             // check if multiple ips exist in var
    103             $iplist = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
    104             foreach ($iplist as $ip) {
    105                 if ($this->validate_ip($ip))
    106                     return $ip;
    107             }
    108         }
    109         if (!empty($_SERVER['HTTP_X_FORWARDED']) && $this->validate_ip($_SERVER['HTTP_X_FORWARDED']))
    110             return $_SERVER['HTTP_X_FORWARDED'];
    111         if (!empty($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']) && $this->validate_ip($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']))
    112             return $_SERVER['HTTP_X_CLUSTER_CLIENT_IP'];
    113         if (!empty($_SERVER['HTTP_FORWARDED_FOR']) && $this->validate_ip($_SERVER['HTTP_FORWARDED_FOR']))
    114             return $_SERVER['HTTP_FORWARDED_FOR'];
    115         if (!empty($_SERVER['HTTP_FORWARDED']) && $this->validate_ip($_SERVER['HTTP_FORWARDED']))
    116             return $_SERVER['HTTP_FORWARDED'];
    117         // return unreliable ip since all else failed
    118         return $_SERVER['REMOTE_ADDR'];
     96    // check for shared internet/ISP IP
     97    if (!empty($_SERVER['HTTP_CLIENT_IP']) && $this->validate_ip($_SERVER['HTTP_CLIENT_IP']))
     98    return $_SERVER['HTTP_CLIENT_IP'];
     99    // check for IPs passing through proxies
     100    if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
     101    // check if multiple ips exist in var
     102    $iplist = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
     103    foreach ($iplist as $ip) {
     104    if ($this->validate_ip($ip))
     105    return $ip;
     106    }
     107    }
     108    if (!empty($_SERVER['HTTP_X_FORWARDED']) && $this->validate_ip($_SERVER['HTTP_X_FORWARDED']))
     109    return $_SERVER['HTTP_X_FORWARDED'];
     110    if (!empty($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']) && $this->validate_ip($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']))
     111    return $_SERVER['HTTP_X_CLUSTER_CLIENT_IP'];
     112    if (!empty($_SERVER['HTTP_FORWARDED_FOR']) && $this->validate_ip($_SERVER['HTTP_FORWARDED_FOR']))
     113    return $_SERVER['HTTP_FORWARDED_FOR'];
     114    if (!empty($_SERVER['HTTP_FORWARDED']) && $this->validate_ip($_SERVER['HTTP_FORWARDED']))
     115    return $_SERVER['HTTP_FORWARDED'];
     116    // return unreliable ip since all else failed
     117    return $_SERVER['REMOTE_ADDR'];
    119118    }
    120119
    121120    public function validate_ip($ip) {
    122          if (filter_var($ip, FILTER_VALIDATE_IP,
    123                              FILTER_FLAG_IPV4 |
    124                              FILTER_FLAG_IPV6 |
    125                              FILTER_FLAG_NO_PRIV_RANGE |
    126                              FILTER_FLAG_NO_RES_RANGE) === false)
    127             return false;
    128         return true;
     121        if (
     122            filter_var( $ip,
     123                FILTER_VALIDATE_IP,
     124                FILTER_FLAG_IPV4 |
     125                FILTER_FLAG_IPV6 |
     126                FILTER_FLAG_NO_PRIV_RANGE |
     127                FILTER_FLAG_NO_RES_RANGE
     128            ) === false
     129        ) {
     130            return false;
     131        }
     132        return true;
    129133    }
    130134
     
    132136        global $post;
    133137        $stored_ip_addresses = 0;
    134         $options = get_option( 'wppv_api_settings' );
    135138        $selected_type = array();
    136         isset($options['wppv_api_post_checkbox_1']) ? $selected_type = $options['wppv_api_post_checkbox_1'] : '';
     139        isset($options['wppv_api_post_checkbox_1'] : '';
    137140       
    138141        if( is_object($post) && in_array($post->post_type , $selected_type)){
    139             if ( !empty($options['wppv_api_text_field_1']) ) {
     142            if ( !empty($options['wppv_api_text_field_1']) ) {
    140143                $stored_ip_addresses = get_post_meta(get_the_ID(),'view_ip',true);
    141144
     
    145148                    if(!in_array($current_ip, $stored_ip_addresses))
    146149                    {
    147                         $meta_key         = 'entry_views';
    148                         $view_post_meta   = get_post_meta(get_the_ID(), $meta_key, true);
     150                        $view_post_meta   = get_post_meta(get_the_ID(), $this->meta_key, true);
    149151                        $new_viewed_count = intval($view_post_meta) + 1;
    150                         update_post_meta(get_the_ID(), $meta_key, $new_viewed_count);
     152                        update_post_meta(get_the_ID(), $meta_key, $new_viewed_count);
    151153                        $stored_ip_addresses[] = $current_ip;
    152154                        update_post_meta(get_the_ID(),'view_ip',$stored_ip_addresses);
     
    154156                } else {
    155157                    $stored_ip_addresses = array();
    156                     $meta_key         = 'entry_views';
    157                     $view_post_meta   = get_post_meta(get_the_ID(), $meta_key, true);
     158                    $view_post_meta   = get_post_meta(get_the_ID(), $this->meta_key, true);
    158159                    $new_viewed_count = intval($view_post_meta) + 1;
    159                     update_post_meta(get_the_ID(), $meta_key, $new_viewed_count);
     160                    update_post_meta(get_the_ID(), $meta_key, $new_viewed_count);
    160161                    $stored_ip_addresses[] = $current_ip;
    161162                    update_post_meta(get_the_ID(),'view_ip',$stored_ip_addresses);
    162163                }
    163164            } else {
    164                 $meta_key         = 'entry_views';
    165                 $view_post_meta   = get_post_meta(get_the_ID(), $meta_key, true);
     165                $view_post_meta   = get_post_meta(get_the_ID(), $this->meta_key, true);
    166166                $new_viewed_count = intval($view_post_meta) + 1;
    167                 update_post_meta(get_the_ID(), $meta_key, $new_viewed_count);
    168             }
    169         }
    170 
    171     }
    172      
    173 }
    174 
    175 $post_view = new WP_Post_Views();
     167                update_post_meta(get_the_ID(), $this->meta_key, $new_viewed_count);
     168            }
     169        }
     170
     171    }
     172
     173    private function count_total_view( $post_type = 'post' ) {
     174        $total = 0;
     175
     176        if( $total = get_transient( $this->total_views_transient_key.$post_type ) ) {
     177            return $total;
     178        }
     179
     180        $arguments = array(
     181            'post_type' => $post_type,
     182            'posts_per_page' => '-1',
     183            'status' => 'publish',
     184        );
     185        $total_count_query = new WP_Query( $arguments );
     186
     187        if( $total_count_query->have_posts() ){
     188            while( $total_count_query->have_posts() ) {
     189                $total_count_query->the_post();
     190                $view_post_meta   = get_post_meta(get_the_ID(), $this->meta_key, true);
     191                $total += $view_post_meta;
     192            }
     193        }
     194        set_transient( $this->total_views_transient_key.$post_type, $total, $this->total_views_transient_expiration );
     195
     196        return $total;
     197    }
     198
     199    public function get_total_views( $post_type = 'post' ) {
     200        return $this->count_total_view($post_type);
     201    }
     202
     203}
     204
     205global $wp_post_views;
     206
     207$wp_post_views = new WP_Post_Views();
  • wp-post-views/trunk/includes/settings.php

    r2757872 r2801279  
    1919            $options = array(
    2020                'wppv_api_text_field_0' => '1' ,
    21                 'wppv_api_text_field_1' => '1',
     21                'wppv_api_text_field_1' => '',
    2222                'wppv_api_post_checkbox_1'=> array(
    2323                    'post' => 'post',
  • wp-post-views/trunk/includes/shortcodes.php

    r2646429 r2801279  
    55    /**
    66     * @param $post which is Post id ( Optional )
    7      * @author of shortcode Ronak Vanpariya.
     7     * @author Ronak Vanpariya.
    88     * @desc Get Post Count For the Blog.
    99     */
     
    1414        return $view_post_meta;
    1515    }
    16     if(!shortcode_exists( 'WPPV-TOTAL-VIEWS' )){
     16    if(shortcode_exists( 'WPPV-TOTAL-VIEWS' )){
    1717        add_shortcode( 'WPPV-TOTAL-VIEWS', 'wppv_current_post_view_callback' );
    18     }   
     18    }
     19
     20    /**
     21     * @param $post_type which is post ( Default )
     22     * @author Ronak Vanpariya.
     23     * @desc Get Post Total Count For the Blog.
     24     */
     25
     26    function wppv_current_post_view_per_post_type_callback($atts = array() , $content = ''){
     27        global $wp_post_views;
     28
     29        $parsed = wp_parse_args(
     30            $atts,
     31            array(
     32                'post_type' => 'post',
     33            )
     34        );
     35        return $wp_post_views->get_total_views( $parsed['post_type'] );
     36    }
     37    if( ! shortcode_exists( 'WPPV-TOTAL-VIEWS-PER-POST-TYPE' )){
     38        add_shortcode( 'WPPV-TOTAL-VIEWS-PER-POST-TYPE', 'wppv_current_post_view_per_post_type_callback' );
     39    }
    1940}
  • wp-post-views/trunk/readme.txt

    r2757872 r2801279  
    11=== Wp Post Views - Wordpress Post views counter ===
    2 Contributors: vanpariyar, ankitatanti, Brijeshdhanani,
     2Contributors: vanpariyar, ankitatanti, Brijeshdhanani,
    33Tags: post views, count wordpress site views, show post views, post view counter, WP Post Views, post view count based on ip 
    44Requires at least: 5.0
    55Requires PHP: 5.3
    6 Tested up to: 6.0
    7 Stable tag: 1.10
     6Tested up to: 6.
     7Stable tag: 1.1
    88License: GPLv2 or later
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    2525
    2626[WPPV-TOTAL-VIEWS]
     27
     28
     29
     30
     31
    2732
    2833### Tutorial
     
    5055
    5156== Changelog ==
     57
     58
     59
     60
    5261
    5362= 1.10 - 18/07/2022 =
  • wp-post-views/trunk/wp-post-views.php

    r2757872 r2801279  
    1212 * Plugin URI:        https://github.com/vanpariyar/wp-post-views
    1313 * Description:       WP Post Views.
    14  * Version:           1.9
     14 * Version:           1.
    1515 * Requires at least: 5.0
    1616 * Requires PHP:      5.3
     
    2121 * License:           GPL v2 or later
    2222 * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
    23 
    24  WP Post Views is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 2 of the License, or any later version.
    25  
    26  WP Post Views is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    27  
    28  You should have received a copy of the GNU General Public License along with WP Post Views. If not, see  * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
    29 
    3023*/
    3124
     
    3831/* Plugin Constants */
    3932if (!defined('WP_POST_VIEW_URL')) {
    40     define('WP_POST_VIEW_URL', plugin_dir_url(__FILE__));
     33define('WP_POST_VIEW_URL', plugin_dir_url(__FILE__));
    4134}
    4235
    4336if (!defined('WP_POST_VIEW_PLUGIN_PATH')) {
    44     define('WP_POST_VIEW_PLUGIN_PATH', plugin_dir_path(__FILE__));
     37define('WP_POST_VIEW_PLUGIN_PATH', plugin_dir_path(__FILE__));
    4538}
    4639
     
    5447 * MAIN CLASS
    5548 */
    56 class WP_Post_Views
    57 {
    58     function __construct()
    59     {
    60         add_action( 'init', array( $this ,'load_textdomain' ) );
    61         add_action( 'wp_head', array( $this , 'counter'), 10, 1 );
    62         add_filter( 'manage_posts_columns', array( $this,'wppv_posts_column_views') );
    63         add_filter( 'manage_pages_columns', array( $this,'wppv_posts_column_views') );
    64         add_action( 'manage_posts_custom_column', array( $this,'wppv_posts_custom_column_views') );
    65         add_action( 'manage_pages_custom_column', array( $this,'wppv_posts_custom_column_views') );
     49class WP_Post_Views {
     50    /**
     51     * Initialize the plugin.
     52     *
     53     * @return void
     54     */
     55    public function __construct() {
     56       
     57        $this->options = get_option( 'wppv_api_settings' );
     58        $this->meta_key         = 'entry_views';
     59        $this->total_views_transient_key = 'wppv_post_total_views';
     60        $this->total_views_transient_expiration = 1 * MINUTE_IN_SECONDS;
     61
     62        add_action( 'init', array( $this, 'load_textdomain' ) );
     63        add_action( 'wp_head', array( $this, 'counter' ), 10, 1 );
     64        add_filter( 'manage_posts_columns', array( $this, 'wppv_posts_column_views' ) );
     65        add_filter( 'manage_pages_columns', array( $this, 'wppv_posts_column_views' ) );
     66        add_action( 'manage_posts_custom_column', array( $this, 'wppv_posts_custom_column_views' ) );
     67        add_action( 'manage_pages_custom_column', array( $this, 'wppv_posts_custom_column_views' ) );
    6668        Wp_post_view_settings::settings_init();
    6769    }
     
    7375    public function wppv_posts_column_views( $columns ) {
    7476
    75         $options = get_option( 'wppv_api_settings' );
    76        
    77         //$options['wppv_api_text_field_0'];
    78         if ( !empty($options['wppv_api_text_field_0']) ) { 
     77        if ( ! empty( $this->options['wppv_api_text_field_0'] ) ) {
    7978            $columns['post_views'] = 'Views';
    8079        }
    81         return $columns;
     80    return $columns;
    8281    }
    8382
    8483    public function wppv_posts_custom_column_views( $column ) {
    85         $options = get_option( 'wppv_api_settings' );
    86         if ( !empty($options['wppv_api_text_field_0']) ) {
     84        $options = get_option( 'wppv_api_settings' );
     85        if ( !empty($options['wppv_api_text_field_0']) ) {
    8786            if ( $column === 'post_views') {
    8887                $view_post_meta = get_post_meta(get_the_ID(), 'entry_views', true);
    8988                echo $view_post_meta;
    90             }
    91         }
    92        
     89        }
     90        }
     91   
    9392    }
    9493
    9594    public function get_ip_address()
    9695    {
    97         // check for shared internet/ISP IP
    98         if (!empty($_SERVER['HTTP_CLIENT_IP']) && $this->validate_ip($_SERVER['HTTP_CLIENT_IP']))
    99             return $_SERVER['HTTP_CLIENT_IP'];
    100         // check for IPs passing through proxies
    101         if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    102             // check if multiple ips exist in var
    103             $iplist = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
    104             foreach ($iplist as $ip) {
    105                 if ($this->validate_ip($ip))
    106                     return $ip;
    107             }
    108         }
    109         if (!empty($_SERVER['HTTP_X_FORWARDED']) && $this->validate_ip($_SERVER['HTTP_X_FORWARDED']))
    110             return $_SERVER['HTTP_X_FORWARDED'];
    111         if (!empty($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']) && $this->validate_ip($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']))
    112             return $_SERVER['HTTP_X_CLUSTER_CLIENT_IP'];
    113         if (!empty($_SERVER['HTTP_FORWARDED_FOR']) && $this->validate_ip($_SERVER['HTTP_FORWARDED_FOR']))
    114             return $_SERVER['HTTP_FORWARDED_FOR'];
    115         if (!empty($_SERVER['HTTP_FORWARDED']) && $this->validate_ip($_SERVER['HTTP_FORWARDED']))
    116             return $_SERVER['HTTP_FORWARDED'];
    117         // return unreliable ip since all else failed
    118         return $_SERVER['REMOTE_ADDR'];
     96    // check for shared internet/ISP IP
     97    if (!empty($_SERVER['HTTP_CLIENT_IP']) && $this->validate_ip($_SERVER['HTTP_CLIENT_IP']))
     98    return $_SERVER['HTTP_CLIENT_IP'];
     99    // check for IPs passing through proxies
     100    if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
     101    // check if multiple ips exist in var
     102    $iplist = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
     103    foreach ($iplist as $ip) {
     104    if ($this->validate_ip($ip))
     105    return $ip;
     106    }
     107    }
     108    if (!empty($_SERVER['HTTP_X_FORWARDED']) && $this->validate_ip($_SERVER['HTTP_X_FORWARDED']))
     109    return $_SERVER['HTTP_X_FORWARDED'];
     110    if (!empty($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']) && $this->validate_ip($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']))
     111    return $_SERVER['HTTP_X_CLUSTER_CLIENT_IP'];
     112    if (!empty($_SERVER['HTTP_FORWARDED_FOR']) && $this->validate_ip($_SERVER['HTTP_FORWARDED_FOR']))
     113    return $_SERVER['HTTP_FORWARDED_FOR'];
     114    if (!empty($_SERVER['HTTP_FORWARDED']) && $this->validate_ip($_SERVER['HTTP_FORWARDED']))
     115    return $_SERVER['HTTP_FORWARDED'];
     116    // return unreliable ip since all else failed
     117    return $_SERVER['REMOTE_ADDR'];
    119118    }
    120119
    121120    public function validate_ip($ip) {
    122          if (filter_var($ip, FILTER_VALIDATE_IP,
    123                              FILTER_FLAG_IPV4 |
    124                              FILTER_FLAG_IPV6 |
    125                              FILTER_FLAG_NO_PRIV_RANGE |
    126                              FILTER_FLAG_NO_RES_RANGE) === false)
    127             return false;
    128         return true;
     121        if (
     122            filter_var( $ip,
     123                FILTER_VALIDATE_IP,
     124                FILTER_FLAG_IPV4 |
     125                FILTER_FLAG_IPV6 |
     126                FILTER_FLAG_NO_PRIV_RANGE |
     127                FILTER_FLAG_NO_RES_RANGE
     128            ) === false
     129        ) {
     130            return false;
     131        }
     132        return true;
    129133    }
    130134
     
    132136        global $post;
    133137        $stored_ip_addresses = 0;
    134         $options = get_option( 'wppv_api_settings' );
    135138        $selected_type = array();
    136         isset($options['wppv_api_post_checkbox_1']) ? $selected_type = $options['wppv_api_post_checkbox_1'] : '';
     139        isset($options['wppv_api_post_checkbox_1'] : '';
    137140       
    138141        if( is_object($post) && in_array($post->post_type , $selected_type)){
    139             if ( !empty($options['wppv_api_text_field_1']) ) {
     142            if ( !empty($options['wppv_api_text_field_1']) ) {
    140143                $stored_ip_addresses = get_post_meta(get_the_ID(),'view_ip',true);
    141144
     
    145148                    if(!in_array($current_ip, $stored_ip_addresses))
    146149                    {
    147                         $meta_key         = 'entry_views';
    148                         $view_post_meta   = get_post_meta(get_the_ID(), $meta_key, true);
     150                        $view_post_meta   = get_post_meta(get_the_ID(), $this->meta_key, true);
    149151                        $new_viewed_count = intval($view_post_meta) + 1;
    150                         update_post_meta(get_the_ID(), $meta_key, $new_viewed_count);
     152                        update_post_meta(get_the_ID(), $meta_key, $new_viewed_count);
    151153                        $stored_ip_addresses[] = $current_ip;
    152154                        update_post_meta(get_the_ID(),'view_ip',$stored_ip_addresses);
     
    154156                } else {
    155157                    $stored_ip_addresses = array();
    156                     $meta_key         = 'entry_views';
    157                     $view_post_meta   = get_post_meta(get_the_ID(), $meta_key, true);
     158                    $view_post_meta   = get_post_meta(get_the_ID(), $this->meta_key, true);
    158159                    $new_viewed_count = intval($view_post_meta) + 1;
    159                     update_post_meta(get_the_ID(), $meta_key, $new_viewed_count);
     160                    update_post_meta(get_the_ID(), $meta_key, $new_viewed_count);
    160161                    $stored_ip_addresses[] = $current_ip;
    161162                    update_post_meta(get_the_ID(),'view_ip',$stored_ip_addresses);
    162163                }
    163164            } else {
    164                 $meta_key         = 'entry_views';
    165                 $view_post_meta   = get_post_meta(get_the_ID(), $meta_key, true);
     165                $view_post_meta   = get_post_meta(get_the_ID(), $this->meta_key, true);
    166166                $new_viewed_count = intval($view_post_meta) + 1;
    167                 update_post_meta(get_the_ID(), $meta_key, $new_viewed_count);
    168             }
    169         }
    170 
    171     }
    172      
    173 }
    174 
    175 $post_view = new WP_Post_Views();
     167                update_post_meta(get_the_ID(), $this->meta_key, $new_viewed_count);
     168            }
     169        }
     170
     171    }
     172
     173    private function count_total_view( $post_type = 'post' ) {
     174        $total = 0;
     175
     176        if( $total = get_transient( $this->total_views_transient_key.$post_type ) ) {
     177            return $total;
     178        }
     179
     180        $arguments = array(
     181            'post_type' => $post_type,
     182            'posts_per_page' => '-1',
     183            'status' => 'publish',
     184        );
     185        $total_count_query = new WP_Query( $arguments );
     186
     187        if( $total_count_query->have_posts() ){
     188            while( $total_count_query->have_posts() ) {
     189                $total_count_query->the_post();
     190                $view_post_meta   = get_post_meta(get_the_ID(), $this->meta_key, true);
     191                $total += $view_post_meta;
     192            }
     193        }
     194        set_transient( $this->total_views_transient_key.$post_type, $total, $this->total_views_transient_expiration );
     195
     196        return $total;
     197    }
     198
     199    public function get_total_views( $post_type = 'post' ) {
     200        return $this->count_total_view($post_type);
     201    }
     202
     203}
     204
     205global $wp_post_views;
     206
     207$wp_post_views = new WP_Post_Views();
Note: See TracChangeset for help on using the changeset viewer.