1 | <?php |
||
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 | else { |
||
61 | $url = admin_url('post-new.php'); |
||
62 | $url = add_query_arg( 'post_type', $posttype->name, $url ); |
||
63 | $url = add_query_arg( 'tes_metaboxes', 'true', $url ); |
||
64 | $posttype_links[ $posttype->name ] = $url; |
||
65 | } |
||
66 | } |
||
67 | |||
68 | wp_localize_script( 'tabify-edit-screen-detection', 'tabify_detection', array( |
||
69 | 'posttype_links' => $posttype_links |
||
70 | ) ); |
||
71 | } |
||
72 | |||
73 | public function head_action() { |
||
74 | global $wp_meta_boxes; |
||
75 | |||
76 | $screen = get_current_screen(); |
||
77 | |||
78 | ob_end_clean(); |
||
79 | |||
80 | echo wp_json_encode( get_transient( 'tabify_detection_' . $screen->post_type ) ); |
||
81 | |||
82 | exit; |
||
83 | } |
||
84 | |||
85 | |||
86 | public function add_missing_meta_boxes( $post_type ) { |
||
87 | if ( is_array( $metaboxes = get_transient( 'tabify_detection_' . $post_type ) ) ) { |
||
88 | foreach ( $metaboxes as $id => $metabox ) { |
||
89 | if ( ! isset( $wp_meta_boxes[ $post_type ][ $metabox->context ][ $metabox->priority ][ $id ] ) ) { |
||
90 | add_meta_box( $id, $metabox->title, '__return_false', $post_type, $metabox->priority, $metabox->context ); |
||
91 | } |
||
92 | } |
||
93 | } |
||
94 | } |
||
95 | |||
96 | public function unattached_metaboxes( $unattached_metaboxes ) { |
||
97 | $screen = get_current_screen(); |
||
98 | |||
99 | if ( get_transient( 'tabify_detection_' . $screen->post_type ) !== false ) { |
||
100 | return; |
||
101 | } |
||
102 | |||
103 | $metaboxes = array(); |
||
104 | |||
105 | if ( $unattached_metaboxes ) { |
||
106 | $metaboxes = $this->get_metaboxes( $screen->post_type ); |
||
107 | |||
108 | foreach ( $metaboxes as $metabox_id => $metabox ) { |
||
109 | if ( ! isset( $unattached_metaboxes[ $metabox_id ] ) ) { |
||
110 | unset( $metaboxes[ $metabox_id ] ); |
||
111 | } |
||
112 | } |
||
113 | } |
||
114 | |||
115 | set_transient( 'tabify_detection_' . $screen->post_type, $metaboxes, WEEK_IN_SECONDS ); |
||
116 | } |
||
117 | |||
118 | private function get_metaboxes( $post_type ) { |
||
142 | |||
143 | } |