=' ) ) {
wp_enqueue_style('annotation-by-country', plugins_url('',__FILE__).'/css/annotation-by-country-options.css', array(), self::VERSION);
}
}
/**
* 設定ページの初期化を行います。
*/
public function page_init()
{
register_setting('settings_of_annotation_by_country',
'_settings_of_annotation_by_country',
array($this,'sanitize'));
// Section
add_settings_section(self::SETTINGS_SECTION_ID,
'Settings',
array($this,'print_section_info'),
self::SETTINGS_PAGE);
// GeoLite2のデータベースのありか。
add_settings_field('geolite2_database_path',
'Path to GeoLite2 database',
array($this,'setup_geolite2_database_path'),
self::SETTINGS_PAGE,
self::SETTINGS_SECTION_ID);
// GeoLite2-phpのありか。
add_settings_field('abc_geolite2_php_path',
'Path to GeoIP2-php library',
array($this,'setup_geolite2_php_path'),
self::SETTINGS_PAGE,
self::SETTINGS_SECTION_ID);
// デフォルトの国
add_settings_field('abc_country_code_as_default_value',
'Default country code (in 2 letters)',
array($this,'setup_country_code'),
self::SETTINGS_PAGE,
self::SETTINGS_SECTION_ID);
// 短縮タグ名を使うか否か。
add_settings_field('abc_tagname_in_short_form',
'Use "abc" as the tag name in short form?',
array($this,'setup_tagname_in_short_form'),
self::SETTINGS_PAGE,
self::SETTINGS_SECTION_ID);
}
public function create_admin_page()
{
if (!current_user_can('manage_options')) {
wp_die(__('You do not have sufficient permissions to access this page.'));
}
$this->options = get_option('_settings_of_annotation_by_country');
?>
Annotation By Country
options = get_option('_settings_of_annotation_by_country');
$new_input = array();
if (isset($input['geolite2_database_path']) &&
trim($input['geolite2_database_path']) !== '') {
$sanitized_path = sanitize_text_field($input['geolite2_database_path']);
if (file_exists($sanitized_path)) {
$new_input['geolite2_database_path'] = $sanitized_path;
} else {
add_settings_error('settings_of_annotation_by_country','geolite2_database_path','Specify the existing path to GeoLite2 database.');
$new_input['geolite2_database_path'] = $this->options['geolite2_database_path'];
}
} else {
add_settings_error('settings_of_annotation_by_country','geolite2_database_path','Specify the path to GeoLite2 database.');
$new_input['geolite2_database_path'] = $this->options['geolite2_database_path'];
}
if (isset($input['geolite2_php_path']) &&
trim($input['geolite2_php_path']) !== '') {
$sanitized_php_path = sanitize_text_field($input['geolite2_php_path']);
if (file_exists($sanitized_php_path."/vendor/autoload.php")) {
$new_input['geolite2_php_path'] = $sanitized_php_path;
} else {
add_settings_error('settings_of_annotation_by_country','geolite2_php_path','Specify the existing path to GeoIP2-php.');
$new_input['geolite2_php_path'] = $this->options['geolite2_php_path'];
}
} else {
add_settings_error('settings_of_annotation_by_country','geolite2_php_path','Specify the path to GeoIP2-php.');
$new_input['geolite2_php_path'] = $this->options['geolite2_php_path'];
}
if (isset($input['geolite2_default_country_code']) &&
trim($input['geolite2_default_country_code']) !== '') {
$sanitized_text = sanitize_text_field($input['geolite2_default_country_code']);
if (preg_match('/^[A-Za-z]{2}$/',$sanitized_text)) {
$new_input['geolite2_default_country_code'] = strtoupper($sanitized_text);
} else {
add_settings_error('settings_of_annotation_by_country','geolite2_default_country_code_should_be_2_letters','Specify the default country code which should be specified in 2 letters.');
$new_input['geolite2_default_country_code'] = $this->options['geolite2_default_country_code'];
}
} else {
add_settings_error('settings_of_annotation_by_country','geolite2_default_country_code','Specify the default country code.');
$new_input['geolite2_default_country_code'] = $this->options['geolite2_default_country_code'];
}
$new_input['geolite2_tagname_in_short_form'] = intval($input['geolite2_tagname_in_short_form']);
return $new_input;
}
function print_section_info() {
return '';
}
function setup_geolite2_database_path() {
printf('',
isset($this->options['geolite2_database_path'])?esc_attr($this->options['geolite2_database_path']):self::DEFAULT_GEOLITE2_COUNTRY_DATABASE_PATH);
}
function setup_geolite2_php_path() {
printf('',
isset($this->options['geolite2_php_path'])?esc_attr($this->options['geolite2_php_path']):"/usr/share/php/GeoIP2-php");
}
function setup_country_code() {
printf('',
isset($this->options['geolite2_default_country_code'])?esc_attr($this->options['geolite2_default_country_code']):self::DEFAULT_COUNTRY_CODE);
}
function setup_tagname_in_short_form() {
printf('',
(isset($this->options['geolite2_tagname_in_short_form']) &&
$this->options['geolite2_tagname_in_short_form'] == 1)?'checked="checked" ':'');
}
}
if (is_admin()) {
$annotationByCountrySettingsPage = new AnnotationByCountrySettingsPage();
}
$_settings_of_annotation_by_country = get_option('_settings_of_annotation_by_country','');
if ($_settings_of_annotation_by_country !== '' &&
is_array($_settings_of_annotation_by_country) &&
array_key_exists('geolite2_php_path',$_settings_of_annotation_by_country) &&
file_exists($_settings_of_annotation_by_country['geolite2_php_path']."/vendor/autoload.php")) {
require_once($_settings_of_annotation_by_country['geolite2_php_path']."/vendor/autoload.php");
function get_country_isocode_() {
global $_settings_of_annotation_by_country;
global $_country_isocode;
if (empty($_country_isocode)) {
$reader = new GeoIp2\Database\Reader($_settings_of_annotation_by_country['geolite2_database_path']);
$record = $reader->country($_SERVER["REMOTE_ADDR"]);
$_country_isocode = $record->country->isoCode;
}
return $_country_isocode;
}
function annotation_by_country($atts, $content = "") {
global $_settings_of_annotation_by_country;
$atts = shortcode_atts(array(
'for' => $_settings_of_annotation_by_country['geolite2_default_country_code'],
'except' => ''), $atts, 'annotation-by-country');
if ($atts['except'] !== '') {
$country_list = explode(',',strtoupper($atts['except']));
if (in_array(get_country_isocode_(),$country_list,true)) {
return '';
} else {
return $content;
}
}
if ($atts['for'] !== '') {
$country_list = explode(',',strtoupper($atts['for']));
if (in_array(get_country_isocode_(),$country_list,true)) {
return $content;
} else {
return '';
}
}
return '';
}
add_shortcode('annotation-by-country','annotation_by_country');
if ($_settings_of_annotation_by_country['geolite2_tagname_in_short_form'] === 1) {
add_shortcode('abc','annotation_by_country');
}
}
?>