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
<?php
class Papi_Attachment_Type extends Papi_Page_Type {
public $post_type = 'attachment';
public $type = 'attachment';
public function get_post_type() {
return $this->post_type[0];
}
protected function setup_filters() {
if ( ! isset( $_GET['post'] ) ) {
add_filter( 'attachment_fields_to_edit', [$this, 'edit_attachment'], 10, 2 );
add_filter( 'attachment_fields_to_save', [$this, 'save_attachment'], 10 );
}
}
public function edit_attachment( $form_fields, $post ) {
foreach ( $this->get_boxes() as $box ) {
if ( ! empty( $box->title ) ) {
$form_fields['papi-media-title-' . uniqid()] = [
'label' => '',
'input' => 'html',
'html' => '<h4 class="papi-media-title">' . $box->title . '</h4>'
];
}
foreach ( $box->properties as $prop ) {
$prop->raw = true;
$prop->set_post_id( $post->ID );
$form_fields[$prop->get_slug()] = [
'label' => $prop->title,
'input' => 'html',
'helps' => $prop->description,
'html' => papi_maybe_get_callable_value(
'papi_render_property',
$prop
)
];
}
}
$form_fields['papi_meta_nonce'] = [
'label' => '',
'input' => 'html',
'html' => sprintf(
'<input name="papi_meta_nonce" type="hidden" value="%s" />',
wp_create_nonce( 'papi_save_data' )
)
];
return $form_fields;
}
public function save_attachment( $post ) {
update_post_meta( $post['ID'], papi_get_page_type_key(), $this->get_id() );
$handler = new Papi_Admin_Meta_Handler();
$handler->save_meta_boxes( $post['ID'], $post );
return $post;
}
public function singleton() {
$key = sprintf( 'entry_type_id.post_type.%s', $this->get_post_type() );
if ( papi()->exists( $key ) ) {
return true;
}
papi()->singleton( $key, $this->get_id() );
return false;
}
}