PluginProbe
Polylang / 3.8.10
Polylang v3.8.10
3.8.10 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 All 234 releases
polylang / src / admin / admin-filters-media.php

admin-filters-media.php in Polylang 3.8.10, at src/admin/admin-filters-media.php

152 lines 4.3 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 use WP_Syntex\Polylang\Capabilities\Capabilities;
7
8 /**
9 * Manages filters and actions related to media on admin side
10 * Capability to edit / create media is checked before loading this class
11 *
12 * @since 1.2
13 */
14 class PLL_Admin_Filters_Media extends PLL_Admin_Filters_Post_Base {
15 /**
16 * @var PLL_CRUD_Posts|null
17 */
18 public $posts;
19
20 /**
21 * Constructor: setups filters and actions
22 *
23 * @since 1.2
24 *
25 * @param object $polylang The Polylang object.
26 */
27 public function __construct( &$polylang ) {
28 parent::__construct( $polylang );
29
30 $this->posts = &$polylang->posts;
31
32 // Adds the language field and translations tables in the 'Edit Media' panel.
33 add_filter( 'attachment_fields_to_edit', array( $this, 'attachment_fields_to_edit' ), 10, 2 );
34
35 // Adds actions related to languages when creating, saving or deleting media.
36 add_filter( 'attachment_fields_to_save', array( $this, 'save_media' ), 10, 2 );
37
38 // Maybe creates a media translation.
39 add_action( 'admin_init', array( $this, 'translate_media' ) );
40 }
41
42 /**
43 * Adds the language field and translations tables in the 'Edit Media' panel.
44 * Needs WP 3.5+
45 *
46 * @since 0.9
47 *
48 * @param array $fields List of form fields.
49 * @param WP_Post $post The attachment being edited.
50 * @return array Modified list of form fields.
51 */
52 public function attachment_fields_to_edit( $fields, $post ) {
53 if ( 'post.php' === $GLOBALS['pagenow'] ) {
54 return $fields; // Don't add anything on edit media panel since we have the metabox.
55 }
56
57 $post_id = $post->ID;
58 $lang = $this->model->post->get_language( $post_id );
59 $user = Capabilities::get_user();
60 $languages = $this->model->languages->filter( 'translator' )->get_list();
61
62 if ( empty( $lang ) ) {
63 // The media has no language: prepend an empty option to prevent displaying a wrong information.
64 array_unshift( $languages, (object) array( 'slug' => '', 'name' => '' ) );
65 } elseif ( ! $user->can_translate( $lang ) ) {
66 // The user cannot translate this language, so it isn't in `$languages` (the dropdown will be disabled).
67 if ( ! in_array( $lang, $languages, true ) ) {
68 array_unshift( $languages, $lang );
69 }
70 }
71
72 // Disable the dropdown if:
73 // - the media has no language and the user is a translator,
74 // - or the media has a language but the user is not allowed to translate it.
75 $disabled = empty( $lang ) ? $user->is_translator() : ! $user->can_translate( $lang );
76
77 $dropdown = new PLL_Walker_Dropdown();
78 $fields['language'] = array(
79 'label' => __( 'Language', 'polylang' ),
80 'input' => 'html',
81 'html' => $dropdown->walk(
82 $languages,
83 -1,
84 array(
85 'name' => sprintf( 'attachments[%d][language]', $post_id ),
86 'class' => 'media_lang_choice',
87 'selected' => $lang ? $lang->slug : '',
88 'disabled' => $disabled,
89 )
90 ),
91 );
92
93 return $fields;
94 }
95
96 /**
97 * Maybe creates a media translation.
98 *
99 * @since 0.9
100 *
101 * @return void
102 */
103 public function translate_media() {
104 $data = $this->links->get_data_from_new_media_translation_request();
105 if ( empty( $data ) ) {
106 return;
107 }
108
109 $post_id = $data['from_post']->ID;
110
111 // Bails if the translations already exists.
112 if ( $this->model->post->get_translation( $post_id, $data['new_lang'] ) ) {
113 wp_safe_redirect( wp_get_referer() );
114 exit;
115 }
116
117 $tr_id = $this->model->post->create_media_translation( $post_id, $data['new_lang'] );
118 wp_safe_redirect( admin_url( sprintf( 'post.php?post=%d&action=edit', $tr_id ) ) ); // WP 3.5+.
119 exit;
120 }
121
122 /**
123 * Called when a media is saved
124 * Saves language and translations
125 *
126 * @since 0.9
127 *
128 * @param array $post An array of post data.
129 * @param array $attachment An array of attachment metadata.
130 * @return array Unmodified $post
131 */
132 public function save_media( $post, $attachment ) {
133 // Language is filled in attachment by the function applying the filter 'attachment_fields_to_save'
134 // All security checks have been done by functions applying this filter
135 if ( empty( $attachment['language'] ) || ! current_user_can( 'edit_post', $post['ID'] ) ) {
136 return $post;
137 }
138
139 $language = $this->model->get_language( $attachment['language'] );
140
141 if ( empty( $language ) ) {
142 return $post;
143 }
144
145 Capabilities::get_user()->can_translate_or_die( $language );
146
147 $this->model->post->set_language( $post['ID'], $language );
148
149 return $post;
150 }
151 }
152