PluginProbe
Polylang / 1.7.7
Polylang v1.7.7
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.7.7, at include/links.php

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