Ask your WordPress questions! Pay money and get answers fast! (more info)

Question for Gabriel Reguly

  • SOLVED

Hi Gabriel,

This is the agreed question in order to have the finalised code.

Answers (1)

2013-03-28

Gabriel Reguly answers:

Hi Eliran,

Thanks for this, please vote for me after you tested the code.

For all other experts, I have helped Eliran here:

http://wpquestions.com/question/showChronoLoggedIn/id/8183

and he requested a bit more of help, so this question was to pay for it.

Regards,
Gabriel


Gabriel Reguly comments:


<?php
class CensoredSearchesWidget {
function CensoredSearchesWidget() {
add_action( 'admin_menu', array( &$this, 'admin_menu' ));
add_action( 'template_redirect', array( &$this, 'template_redirect'));
add_action( 'widgets_init', array( &$this, 'widgets_init' ));

}


function admin_menu() {
add_submenu_page('options-general.php',
__('Censored Searches', 'cs'),
__('Censored Searches', 'cs') ,
'read',
'censored_searches',
array( &$this, 'censored_searches_page' ));
}


function censored_searches_page() {
$settings_updated = false;
if ( isset( $_REQUEST['block_ip'] ) ) {
$blocked_ips = get_option( 'recent_searches_widget_log' );
$ip = $_REQUEST['block_ip'];
$key = md5( $ip );
$blocked_ip = array (
'ip' => $ip,
'count' => 3,
);
$blocked_ips[ $key ] = $blocked_ip;
update_option( 'recent_searches_widget_log', $blocked_ips );
$settings_updated = true;
}
if ( isset( $_REQUEST['unblock'] ) ) {
$blocked_ips = get_option( 'recent_searches_widget_log' );
unset( $blocked_ips[ $_REQUEST['unblock'] ]);
update_option( 'recent_searches_widget_log', $blocked_ips );
$settings_updated = true;
}

if ( isset( $_REQUEST['google'] ) ) {
$google_ips = get_option( 'recent_searches_widget_google' );
$ip = $_REQUEST['google'];
$key = md5( $ip );
$google_ip = array (
'ip' => $ip,
'google' => 'yes',
'name' => $_REQUEST['name'],
);
$google_ips[ $key ] = $google_ip;
update_option( 'recent_searches_widget_google', $google_ips );
$settings_updated = true;
}
if ( isset( $_REQUEST['ungoogle'] ) ) {
$google_ips = get_option( 'recent_searches_widget_google' );
unset( $google_ips[ $_REQUEST['ungoogle'] ]);
update_option( 'recent_searches_widget_google', $google_ips );
$settings_updated = true;
}

if ( isset( $_REQUEST['allow_ip'] ) ) {
$allowed_ips = get_option( 'recent_searches_widget_allow' );
$ip = $_REQUEST['allow_ip'];
$key = md5( $ip );
$allowed_ip = array (
'ip' => $ip,
);
$allowed_ips[ $key ] = $allowed_ip;
update_option( 'recent_searches_widget_allow', $allowed_ips );
$settings_updated = true;
}
if ( isset( $_REQUEST['unallow'] ) ) {
$allowed_ips = get_option( 'recent_searches_widget_allow' );
unset( $allowed_ips[ $_REQUEST['unallow'] ]);
update_option( 'recent_searches_widget_allow', $allowed_ips );
$settings_updated = true;
}
?>

<div class="wrap woocommerce">
<div class="icon32 icon32-woocommerce-settings" id="icon-woocommerce"><br /></div>
<h2><?php _e('Censored Searches', 'cs')?></h2>
<?php
if ( false !== $settings_updated ) {
?>
<div id="message" class="updated fade">
<p><strong><?php _e( 'Your settings have been saved.', 'woocommerce' ) ?></strong></p>
</div>
<?php
}
?>
<h3><?php _e('Allowed IPs', 'cs')?></h3>
<form action="<?php echo admin_url() ?>/options-general.php">
<input type="hidden" name="page" value="censored_searches" />
<input type="text" name="allow_ip" />
<input type="submit" value="<?php _e('Allow', 'cs') ?>" />
</form>
<?php
$allowed_ips = get_option( 'recent_searches_widget_allow' );
if ( ! $allowed_ips ) {
_e( 'None', 'cs');
} else {
echo '<ol>';
foreach( $allowed_ips as $key => $allowed_ip ) {
echo '<li>' . $allowed_ip['ip'] . ' <a href="'. admin_url() . '/options-general.php?page=censored_searches&unallow=' . $key . '">' . __( 'Delete' ) .'</a></li>';
}
echo '</ol>';
}
?>
<h3><?php _e('Blocked IPs', 'cs')?></h3>
<form action="<?php echo admin_url() ?>/options-general.php">
<input type="hidden" name="page" value="censored_searches" />
<input type="text" name="block_ip" />
<input type="submit" value="<?php _e('Block', 'cs') ?>" />
</form>


<?php
$blocked_ips = get_option( 'recent_searches_widget_log' );
if ( ! $blocked_ips ) {
_e( 'None', 'cs');
} else {
echo '<ol>';
foreach( $blocked_ips as $key => $blocked_ip ) {
if ( $blocked_ip['count'] < 3 ) continue;
echo '<li>' . $blocked_ip['ip'] . ' <a href="'. admin_url() . '/options-general.php?page=censored_searches&unblock=' . $key . '">' . __( 'Delete' ) .'</a></li>';
}
echo '</ol>';
}

?>
<h3><?php _e('Google IPs', 'cs')?></h3>
<form action="<?php echo admin_url() ?>/options-general.php">
<input type="hidden" name="page" value="censored_searches" />
<input type="text" name="google" /><input type="text" name="name" placeholder="<?php _e('Domain name', 'cs'); ?>" />
<input type="submit" value="<?php _e('Add Google IP', 'cs') ?>" />
</form>
<?php
$google_ips = get_option( 'recent_searches_widget_google' );
if ( ! $google_ips ) {
_e( 'None', 'cs');
} else {
echo '<ol>';
foreach( $google_ips as $key => $google_ip ) {
echo '<li>' . $google_ip['ip'] . ' ' . $google_ip['name'] . ' <a href="'. admin_url() . '/options-general.php?page=censored_searches&ungoogle=' . $key . '">' . __( 'Remove' ) .'</a></li>';
}
echo '</ol>';
}
?>
</div>
<?php
}



function template_redirect() {


$mtime = microtime();
$mtime = explode(' ', $mtime);
$time = $mtime[1] + $mtime[0];

$logged_ips = (array) get_option( 'recent_searches_widget_log' );
$allowed_ips = (array) get_option( 'recent_searches_widget_allow' );
$google_ips = (array) get_option( 'recent_searches_widget_google' );
$proxy = '';
if ( getenv( 'HTTP_CLIENT_IP' ) ) {
$proxy = $_SERVER['REMOTE_ADDR'];
$ip = getenv( 'HTTP_CLIENT_IP' ) ;
} elseif( $_SERVER['HTTP_X_FORWARDED_FOR'] ) {
$proxy = $_SERVER['REMOTE_ADDR'];
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}

$user_agent = $_SERVER['HTTP_USER_AGENT'];

$saved_ip = $ip;

if ( ! empty( $proxy ) ) {
$ip = $ip . $proxy;
}
$key = md5( $ip );

if ( isset( $allowed_ips[$key] ) ) {
// echo $allowed_ips[$key]['ip'] . ' is allowed ';
} elseif ( isset( $google_ips[$key] ) ) {
// echo $google_ips[$key]['ip'] . ' is google, therefore allowed ' . $google_ips[$key]['name'];
} else {
if ( isset( $logged_ips[$key] ) ) {
if ( (int) $logged_ips[$key]['count'] >= 3 ) {
wp_die('Die spammer!');
}
}
}

if (is_search()) {
global $timestart;
if ( strstr( $user_agent, 'Googlebot' ) ) {
$reverse = @gethostbyaddr( $saved_ip );
if ( stristr( $reverse, 'googlebot' ) ) {
$check_ip = @gethostbyname( $reverse );
if ( $check_ip == $saved_ip ) {
$google_ip = array (
'ip' => $ip,
'google' => 'yes',
'name' => $reverse,
);
$google_ips[ $key ] = $google_ip;
update_option( 'recent_searches_widget_google', $google_ips );
}
}
}
if ( ! isset( $logged_ips[$key] ) ) {
$logged_ips[$key] = array (
'count' => '1',
'ip' => $ip,
'time' => $time,
);
} else {
if ( (float) ( $time - $logged_ips[$key]['time'] ) <= 10 ) {
$logged_ips[$key]['count']++;
// echo $logged_ips[$key]['ip'] . ' is being incremented to ' . ($logged_ips[$key]['count']) . ' , time diff is ' . (float) ( $time - $logged_ips[$key]['time'] );
} else {
// echo $logged_ips[$key]['ip'] . ' is starting over, time diff is ' . number_format( ( $time - $logged_ips[$key]['time']) , 3 );
$logged_ips[$key]['count'] = 1;
$logged_ips[$key]['time'] = $time;
}
}

foreach( $logged_ips as $key => $values ) {
if ( @array_key_exists( 'time', $values ) ) {
if ( (float) ( $time - $values['time'] ) > 604800 ) { // 7 days
unset( $logged_ips[ $key ] );
}
}
}

update_option( 'recent_searches_widget_log', $logged_ips );

$query = $this->strtolower(trim( get_search_query()));
$options = get_option('recent_searches_widget');
if (!is_array($options)) {
$options = $this->get_default_options();
}
$max = $options['max'];
$data = get_option('recent_searches_widget_data', array());
if (!is_array($data)) {
if (isset( $options['data'])) {
$data = $options['data'];
unset($options['data']);
update_option('recent_searches_widget', $options);
}

if (!is_array($data)) {
$data = array();
}
}

$pos = array_search($query, $data);
if ($pos !== false) {
if ($pos != 0) {
$data = array_merge(array_slice($data, 0, $pos), array($query), array_slice($data, $pos + 1));
}
} else {
array_unshift($data, $query);
if (count($data) > $max) {
array_pop($data);
}
}
update_option('recent_searches_widget_data', $data);
}
}

function widgets_init() {
$widget_ops = array(
'classname' => 'widget_rsw',
'description' => __('Shows recent searches', 'recent-searches-widget'),
);
wp_register_sidebar_widget( 'CensoredSearchesWidget', __('Recent Searches', 'recent-searches-widget'), array( &$this, 'widget_rsw' ), $widget_ops );
wp_register_widget_control( 'CensoredSearchesWidget', __('Recent Searches', 'recent-searches-widget'), array( &$this, 'widget_rsw_control' ) );
}

function widget_rsw( $args ) {
extract( $args );
if (empty($title)) {
$title = '&nbsp;';
}
$options = get_option('recent_searches_widget', array() );
echo $before_widget . $before_title . $options['title'] . $after_title, "\n";
$this->show_recent_searches( "<ul>\n<li>", "</li>\n</ul>", "</li>\n<li>" );
echo $after_widget;
}

function show_recent_searches( $before_list, $after_list, $between_items ) {
$options = get_option( 'recent_searches_widget' );
if ( !is_array( $options ) ) {
$options = $this->get_default_options();
}
$data = get_option( 'recent_searches_widget_data' );
if ( !is_array( $data ) ) {
if ( isset( $options['data'] ) ) {
$data = $options['data'];
}
if ( !is_array( $data ) ) {
$data = array();
}
}

if ( count( $data ) > 0 ) {
echo $before_list;
$first = true;
foreach ( $data as $search ) {
$found=false;
$banned = explode("\n", $options['filter']);
foreach ($banned as $filter) {
if (mb_strlen($search, mb_detect_encoding($search)) <= 47) {
if ($filter!="") {
// preg_match("/".str_replace(array("?", "$", "/", "*", "^"), array("\?", "\$", "\/", "\*", "\^"), trim($filter))."/i", trim($search))
if (substr_count(strtolower(trim($search)), strtolower(trim($filter)))>=1) {
$found=true;
}
}
} else $found = true;
}
if ($found===false) {
if ( $first ) {
$first = false;
} else {
echo $between_items;
}
echo '<a href="', get_search_link( $search ), '"';
if ( $options['nofollow'] ) {
echo ' rel="nofollow"';
}
echo '>', wp_specialchars( $search ), '</a>';
}
}
echo $after_list, "\n";
}
}

function widget_rsw_control() {
$options = $newoptions = get_option('recent_searches_widget', array() );
if ( count( $options ) == 0 ) {
$options = $this->get_default_options();
update_option( 'recent_searches_widget', $options );
}
if ( isset( $_POST['rsw-submit'] ) ) {

$options['title'] = strip_tags( stripslashes( $_POST['rsw-title'] ) );
$options['max'] = (int)( $_POST['rsw-max'] );
$options['filter'] = stripslashes($_POST["rsw-filter"]);
if ( count( $options['data'] ) > $options['max'] ) {
$options['data'] = array_slice( $options['data'], 0, $options['max'] );
}
update_option( 'recent_searches_widget', $options );
}
$title = attribute_escape( $options['title'] );
$max = attribute_escape( $options['max'] );
$nofollow = $options['nofollow'];
$filter = $options['filter'];
?>
<p><label for="rsw-title"><?php _e('Title:', 'recent-searches-widget'); ?> <input class="widefat" id="rsw-title" name="rsw-title" type="text" value="<?php echo $title; ?>" /></label></p>
<p><label for="rsw-title"><textarea id="rsw-filter" name="rsw-filter" style="width: 100%; height: 175px;"><?php echo $filter; ?></textarea></label></p>
<p><label for="rsw-max"><?php _e('Max searches:', 'recent-searches-widget'); ?> <input id="rsw-max" name="rsw-max" type="text" size="3" maxlength="5" value="<?php echo $max; ?>" /></label></p>
<input type="hidden" id="rsw-submit" name="rsw-submit" value="1" />
<?php
}

function strtolower( $str ) {
if ( function_exists( 'mb_strtolower' ) ) {
return mb_strtolower( $str );
} else {
return strtolower( $str );
}
}

function get_default_options() {
return array(
'title' => '',
'max' => 10,
'nofollow' => true,
);
}
}

