1: <?php
2:
3: 4: 5: 6: 7:
8: class Papi_Attachment_Type extends Papi_Page_Type {
9:
10: 11: 12: 13: 14:
15: public $post_type = 'attachment';
16:
17: 18: 19: 20: 21:
22: public function get_post_type() {
23: return $this->post_type[0];
24: }
25:
26: 27: 28:
29: protected function setup_filters() {
30:
31: if ( ! isset( $_GET['post'] ) ) {
32: add_filter( 'attachment_fields_to_edit', [$this, 'edit_attachment'], 10, 2 );
33: add_filter( 'attachment_fields_to_save', [$this, 'save_attachment'], 10, 2 );
34: }
35: }
36:
37: 38: 39: 40: 41: 42: 43: 44:
45: public function edit_attachment( $form_fields, $post ) {
46: foreach ( $this->get_boxes() as $box ) {
47:
48: if ( ! empty( $box[0]['title'] ) ) {
49: $form_fields['papi-media-title-' . uniqid()] = [
50: 'label' => '',
51: 'input' => 'html',
52: 'html' => '<h4 class="papi-media-title">' . $box[0]['title'] . '</h4>'
53: ];
54: }
55:
56: $properties = isset( $box[1][0]->properties ) ?
57: $box[1][0]->properties : $box[1];
58:
59: foreach ( $properties as $prop ) {
60:
61: $prop->raw = true;
62:
63:
64: $prop->set_post_id( $post->ID );
65:
66:
67: $form_fields[$prop->get_slug()] = [
68: 'label' => $prop->title,
69: 'input' => 'html',
70: 'helps' => $prop->description,
71: 'html' => papi_maybe_get_callable_value(
72: 'papi_render_property',
73: $prop
74: )
75: ];
76: }
77: }
78:
79:
80: $form_fields['papi_meta_nonce'] = [
81: 'label' => '',
82: 'input' => 'html',
83: 'html' => sprintf(
84: '<input name="papi_meta_nonce" type="hidden" value="%s" />',
85: wp_create_nonce( 'papi_save_data' )
86: )
87: ];
88:
89: return $form_fields;
90: }
91:
92: 93: 94: 95: 96: 97: 98: 99:
100: public function save_attachment( $post, $attachment ) {
101: update_post_meta( $post['ID'], papi_get_page_type_key(), $this->get_id() );
102: $handler = new Papi_Admin_Post_Handler();
103: $handler->save_meta_boxes( $post['ID'], $post );
104: return $post;
105: }
106: }
107: