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

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