| @@ -1,8 +1,11 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * @package Polylang | |
| 4 | + */ | |
| 2 | 5 | |
| 3 | 6 | /** |
| 4 | - * Manages links related functions | |
| 7 | + * Manages links related functions. | |
| 5 | 8 | * |
| 6 | 9 | * @since 1.8 |
| 7 | 10 | */ |
| 8 | 11 | class PLL_Admin_Links extends PLL_Links { |
| @@ -7,14 +10,14 @@ | ||
| 7 | 10 | */ |
| 8 | 11 | class PLL_Admin_Links extends PLL_Links { |
| 9 | 12 | |
| 10 | 13 | /** |
| 11 | - * Returns html markup for a new translation link | |
| 14 | + * Returns the html markup for a new translation link. | |
| 12 | 15 | * |
| 13 | 16 | * @since 2.6 |
| 14 | 17 | * |
| 15 | - * @param string $link The new translation link. | |
| 16 | - * @param object $language The language of the new translation. | |
| 18 | + * @param string $link The new translation link. | |
| 19 | + * @param PLL_Language $language The language of the new translation. | |
| 17 | 20 | * @return string |
| 18 | 21 | */ |
| 19 | 22 | protected function new_translation_link( $link, $language ) { |
| 20 | 23 | $str = ''; |
| @@ -34,14 +37,14 @@ | ||
| 34 | 37 | return $str; |
| 35 | 38 | } |
| 36 | 39 | |
| 37 | 40 | /** |
| 38 | - * Returns html markup for a translation link | |
| 41 | + * Returns the html markup for a translation link. | |
| 39 | 42 | * |
| 40 | 43 | * @since 2.6 |
| 41 | 44 | * |
| 42 | - * @param string $link The translation link. | |
| 43 | - * @param object $language The language of the translation. | |
| 45 | + * @param string $link The translation link. | |
| 46 | + * @param PLL_Language $language The language of the translation. | |
| 44 | 47 | * @return string |
| 45 | 48 | */ |
| 46 | 49 | public function edit_translation_link( $link, $language ) { |
| 47 | 50 | return $link ? sprintf( |
| @@ -52,22 +55,22 @@ | ||
| 52 | 55 | ) : ''; |
| 53 | 56 | } |
| 54 | 57 | |
| 55 | 58 | /** |
| 56 | - * Get the link to create a new post translation | |
| 59 | + * Get the link to create a new post translation. | |
| 57 | 60 | * |
| 58 | 61 | * @since 1.5 |
| 59 | 62 | * |
| 60 | - * @param int $post_id The source post id. | |
| 61 | - * @param object $language The language of the new translation. | |
| 62 | - * @param string $context Optional. Defaults to 'display' which encodes '&' to '&'. | |
| 63 | - * Otherwise, preserves '&'. | |
| 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 '&'. | |
| 66 | + * Otherwise, preserves '&'. | |
| 64 | 67 | * @return string |
| 65 | 68 | */ |
| 66 | 69 | public function get_new_post_translation_link( $post_id, $language, $context = 'display' ) { |
| 67 | 70 | $post_type = get_post_type( $post_id ); |
| 68 | 71 | $post_type_object = get_post_type_object( get_post_type( $post_id ) ); |
| 69 | - if ( ! current_user_can( $post_type_object->cap->create_posts ) ) { | |
| 72 | + if ( empty( $post_type_object ) || ! current_user_can( $post_type_object->cap->create_posts ) ) { | |
| 70 | 73 | return ''; |
| 71 | 74 | } |
| 72 | 75 | |
| 73 | 76 | // Special case for the privacy policy page which is associated to a specific capability |
| @@ -109,26 +112,26 @@ | ||
| 109 | 112 | } |
| 110 | 113 | } |
| 111 | 114 | |
| 112 | 115 | /** |
| 113 | - * Filter the new post translation link | |
| 116 | + * Filters the new post translation link. | |
| 114 | 117 | * |
| 115 | 118 | * @since 1.8 |
| 116 | 119 | * |
| 117 | - * @param string $link the new post translation link | |
| 118 | - * @param object $language the language of the new translation | |
| 119 | - * @param int $post_id the source post id | |
| 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. | |
| 120 | 123 | */ |
| 121 | 124 | return apply_filters( 'pll_get_new_post_translation_link', $link, $language, $post_id ); |
| 122 | 125 | } |
| 123 | 126 | |
| 124 | 127 | /** |
| 125 | - * Returns html markup for a new post translation link | |
| 128 | + * Returns the html markup for a new post translation link. | |
| 126 | 129 | * |
| 127 | 130 | * @since 1.8 |
| 128 | 131 | * |
| 129 | - * @param int $post_id | |
| 130 | - * @param object $language | |
| 132 | + * @param int $post_id The source post id. | |
| 133 | + * @param PLL_Language $language The language of the new translation. | |
| 131 | 134 | * @return string |
| 132 | 135 | */ |
| 133 | 136 | public function new_post_translation_link( $post_id, $language ) { |
| 134 | 137 | $link = $this->get_new_post_translation_link( $post_id, $language ); |
| @@ -135,13 +138,13 @@ | ||
| 135 | 138 | return $this->new_translation_link( $link, $language ); |
| 136 | 139 | } |
| 137 | 140 | |
| 138 | 141 | /** |
| 139 | - * Returns html markup for a translation link | |
| 142 | + * Returns the html markup for a post translation link. | |
| 140 | 143 | * |
| 141 | 144 | * @since 1.4 |
| 142 | 145 | * |
| 143 | - * @param int $post_id translation post id | |
| 146 | + * @param int $post_id The translation post id. | |
| 144 | 147 | * @return string |
| 145 | 148 | */ |
| 146 | 149 | public function edit_post_translation_link( $post_id ) { |
| 147 | 150 | $link = get_edit_post_link( $post_id ); |
| @@ -149,16 +152,16 @@ | ||
| 149 | 152 | return $this->edit_translation_link( $link, $language ); |
| 150 | 153 | } |
| 151 | 154 | |
| 152 | 155 | /** |
| 153 | - * Get the link to create a new term translation | |
| 156 | + * Get the link to create a new term translation. | |
| 154 | 157 | * |
| 155 | 158 | * @since 1.5 |
| 156 | 159 | * |
| 157 | - * @param int $term_id | |
| 158 | - * @param string $taxonomy | |
| 159 | - * @param string $post_type | |
| 160 | - * @param object $language | |
| 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. | |
| 161 | 164 | * @return string |
| 162 | 165 | */ |
| 163 | 166 | public function get_new_term_translation_link( $term_id, $taxonomy, $post_type, $language ) { |
| 164 | 167 | $tax = get_taxonomy( $taxonomy ); |
| @@ -175,30 +178,30 @@ | ||
| 175 | 178 | |
| 176 | 179 | $link = add_query_arg( $args, admin_url( 'edit-tags.php' ) ); |
| 177 | 180 | |
| 178 | 181 | /** |
| 179 | - * Filter the new term translation link | |
| 182 | + * Filters the new term translation link. | |
| 180 | 183 | * |
| 181 | 184 | * @since 1.8 |
| 182 | 185 | * |
| 183 | - * @param string $link the new term translation link | |
| 184 | - * @param object $language the language of the new translation | |
| 185 | - * @param int $term_id the source term id | |
| 186 | - * @param string $taxonomy | |
| 187 | - * @param string $post_type | |
| 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. | |
| 188 | 191 | */ |
| 189 | 192 | return apply_filters( 'pll_get_new_term_translation_link', $link, $language, $term_id, $taxonomy, $post_type ); |
| 190 | 193 | } |
| 191 | 194 | |
| 192 | 195 | /** |
| 193 | - * Returns html markup for a new term translation | |
| 196 | + * Returns the html markup for a new term translation. | |
| 194 | 197 | * |
| 195 | 198 | * @since 1.8 |
| 196 | 199 | * |
| 197 | - * @param int $term_id | |
| 198 | - * @param string $taxonomy | |
| 199 | - * @param string $post_type | |
| 200 | - * @param object $language | |
| 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. | |
| 201 | 204 | * @return string |
| 202 | 205 | */ |
| 203 | 206 | public function new_term_translation_link( $term_id, $taxonomy, $post_type, $language ) { |
| 204 | 207 | $link = $this->get_new_term_translation_link( $term_id, $taxonomy, $post_type, $language ); |
| @@ -205,15 +208,15 @@ | ||
| 205 | 208 | return $this->new_translation_link( $link, $language ); |
| 206 | 209 | } |
| 207 | 210 | |
| 208 | 211 | /** |
| 209 | - * Returns html markup for a term translation link | |
| 212 | + * Returns the html markup for a term translation link. | |
| 210 | 213 | * |
| 211 | 214 | * @since 1.4 |
| 212 | 215 | * |
| 213 | - * @param object $term_id translation term id | |
| 214 | - * @param string $taxonomy | |
| 215 | - * @param string $post_type | |
| 216 | + * @param int $term_id Translation term id. | |
| 217 | + * @param string $taxonomy Taxonomy name. | |
| 218 | + * @param string $post_type Post type name. | |
| 216 | 219 | * @return string |
| 217 | 220 | */ |
| 218 | 221 | public function edit_term_translation_link( $term_id, $taxonomy, $post_type ) { |
| 219 | 222 | $link = get_edit_term_link( $term_id, $taxonomy, $post_type ); |
| @@ -218,6 +221,98 @@ | ||
| 218 | 221 | public function edit_term_translation_link( $term_id, $taxonomy, $post_type ) { |
| 219 | 222 | $link = get_edit_term_link( $term_id, $taxonomy, $post_type ); |
| 220 | 223 | $language = $this->model->term->get_language( $term_id ); |
| 221 | 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 | + ); | |
| 222 | 317 | } |
| 223 | 318 | } |