PluginProbe
Advanced Custom Fields (ACF®) / 5.9.0
Advanced Custom Fields (ACF®) v5.9.0
6.8.9 6.8.8 6.8.7 6.8.6 6.8.5 6.8.4 6.8.3 6.8.2 6.8.1 5.8.5 5.8.6 5.8.7 5.8.8 5.8.9 5.9.0 5.9.1 5.9.2 5.9.3 5.9.4 5.9.5 5.9.6 5.9.7 5.9.8 5.9.9 6.0.0 All 230 releases
advanced-custom-fields / includes / forms / form-attachment.php
form-attachment.php
243 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * __construct
19 *
20 * This function will setup the class functionality
21 *
22 * @type function
23 * @date 5/03/2014
24 * @since 5.0.0
25 *
26 * @param n/a
27 * @return n/a
28 */
29
30 function __construct() {
31
32 // actions
33 add_action('admin_enqueue_scripts', array($this, 'admin_enqueue_scripts'));
34
35
36 // render
37 add_filter('attachment_fields_to_edit', array($this, 'edit_attachment'), 10, 2);
38
39
40 // save
41 add_filter('attachment_fields_to_save', array($this, 'save_attachment'), 10, 2);
42
43 }
44
45
46 /*
47 * admin_enqueue_scripts
48 *
49 * This action is run after post query but before any admin script / head actions.
50 * It is a good place to register all actions.
51 *
52 * @type action (admin_enqueue_scripts)
53 * @date 26/01/13
54 * @since 3.6.0
55 *
56 * @param N/A
57 * @return N/A
58 */
59
60 function admin_enqueue_scripts() {
61
62 // bail early if not valid screen
63 if( !acf_is_screen(array('attachment', 'upload')) ) {
64 return;
65 }
66
67 // load acf scripts
68 acf_enqueue_scripts(array(
69 'uploader' => true,
70 ));
71
72 // actions
73 if( acf_is_screen('upload') ) {
74 add_action('admin_footer', array($this, 'admin_footer'), 0);
75 }
76 }
77
78
79 /*
80 * admin_footer
81 *
82 * This function will add acf_form_data to the WP 4.0 attachment grid
83 *
84 * @type action (admin_footer)
85 * @date 11/09/2014
86 * @since 5.0.0
87 *
88 * @param n/a
89 * @return n/a
90 */
91
92 function admin_footer() {
93
94 // render post data
95 acf_form_data(array(
96 'screen' => 'attachment',
97 'post_id' => 0,
98 ));
99
100 ?>
101 <script type="text/javascript">
102
103 // WP saves attachment on any input change, so unload is not needed
104 acf.unload.active = 0;
105
106 </script>
107 <?php
108
109 }
110
111
112 /*
113 * edit_attachment
114 *
115 * description
116 *
117 * @type function
118 * @date 8/10/13
119 * @since 5.0.0
120 *
121 * @param $post_id (int)
122 * @return $post_id (int)
123 */
124
125 function edit_attachment( $form_fields, $post ) {
126
127 // vars
128 $is_page = acf_is_screen('attachment');
129 $post_id = $post->ID;
130 $el = 'tr';
131
132
133 // get field groups
134 $field_groups = acf_get_field_groups(array(
135 'attachment_id' => $post_id,
136 'attachment' => $post_id // Leave for backwards compatibility
137 ));
138
139
140 // render
141 if( !empty($field_groups) ) {
142
143 // get acf_form_data
144 ob_start();
145
146
147 acf_form_data(array(
148 'screen' => 'attachment',
149 'post_id' => $post_id,
150 ));
151
152
153 // open
154 echo '</td></tr>';
155
156
157 // loop
158 foreach( $field_groups as $field_group ) {
159
160 // load fields
161 $fields = acf_get_fields( $field_group );
162
163
164 // override instruction placement for modal
165 if( !$is_page ) {
166
167 $field_group['instruction_placement'] = 'field';
168 }
169
170
171 // render
172 acf_render_fields( $fields, $post_id, $el, $field_group['instruction_placement'] );
173
174 }
175
176
177 // close
178 echo '<tr class="compat-field-acf-blank"><td>';
179
180
181
182 $html = ob_get_contents();
183
184
185 ob_end_clean();
186
187
188 $form_fields[ 'acf-form-data' ] = array(
189 'label' => '',
190 'input' => 'html',
191 'html' => $html
192 );
193
194 }
195
196
197 // return
198 return $form_fields;
199
200 }
201
202
203 /*
204 * save_attachment
205 *
206 * description
207 *
208 * @type function
209 * @date 8/10/13
210 * @since 5.0.0
211 *
212 * @param $post_id (int)
213 * @return $post_id (int)
214 */
215
216 function save_attachment( $post, $attachment ) {
217
218 // bail early if not valid nonce
219 if( !acf_verify_nonce('attachment') ) {
220 return $post;
221 }
222
223 // bypass validation for ajax
224 if( acf_is_ajax('save-attachment-compat') ) {
225 acf_save_post( $post['ID'] );
226
227 // validate and save
228 } elseif( acf_validate_save_post(true) ) {
229 acf_save_post( $post['ID'] );
230 }
231
232 // return
233 return $post;
234 }
235
236
237 }
238
239 new acf_form_attachment();
240
241 endif;
242
243 ?>