PluginProbe
Polylang / 3.7.7
Polylang v3.7.7
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
← All changes | include/links-domain.php +47 -30 2.93.7.7 View file →
@@ -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,38 +29,41 @@
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
42 + * @since 3.4 Accepts now a language slug.
37 43 *
38 - * @param string $url url to modify
39 - * @param object $lang language
40 - * @return string modified url
44 + * @param string $url The url to modify.
45 + * @param PLL_Language|string|false $language Language object or slug.
46 + * @return string The modified url.
41 47 */
42 - public function add_language_to_link( $url, $lang ) {
43 - if ( ! empty( $lang ) && ! empty( $this->hosts[ $lang->slug ] ) ) {
44 - $url = preg_replace( '#://(' . wp_parse_url( $this->home, PHP_URL_HOST ) . ')($|/.*)#', '://' . $this->hosts[ $lang->slug ] . '$2', $url );
48 + public function add_language_to_link( $url, $language ) {
49 + if ( $language instanceof PLL_Language ) {
50 + $language = $language->slug;
45 51 }
52 +
53 + if ( ! empty( $language ) && ! empty( $this->hosts[ $language ] ) ) {
54 + $url = preg_replace( '#://(' . wp_parse_url( $this->home, PHP_URL_HOST ) . ')($|/.*)#', '://' . $this->hosts[ $language ] . '$2', $url );
55 + }
46 56 return $url;
47 57 }
48 58
49 59 /**
50 - * Returns the url without language code
51 - * links_model interface
60 + * Returns the url with the primary domain.
52 61 *
53 62 * @since 1.2
54 63 *
55 - * @param string $url url to modify
56 - * @return string modified url
64 + * @param string $url The url to modify.
65 + * @return string The modified url.
57 66 */
58 67 public function remove_language_from_link( $url ) {
59 68 if ( ! empty( $this->hosts ) ) {
60 69 $url = preg_replace( '#://(' . implode( '|', $this->hosts ) . ')($|/.*)#', '://' . wp_parse_url( $this->home, PHP_URL_HOST ) . '$2', $url );
@@ -62,37 +71,45 @@
62 71 return $url;
63 72 }
64 73
65 74 /**
66 - * Returns the home url
67 - * links_model interface
75 + * Returns the home url in a given language.
68 76 *
69 77 * @since 1.3.1
78 + * @since 3.4 Accepts now a language slug.
70 79 *
71 - * @param object $lang PLL_Language object
80 + * @param PLL_Language|string $language Language object or slug.
72 81 * @return string
73 82 */
74 - public function home_url( $lang ) {
75 - return trailingslashit( empty( $this->options['domains'][ $lang->slug ] ) ? $this->home : $this->options['domains'][ $lang->slug ] );
83 + public function home_url( $language ) {
84 + if ( $language instanceof PLL_Language ) {
85 + $language = $language->slug;
86 + }
87 +
88 + return trailingslashit( empty( $this->options['domains'][ $language ] ) ? $this->home : $this->options['domains'][ $language ] );
76 89 }
77 90
78 91 /**
79 - * Get hosts managed on the website
92 + * Get the hosts managed on the website.
80 93 *
81 94 * @since 1.5
82 95 *
83 - * @return array list of hosts
96 + * @return string[] List of hosts.
84 97 */
85 98 public function get_hosts() {
86 99 $hosts = array();
87 - foreach ( $this->options['domains'] as $lang => $domain ) {
100 + foreach ( (array) $this->options['domains'] as $lang => $domain ) {
88 101 $host = wp_parse_url( $domain, PHP_URL_HOST );
89 - // idn_to_ascii is much faster than the WordPress method.
90 - if ( function_exists( 'idn_to_ascii' ) ) {
91 - // The use of the constant is mandatory in PHP 7.2 and PHP 7.3 to avoid a deprecated notice.
92 - $hosts[ $lang ] = defined( 'INTL_IDNA_VARIANT_UTS46' ) ? idn_to_ascii( $host, 0, INTL_IDNA_VARIANT_UTS46 ) : idn_to_ascii( $host );
102 +
103 + if ( ! is_string( $host ) ) {
104 + continue;
105 + }
106 +
107 + // The function idn_to_ascii() is much faster than the WordPress method.
108 + if ( function_exists( 'idn_to_ascii' ) && defined( 'INTL_IDNA_VARIANT_UTS46' ) ) {
109 + $hosts[ $lang ] = (string) idn_to_ascii( $host, 0, INTL_IDNA_VARIANT_UTS46 );
93 110 } else {
94 - $hosts[ $lang ] = Requests_IDNAEncoder::encode( $host );
111 + $hosts[ $lang ] = \WpOrg\Requests\IdnaEncoder::encode( $host ); // Since WP 6.2.
95 112 }
96 113 }
97 114
98 115 return $hosts;