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
<?php
final class Papi_Admin_Meta_Box {
private $box;
public function __construct( Papi_Core_Box $box ) {
if ( ! papi_current_user_is_allowed( $box->capabilities ) ) {
return;
}
if ( $box->display ) {
$this->box = $box;
$this->setup_actions();
}
}
public function meta_box_css_classes( array $classes ) {
return array_merge( $classes, [
'papi-box'
] );
}
public function move_meta_box_after_title() {
global $post, $wp_meta_boxes;
do_meta_boxes( get_current_screen(), $this->box->context, $post );
unset( $wp_meta_boxes[get_post_type( $post )][$this->box->context] );
}
private function get_post_type() {
if ( papi_get_meta_type() === 'post' ) {
if ( $post_id = papi_get_post_id() ) {
return get_post_type( $post_id );
}
if ( $post_type = papi_get_post_type() ) {
return $post_type;
}
}
return $this->box->id;
}
private function get_title() {
$title = $this->box->title;
if ( $this->box->get_option( 'required' ) ) {
$title .= papi_required_html(
$this->box->properties[0],
true
);
}
return $title;
}
public function render_meta_box( $post, array $args ) {
if ( ! isset( $args['args'] ) ) {
return;
}
$args['args'] = array_filter( $args['args'], 'papi_is_property' );
foreach ( $args['args'] as $index => $property ) {
if ( $property->layout === 'horizontal' ) {
$args['args'][$index]->layout = $this->box->layout;
}
}
papi_render_properties( papi_sort_order( array_reverse( $args['args'] ) ) );
}
private function setup_actions() {
if ( post_type_exists( $this->get_post_type() ) && papi_get_meta_type() === 'post' ) {
add_action( 'add_meta_boxes', [$this, 'setup_meta_box'] );
if ( $this->box->context === 'after_title' ) {
add_action( 'edit_form_after_title', [$this, 'move_meta_box_after_title'] );
}
} else {
$this->setup_meta_box();
}
add_action(
sprintf(
'postbox_classes_%s_%s',
strtolower( $this->get_post_type() ),
$this->box->id
),
[$this, 'meta_box_css_classes']
);
}
public function setup_meta_box() {
add_meta_box(
$this->box->id,
$this->get_title(),
[$this, 'render_meta_box'],
$this->get_post_type(),
$this->box->context,
$this->box->priority,
$this->box->properties
);
}
}