PluginProbe
Polylang / 3.8.6
Polylang v3.8.6
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 / src / filters-links.php

filters-links.php in Polylang 3.8.6, at src/filters-links.php

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