Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

119 lines
2.7KB

  1. <?php
  2. /*
  3. * Plugin Name: Australia Local Check
  4. * Plugin URI: https://biukop.com.au/
  5. * Description: Redirect to Australia Website if detecting a visitor is from Australia, but not doing so if the admin is logged in. This is a customized plugin developed for superforex only.
  6. * Version: 1.3.3
  7. * Author: Patrick
  8. * Author URI: https://biukop.com.au/
  9. * Text Domain: Biukop
  10. * Domain Path: /i18n/
  11. * */
  12. namespace Biukop;
  13. require_once (ABSPATH . 'wp-includes/pluggable.php');
  14. class BAU {
  15. private $nonce;
  16. public function __construct(){
  17. add_action('wp_enqueue_scripts', array($this, 'register_js_css'), 99);
  18. $this->ajax_hook('is_admin');
  19. $this->ajax_hook('is_logged_in');
  20. add_action('wp', array($this, 'check_au'));
  21. }
  22. public function check_au()
  23. {
  24. $ip = \geoip_detect2_get_client_ip();
  25. $ipinfo = \geoip_detect2_get_info_from_current_ip();
  26. if ($this->is_au($ipinfo)){
  27. if (!$this->is_user_admin()) {
  28. if($this->is_url_wp_admin()){
  29. //wp-admin, we stay
  30. }else{
  31. wp_redirect("https://supertraderfx.com.au/", 307);
  32. }
  33. }
  34. }
  35. }
  36. private function is_au ($ipinfo)
  37. {
  38. return $ipinfo->country->isoCode == "AU";
  39. }
  40. private function is_url_wp_admin()
  41. {
  42. $url = $_SERVER['REQUEST_URI'];
  43. if ($url == "/wp-admin/"){
  44. return true;
  45. }else{
  46. return false;
  47. }
  48. }
  49. private function ajax_hook($code, $admin_only = false)
  50. {
  51. add_action("wp_ajax_$code", array($this,"ajax_" . $code ));
  52. if (!$admin_only) {
  53. add_action("wp_ajax_nopriv_$code", array($this,"ajax_" . $code));
  54. }
  55. }
  56. public function register_js_css()
  57. {
  58. $this->nonce = wp_create_nonce('bau');
  59. $this->register_visitor_js_css();
  60. }
  61. private function register_visitor_js_css()
  62. {
  63. wp_enqueue_style( 'bau', plugins_url('css/bau.css', __FILE__));
  64. wp_enqueue_script('bau', plugins_url('js/bau.js', __FILE__), array('jquery', 'jquery-ui-core'));
  65. wp_localize_script('bau', 'bau', array(
  66. 'nonce' => $this->nonce,
  67. 'ajax_url' => admin_url('admin_ajax.php'),
  68. 'is_admin' => $this->is_user_admin(),
  69. 'is_editor'=> $this->is_user_editor(),
  70. 'is_logged_in' => $this->is_user_logged_in(),
  71. ));
  72. }
  73. public function ajax_is_logged_in() {
  74. echo is_user_logged_in()?'yes':'no';
  75. wp_die();
  76. }
  77. public function ajax_is_admin() {
  78. if( $this->is_user_admin() ){
  79. echo 'yes';
  80. }else{
  81. echo 'no';
  82. }
  83. wp_die();
  84. }
  85. private function is_user_admin() {
  86. if( current_user_can('administrator')) {
  87. return true;
  88. }else{
  89. return false;
  90. }
  91. }
  92. private function is_user_editor() {
  93. if ( current_user_can('editor') ){
  94. return true;
  95. }else{
  96. return false;
  97. }
  98. }
  99. private function is_user_logged_in()
  100. {
  101. return is_user_logged_in();
  102. }
  103. }
  104. $bau = new BAU;
  105. ?>