PluginProbe
Polylang / 2.6.2
Polylang v2.6.2
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-filters-media.php

admin-filters-media.php in Polylang 2.6.2, at admin/admin-filters-media.php

120 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Manages filters and actions related to media on admin side
5 * Capability to edit / create media is checked before loading this class
6 *
7 * @since 1.2
8 */
9 class PLL_Admin_Filters_Media extends PLL_Admin_Filters_Post_Base {
10 public $posts;
11
12 /**
13 * Constructor: setups filters and actions
14 *
15 * @since 1.2
16 *
17 * @param object $polylang
18 */
19 public function __construct( &$polylang ) {
20 parent::__construct( $polylang );
21
22 $this->posts = &$polylang->posts;
23
24 // Adds the language field and translations tables in the 'Edit Media' panel
25 add_filter( 'attachment_fields_to_edit', array( $this, 'attachment_fields_to_edit' ), 10, 2 );
26
27 // Adds actions related to languages when creating, saving or deleting media
28 add_filter( 'attachment_fields_to_save', array( $this, 'save_media' ), 10, 2 );
29
30 // Creates a media translation
31 if ( isset( $_GET['action'], $_GET['new_lang'], $_GET['from_media'] ) && 'translate_media' === $_GET['action'] ) { // phpcs:ignore WordPress.Security.NonceVerification
32 add_action( 'admin_init', array( $this, 'translate_media' ) );
33 }
34 }
35
36 /**
37 * Adds the language field and translations tables in the 'Edit Media' panel
38 * Needs WP 3.5+
39 *
40 * @since 0.9
41 *
42 * @param array $fields list of form fields
43 * @param object $post
44 * @return array modified list of form fields
45 */
46 public function attachment_fields_to_edit( $fields, $post ) {
47 if ( 'post.php' == $GLOBALS['pagenow'] ) {
48 return $fields; // Don't add anything on edit media panel for WP 3.5+ since we have the metabox
49 }
50
51 $post_id = $post->ID;
52 $lang = $this->model->post->get_language( $post_id );
53
54 $dropdown = new PLL_Walker_Dropdown();
55 $fields['language'] = array(
56 'label' => __( 'Language', 'polylang' ),
57 'input' => 'html',
58 'html' => $dropdown->walk(
59 $this->model->get_languages_list(),
60 array(
61 'name' => sprintf( 'attachments[%d][language]', $post_id ),
62 'class' => 'media_lang_choice',
63 'selected' => $lang ? $lang->slug : '',
64 )
65 ),
66 );
67
68 return $fields;
69 }
70
71 /**
72 * Creates a media translation
73 *
74 * @since 0.9
75 */
76 public function translate_media() {
77 if ( isset( $_GET['from_media'], $_GET['new_lang'] ) ) {
78 // Security check
79 check_admin_referer( 'translate_media' );
80 $post_id = (int) $_GET['from_media'];
81
82 // Bails if the translations already exists
83 // See https://wordpress.org/support/topic/edit-translation-in-media-attachments?#post-7322303
84 // Or if the source media does not exist
85 if ( $this->model->post->get_translation( $post_id, sanitize_key( $_GET['new_lang'] ) ) || ! get_post( $post_id ) ) {
86 wp_safe_redirect( wp_get_referer() );
87 exit;
88 }
89
90 $tr_id = $this->posts->create_media_translation( $post_id, sanitize_key( $_GET['new_lang'] ) );
91 wp_safe_redirect( admin_url( sprintf( 'post.php?post=%d&action=edit', $tr_id ) ) ); // WP 3.5+
92 exit;
93 }
94 }
95
96 /**
97 * Called when a media is saved
98 * Saves language and translations
99 *
100 * @since 0.9
101 *
102 * @param array $post
103 * @param array $attachment
104 * @return array unmodified $post
105 */
106 public function save_media( $post, $attachment ) {
107 // Language is filled in attachment by the function applying the filter 'attachment_fields_to_save'
108 // All security checks have been done by functions applying this filter
109 if ( ! empty( $attachment['language'] ) ) {
110 $this->model->post->set_language( $post['ID'], $attachment['language'] );
111 }
112
113 if ( isset( $_POST['media_tr_lang'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
114 $this->save_translations( $post['ID'], array_map( 'absint', $_POST['media_tr_lang'] ) ); // phpcs:ignore WordPress.Security.NonceVerification
115 }
116
117 return $post;
118 }
119 }
120