PLL_Links_Domain

Links model for use when using one domain per language for example mysite.com/sth and mysite.fr/qqch implements the "links_model interface".

Defined (1)

The class is defined in the following location(s).

/include/links-domain.php  
  1. class PLL_Links_Domain extends PLL_Links_Abstract_Domain { 
  2.  
  3. /** 
  4. * Constructor 
  5. * @since 1.8 
  6. * @param object $model PLL_Model instance 
  7. */ 
  8. public function __construct( &$model ) { 
  9. parent::__construct( $model ); 
  10.  
  11. $this->hosts = $this->get_hosts(); 
  12.  
  13. // Filrer the site url ( mainly to get the correct login form ) 
  14. add_filter( 'site_url', array( $this, 'site_url' ) ); 
  15.  
  16.  
  17. /** 
  18. * Adds the language code in url 
  19. * links_model interface 
  20. * @since 1.2 
  21. * @param string $url url to modify 
  22. * @param object $lang language 
  23. * @return string modified url 
  24. */ 
  25. public function add_language_to_link( $url, $lang ) { 
  26. if ( ! empty( $lang ) && ! empty( $this->hosts[ $lang->slug ] ) ) { 
  27. $url = preg_replace( '#:\/\/(' . parse_url( $this->home, PHP_URL_HOST ) . ')($|\/.*)#', '://' . $this->hosts[ $lang->slug ] . "$2", $url ); 
  28. return $url; 
  29.  
  30. /** 
  31. * Returns the url without language code 
  32. * links_model interface 
  33. * @since 1.2 
  34. * @param string $url url to modify 
  35. * @return string modified url 
  36. */ 
  37. public function remove_language_from_link( $url ) { 
  38. if ( ! empty( $this->hosts ) ) { 
  39. $url = preg_replace( '#:\/\/(' . implode( '|', $this->hosts ) . ')($|\/.*)#', '://' . parse_url( $this->home, PHP_URL_HOST ) . "$2", $url ); 
  40. return $url; 
  41.  
  42. /** 
  43. * Returns the language based on language code in url 
  44. * links_model interface 
  45. * @since 1.2 
  46. * @since 2.0 add $url argument 
  47. * @param string $url optional, defaults to current url 
  48. * @return string language slug 
  49. */ 
  50. public function get_language_from_url( $url = '' ) { 
  51. $host = empty( $url ) ? $_SERVER['HTTP_HOST'] : parse_url( $url, PHP_URL_HOST ); 
  52. return ( $lang = array_search( $host , $this->hosts ) ) ? $lang : ''; 
  53.  
  54. /** 
  55. * Returns the home url 
  56. * links_model interface 
  57. * @since 1.3.1 
  58. * @param object $lang PLL_Language object 
  59. * @return string 
  60. */ 
  61. function home_url( $lang ) { 
  62. return trailingslashit( empty( $this->options['domains'][ $lang->slug ] ) ? $this->home : $this->options['domains'][ $lang->slug ] ); 
  63.  
  64. /** 
  65. * Get hosts managed on the website 
  66. * @since 1.5 
  67. * @return array list of hosts 
  68. */ 
  69. public function get_hosts() { 
  70. $hosts = array(); 
  71. foreach ( $this->options['domains'] as $lang => $domain ) { 
  72. $hosts[ $lang ] = parse_url( $domain, PHP_URL_HOST ); 
  73. return $hosts;