class-wpml-custom-columns.php
5.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
use WPML\Element\API\TranslationsRepository;
/**
* Class WPML_Custom_Columns
*/
class WPML_Custom_Columns implements IWPML_Action {
const COLUMN_KEY = 'icl_translations';
const CUSTOM_COLUMNS_PRIORITY = 1010;
/**
* @param SitePress $sitepress
*/
private $sitepress;
/**
* @var WPML_Post_Status_Display
*/
public $post_status_display;
public function __construct( SitePress $sitepress ) {
$this->sitepress = $sitepress;
}
/**
* @param array $columns
*
* @return array
*/
public function add_posts_management_column( $columns ) {
$new_columns = $columns;
if ( 'trash' === get_query_var( 'post_status' ) ) {
return $columns;
}
$flags_column = $this->get_flags_column();
if ( $flags_column ) {
$new_columns = [];
foreach ( $columns as $column_key => $column_content ) {
$new_columns[ $column_key ] = $column_content;
if ( ( 'title' === $column_key || 'name' === $column_key ) && ! isset( $new_columns[ self::COLUMN_KEY ] ) ) {
$new_columns[ self::COLUMN_KEY ] = $flags_column;
}
}
}
return $new_columns;
}
public function get_flags_column() {
$active_languages = $this->get_filtered_active_languages();
if ( count( $active_languages ) <= 1 ) {
return '';
}
$current_language = $this->sitepress->get_current_language();
unset( $active_languages[ $current_language ] );
if ( ! count( $active_languages ) ) {
return '';
}
$flags_column = '<span class="screen-reader-text">' . esc_html__( 'Languages', 'sitepress' ) . '</span>';
foreach ( $active_languages as $language_data ) {
$flags_column .= $this->get_flag_img( $language_data );
}
return $flags_column;
}
private function get_flag_img( $language_data ) {
$url = $this->sitepress->get_flag_url( $language_data['code'] );
if ( $url !== '' ) {
return '<img src="' . esc_url( $url ) .
'" width="18" height="12" alt="' . esc_attr( $language_data['display_name'] ) . '" title="' .
esc_attr( $language_data['display_name'] ) . '" style="margin:2px" />';
} else {
return $language_data['code'];
}
}
/**
* Add posts management column.
*
* @param string $column_name
* @param int|null $post_id
*/
public function add_content_for_posts_management_column( $column_name, $post_id = null ) {
global $post;
if ( ! $post_id ) {
$post_id = $post->ID;
}
if ( self::COLUMN_KEY !== $column_name ) {
return;
}
$active_languages = $this->get_filtered_active_languages();
if ( null === $this->post_status_display ) {
$this->post_status_display = new WPML_Post_Status_Display( $active_languages );
}
unset( $active_languages[ $this->sitepress->get_current_language() ] );
foreach ( $active_languages as $language_data ) {
$icon_html = $this->post_status_display->get_status_html( $post_id, $language_data['code'] );
echo $icon_html;
}
}
/**
* Check translation management column screen option.
*
* @param string $post_type Current post type.
*
* @return bool
*/
public function show_management_column_content( $post_type ) {
$user = get_current_user_id();
$hidden_columns = get_user_meta( $user, 'manageedit-' . $post_type . 'columnshidden', true );
if ( '' === $hidden_columns ) {
$is_visible = (bool) apply_filters( 'wpml_hide_management_column', true, $post_type );
if ( false === $is_visible ) {
update_user_meta( $user, 'manageedit-' . $post_type . 'columnshidden', array( self::COLUMN_KEY ) );
}
return $is_visible;
}
return ! is_array( $hidden_columns ) || ! in_array( self::COLUMN_KEY, $hidden_columns, true );
}
/**
* Get list of active languages.
*
* @return array
*/
private function get_filtered_active_languages() {
$active_languages = $this->sitepress->get_active_languages();
return apply_filters( 'wpml_active_languages_access', $active_languages, array( 'action' => 'edit' ) );
}
public function add_hooks() {
add_action(
'admin_init',
array(
$this,
'add_custom_columns_hooks',
),
self::CUSTOM_COLUMNS_PRIORITY
); // accommodate Types init@999
}
/**
* Add custom columns hooks.
*/
public function add_custom_columns_hooks() {
$post_type = isset( $_REQUEST['post_type'] ) ? $_REQUEST['post_type'] : 'post';
if (
$post_type && $this->has_custom_columns()
&& array_key_exists( $post_type, $this->sitepress->get_translatable_documents() )
) {
add_filter(
'manage_' . $post_type . '_posts_columns',
array(
$this,
'add_posts_management_column',
)
);
$show_management_column_content = $this->show_management_column_content( $post_type );
if ( $show_management_column_content ) {
if ( is_post_type_hierarchical( $post_type ) ) {
add_action(
'manage_pages_custom_column',
array(
$this,
'add_content_for_posts_management_column',
)
);
}
add_action(
'manage_posts_custom_column',
array(
$this,
'add_content_for_posts_management_column',
)
);
add_action( 'manage_posts_custom_column', [ $this, 'preloadTranslationData' ], 1, 0 );
add_action( 'manage_pages_custom_column', [ $this, 'preloadTranslationData' ], 1, 0 );
}
}
}
public function preloadTranslationData() {
static $loaded = false;
if ( $loaded ) {
return;
}
global $wp_query;
$posts = $wp_query->posts;
if ( is_array( $posts ) ) {
TranslationsRepository::preloadForPosts( $posts );
$loaded = true;
}
}
/**
* Check if we need to add custom columns on page.
*
* @return bool
*/
private function has_custom_columns() {
global $pagenow;
if ( 'edit.php' === $pagenow
|| 'edit-pages.php' === $pagenow
|| (
'admin-ajax.php' === $pagenow
&& (
( array_key_exists( 'action', $_POST ) && 'inline-save' === filter_var( $_POST['action'] ) )
|| ( array_key_exists( 'action', $_GET ) && 'fetch-list' === filter_var( $_GET['action'] )
)
)
)
) {
return true;
}
return false;
}
}