wpml-tm-menus.class.php
4.09 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
<?php
use WPML\API\Sanitize;
if ( Sanitize::stringProp( 'sm', $_GET ) === 'basket' ) {
add_action( 'admin_enqueue_scripts', array( 'SitePress_Table_Basket', 'enqueue_js' ) );
}
abstract class WPML_TM_Menus {
protected $post_types;
protected $tab_items;
private $base_target_url;
protected $current_shown_item;
/** @var WPML_UI_Screen_Options_Pagination|null $dashboard_pagination */
protected $dashboard_pagination;
function __construct() {
$this->current_shown_item = isset( $_GET['sm'] ) ? $_GET['sm'] : $this->get_default_tab();
$this->base_target_url = dirname( __FILE__ );
}
public function display_main( WPML_UI_Screen_Options_Pagination $dashboard_pagination = null ) {
$this->dashboard_pagination = $dashboard_pagination;
if ( true !== apply_filters( 'wpml_tm_lock_ui', false ) ) {
$this->render_main();
}
}
abstract protected function render_main();
private function build_tab_item_target_url( $target ) {
return $this->base_target_url . $target;
}
abstract protected function build_tab_items();
/**
* @return string
*/
private function get_current_shown_item() {
return $this->current_shown_item;
}
private function build_tabs() {
$tm_sub_menu = $this->get_current_shown_item();
foreach ( $this->tab_items as $id => $tab_item ) {
if ( ! isset( $tab_item['caption'] ) ) {
continue;
}
if ( isset( $tab_item['visible'] ) && ! $tab_item['visible'] ) {
continue;
}
if ( ! isset( $tab_item['target'] ) && ! isset( $tab_item['callback'] ) ) {
continue;
}
$caption = $tab_item['caption'];
if ( ! $this->current_user_can_access( $tab_item ) ) {
continue;
}
$classes = array(
'nav-tab',
'nav-tab-' . $id,
);
if ( $tm_sub_menu === $id ) {
$classes[] = 'nav-tab-active';
}
$class = implode( ' ', $classes );
$href = 'admin.php?page=' . WPML_TM_FOLDER . $this->get_page_slug() . '&sm=' . $id;
?>
<a class="<?php echo esc_attr( $class ); ?>" href="<?php echo esc_attr( $href ); ?>">
<?php echo $caption; ?>
</a>
<?php
}
}
private function build_content() {
$tm_sub_menu = $this->get_current_shown_item();
foreach ( $this->tab_items as $id => $tab_item ) {
if ( ! isset( $tab_item['caption'] ) ) {
continue;
}
if ( ! isset( $tab_item['target'] ) && ! isset( $tab_item['callback'] ) ) {
continue;
}
if ( $tm_sub_menu == $id ) {
if ( $this->current_user_can_access( $tab_item ) ) {
if ( isset( $tab_item['target'] ) ) {
$target = $tab_item['target'];
/** @noinspection PhpIncludeInspection */
include_once $this->build_tab_item_target_url( $target );
}
if ( isset( $tab_item['callback'] ) ) {
$callback = $tab_item['callback'];
call_user_func( $callback );
}
}
}
}
do_action( 'icl_tm_menu_' . $tm_sub_menu );
}
protected function render_items() {
if ( $this->tab_items ) {
?>
<div class="icl-translation-management-menu wpml-tabs">
<?php
$this->build_tabs();
?>
</div>
<div class="icl_tm_wrap wpml-wrap">
<?php
$this->build_content();
?>
</div>
<?php
}
}
public function build_content_dashboard_fetch_translations_box() {
if ( TranslationProxy::is_current_service_active_and_authenticated() ) {
$tm_polling_box = new WPML_TM_Polling_Box();
echo $tm_polling_box->render();
}
}
/**
* Used only by unit tests at the moment
*
* @return mixed
*/
public function get_post_types() {
return $this->post_types;
}
protected function heading( $text ) {
?>
<h3 class="wpml-tm-section-header wpml-tm-dashboard-h3"><?php echo esc_html( $text ); ?></h3>
<?php
}
private function current_user_can_access( $tab_item ) {
$current_user_can = isset( $tab_item['current_user_can'] ) ? $tab_item['current_user_can'] : false;
if ( is_array( $current_user_can ) ) {
foreach ( $current_user_can as $capability ) {
if ( current_user_can( $capability ) ) {
return true;
}
}
return false;
} else {
return current_user_can( $current_user_can );
}
}
abstract protected function get_page_slug();
abstract protected function get_default_tab();
}