PluginProbe
Polylang / 2.8.3
Polylang v2.8.3
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 2.8.3, at include/filters-links.php

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