PluginProbe
Polylang / 1.9.2
Polylang v1.9.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 1.9.2, at include/filters-links.php

162 lines 4.5 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;
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
24 // low priority on links filters to come after any other modifications
25 if ( $this->options['force_lang'] ) {
26 add_filter( 'post_link', array( &$this, 'post_link' ), 20, 2 );
27 add_filter( '_get_page_link', array( &$this, '_get_page_link' ), 20, 2 );
28 }
29
30 add_filter( 'post_type_link', array( &$this, 'post_type_link' ), 20, 2 );
31 add_filter( 'term_link', array( &$this, 'term_link' ), 20, 3 );
32
33 if ( $this->options['force_lang'] > 0 ) {
34 add_filter( 'attachment_link', array( &$this, 'attachment_link' ), 20, 2 );
35 }
36
37 if ( 3 === $this->options['force_lang'] ) {
38 add_filter( 'preview_post_link', array( &$this, 'preview_post_link' ), 20 );
39 }
40 }
41
42 /**
43 * modifies post & page links
44 *
45 * @since 0.7
46 *
47 * @param string $link post link
48 * @param object $post post object
49 * @return string modified post link
50 */
51 public function post_link( $link, $post ) {
52 // /!\ WP does not use pretty permalinks for preview
53 return false !== strpos( $link, 'preview=true' ) && false !== strpos( $link, 'p=' ) ? $link : $this->links_model->add_language_to_link( $link, $this->model->post->get_language( $post->ID ) );
54 }
55
56
57 /**
58 * modifies page links
59 *
60 * @since 1.7
61 *
62 * @param string $link post link
63 * @param int $post_id post ID
64 * @return string modified post link
65 */
66 public function _get_page_link( $link, $post_id ) {
67 // /!\ WP does not use pretty permalinks for preview
68 return false !== strpos( $link, 'preview=true' ) && false !== strpos( $link, 'page_id=' ) ? $link : $this->links_model->add_language_to_link( $link, $this->model->post->get_language( $post_id ) );
69 }
70
71 /**
72 * modifies attachment links
73 *
74 * @since 1.6.2
75 *
76 * @param string $link attachment link
77 * @param int $post_id attachment link
78 * @return string modified attachment link
79 */
80 public function attachment_link( $link, $post_id ) {
81 return wp_get_post_parent_id( $post_id ) ? $link : $this->links_model->add_language_to_link( $link, $this->model->post->get_language( $post_id ) );
82 }
83
84 /**
85 * modifies custom posts links
86 *
87 * @since 1.6
88 *
89 * @param string $link post link
90 * @param object $post post object
91 * @return string modified post link
92 */
93 public function post_type_link( $link, $post ) {
94 // /!\ WP does not use pretty permalinks for preview
95 if ( ( false === strpos( $link, 'preview=true' ) || false === strpos( $link, 'p=' ) ) && $this->model->is_translated_post_type( $post->post_type ) ) {
96 $lang = $this->model->post->get_language( $post->ID );
97 $link = $this->options['force_lang'] ? $this->links_model->add_language_to_link( $link, $lang ) : $link;
98
99 /**
100 * Filter a custom post type link
101 *
102 * @since 1.6
103 *
104 * @param string $link the term link
105 * @param object $lang the current language
106 * @param object $post the post object
107 */
108 $link = apply_filters( 'pll_post_type_link', $link, $lang, $post );
109 }
110
111 return $link;
112 }
113
114 /**
115 * modifies term link
116 *
117 * @since 0.7
118 *
119 * @param string $link term link
120 * @param object $term term object
121 * @param string $tax taxonomy name
122 * @return string modified term link
123 */
124 public function term_link( $link, $term, $tax ) {
125 if ( $this->model->is_translated_taxonomy( $tax ) ) {
126 $lang = $this->model->term->get_language( $term->term_id );
127 $link = $this->options['force_lang'] ? $this->links_model->add_language_to_link( $link, $lang ) : $link;
128
129 /**
130 * Filter a term link
131 *
132 * @since 1.6
133 *
134 * @param string $link the term link
135 * @param object $lang the current language
136 * @param object $term the term object
137 */
138 return apply_filters( 'pll_term_link', $link, $lang, $term );
139 }
140
141 // in case someone calls get_term_link for the 'language' taxonomy
142 if ( 'language' === $tax ) {
143 return $this->links_model->home_url( $term );
144 }
145
146 return $link;
147 }
148
149 /**
150 * FIXME: keeps the preview post link on default domain when using multiple domains
151 *
152 * @since 1.6.1
153 *
154 * @param string $url
155 * @return string modified url
156 */
157 public function preview_post_link( $url ) {
158 return $this->links_model->remove_language_from_link( $url );
159 }
160 }
161
162