PluginProbe
Polylang / 3.0
Polylang v3.0
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
polylang / include / links-domain.php

links-domain.php in Polylang 3.0, at include/links-domain.php

108 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package Polylang
4 */
5
6 /**
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"
10 *
11 * @since 1.2
12 */
13 class PLL_Links_Domain extends PLL_Links_Abstract_Domain {
14
15 /**
16 * An array with language code as keys and the host as values.
17 *
18 * @var string[]
19 */
20 protected $hosts;
21
22 /**
23 * Constructor
24 *
25 * @since 1.8
26 *
27 * @param object $model PLL_Model instance
28 */
29 public function __construct( &$model ) {
30 parent::__construct( $model );
31
32 $this->hosts = $this->get_hosts();
33
34 // Filter the site url ( mainly to get the correct login form )
35 add_filter( 'site_url', array( $this, 'site_url' ) );
36 }
37
38
39 /**
40 * Adds the language code in url
41 * links_model interface
42 *
43 * @since 1.2
44 *
45 * @param string $url The url to modify.
46 * @param PLL_Language $lang The language object.
47 * @return string Modified url.
48 */
49 public function add_language_to_link( $url, $lang ) {
50 if ( ! empty( $lang ) && ! empty( $this->hosts[ $lang->slug ] ) ) {
51 $url = preg_replace( '#://(' . wp_parse_url( $this->home, PHP_URL_HOST ) . ')($|/.*)#', '://' . $this->hosts[ $lang->slug ] . '$2', $url );
52 }
53 return $url;
54 }
55
56 /**
57 * Returns the url without language code
58 * links_model interface
59 *
60 * @since 1.2
61 *
62 * @param string $url url to modify
63 * @return string modified url
64 */
65 public function remove_language_from_link( $url ) {
66 if ( ! empty( $this->hosts ) ) {
67 $url = preg_replace( '#://(' . implode( '|', $this->hosts ) . ')($|/.*)#', '://' . wp_parse_url( $this->home, PHP_URL_HOST ) . '$2', $url );
68 }
69 return $url;
70 }
71
72 /**
73 * Returns the home url in a given language.
74 * links_model interface.
75 *
76 * @since 1.3.1
77 *
78 * @param PLL_Language $lang PLL_Language object.
79 * @return string
80 */
81 public function home_url( $lang ) {
82 return trailingslashit( empty( $this->options['domains'][ $lang->slug ] ) ? $this->home : $this->options['domains'][ $lang->slug ] );
83 }
84
85 /**
86 * Get hosts managed on the website.
87 *
88 * @since 1.5
89 *
90 * @return string[] List of hosts.
91 */
92 public function get_hosts() {
93 $hosts = array();
94 foreach ( $this->options['domains'] as $lang => $domain ) {
95 $host = wp_parse_url( $domain, PHP_URL_HOST );
96 // idn_to_ascii is much faster than the WordPress method.
97 if ( function_exists( 'idn_to_ascii' ) ) {
98 // The use of the constant is mandatory in PHP 7.2 and PHP 7.3 to avoid a deprecated notice.
99 $hosts[ $lang ] = defined( 'INTL_IDNA_VARIANT_UTS46' ) ? idn_to_ascii( $host, 0, INTL_IDNA_VARIANT_UTS46 ) : idn_to_ascii( $host );
100 } else {
101 $hosts[ $lang ] = Requests_IDNAEncoder::encode( $host );
102 }
103 }
104
105 return $hosts;
106 }
107 }
108