| @@ -4,10 +4,9 @@ | ||
| 4 | 4 | */ |
| 5 | 5 | |
| 6 | 6 | /** |
| 7 | 7 | * Links model for use when using one domain per language |
| 8 | - * for example mysite.com/sth and mysite.fr/qqch | |
| 9 | - * implements the "links_model interface" | |
| 8 | + * for example mysite.com/something and mysite.fr/quelquechose. | |
| 10 | 9 | * |
| 11 | 10 | * @since 1.2 |
| 12 | 11 | */ |
| 13 | 12 | class PLL_Links_Domain extends PLL_Links_Abstract_Domain { |
| @@ -12,13 +11,20 @@ | ||
| 12 | 11 | */ |
| 13 | 12 | class PLL_Links_Domain extends PLL_Links_Abstract_Domain { |
| 14 | 13 | |
| 15 | 14 | /** |
| 16 | - * Constructor | |
| 15 | + * An array with language code as keys and the host as values. | |
| 17 | 16 | * |
| 17 | + * @var string[] | |
| 18 | + */ | |
| 19 | + protected $hosts; | |
| 20 | + | |
| 21 | + /** | |
| 22 | + * Constructor. | |
| 23 | + * | |
| 18 | 24 | * @since 1.8 |
| 19 | 25 | * |
| 20 | - * @param object $model PLL_Model instance | |
| 26 | + * @param object $model PLL_Model instance. | |
| 21 | 27 | */ |
| 22 | 28 | public function __construct( &$model ) { |
| 23 | 29 | parent::__construct( $model ); |
| 24 | 30 | |
| @@ -23,22 +29,21 @@ | ||
| 23 | 29 | parent::__construct( $model ); |
| 24 | 30 | |
| 25 | 31 | $this->hosts = $this->get_hosts(); |
| 26 | 32 | |
| 27 | - // Filter the site url ( mainly to get the correct login form ) | |
| 33 | + // Filters the site url (mainly to get the correct login form). | |
| 28 | 34 | add_filter( 'site_url', array( $this, 'site_url' ) ); |
| 29 | 35 | } |
| 30 | 36 | |
| 31 | 37 | |
| 32 | 38 | /** |
| 33 | - * Adds the language code in url | |
| 34 | - * links_model interface | |
| 39 | + * Switches the primary domain to a secondary domain in the url. | |
| 35 | 40 | * |
| 36 | 41 | * @since 1.2 |
| 37 | 42 | * |
| 38 | - * @param string $url url to modify | |
| 39 | - * @param object $lang language | |
| 40 | - * @return string modified url | |
| 43 | + * @param string $url The url to modify. | |
| 44 | + * @param PLL_Language|false $lang The language object. | |
| 45 | + * @return string The modified url. | |
| 41 | 46 | */ |
| 42 | 47 | public function add_language_to_link( $url, $lang ) { |
| 43 | 48 | if ( ! empty( $lang ) && ! empty( $this->hosts[ $lang->slug ] ) ) { |
| 44 | 49 | $url = preg_replace( '#://(' . wp_parse_url( $this->home, PHP_URL_HOST ) . ')($|/.*)#', '://' . $this->hosts[ $lang->slug ] . '$2', $url ); |
| @@ -46,15 +51,14 @@ | ||
| 46 | 51 | return $url; |
| 47 | 52 | } |
| 48 | 53 | |
| 49 | 54 | /** |
| 50 | - * Returns the url without language code | |
| 51 | - * links_model interface | |
| 55 | + * Returns the url with the primary domain. | |
| 52 | 56 | * |
| 53 | 57 | * @since 1.2 |
| 54 | 58 | * |
| 55 | - * @param string $url url to modify | |
| 56 | - * @return string modified url | |
| 59 | + * @param string $url The url to modify. | |
| 60 | + * @return string The modified url. | |
| 57 | 61 | */ |
| 58 | 62 | public function remove_language_from_link( $url ) { |
| 59 | 63 | if ( ! empty( $this->hosts ) ) { |
| 60 | 64 | $url = preg_replace( '#://(' . implode( '|', $this->hosts ) . ')($|/.*)#', '://' . wp_parse_url( $this->home, PHP_URL_HOST ) . '$2', $url ); |
| @@ -62,14 +66,13 @@ | ||
| 62 | 66 | return $url; |
| 63 | 67 | } |
| 64 | 68 | |
| 65 | 69 | /** |
| 66 | - * Returns the home url | |
| 67 | - * links_model interface | |
| 70 | + * Returns the home url in a given language. | |
| 68 | 71 | * |
| 69 | 72 | * @since 1.3.1 |
| 70 | 73 | * |
| 71 | - * @param object $lang PLL_Language object | |
| 74 | + * @param PLL_Language $lang The language object. | |
| 72 | 75 | * @return string |
| 73 | 76 | */ |
| 74 | 77 | public function home_url( $lang ) { |
| 75 | 78 | return trailingslashit( empty( $this->options['domains'][ $lang->slug ] ) ? $this->home : $this->options['domains'][ $lang->slug ] ); |
| @@ -75,19 +78,19 @@ | ||
| 75 | 78 | return trailingslashit( empty( $this->options['domains'][ $lang->slug ] ) ? $this->home : $this->options['domains'][ $lang->slug ] ); |
| 76 | 79 | } |
| 77 | 80 | |
| 78 | 81 | /** |
| 79 | - * Get hosts managed on the website | |
| 82 | + * Get the hosts managed on the website. | |
| 80 | 83 | * |
| 81 | 84 | * @since 1.5 |
| 82 | 85 | * |
| 83 | - * @return array list of hosts | |
| 86 | + * @return string[] List of hosts. | |
| 84 | 87 | */ |
| 85 | 88 | public function get_hosts() { |
| 86 | 89 | $hosts = array(); |
| 87 | 90 | foreach ( $this->options['domains'] as $lang => $domain ) { |
| 88 | 91 | $host = wp_parse_url( $domain, PHP_URL_HOST ); |
| 89 | - // idn_to_ascii is much faster than the WordPress method. | |
| 92 | + // The function idn_to_ascii() is much faster than the WordPress method. | |
| 90 | 93 | if ( function_exists( 'idn_to_ascii' ) ) { |
| 91 | 94 | // The use of the constant is mandatory in PHP 7.2 and PHP 7.3 to avoid a deprecated notice. |
| 92 | 95 | $hosts[ $lang ] = defined( 'INTL_IDNA_VARIANT_UTS46' ) ? idn_to_ascii( $host, 0, INTL_IDNA_VARIANT_UTS46 ) : idn_to_ascii( $host ); |
| 93 | 96 | } else { |