form-attachment.php
5 months ago
form-comment.php
4 weeks ago
form-customizer.php
5 months ago
form-front.php
2 months ago
form-gutenberg.php
5 months ago
form-nav-menu.php
5 months ago
form-post.php
3 months ago
form-taxonomy.php
2 months ago
form-user.php
4 weeks ago
form-widget.php
5 months ago
index.php
2 years ago
form-attachment.php
212 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package ACF |
| 4 | * @author WP Engine |
| 5 | * |
| 6 | * © 2026 Advanced Custom Fields (ACF®). All rights reserved. |
| 7 | * "ACF" is a trademark of WP Engine. |
| 8 | * Licensed under the GNU General Public License v2 or later. |
| 9 | * https://www.gnu.org/licenses/gpl-2.0.html |
| 10 | */ |
| 11 | |
| 12 | if ( ! class_exists( 'acf_form_attachment' ) ) : |
| 13 | |
| 14 | class acf_form_attachment { |
| 15 | |
| 16 | /** |
| 17 | * This function will setup the class functionality |
| 18 | * |
| 19 | * @type function |
| 20 | * @date 5/03/2014 |
| 21 | * @since 5.0.0 |
| 22 | * |
| 23 | * @param n/a |
| 24 | * @return n/a |
| 25 | */ |
| 26 | function __construct() { |
| 27 | |
| 28 | // actions |
| 29 | add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); |
| 30 | |
| 31 | // render |
| 32 | add_filter( 'attachment_fields_to_edit', array( $this, 'edit_attachment' ), 10, 2 ); |
| 33 | |
| 34 | // save |
| 35 | add_filter( 'attachment_fields_to_save', array( $this, 'save_attachment' ), 10, 2 ); |
| 36 | } |
| 37 | |
| 38 | |
| 39 | /** |
| 40 | * This action is run after post query but before any admin script / head actions. |
| 41 | * It is a good place to register all actions. |
| 42 | * |
| 43 | * @type action (admin_enqueue_scripts) |
| 44 | * @date 26/01/13 |
| 45 | * @since 3.6.0 |
| 46 | * |
| 47 | * @param N/A |
| 48 | * @return N/A |
| 49 | */ |
| 50 | function admin_enqueue_scripts() { |
| 51 | |
| 52 | // bail early if not valid screen |
| 53 | if ( ! acf_is_screen( array( 'attachment', 'upload' ) ) ) { |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | // load acf scripts |
| 58 | acf_enqueue_scripts( |
| 59 | array( |
| 60 | 'uploader' => true, |
| 61 | ) |
| 62 | ); |
| 63 | |
| 64 | // actions |
| 65 | if ( acf_is_screen( 'upload' ) ) { |
| 66 | add_action( 'admin_footer', array( $this, 'admin_footer' ), 0 ); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | |
| 71 | /** |
| 72 | * This function will add acf_form_data to the WP 4.0 attachment grid |
| 73 | * |
| 74 | * @type action (admin_footer) |
| 75 | * @date 11/09/2014 |
| 76 | * @since 5.0.0 |
| 77 | * |
| 78 | * @param n/a |
| 79 | * @return n/a |
| 80 | */ |
| 81 | function admin_footer() { |
| 82 | |
| 83 | // render post data |
| 84 | acf_form_data( |
| 85 | array( |
| 86 | 'screen' => 'attachment', |
| 87 | 'post_id' => 0, |
| 88 | ) |
| 89 | ); |
| 90 | |
| 91 | ?> |
| 92 | <script type="text/javascript"> |
| 93 | |
| 94 | // WP saves attachment on any input change, so unload is not needed |
| 95 | acf.unload.active = 0; |
| 96 | |
| 97 | </script> |
| 98 | <?php |
| 99 | } |
| 100 | |
| 101 | |
| 102 | /** |
| 103 | * description |
| 104 | * |
| 105 | * @type function |
| 106 | * @date 8/10/13 |
| 107 | * @since 5.0.0 |
| 108 | * |
| 109 | * @param $post_id (int) |
| 110 | * @return $post_id (int) |
| 111 | */ |
| 112 | function edit_attachment( $form_fields, $post ) { |
| 113 | |
| 114 | // vars |
| 115 | $is_page = acf_is_screen( 'attachment' ); |
| 116 | $post_id = $post->ID; |
| 117 | $el = 'tr'; |
| 118 | |
| 119 | // get field groups |
| 120 | $field_groups = acf_get_field_groups( |
| 121 | array( |
| 122 | 'attachment_id' => $post_id, |
| 123 | 'attachment' => $post_id, // Leave for backwards compatibility |
| 124 | ) |
| 125 | ); |
| 126 | |
| 127 | // render |
| 128 | if ( ! empty( $field_groups ) ) { |
| 129 | |
| 130 | // get acf_form_data |
| 131 | ob_start(); |
| 132 | |
| 133 | acf_form_data( |
| 134 | array( |
| 135 | 'screen' => 'attachment', |
| 136 | 'post_id' => $post_id, |
| 137 | ) |
| 138 | ); |
| 139 | |
| 140 | // open |
| 141 | echo '</td></tr>'; |
| 142 | |
| 143 | // loop |
| 144 | foreach ( $field_groups as $field_group ) { |
| 145 | |
| 146 | // load fields |
| 147 | $fields = acf_get_fields( $field_group ); |
| 148 | |
| 149 | // override instruction placement for modal |
| 150 | if ( ! $is_page ) { |
| 151 | $field_group['instruction_placement'] = 'field'; |
| 152 | } |
| 153 | |
| 154 | // render |
| 155 | acf_render_fields( $fields, $post_id, $el, $field_group['instruction_placement'] ); |
| 156 | } |
| 157 | |
| 158 | // close |
| 159 | echo '<tr class="compat-field-acf-blank"><td>'; |
| 160 | |
| 161 | $html = ob_get_contents(); |
| 162 | |
| 163 | ob_end_clean(); |
| 164 | |
| 165 | $form_fields['acf-form-data'] = array( |
| 166 | 'label' => '', |
| 167 | 'input' => 'html', |
| 168 | 'html' => $html, |
| 169 | ); |
| 170 | } |
| 171 | |
| 172 | // return |
| 173 | return $form_fields; |
| 174 | } |
| 175 | |
| 176 | |
| 177 | /** |
| 178 | * description |
| 179 | * |
| 180 | * @type function |
| 181 | * @date 8/10/13 |
| 182 | * @since 5.0.0 |
| 183 | * |
| 184 | * @param $post_id (int) |
| 185 | * @return $post_id (int) |
| 186 | */ |
| 187 | function save_attachment( $post, $attachment ) { |
| 188 | |
| 189 | // bail early if not valid nonce |
| 190 | if ( ! acf_verify_nonce( 'attachment' ) ) { |
| 191 | return $post; |
| 192 | } |
| 193 | |
| 194 | // bypass validation for ajax |
| 195 | if ( acf_is_ajax( 'save-attachment-compat' ) ) { |
| 196 | acf_save_post( $post['ID'] ); |
| 197 | |
| 198 | // validate and save |
| 199 | } elseif ( acf_validate_save_post( true ) ) { |
| 200 | acf_save_post( $post['ID'] ); |
| 201 | } |
| 202 | |
| 203 | // return |
| 204 | return $post; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | new acf_form_attachment(); |
| 209 | endif; |
| 210 | |
| 211 | ?> |
| 212 |