PluginProbe
Polylang / 1.6.2
Polylang v1.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 / links.php

links.php in Polylang 1.6.2, at include/links.php

244 lines 6.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.2
7 */
8 class PLL_Links {
9 public $links_model, $model, $options;
10 protected $_links;
11
12 /*
13 * constructor
14 *
15 * @since 1.2
16 *
17 * @param object $polylang
18 */
19 public function __construct(&$polylang) {
20 $this->links_model = &$polylang->links_model;
21 $this->model = &$polylang->model;
22 $this->options = &$polylang->options;
23
24 // adds our domains or subdomains to allowed hosts for safe redirection
25 add_filter('allowed_redirect_hosts', array(&$this, 'allowed_redirect_hosts'));
26
27 // low priority on links filters to come after any other modifications
28 if ($this->options['force_lang']) {
29 add_filter('post_link', array(&$this, 'post_link'), 20, 2);
30 add_filter('_get_page_link', array(&$this, 'post_link'), 20, 2);
31 }
32
33 if ($this->links_model->using_permalinks) {
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
38 if ($this->options['force_lang'] > 1)
39 add_filter('attachment_link', array(&$this, 'attachment_link'), 20, 2);
40
41 if (3 == $this->options['force_lang'])
42 add_filter('preview_post_link', array(&$this, 'preview_post_link'), 20);
43 }
44
45 /*
46 * adds our domains or subdomains to allowed hosts for safe redirection
47 *
48 * @since 1.4.3
49 *
50 * @param array $hosts allowed hosts
51 * @return array
52 */
53 public function allowed_redirect_hosts($hosts) {
54 return array_unique(array_merge($hosts, $this->links_model->get_hosts()));
55 }
56
57 /*
58 * modifies post & page links
59 *
60 * @since 0.7
61 *
62 * @param string $link post link
63 * @param object|int $post post object or post ID
64 * @return string modified post link
65 */
66 public function post_link($link, $post) {
67 if (isset($this->_links[$link]))
68 return $this->_links[$link];
69
70 if ('_get_page_link' == current_filter()) // this filter uses the ID instead of the post object
71 $post = get_post($post);
72
73 // /!\ when post_status is not "publish", WP does not use pretty permalinks
74 return $this->_links[$link] = $post->post_status != 'publish' ? $link : $this->links_model->add_language_to_link($link, $this->model->get_post_language($post->ID));
75 }
76
77 /*
78 * modifies attachment links
79 *
80 * @since 1.6.2
81 *
82 * @param string $link attachment link
83 * @param int $post_id attachment link
84 * @return string modified attachment link
85 */
86 public function attachment_link($link, $post_id) {
87 if (isset($this->_links[$link]))
88 return $this->_links[$link];
89
90 return $this->_links[$link] = $this->links_model->add_language_to_link($link, $this->model->get_post_language($post_id));
91 }
92
93 /*
94 * modifies custom posts links
95 *
96 * @since 1.6
97 *
98 * @param string $link post link
99 * @param object|int $post post object or post ID
100 * @return string modified post link
101 */
102 public function post_type_link($link, $post) {
103 if (isset($this->_links[$link]))
104 return $this->_links[$link];
105
106 // /!\ when post_status is not "publish", WP does not use pretty permalinks
107 if ('publish' == $post->post_status && $this->model->is_translated_post_type($post->post_type)) {
108 $lang = $this->model->get_post_language($post->ID);
109
110 if ($this->options['force_lang'])
111 $link = $this->links_model->add_language_to_link($link, $lang);
112
113 $link = apply_filters('pll_post_type_link', $link, $lang, $post);
114 }
115
116 return $this->_links[$link] = $link;
117 }
118
119 /*
120 * modifies term link
121 *
122 * @since 0.7
123 *
124 * @param string $link term link
125 * @param object $post term object
126 * @param string $tax taxonomy name
127 * @return string modified term link
128 */
129 public function term_link($link, $term, $tax) {
130 if (isset($this->_links[$link]))
131 return $this->_links[$link];
132
133 if ($this->model->is_translated_taxonomy($tax)) {
134 $lang = $this->model->get_term_language($term->term_id);
135
136 if ($this->options['force_lang'])
137 $link = $this->links_model->add_language_to_link($link, $lang);
138
139 $link = apply_filters('pll_term_link', $link, $lang, $term);
140 }
141
142 return $this->_links[$link] = $link;
143 }
144
145 /*
146 * FIXME: keeps the preview post link on default domain when using multiple domains
147 *
148 * @since 1.6.1
149 *
150 * @param string $url
151 * @return string modified url
152 */
153 public function preview_post_link($url) {
154 return $this->links_model->remove_language_from_link($url);
155 }
156
157 /*
158 * returns the home url in the requested language
159 *
160 * @since 1.3
161 *
162 * @param object|string $language
163 * @param bool $is_search optional wether we need the home url for a search form, defaults to false
164 */
165 public function get_home_url($language, $is_search = false) {
166 $language = is_object($language) ? $language : $this->model->get_language($language);
167 return $is_search ? $language->search_url : $language->home_url;
168 }
169
170 /*
171 * get the link to create a new post translation
172 *
173 * @since 1.5
174 *
175 * @param int $post_id
176 * @param object $language
177 * @return string
178 */
179 public function get_new_post_translation_link($post_id, $language) {
180 $post_type = get_post_type($post_id);
181
182 if ('attachment' == $post_type) {
183 $args = array(
184 'action' => 'translate_media',
185 'from_media' => $post_id,
186 'new_lang' => $language->slug
187 );
188
189 // add nonce for media as we will directly publish a new attachment from a clic on this link
190 return wp_nonce_url(add_query_arg($args, admin_url('admin.php')), 'translate_media');
191 }
192 else {
193 $args = array(
194 'post_type' => $post_type,
195 'from_post' => $post_id,
196 'new_lang' => $language->slug
197 );
198
199 return add_query_arg($args, admin_url('post-new.php'));
200 }
201 }
202
203 /*
204 * get the link to create a new term translation
205 *
206 * @since 1.5
207 *
208 * @param int $term_id
209 * @param string $taxonomy
210 * @param string $post_type
211 * @param object $language
212 * @return string
213 */
214 public function get_new_term_translation_link($term_id, $taxonomy, $post_type, $language) {
215 $args = array(
216 'taxonomy' => $taxonomy,
217 'post_type' => $post_type,
218 'from_tag' => $term_id,
219 'new_lang' => $language->slug
220 );
221
222 return add_query_arg($args, admin_url('edit-tags.php'));
223 }
224
225 /*
226 * checks if the current user can read the post
227 *
228 * @since 1.5
229 *
230 * @param int $post_id
231 * @return bool
232 */
233 public function current_user_can_read($post_id) {
234 $post = get_post($post_id);
235 if (in_array($post->post_status, get_post_stati(array('public' => true))))
236 return true;
237
238 $post_type_object = get_post_type_object($post->post_type);
239 $user = wp_get_current_user();
240 return is_user_logged_in() && (current_user_can($post_type_object->cap->read_private_posts) || $user->ID == $post->post_author);
241 }
242 }
243
244