PluginProbe
Advanced Custom Fields (ACF®) / 5.9.4
Advanced Custom Fields (ACF®) v5.9.4
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 / media.php
media.php
214 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
4
5 if( ! class_exists('ACF_Media') ) :
6
7 class ACF_Media {
8
9
10 /*
11 * __construct
12 *
13 * Initialize filters, action, variables and includes
14 *
15 * @type function
16 * @date 23/06/12
17 * @since 5.0.0
18 *
19 * @param N/A
20 * @return N/A
21 */
22
23 function __construct() {
24
25 // actions
26 add_action('acf/enqueue_scripts', array($this, 'enqueue_scripts'));
27 add_action('acf/save_post', array($this, 'save_files'), 5, 1);
28
29
30 // filters
31 add_filter('wp_handle_upload_prefilter', array($this, 'handle_upload_prefilter'), 10, 1);
32
33
34 // ajax
35 add_action('wp_ajax_query-attachments', array($this, 'wp_ajax_query_attachments'), -1);
36 }
37
38
39 /**
40 * enqueue_scripts
41 *
42 * Localizes data
43 *
44 * @date 27/4/18
45 * @since 5.6.9
46 *
47 * @param void
48 * @return void
49 */
50
51 function enqueue_scripts(){
52 if( wp_script_is('acf-input') ) {
53 acf_localize_text(array(
54 'Select.verb' => _x('Select', 'verb', 'acf'),
55 'Edit.verb' => _x('Edit', 'verb', 'acf'),
56 'Update.verb' => _x('Update', 'verb', 'acf'),
57 'Uploaded to this post' => __('Uploaded to this post', 'acf'),
58 'Expand Details' => __('Expand Details', 'acf'),
59 'Collapse Details' => __('Collapse Details', 'acf'),
60 'Restricted' => __('Restricted', 'acf'),
61 'All images' => __('All images', 'acf')
62 ));
63 acf_localize_data(array(
64 'mimeTypeIcon' => wp_mime_type_icon(),
65 'mimeTypes' => get_allowed_mime_types()
66 ));
67 }
68 }
69
70
71 /*
72 * handle_upload_prefilter
73 *
74 * description
75 *
76 * @type function
77 * @date 16/02/2015
78 * @since 5.1.5
79 *
80 * @param $post_id (int)
81 * @return $post_id (int)
82 */
83
84 function handle_upload_prefilter( $file ) {
85
86 // bail early if no acf field
87 if( empty($_POST['_acfuploader']) ) {
88 return $file;
89 }
90
91
92 // load field
93 $field = acf_get_field( $_POST['_acfuploader'] );
94 if( !$field ) {
95 return $file;
96 }
97
98
99 // get errors
100 $errors = acf_validate_attachment( $file, $field, 'upload' );
101
102
103 /**
104 * Filters the errors for a file before it is uploaded to WordPress.
105 *
106 * @date 16/02/2015
107 * @since 5.1.5
108 *
109 * @param array $errors An array of errors.
110 * @param array $file An array of data for a single file.
111 * @param array $field The field array.
112 */
113 $errors = apply_filters( "acf/upload_prefilter/type={$field['type']}", $errors, $file, $field );
114 $errors = apply_filters( "acf/upload_prefilter/name={$field['_name']}", $errors, $file, $field );
115 $errors = apply_filters( "acf/upload_prefilter/key={$field['key']}", $errors, $file, $field );
116 $errors = apply_filters( "acf/upload_prefilter", $errors, $file, $field );
117
118
119 // append error
120 if( !empty($errors) ) {
121 $file['error'] = implode("\n", $errors);
122 }
123
124
125 // return
126 return $file;
127 }
128
129
130 /*
131 * save_files
132 *
133 * This function will save the $_FILES data
134 *
135 * @type function
136 * @date 24/10/2014
137 * @since 5.0.9
138 *
139 * @param $post_id (int)
140 * @return $post_id (int)
141 */
142
143 function save_files( $post_id = 0 ) {
144
145 // bail early if no $_FILES data
146 if( empty($_FILES['acf']['name']) ) {
147 return;
148 }
149
150
151 // upload files
152 acf_upload_files();
153 }
154
155
156 /*
157 * wp_ajax_query_attachments
158 *
159 * description
160 *
161 * @type function
162 * @date 26/06/2015
163 * @since 5.2.3
164 *
165 * @param $post_id (int)
166 * @return $post_id (int)
167 */
168
169 function wp_ajax_query_attachments() {
170
171 add_filter('wp_prepare_attachment_for_js', array($this, 'wp_prepare_attachment_for_js'), 10, 3);
172
173 }
174
175 function wp_prepare_attachment_for_js( $response, $attachment, $meta ) {
176
177 // append attribute
178 $response['acf_errors'] = false;
179
180
181 // bail early if no acf field
182 if( empty($_POST['query']['_acfuploader']) ) {
183 return $response;
184 }
185
186
187 // load field
188 $field = acf_get_field( $_POST['query']['_acfuploader'] );
189 if( !$field ) {
190 return $response;
191 }
192
193
194 // get errors
195 $errors = acf_validate_attachment( $response, $field, 'prepare' );
196
197
198 // append errors
199 if( !empty($errors) ) {
200 $response['acf_errors'] = implode('<br />', $errors);
201 }
202
203
204 // return
205 return $response;
206 }
207 }
208
209 // instantiate
210 acf_new_instance('ACF_Media');
211
212 endif; // class_exists check
213
214 ?>