PluginProbe
Advanced Custom Fields (ACF®) / 5.8.8
Advanced Custom Fields (ACF®) v5.8.8
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
204 lines 3.6 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
53 // localize
54 acf_localize_data(array(
55 'mimeTypeIcon' => wp_mime_type_icon(),
56 'mimeTypes' => get_allowed_mime_types()
57 ));
58 }
59
60
61 /*
62 * handle_upload_prefilter
63 *
64 * description
65 *
66 * @type function
67 * @date 16/02/2015
68 * @since 5.1.5
69 *
70 * @param $post_id (int)
71 * @return $post_id (int)
72 */
73
74 function handle_upload_prefilter( $file ) {
75
76 // bail early if no acf field
77 if( empty($_POST['_acfuploader']) ) {
78 return $file;
79 }
80
81
82 // load field
83 $field = acf_get_field( $_POST['_acfuploader'] );
84 if( !$field ) {
85 return $file;
86 }
87
88
89 // get errors
90 $errors = acf_validate_attachment( $file, $field, 'upload' );
91
92
93 /**
94 * Filters the errors for a file before it is uploaded to WordPress.
95 *
96 * @date 16/02/2015
97 * @since 5.1.5
98 *
99 * @param array $errors An array of errors.
100 * @param array $file An array of data for a single file.
101 * @param array $field The field array.
102 */
103 $errors = apply_filters( "acf/upload_prefilter/type={$field['type']}", $errors, $file, $field );
104 $errors = apply_filters( "acf/upload_prefilter/name={$field['_name']}", $errors, $file, $field );
105 $errors = apply_filters( "acf/upload_prefilter/key={$field['key']}", $errors, $file, $field );
106 $errors = apply_filters( "acf/upload_prefilter", $errors, $file, $field );
107
108
109 // append error
110 if( !empty($errors) ) {
111 $file['error'] = implode("\n", $errors);
112 }
113
114
115 // return
116 return $file;
117 }
118
119
120 /*
121 * save_files
122 *
123 * This function will save the $_FILES data
124 *
125 * @type function
126 * @date 24/10/2014
127 * @since 5.0.9
128 *
129 * @param $post_id (int)
130 * @return $post_id (int)
131 */
132
133 function save_files( $post_id = 0 ) {
134
135 // bail early if no $_FILES data
136 if( empty($_FILES['acf']['name']) ) {
137 return;
138 }
139
140
141 // upload files
142 acf_upload_files();
143 }
144
145
146 /*
147 * wp_ajax_query_attachments
148 *
149 * description
150 *
151 * @type function
152 * @date 26/06/2015
153 * @since 5.2.3
154 *
155 * @param $post_id (int)
156 * @return $post_id (int)
157 */
158
159 function wp_ajax_query_attachments() {
160
161 add_filter('wp_prepare_attachment_for_js', array($this, 'wp_prepare_attachment_for_js'), 10, 3);
162
163 }
164
165 function wp_prepare_attachment_for_js( $response, $attachment, $meta ) {
166
167 // append attribute
168 $response['acf_errors'] = false;
169
170
171 // bail early if no acf field
172 if( empty($_POST['query']['_acfuploader']) ) {
173 return $response;
174 }
175
176
177 // load field
178 $field = acf_get_field( $_POST['query']['_acfuploader'] );
179 if( !$field ) {
180 return $response;
181 }
182
183
184 // get errors
185 $errors = acf_validate_attachment( $response, $field, 'prepare' );
186
187
188 // append errors
189 if( !empty($errors) ) {
190 $response['acf_errors'] = implode('<br />', $errors);
191 }
192
193
194 // return
195 return $response;
196 }
197 }
198
199 // instantiate
200 acf_new_instance('ACF_Media');
201
202 endif; // class_exists check
203
204 ?>