PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.1
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.1
16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 All 504 releases
← All changes | _inc/lib/class.media.php +27 -31 12.5.2 → 16.3-a.1 View file →
@@ -1,6 +1,10 @@
1 1 <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2 2
3 +if ( ! defined( 'ABSPATH' ) ) {
4 + exit( 0 );
5 +}
6 +
3 7 require_once JETPACK__PLUGIN_DIR . 'sal/class.json-api-date.php';
4 8
5 9 /**
6 10 * Class to handle different actions related to media.
@@ -36,9 +40,9 @@
36 40 * The returned name has the `{basename}-{hash}-{random-number}.{ext}` shape.
37 41 * The hash is built according to the filename trying to avoid name collisions
38 42 * with other media files.
39 43 *
40 - * @param number $media_id - media post ID.
44 + * @param int $media_id - media post ID.
41 45 * @param string $new_filename - the new filename.
42 46 * @return string A random filename.
43 47 */
44 48 public static function generate_new_filename( $media_id, $new_filename ) {
@@ -144,25 +148,16 @@
144 148 return jetpack_is_file_supported_for_sideloading( $file );
145 149 }
146 150
147 151 /**
148 - * Try to remove the temporal file from the given file array.
149 - *
150 - * @param array $file_array Array with data about the temporal file.
151 - */
152 - private static function remove_tmp_file( $file_array ) {
153 - if ( file_exists( $file_array['tmp_name'] ) ) {
154 - wp_delete_file( $file_array['tmp_name'] );
155 - }
156 - }
157 -
158 - /**
159 - * Save the given temporal file considering file type,
152 + * Save the given uploaded temporary file considering file type,
160 153 * correct location according to the original file path, etc.
161 154 * The file type control is done through of `jetpack_supported_media_sideload_types` filter,
162 155 * which allows define to the users their own file types list.
163 156 *
164 - * @param array $file_array file to save.
157 + * Note this does not support sideloads, only uploads.
158 + *
159 + * @param array $file_array Data derived from `$_FILES` for an uploaded file.
165 160 * @param int $media_id Attachment ID.
166 161 * @return array|WP_Error an array with information about the new file saved or a WP_Error is something went wrong.
167 162 */
168 163 public static function save_temporary_file( $file_array, $media_id ) {
@@ -167,9 +162,9 @@
167 162 */
168 163 public static function save_temporary_file( $file_array, $media_id ) {
169 164 $tmp_filename = $file_array['tmp_name'];
170 165
171 - if ( ! file_exists( $tmp_filename ) ) {
166 + if ( ! is_uploaded_file( $tmp_filename ) ) {
172 167 return new WP_Error( 'invalid_input', 'No media provided in input.' );
173 168 }
174 169
175 170 // add additional mime_types through of the `jetpack_supported_media_sideload_types` filter.
@@ -182,9 +177,8 @@
182 177 if (
183 178 ! self::is_file_supported_for_sideloading( $tmp_filename ) &&
184 179 ! file_is_displayable_image( $tmp_filename )
185 180 ) {
186 - wp_delete_file( $tmp_filename );
187 181 return new WP_Error( 'invalid_input', 'Invalid file type.', 403 );
188 182 }
189 183 remove_filter( 'jetpack_supported_media_sideload_types', $mime_type_static_filter );
190 184
@@ -197,12 +191,10 @@
197 191 // get time according to the original filaname.
198 192 $time = self::get_time_string_from_guid( $media_id );
199 193
200 194 $file_array['name'] = $tmp_new_filename;
201 - $file = wp_handle_sideload( $file_array, $overrides, $time );
195 + $file = wp_handle_upload( $file_array, $overrides, $time );
202 196
203 - self::remove_tmp_file( $file_array );
204 -
205 197 if ( isset( $file['error'] ) ) {
206 198 return new WP_Error( 'upload_error', $file['error'] );
207 199 }
208 200
@@ -233,11 +225,11 @@
233 225
234 226 /**
235 227 * Add a new item into revision_history array.
236 228 *
237 - * @param object $media_item - media post object.
238 - * @param file $file - file recently added.
239 - * @param bool $has_original_media - condition is the original media has been already added.
229 + * @param object $media_item - media post object.
230 + * @param array|WP_Error $file - File data, or WP_Error on error.
231 + * @param bool $has_original_media - condition is the original media has been already added.
240 232 * @return bool `true` if the item has been added. Otherwise `false`.
241 233 */
242 234 public static function register_revision( $media_item, $file, $has_original_media ) {
243 235 if ( is_wp_error( $file ) || ! $has_original_media ) {
@@ -248,13 +240,13 @@
248 240 }
249 241 /**
250 242 * Return the `revision_history` of the given media.
251 243 *
252 - * @param number $media_id - media post ID.
244 + * @param int $media_id - media post ID.
253 245 * @return array `revision_history` array
254 246 */
255 247 public static function get_revision_history( $media_id ) {
256 - return array_reverse( get_post_meta( $media_id, self::WP_REVISION_HISTORY ) );
248 + return array_reverse( get_post_meta( $media_id, self::WP_REVISION_HISTORY, false ) );
257 249 }
258 250
259 251 /**
260 252 * Return the original media data.
@@ -273,10 +265,13 @@
273 265 * @param string $pathname Path name.
274 266 */
275 267 public static function delete_file( $pathname ) {
276 268 if ( ! file_exists( $pathname ) || ! is_file( $pathname ) ) {
277 - // let's touch a fake file to try to `really` remove the media file.
278 - touch( $pathname ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_touch
269 + $dir = dirname( $pathname );
270 + if ( is_dir( $dir ) ) {
271 + // let's touch a fake file to try to `really` remove the media file.
272 + touch( $pathname ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_touch
273 + }
279 274 }
280 275
281 276 return wp_delete_file( $pathname );
282 277 }
@@ -357,9 +352,9 @@
357 352 * Limit the number of items of the `revision_history` array.
358 353 * When the stack is overflowing the oldest item is remove from there (FIFO).
359 354 *
360 355 * @param int $media_id - media post ID.
361 - * @param null|int $limit - maximun amount of items. 20 as default.
356 + * @param null|int $limit - maximum amount of items. 20 as default.
362 357 *
363 358 * @return array items removed from `revision_history`
364 359 */
365 360 public static function limit_revision_history( $media_id, $limit = null ) {
@@ -442,11 +437,13 @@
442 437 * - update attachment file
443 438 * - preserve original media file
444 439 * - trace revision history
445 440 *
446 - * @param number $media_id - media post ID.
447 - * @param array $file_array - temporal file.
448 - * @return {Post|WP_Error} Updated media item or a WP_Error is something went wrong.
441 + * Note this does not support sideloads, only uploads.
442 + *
443 + * @param int $media_id - media post ID.
444 + * @param array $file_array - Data derived from `$_FILES` for an uploaded file.
445 + * @return WP_Post|WP_Error Updated media item or a WP_Error is something went wrong.
449 446 */
450 447 public static function edit_media_file( $media_id, $file_array ) {
451 448 $media_item = get_post( $media_id );
452 449 $has_original_media = self::get_original_media( $media_id );
@@ -462,9 +459,8 @@
462 459 // Save temporary file in the correct location.
463 460 $uploaded_file = self::save_temporary_file( $file_array, $media_id );
464 461
465 462 if ( is_wp_error( $uploaded_file ) ) {
466 - self::remove_tmp_file( $file_array );
467 463 return $uploaded_file;
468 464 }
469 465
470 466 // Revision_history control.