PluginProbe
Polylang / 1.5
Polylang v1.5
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
← All changes | include/links.php +131 -13 2.91.5 View file →
@@ -1,39 +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 9 public $links_model, $model, $options;
10 + protected $_links;
13 11
14 - /**
15 - * Constructor
12 + /*
13 + * constructor
16 14 *
17 15 * @since 1.2
18 16 *
19 17 * @param object $polylang
20 18 */
21 - public function __construct( &$polylang ) {
19 + public function __construct(&$polylang) {
22 20 $this->links_model = &$polylang->links_model;
23 21 $this->model = &$polylang->model;
24 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 + }
25 34 }
26 35
27 - /**
28 - * Returns the home url in the requested language
36 + /*
37 + * adds our domains or subdomains to allowed hosts for safe redirection
29 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 + *
30 92 * @since 1.3
31 93 *
32 94 * @param object|string $language
33 - * @param bool $is_search optional whether we need the home url for a search form, defaults to false
95 + * @param bool $is_search optional wether we need the home url for a search form, defaults to false
34 96 */
35 - public function get_home_url( $language, $is_search = false ) {
36 - $language = is_object( $language ) ? $language : $this->model->get_language( $language );
97 + public function get_home_url($language, $is_search = false) {
98 + $language = is_object($language) ? $language : $this->model->get_language($language);
37 99 return $is_search ? $language->search_url : $language->home_url;
38 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 + }
39 156 }
157 +