Skip to content

Instantly share code, notes, and snippets.

@timersys
Last active January 26, 2016 15:03
Show Gist options
  • Select an option

  • Save timersys/9fc2249c876c3fef69ad to your computer and use it in GitHub Desktop.

Select an option

Save timersys/9fc2249c876c3fef69ad to your computer and use it in GitHub Desktop.
Maxmind shortcodes Plugin
<?php
/*
Plugin Name: Maxmind Shortcodes
Description: Using Maxmind API to display user's country with a simple shortcode
Version: 1.0.0
Author: Damian Logghe
Author URI: https://timersys.com
License: GPL2
License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/
// import reader class
use GeoIp2\Database\Reader;
// Prevent direct access to this file
defined( 'ABSPATH' ) or die( 'no direct access!' );
// Include composer files
require 'vendor/autoload.php';
/**
* SHORTCODES
*/
/**
* [country-name]
* Display the current user country. So we can say something like:
* Hello people from [country-name] !!
* @param $atts Array of attributtes, not used on this shortcode
*
* @return string
*/
function maxmind_country_name( $atts ) {
// retrieve user country
$country = maxmind_get_country();
// If no record we print a default word
if( ! $country )
return 'Everywhere';
// otherwise print country name
return $country->country->name;
}
add_shortcode( 'country-name', 'maxmind_country_name');
/**
* Depending on user's country we show or hide content
* Usage: [target-users country="US,FR"] Content here [/target-users]
* @param $atts
* @param $content
*
* @return string
*/
function target_users( $atts, $content ) {
$countries = array();
if( !empty( $atts['country'] ) )
$countries = explode( ',', $atts['country'] );
if( targetCountry( $countries ) ) {
return $content;
}
return 'The content is restricted in your region';
}
add_shortcode( 'target-users', 'target_users');
/**
* Modified version of Geotargeting Pro target functions for demo purposes
* https://timersys.com/plugins/geotargeting-pro/
* @param array $country
*
* @return bool
*/
function targetCountry( $country = array() ) {
//set target to false
$target = false;
$user_country = maxmind_get_country();
if ( count( $country ) > 0 ) {
foreach ( $country as $c ) {
if ( strtolower( $user_country->country->name ) == strtolower( $c )|| strtolower( $user_country->country->isoCode ) == strtolower( $c ) ) {
$target = true;
}
}
} else {
// If we don't have countries to target return true
$target = true;
}
return $target;
}
/**
* Main Function that retrieve user country.
* You probably want to add this to user session
* or some sort of cache to avoid calculation on each page load
* @return bool|\GeoIp2\Model\Country
*/
function maxmind_get_country() {
// If you use CDN services such as Cloudflare, Reblaze, Akamai, etc
// you will need to get real user IP using their server headers such as $_SERVER['HTTP_CF_CONNECTING_IP']
// 181.30.241.162 is google IP for testing purposes
$ip = isset( $_SERVER['REMOTE_ADDR'] ) ? $_SERVER['REMOTE_ADDR'] : '181.30.241.162';
try {
$reader = new Reader( plugin_dir_path( __FILE__ ) . 'data/GeoLite2-Country.mmdb' );
$country = $reader->country( $ip );
} catch ( Exception $e ) {
/* There are a bunch of handfull exceptions classes depending on which services you are using
* such as GeoIp2\Exception\OutOfQueriesException, GeoIp2\Exception\AuthenticationException ,etc
* Send error to logs or print them out
*/
// error_log( $e->getMessage() );
return false;
}
return $country;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment