| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name |
| 4 |
* |
| 5 |
* @package WP Post Views |
| 6 |
* @author Ronak J Vanpariya |
| 7 |
* @copyright Ronak J Vanpariya |
| 8 |
* @license GPL-2.0-or-later |
| 9 |
* |
| 10 |
* @wordpress-plugin |
| 11 |
* Plugin Name: WP Post Views |
| 12 |
* Plugin URI: https://github.com/vanpariyar/wp-post-views |
| 13 |
* Description: WP Post Views. |
| 14 |
* Version: 1.8 |
| 15 |
* Requires at least: 5.0 |
| 16 |
* Requires PHP: 5.3 |
| 17 |
* Author: Ronak J Vanpariya |
| 18 |
* Author URI: https://vanpariyar.github.io |
| 19 |
* Text Domain: wppv |
| 20 |
* License: GPL v2 or later |
| 21 |
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt |
| 22 |
|
| 23 |
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. |
| 24 |
|
| 25 |
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. |
| 26 |
|
| 27 |
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 |
| 28 |
|
| 29 |
*/ |
| 30 |
|
| 31 |
// Make sure we don't expose any info if called directly |
| 32 |
if ( !function_exists( 'add_action' ) ) { |
| 33 |
echo __('Hi there! I\'m just a plugin, not much I can do when called directly.', 'wppv'); |
| 34 |
exit; |
| 35 |
} |
| 36 |
|
| 37 |
/* Plugin Constants */ |
| 38 |
if (!defined('WP_POST_VIEW_URL')) { |
| 39 |
define('WP_POST_VIEW_URL', plugin_dir_url(__FILE__)); |
| 40 |
} |
| 41 |
|
| 42 |
if (!defined('WP_POST_VIEW_PLUGIN_PATH')) { |
| 43 |
define('WP_POST_VIEW_PLUGIN_PATH', plugin_dir_path(__FILE__)); |
| 44 |
} |
| 45 |
|
| 46 |
require_once (WP_POST_VIEW_PLUGIN_PATH . '/includes/settings.php'); |
| 47 |
|
| 48 |
require_once (WP_POST_VIEW_PLUGIN_PATH . '/includes/shortcodes.php'); |
| 49 |
|
| 50 |
register_activation_hook( __FILE__, array('Wp_post_view_settings','wppv_activation_hook') ); |
| 51 |
|
| 52 |
/** |
| 53 |
* MAIN CLASS |
| 54 |
*/ |
| 55 |
class WP_Post_Views |
| 56 |
{ |
| 57 |
function __construct() |
| 58 |
{ |
| 59 |
add_action( 'wp_head', array( $this , 'counter'), 10, 1 ); |
| 60 |
add_filter( 'manage_posts_columns', array( $this,'wppv_posts_column_views') ); |
| 61 |
add_filter( 'manage_pages_columns', array( $this,'wppv_posts_column_views') ); |
| 62 |
add_action( 'manage_posts_custom_column', array( $this,'wppv_posts_custom_column_views') ); |
| 63 |
add_action( 'manage_pages_custom_column', array( $this,'wppv_posts_custom_column_views') ); |
| 64 |
Wp_post_view_settings::settings_init(); |
| 65 |
} |
| 66 |
|
| 67 |
public function wppv_posts_column_views( $columns ) { |
| 68 |
|
| 69 |
$options = get_option( 'wppv_api_settings' ); |
| 70 |
|
| 71 |
//$options['wppv_api_text_field_0']; |
| 72 |
if ( !empty($options['wppv_api_text_field_0']) ) { |
| 73 |
$columns['post_views'] = 'Views'; |
| 74 |
} |
| 75 |
return $columns; |
| 76 |
} |
| 77 |
|
| 78 |
public function wppv_posts_custom_column_views( $column ) { |
| 79 |
$options = get_option( 'wppv_api_settings' ); |
| 80 |
if ( !empty($options['wppv_api_text_field_0']) ) { |
| 81 |
if ( $column === 'post_views') { |
| 82 |
$view_post_meta = get_post_meta(get_the_ID(), 'entry_views', true); |
| 83 |
echo $view_post_meta; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
} |
| 88 |
|
| 89 |
public function get_ip_address() |
| 90 |
{ |
| 91 |
// check for shared internet/ISP IP |
| 92 |
if (!empty($_SERVER['HTTP_CLIENT_IP']) && $this->validate_ip($_SERVER['HTTP_CLIENT_IP'])) |
| 93 |
return $_SERVER['HTTP_CLIENT_IP']; |
| 94 |
// check for IPs passing through proxies |
| 95 |
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { |
| 96 |
// check if multiple ips exist in var |
| 97 |
$iplist = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']); |
| 98 |
foreach ($iplist as $ip) { |
| 99 |
if ($this->validate_ip($ip)) |
| 100 |
return $ip; |
| 101 |
} |
| 102 |
} |
| 103 |
if (!empty($_SERVER['HTTP_X_FORWARDED']) && $this->validate_ip($_SERVER['HTTP_X_FORWARDED'])) |
| 104 |
return $_SERVER['HTTP_X_FORWARDED']; |
| 105 |
if (!empty($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']) && $this->validate_ip($_SERVER['HTTP_X_CLUSTER_CLIENT_IP'])) |
| 106 |
return $_SERVER['HTTP_X_CLUSTER_CLIENT_IP']; |
| 107 |
if (!empty($_SERVER['HTTP_FORWARDED_FOR']) && $this->validate_ip($_SERVER['HTTP_FORWARDED_FOR'])) |
| 108 |
return $_SERVER['HTTP_FORWARDED_FOR']; |
| 109 |
if (!empty($_SERVER['HTTP_FORWARDED']) && $this->validate_ip($_SERVER['HTTP_FORWARDED'])) |
| 110 |
return $_SERVER['HTTP_FORWARDED']; |
| 111 |
// return unreliable ip since all else failed |
| 112 |
return $_SERVER['REMOTE_ADDR']; |
| 113 |
} |
| 114 |
|
| 115 |
public function validate_ip($ip) { |
| 116 |
if (filter_var($ip, FILTER_VALIDATE_IP, |
| 117 |
FILTER_FLAG_IPV4 | |
| 118 |
FILTER_FLAG_IPV6 | |
| 119 |
FILTER_FLAG_NO_PRIV_RANGE | |
| 120 |
FILTER_FLAG_NO_RES_RANGE) === false) |
| 121 |
return false; |
| 122 |
return true; |
| 123 |
} |
| 124 |
|
| 125 |
public function counter(){ |
| 126 |
global $post; |
| 127 |
$stored_ip_addresses = 0; |
| 128 |
$options = get_option( 'wppv_api_settings' ); |
| 129 |
$selected_type = array(); |
| 130 |
isset($options['wppv_api_post_checkbox_1']) ? $selected_type = $options['wppv_api_post_checkbox_1'] : ''; |
| 131 |
if(in_array($post->post_type , $selected_type)){ |
| 132 |
if ( !empty($options['wppv_api_text_field_1']) ) { |
| 133 |
$stored_ip_addresses = get_post_meta(get_the_ID(),'view_ip',true); |
| 134 |
|
| 135 |
$current_ip = $this->get_ip_address(); |
| 136 |
if( $stored_ip_addresses ) |
| 137 |
{ |
| 138 |
if(!in_array($current_ip, $stored_ip_addresses)) |
| 139 |
{ |
| 140 |
$meta_key = 'entry_views'; |
| 141 |
$view_post_meta = get_post_meta(get_the_ID(), $meta_key, true); |
| 142 |
$new_viewed_count = intval($view_post_meta) + 1; |
| 143 |
update_post_meta(get_the_ID(), $meta_key, $new_viewed_count); |
| 144 |
$stored_ip_addresses[] = $current_ip; |
| 145 |
update_post_meta(get_the_ID(),'view_ip',$stored_ip_addresses); |
| 146 |
} |
| 147 |
} else { |
| 148 |
$stored_ip_addresses = array(); |
| 149 |
$meta_key = 'entry_views'; |
| 150 |
$view_post_meta = get_post_meta(get_the_ID(), $meta_key, true); |
| 151 |
$new_viewed_count = intval($view_post_meta) + 1; |
| 152 |
update_post_meta(get_the_ID(), $meta_key, $new_viewed_count); |
| 153 |
$stored_ip_addresses[] = $current_ip; |
| 154 |
update_post_meta(get_the_ID(),'view_ip',$stored_ip_addresses); |
| 155 |
} |
| 156 |
} else { |
| 157 |
$meta_key = 'entry_views'; |
| 158 |
$view_post_meta = get_post_meta(get_the_ID(), $meta_key, true); |
| 159 |
$new_viewed_count = intval($view_post_meta) + 1; |
| 160 |
update_post_meta(get_the_ID(), $meta_key, $new_viewed_count); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
} |
| 165 |
|
| 166 |
} |
| 167 |
|
| 168 |
$post_view = new WP_Post_Views(); |
| 169 |
|