| @@ -9,8 +9,11 @@ | ||
| 9 | 9 | * |
| 10 | 10 | * @since 1.2 |
| 11 | 11 | */ |
| 12 | 12 | class PLL_Admin_Filters_Media extends PLL_Admin_Filters_Post_Base { |
| 13 | + /** | |
| 14 | + * @var PLL_CRUD_Posts|null | |
| 15 | + */ | |
| 13 | 16 | public $posts; |
| 14 | 17 | |
| 15 | 18 | /** |
| 16 | 19 | * Constructor: setups filters and actions |
| @@ -16,9 +19,9 @@ | ||
| 16 | 19 | * Constructor: setups filters and actions |
| 17 | 20 | * |
| 18 | 21 | * @since 1.2 |
| 19 | 22 | * |
| 20 | - * @param object $polylang | |
| 23 | + * @param object $polylang The Polylang object. | |
| 21 | 24 | */ |
| 22 | 25 | public function __construct( &$polylang ) { |
| 23 | 26 | parent::__construct( $polylang ); |
| 24 | 27 | |
| @@ -23,33 +26,31 @@ | ||
| 23 | 26 | parent::__construct( $polylang ); |
| 24 | 27 | |
| 25 | 28 | $this->posts = &$polylang->posts; |
| 26 | 29 | |
| 27 | - // Adds the language field and translations tables in the 'Edit Media' panel | |
| 30 | + // Adds the language field and translations tables in the 'Edit Media' panel. | |
| 28 | 31 | add_filter( 'attachment_fields_to_edit', array( $this, 'attachment_fields_to_edit' ), 10, 2 ); |
| 29 | 32 | |
| 30 | - // Adds actions related to languages when creating, saving or deleting media | |
| 33 | + // Adds actions related to languages when creating, saving or deleting media. | |
| 31 | 34 | add_filter( 'attachment_fields_to_save', array( $this, 'save_media' ), 10, 2 ); |
| 32 | 35 | |
| 33 | - // Creates a media translation | |
| 34 | - if ( isset( $_GET['action'], $_GET['new_lang'], $_GET['from_media'] ) && 'translate_media' === $_GET['action'] ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 35 | - add_action( 'admin_init', array( $this, 'translate_media' ) ); | |
| 36 | - } | |
| 36 | + // Maybe creates a media translation. | |
| 37 | + add_action( 'admin_init', array( $this, 'translate_media' ) ); | |
| 37 | 38 | } |
| 38 | 39 | |
| 39 | 40 | /** |
| 40 | - * Adds the language field and translations tables in the 'Edit Media' panel | |
| 41 | + * Adds the language field and translations tables in the 'Edit Media' panel. | |
| 41 | 42 | * Needs WP 3.5+ |
| 42 | 43 | * |
| 43 | 44 | * @since 0.9 |
| 44 | 45 | * |
| 45 | - * @param array $fields list of form fields | |
| 46 | - * @param object $post | |
| 47 | - * @return array modified list of form fields | |
| 46 | + * @param array $fields List of form fields. | |
| 47 | + * @param WP_Post $post The attachment being edited. | |
| 48 | + * @return array Modified list of form fields. | |
| 48 | 49 | */ |
| 49 | 50 | public function attachment_fields_to_edit( $fields, $post ) { |
| 50 | - if ( 'post.php' == $GLOBALS['pagenow'] ) { | |
| 51 | - return $fields; // Don't add anything on edit media panel for WP 3.5+ since we have the metabox | |
| 51 | + if ( 'post.php' === $GLOBALS['pagenow'] ) { | |
| 52 | + return $fields; // Don't add anything on edit media panel for WP 3.5+ since we have the metabox. | |
| 52 | 53 | } |
| 53 | 54 | |
| 54 | 55 | $post_id = $post->ID; |
| 55 | 56 | $lang = $this->model->post->get_language( $post_id ); |
| @@ -72,30 +73,31 @@ | ||
| 72 | 73 | return $fields; |
| 73 | 74 | } |
| 74 | 75 | |
| 75 | 76 | /** |
| 76 | - * Creates a media translation | |
| 77 | + * Maybe creates a media translation. | |
| 77 | 78 | * |
| 78 | 79 | * @since 0.9 |
| 80 | + * | |
| 81 | + * @return void | |
| 79 | 82 | */ |
| 80 | 83 | public function translate_media() { |
| 81 | - if ( isset( $_GET['from_media'], $_GET['new_lang'] ) ) { | |
| 82 | - // Security check | |
| 83 | - check_admin_referer( 'translate_media' ); | |
| 84 | - $post_id = (int) $_GET['from_media']; | |
| 84 | + $data = $this->links->get_data_from_new_media_translation_request(); | |
| 85 | + if ( empty( $data ) ) { | |
| 86 | + return; | |
| 87 | + } | |
| 85 | 88 | |
| 86 | - // Bails if the translations already exists | |
| 87 | - // See https://wordpress.org/support/topic/edit-translation-in-media-attachments?#post-7322303 | |
| 88 | - // Or if the source media does not exist | |
| 89 | - if ( $this->model->post->get_translation( $post_id, sanitize_key( $_GET['new_lang'] ) ) || ! get_post( $post_id ) ) { | |
| 90 | - wp_safe_redirect( wp_get_referer() ); | |
| 91 | - exit; | |
| 92 | - } | |
| 89 | + $post_id = $data['from_post']->ID; | |
| 93 | 90 | |
| 94 | - $tr_id = $this->posts->create_media_translation( $post_id, sanitize_key( $_GET['new_lang'] ) ); | |
| 95 | - wp_safe_redirect( admin_url( sprintf( 'post.php?post=%d&action=edit', $tr_id ) ) ); // WP 3.5+ | |
| 91 | + // Bails if the translations already exists. | |
| 92 | + if ( $this->model->post->get_translation( $post_id, $data['new_lang'] ) ) { | |
| 93 | + wp_safe_redirect( wp_get_referer() ); | |
| 96 | 94 | exit; |
| 97 | 95 | } |
| 96 | + | |
| 97 | + $tr_id = $this->model->post->create_media_translation( $post_id, $data['new_lang'] ); | |
| 98 | + wp_safe_redirect( admin_url( sprintf( 'post.php?post=%d&action=edit', $tr_id ) ) ); // WP 3.5+. | |
| 99 | + exit; | |
| 98 | 100 | } |
| 99 | 101 | |
| 100 | 102 | /** |
| 101 | 103 | * Called when a media is saved |
| @@ -102,22 +104,26 @@ | ||
| 102 | 104 | * Saves language and translations |
| 103 | 105 | * |
| 104 | 106 | * @since 0.9 |
| 105 | 107 | * |
| 106 | - * @param array $post | |
| 107 | - * @param array $attachment | |
| 108 | - * @return array unmodified $post | |
| 108 | + * @param array $post An array of post data. | |
| 109 | + * @param array $attachment An array of attachment metadata. | |
| 110 | + * @return array Unmodified $post | |
| 109 | 111 | */ |
| 110 | 112 | public function save_media( $post, $attachment ) { |
| 111 | 113 | // Language is filled in attachment by the function applying the filter 'attachment_fields_to_save' |
| 112 | 114 | // All security checks have been done by functions applying this filter |
| 113 | - if ( ! empty( $attachment['language'] ) ) { | |
| 114 | - $this->model->post->set_language( $post['ID'], $attachment['language'] ); | |
| 115 | + if ( empty( $attachment['language'] ) || ! current_user_can( 'edit_post', $post['ID'] ) ) { | |
| 116 | + return $post; | |
| 115 | 117 | } |
| 116 | 118 | |
| 117 | - if ( isset( $_POST['media_tr_lang'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 118 | - $this->save_translations( $post['ID'], array_map( 'absint', $_POST['media_tr_lang'] ) ); // phpcs:ignore WordPress.Security.NonceVerification | |
| 119 | + $language = $this->model->get_language( $attachment['language'] ); | |
| 120 | + | |
| 121 | + if ( empty( $language ) ) { | |
| 122 | + return $post; | |
| 119 | 123 | } |
| 124 | + | |
| 125 | + $this->model->post->set_language( $post['ID'], $language ); | |
| 120 | 126 | |
| 121 | 127 | return $post; |
| 122 | 128 | } |
| 123 | 129 | } |