Oluşturulan kod şu şekilde;
<?php
class arac_bilgisi {
private $config = '{
"title":"Ek Bilgiler",
"prefix":"arac_bilgisi_ek_bilgiler",
"class_name":"arac_bilgisi",
"context":"side",
"priority":"default",
"cpt":"audi",
"fields":[{
"type":"number",
"label":"Yakıt Tüketimi",
"default":"50",
"max":"100",
"step":"5",
"id":"arac_bilgisi_ek_bilgiler_yakit"
}]
}';
public function __construct() {
$this->config = json_decode( $this->config, true );
$this->process_cpts();
add_action( 'add_meta_boxes', [ $this, 'add_meta_boxes' ] );
add_action( 'admin_head', [ $this, 'admin_head' ] );
add_action( 'save_post', [ $this, 'save_post' ] );
}
public function process_cpts() {
if ( !empty( $this->config['cpt'] ) ) {
if ( empty( $this->config['post-type'] ) ) {
$this->config['post-type'] = [];
}
$parts = explode( ',', $this->config['cpt'] );
$parts = array_map( 'trim', $parts );
$this->config['post-type'] = array_merge( $this->config['post-type'], $parts );
}
}
public function add_meta_boxes() {
foreach ( $this->config['post-type'] as $screen ) {
add_meta_box(
sanitize_title( $this->config['title'] ),
$this->config['title'],
[ $this, 'add_meta_box_callback' ],
$screen,
$this->config['context'],
$this->config['priority']
);
}
}
public function admin_head() {
global $typenow;
if ( in_array( $typenow, $this->config['post-type'] ) ) {
?><?php
}
}
public function save_post( $post_id ) {
foreach ( $this->config['fields'] as $field ) {
switch ( $field['type'] ) {
default:
if ( isset( $_POST[ $field['id'] ] ) ) {
$sanitized = sanitize_text_field( $_POST[ $field['id'] ] );
update_post_meta( $post_id, $field['id'], $sanitized );
}
}
}
}
public function add_meta_box_callback() {
echo '<div class="rwp-description">' . $this->config['description'] . '</div>';
$this->fields_div();
}
private function fields_div() {
foreach ( $this->config['fields'] as $field ) {
?><div class="components-base-control">
<div class="components-base-control__field"><?php
$this->label( $field );
$this->field( $field );
?></div>
</div><?php
}
}
private function label( $field ) {
switch ( $field['type'] ) {
default:
printf(
'<label class="components-base-control__label" for="%s">%s</label>',
$field['id'], $field['label']
);
}
}
private function field( $field ) {
switch ( $field['type'] ) {
case 'number':
$this->input_minmax( $field );
break;
default:
$this->input( $field );
}
}
private function input( $field ) {
printf(
'<input class="components-text-control__input %s" id="%s" name="%s" %s type="%s" value="%s">',
isset( $field['class'] ) ? $field['class'] : '',
$field['id'], $field['id'],
isset( $field['pattern'] ) ? "pattern='{$field['pattern']}'" : '',
$field['type'],
$this->value( $field )
);
}
private function input_minmax( $field ) {
printf(
'<input class="components-text-control__input" id="%s" %s %s name="%s" %s type="%s" value="%s">',
$field['id'],
isset( $field['max'] ) ? "max='{$field['max']}'" : '',
isset( $field['min'] ) ? "min='{$field['min']}'" : '',
$field['id'],
isset( $field['step'] ) ? "step='{$field['step']}'" : '',
$field['type'],
$this->value( $field )
);
}
private function value( $field ) {
global $post;
if ( metadata_exists( 'post', $post->ID, $field['id'] ) ) {
$value = get_post_meta( $post->ID, $field['id'], true );
} else if ( isset( $field['default'] ) ) {
$value = $field['default'];
} else {
return '';
}
return str_replace( '\u0027', "'", $value );
}
}
new arac_bilgisi;