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