PluginProbe
Polylang / 3.1.4
Polylang v3.1.4
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 3.1.4, at include/links-subdomain.php

91 lines 2.1 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 the language code is added in url as a subdomain
8 * for example en.mysite.com/something
9 * implements the "links_model interface"
10 *
11 * @since 1.2
12 */
13 class PLL_Links_Subdomain extends PLL_Links_Abstract_Domain {
14 /**
15 * Stores whether the home url includes www. or not.
16 * Either '://' or '://www.'.
17 *
18 * @var string
19 */
20 protected $www;
21
22 /**
23 * Constructor.
24 *
25 * @since 1.7.4
26 *
27 * @param PLL_Model $model Instance of PLL_Model.
28 */
29 public function __construct( &$model ) {
30 parent::__construct( $model );
31 $this->www = ( false === strpos( $this->home, '://www.' ) ) ? '://' : '://www.';
32 }
33
34 /**
35 * Adds the language code in a url.
36 * links_model interface.
37 *
38 * @since 1.2
39 *
40 * @param string $url The url to modify.
41 * @param PLL_Language $lang The language object.
42 * @return string Modified url.
43 */
44 public function add_language_to_link( $url, $lang ) {
45 if ( ! empty( $lang ) && false === strpos( $url, '://' . $lang->slug . '.' ) ) {
46 $url = $this->options['default_lang'] == $lang->slug && $this->options['hide_default'] ? $url : str_replace( $this->www, '://' . $lang->slug . '.', $url );
47 }
48 return $url;
49 }
50
51 /**
52 * Returns the url without language code
53 * links_model interface
54 *
55 * @since 1.2
56 *
57 * @param string $url url to modify
58 * @return string modified url
59 */
60 public function remove_language_from_link( $url ) {
61 $languages = array();
62
63 foreach ( $this->model->get_languages_list() as $language ) {
64 if ( ! $this->options['hide_default'] || $this->options['default_lang'] != $language->slug ) {
65 $languages[] = $language->slug;
66 }
67 }
68
69 if ( ! empty( $languages ) ) {
70 $url = preg_replace( '#://(' . implode( '|', $languages ) . ')\.#', $this->www, $url );
71 }
72
73 return $url;
74 }
75
76 /**
77 * Get hosts managed on the website.
78 *
79 * @since 1.5
80 *
81 * @return string[] The list of hosts.
82 */
83 public function get_hosts() {
84 $hosts = array();
85 foreach ( $this->model->get_languages_list() as $lang ) {
86 $hosts[ $lang->slug ] = wp_parse_url( $this->home_url( $lang ), PHP_URL_HOST );
87 }
88 return $hosts;
89 }
90 }
91