PluginProbe
Polylang / 3.6.7
Polylang v3.6.7
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 / filters-links.php

filters-links.php in Polylang 3.6.7, at include/filters-links.php

203 lines 5.6 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 * Manages links filters needed on both frontend and admin
8 *
9 * @since 1.8
10 */
11 class PLL_Filters_Links {
12 /**
13 * Stores the plugin options.
14 *
15 * @var array
16 */
17 public $options;
18
19 /**
20 * @var PLL_Model
21 */
22 public $model;
23
24 /**
25 * Instance of a child class of PLL_Links_Model.
26 *
27 * @var PLL_Links_Model
28 */
29 public $links_model;
30
31 /**
32 * @var PLL_Links|null
33 */
34 public $links;
35
36 /**
37 * Current language.
38 *
39 * @var PLL_Language|null
40 */
41 public $curlang;
42
43 /**
44 * Constructor.
45 *
46 * @since 1.8
47 *
48 * @param object $polylang The Polylang object.
49 */
50 public function __construct( &$polylang ) {
51 $this->links = &$polylang->links;
52 $this->links_model = &$polylang->links_model;
53 $this->model = &$polylang->model;
54 $this->options = &$polylang->options;
55 $this->curlang = &$polylang->curlang;
56
57 // Low priority on links filters to come after any other modifications.
58 if ( $this->options['force_lang'] ) {
59 add_filter( 'post_link', array( $this, 'post_type_link' ), 20, 2 );
60 add_filter( '_get_page_link', array( $this, '_get_page_link' ), 20, 2 );
61 }
62
63 add_filter( 'post_type_link', array( $this, 'post_type_link' ), 20, 2 );
64 add_filter( 'term_link', array( $this, 'term_link' ), 20, 3 );
65
66 if ( $this->options['force_lang'] > 0 ) {
67 add_filter( 'attachment_link', array( $this, 'attachment_link' ), 20, 2 );
68 }
69
70 // Keeps the preview post link on default domain when using multiple domains and SSO is not available.
71 if ( 3 === $this->options['force_lang'] && ! class_exists( 'PLL_Xdata_Domain' ) ) {
72 add_filter( 'preview_post_link', array( $this, 'preview_post_link' ), 20 );
73 }
74
75 // Rewrites post types archives links to filter them by language.
76 add_filter( 'post_type_archive_link', array( $this, 'post_type_archive_link' ), 20, 2 );
77 }
78
79 /**
80 * Modifies page links
81 *
82 * @since 1.7
83 *
84 * @param string $link post link
85 * @param int $post_id post ID
86 * @return string modified post link
87 */
88 public function _get_page_link( $link, $post_id ) {
89 // /!\ WP does not use pretty permalinks for preview
90 return false !== strpos( $link, 'preview=true' ) && false !== strpos( $link, 'page_id=' ) ? $link : $this->links_model->switch_language_in_link( $link, $this->model->post->get_language( $post_id ) );
91 }
92
93 /**
94 * Modifies attachment links
95 *
96 * @since 1.6.2
97 *
98 * @param string $link attachment link
99 * @param int $post_id attachment link
100 * @return string modified attachment link
101 */
102 public function attachment_link( $link, $post_id ) {
103 return wp_get_post_parent_id( $post_id ) ? $link : $this->links_model->switch_language_in_link( $link, $this->model->post->get_language( $post_id ) );
104 }
105
106 /**
107 * Modifies custom posts links.
108 *
109 * @since 1.6
110 *
111 * @param string $link Post link.
112 * @param WP_Post $post Post object.
113 * @return string Modified post link.
114 */
115 public function post_type_link( $link, $post ) {
116 // /!\ WP does not use pretty permalinks for preview
117 if ( ( false === strpos( $link, 'preview=true' ) || false === strpos( $link, 'p=' ) ) && $this->model->is_translated_post_type( $post->post_type ) ) {
118 $lang = $this->model->post->get_language( $post->ID );
119 $link = $this->options['force_lang'] ? $this->links_model->switch_language_in_link( $link, $lang ) : $link;
120
121 /**
122 * Filters a post or custom post type link.
123 *
124 * @since 1.6
125 *
126 * @param string $link The post link.
127 * @param PLL_Language $lang The current language.
128 * @param WP_Post $post The post object.
129 */
130 $link = apply_filters( 'pll_post_type_link', $link, $lang, $post );
131 }
132
133 return $link;
134 }
135
136 /**
137 * Modifies term links.
138 *
139 * @since 0.7
140 *
141 * @param string $link Term link.
142 * @param WP_Term $term Term object.
143 * @param string $tax Taxonomy name;
144 * @return string Modified term link.
145 */
146 public function term_link( $link, $term, $tax ) {
147 if ( $this->model->is_translated_taxonomy( $tax ) ) {
148 $lang = $this->model->term->get_language( $term->term_id );
149 $link = $this->options['force_lang'] ? $this->links_model->switch_language_in_link( $link, $lang ) : $link;
150
151 /**
152 * Filter a term link
153 *
154 * @since 1.6
155 *
156 * @param string $link The term link.
157 * @param PLL_Language $lang The current language.
158 * @param WP_Term $term The term object.
159 */
160 return apply_filters( 'pll_term_link', $link, $lang, $term );
161 }
162
163 // In case someone calls get_term_link for the 'language' taxonomy.
164 if ( 'language' === $tax ) {
165 $lang = $this->model->get_language( $term->term_id );
166 if ( $lang ) {
167 return $this->links_model->home_url( $lang );
168 }
169 }
170
171 return $link;
172 }
173
174 /**
175 * Keeps the preview post link on default domain when using multiple domains.
176 *
177 * @since 1.6.1
178 *
179 * @param string $url URL used for the post preview.
180 * @return string The modified url.
181 */
182 public function preview_post_link( $url ) {
183 return $this->links_model->remove_language_from_link( $url );
184 }
185
186 /**
187 * Modifies the post type archive links to add the language parameter
188 * only if the post type is translated.
189 *
190 * The filter was originally only on frontend but is needed on admin too for
191 * compatibility with the archive link of the ACF link field since ACF 5.4.0.
192 *
193 * @since 1.7.6
194 *
195 * @param string $link The post type archive permalink.
196 * @param string $post_type Post type name.
197 * @return string
198 */
199 public function post_type_archive_link( $link, $post_type ) {
200 return $this->model->is_translated_post_type( $post_type ) && 'post' !== $post_type ? $this->links_model->switch_language_in_link( $link, $this->curlang ) : $link;
201 }
202 }
203