Last active
January 11, 2017 22:06
-
-
Save serkanalgur/9ba86c252d27e84d58c277f351d65a7f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Get user IP in WordPress | |
function get_the_user_ip() { | |
if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) { | |
//check ip from share internet | |
$ip = $_SERVER['HTTP_CLIENT_IP']; | |
} elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) { | |
//to check ip is pass from proxy | |
$ip = $_SERVER['HTTP_X_FORWARDED_FOR']; | |
} else { | |
$ip = $_SERVER['REMOTE_ADDR']; | |
} | |
return apply_filters( 'wpb_get_ip', $ip ); | |
} | |
function search_visitor_ip_block($query) { | |
// Visitor search limit | |
$visitorIPLimit = 20; | |
$visitorIPCount = get_option( 'visitor_ip_count'.get_the_user_ip() ); | |
//Daha Zamanı var mı soralım | |
$is_block = get_transient( 'visitor_ip_block'.get_the_user_ip() ); | |
$will_deleted = get_option('visitor_ip_block'.get_the_user_ip().'-deleteafter30min'); | |
//Sonrasında blok kontrolü yapalım | |
if($is_block == 'blocked') : | |
wp_die('Sakin ol Şampiyon? Daha blok süren bitmemiş :)'); | |
else : | |
//Daha varmış normal prosedür devam etsin | |
if ( $query->is_search ) : | |
if(($visitorIPCount >= $visitorIPLimit) && $will_deleted == 'yes') : | |
delete_option('visitor_ip_count'.get_the_user_ip()); | |
delete_option('visitor_ip_block'.get_the_user_ip().'-deleteafter30min'); | |
//Limitimizden aşağıda mı kontrol edip ekleyelim | |
elseif( $visitorIPCount < $visitorIPLimit ) : | |
$visitorIPCount += 1 ; | |
update_option( 'visitor_ip_count'.get_the_user_ip(), $visitorIPCount ); | |
return $query; | |
else : | |
//30 Dakika engel için geçici opsiyon atayalım | |
set_transient('visitor_ip_block'.get_the_user_ip(),'blocked',30 * MINUTE_IN_SECONDS); | |
update_option('visitor_ip_block'.get_the_user_ip().'-deleteafter30min','yes'); | |
//Sorguyu öldürelim | |
wp_die('Sakin ol Şampiyon?'); | |
endif; | |
endif; | |
endif; | |
} | |
//hook filters to search | |
add_filter('pre_get_posts','search_visitor_ip_block'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment