| 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 $links_model |
| 17 |
*/ |
| 18 |
public function __construct(&$links_model) { |
| 19 |
$this->links_model = &$links_model; |
| 20 |
$this->model = &$links_model->model; |
| 21 |
$this->options = &$this->model->options; |
| 22 |
|
| 23 |
// low priority on links filters to come after any other modifications |
| 24 |
if ($this->options['force_lang']) { |
| 25 |
foreach (array('post_link', '_get_page_link', 'post_type_link') as $filter) |
| 26 |
add_filter($filter, array(&$this, 'post_link'), 20, 2); |
| 27 |
|
| 28 |
add_filter('term_link', array(&$this, 'term_link'), 20, 3); |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
/* |
| 33 |
* modifies post & page links |
| 34 |
* |
| 35 |
* @since 0.7 |
| 36 |
* |
| 37 |
* @param string $link post link |
| 38 |
* @param object|int $post post object or post ID |
| 39 |
* @return string modified post link |
| 40 |
*/ |
| 41 |
public function post_link($link, $post) { |
| 42 |
static $links = array(); |
| 43 |
|
| 44 |
if (isset($links[$link])) |
| 45 |
return $links[$link]; |
| 46 |
|
| 47 |
if ('post_type_link' == current_filter() && !in_array($post->post_type, $this->model->post_types)) |
| 48 |
return $links[$link] = $link; |
| 49 |
|
| 50 |
if ('_get_page_link' == current_filter()) // this filter uses the ID instead of the post object |
| 51 |
$post = get_post($post); |
| 52 |
|
| 53 |
// /!\ when post_status in not "publish", WP does not use pretty permalinks |
| 54 |
return $links[$link] = $post->post_status != 'publish' ? $link : $this->links_model->add_language_to_link($link, $this->model->get_post_language($post->ID)); |
| 55 |
} |
| 56 |
|
| 57 |
/* |
| 58 |
* modifies term link |
| 59 |
* |
| 60 |
* @since 0.7 |
| 61 |
* |
| 62 |
* @param string $link term link |
| 63 |
* @param object $post term object |
| 64 |
* @param string $tax taxonomy name |
| 65 |
* @return string modified term link |
| 66 |
*/ |
| 67 |
public function term_link($link, $term, $tax) { |
| 68 |
static $links = array(); |
| 69 |
|
| 70 |
if (isset($links[$link])) |
| 71 |
return $links[$link]; |
| 72 |
|
| 73 |
return $links[$link] = in_array($tax, $this->model->taxonomies) ? |
| 74 |
$this->links_model->add_language_to_link($link, $this->model->get_term_language($term->term_id)) : $link; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
|