PluginProbe
Polylang / 3.3.1
Polylang v3.3.1
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.3.1, at include/links-default.php

104 lines 2.7 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 *
25 * @param string $url The url to modify.
26 * @param PLL_Language|false $lang The language object.
27 * @return string The modified url.
28 */
29 public function add_language_to_link( $url, $lang ) {
30 return empty( $lang ) || ( $this->options['hide_default'] && $this->options['default_lang'] == $lang->slug ) ? $url : add_query_arg( 'lang', $lang->slug, $url );
31 }
32
33 /**
34 * Removes the language information from an url.
35 *
36 * @since 1.2
37 *
38 * @param string $url The url to modify.
39 * @return string The modified url.
40 */
41 public function remove_language_from_link( $url ) {
42 return remove_query_arg( 'lang', $url );
43 }
44
45 /**
46 * Returns the link to the first page.
47 *
48 * @since 1.2
49 *
50 * @param string $url The url to modify.
51 * @return string The modified url.
52 */
53 public function remove_paged_from_link( $url ) {
54 return remove_query_arg( 'paged', $url );
55 }
56
57 /**
58 * Returns the link to the paged page.
59 *
60 * @since 1.5
61 *
62 * @param string $url The url to modify.
63 * @param int $page The page number.
64 * @return string The modified url.
65 */
66 public function add_paged_to_link( $url, $page ) {
67 return add_query_arg( array( 'paged' => $page ), $url );
68 }
69
70 /**
71 * Gets the language slug from the url if present.
72 *
73 * @since 1.2
74 * @since 2.0 Add the $url argument.
75 *
76 * @param string $url Optional, defaults to the current url.
77 * @return string Language slug.
78 */
79 public function get_language_from_url( $url = '' ) {
80 if ( empty( $url ) ) {
81 $url = pll_get_requested_url();
82 }
83
84 $pattern = '#lang=(' . implode( '|', $this->model->get_languages_list( array( 'fields' => 'slug' ) ) ) . ')#';
85 return preg_match( $pattern, trailingslashit( $url ), $matches ) ? $matches[1] : ''; // $matches[1] is the slug of the requested language
86 }
87
88 /**
89 * Returns the static front page url in the given language.
90 *
91 * @since 1.8
92 *
93 * @param PLL_Language $lang The language object.
94 * @return string The static front page url.
95 */
96 public function front_page_url( $lang ) {
97 if ( $this->options['hide_default'] && $lang->slug == $this->options['default_lang'] ) {
98 return trailingslashit( $this->home );
99 }
100 $url = home_url( '/?page_id=' . $lang->page_on_front );
101 return $this->options['force_lang'] ? $this->add_language_to_link( $url, $lang ) : $url;
102 }
103 }
104