about
2 years ago
fields
2 years ago
helpers
2 years ago
interfaces
3 years ago
storages
2 years ago
templates
2 years ago
walkers
2 years ago
autoloader.php
2 years ago
clone.php
3 years ago
core.php
3 years ago
field-registry.php
3 years ago
field.php
3 years ago
functions.php
3 years ago
loader.php
2 years ago
media-modal.php
3 years ago
meta-box-registry.php
3 years ago
meta-box.php
3 years ago
request.php
3 years ago
sanitizer.php
2 years ago
shortcode.php
3 years ago
storage-registry.php
3 years ago
validation.php
2 years ago
wpml.php
2 years ago
media-modal.php
111 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Add support for editing attachment custom fields in the media modal. |
| 4 | */ |
| 5 | class RWMB_Media_Modal { |
| 6 | /** |
| 7 | * List of custom fields. |
| 8 | * |
| 9 | * @var array |
| 10 | */ |
| 11 | protected $fields = []; |
| 12 | |
| 13 | public function init() { |
| 14 | // Meta boxes are registered at priority 20, so we use 30 to capture them all. |
| 15 | add_action( 'init', [ $this, 'get_fields' ], 30 ); |
| 16 | |
| 17 | add_filter( 'attachment_fields_to_edit', [ $this, 'add_fields' ], 11, 2 ); |
| 18 | add_filter( 'attachment_fields_to_save', [ $this, 'save_fields' ], 11, 2 ); |
| 19 | } |
| 20 | |
| 21 | public function get_fields() { |
| 22 | $meta_boxes = rwmb_get_registry( 'meta_box' )->all(); |
| 23 | foreach ( $meta_boxes as $meta_box ) { |
| 24 | if ( $this->is_in_modal( $meta_box->meta_box ) ) { |
| 25 | $this->fields = array_merge( $this->fields, array_values( $meta_box->fields ) ); |
| 26 | } |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Add fields to the attachment edit popup. |
| 32 | * |
| 33 | * @param array $form_fields An array of attachment form fields. |
| 34 | * @param WP_Post $post The WP_Post attachment object. |
| 35 | * |
| 36 | * @return mixed |
| 37 | */ |
| 38 | public function add_fields( $form_fields, WP_Post $post ) { |
| 39 | if ( $this->is_attachment_edit_screen() ) { |
| 40 | return $form_fields; |
| 41 | } |
| 42 | |
| 43 | foreach ( $this->fields as $field ) { |
| 44 | $form_field = $field; |
| 45 | $form_field['label'] = $field['name']; |
| 46 | $form_field['input'] = 'html'; |
| 47 | |
| 48 | // Just ignore the field 'std' because there's no way to check it. |
| 49 | $meta = RWMB_Field::call( $field, 'meta', $post->ID, true ); |
| 50 | $form_field['value'] = $meta; |
| 51 | |
| 52 | $field['field_name'] = 'attachments[' . $post->ID . '][' . $field['field_name'] . ']'; |
| 53 | |
| 54 | ob_start(); |
| 55 | $field['name'] = ''; // Don't show field label as it's already handled by WordPress. |
| 56 | |
| 57 | RWMB_Field::call( 'show', $field, true, $post->ID ); |
| 58 | |
| 59 | // For MB Custom Table to flush data from the cache to the database. |
| 60 | do_action( 'rwmb_flush_data', $post->ID, $field, [] ); |
| 61 | |
| 62 | $form_field['html'] = ob_get_clean(); |
| 63 | |
| 64 | $form_fields[ $field['id'] ] = $form_field; |
| 65 | } |
| 66 | |
| 67 | return $form_fields; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Save custom fields. |
| 72 | * |
| 73 | * @param array $post An array of post data. |
| 74 | * @param array $attachment An array of attachment metadata. |
| 75 | * |
| 76 | * @return array |
| 77 | */ |
| 78 | public function save_fields( $post, $attachment ) { |
| 79 | foreach ( $this->fields as $field ) { |
| 80 | $key = $field['id']; |
| 81 | |
| 82 | $old = RWMB_Field::call( $field, 'raw_meta', $post['ID'] ); |
| 83 | $new = isset( $attachment[ $key ] ) ? $attachment[ $key ] : ''; |
| 84 | |
| 85 | $new = RWMB_Field::process_value( $new, $post['ID'], $field ); |
| 86 | |
| 87 | // Call defined method to save meta value, if there's no methods, call common one. |
| 88 | RWMB_Field::call( $field, 'save', $new, $old, $post['ID'] ); |
| 89 | |
| 90 | // For MB Custom Table to flush data from the cache to the database. |
| 91 | do_action( 'rwmb_flush_data', $post['ID'], $field, [] ); |
| 92 | } |
| 93 | |
| 94 | return $post; |
| 95 | } |
| 96 | |
| 97 | private function is_in_modal( array $meta_box ): bool { |
| 98 | return in_array( 'attachment', $meta_box['post_types'], true ) && ! empty( $meta_box['media_modal'] ); |
| 99 | } |
| 100 | |
| 101 | private function is_attachment_edit_screen(): bool { |
| 102 | if ( ! function_exists( 'get_current_screen' ) ) { |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | $screen = get_current_screen(); |
| 107 | |
| 108 | return $screen && $screen->id === 'attachment'; |
| 109 | } |
| 110 | } |
| 111 |