Changeset 1051524
- Timestamp:
- 12/22/2014 01:01:12 PM (11 years ago)
- Location:
- geoswitch/trunk
- Files:
-
- 3 edited
-
class.geoswitch.php (modified) (12 diffs)
-
class.geoswitch_admin.php (modified) (5 diffs)
-
readme.txt (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
geoswitch/trunk/class.geoswitch.php
r1041126 r1051524 2 2 if ( ! defined( 'ABSPATH' ) ) 3 3 die( 'This is just a Wordpress plugin.' ); 4 4 5 5 if ( ! defined( 'GEOSWITCH_PLUGIN_DIR' ) ) 6 6 define( 'GEOSWITCH_PLUGIN_DIR', plugin_dir_path( __FILE__ ) ); … … 11 11 private static $user_ip = null; 12 12 private static $record = null; 13 private static $data_source = null; 13 14 private static $useKm = true; 14 15 15 16 public static function init() { 16 17 if (self::$initialized) { … … 18 19 } 19 20 self::$initialized = true; 20 21 21 22 self::$user_ip = self::get_user_ip(); 22 23 23 24 try { 24 $opt = get_option('geoswitch_options'); 25 $useKM = ($opt['units'] == 'km'); 26 $database = GEOSWITCH_PLUGIN_DIR . 'database/' . $opt['database_name']; 27 $reader = new GeoIp2\Database\Reader($database); 28 29 self::$record = $reader->city(self::$user_ip); 25 $opt = self::get_options(); 26 27 self::$useKm = ($opt['units'] == 'km'); 28 self::$data_source = self::request_record($opt); 29 self::$record = self::$data_source->city(self::$user_ip); 30 30 } catch (Exception $e) { 31 31 self::$record = null; … … 34 34 add_shortcode('geoswitch', array( 'GeoSwitch', 'switch_block' )); 35 35 add_shortcode('geoswitch_case', array( 'GeoSwitch', 'switch_case' )); 36 36 37 37 add_shortcode('geoswitch_ip', array( 'GeoSwitch', 'get_ip' )); 38 38 add_shortcode('geoswitch_city', array( 'GeoSwitch', 'get_city' )); … … 45 45 } 46 46 47 public static function request_record($opts) { 48 return ($opts['data_source'] == 'localdb') ? self::build_reader($opts) : self::build_client($opts); 49 } 50 51 public static function build_client($opts) { 52 return new GeoIp2\WebService\Client($opts['service_user_name'], $opts['service_license_key']); 53 } 54 55 public static function build_reader($opts){ 56 $database = GEOSWITCH_PLUGIN_DIR . 'database/' . $opts['database_name']; 57 return new GeoIp2\Database\Reader($database); 58 } 59 47 60 public static function switch_block($atts, $content) { 48 61 $str = do_shortcode($content); 49 62 $arr = explode('#', $str, 3); 50 51 return count($arr) == 3 63 64 return count($arr) == 3 52 65 ? substr($arr[2], 0, intval($arr[1])) 53 66 : ''; … … 56 69 public static function switch_case($atts, $content) { 57 70 $expandedContent = do_shortcode($content); 58 71 59 72 if (is_null(self::$record)) { 60 73 if (!empty($atts['city']) || 61 !empty($atts['state']) || 62 !empty($atts['state_code']) || 63 !empty($atts['country']) || 74 !empty($atts['state']) || 75 !empty($atts['state_code']) || 76 !empty($atts['country']) || 64 77 !empty($atts['country_code']) || 65 78 !empty($atts['within']) || … … 69 82 return '#'.strlen($expandedContent).'#'.$expandedContent; 70 83 } 71 72 84 85 73 86 if ((empty($atts['city']) || strcasecmp($atts['city'], self::$record->city->name) == 0) 74 87 && … … 79 92 (empty($atts['country']) || strcasecmp($atts['country'], self::$record->country->name) == 0) 80 93 && 81 (empty($atts['country_code']) || strcasecmp($atts['country_code'], self::$record->country->isoCode) == 0) 94 (empty($atts['country_code']) || strcasecmp($atts['country_code'], self::$record->country->isoCode) == 0) 82 95 && 83 96 (empty($atts['within']) || self::within($atts['within'], $atts['from']))) { … … 111 124 return self::$record->mostSpecificSubdivision->isoCode; 112 125 } 113 126 114 127 public static function get_country($atts, $content) { 115 128 if (is_null(self::$record)) { … … 144 157 $default_options=array( 145 158 'database_name'=>'GeoLite2-City.mmdb', 146 'units'=>'km' 159 'units'=>'km', 160 'data_source'=>'localdb', 161 'service_user_name'=>'', 162 'service_license_key'=>'' 147 163 ); 148 164 add_option('geoswitch_options',$default_options); 149 165 } 150 166 151 167 public static function deactivation() { 152 168 unregister_setting('geoswitch_options', 'geoswitch_options'); 153 169 delete_option('geoswitch_options'); 154 170 } 155 171 172 public static function get_options() { 173 $opt = get_option('geoswitch_options'); 174 if (!array_key_exists('database_name', $opt)) 175 $opt['database_name']='GeoLite2-City.mmdb'; 176 if (!array_key_exists('units', $opt)) 177 $opt['units']='km'; 178 if (!array_key_exists('data_source', $opt)) 179 $opt['data_source']='localdb'; 180 if (!array_key_exists('service_user_name', $opt)) 181 $opt['service_user_name']=''; 182 if (!array_key_exists('service_license_key', $opt)) 183 $opt['service_license_key']=''; 184 return $opt; 185 } 186 156 187 private static function within($within, $from) { 157 188 $within = 0.0 + $within; 158 189 $from = explode(',', $from, 2); 159 190 160 191 $pi80 = M_PI / 180; 161 192 $lat1 = ($from[0] + 0.0) * $pi80; … … 163 194 $lat2 = self::$record->location->latitude * $pi80; 164 195 $lng2 = self::$record->location->longitude * $pi80; 165 196 166 197 $r = 6372.797; // mean radius of Earth in km 167 198 $dlat = $lat2 - $lat1; … … 170 201 $c = 2 * atan2(sqrt($a), sqrt(1 - $a)); 171 202 $km = $r * $c; 172 203 173 204 return self::$useKm ? ($km <= $within) : (($km * 0.621371192) <= $within); 174 205 } 175 206 176 207 private static function get_user_ip() { 177 208 if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) { -
geoswitch/trunk/class.geoswitch_admin.php
r1041126 r1051524 8 8 class GeoSwitchAdmin { 9 9 private static $initialized = false; 10 private static $user_ip = null; 11 private static $record = null; 12 13 public static function init() { 10 11 public static function init() { 14 12 if (self::$initialized) { 15 13 return; … … 22 20 public static function admin_init() { 23 21 register_setting( 'geoswitch_options', 'geoswitch_options', array('GeoSwitchAdmin', 'validate') ); 24 add_settings_section('geoswitch_main', 'General Settings', array('GeoSwitchAdmin', 'main_section_text'), 'geoswitch_options_main_page'); 25 add_settings_field('geoswitch_database_name', 'MaxMind Database Name', array('GeoSwitchAdmin', 'database_name'), 'geoswitch_options_main_page', 'geoswitch_main'); 26 add_settings_field('geoswitch_units', 'Distance Units', array('GeoSwitchAdmin', 'units'), 'geoswitch_options_main_page', 'geoswitch_main'); 22 add_settings_section('geoswitch_main', 'Geolocation Service Settings', array('GeoSwitchAdmin', 'data_source_section_text'), 'geoswitch_options_data_source_page'); 23 add_settings_field('geoswitch_data_source', 'Select Geolocation Service', array('GeoSwitchAdmin', 'data_source'), 'geoswitch_options_data_source_page', 'geoswitch_main'); 24 add_settings_section('geoswitch_main', 'Local DataBase Settings', array('GeoSwitchAdmin', 'localdb_section_text'), 'geoswitch_options_localdb_page'); 25 add_settings_field('geoswitch_database_name', 'MaxMind Database Name', array('GeoSwitchAdmin', 'database_name'), 'geoswitch_options_localdb_page', 'geoswitch_main'); 26 add_settings_section('geoswitch_main', 'Web Service Settings', array('GeoSwitchAdmin', 'webservice_section_text'), 'geoswitch_options_webservice_page'); 27 add_settings_field('geoswitch_service_user_name', 'User ID', array('GeoSwitchAdmin', 'service_user_name'), 'geoswitch_options_webservice_page', 'geoswitch_main'); 28 add_settings_field('geoswitch_service_license_key', 'License key', array('GeoSwitchAdmin', 'service_license_key'), 'geoswitch_options_webservice_page', 'geoswitch_main'); 29 add_settings_section('geoswitch_main', 'Measurement Settings', array('GeoSwitchAdmin', 'measurement_section_text'), 'geoswitch_options_measurement_page'); 30 add_settings_field('geoswitch_units', 'Distance Units', array('GeoSwitchAdmin', 'units'), 'geoswitch_options_measurement_page', 'geoswitch_main'); 27 31 } 28 32 … … 41 45 <?php 42 46 settings_fields('geoswitch_options'); 43 do_settings_sections('geoswitch_options_main_page'); 47 do_settings_sections('geoswitch_options_data_source_page'); 48 do_settings_sections('geoswitch_options_localdb_page'); 49 do_settings_sections('geoswitch_options_webservice_page'); 50 do_settings_sections('geoswitch_options_measurement_page'); 44 51 ?> 45 52 <input name="Submit" type="submit" value="<?php esc_attr_e('Save Changes'); ?>" /> … … 48 55 } 49 56 50 public static function main_section_text() {57 public static function data_source_section_text() { 51 58 } 52 59 60 public static function localdb_section_text() { 61 } 62 63 public static function webservice_section_text() { 64 } 65 66 public static function measurement_section_text() { 67 } 68 69 public static function data_source() { 70 $options = GeoSwitch::get_options(); 71 ?> 72 <select id='geoswitch_data_source' name='geoswitch_options[data_source]'> 73 <option value="localdb" <?=selected($options['data_source'], 'localdb', false)?>>Local Database</option> 74 <option value="webservice" <?=selected($options['data_source'], 'webservice', false)?>>GeoIP2 Precision Service</option> 75 </select> 76 77 <?php 78 } 53 79 public static function database_name() { 54 $options = get_option('geoswitch_options'); 80 $options = GeoSwitch::get_options(); 81 55 82 ?> 56 83 <input id='geoswitch_database_name' name='geoswitch_options[database_name]' size='64' type='text' value='<?= $options['database_name']?>' /> 84 85 <?php 86 } 87 88 public static function service_user_name() { 89 $options = GeoSwitch::get_options(); 90 ?> 91 <input id='geoswitch_service_user_name', name='geoswitch_options[service_user_name]' size='64' type='text' value='<?= $options['service_user_name']?>' /> 92 93 94 <?php 95 } 96 97 public static function service_license_key() { 98 $options = GeoSwitch::get_options(); 99 ?> 100 <input id='geoswitch_service_license_key', name='geoswitch_options[service_license_key]' size='64' type='text' value='<?= $options['service_license_key']?>' /> 101 102 57 103 <?php 58 104 } 59 105 60 106 public static function units() { 61 $options = get_option('geoswitch_options');107 $options = GeoSwitch::get_options(); 62 108 ?> 63 109 <select id='geoswitch_units' name='geoswitch_options[units]'> … … 78 124 $newinput['database_name'] = 'GeoLite2-City.mmdb'; 79 125 } 80 126 if (isset($input['data_source'])) { 127 $newinput['data_source'] = ($input['data_source'] == 'localdb' ? 'localdb' : 'webservice'); 128 } else { 129 $newinput['units'] = 'localdb'; 130 } 131 81 132 if (isset($input['units'])) { 82 133 $newinput['units'] = ($input['units'] == 'm' ? 'm' : 'km'); 83 134 84 135 } else { 85 136 $newinput['units'] = 'km'; 137 } 138 if (isset($input['service_user_name'])){ 139 $newinput['service_user_name'] = trim($input['service_user_name']); 140 } 141 if (isset($input['service_license_key'])){ 142 $newinput['service_license_key'] = trim($input['service_license_key']); 86 143 } 87 144 return $newinput; -
geoswitch/trunk/readme.txt
r1041451 r1051524 3 3 Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=C7QAD2M3L5T6E 4 4 Tags: geocode, geocode switch, geocode filter, geotag, geomarketing, geomarking, geolocation, geofilter, location, local marketing, GeoIP2, MaxMind 5 Version: 1. 0.05 Version: 1.1.0 6 6 Requires at least: 3.0 7 Tested up to: 4. 0.17 Tested up to: 4.1.0 8 8 Stable tag: 1.0.0 9 9 License: GPLv2 or later for plugin code, Apache License version 2.0 for Maxmind library under vendor directory … … 14 14 == Description == 15 15 GeoSwitch is a plugin that allows you to change the content of your site based on the location of your client’s IP. 16 GeoSwitch uses the new the new GeoIP2 [MaxMind](https://www.maxmind.com) databases to geolocate users based on IP. 17 MaxMind offers free and paid geolocation databases, the author of this plugin is not affiliated with MaxMind in any way. 16 To geolocate users based on IP GeoSwitch supports can user either the new the new GeoIP2 [MaxMind](https://www.maxmind.com) 17 databases or GeoIP2 Precision Service. 18 MaxMind offers free and paid geolocation databases and the paid GeoIP2 Precision web service, 19 the author of this plugin is not affiliated with MaxMind in any way. 18 20 19 21 The main development of this plugin is in [github](https://github.com/elialgranti/geoswitch). … … 25 27 == Installation == 26 28 = Prerequisites = 27 The Geoswitch plugin uses MaxMind’s city database you’ll need either the free GeoLite2 city database 29 The Geoswitch plugin uses either MaxMind’s city database or webservice. 30 31 * To use a local database for geocoding you’ll need either the free GeoLite2 city database 28 32 (download from [here](http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz)) or obtain a license from MaxMind 29 for a GeoIP2 citydatabase. 30 After obtaining the database uncompress it before installation.33 for a GeoIP2 citydatabase. After obtaining the database uncompress it before installation. 34 * To use the webservice you'll need to obtain a user ID and license key for [GeoIP2 Precision Services](https://www.maxmind.com/en/geoip2-precision-services). 31 35 36 = Installation = 32 37 1. Copy the Plugin directory to your wordpress plugins directory (usually wp-content/plugins) 33 2. Copy your MaxMind binary database to the database subdirectory inside the plugin root direcory (GeoSwitch/database).38 2. Optionally copy your MaxMind binary database to the database subdirectory inside the plugin root direcory (GeoSwitch/database). 34 39 The database should be uncompressed. 35 3. In the Wordpress administration settings search for the GeoSwitch configuration page and set the name of the database 36 and the units to use for distance calculations (kilometer or miles). 40 3. In the Wordpress administration settings search for the GeoSwitch configuration page: 41 * Select the type of geocoding service to use (local database or webservice). 42 * Enter the name of the database or the user ID and license key depending on the service you've selected. 43 * Set the units to use for distance calculations (kilometer or miles). 44 45 *Note: if you use the local database you should update it periodically.* 37 46 38 47 == Usage == … … 102 111 = 1.0.0 = 103 112 * Initial release 113 = 1.1.0 = 114 * Added support for MaxMind GeoIP2 Precision Service (thanks to [Paul Scarrone](https://github.com/ninjapanzer) 115 and [carlcapozza](https://github.com/carlcapozza)). 116 * Fixed bug with measurement units. Units were always considered kilometers. 117 * Tested under Wordpress 4.1. 104 118 105 119 == Upgrade Notice == 120 = 1.1.0 = 121 Added support for MaxMind GeoIP2 Precision Service and fixed bug with measurement units. 106 122 107 123 == Frequently Asked Questions ==
Note: See TracChangeset
for help on using the changeset viewer.