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
6 days 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' ), 20, 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 |