PluginProbe
Polylang / 3.2.2
Polylang v3.2.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.2.2, at include/links-default.php

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