| 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 |
foreach (array('post_link', '_get_page_link', 'post_type_link') as $filter) |
| 30 |
add_filter($filter, array(&$this, 'post_link'), 20, 2); |
| 31 |
|
| 32 |
add_filter('term_link', array(&$this, 'term_link'), 20, 3); |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
/* |
| 37 |
* adds our domains or subdomains to allowed hosts for safe redirection |
| 38 |
* |
| 39 |
* @since 1.4.3 |
| 40 |
* |
| 41 |
* @param array $hosts allowed hosts |
| 42 |
* @return array |
| 43 |
*/ |
| 44 |
public function allowed_redirect_hosts($hosts) { |
| 45 |
return array_unique(array_merge($hosts, $this->links_model->get_hosts())); |
| 46 |
} |
| 47 |
|
| 48 |
/* |
| 49 |
* modifies post & page links |
| 50 |
* |
| 51 |
* @since 0.7 |
| 52 |
* |
| 53 |
* @param string $link post link |
| 54 |
* @param object|int $post post object or post ID |
| 55 |
* @return string modified post link |
| 56 |
*/ |
| 57 |
public function post_link($link, $post) { |
| 58 |
if (isset($this->_links[$link])) |
| 59 |
return $this->_links[$link]; |
| 60 |
|
| 61 |
if ('post_type_link' == current_filter() && !$this->model->is_translated_post_type($post->post_type)) |
| 62 |
return $this->_links[$link] = $link; |
| 63 |
|
| 64 |
if ('_get_page_link' == current_filter()) // this filter uses the ID instead of the post object |
| 65 |
$post = get_post($post); |
| 66 |
|
| 67 |
// /!\ when post_status is not "publish", WP does not use pretty permalinks |
| 68 |
return $this->_links[$link] = $post->post_status != 'publish' ? $link : $this->links_model->add_language_to_link($link, $this->model->get_post_language($post->ID)); |
| 69 |
} |
| 70 |
|
| 71 |
/* |
| 72 |
* modifies term link |
| 73 |
* |
| 74 |
* @since 0.7 |
| 75 |
* |
| 76 |
* @param string $link term link |
| 77 |
* @param object $post term object |
| 78 |
* @param string $tax taxonomy name |
| 79 |
* @return string modified term link |
| 80 |
*/ |
| 81 |
public function term_link($link, $term, $tax) { |
| 82 |
if (isset($this->_links[$link])) |
| 83 |
return $this->_links[$link]; |
| 84 |
|
| 85 |
return $this->_links[$link] = $this->model->is_translated_taxonomy($tax) ? |
| 86 |
$this->links_model->add_language_to_link($link, $this->model->get_term_language($term->term_id)) : $link; |
| 87 |
} |
| 88 |
|
| 89 |
/* |
| 90 |
* returns the home url in the requested language |
| 91 |
* |
| 92 |
* @since 1.3 |
| 93 |
* |
| 94 |
* @param object|string $language |
| 95 |
* @param bool $is_search optional wether we need the home url for a search form, defaults to false |
| 96 |
*/ |
| 97 |
public function get_home_url($language, $is_search = false) { |
| 98 |
$language = is_object($language) ? $language : $this->model->get_language($language); |
| 99 |
return $is_search ? $language->search_url : $language->home_url; |
| 100 |
} |
| 101 |
|
| 102 |
/* |
| 103 |
* get the link to create a new post translation |
| 104 |
* |
| 105 |
* @since 1.5 |
| 106 |
* |
| 107 |
* @param int $post_id |
| 108 |
* @param object $language |
| 109 |
* @return string |
| 110 |
*/ |
| 111 |
public function get_new_post_translation_link($post_id, $language) { |
| 112 |
$post_type = get_post_type($post_id); |
| 113 |
|
| 114 |
if ('attachment' == $post_type) { |
| 115 |
$args = array( |
| 116 |
'action' => 'translate_media', |
| 117 |
'from_media' => $post_id, |
| 118 |
'new_lang' => $language->slug |
| 119 |
); |
| 120 |
|
| 121 |
// add nonce for media as we will directly publish a new attachment from a clic on this link |
| 122 |
return wp_nonce_url(add_query_arg($args, admin_url('admin.php')), 'translate_media'); |
| 123 |
} |
| 124 |
else { |
| 125 |
$args = array( |
| 126 |
'post_type' => $post_type, |
| 127 |
'from_post' => $post_id, |
| 128 |
'new_lang' => $language->slug |
| 129 |
); |
| 130 |
|
| 131 |
return add_query_arg($args, admin_url('post-new.php')); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
/* |
| 136 |
* get the link to create a new term translation |
| 137 |
* |
| 138 |
* @since 1.5 |
| 139 |
* |
| 140 |
* @param int $term_id |
| 141 |
* @param string $taxonomy |
| 142 |
* @param string $post_type |
| 143 |
* @param object $language |
| 144 |
* @return string |
| 145 |
*/ |
| 146 |
public function get_new_term_translation_link($term_id, $taxonomy, $post_type, $language) { |
| 147 |
$args = array( |
| 148 |
'taxonomy' => $taxonomy, |
| 149 |
'post_type' => $post_type, |
| 150 |
'from_tag' => $term_id, |
| 151 |
'new_lang' => $language->slug |
| 152 |
); |
| 153 |
|
| 154 |
return add_query_arg($args, admin_url('edit-tags.php')); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
|