PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/public/class-wanaham-redirect-ip-public.php

https://gitlab.com/wanaham/wanaham-redirect-ip
PHP | 149 lines | 40 code | 27 blank | 82 comment | 7 complexity | 2a6006008a1d1c4c6e45ca2abaa986d5 MD5 | raw file
  1. <?php
  2. /**
  3. * The public-facing functionality of the plugin.
  4. *
  5. * @link www.wanaham.com
  6. * @since 1.0.0
  7. *
  8. * @package Wanaham_Redirect_Ip
  9. * @subpackage Wanaham_Redirect_Ip/public
  10. */
  11. /**
  12. * The public-facing functionality of the plugin.
  13. *
  14. * Defines the plugin name, version, and two examples hooks for how to
  15. * enqueue the admin-specific stylesheet and JavaScript.
  16. *
  17. * @package Wanaham_Redirect_Ip
  18. * @subpackage Wanaham_Redirect_Ip/public
  19. * @author Wanaham <wanaham.mail@gmail.com>
  20. */
  21. class Wanaham_Redirect_Ip_Public {
  22. /**
  23. * The ID of this plugin.
  24. *
  25. * @since 1.0.0
  26. * @access private
  27. * @var string $plugin_name The ID of this plugin.
  28. */
  29. private $plugin_name;
  30. /**
  31. * The version of this plugin.
  32. *
  33. * @since 1.0.0
  34. * @access private
  35. * @var string $version The current version of this plugin.
  36. */
  37. private $version;
  38. /**
  39. * Initialize the class and set its properties.
  40. *
  41. * @since 1.0.0
  42. * @param string $plugin_name The name of the plugin.
  43. * @param string $version The version of this plugin.
  44. */
  45. private $options = array();
  46. private $deactivated = false;
  47. private $site_ur = '';
  48. public function __construct( $plugin_name, $version,$options ) {
  49. $this->plugin_name = $plugin_name;
  50. $this->version = $version;
  51. $this->options = $options;
  52. $this->site_url = get_site_url();
  53. if(isset($_SERVER['HTTP_REFERER'])){
  54. $pos = strrpos($_SERVER['HTTP_REFERER'],$this->site_url);
  55. if ($pos !== false) {
  56. $this->deactivated = true;
  57. }
  58. }
  59. }
  60. /**
  61. * Register the stylesheets for the public-facing side of the site.
  62. *
  63. * @since 1.0.0
  64. */
  65. public function enqueue_styles() {
  66. /**
  67. * This function is provided for demonstration purposes only.
  68. *
  69. * An instance of this class should be passed to the run() function
  70. * defined in Wanaham_Redirect_Ip_Loader as all of the hooks are defined
  71. * in that particular class.
  72. *
  73. * The Wanaham_Redirect_Ip_Loader will then create the relationship
  74. * between the defined hooks and the functions defined in this
  75. * class.
  76. */
  77. //wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/wanaham-redirect-ip-public.css', array(), $this->version, 'all' );
  78. }
  79. /**
  80. * Register the stylesheets for the public-facing side of the site.
  81. *
  82. * @since 1.0.0
  83. */
  84. public function enqueue_scripts() {
  85. /**
  86. * This function is provided for demonstration purposes only.
  87. *
  88. * An instance of this class should be passed to the run() function
  89. * defined in Wanaham_Redirect_Ip_Loader as all of the hooks are defined
  90. * in that particular class.
  91. *
  92. * The Wanaham_Redirect_Ip_Loader will then create the relationship
  93. * between the defined hooks and the functions defined in this
  94. * class.
  95. */
  96. //wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/wanaham-redirect-ip-public.js', array( 'jquery' ), $this->version, false );
  97. }
  98. /**
  99. * Redirect user from his ip and navigator language
  100. */
  101. public function redirect(){
  102. if($this->deactivated){
  103. return false;
  104. }
  105. $geoip = new Wanaham_Redirect_Ip_Geoip($this->plugin_name);
  106. $ip = false;
  107. //$ip = '192.206.151.131';//Toronto
  108. //$ip = '3.255.255.255'; // US
  109. //$ip = '2.223.255.255'; // GB
  110. //$ip = '89.91.148.159';//FR
  111. //$ip = '24.24.24.24';//MaxMind
  112. $country = $geoip->getCountryByAddr($ip);
  113. if($country === null){
  114. return false;
  115. }
  116. $geoip_redirect_urls = isset($this->options['geoip_redirect_urls']) ? $this->options['geoip_redirect_urls'] : array();
  117. if(isset($geoip_redirect_urls[$country]) && $this->site_url !== $geoip_redirect_urls[$country] && $geoip->hasLanguage($country)){
  118. wp_redirect( $geoip_redirect_urls[$country] );
  119. exit;
  120. }
  121. }
  122. }