PluginProbe
Polylang / 1.9.2
Polylang v1.9.2
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-subdomain.php

links-subdomain.php in Polylang 1.9.2, at include/links-subdomain.php

93 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Links model for use when the language code is added in url as a subdomain
5 * for example en.mysite.com/something
6 * implements the "links_model interface"
7 *
8 * @since 1.2
9 */
10 class PLL_Links_Subdomain extends PLL_Links_Permalinks {
11 protected $www;
12
13 /**
14 * Constructor
15 *
16 * @since 1.7.4
17 *
18 * @param object $model PLL_Model instance
19 */
20 public function __construct( &$model ) {
21 parent::__construct( $model );
22 $this->www = false === strpos( $this->home, '://www.' ) ? '://' : '://www.';
23 }
24
25 /**
26 * Adds the language code in url
27 * links_model interface
28 *
29 * @since 1.2
30 *
31 * @param string $url url to modify
32 * @param object $lang language
33 * @return string modified url
34 */
35 public function add_language_to_link( $url, $lang ) {
36 if ( ! empty( $lang ) ) {
37 $url = $this->options['default_lang'] == $lang->slug && $this->options['hide_default'] ? $url : str_replace( $this->www, '://' . $lang->slug . '.', $url );
38 }
39 return $url;
40 }
41
42 /**
43 * Returns the url without language code
44 * links_model interface
45 *
46 * @since 1.2
47 *
48 * @param string $url url to modify
49 * @return string modified url
50 */
51 public function remove_language_from_link( $url ) {
52 foreach ( $this->model->get_languages_list() as $language ) {
53 if ( ! $this->options['hide_default'] || $this->options['default_lang'] != $language->slug ) {
54 $languages[] = $language->slug;
55 }
56 }
57
58 if ( ! empty( $languages ) ) {
59 $url = preg_replace( '#:\/\/(' . implode( '|', $languages ) . ')\.#', $this->www, $url );
60 }
61
62 return $url;
63 }
64
65 /**
66 * Returns the language based on language code in url
67 * links_model interface
68 *
69 * @since 1.2
70 *
71 * @return string language slug
72 */
73 public function get_language_from_url() {
74 $pattern = '#('.implode( '|', $this->model->get_languages_list( array( 'fields' => 'slug' ) ) ).')\.#';
75 return preg_match( $pattern, trailingslashit( $_SERVER['HTTP_HOST'] ), $matches ) ? $matches[1] : ''; // $matches[1] is the slug of the requested language
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->model->get_languages_list() as $lang ) {
88 $hosts[] = parse_url( $this->home_url( $lang ), PHP_URL_HOST );
89 }
90 return $hosts;
91 }
92 }
93