if ( !function_exists( 'esc_html' ) ) {
function esc_html( $text ) {
return wp_specialchars( $text );
}
}



if ( !function_exists( 'esc_attr' ) ) {
function esc_attr( $text ) {
return attribute_escape( $text );
}
}

if ( !function_exists( 'get_search_link' ) ) {
function get_search_link( $query = '' ) {
global $wp_rewrite;
if ( empty($query) )
$search = get_search_query();
else
$search = stripslashes($query);

$permastruct = $wp_rewrite->get_search_permastruct();
if ( empty( $permastruct ) ) {
$link = home_url('?s=' . urlencode($search) );
} else {
$search = urlencode($search);
$search = str_replace('%2F', '/', $search);
$link = str_replace( '%search%', $search, $permastruct );
$link = trailingslashit( get_option( 'home' ) ) . user_trailingslashit( $link, 'search' );
}
return apply_filters( 'search_link', $link, $search );
}
}

$wp_recent_searches_widget = new CensoredSearchesWidget();

function rsw_show_recent_searches( $before_list = "<ul>\n<li>", $after_list = "</li>\n</ul>", $between_items = "</li>\n<li>" ) {
global $wp_recent_searches_widget;
$wp_recent_searches_widget->show_recent_searches( $before_list, $after_list, $between_items );
}

?>