1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class Tabify_Edit_Screen_Feature_Detection { |
4
|
|
|
/** |
5
|
|
|
* Set hooks |
6
|
|
|
* |
7
|
|
|
* @since 0.9.0 |
8
|
|
|
*/ |
9
|
|
|
public function __construct() { |
10
|
|
|
add_action( 'current_screen', array( $this, 'head_action_begin_settings_page' ) ); |
11
|
|
|
add_action( 'current_screen', array( $this, 'head_action_begin_edit_page' ) ); |
12
|
|
|
|
13
|
|
|
// Hook for requesting missing hooks |
14
|
|
|
add_action( 'tabify_add_meta_boxes', array( $this, 'add_missing_meta_boxes' ) ); |
15
|
|
|
|
16
|
|
|
// Checking & storing unattached meta boxes on the edit page |
17
|
|
|
add_action( 'tabify_unattached_metaboxes', array( $this, 'unattached_metaboxes' ) ); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Checks if script for edit page need to be enqueued |
22
|
|
|
* |
23
|
|
|
* @since 1.0.0 |
24
|
|
|
*/ |
25
|
|
|
public function head_action_begin_settings_page( $screen ) { |
26
|
|
|
if ( 'settings_page_tabify-edit-screen' == $screen->base ) { |
27
|
|
|
$this->enqueue_script(); |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Actions to return JSON output on post type new/edit screen |
33
|
|
|
* |
34
|
|
|
* @since 1.0.0 |
35
|
|
|
*/ |
36
|
|
|
public function head_action_begin_edit_page( $screen ) { |
37
|
|
|
if ( ( 'post' == $screen->base || 'media' == $screen->base ) && isset( $_GET['test_metaboxes'] ) ) { |
38
|
|
|
ob_end_clean(); // For when warnings are displayed |
39
|
|
|
ob_start(); |
40
|
|
|
|
41
|
|
|
add_filter( 'tabify_tab_posttype_show', '__return_true', 1000 ); |
42
|
|
|
add_action( 'admin_head', array( $this, 'head_action' ), 110 ); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Enqueue script to load detected meta boxes and display them in the settings page |
48
|
|
|
* |
49
|
|
|
* @since 0.9.0 |
50
|
|
|
*/ |
51
|
|
|
private function enqueue_script() { |
52
|
|
|
wp_register_script( 'tabify-edit-screen-detection', plugins_url( '/detection.js', __FILE__ ), array( 'jquery' ), '1.0' ); |
53
|
|
|
wp_enqueue_script( 'tabify-edit-screen-detection' ); |
54
|
|
|
|
55
|
|
|
$posttype_links = array(); |
56
|
|
|
|
57
|
|
|
$args = array( |
58
|
|
|
'show_ui' => true |
59
|
|
|
); |
60
|
|
|
$posttypes_objects = get_post_types( $args, 'objects' ); |
61
|
|
|
$posttypes_objects = apply_filters( 'tabify_posttypes', $posttypes_objects ); |
62
|
|
|
|
63
|
|
|
foreach ( $posttypes_objects as $posttype ) { |
64
|
|
|
if ( get_transient( 'tabify_detection_' . $posttype->name ) !== false ) { |
65
|
|
|
continue; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$args = array( |
69
|
|
|
'post_type' => $posttype->name, |
70
|
|
|
'orderby' => 'rand', |
71
|
|
|
'posts_per_page' => '1', |
72
|
|
|
'post_status' => 'any' |
73
|
|
|
); |
74
|
|
|
$post = get_posts( $args ); |
75
|
|
|
|
76
|
|
|
if ( ! empty( $post ) ) { |
77
|
|
|
$url = get_edit_post_link( $post[0], 'raw' ); |
78
|
|
|
$url = add_query_arg( 'test_metaboxes', 'true', $url ); |
79
|
|
|
$posttype_links[ $posttype->name ] = $url; |
80
|
|
|
} |
81
|
|
|
else { |
82
|
|
|
$url = admin_url('post-new.php'); |
83
|
|
|
$url = add_query_arg( 'post_type', $posttype->name, $url ); |
84
|
|
|
$url = add_query_arg( 'test_metaboxes', 'true', $url ); |
85
|
|
|
$posttype_links[ $posttype->name ] = $url; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
wp_localize_script( 'tabify-edit-screen-detection', 'tabify_detection', array( |
90
|
|
|
'posttype_links' => $posttype_links |
91
|
|
|
) ); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Load the JSON data for the settings page |
96
|
|
|
* |
97
|
|
|
* @since 0.9.0 |
98
|
|
|
*/ |
99
|
|
|
public function head_action() { |
100
|
|
|
$screen = get_current_screen(); |
|
|
|
|
101
|
|
|
|
102
|
|
|
ob_end_clean(); |
103
|
|
|
|
104
|
|
|
echo wp_json_encode( get_transient( 'tabify_detection_' . $screen->post_type ) ); |
|
|
|
|
105
|
|
|
|
106
|
|
|
exit; |
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Add missing meta boxes as meta boxes on the edit screen so it doesn't disappear on first load |
111
|
|
|
* |
112
|
|
|
* @since 0.9.0 |
113
|
|
|
*/ |
114
|
|
|
public function add_missing_meta_boxes( $post_type ) { |
115
|
|
|
global $wp_meta_boxes; |
116
|
|
|
|
117
|
|
|
if ( is_array( $metaboxes = get_transient( 'tabify_detection_' . $post_type ) ) ) { |
118
|
|
|
foreach ( $metaboxes as $id => $metabox ) { |
119
|
|
|
if ( ! isset( $wp_meta_boxes[ $post_type ][ $metabox->context ][ $metabox->priority ][ $id ] ) ) { |
120
|
|
|
add_meta_box( $id, $metabox->title, '__return_false', $post_type, $metabox->priority, $metabox->context ); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Check and save missing |
128
|
|
|
* |
129
|
|
|
* @since 0.9.0 |
130
|
|
|
*/ |
131
|
|
|
public function unattached_metaboxes( $unattached_metaboxes ) { |
132
|
|
|
$screen = get_current_screen(); |
|
|
|
|
133
|
|
|
$previous_metaboxes = get_transient( 'tabify_detection_' . $screen->post_type ); |
134
|
|
|
|
135
|
|
|
$metaboxes = array(); |
136
|
|
|
|
137
|
|
|
if ( $unattached_metaboxes ) { |
138
|
|
|
$metaboxes = $this->get_metaboxes( $screen->post_type ); |
139
|
|
|
|
140
|
|
|
foreach ( $metaboxes as $metabox_id => $metabox ) { |
141
|
|
|
if ( ! isset( $unattached_metaboxes[ $metabox_id ] ) ) { |
142
|
|
|
unset( $metaboxes[ $metabox_id ] ); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
if ($metaboxes != $previous_metaboxes) { |
148
|
|
|
set_transient( 'tabify_detection_' . $screen->post_type, $metaboxes, WEEK_IN_SECONDS ); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* |
154
|
|
|
* |
155
|
|
|
* @since 0.9.0 |
156
|
|
|
*/ |
157
|
|
|
private function get_metaboxes( $post_type ) { |
158
|
|
|
global $wp_meta_boxes; |
159
|
|
|
|
160
|
|
|
$metaboxes = array(); |
161
|
|
|
|
162
|
|
|
foreach ( $wp_meta_boxes[ $post_type ] as $context => $priorities ) { |
163
|
|
|
foreach ( $priorities as $priority => $_metaboxes ) { |
164
|
|
|
foreach ( $_metaboxes as $metabox ) { |
165
|
|
|
// Metabox has been removed |
166
|
|
|
if ( ! isset( $metabox['id'] ) ) { |
167
|
|
|
continue; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
$metaboxes[ $metabox['id'] ] = (object) array( |
171
|
|
|
'title' => $metabox['title'], |
172
|
|
|
'priority' => $priority, |
173
|
|
|
'context' => $context |
174
|
|
|
); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
return $metaboxes; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
} |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.