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