| 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|int $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'] ) ) { |
| 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 |
// Search for field key within available data. |
| 143 |
// Case 1) Media modal query. |
| 144 |
if ( isset( $_POST['query']['_acfuploader'] ) ) { |
| 145 |
$field_key = (string) $_POST['query']['_acfuploader']; |
| 146 |
|
| 147 |
// Case 2) Media modal upload. |
| 148 |
} elseif ( isset( $_POST['_acfuploader'] ) ) { |
| 149 |
$field_key = (string) $_POST['_acfuploader']; |
| 150 |
} |
| 151 |
|
| 152 |
// Attempt to load field. |
| 153 |
// Note the `acf_get_field()` function will return false if not found. |
| 154 |
if ( isset( $field_key ) ) { |
| 155 |
$field = acf_get_field( $field_key ); |
| 156 |
} |
| 157 |
return $field; |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Fires during the WP Modal Query AJAX call. |
| 162 |
* |
| 163 |
* @date 26/06/2015 |
| 164 |
* @since 5.2.3 |
| 165 |
* |
| 166 |
* @param void |
| 167 |
* @return void |
| 168 |
*/ |
| 169 |
function wp_ajax_query_attachments() { |
| 170 |
if ( $this->get_source_field() ) { |
| 171 |
add_filter( 'wp_prepare_attachment_for_js', array( $this, 'wp_prepare_attachment_for_js' ), 10, 3 ); |
| 172 |
add_filter( 'image_size_names_choose', array( $this, 'image_size_names_choose' ), 10, 1 ); |
| 173 |
} else { |
| 174 |
add_filter( 'wp_prepare_attachment_for_js', array( $this, 'clear_acf_errors_for_core_requests' ), 5, 3 ); |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Append acf_errors false for non-acf media library calls to prevent media library caching. |
| 180 |
* |
| 181 |
* @date 31/8/21 |
| 182 |
* @since 5.10.2 |
| 183 |
* |
| 184 |
* @param array $response Array of prepared attachment data. |
| 185 |
* @param WP_Post $attachment Attachment object. |
| 186 |
* @param array|false $meta Array of attachment meta data, or false if there is none. |
| 187 |
* @return array |
| 188 |
*/ |
| 189 |
function clear_acf_errors_for_core_requests( $response, $attachment, $meta ) { |
| 190 |
$response['acf_errors'] = false; |
| 191 |
return $response; |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Filters attachment data as it is being prepared for JS. |
| 196 |
* |
| 197 |
* @date 21/5/21 |
| 198 |
* @since 5.9.7 |
| 199 |
* |
| 200 |
* @param array $response Array of prepared attachment data. |
| 201 |
* @param WP_Post $attachment Attachment object. |
| 202 |
* @param array|false $meta Array of attachment meta data, or false if there is none. |
| 203 |
* @return array |
| 204 |
*/ |
| 205 |
function wp_prepare_attachment_for_js( $response, $attachment, $meta ) { |
| 206 |
$field = $this->get_source_field(); |
| 207 |
|
| 208 |
// Validate the attachment and append any errors. |
| 209 |
$errors = acf_validate_attachment( $response, $field, 'prepare' ); |
| 210 |
$response['acf_errors'] = false; |
| 211 |
if ( ! empty( $errors ) ) { |
| 212 |
$response['acf_errors'] = implode( '<br />', $errors ); |
| 213 |
} |
| 214 |
|
| 215 |
// Return. |
| 216 |
return $response; |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Filters the names and labels of the default image sizes. |
| 221 |
* |
| 222 |
* @date 21/5/21 |
| 223 |
* @since 5.9.7 |
| 224 |
* |
| 225 |
* @param array $size_names Array of image size labels keyed by their name. |
| 226 |
* @return array |
| 227 |
*/ |
| 228 |
function image_size_names_choose( $size_names ) { |
| 229 |
$field = $this->get_source_field(); |
| 230 |
|
| 231 |
// Append "preview_size" setting to array of image sizes so WP will include in prepared JS data. |
| 232 |
if ( isset( $field['preview_size'] ) ) { |
| 233 |
$name = (string) $field['preview_size']; |
| 234 |
$size_names[ $name ] = $name; // Don't worry about size label, it is never used. |
| 235 |
} |
| 236 |
return $size_names; |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
// Instantiate. |
| 241 |
acf_new_instance( 'ACF_Media' ); |
| 242 |
|
| 243 |
endif; // class_exists check |
| 244 |
|