| 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 |
// /!\ when post_status is not "publish", WP does not use pretty permalinks |
| 65 |
return $post->post_status != 'publish' ? $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 |
$post = get_post($post_id); |
| 80 |
|
| 81 |
// /!\ when post_status is not "publish", WP does not use pretty permalinks |
| 82 |
return $post->post_status != 'publish' ? $link : $this->links_model->add_language_to_link($link, $this->model->get_post_language($post->ID)); |
| 83 |
} |
| 84 |
|
| 85 |
/* |
| 86 |
* modifies attachment links |
| 87 |
* |
| 88 |
* @since 1.6.2 |
| 89 |
* |
| 90 |
* @param string $link attachment link |
| 91 |
* @param int $post_id attachment link |
| 92 |
* @return string modified attachment link |
| 93 |
*/ |
| 94 |
public function attachment_link($link, $post_id) { |
| 95 |
return $this->links_model->add_language_to_link($link, $this->model->get_post_language($post_id)); |
| 96 |
} |
| 97 |
|
| 98 |
/* |
| 99 |
* modifies custom posts links |
| 100 |
* |
| 101 |
* @since 1.6 |
| 102 |
* |
| 103 |
* @param string $link post link |
| 104 |
* @param object $post post object |
| 105 |
* @return string modified post link |
| 106 |
*/ |
| 107 |
public function post_type_link($link, $post) { |
| 108 |
// /!\ when post_status is not "publish", WP does not use pretty permalinks |
| 109 |
if ('publish' == $post->post_status && $this->model->is_translated_post_type($post->post_type)) { |
| 110 |
$lang = $this->model->get_post_language($post->ID); |
| 111 |
$link = $this->options['force_lang'] ? $this->links_model->add_language_to_link($link, $lang) : $link; |
| 112 |
$link = apply_filters('pll_post_type_link', $link, $lang, $post); |
| 113 |
} |
| 114 |
|
| 115 |
return $link; |
| 116 |
} |
| 117 |
|
| 118 |
/* |
| 119 |
* modifies term link |
| 120 |
* |
| 121 |
* @since 0.7 |
| 122 |
* |
| 123 |
* @param string $link term link |
| 124 |
* @param object $post term object |
| 125 |
* @param string $tax taxonomy name |
| 126 |
* @return string modified term link |
| 127 |
*/ |
| 128 |
public function term_link($link, $term, $tax) { |
| 129 |
if ($this->model->is_translated_taxonomy($tax)) { |
| 130 |
$lang = $this->model->get_term_language($term->term_id); |
| 131 |
$link = $this->options['force_lang'] ? $this->links_model->add_language_to_link($link, $lang) : $link; |
| 132 |
$link = apply_filters('pll_term_link', $link, $lang, $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 |
* returns the home url in the requested language |
| 152 |
* |
| 153 |
* @since 1.3 |
| 154 |
* |
| 155 |
* @param object|string $language |
| 156 |
* @param bool $is_search optional wether we need the home url for a search form, defaults to false |
| 157 |
*/ |
| 158 |
public function get_home_url($language, $is_search = false) { |
| 159 |
$language = is_object($language) ? $language : $this->model->get_language($language); |
| 160 |
return $is_search ? $language->search_url : $language->home_url; |
| 161 |
} |
| 162 |
|
| 163 |
/* |
| 164 |
* get the link to create a new post translation |
| 165 |
* |
| 166 |
* @since 1.5 |
| 167 |
* |
| 168 |
* @param int $post_id |
| 169 |
* @param object $language |
| 170 |
* @return string |
| 171 |
*/ |
| 172 |
public function get_new_post_translation_link($post_id, $language) { |
| 173 |
$post_type = get_post_type($post_id); |
| 174 |
|
| 175 |
if ('attachment' == $post_type) { |
| 176 |
$args = array( |
| 177 |
'action' => 'translate_media', |
| 178 |
'from_media' => $post_id, |
| 179 |
'new_lang' => $language->slug |
| 180 |
); |
| 181 |
|
| 182 |
// add nonce for media as we will directly publish a new attachment from a clic on this link |
| 183 |
return wp_nonce_url(add_query_arg($args, admin_url('admin.php')), 'translate_media'); |
| 184 |
} |
| 185 |
else { |
| 186 |
$args = array( |
| 187 |
'post_type' => $post_type, |
| 188 |
'from_post' => $post_id, |
| 189 |
'new_lang' => $language->slug |
| 190 |
); |
| 191 |
|
| 192 |
return add_query_arg($args, admin_url('post-new.php')); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
/* |
| 197 |
* get the link to create a new term translation |
| 198 |
* |
| 199 |
* @since 1.5 |
| 200 |
* |
| 201 |
* @param int $term_id |
| 202 |
* @param string $taxonomy |
| 203 |
* @param string $post_type |
| 204 |
* @param object $language |
| 205 |
* @return string |
| 206 |
*/ |
| 207 |
public function get_new_term_translation_link($term_id, $taxonomy, $post_type, $language) { |
| 208 |
$args = array( |
| 209 |
'taxonomy' => $taxonomy, |
| 210 |
'post_type' => $post_type, |
| 211 |
'from_tag' => $term_id, |
| 212 |
'new_lang' => $language->slug |
| 213 |
); |
| 214 |
|
| 215 |
return add_query_arg($args, admin_url('edit-tags.php')); |
| 216 |
} |
| 217 |
|
| 218 |
/* |
| 219 |
* checks if the current user can read the post |
| 220 |
* |
| 221 |
* @since 1.5 |
| 222 |
* |
| 223 |
* @param int $post_id |
| 224 |
* @return bool |
| 225 |
*/ |
| 226 |
public function current_user_can_read($post_id) { |
| 227 |
$post = get_post($post_id); |
| 228 |
if (in_array($post->post_status, get_post_stati(array('public' => true)))) |
| 229 |
return true; |
| 230 |
|
| 231 |
$post_type_object = get_post_type_object($post->post_type); |
| 232 |
$user = wp_get_current_user(); |
| 233 |
return is_user_logged_in() && (current_user_can($post_type_object->cap->read_private_posts) || $user->ID == $post->post_author); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
|