| @@ -222,5 +222,97 @@ | ||
| 222 | 222 | $link = get_edit_term_link( $term_id, $taxonomy, $post_type ); |
| 223 | 223 | $language = $this->model->term->get_language( $term_id ); |
| 224 | 224 | return $this->edit_translation_link( $link, $language ); |
| 225 | 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 | + } | |
| 226 | 318 | } |