| @@ -1,57 +1,157 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * @package Polylang | |
| 4 | - */ | |
| 5 | 2 | |
| 6 | -/** | |
| 7 | - * Manages links related functions | |
| 3 | +/* | |
| 4 | + * manages links filters needed on both frontend and admin | |
| 8 | 5 | * |
| 9 | 6 | * @since 1.2 |
| 10 | 7 | */ |
| 11 | 8 | class PLL_Links { |
| 12 | - /** | |
| 13 | - * Stores the plugin options. | |
| 9 | + public $links_model, $model, $options; | |
| 10 | + protected $_links; | |
| 11 | + | |
| 12 | + /* | |
| 13 | + * constructor | |
| 14 | 14 | * |
| 15 | - * @var array | |
| 15 | + * @since 1.2 | |
| 16 | + * | |
| 17 | + * @param object $polylang | |
| 16 | 18 | */ |
| 17 | - public $options; | |
| 19 | + public function __construct(&$polylang) { | |
| 20 | + $this->links_model = &$polylang->links_model; | |
| 21 | + $this->model = &$polylang->model; | |
| 22 | + $this->options = &$polylang->options; | |
| 18 | 23 | |
| 19 | - /** | |
| 20 | - * @var PLL_Model | |
| 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 | |
| 21 | 43 | */ |
| 22 | - public $model; | |
| 44 | + public function allowed_redirect_hosts($hosts) { | |
| 45 | + return array_unique(array_merge($hosts, $this->links_model->get_hosts())); | |
| 46 | + } | |
| 23 | 47 | |
| 24 | - /** | |
| 25 | - * Instance of a child class of PLL_Links_Model. | |
| 48 | + /* | |
| 49 | + * modifies post & page links | |
| 26 | 50 | * |
| 27 | - * @var PLL_Links_Model | |
| 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 | |
| 28 | 56 | */ |
| 29 | - public $links_model; | |
| 57 | + public function post_link($link, $post) { | |
| 58 | + if (isset($this->_links[$link])) | |
| 59 | + return $this->_links[$link]; | |
| 30 | 60 | |
| 31 | - /** | |
| 32 | - * Constructor | |
| 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 | |
| 33 | 73 | * |
| 34 | - * @since 1.2 | |
| 74 | + * @since 0.7 | |
| 35 | 75 | * |
| 36 | - * @param object $polylang | |
| 76 | + * @param string $link term link | |
| 77 | + * @param object $post term object | |
| 78 | + * @param string $tax taxonomy name | |
| 79 | + * @return string modified term link | |
| 37 | 80 | */ |
| 38 | - public function __construct( &$polylang ) { | |
| 39 | - $this->links_model = &$polylang->links_model; | |
| 40 | - $this->model = &$polylang->model; | |
| 41 | - $this->options = &$polylang->options; | |
| 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; | |
| 42 | 87 | } |
| 43 | 88 | |
| 44 | - /** | |
| 45 | - * Returns the home url in the requested language. | |
| 89 | + /* | |
| 90 | + * returns the home url in the requested language | |
| 46 | 91 | * |
| 47 | 92 | * @since 1.3 |
| 48 | 93 | * |
| 49 | - * @param PLL_Language|string $language The language. | |
| 50 | - * @param bool $is_search Optional, whether we need the home url for a search form, defaults to false. | |
| 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 | |
| 51 | 109 | * @return string |
| 52 | 110 | */ |
| 53 | - public function get_home_url( $language, $is_search = false ) { | |
| 54 | - $language = is_object( $language ) ? $language : $this->model->get_language( $language ); | |
| 55 | - return $is_search ? $language->search_url : $language->home_url; | |
| 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 | + } | |
| 56 | 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 | + } | |
| 57 | 156 | } |
| 157 | + | |