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

Apply PHPCS and change some code according to coding standard #24

Merged
merged 1 commit into from
Jun 16, 2023
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
Binary file added .DS_Store
Binary file not shown.
104 changes: 51 additions & 53 deletions includes/settings.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
<?php
/**
* Settings Helper Class
*/
*/

class Wp_post_view_settings
{
public static function settings_init(){
// add_action( 'admin_menu', array( 'Wp_post_view_settings', 'settings_options' ) );
// add_action( 'admin_init', array( 'Wp_post_view_settings', 'settings_init_call' ) );

add_action( 'admin_menu', array( 'Wp_post_view_settings','wppv_api_add_admin_menu' ) );
add_action( 'admin_menu', array( 'Wp_post_view_settings','wppv_api_add_admin_menu' ) );

add_action( 'admin_init', array( 'Wp_post_view_settings','wppv_api_settings_init' ) );
add_action( 'admin_init', array( 'Wp_post_view_settings','wppv_api_settings_init' ) );

}
public static function wppv_activation_hook(){
Expand All @@ -29,55 +27,55 @@ public static function wppv_activation_hook(){
}

public static function wppv_api_add_admin_menu( ) {
add_options_page( 'Wp Post Views Settings', __('Wp Post Views Settings', 'wppv'), 'manage_options', 'settings-api-page', array( 'Wp_post_view_settings','wppv_api_options_page' ) );
add_options_page( 'Wp Post Views Settings', __('Wp Post Views Settings', 'wppv'), 'manage_options', 'settings-api-page', array( 'Wp_post_view_settings','wppv_api_options_page' ) );
}

public static function wppv_api_settings_init( ) {
register_setting( 'wppvPlugin', 'wppv_api_settings' );
add_settings_section(
'wppv_api_wppvPlugin_section',
__( 'Settings for WP Post Views', 'wppv' ),array( 'Wp_post_view_settings','wppv_api_settings_section_callback'),
'wppvPlugin'
);
register_setting( 'wppvPlugin', 'wppv_api_settings' );
add_settings_section(
'wppv_api_wppvPlugin_section',
__( 'Settings for WP Post Views', 'wppv' ),array( 'Wp_post_view_settings','wppv_api_settings_section_callback'),
'wppvPlugin'
);

add_settings_field(
'wppv_api_text_field_0',
__( 'Show post views coloumn', 'wppv' ),array( 'Wp_post_view_settings','wppv_show_views_callback'),
'wppvPlugin',
'wppv_api_wppvPlugin_section'
);
add_settings_field(
'wppv_api_text_field_1',
__( 'Views filter on IP (If checked multiple views will not count From same IP)', 'wppv' ),array( 'Wp_post_view_settings','filter_on_ip_callback'),
'wppvPlugin',
'wppv_api_wppvPlugin_section'
add_settings_field(
'wppv_api_text_field_0',
__( 'Show post views coloumn', 'wppv' ),array( 'Wp_post_view_settings','wppv_show_views_callback'),
'wppvPlugin',
'wppv_api_wppvPlugin_section'
);
add_settings_field(
'wppv_api_text_field_1',
__( 'Views filter on IP (If checked multiple views will not count From same IP)', 'wppv' ),array( 'Wp_post_view_settings','filter_on_ip_callback'),
'wppvPlugin',
'wppv_api_wppvPlugin_section'
);
add_settings_field(
'wppv_api_post_checkbox_1',
__( 'Select your custom post type', 'wppv' ),array( 'Wp_post_view_settings','select_post_type_callback'),
'wppvPlugin',
'wppv_api_wppvPlugin_section'
);
'wppv_api_post_checkbox_1',
__( 'Select your custom post type', 'wppv' ),array( 'Wp_post_view_settings','select_post_type_callback'),
'wppvPlugin',
'wppv_api_wppvPlugin_section'
);
}

public static function wppv_show_views_callback( ) {
$options = get_option( 'wppv_api_settings' );
$checkbox_val = empty($options['wppv_api_text_field_0']) ? '' : $options['wppv_api_text_field_0'] ;
?>
<input type='checkbox' name='wppv_api_settings[wppv_api_text_field_0]' value="1" <?php checked( 1, $checkbox_val, true ); ?>>
<?php
$options = get_option( 'wppv_api_settings' );
$checkbox_val = empty($options['wppv_api_text_field_0']) ? '' : $options['wppv_api_text_field_0'] ;
?>
<input type='checkbox' name='wppv_api_settings[wppv_api_text_field_0]' value="1" <?php checked( 1, $checkbox_val, true ); ?>>
<?php
}

public static function filter_on_ip_callback( ) {
$options = get_option( 'wppv_api_settings' );
$checkbox_val = empty($options['wppv_api_text_field_1']) ? '' : $options['wppv_api_text_field_1'] ;
?>
<input type='checkbox' name='wppv_api_settings[wppv_api_text_field_1]' value="1" <?php checked( 1, $checkbox_val, true ); ?>>
<?php
$options = get_option( 'wppv_api_settings' );
$checkbox_val = empty($options['wppv_api_text_field_1']) ? '' : $options['wppv_api_text_field_1'] ;
?>
<input type='checkbox' name='wppv_api_settings[wppv_api_text_field_1]' value="1" <?php checked( 1, $checkbox_val, true ); ?>>
<?php
}

public static function select_post_type_callback( ) {
$options = get_option( 'wppv_api_settings' );
$options = get_option( 'wppv_api_settings' );
$checkbox_val = empty($options['wppv_api_post_checkbox_1']) ? '' : $options['wppv_api_post_checkbox_1'] ;
$args = [
'public' => true,
Expand All @@ -92,27 +90,27 @@ public static function select_post_type_callback( ) {
<?php
foreach($post_types as $post_type){
?>
<label for=""><?php echo $post_type->label;?> </label>
<input type='checkbox' name='wppv_api_settings[wppv_api_post_checkbox_1][<?php echo $post_type->name ?>]' value="<?php echo $post_type->name ?>" <?php checked( ( $post_type->name == @$checkbox_val[$post_type->name]), true ); ?>>
<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 ); ?>>
<?php
}
}

public static function wppv_api_settings_section_callback( ) {
echo __( 'This will show one extra coloumn in Post listing page which is show the counts', 'wppv' );
echo esc_html__( 'This will show one extra coloumn in Post listing page which is show the counts', 'wppv' );
}

public static function wppv_api_options_page( ) {
?>
<form action='options.php' method='post'>
<h2><?php _e("Wp post View All Settings Admin Page", 'wppv'); ?></h2>
<?php
settings_fields( 'wppvPlugin' );
do_settings_sections( 'wppvPlugin' );
submit_button();
?>
</form>
<?php
?>
<form action='options.php' method='post'>
<h2><?php _e("Wp post View All Settings Admin Page", 'wppv'); ?></h2>
<?php
settings_fields( 'wppvPlugin' );
do_settings_sections( 'wppvPlugin' );
submit_button();
?>
</form>
<?php
}
}
?>
?>
2 changes: 1 addition & 1 deletion includes/shortcodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ function wppv_current_post_view_per_post_type_callback($atts = array() , $conten
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' );
}
}
}
2 changes: 1 addition & 1 deletion readme.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
=== Wp Post Views - Wordpress Post views counter ===
Contributors: vanpariyar, ankitatanti, Brijeshdhanani, piyushmultidots
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
Expand Down
74 changes: 49 additions & 25 deletions wp-post-views.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
* Author: Ronak J Vanpariya
* Author URI: https://vanpariyar.github.io
* Text Domain: wppv
* Domain Path: /languages
* Domain Path: /languages
* License: GPL v2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/
*/

// Make sure we don't expose any info if called directly
if ( !function_exists( 'add_action' ) ) {
echo __('Hi there! I\'m just a plugin, not much I can do when called directly.', 'wppv');
// Make sure we don't expose any info if called directly.
if ( ! function_exists( 'add_action' ) ) {
echo esc_html__('Hi there! I\'m just a plugin, not much I can do when called directly.', 'wppv');
exit;
}

Expand Down Expand Up @@ -85,38 +85,62 @@ public function wppv_posts_custom_column_views( $column ) {
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;
echo esc_html( $view_post_meta );
}
}

}

public function get_ip_address()
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']);
// 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_STRING);
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;
}
}
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 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,
Expand Down Expand Up @@ -144,7 +168,7 @@ public function counter(){

$current_ip = $this->get_ip_address();
if( $stored_ip_addresses )
{
{
if(!in_array($current_ip, $stored_ip_addresses))
{
$view_post_meta = get_post_meta(get_the_ID(), $this->meta_key, true);
Expand Down