1 | <?php |
||
8 | class Manager |
||
9 | { |
||
10 | /** |
||
11 | * @var Singleton The reference to *Singleton* instance of this class |
||
12 | */ |
||
13 | private static $instance; |
||
14 | |||
15 | /** |
||
16 | * @var Array Stores all the registered metaboxes |
||
17 | */ |
||
18 | private $metaboxes = array(); |
||
19 | |||
20 | /** |
||
21 | * Security nonce action |
||
22 | */ |
||
23 | const NONCE_ACTION = 'amarkal_metabox'; |
||
24 | |||
25 | /** |
||
26 | * Returns the *Singleton* instance of this class. |
||
27 | * |
||
28 | * @return Singleton The *Singleton* instance. |
||
29 | */ |
||
30 | public static function get_instance() |
||
38 | |||
39 | /** |
||
40 | * Private constructor to prevent instantiation |
||
41 | */ |
||
42 | private function __construct() |
||
46 | |||
47 | /** |
||
48 | * Add a metabox. |
||
49 | * |
||
50 | * @param string $id |
||
51 | * @param array $args |
||
52 | * @throws \RuntimeException if the given metabox id has already been registered |
||
53 | */ |
||
54 | public function add( $id, array $args ) |
||
63 | |||
64 | /** |
||
65 | * Render a metabox. |
||
66 | * |
||
67 | * @param WP_Post $post |
||
68 | * @param array $args |
||
69 | */ |
||
70 | public function render( $post, $args ) |
||
84 | |||
85 | /** |
||
86 | * Internally used to register metaboxes. |
||
87 | */ |
||
88 | public function add_meta_boxes() |
||
102 | |||
103 | /** |
||
104 | * Save metaboxes data for a given page. |
||
105 | * |
||
106 | * @param number $post_id |
||
107 | */ |
||
108 | public function save_meta_boxes( $post_id ) |
||
120 | |||
121 | /** |
||
122 | * Save the data of a single metabox. |
||
123 | * |
||
124 | * @param number $post_id |
||
125 | * @param string $id |
||
126 | * @param array $metabox |
||
127 | */ |
||
128 | public function save_meta_box( $post_id, $id, $metabox ) |
||
142 | |||
143 | /** |
||
144 | * Get the value of the given field. |
||
145 | * |
||
146 | * @param string $metabox_id |
||
147 | * @param string $name |
||
148 | * @param number $post_id |
||
149 | * @return mix |
||
150 | */ |
||
151 | public function get_meta_box_value( $metabox_id, $name, $post_id ) |
||
163 | |||
164 | /** |
||
165 | * Print custom metabox style. |
||
166 | */ |
||
167 | public function print_style() |
||
182 | |||
183 | /** |
||
184 | * Print all errors stored in a transient for a given post ID. |
||
185 | * |
||
186 | * @param number $post_id |
||
187 | * @param array $metabox |
||
188 | */ |
||
189 | public function print_errors( $post_id, $metabox ) |
||
204 | |||
205 | /** |
||
206 | * Initiate the metaboxes by adding action hooks for printing and saving. |
||
207 | */ |
||
208 | private function init() |
||
214 | |||
215 | /** |
||
216 | * Update the form data for the given given metabox. If the $new_instance |
||
217 | * contains new data, it will be saved into the db and if there are any |
||
218 | * validation errors they will be printed. |
||
219 | * |
||
220 | * @param array $metabox |
||
221 | * @param number $post_id |
||
222 | * @param array $new_instance |
||
223 | */ |
||
224 | private function update_form( $metabox, $post_id, array $new_instance = array() ) |
||
242 | |||
243 | /** |
||
244 | * Update post meta for the given post id |
||
245 | * |
||
246 | * @param type $final_instance |
||
247 | * @param type $post_id |
||
248 | */ |
||
249 | private function update_post_meta( $final_instance, $post_id ) |
||
256 | |||
257 | /** |
||
258 | * A note on security: |
||
259 | * |
||
260 | * We need to verify this came from the our screen and with proper authorization, |
||
261 | * because save_post can be triggered at other times. since metaboxes can |
||
262 | * be removed - by having a nonce field in only one metabox there is no |
||
263 | * guarantee the nonce will be there. By placing a nonce field in each |
||
264 | * metabox you can check if data from that metabox has been sent |
||
265 | * (and is actually from where you think it is) prior to processing any data. |
||
266 | * @see http://wordpress.stackexchange.com/a/49460/25959 |
||
267 | */ |
||
268 | private function can_save( $post_id ) |
||
288 | |||
289 | /** |
||
290 | * Get existing meta values from the database. |
||
291 | * |
||
292 | * @param array $metabox |
||
293 | * @param number $post_id |
||
294 | * @return array |
||
295 | */ |
||
296 | private function get_old_instance( $metabox, $post_id ) |
||
310 | |||
311 | /** |
||
312 | * Default arguments for the add() method. |
||
313 | * |
||
314 | * @return array |
||
315 | */ |
||
316 | private function default_args() |
||
326 | } |
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: