PluginProbe
Polylang / 2.9
Polylang v2.9
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 2.9, at include/links-domain.php

101 lines 2.5 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 * Constructor
17 *
18 * @since 1.8
19 *
20 * @param object $model PLL_Model instance
21 */
22 public function __construct( &$model ) {
23 parent::__construct( $model );
24
25 $this->hosts = $this->get_hosts();
26
27 // Filter the site url ( mainly to get the correct login form )
28 add_filter( 'site_url', array( $this, 'site_url' ) );
29 }
30
31
32 /**
33 * Adds the language code in url
34 * links_model interface
35 *
36 * @since 1.2
37 *
38 * @param string $url url to modify
39 * @param object $lang language
40 * @return string modified url
41 */
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 );
45 }
46 return $url;
47 }
48
49 /**
50 * Returns the url without language code
51 * links_model interface
52 *
53 * @since 1.2
54 *
55 * @param string $url url to modify
56 * @return string modified url
57 */
58 public function remove_language_from_link( $url ) {
59 if ( ! empty( $this->hosts ) ) {
60 $url = preg_replace( '#://(' . implode( '|', $this->hosts ) . ')($|/.*)#', '://' . wp_parse_url( $this->home, PHP_URL_HOST ) . '$2', $url );
61 }
62 return $url;
63 }
64
65 /**
66 * Returns the home url
67 * links_model interface
68 *
69 * @since 1.3.1
70 *
71 * @param object $lang PLL_Language object
72 * @return string
73 */
74 public function home_url( $lang ) {
75 return trailingslashit( empty( $this->options['domains'][ $lang->slug ] ) ? $this->home : $this->options['domains'][ $lang->slug ] );
76 }
77
78 /**
79 * Get hosts managed on the website
80 *
81 * @since 1.5
82 *
83 * @return array list of hosts
84 */
85 public function get_hosts() {
86 $hosts = array();
87 foreach ( $this->options['domains'] as $lang => $domain ) {
88 $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 );
93 } else {
94 $hosts[ $lang ] = Requests_IDNAEncoder::encode( $host );
95 }
96 }
97
98 return $hosts;
99 }
100 }
101