PluginProbe
Polylang / 2.8.2
Polylang v2.8.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.8.2, at admin/admin-filters-media.php

124 lines 3.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 filters and actions related to media on admin side
8 * Capability to edit / create media is checked before loading this class
9 *
10 * @since 1.2
11 */
12 class PLL_Admin_Filters_Media extends PLL_Admin_Filters_Post_Base {
13 public $posts;
14
15 /**
16 * Constructor: setups filters and actions
17 *
18 * @since 1.2
19 *
20 * @param object $polylang
21 */
22 public function __construct( &$polylang ) {
23 parent::__construct( $polylang );
24
25 $this->posts = &$polylang->posts;
26
27 // Adds the language field and translations tables in the 'Edit Media' panel
28 add_filter( 'attachment_fields_to_edit', array( $this, 'attachment_fields_to_edit' ), 10, 2 );
29
30 // Adds actions related to languages when creating, saving or deleting media
31 add_filter( 'attachment_fields_to_save', array( $this, 'save_media' ), 10, 2 );
32
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 }
37 }
38
39 /**
40 * Adds the language field and translations tables in the 'Edit Media' panel
41 * Needs WP 3.5+
42 *
43 * @since 0.9
44 *
45 * @param array $fields list of form fields
46 * @param object $post
47 * @return array modified list of form fields
48 */
49 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
52 }
53
54 $post_id = $post->ID;
55 $lang = $this->model->post->get_language( $post_id );
56
57 $dropdown = new PLL_Walker_Dropdown();
58 $fields['language'] = array(
59 'label' => __( 'Language', 'polylang' ),
60 'input' => 'html',
61 'html' => $dropdown->walk(
62 $this->model->get_languages_list(),
63 -1,
64 array(
65 'name' => sprintf( 'attachments[%d][language]', $post_id ),
66 'class' => 'media_lang_choice',
67 'selected' => $lang ? $lang->slug : '',
68 )
69 ),
70 );
71
72 return $fields;
73 }
74
75 /**
76 * Creates a media translation
77 *
78 * @since 0.9
79 */
80 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'];
85
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 }
93
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+
96 exit;
97 }
98 }
99
100 /**
101 * Called when a media is saved
102 * Saves language and translations
103 *
104 * @since 0.9
105 *
106 * @param array $post
107 * @param array $attachment
108 * @return array unmodified $post
109 */
110 public function save_media( $post, $attachment ) {
111 // Language is filled in attachment by the function applying the filter 'attachment_fields_to_save'
112 // 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 }
116
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 }
120
121 return $post;
122 }
123 }
124