<?php
/*
Plugin Name: IP2Phrase
Plugin URI: http://www.ip2phrase.com
Description:
Version: 1.0
Author: Sei Kan
Author URI: http://www.ip2phrase.com
*/
add_action('widgets_init', array('IP2Phrase', 'register'));
add_action('admin_menu', array('IP2Phrase', 'menu'));
add_action('admin_head', array('IP2Phrase', 'farbtastic') );
class IP2Phrase {
function activate(){
if(!function_exists('register_sidebar_widget')) return;
$options = array('title'=>'IP2Phrase');
if(!get_option('IP2Phrase')){
add_option('IP2Phrase' , $options);
}
else{
update_option('IP2Phrase' , $options);
}
}
function deactivate(){
delete_option('IP2Phrase');
}
function control(){
echo '<a href="options-general.php?page=' . basename(__FILE__) . '">Go to Settings</a>';
}
function widget($args){
$options = get_option('IP2Phrase');
$text = str_replace(array('%lt%', '%gt%'), array('<', '>'), $options['text']);
echo $args['before_widget'] . $args['before_title'] . $options['title'] . $args['after_title'];
echo '<style>p#ip2phrase a{font-size:' . $options['fontSize'] .'px;color:' . $options['fontColor'] . ';}</style>
<p id="ip2phrase"><script language="javascript" src="http://www.ip2phrase.com/ip2phrase.asp?template=' . str_replace('"', '"', nl2br($text)) . '"></script></p><hr style="margin-bottom:0;" /><p style="font-size:10px;">Powered by <a href="http://www.ip2phrase.com" target="_blank">IP2Phrase.com</a></p>';
echo $args['after_widget'];
}
function menu() {
add_submenu_page( 'options-general.php', 'IP2Phrase', 'IP2Phrase', 'administrator', basename(__FILE__), array('IP2Phrase', 'setting') );
}
function setting(){
$options = get_option('IP2Phrase');
$fontSize = array(9, 10, 11, 12, 13, 14, 15, 16, 17, 18);
if($_POST['ip2phrase-title']){
if(!preg_match('/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/', $_POST['ip2phrase-font-color'])) $_POST['ip2phrase-font-color'] = $options['borderColor'];
if(!in_array($_POST['ip2phrase-font-size'], $fontSize)) $_POST['ip2phrase-font-size'] = 10;
$data['title'] = strip_tags(stripslashes($_POST['ip2phrase-title']));
$data['text'] = strip_tags(stripslashes(str_replace(array('<', '>'), array('%lt%', '%gt%'), $_POST['ip2phrase-text'])));
$data['fontColor'] = strip_tags(stripslashes($_POST['ip2phrase-font-color']));
$data['fontSize'] = strip_tags(stripslashes($_POST['ip2phrase-font-size']));
update_option('IP2Phrase', $data);
$options = get_option('IP2Phrase');
echo '<div id="setting-error-settings_updated" class="updated settings-error"><p><strong>Settings saved.</strong></p></div> ';
}
if(!is_array($options)) $options = array(
'title'=>'IP2Phrase',
'text'=>'Your IP is %lt%IP%gt%, and you are coming from %lt%COUNTRY%gt%!',
'fontSize'=>'10',
'fontColor'=>'#000000',
);
$text = str_replace(array('%lt%', '%gt%'), array('<', '>'), $options['text']);
$fontSizeOptions = '';
foreach($fontSize as $size){
$fontSizeOptions .= '<option value="' . $size . '"' . (($size == $options['fontSize']) ? ' selected="selected"' : '') . '> ' . $size . 'px</option>';
}
echo '
<div class="wrap">
<div id="icon-options-general" class="icon32"></div>
<h2>IP2Phrase Settings</h2>
<p> </p>
<form id="form-ip2phrase" method="post">
<table>
<tr>
<td>Title</td>
<td><input style="width:400px;" name="ip2phrase-title" type="text" value="' . htmlspecialchars($options['title'], ENT_QUOTES) . '" /></td>
</tr>
<tr>
<td>Text</td>
<td><textarea name="ip2phrase-text" style="width:400px;height:100px;">' . htmlspecialchars($text, ENT_QUOTES) . '</textarea></td>
</tr>
<tr>
<td>Font Size</td>
<td>
<select name="ip2phrase-font-size" style="width:400px;">
' . $fontSizeOptions . '
</select>
</td>
</tr>
<tr>
<td>Font Color</td>
<td><input style="width:400px;" name="ip2phrase-font-color" id="ip2phrase-font-color" type="text" value="' . htmlspecialchars($options['fontColor'], ENT_QUOTES) . '" maxlength="7" class="color-picker" /></td>
</tr>
<tr>
<td></td>
<td><div id="farbtastic-ip2phrase-font-color"></div></td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td> </td>
<td>
<input type="submit" name="submit" class="button-primary" value="Save Changes" />
</td>
</tr>
</table>
</form>
<h4>Available Tags</h4>
<table width="500" cellpadding="0" cellspacing="0" style="border:solid 1px #004884;">
<thead style="font-weight:bold;color:#fff;background:#004884;">
<tr>
<td>Tag</td>
<td>Description</td>
<td>Output</td>
</tr>
</thead>
<tbody>
<tr>
<td><IP></td>
<td>IP address of visitor</td>
<td><script language="javascript" src="http://www.ip2phrase.com/ip2phrase.asp?template=<IP>"></script></td>
</tr>
<tr style="background:#fff;">
<td><COUNTRY></td>
<td>Full country name of visitor</td>
<td><script language="javascript" src="http://www.ip2phrase.com/ip2phrase.asp?template=<COUNTRY>"></script></td>
</tr>
<tr>
<td><COUNTRYSHORT></td>
<td>2-digit country name of visitor</td>
<td><script language="javascript" src="http://www.ip2phrase.com/ip2phrase.asp?template=<COUNTRYSHORT>"></script></td>
</tr>
<tr style="background:#fff;">
<td><REGION></td>
<td>State or region of visitor</td>
<td><script language="javascript" src="http://www.ip2phrase.com/ip2phrase.asp?template=<REGION>"></script></td>
</tr>
<tr>
<td><CITY></td>
<td>City of visitor</td>
<td><script language="javascript" src="http://www.ip2phrase.com/ip2phrase.asp?template=<CITY>"></script></td>
</tr>
<tr style="background:#fff;">
<td><ISP></td>
<td>Internet service provider (ISP) of visitor</td>
<td><script language="javascript" src="http://www.ip2phrase.com/ip2phrase.asp?template=<ISP>"></script></td>
</tr>
<tr>
<td><FLAG></td>
<td>Flag of country</td>
<td><script language="javascript" src="http://www.ip2phrase.com/ip2phrase.asp?template=<FLAG>"></script></td>
</tr>
</tbody>
</table>
</div>
<script type="text/javascript">
jQuery(function(){
jQuery(document).ready(function() {
jQuery(\'.color-picker\').each(function() {
jQuery(\'#farbtastic-\'+this.id).hide();
jQuery(\'#farbtastic-\'+this.id).farbtastic(this);
jQuery(this).click(function(){jQuery(\'#farbtastic-\'+this.id).fadeIn()});
jQuery(this).blur(function(){jQuery(\'#farbtastic-\'+this.id).hide()});
});
});
});
jQuery(document).mousedown(function() {
jQuery(\'.color-picker\').each(function() {
var display = jQuery(\'#\'+this.id).css(\'display\');
if(display == \'block\') jQuery(\'#\'+this.id).fadeOut();
});
});
</script>
';
}
function farbtastic(){
global $current_screen;
if($current_screen->id == 'IP2Phrase.php') {
wp_enqueue_style( 'farbtastic' );
wp_enqueue_script( 'farbtastic' );
}
}
function register(){
register_sidebar_widget('IP2Phrase', array('IP2Phrase', 'widget'));
register_widget_control('IP2Phrase', array('IP2Phrase', 'control'));
}
}
?>