Plugin Directory

Changeset 1051524


Ignore:
Timestamp:
12/22/2014 01:01:12 PM (11 years ago)
Author:
elialgranti
Message:

Updated to 1.1.0 see https://github.com/elialgranti/geoswitch for full history

Location:
geoswitch/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • geoswitch/trunk/class.geoswitch.php

    r1041126 r1051524  
    22if ( ! defined( 'ABSPATH' ) )
    33        die( 'This is just a Wordpress plugin.' );
    4  
     4
    55if ( ! defined( 'GEOSWITCH_PLUGIN_DIR' ) )
    66    define( 'GEOSWITCH_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
     
    1111    private static $user_ip = null;
    1212    private static $record = null;
     13    private static $data_source = null;
    1314    private static $useKm = true;
    14    
     15
    1516    public static function init() {
    1617        if (self::$initialized) {
     
    1819        }
    1920        self::$initialized = true;
    20        
     21
    2122        self::$user_ip = self::get_user_ip();
    2223
    2324        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);
    3030        } catch (Exception $e) {
    3131            self::$record = null;
     
    3434        add_shortcode('geoswitch', array( 'GeoSwitch', 'switch_block' ));
    3535        add_shortcode('geoswitch_case', array( 'GeoSwitch', 'switch_case' ));
    36        
     36
    3737        add_shortcode('geoswitch_ip', array( 'GeoSwitch', 'get_ip' ));
    3838        add_shortcode('geoswitch_city', array( 'GeoSwitch', 'get_city' ));
     
    4545    }
    4646
     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
    4760    public static function switch_block($atts, $content) {
    4861        $str = do_shortcode($content);
    4962        $arr = explode('#', $str, 3);
    50        
    51         return count($arr) == 3 
     63
     64        return count($arr) == 3
    5265            ? substr($arr[2], 0, intval($arr[1]))
    5366            : '';
     
    5669    public static function switch_case($atts, $content) {
    5770        $expandedContent = do_shortcode($content);
    58        
     71
    5972        if (is_null(self::$record)) {
    6073            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']) ||
    6477                !empty($atts['country_code']) ||
    6578                !empty($atts['within']) ||
     
    6982            return '#'.strlen($expandedContent).'#'.$expandedContent;
    7083        }
    71        
    72        
     84
     85
    7386        if ((empty($atts['city']) || strcasecmp($atts['city'], self::$record->city->name) == 0)
    7487            &&
     
    7992            (empty($atts['country']) || strcasecmp($atts['country'], self::$record->country->name) == 0)
    8093            &&
    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)
    8295            &&
    8396            (empty($atts['within']) || self::within($atts['within'], $atts['from']))) {
     
    111124        return self::$record->mostSpecificSubdivision->isoCode;
    112125    }
    113    
     126
    114127    public static function get_country($atts, $content) {
    115128        if (is_null(self::$record)) {
     
    144157        $default_options=array(
    145158            'database_name'=>'GeoLite2-City.mmdb',
    146             'units'=>'km'
     159            'units'=>'km',
     160            'data_source'=>'localdb',
     161            'service_user_name'=>'',
     162            'service_license_key'=>''
    147163         );
    148164        add_option('geoswitch_options',$default_options);
    149165    }
    150    
     166
    151167    public static function deactivation() {
    152168        unregister_setting('geoswitch_options', 'geoswitch_options');
    153169        delete_option('geoswitch_options');
    154170    }
    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
    156187    private static function within($within, $from) {
    157188        $within = 0.0 + $within;
    158189        $from = explode(',', $from, 2);
    159        
     190
    160191        $pi80 = M_PI / 180;
    161192        $lat1 = ($from[0] + 0.0) * $pi80;
     
    163194        $lat2 = self::$record->location->latitude * $pi80;
    164195        $lng2 = self::$record->location->longitude * $pi80;
    165  
     196
    166197        $r = 6372.797; // mean radius of Earth in km
    167198        $dlat = $lat2 - $lat1;
     
    170201        $c = 2 * atan2(sqrt($a), sqrt(1 - $a));
    171202        $km = $r * $c;
    172        
     203
    173204        return self::$useKm ? ($km <= $within) : (($km * 0.621371192) <= $within);
    174205    }
    175    
     206
    176207    private static function get_user_ip() {
    177208        if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) {
  • geoswitch/trunk/class.geoswitch_admin.php

    r1041126 r1051524  
    88class GeoSwitchAdmin {
    99    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() {
    1412        if (self::$initialized) {
    1513            return;
     
    2220    public static function admin_init() {   
    2321        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');
    2731    }
    2832
     
    4145<?php
    4246    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');
    4451?>
    4552<input name="Submit" type="submit" value="<?php esc_attr_e('Save Changes'); ?>" />
     
    4855    }
    4956
    50     public static function main_section_text() {
     57    public static function data_source_section_text() {
    5158    }
    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    }
    5379    public static function database_name() {
    54         $options = get_option('geoswitch_options');
     80        $options =  GeoSwitch::get_options();
     81       
    5582?>
    5683<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
    57103<?php
    58104    }
    59105
    60106    public static function units() {
    61         $options = get_option('geoswitch_options');
     107        $options =  GeoSwitch::get_options();
    62108?>
    63109<select id='geoswitch_units' name='geoswitch_options[units]'>
     
    78124            $newinput['database_name'] = 'GeoLite2-City.mmdb';
    79125        }
    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
    81132        if (isset($input['units'])) {
    82133            $newinput['units'] = ($input['units'] == 'm' ? 'm' : 'km');
    83            
     134
    84135        } else {
    85136            $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']);
    86143        }
    87144        return $newinput;
  • geoswitch/trunk/readme.txt

    r1041451 r1051524  
    33Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=C7QAD2M3L5T6E
    44Tags: geocode, geocode switch, geocode filter, geotag, geomarketing, geomarking, geolocation, geofilter, location, local marketing, GeoIP2, MaxMind
    5 Version: 1.0.0
     5Version: 1.1.0
    66Requires at least: 3.0
    7 Tested up to: 4.0.1
     7Tested up to: 4.1.0
    88Stable tag: 1.0.0
    99License: GPLv2 or later for plugin code, Apache License version 2.0 for Maxmind library under vendor directory
     
    1414== Description ==
    1515GeoSwitch 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.
     16To geolocate users based on IP GeoSwitch supports can user either the new the new GeoIP2 [MaxMind](https://www.maxmind.com)
     17databases or GeoIP2 Precision Service.
     18MaxMind offers free and paid geolocation databases and the paid GeoIP2 Precision web service,
     19the author of this plugin is not affiliated with MaxMind in any way.
    1820
    1921The main development of this plugin is in [github](https://github.com/elialgranti/geoswitch).
     
    2527== Installation ==
    2628= Prerequisites =
    27 The Geoswitch plugin uses MaxMind’s city database you’ll need either the free GeoLite2 city database
     29The 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
    2832(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.
     33for 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).
    3135
     36= Installation =
    32371. 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).
     382. Optionally copy your MaxMind binary database to the database subdirectory inside the plugin root direcory (GeoSwitch/database).
    3439   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).
     403. 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.*
    3746
    3847== Usage ==
     
    102111= 1.0.0 =
    103112* Initial release
     113= 1.1.0 =
     114* Added support for MaxMind GeoIP2 Precision Service (thanks to [Paul Scarrone](https://github.com/ninjapanzer)
     115and [carlcapozza](https://github.com/carlcapozza)).
     116* Fixed bug with measurement units. Units were always considered kilometers.
     117* Tested under Wordpress 4.1.
    104118
    105119== Upgrade Notice ==
     120= 1.1.0 =
     121Added support for MaxMind GeoIP2 Precision Service and fixed bug with measurement units.
    106122
    107123== Frequently Asked Questions ==
Note: See TracChangeset for help on using the changeset viewer.