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
|
<?php /** * Meta box trait. * * To keep the abstract Page class clean, all the meta box functionality is moved out to a trait. * * @since 2.5.0 * @package Hummingbird\Admin */
namespace Hummingbird\Admin;
if ( ! defined( 'ABSPATH' ) ) { exit; }
/** * Trait MetaBox */ trait MetaBox {
/** * Register a new meta box. * * @since 2.5.0 * * @param string $box_id Meta box ID. * @param string $title Meta box title. * @param string $context Meta box context. */ protected function register_meta_box( $box_id, $title, $context = 'main' ) { if ( ! isset( $this->meta_boxes[ $this->slug ] ) ) { $this->meta_boxes[ $this->slug ] = array(); }
if ( ! isset( $this->meta_boxes[ $this->slug ][ $context ] ) ) { $this->meta_boxes[ $this->slug ][ $context ] = array(); }
$meta_box = array( 'id' => $box_id, 'title' => $title, 'callback' => null, 'callback_header' => null, 'callback_footer' => null, 'args' => array( 'box_class' => 'sui-box', 'box_header_class' => 'sui-box-header', 'box_content_class' => 'sui-box-body', 'box_footer_class' => 'sui-box-footer', ), );
/** * Allow to filter a WP Hummingbird meta box. * * @param array $meta_box Meta box attributes. * @param string $slug Admin page slug. * @param string $page_id Admin page ID. */ $meta_box = apply_filters( 'wphb_add_meta_box', $meta_box, $this->slug, $this->page_id ); $meta_box = apply_filters( 'wphb_add_meta_box_' . $meta_box['id'], $meta_box, $this->slug, $this->page_id );
if ( $meta_box ) { $this->meta_boxes[ $this->slug ][ $context ][ $box_id ] = $meta_box; } }
/** * Add meta box callback. * * @since 2.5.0 * * @param string $box_id Meta box ID. * @param callable $callback Callback for meta box content. * @param string $context Meta box context. */ protected function register_meta_box_callback( $box_id, $callback = null, $context = 'main' ) { if ( ! $this->is_valid_meta_box( $box_id, $context ) ) { return; }
$this->meta_boxes[ $this->slug ][ $context ][ $box_id ]['callback'] = $callback; }
/** * Add meta box header callback. * * @since 2.5.0 * * @param string $box_id Meta box ID. * @param callable $callback Callback for meta box header. * @param string $context Meta box context. */ protected function register_meta_box_header( $box_id, $callback = null, $context = 'main' ) { if ( ! $this->is_valid_meta_box( $box_id, $context ) ) { return; }
$this->meta_boxes[ $this->slug ][ $context ][ $box_id ]['callback_header'] = $callback; }
/** * Add meta box footer callback. * * @since 2.5.0 * * @param string $box_id Meta box ID. * @param callable $callback Callback for meta box footer. * @param string $context Meta box context. */ protected function register_meta_box_footer( $box_id, $callback = null, $context = 'main' ) { if ( ! $this->is_valid_meta_box( $box_id, $context ) ) { return; }
$this->meta_boxes[ $this->slug ][ $context ][ $box_id ]['callback_footer'] = $callback; }
/** * Add meta box footer callback. * * @since 2.5.0 * * @param string $box_id Meta box ID. * @param array $args Callback for meta box content. * @param string $context Meta box context. */ protected function add_meta_box_arguments( $box_id, $args = array(), $context = 'main' ) { if ( ! $this->is_valid_meta_box( $box_id, $context ) ) { return; }
$args = wp_parse_args( $args, $this->meta_boxes[ $this->slug ][ $context ][ $box_id ]['args'] );
$this->meta_boxes[ $this->slug ][ $context ][ $box_id ]['args'] = $args; }
/** * Check if the meta box is already registered. * * @since 2.5.0 * * @param string $box_id Meta box ID. * @param string $context Meta box context. * * @return bool */ private function is_valid_meta_box( $box_id, $context ) { if ( ! isset( $this->meta_boxes[ $this->slug ] ) ) { return false; }
if ( ! isset( $this->meta_boxes[ $this->slug ][ $context ] ) ) { return false; }
if ( ! isset( $this->meta_boxes[ $this->slug ][ $context ][ $box_id ] ) ) { return false; }
return true; }
}
|