PluginProbe
Polylang / 2.6.2
Polylang v2.6.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 / filters-links.php

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

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