wpml.php
3.9 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
<?php
/**
* WPML: display correct flags on the overview screens
*/
class CPAC_WPML_COLUMN {
CONST COLUMN_NAME = 'icl_translations';
private $column;
function __construct( $post_type ) {
add_filter( "manage_{$post_type}_posts_columns", array( $this, 'store_wpml_column' ), 11 ); // prio just after WPML set's it's column
add_filter( "manage_edit-{$post_type}_columns", array( $this, 'replace_wpml_column' ), 101 ); // prio just after AC overwrite all columns
}
public function store_wpml_column( $columns ) {
if ( empty( $this->column ) && isset( $columns[ self::COLUMN_NAME ] ) ) {
$this->column = $columns[ self::COLUMN_NAME ];
}
return $columns;
}
public function replace_wpml_column( $columns ) {
if ( $this->column && isset( $columns[ self::COLUMN_NAME ] ) ) {
$columns[ self::COLUMN_NAME ] = $this->column;
}
return $columns;
}
}
/**
* WPML compatibility
*/
class CPAC_WPML {
function __construct() {
// load wpml columns in AC columns menu
add_action( 'cac/set_columns', array( $this, 'add_columns_to_settings_menu' ) );
// display correct flags on the overview screens
add_action( 'cac/loaded', array( $this, 'replace_flags' ) );
// enable the translation of the column labels
add_action( 'wp_loaded', array( $this, 'register_column_labels' ), 99 );
// enable the WPML translation of column headings
add_filter( 'cac/headings/label', array( $this, 'register_translated_label' ), 10, 4 );
// set WPML to be a columns screen for translation so that storage models are loaded
add_filter( 'cac/is_cac_screen', array( $this, 'is_cac_screen' ) );
}
public function replace_flags( $cac ) {
if ( ! class_exists( 'SitePress', false ) ) {
return;
}
if ( ! $cac->is_columns_screen() ) {
return;
}
$settings = get_option( 'icl_sitepress_settings' );
if ( ! isset( $settings['custom_posts_sync_option'] ) ) {
return;
}
$post_types = (array) $settings['custom_posts_sync_option'];
$post_types['post'] = 1;
$post_types['page'] = 1;
foreach ( $post_types as $post_type => $value ) {
if ( $value ) {
new CPAC_WPML_COLUMN( $post_type );
}
}
}
public function add_columns_to_settings_menu( $storage_model ) {
if ( ! class_exists( 'SitePress', false ) ) {
return;
}
if ( 'post' !== $storage_model->type ) {
return;
}
global $pagenow, $cpac;
// check if we are on the correct page or when a column is being refreshed by ajax.
if ( ( 'options-general.php' !== $pagenow ) && ( empty( $_POST['action'] ) || 'cpac_column_refresh' !== $_POST['action'] ) ) {
return;
}
// prevent PHP errors from SitePress
global $sitepress, $posts;
$posts = get_posts( array(
'post_type' => $storage_model->post_type,
'posts_per_page' => 1
));
add_filter( 'manage_' . $storage_model->post_type . 's_columns', array( $sitepress, 'add_posts_management_column' ) );
}
public function register_column_labels() {
global $cpac;
// dont load this unless required by WPML
if ( ! isset( $_GET['page'] ) || 'wpml-string-translation/menu/string-translation.php' !== $_GET['page'] ) {
return;
}
foreach ( $cpac->storage_models as $storage_model ) {
foreach ( $storage_model->get_stored_columns() as $column_name => $options ) {
icl_register_string( 'Admin Columns', $storage_model->key . '_' . $column_name, stripslashes( $options['label'] ) );
}
}
}
public function register_translated_label( $label, $column_name, $column_options, $storage_model ) {
if ( function_exists( 'icl_t' ) ) {
$name = $storage_model->key . '_' . $column_name;
$label = icl_t( 'Admin Columns', $name, $label );
}
return $label;
}
public function is_cac_screen( $is_columns_screen ) {
if ( isset( $_GET['page'] ) && 'wpml-string-translation/menu/string-translation.php' == $_GET['page'] ) {
return true;
}
return $is_columns_screen;
}
}
new CPAC_WPML;