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