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.2.1 4.2.2 4.3.0 4.3.1 4.3.2 4.3.3 4.3.4 4.3.5 4.3.6 4.3.7 4.3.8 4.3.9 4.4.0 4.4.1 4.4.10 4.4.11 4.4.12 4.4.2 4.4.3 4.4.4 4.4.5 4.4.6 4.4.7 4.4.8 4.4.9 5.10 5.10.1 5.10.2 5.11 5.11.1 5.11.2 5.11.3 5.11.4 5.12 5.12.1 5.12.2 5.12.3 5.12.4 5.12.5 5.12.6 5.6.10 5.6.2 5.6.3 5.6.4 5.6.5 5.6.6 5.6.7 5.6.8 5.6.9 5.7.0 5.7.1 5.7.10 5.7.12 5.7.13 5.7.2 5.7.3 5.7.4 5.7.5 5.7.6 5.7.7 5.7.8 5.7.9 5.8.0 5.8.1 5.8.10 5.8.11 5.8.12 5.8.13 5.8.14 5.8.2 5.8.3 5.8.4
advanced-custom-fields / includes / media.php
advanced-custom-fields / includes Last commit date
admin 1 month ago ajax 3 months ago api 2 weeks ago fields 4 weeks ago forms 4 weeks ago legacy 5 months ago locations 1 month ago post-types 5 months ago rest-api 4 weeks ago walkers 5 months ago acf-bidirectional-functions.php 5 months ago acf-field-functions.php 5 months ago acf-field-group-functions.php 5 months ago acf-form-functions.php 5 months ago acf-helper-functions.php 5 months ago acf-hook-functions.php 5 months ago acf-input-functions.php 5 months ago acf-internal-post-type-functions.php 5 months ago acf-meta-functions.php 2 months ago acf-post-functions.php 5 months ago acf-post-type-functions.php 5 months ago acf-taxonomy-functions.php 5 months ago acf-user-functions.php 5 months ago acf-utility-functions.php 5 months ago acf-value-functions.php 5 months ago acf-wp-functions.php 5 months ago assets.php 5 months ago class-acf-data.php 5 months ago class-acf-internal-post-type.php 5 months ago compatibility.php 5 months ago deprecated.php 5 months ago fields.php 5 months ago index.php 2 years ago l10n.php 5 months ago local-fields.php 5 months ago local-json.php 3 months ago local-meta.php 5 months ago locations.php 5 months ago loop.php 5 months ago media.php 5 months ago rest-api.php 5 months ago revisions.php 3 months ago third-party.php 5 months ago upgrades.php 2 months ago validation.php 5 months ago wpml.php 5 months ago
media.php
254 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 ( ! defined( 'ABSPATH' ) ) {
13 exit; // Exit if accessed directly
14 }
15
16 if ( ! class_exists( 'ACF_Media' ) ) :
17
18 class ACF_Media {
19
20 /**
21 * Constructor.
22 *
23 * @date 23/06/12
24 * @since 5.0.0
25 *
26 * @param void
27 * @return void
28 */
29 public function __construct() {
30
31 // Localize media strings.
32 add_action( 'acf/enqueue_scripts', array( $this, 'enqueue_scripts' ) );
33
34 // Save files uploaded from basic `$_FILE` field.
35 add_action( 'acf/save_post', array( $this, 'save_files' ), 5, 1 );
36
37 // Hook into Media Upload to run additional logic.
38 add_filter( 'wp_handle_upload_prefilter', array( $this, 'handle_upload_prefilter' ), 10, 1 );
39
40 // Hook into Media Modal Query to run additional logic.
41 add_action( 'wp_ajax_query-attachments', array( $this, 'wp_ajax_query_attachments' ), -1 );
42 }
43
44 /**
45 * Fires when ACF scrtips are enqueued.
46 *
47 * @date 27/4/18
48 * @since 5.6.9
49 *
50 * @param void
51 * @return void
52 */
53 public function enqueue_scripts() {
54 if ( wp_script_is( 'acf-input' ) ) {
55 acf_localize_text(
56 array(
57 'Select.verb' => _x( 'Select', 'verb', 'acf' ),
58 'Edit.verb' => _x( 'Edit', 'verb', 'acf' ),
59 'Update.verb' => _x( 'Update', 'verb', 'acf' ),
60 'Uploaded to this post' => __( 'Uploaded to this post', 'acf' ),
61 'Expand Details' => __( 'Expand Details', 'acf' ),
62 'Collapse Details' => __( 'Collapse Details', 'acf' ),
63 'Restricted' => __( 'Restricted', 'acf' ),
64 'All images' => __( 'All images', 'acf' ),
65 )
66 );
67 acf_localize_data(
68 array(
69 'mimeTypeIcon' => wp_mime_type_icon(),
70 'mimeTypes' => get_allowed_mime_types(),
71 )
72 );
73 }
74 }
75
76 /**
77 * Uploads attachments found in the basic `$_FILES` array.
78 *
79 * @date 24/10/2014
80 * @since 5.0.9
81 *
82 * @param string|integer $post_id The post ID being saved.
83 * @return void
84 */
85 public function save_files( $post_id = 0 ) {
86 if ( isset( $_FILES['acf']['name'] ) ) { // phpcs:disable WordPress.Security.NonceVerification.Missing -- Verified upstream.
87 acf_upload_files();
88 }
89 }
90
91 /**
92 * Filters data for the current file being uploaded.
93 *
94 * @date 16/02/2015
95 * @since 5.1.5
96 *
97 * @param array $file An array of data for a single file.
98 * @return array
99 */
100 public function handle_upload_prefilter( $file ) {
101 $field = $this->get_source_field();
102 if ( ! $field ) {
103 return $file;
104 }
105
106 // Validate the attachment and append any errors.
107 $errors = acf_validate_attachment( $file, $field, 'upload' );
108
109 /**
110 * Filters the errors for a file before it is uploaded to WordPress.
111 *
112 * @date 16/02/2015
113 * @since 5.1.5
114 *
115 * @param array $errors An array of errors.
116 * @param array $file An array of data for a single file.
117 * @param array $field The field array.
118 */
119 $errors = apply_filters( "acf/upload_prefilter/type={$field['type']}", $errors, $file, $field );
120 $errors = apply_filters( "acf/upload_prefilter/name={$field['_name']}", $errors, $file, $field );
121 $errors = apply_filters( "acf/upload_prefilter/key={$field['key']}", $errors, $file, $field );
122 $errors = apply_filters( 'acf/upload_prefilter', $errors, $file, $field );
123
124 // Append errors.
125 if ( ! empty( $errors ) ) {
126 $file['error'] = implode( "\n", $errors );
127 }
128
129 // Ensure newly uploaded image contains "preview_size" within the "size" data.
130 add_filter( 'image_size_names_choose', array( $this, 'image_size_names_choose' ), 10, 1 );
131
132 // Return.
133 return $file;
134 }
135
136
137
138
139 /**
140 * Returns the field responsible for the current Media query or upload context.
141 *
142 * @date 21/5/21
143 * @since 5.9.7
144 *
145 * @param void
146 * @return array| false.
147 */
148 private function get_source_field() {
149 $field = false;
150
151 // phpcs:disable WordPress.Security.NonceVerification.Missing -- Verified elsewhere.
152 // Search for field key within available data.
153 // Case 1) Media modal query.
154 if ( isset( $_POST['query']['_acfuploader'] ) ) {
155 $field_key = sanitize_text_field( $_POST['query']['_acfuploader'] );
156
157 // Case 2) Media modal upload.
158 } elseif ( isset( $_POST['_acfuploader'] ) ) {
159 $field_key = sanitize_text_field( $_POST['_acfuploader'] );
160 }
161 // phpcs:enable WordPress.Security.NonceVerification.Missing
162
163 // Attempt to load field.
164 // Note the `acf_get_field()` function will return false if not found.
165 if ( isset( $field_key ) ) {
166 $field = acf_get_field( $field_key );
167 }
168 return $field;
169 }
170
171 /**
172 * Fires during the WP Modal Query AJAX call.
173 *
174 * @date 26/06/2015
175 * @since 5.2.3
176 *
177 * @param void
178 * @return void
179 */
180 function wp_ajax_query_attachments() {
181 if ( $this->get_source_field() ) {
182 add_filter( 'wp_prepare_attachment_for_js', array( $this, 'wp_prepare_attachment_for_js' ), 10, 3 );
183 add_filter( 'image_size_names_choose', array( $this, 'image_size_names_choose' ), 10, 1 );
184 } else {
185 add_filter( 'wp_prepare_attachment_for_js', array( $this, 'clear_acf_errors_for_core_requests' ), 5, 3 );
186 }
187 }
188
189 /**
190 * Append acf_errors false for non-acf media library calls to prevent media library caching.
191 *
192 * @date 31/8/21
193 * @since 5.10.2
194 *
195 * @param array $response Array of prepared attachment data.
196 * @param WP_Post $attachment Attachment object.
197 * @param array|false $meta Array of attachment meta data, or false if there is none.
198 * @return array
199 */
200 function clear_acf_errors_for_core_requests( $response, $attachment, $meta ) {
201 $response['acf_errors'] = false;
202 return $response;
203 }
204
205 /**
206 * Filters attachment data as it is being prepared for JS.
207 *
208 * @date 21/5/21
209 * @since 5.9.7
210 *
211 * @param array $response Array of prepared attachment data.
212 * @param WP_Post $attachment Attachment object.
213 * @param array|false $meta Array of attachment meta data, or false if there is none.
214 * @return array
215 */
216 function wp_prepare_attachment_for_js( $response, $attachment, $meta ) {
217 $field = $this->get_source_field();
218
219 // Validate the attachment and append any errors.
220 $errors = acf_validate_attachment( $response, $field, 'prepare' );
221 $response['acf_errors'] = false;
222 if ( ! empty( $errors ) ) {
223 $response['acf_errors'] = implode( '<br />', $errors );
224 }
225
226 // Return.
227 return $response;
228 }
229
230 /**
231 * Filters the names and labels of the default image sizes.
232 *
233 * @date 21/5/21
234 * @since 5.9.7
235 *
236 * @param array $size_names Array of image size labels keyed by their name.
237 * @return array
238 */
239 function image_size_names_choose( $size_names ) {
240 $field = $this->get_source_field();
241
242 // Append "preview_size" setting to array of image sizes so WP will include in prepared JS data.
243 if ( isset( $field['preview_size'] ) ) {
244 $name = (string) $field['preview_size'];
245 $size_names[ $name ] = $name; // Don't worry about size label, it is never used.
246 }
247 return $size_names;
248 }
249 }
250
251 // Instantiate.
252 acf_new_instance( 'ACF_Media' );
253 endif; // class_exists check
254