1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
include 'tabs.php'; |
4
|
|
|
|
5
|
|
|
class Tabify_Edit_Screen_Edit_Screen { |
6
|
|
|
private $tab_location = 'default'; |
7
|
|
|
private $all_metaboxes = array(); |
8
|
|
|
|
9
|
|
|
private $editscreen_tabs; |
10
|
|
|
private $settings; |
11
|
|
|
|
12
|
|
|
public function __construct() { |
13
|
|
|
add_filter( 'redirect_post_location', array( $this, 'redirect_add_current_tab' ), 10 ); |
14
|
|
|
add_action( 'admin_head', array( $this, 'show_tabs' ), 100 ); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* When a post is saved let it return to the current selected tab. |
19
|
|
|
* |
20
|
|
|
* @param string $location The location the user will be sent to |
21
|
|
|
* @return string $location The new location the user will be sent to |
22
|
|
|
* |
23
|
|
|
* @since 0.2.0 |
24
|
|
|
* |
25
|
|
|
*/ |
26
|
|
|
public function redirect_add_current_tab( $location ) { |
|
|
|
|
27
|
|
|
if ( isset( $_REQUEST['tab'] ) ) { |
28
|
|
|
$location = esc_url_raw( add_query_arg( 'tab', $_REQUEST['tab'], $location ) ); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
return $location; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Show the tabs on the edit screens. |
36
|
|
|
* This will load the tab class, tab options and actions |
37
|
|
|
* It will also will add the required classes to all the metaboxes |
38
|
|
|
* |
39
|
|
|
* @since 0.1.0 |
40
|
|
|
* |
41
|
|
|
*/ |
42
|
|
|
public function show_tabs() { |
43
|
|
|
global $wp_meta_boxes; |
44
|
|
|
|
45
|
|
|
$screen = get_current_screen(); |
46
|
|
|
|
47
|
|
|
if ( ! $screen || 'post' != $screen->base ) { |
48
|
|
|
return; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$this->tab_location = apply_filters( 'tabify_tab_location', $this->tab_location, 'posttype' ); |
52
|
|
|
|
53
|
|
|
$post_type = $screen->post_type; |
54
|
|
|
$options = get_option( 'tabify-edit-screen', array() ); |
55
|
|
|
|
56
|
|
|
if ( ! isset( $options['posttypes'][ $post_type ] ) ) { |
57
|
|
|
return; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// Ability to change if the tabs should be showed or not. |
61
|
|
|
$display_tabs = apply_filters( 'tabify_tab_posttype_show', (bool) $options['posttypes'][ $post_type ]['show'] ); |
62
|
|
|
|
63
|
|
|
// Check if this post type is enabled. |
64
|
|
|
if ( ! $display_tabs ) { |
65
|
|
|
return; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
add_filter( 'admin_body_class', array( $this, 'add_admin_body_class' ) ); |
69
|
|
|
add_action( 'admin_print_footer_scripts', array( $this, 'generate_javascript' ), 9 ); |
70
|
|
|
|
71
|
|
|
$default_metaboxes = $this->get_default_items( $post_type ); |
72
|
|
|
$this->all_metaboxes = array(); |
73
|
|
|
|
74
|
|
|
foreach ( $wp_meta_boxes[ $post_type ] as $priorities ) { |
75
|
|
|
foreach ( $priorities as $priority => $_metaboxes ) { |
76
|
|
|
foreach ( $_metaboxes as $metabox ) { |
77
|
|
|
if ( ! in_array( $metabox['id'], $default_metaboxes ) ) { |
78
|
|
|
$this->all_metaboxes[ $metabox['id'] ] = $metabox['id']; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
// Filter the tabs |
85
|
|
|
$tabs = apply_filters( 'tabify_tab_posttype_tabs', $options['posttypes'][ $post_type ]['tabs'], $post_type ); |
86
|
|
|
|
87
|
|
|
// Filter empty tabs |
88
|
|
|
$tabs = array_filter( $tabs, array( $this, 'filter_empty_tabs' ) ); |
89
|
|
|
|
90
|
|
|
// Create Tabify_Edit_Screen_Tabs that is for displaying the UI. |
91
|
|
|
$this->editscreen_tabs = new Tabify_Edit_Screen_Tabs( $tabs ); |
92
|
|
|
|
93
|
|
|
// Load the tabs on the edit screen. |
94
|
|
|
$this->load_tabs(); |
95
|
|
|
|
96
|
|
|
$tab_index = 0; |
97
|
|
|
foreach ( $tabs as $tab_index => $tab ) { |
98
|
|
|
$class = 'tabifybox tabifybox-' . $tab_index; |
99
|
|
|
|
100
|
|
|
if ( $this->editscreen_tabs->get_current_tab() != $tab_index ) { |
101
|
|
|
$class .= ' tabifybox-hide'; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if ( isset( $tab['items'] ) ) { |
105
|
|
|
foreach ( $tab['items'] as $metabox_id_fallback => $metabox_id ) { |
106
|
|
|
if ( intval( $metabox_id_fallback ) == 0 && $metabox_id_fallback !== 0 ) { |
107
|
|
|
$metabox_id = $metabox_id_fallback; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if ( ! in_array( $metabox_id, $default_metaboxes ) ) { |
111
|
|
|
if ( $metabox_id == 'titlediv' || $metabox_id == 'postdivrich' ) { |
112
|
|
|
add_action( 'tabify_custom_javascript', function() use ( $class, $metabox_id ) { |
113
|
|
|
echo 'jQuery(\'#' . $metabox_id . '\').addClass(\'' . $class . '\');'; |
114
|
|
|
} ); |
115
|
|
|
} |
116
|
|
|
else { |
117
|
|
|
add_action( 'postbox_classes_' . $post_type . '_' . $metabox_id, function( $args ) use ( $class ) { |
118
|
|
|
array_push( $args, $class ); |
119
|
|
|
return $args; |
120
|
|
|
} ); |
121
|
|
|
|
122
|
|
|
if ( isset( $this->all_metaboxes[ $metabox_id ] ) ) { |
123
|
|
|
unset( $this->all_metaboxes[ $metabox_id ] ); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$show = apply_filters( 'tabify_unattached_metaboxes_show', true, $post_type ); |
132
|
|
|
do_action( 'tabify_unattached_metaboxes', $this->all_metaboxes, $show ); |
133
|
|
|
|
134
|
|
|
// Metaboxes that aren't attachted |
135
|
|
|
if ( $show ) { |
136
|
|
|
foreach ( $this->all_metaboxes as $metabox_id ) { |
137
|
|
|
$last_index = $tab_index; |
138
|
|
|
$unattached_metaboxes_index = apply_filters( 'tabify_unattached_metaboxes_index', $last_index, $post_type ); |
139
|
|
|
|
140
|
|
|
if ( $unattached_metaboxes_index < 0 || $unattached_metaboxes_index > $last_index ) { |
141
|
|
|
$unattached_metaboxes_index = $last_index; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$class = 'tabifybox tabifybox-' . $unattached_metaboxes_index; |
145
|
|
|
|
146
|
|
|
if ( $this->editscreen_tabs->get_current_tab() != $unattached_metaboxes_index ) { |
147
|
|
|
$class .= ' tabifybox-hide'; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
add_action( 'postbox_classes_' . $post_type . '_' . $metabox_id, function( $args ) use ( $class ) { |
151
|
|
|
array_push( $args, $class ); |
152
|
|
|
return $args; |
153
|
|
|
} ); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function add_admin_body_class( $body ) { |
159
|
|
|
if ( $this->tab_location ) { |
160
|
|
|
$body .= ' tabify_tab' . $this->tab_location; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return $body; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Check where tabs should be loaded and fire the right action and callback for it |
168
|
|
|
* |
169
|
|
|
* @since 0.5.0 |
170
|
|
|
* |
171
|
|
|
*/ |
172
|
|
|
private function load_tabs() { |
173
|
|
|
if ( 'after_title' == $this->tab_location ) { |
174
|
|
|
add_action( 'edit_form_after_title', array( $this, 'output_tabs' ), 9 ); |
175
|
|
|
} |
176
|
|
|
else { //default |
177
|
|
|
$tabs = $this->submit_button(); |
178
|
|
|
$tabs .= $this->editscreen_tabs->get_tabs_with_container(); |
179
|
|
|
|
180
|
|
|
add_action( 'tabify_custom_javascript', function() use ( $tabs ) { |
181
|
|
|
echo '$(\'#post\').prepend(\'' . addslashes( $tabs ) . '\');'; |
182
|
|
|
} ); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Outputs the tabs |
188
|
|
|
* |
189
|
|
|
* @since 0.5.0 |
190
|
|
|
* |
191
|
|
|
*/ |
192
|
|
|
public function output_tabs() { |
193
|
|
|
echo $this->submit_button(); |
194
|
|
|
echo $this->editscreen_tabs->get_tabs_with_container(); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Add submit button when the submitbox isn't showed on every tab |
199
|
|
|
* |
200
|
|
|
* @since 0.7.0 |
201
|
|
|
* |
202
|
|
|
*/ |
203
|
|
|
private function submit_button() { |
204
|
|
|
$post = get_post(); |
205
|
|
|
$default = $this->get_default_items( $post->post_type ); |
206
|
|
|
|
207
|
|
|
if ( in_array( 'submitdiv', $default ) ) { |
208
|
|
|
return; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
$post_type_object = get_post_type_object( $post->post_type ); |
212
|
|
|
$can_publish = current_user_can( $post_type_object->cap->publish_posts ); |
213
|
|
|
|
214
|
|
|
if ( ! in_array( $post->post_status, array( 'publish', 'future', 'private' ) ) || 0 == $post->ID ) { |
215
|
|
|
if ( $can_publish ) { |
216
|
|
|
if ( ! empty( $post->post_date_gmt ) && time() < strtotime( $post->post_date_gmt . ' +0000' ) ) { |
217
|
|
|
$text = __( 'Schedule' ); |
218
|
|
|
} |
219
|
|
|
else { |
220
|
|
|
$text = __( 'Publish' ); |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
else { |
224
|
|
|
$text = __( 'Submit for Review' ); |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
else { |
228
|
|
|
$text = __('Update'); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
return get_submit_button( $text, 'secondary', 'second-submit', false ); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Generate the javascript for the edit screen |
236
|
|
|
* |
237
|
|
|
* @since 0.1.0 |
238
|
|
|
* |
239
|
|
|
*/ |
240
|
|
|
public function generate_javascript() { |
241
|
|
|
echo '<script type="text/javascript">'; |
242
|
|
|
echo 'jQuery(function($) {'; |
243
|
|
|
do_action( 'tabify_custom_javascript' ); |
244
|
|
|
echo '});'; |
245
|
|
|
echo '</script>'; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Filter out tabs that don't have any meta boxes to show |
250
|
|
|
* |
251
|
|
|
* @since 0.9.6 |
252
|
|
|
* |
253
|
|
|
*/ |
254
|
|
|
public function filter_empty_tabs( $tab ) { |
255
|
|
|
if ( isset( $tab['items'] ) ) { |
256
|
|
|
$tab['items'] = array_intersect( $tab['items'], $this->all_metaboxes ); |
257
|
|
|
|
258
|
|
|
return $tab['items']; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
return false; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
|
265
|
|
|
private function get_default_items( $post_type ) { |
266
|
|
|
if ( ! $this->settings ) { |
267
|
|
|
$this->settings = new Tabify_Edit_Screen_Settings_Posttypes; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
return $this->settings->get_default_items( $post_type ); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
} |
274
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: