PLL_Links_Subdomain

Links model for use when the language code is added in url as a subdomain for example en.mysite.com/something implements the "links_model interface".

Defined (1)

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

/include/links-subdomain.php  
  1. class PLL_Links_Subdomain extends PLL_Links_Abstract_Domain { 
  2. protected $www; 
  3.  
  4. /** 
  5. * Constructor 
  6. * @since 1.7.4 
  7. * @param object $model PLL_Model instance 
  8. */ 
  9. public function __construct( &$model ) { 
  10. parent::__construct( $model ); 
  11. $this->www = false === strpos( $this->home, '://www.' ) ? '://' : '://www.'; 
  12.  
  13. /** 
  14. * Adds the language code in url 
  15. * links_model interface 
  16. * @since 1.2 
  17. * @param string $url url to modify 
  18. * @param object $lang language 
  19. * @return string modified url 
  20. */ 
  21. public function add_language_to_link( $url, $lang ) { 
  22. if ( ! empty( $lang ) && false === strpos( $url, '://' . $lang->slug . '.' ) ) { 
  23. $url = $this->options['default_lang'] == $lang->slug && $this->options['hide_default'] ? $url : str_replace( $this->www, '://' . $lang->slug . '.', $url ); 
  24. return $url; 
  25.  
  26. /** 
  27. * Returns the url without language code 
  28. * links_model interface 
  29. * @since 1.2 
  30. * @param string $url url to modify 
  31. * @return string modified url 
  32. */ 
  33. public function remove_language_from_link( $url ) { 
  34. foreach ( $this->model->get_languages_list() as $language ) { 
  35. if ( ! $this->options['hide_default'] || $this->options['default_lang'] != $language->slug ) { 
  36. $languages[] = $language->slug; 
  37.  
  38. if ( ! empty( $languages ) ) { 
  39. $url = preg_replace( '#:\/\/(' . implode( '|', $languages ) . ')\.#', $this->www, $url ); 
  40.  
  41. return $url; 
  42.  
  43. /** 
  44. * Returns the language based on language code in url 
  45. * links_model interface 
  46. * @since 1.2 
  47. * @since 2.0 add $url argument 
  48. * @param string $url optional, defaults to current url 
  49. * @return string language slug 
  50. */ 
  51. public function get_language_from_url( $url = '' ) { 
  52. $host = empty( $url ) ? $_SERVER['HTTP_HOST'] : parse_url( $url, PHP_URL_HOST ); 
  53. $pattern = '#('.implode( '|', $this->model->get_languages_list( array( 'fields' => 'slug' ) ) ).')\.#'; 
  54. return preg_match( $pattern, trailingslashit( $host ), $matches ) ? $matches[1] : ''; // $matches[1] is the slug of the requested language 
  55.  
  56. /** 
  57. * Get hosts managed on the website 
  58. * @since 1.5 
  59. * @return array list of hosts 
  60. */ 
  61. public function get_hosts() { 
  62. $hosts = array(); 
  63. foreach ( $this->model->get_languages_list() as $lang ) { 
  64. $hosts[] = parse_url( $this->home_url( $lang ), PHP_URL_HOST ); 
  65. return $hosts;