1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class Tabify_Edit_Screen_Feature_Detection { |
4
|
|
|
|
5
|
|
|
public function __construct() { |
6
|
|
|
// Actions to return JSON output on post type new/edit screen |
7
|
|
|
add_action( 'current_screen', array( $this, 'head_action_begin' ) ); |
8
|
|
|
|
9
|
|
|
// Hook for requesting missing hooks |
10
|
|
|
add_action( 'tabify_add_meta_boxes', array( $this, 'add_missing_meta_boxes' ) ); |
11
|
|
|
|
12
|
|
|
// Checking & storing unattached meta boxes on the edit page |
13
|
|
|
add_action( 'tabify_unattached_metaboxes', array( $this, 'unattached_metaboxes' ) ); |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
public function head_action_begin( $screen ) { |
|
|
|
|
18
|
|
|
if ( ( 'post' == $screen->base || 'media' == $screen->base ) && isset( $_GET['tes_metaboxes'] ) ) { |
19
|
|
|
ob_end_clean(); // For when warnings are displayed |
20
|
|
|
ob_start(); |
21
|
|
|
|
22
|
|
|
add_filter( 'tabify_tab_posttype_show', '__return_true', 1000 ); |
23
|
|
|
add_action( 'admin_head', array( $this, 'head_action' ), 110 ); |
24
|
|
|
} |
25
|
|
|
else if ( 'settings_page_tabify-edit-screen' == $screen->base ) { |
26
|
|
|
$this->enqueue_script(); |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
private function enqueue_script() { |
31
|
|
|
wp_register_script( 'tabify-edit-screen-detection', plugins_url( '/detection.js', __FILE__ ), array( 'jquery' ), '1.0' ); |
32
|
|
|
wp_enqueue_script( 'tabify-edit-screen-detection' ); |
33
|
|
|
|
34
|
|
|
$posttype_links = array(); |
35
|
|
|
|
36
|
|
|
$args = array( |
37
|
|
|
'show_ui' => true |
38
|
|
|
); |
39
|
|
|
$posttypes_objects = get_post_types( $args, 'objects' ); |
40
|
|
|
$posttypes_objects = apply_filters( 'tabify_posttypes', $posttypes_objects ); |
41
|
|
|
|
42
|
|
|
foreach ( $posttypes_objects as $posttype ) { |
43
|
|
|
if ( get_transient( 'tabify_detection_' . $posttype->name ) !== false ) { |
44
|
|
|
continue; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$args = array( |
48
|
|
|
'post_type' => $posttype->name, |
49
|
|
|
'orderby' => 'rand', |
50
|
|
|
'posts_per_page' => '1', |
51
|
|
|
'post_status' => 'any' |
52
|
|
|
); |
53
|
|
|
$post = get_posts( $args ); |
54
|
|
|
|
55
|
|
|
if ( ! empty( $post ) ) { |
56
|
|
|
$url = get_edit_post_link( $post[0], 'raw' ); |
57
|
|
|
$url = add_query_arg( 'tes_metaboxes', 'true', $url ); |
58
|
|
|
$posttype_links[ $posttype->name ] = $url; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
wp_localize_script( 'tabify-edit-screen-detection', 'tabify_detection', array( |
63
|
|
|
'posttype_links' => $posttype_links |
64
|
|
|
) ); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function head_action() { |
68
|
|
|
global $wp_meta_boxes; |
69
|
|
|
|
70
|
|
|
$screen = get_current_screen(); |
71
|
|
|
|
72
|
|
|
ob_end_clean(); |
73
|
|
|
|
74
|
|
|
echo wp_json_encode( get_transient( 'tabify_detection_' . $screen->post_type ) ); |
75
|
|
|
|
76
|
|
|
exit; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
public function add_missing_meta_boxes( $post_type ) { |
81
|
|
|
if ( is_array( $metaboxes = get_transient( 'tabify_detection_' . $post_type ) ) ) { |
82
|
|
|
foreach ( $metaboxes as $id => $metabox ) { |
83
|
|
|
if ( ! isset( $wp_meta_boxes[ $post_type ][ $metabox->context ][ $metabox->priority ][ $id ] ) ) { |
84
|
|
|
add_meta_box( $id, $metabox->title, '__return_false', $post_type, $metabox->priority, $metabox->context ); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function unattached_metaboxes( $unattached_metaboxes ) { |
91
|
|
|
global $wp_meta_boxes; |
92
|
|
|
|
93
|
|
|
$screen = get_current_screen(); |
94
|
|
|
|
95
|
|
|
if ( get_transient( 'tabify_detection_' . $screen->post_type ) !== false ) { |
96
|
|
|
return; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$metaboxes = array(); |
100
|
|
|
|
101
|
|
|
if ( $unattached_metaboxes ) { |
102
|
|
|
$metaboxes = $this->get_metaboxes( $screen->post_type ); |
|
|
|
|
103
|
|
|
|
104
|
|
|
foreach ( $metaboxes as $metabox_id => $metabox ) { |
|
|
|
|
105
|
|
|
if ( ! isset( $unattached_metaboxes[ $metabox_id ] ) ) { |
106
|
|
|
unset( $metaboxes[ $metabox_id ] ); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
set_transient( 'tabify_detection_' . $screen->post_type, $metaboxes, WEEK_IN_SECONDS ); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
private function get_metaboxes( $post_type ) { |
115
|
|
|
$metaboxes = array(); |
116
|
|
|
|
117
|
|
|
foreach ( $wp_meta_boxes[ $post_type ] as $context => $priorities ) { |
|
|
|
|
118
|
|
|
foreach ( $priorities as $priority => $_metaboxes ) { |
119
|
|
|
foreach ( $_metaboxes as $metabox ) { |
120
|
|
|
// Metabox has been removed |
121
|
|
|
if ( ! isset( $metabox['id'] ) ) { |
122
|
|
|
continue; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$metaboxes[ $metabox['id'] ] = (object) array( |
126
|
|
|
'title' => $metabox['title'], |
127
|
|
|
'priority' => $priority, |
128
|
|
|
'context' => $context |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
} |
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: