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