Skip to content

Instantly share code, notes, and snippets.

@vanaf1979
Last active March 6, 2024 09:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vanaf1979/2f3007b5f48dd3ec6cac2a4badae5ec5 to your computer and use it in GitHub Desktop.
Save vanaf1979/2f3007b5f48dd3ec6cac2a4badae5ec5 to your computer and use it in GitHub Desktop.
Wordpress shortcode and tags/functions to deal with cloudflare returned country codes
<?php
/**
* GMIP
*
* @package GMIP
* @author Stephan Nijman
* @copyright 2024 Granmedia
* @license GPL-2.0-or-later
* @version 1.0.0
*
* @wordpress-plugin
* Plugin Name: GMIP
* Plugin URI: https://granmedia.nl
* Description: Shortcode and Tags based on Cloudflare returned country codes.
* Version: 1.0.0
* Requires at least: 6.0
* Requires PHP: 8.0
* Author: Stephan Nijman
* Author URI: https://granmedia.nl
* Text Domain: gmip
* License: GPL v2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/
/**
* Example shortcode usage:
* [gmip lang="EN"]...content for england...[/gmip]
* [gmip lang="US|CA"]...content for usa and canada...[/gmip]
*/
namespace GMIP;
/**
* Check WordPress context.
*/
if (!defined('ABSPATH')) exit;
/**
* GMIP
*
* Shortcode and Tags based on Cloudflare returned country codes.
*
* @package GMIP
*/
final class GMIP
{
/**
* instance.
*
* @var GMIP $instance instance instance.
*/
private static $instance = null;
/**
* $default_lang.
*
* @var string $default_lang Default language
*/
private string $default_lang = 'OTHER';
/**
* $current_lang.
*
* @var string $current_lang Current users language
*/
private string $current_lang = 'OTHER';
/**
* $language_config.
*
* @var string $language_config Combine countries into a single code,
*/
private array $language_config = array(
'US' => array('US', 'CA'),
'ES' => array('ES'),
'AU' => array('AU'),
);
/**
* instance.
*
* Return a instance of this class.
*/
public static function instance(): GMIP
{
if (!isset(self::$instance) && !(self::$instance instanceof \GMIP\GMIP)) {
self::$instance = new Self();
}
return self::$instance;
}
/**
* init
*
* initialize the plugin.
*/
public function init(): void
{
$this->set_language();
$this->register_wp_hooks();
}
/**
* set_language
*
* Set the current language to a local property.
*/
private function set_language(): void
{
$this->current_lang = $_SERVER["HTTP_CF_IPCOUNTRY"] ?? $this->default_lang;
}
/**
* get_language
*
* Get the current language from local property.
*/
public function get_language(): string
{
return $this->current_lang;
}
/**
* register_wp_hooks
*
* initialize the plugin.
*
* @uses add_action https://developer.wordpress.org/reference/functions/add_action/
*/
private function register_wp_hooks(): void
{
add_action('init', array($this, 'register_shortcode'));
}
/**
* register_shortcode
*
* Register a custom gmip shortcode with wordpress.
*
* @uses add_shortcode https://developer.wordpress.org/reference/functions/add_shortcode/
*/
public function register_shortcode(): void
{
add_shortcode('gmip', array($this, 'handle_shortcode'));
}
/**
* handle_shortcode
*
* Handle the custom gmip shortcode
*/
public function handle_shortcode($atts, $content, $tag): void
{
if ($tag != 'gmip') return;
if (empty($atts['lang'])) return;
// Match given language to a config language.
foreach($this->language_config as $config) {
$equals = array_intersect($config, explode('|', $atts['lang']));
if(in_array($this->get_language(), $equals)) {
echo $content;
return;
}
}
// If no match is found show the "OTHER" shortcode content.
if (! in_array($this->get_language(), $this->get_all_config_languages()) and $atts['lang'] == 'OTHER') {
echo $content;
}
}
/**
* get_all_config_languages
*
* Get all languages listed in teh language config array.
*/
private function get_all_config_languages() {
$all_languages = array();
foreach($this->language_config as $config) {
$all_languages = array_merge($all_languages, $config);
}
return $all_languages;
}
}
/**
* get_language
*
* Returns the current language code.
*
* Example:
* $lang = \GMIP\get_language(); // 'EN', 'NL'
*/
function get_language(): string
{
return \GMIP\GMIP::instance()->get_language();
}
/**
* is_language
*
* Check if the given language is the current language.
*
* Example:
* if(\GMIP\is_language('NL')) {
* // Show content for dutch language.
* }
*/
function is_language(string $lang): bool
{
return \GMIP\GMIP::instance()->get_language() == $lang;
}
/**
* runGMIP
*
* Initialize this plugin.
*/
function runGMIP(): void
{
\GMIP\GMIP::instance()->init();
}
runGMIP();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment