<?php
/**
* Geo Redirect Admin functions
*/
function geo_redirect_admin_page(){
if ( function_exists('add_submenu_page') )
add_submenu_page( 'options-general.php', __('Geographical Redirect Options'), __('Geo Redirect'), 'manage_options', 'geo_redirect', 'geo_redirect_admin_page_display' );
}
function geo_redirect_admin_description(){
$html = '<p>This plugin allows you to redirect your visitors according to their country.</p>
<p>Just add the country from selectbox and fill in preferable options.</p>
<p>Static URL redirect has the highest priority if not blank.<br />
Domain redirect has lower priority if not blank.<br />
Redirect by language has the lowest priority among the options.</p>
<p>If you have some troubles accessing your site because of incorrect redirect,
just add to browser\'s url «no_redirect» (example: '.get_bloginfo('url').'/?page_id=5&<b>no_redirect</b>) to ignore redirection.</p>';
return $html;
}
function geo_redirect_admin_page_display(){
if (isset($_POST['submit']) && check_admin_referer('submit_geo_redirect_x','geo_redirect_nonce_y'))
geo_redirect_save();
if ( !empty($_POST['submit'] ) )
$html = '<div id="message" class="updated fade"><p><strong>' . __('Options saved.') . '</strong></p></div>';
$html .= '
<div class="wrap">
<div id="icon-options-general" class="icon32"><br></div>
<h2>' . __('Geographical Redirect Options') . '</h2>
<form action="" method="post" enctype="multipart/form-data">';
$html .= geo_redirect_admin_description();
$redirect = '';
$only_outsite = 0;
$lang_slug = 'lang';
$geo_redirect_data = get_option('geo_redirect_data');
if ($geo_redirect_data === false) {
add_option('geo_redirect_data', '');
} elseif (is_array($geo_redirect_data)) {
$redirect = $geo_redirect_data['redirect'];
$only_outsite = $geo_redirect_data['only_outsite'];
$lang_slug = $geo_redirect_data['lang_slug'];
}
$geoip = new GeoIP();
$countries = $geoip->GEOIP_COUNTRY_NAMES;
if (is_array($countries)) :
$html .= '<div class="tablenav top">';
$html .= '<div class="alignleft actions">';
$html .= '<select class="countries" name="countries[]">';
foreach ($countries as $country_id => $country) :
if ($country_id == 0)
$html .= '<option value="' . $country_id . '">' . __('Select country') . '</option>';
elseif (!in_array($country_id,array(1,2)))
$html .= '<option value="' . $country_id . '">' . htmlspecialchars($country) . '</option>';
endforeach;
$html .= '</select>';
$html .= '<input onclick="return geoAddCountry();" type="submit" class="button-secondary action" value="Add country" />';
$html .= '</div></div><br clear="all" />';
endif;
$html .= '<div class="geo-redirect-options">
<table class="wp-list-table widefat plugins" cellspacing="0">
<thead>
<tr>
<th scope="col" id="name" class="manage-column column-name" style="">Country</th>
<th scope="col" id="name" class="manage-column column-name" style="">Language Code</th>
<th scope="col" id="name" class="manage-column column-name" style="">Domain Name</th>
<th scope="col" id="name" class="manage-column column-name" style="">Static URL</th>
<th scope="col" id="name" class="manage-column column-name" style=""></th>
</tr>
</thead>
<tbody>';
$default_shown = false;
if (is_array($redirect)) {
foreach ($redirect as $data) {
if ($data['country_id'] == -1) {
$default_shown = true;
$html .='<tr class="geo-redirect-option active">'.
'<td>'.
'<input type="hidden" name="country_ids[]" value="-1">'.
'<p><b>Not assigned countries</b></p>'.
'</td>'.
'<td>'.
'<p><input id="geo-redirect-country--1" class="small-text" name="lang_codes[]" type="text" maxlength="2" value="'.stripslashes($data['lang_code']).'"></p>'.
'</td>'.
'<td>'.
'<p><input class="regular-text" name="domains[]" type="text" value="'.stripslashes($data['domain']).'"></p>'.
'</td>'.
'<td>'.
'<p><input class="regular-text" name="urls[]" type="text" value="'.stripslashes($data['url']).'"></p>'.
'</td>'.
'<td>'.
'</td>'.
'</tr>';
} else {
$html .='<tr class="geo-redirect-option active">'.
'<td>'.
'<input type="hidden" name="country_ids[]" value="'.$data['country_id'].'">'.
'<p><b>'.$countries[$data['country_id']].'</b></p>'.
'</td>'.
'<td>'.
'<p><input id="geo-redirect-country-'.$data['country_id'].'" class="small-text" name="lang_codes[]" type="text" maxlength="2" value="'.stripslashes($data['lang_code']).'"></p>'.
'</td>'.
'<td>'.
'<p><input class="regular-text" name="domains[]" type="text" value="'.stripslashes($data['domain']).'"></p>'.
'</td>'.
'<td>'.
'<p><input class="regular-text" name="urls[]" type="text" value="'.stripslashes($data['url']).'"></p>'.
'</td>'.
'<td>'.
'<p style="line-height:2.3"><a onclick="return geoRemoveCountry(this);" href="#" class="delete">Remove</a></p>'.
'</td>'.
'</tr>';
}
}
}
if (!$default_shown) {
$html .='<tr class="geo-redirect-option active">'.
'<td>'.
'<input type="hidden" name="country_ids[]" value="-1">'.
'<b>Not assigned countries</b></p>'.
'</td>'.
'<td>'.
'<p><input id="geo-redirect-country--1" class="small-text" name="lang_codes[]" type="text" maxlength="2"></p>'.
'</td>'.
'<td>'.
'<p><input class="regular-text" name="domains[]" type="text"></p>'.
'</td>'.
'<td>'.
'<p><input class="regular-text" name="urls[]" type="text"></p>'.
'</td>'.
'<td>'.
'</td>'.
'</tr>';
}
$html .= '</tbody>
</table>
</div>';
$html .= '<br clear="all" />';
$html .= '<table class="wp-list-table widefat plugins" cellspacing="0">
<thead>
<tr>
<th scope="col" id="name" class="manage-column column-name" style="">Language URL variable</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<p><input class="small-text" name="lang_slug" value="'.$lang_slug.'" type="text">'.
' (example: '.get_bloginfo('url').'/?page_id=10&<b>lang</b>=en)</p>
</td>
</tr>
</tbody>
</table>';
$html .= '<br clear="all" />';
$checked = ($only_outsite == 1)?'checked="checked"':'';
$html .= '<label><input type="checkbox" name="only_outsite" value="1" '.$checked.'> Redirect only visitors who came from another site by link</label>';
$html .= ' <p class="submit">
<input type="submit" name="submit" class="button-primary" value="Save Changes">
</p>';
$html .= wp_nonce_field('submit_geo_redirect_x', 'geo_redirect_nonce_y', true, false);
$html .= '</form></div>';
echo $html;
geo_redirect_javascript();
}
function geo_redirect_javascript(){
?>
<script type="text/javascript">
var j = jQuery;
function geoAddCountry(){
var exist = false;
var country_id = j('select.countries').val();
country_id = parseInt(country_id, 10);
if (country_id == 0 || isNaN(country_id))
return false;
j('.geo-redirect-options input[name="country_ids[]"]').each(function(index) {
if (j(this).val() == country_id) {
exist = true;
j('#geo-redirect-country-'+country_id).focus();
}
});
if (exist === true)
return false;
var country_name = j('select.countries option:selected').text();
var option_html = '<tr class="geo-redirect-option inactive">'+
'<td>'+
'<input type="hidden" name="country_ids[]" value="'+country_id+'">'+
'<p><b>'+country_name+'</b></p>'+
'</td>'+
'<td>'+
'<p><input id="geo-redirect-country-'+country_id+'" class="small-text" name="lang_codes[]" type="text" maxlength="2"></p>'+
'</td>'+
'<td>'+
'<p><input class="regular-text" name="domains[]" type="text"></p>'+
'</td>'+
'<td>'+
'<p><input class="regular-text" name="urls[]" type="text"></p>'+
'</td>'+
'<td>'+
'<p style="line-height:2.3"><a onclick="return geoRemoveCountry(this);" href="#" class="delete">Remove</a></p>'+
'</td>'+
'</tr>';
j('.geo-redirect-options table tbody').prepend(option_html);
setTimeout(function() {
var first = j('.geo-redirect-option').first();
first.addClass('active');
first.removeClass('inactive');
},500);
return false;
}
function geoRemoveCountry(option){
j(option).parents('.geo-redirect-option').addClass('inactive');
setTimeout(function() {
j(option).parents('.geo-redirect-option').remove();
},500);
return false;
}
</script>
<?php
}
function geo_redirect_save(){
$country_ids = $_POST['country_ids'];
$lang_codes = $_POST['lang_codes'];
$domains = $_POST['domains'];
$urls = $_POST['urls'];
$only_outsite = intval($_POST['only_outsite']);
$lang_slug = (trim($_POST['lang_slug']) != '') ? (string)urlencode(strtolower(trim($_POST['lang_slug']))) : 'lang';
if (is_array($country_ids)) {
$redirect = array();
foreach ($country_ids as $key => $country_id) {
$redirect[] = array('country_id' => intval($country_id),
'lang_code' => (string)htmlspecialchars(strtolower(trim(strip_tags($lang_codes[$key])))),
'domain' => (string)htmlspecialchars(strtolower(trim(str_ireplace('http://','',strip_tags($domains[$key]))))),
'url' => (string)htmlspecialchars(trim(strip_tags($urls[$key]))) );
}
} else {
$redirect = '';
}
$data = array( 'redirect' => $redirect,
'only_outsite' => $only_outsite,
'lang_slug' => $lang_slug );
update_option( 'geo_redirect_data', $data);
}
add_action( 'admin_menu', 'geo_redirect_admin_page' );
?>