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
<?php
class CMB2_Integration_Box {
protected $box;
protected $hook;
public function __construct($args = []) {
$this->hook = 'cmb2_integration_tests_handle_box_' . md5(wp_generate_password());
add_action('cmb2_admin_init', function () use ($args) {
$this->box = new_cmb2_box($args);
do_action($this->hook, $this->box);
});
}
public function addField($type, $args = []) {
$args = array_merge([
'type' => $type,
'id' => 'cmb2_integration_tests_field_' . $type,
'name' => $type,
'desc' => $type . ' Description',
], $args);
add_action($this->hook, function($box) use ($args) {
$box->add_field($args);
});
}
}
add_action('plugins_loaded', function () {
$prefix = 'cmb2_integration_tests_';
$closed = new CMB2_Integration_Box([
'id' => $prefix . 'default_closed',
'title' => 'Default Closed',
'object_types' => [ 'post' ],
'closed' => true,
]);
$closed->addField('text');
});