Forked from serkanalgur/Search Block in Transient (30 Sec)
Last active
January 12, 2017 07:33
-
-
Save numanturle/70f2455c1adf3784c2f592f1132ca361 to your computer and use it in GitHub Desktop.
This file contains 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']; | |
}else { | |
$ip = $_SERVER['REMOTE_ADDR']; | |
} | |
return apply_filters( 'wpb_get_ip', $ip ); | |
} | |
function search_visitor_ip_block($query) { | |
// Visitor search limit | |
$visitorIPLimit = 5; | |
if(preg_match('/google|yandex|yndx|spider|bot|slurp|msn|bing|adsbot|AdIdxBot|search|face|baidu|duck|sogou|youdao|ccbot|alexa|microsoft/i',gethostbyaddr(get_the_user_ip()))){ | |
// bot dedect | |
return $query; | |
}else { | |
$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); | |
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