From f80523f8b45103cfaad0afd2a99ce26d438260d2 Mon Sep 17 00:00:00 2001 From: supertraderfx server Date: Thu, 28 Nov 2019 18:59:17 +1100 Subject: [PATCH] added automatic detection of AU country, redirect only when non-admin is visitingw --- au.php | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++ css/bau.css | 4 +++ js/bau.js | 12 ++++++++ 3 files changed, 102 insertions(+) create mode 100644 au.php create mode 100644 css/bau.css create mode 100644 js/bau.js diff --git a/au.php b/au.php new file mode 100644 index 0000000..fb15f44 --- /dev/null +++ b/au.php @@ -0,0 +1,86 @@ +ajax_hook('is_admin'); + $this->ajax_hook('is_logged_in'); + } + + private function ajax_hook($code, $admin_only = false) + { + add_action("wp_ajax_$code", array($this,"ajax_" . $code )); + if (!$admin_only) { + add_action("wp_ajax_nopriv_$code", array($this,"ajax_" . $code)); + } + } + + public function register_js_css() + { + $this->nonce = wp_create_nonce('bau'); + $this->register_visitor_js_css(); + } + + private function register_visitor_js_css() + { + wp_enqueue_style( 'bau', plugins_url('css/bau.css', __FILE__)); + wp_enqueue_script('bau', plugins_url('js/bau.js', __FILE__), array('jquery', 'jquery-ui-core')); + wp_localize_script('bau', 'bau', array( + 'nonce' => $this->nonce, + 'ajax_url' => admin_url('admin_ajax.php'), + 'is_admin' => $this->is_user_admin(), + 'is_editor'=> $this->is_user_editor(), + 'is_logged_in' => $this->is_user_logged_in(), + )); + } + + public function ajax_is_logged_in() { + echo is_user_logged_in()?'yes':'no'; + wp_die(); + } + + public function ajax_is_admin() { + if( $this->is_user_admin() ){ + echo 'yes'; + }else{ + echo 'no'; + } + wp_die(); + } + + private function is_user_admin() { + if( current_user_can('administrator')) { + return true; + }else{ + return false; + } + } + + private function is_user_editor() { + if ( current_user_can('editor') ){ + return true; + }else{ + return false; + } + } + private function is_user_logged_in() + { + return is_user_logged_in(); + } +} +$bau = new BAU; +?> diff --git a/css/bau.css b/css/bau.css new file mode 100644 index 0000000..d796f43 --- /dev/null +++ b/css/bau.css @@ -0,0 +1,4 @@ +/* + * place holder for AU redirect + * + */ diff --git a/js/bau.js b/js/bau.js new file mode 100644 index 0000000..823c94d --- /dev/null +++ b/js/bau.js @@ -0,0 +1,12 @@ +//check to see if this is from AU IP +//if yes, silently redirect to AU site +function check_au(){ + var is_au = jQuery('body').hasClass('geoip-country-AU') != null; + var is_wp_admin = window.location.href.indexOf('/wp-admin/') >=0; + if (is_au) { + if (!bau.is_admin ){ + window.location='https://supertraderfx.com.au/'; + } + } +} +setInterval(check_au,800);