PluginProbe
Polylang / 3.7
Polylang v3.7
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
polylang / admin / admin-links.php

admin-links.php in Polylang 3.7, at admin/admin-links.php

319 lines 9.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package Polylang
4 */
5
6 /**
7 * Manages links related functions.
8 *
9 * @since 1.8
10 */
11 class PLL_Admin_Links extends PLL_Links {
12
13 /**
14 * Returns the html markup for a new translation link.
15 *
16 * @since 2.6
17 *
18 * @param string $link The new translation link.
19 * @param PLL_Language $language The language of the new translation.
20 * @return string
21 */
22 protected function new_translation_link( $link, $language ) {
23 $str = '';
24
25 if ( $link ) {
26 /* translators: accessibility text, %s is a native language name */
27 $hint = sprintf( __( 'Add a translation in %s', 'polylang' ), $language->name );
28
29 $str = sprintf(
30 '<a href="%1$s" title="%2$s" class="pll_icon_add"><span class="screen-reader-text">%3$s</span></a>',
31 esc_url( $link ),
32 esc_attr( $hint ),
33 esc_html( $hint )
34 );
35 }
36
37 return $str;
38 }
39
40 /**
41 * Returns the html markup for a translation link.
42 *
43 * @since 2.6
44 *
45 * @param string $link The translation link.
46 * @param PLL_Language $language The language of the translation.
47 * @return string
48 */
49 public function edit_translation_link( $link, $language ) {
50 return $link ? sprintf(
51 '<a href="%1$s" class="pll_icon_edit"><span class="screen-reader-text">%2$s</span></a>',
52 esc_url( $link ),
53 /* translators: accessibility text, %s is a native language name */
54 esc_html( sprintf( __( 'Edit the translation in %s', 'polylang' ), $language->name ) )
55 ) : '';
56 }
57
58 /**
59 * Get the link to create a new post translation.
60 *
61 * @since 1.5
62 *
63 * @param int $post_id The source post id.
64 * @param PLL_Language $language The language of the new translation.
65 * @param string $context Optional. Defaults to 'display' which encodes '&' to '&amp;'.
66 * Otherwise, preserves '&'.
67 * @return string
68 */
69 public function get_new_post_translation_link( $post_id, $language, $context = 'display' ) {
70 $post_type = get_post_type( $post_id );
71 $post_type_object = get_post_type_object( get_post_type( $post_id ) );
72 if ( empty( $post_type_object ) || ! current_user_can( $post_type_object->cap->create_posts ) ) {
73 return '';
74 }
75
76 // Special case for the privacy policy page which is associated to a specific capability
77 if ( 'page' === $post_type_object->name && ! current_user_can( 'manage_privacy_options' ) ) {
78 $privacy_page = get_option( 'wp_page_for_privacy_policy' );
79 if ( $privacy_page && in_array( $post_id, $this->model->post->get_translations( $privacy_page ) ) ) {
80 return '';
81 }
82 }
83
84 if ( 'attachment' === $post_type ) {
85 $args = array(
86 'action' => 'translate_media',
87 'from_media' => $post_id,
88 'new_lang' => $language->slug,
89 );
90
91 $link = add_query_arg( $args, admin_url( 'admin.php' ) );
92
93 // Add nonce for media as we will directly publish a new attachment from a click on this link
94 if ( 'display' === $context ) {
95 $link = wp_nonce_url( $link, 'translate_media' );
96 } else {
97 $link = add_query_arg( '_wpnonce', wp_create_nonce( 'translate_media' ), $link );
98 }
99 } else {
100 $args = array(
101 'post_type' => $post_type,
102 'from_post' => $post_id,
103 'new_lang' => $language->slug,
104 );
105
106 $link = add_query_arg( $args, admin_url( 'post-new.php' ) );
107
108 if ( 'display' === $context ) {
109 $link = wp_nonce_url( $link, 'new-post-translation' );
110 } else {
111 $link = add_query_arg( '_wpnonce', wp_create_nonce( 'new-post-translation' ), $link );
112 }
113 }
114
115 /**
116 * Filters the new post translation link.
117 *
118 * @since 1.8
119 *
120 * @param string $link The new post translation link.
121 * @param PLL_Language $language The language of the new translation.
122 * @param int $post_id The source post id.
123 */
124 return apply_filters( 'pll_get_new_post_translation_link', $link, $language, $post_id );
125 }
126
127 /**
128 * Returns the html markup for a new post translation link.
129 *
130 * @since 1.8
131 *
132 * @param int $post_id The source post id.
133 * @param PLL_Language $language The language of the new translation.
134 * @return string
135 */
136 public function new_post_translation_link( $post_id, $language ) {
137 $link = $this->get_new_post_translation_link( $post_id, $language );
138 return $this->new_translation_link( $link, $language );
139 }
140
141 /**
142 * Returns the html markup for a post translation link.
143 *
144 * @since 1.4
145 *
146 * @param int $post_id The translation post id.
147 * @return string
148 */
149 public function edit_post_translation_link( $post_id ) {
150 $link = get_edit_post_link( $post_id );
151 $language = $this->model->post->get_language( $post_id );
152 return $this->edit_translation_link( $link, $language );
153 }
154
155 /**
156 * Get the link to create a new term translation.
157 *
158 * @since 1.5
159 *
160 * @param int $term_id Source term id.
161 * @param string $taxonomy Taxonomy name.
162 * @param string $post_type Post type name.
163 * @param PLL_Language $language The language of the new translation.
164 * @return string
165 */
166 public function get_new_term_translation_link( $term_id, $taxonomy, $post_type, $language ) {
167 $tax = get_taxonomy( $taxonomy );
168 if ( ! $tax || ! current_user_can( $tax->cap->edit_terms ) ) {
169 return '';
170 }
171
172 $args = array(
173 'taxonomy' => $taxonomy,
174 'post_type' => $post_type,
175 'from_tag' => $term_id,
176 'new_lang' => $language->slug,
177 );
178
179 $link = add_query_arg( $args, admin_url( 'edit-tags.php' ) );
180
181 /**
182 * Filters the new term translation link.
183 *
184 * @since 1.8
185 *
186 * @param string $link The new term translation link.
187 * @param PLL_Language $language The language of the new translation.
188 * @param int $term_id The source term id.
189 * @param string $taxonomy Taxonomy name.
190 * @param string $post_type Post type name.
191 */
192 return apply_filters( 'pll_get_new_term_translation_link', $link, $language, $term_id, $taxonomy, $post_type );
193 }
194
195 /**
196 * Returns the html markup for a new term translation.
197 *
198 * @since 1.8
199 *
200 * @param int $term_id Source term id.
201 * @param string $taxonomy Taxonomy name.
202 * @param string $post_type Post type name.
203 * @param PLL_Language $language The language of the new translation.
204 * @return string
205 */
206 public function new_term_translation_link( $term_id, $taxonomy, $post_type, $language ) {
207 $link = $this->get_new_term_translation_link( $term_id, $taxonomy, $post_type, $language );
208 return $this->new_translation_link( $link, $language );
209 }
210
211 /**
212 * Returns the html markup for a term translation link.
213 *
214 * @since 1.4
215 *
216 * @param int $term_id Translation term id.
217 * @param string $taxonomy Taxonomy name.
218 * @param string $post_type Post type name.
219 * @return string
220 */
221 public function edit_term_translation_link( $term_id, $taxonomy, $post_type ) {
222 $link = get_edit_term_link( $term_id, $taxonomy, $post_type );
223 $language = $this->model->term->get_language( $term_id );
224 return $this->edit_translation_link( $link, $language );
225 }
226
227 /**
228 * Returns some data (`from_post` and `new_lang`) from the current request.
229 *
230 * @since 3.7
231 *
232 * @param string $post_type A post type.
233 * @return array {
234 * @type WP_Post $from_post The source post.
235 * @type PLL_Language $new_lang The target language.
236 * }
237 *
238 * @phpstan-return array{}|array{from_post: WP_Post, new_lang: PLL_Language}|never
239 */
240 public function get_data_from_new_post_translation_request( string $post_type ): array {
241 if ( 'attachment' === $post_type ) {
242 return $this->get_data_from_new_media_translation_request();
243 }
244
245 if ( ! isset( $GLOBALS['pagenow'], $_GET['_wpnonce'], $_GET['from_post'], $_GET['new_lang'], $_GET['post_type'] ) ) {
246 return array();
247 }
248
249 if ( 'post-new.php' !== $GLOBALS['pagenow'] ) {
250 return array();
251 }
252
253 if ( empty( $post_type ) || $post_type !== $_GET['post_type'] || ! $this->model->is_translated_post_type( $post_type ) ) {
254 return array();
255 }
256
257 // Capability check already done in post-new.php.
258 check_admin_referer( 'new-post-translation' );
259 return $this->get_objects_from_new_post_translation_request( (int) $_GET['from_post'], sanitize_key( $_GET['new_lang'] ) );
260 }
261
262 /**
263 * Returns some data (`from_post` and `new_lang`) from the current request.
264 *
265 * @since 3.7
266 *
267 * @return array {
268 * @type WP_Post $from_post The source media.
269 * @type PLL_Language $new_lang The target language.
270 * }
271 *
272 * @phpstan-return array{}|array{from_post: WP_Post, new_lang: PLL_Language}|never
273 */
274 public function get_data_from_new_media_translation_request(): array {
275 if ( ! $this->options['media_support'] ) {
276 return array();
277 }
278
279 if ( ! isset( $_GET['action'], $_GET['_wpnonce'], $_GET['from_media'], $_GET['new_lang'] ) || 'translate_media' !== $_GET['action'] ) {
280 return array();
281 }
282
283 check_admin_referer( 'translate_media' );
284 return $this->get_objects_from_new_post_translation_request( (int) $_GET['from_media'], sanitize_key( $_GET['new_lang'] ) );
285 }
286
287 /**
288 * Returns the objects given the post ID and language slug provided in the new post translation request.
289 *
290 * @since 3.7
291 *
292 * @param int $post_id The original Post ID provided.
293 * @param string $lang_slug The new translation language provided
294 * @return array {
295 * @type WP_Post $from_post The source post.
296 * @type PLL_Language $new_lang The target language.
297 * }
298 *
299 * @phpstan-return array{}|array{from_post: WP_Post, new_lang: PLL_Language}|never
300 */
301 private function get_objects_from_new_post_translation_request( int $post_id, string $lang_slug ): array {
302 if ( $post_id <= 0 || empty( $lang_slug ) ) {
303 return array();
304 }
305
306 $post = get_post( $post_id );
307 $lang = $this->model->get_language( $lang_slug );
308
309 if ( empty( $post ) || empty( $lang ) ) {
310 return array();
311 }
312
313 return array(
314 'from_post' => $post,
315 'new_lang' => $lang,
316 );
317 }
318 }
319