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