PluginProbe
Polylang / 3.4.2
Polylang v3.4.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-default.php

links-default.php in Polylang 3.4.2, at include/links-default.php

117 lines 3.0 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 the default permalinks
8 * for example mysite.com/?somevar=something&lang=en.
9 *
10 * @since 1.2
11 */
12 class PLL_Links_Default extends PLL_Links_Model {
13 /**
14 * Tells this child class of PLL_Links_Model does not use pretty permalinks.
15 *
16 * @var bool
17 */
18 public $using_permalinks = false;
19
20 /**
21 * Adds the language code in a url.
22 *
23 * @since 1.2
24 * @since 3.4 Accepts now a language slug.
25 *
26 * @param string $url The url to modify.
27 * @param PLL_Language|string|false $language Language object or slug.
28 * @return string The modified url.
29 */
30 public function add_language_to_link( $url, $language ) {
31 if ( $language instanceof PLL_Language ) {
32 $language = $language->slug;
33 }
34
35 return empty( $language ) || ( $this->options['hide_default'] && $this->options['default_lang'] === $language ) ? $url : add_query_arg( 'lang', $language, $url );
36 }
37
38 /**
39 * Removes the language information from an url.
40 *
41 * @since 1.2
42 *
43 * @param string $url The url to modify.
44 * @return string The modified url.
45 */
46 public function remove_language_from_link( $url ) {
47 return remove_query_arg( 'lang', $url );
48 }
49
50 /**
51 * Returns the link to the first page.
52 *
53 * @since 1.2
54 *
55 * @param string $url The url to modify.
56 * @return string The modified url.
57 */
58 public function remove_paged_from_link( $url ) {
59 return remove_query_arg( 'paged', $url );
60 }
61
62 /**
63 * Returns the link to the paged page.
64 *
65 * @since 1.5
66 *
67 * @param string $url The url to modify.
68 * @param int $page The page number.
69 * @return string The modified url.
70 */
71 public function add_paged_to_link( $url, $page ) {
72 return add_query_arg( array( 'paged' => $page ), $url );
73 }
74
75 /**
76 * Gets the language slug from the url if present.
77 *
78 * @since 1.2
79 * @since 2.0 Add the $url argument.
80 *
81 * @param string $url Optional, defaults to the current url.
82 * @return string Language slug.
83 */
84 public function get_language_from_url( $url = '' ) {
85 if ( empty( $url ) ) {
86 $url = pll_get_requested_url();
87 }
88
89 $pattern = sprintf(
90 '#[?&]lang=(?<lang>%s)(?:$|&)#',
91 implode( '|', $this->model->get_languages_list( array( 'fields' => 'slug' ) ) )
92 );
93 return preg_match( $pattern, $url, $matches ) ? $matches['lang'] : ''; // $matches['lang'] is the slug of the requested language.
94 }
95
96 /**
97 * Returns the static front page url in the given language.
98 *
99 * @since 1.8
100 * @since 3.4 Accepts now an array of language properties.
101 *
102 * @param PLL_Language|array $language Language object or array of language properties.
103 * @return string The static front page url.
104 */
105 public function front_page_url( $language ) {
106 if ( $language instanceof PLL_Language ) {
107 $language = $language->to_array();
108 }
109
110 if ( $this->options['hide_default'] && $language['is_default'] ) {
111 return trailingslashit( $this->home );
112 }
113 $url = home_url( '/?page_id=' . $language['page_on_front'] );
114 return $this->options['force_lang'] ? $this->add_language_to_link( $url, $language['slug'] ) : $url;
115 }
116 }
117