admin-pages
3 years ago
core-api
3 years ago
debugger
3 years ago
markdown
4 years ago
class-jetpack-ai-helper.php
3 years ago
class-jetpack-currencies.php
5 years ago
class-jetpack-google-drive-helper.php
3 years ago
class-jetpack-instagram-gallery-helper.php
3 years ago
class-jetpack-mapbox-helper.php
3 years ago
class-jetpack-podcast-feed-locator.php
5 years ago
class-jetpack-podcast-helper.php
3 years ago
class-jetpack-recommendations.php
3 years ago
class-jetpack-tweetstorm-helper.php
3 years ago
class-jetpack-wizard.php
5 years ago
class.color.php
4 years ago
class.core-rest-api-endpoints.php
3 years ago
class.jetpack-automatic-install-skin.php
4 years ago
class.jetpack-iframe-embed.php
3 years ago
class.jetpack-keyring-service-helper.php
3 years ago
class.jetpack-password-checker.php
3 years ago
class.jetpack-search-performance-logger.php
4 years ago
class.media-extractor.php
3 years ago
class.media-summary.php
3 years ago
class.media.php
3 years ago
components.php
3 years ago
debugger.php
4 years ago
functions.wp-notify.php
3 years ago
icalendar-reader.php
3 years ago
markdown.php
3 years ago
plans.php
4 years ago
plugins.php
4 years ago
tonesque.php
3 years ago
widgets.php
4 years ago
class.media.php
514 lines
| 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 | |
| 3 | require_once JETPACK__PLUGIN_DIR . 'sal/class.json-api-date.php'; |
| 4 | |
| 5 | /** |
| 6 | * Class to handle different actions related to media. |
| 7 | */ |
| 8 | class Jetpack_Media { |
| 9 | /** |
| 10 | * Original media meta data. Metadata key as stored by WP. |
| 11 | * |
| 12 | * @var string |
| 13 | */ |
| 14 | const WP_ORIGINAL_MEDIA = '_wp_original_post_media'; |
| 15 | /** |
| 16 | * Revision history. Metadata key as stored by WP. |
| 17 | * |
| 18 | * @var string |
| 19 | */ |
| 20 | const WP_REVISION_HISTORY = '_wp_revision_history'; |
| 21 | /** |
| 22 | * Maximum amount of revisions. |
| 23 | * |
| 24 | * @var int |
| 25 | */ |
| 26 | const REVISION_HISTORY_MAXIMUM_AMOUNT = 0; |
| 27 | /** |
| 28 | * Image Alt. Metadata key as stored by WP. |
| 29 | * |
| 30 | * @var string |
| 31 | */ |
| 32 | const WP_ATTACHMENT_IMAGE_ALT = '_wp_attachment_image_alt'; |
| 33 | |
| 34 | /** |
| 35 | * Generate a filename in function of the original filename of the media. |
| 36 | * The returned name has the `{basename}-{hash}-{random-number}.{ext}` shape. |
| 37 | * The hash is built according to the filename trying to avoid name collisions |
| 38 | * with other media files. |
| 39 | * |
| 40 | * @param number $media_id - media post ID. |
| 41 | * @param string $new_filename - the new filename. |
| 42 | * @return string A random filename. |
| 43 | */ |
| 44 | public static function generate_new_filename( $media_id, $new_filename ) { |
| 45 | // Get the right filename extension. |
| 46 | $new_filename_paths = pathinfo( $new_filename ); |
| 47 | $new_file_ext = $new_filename_paths['extension']; |
| 48 | |
| 49 | // Get the file parts from the current attachment. |
| 50 | $current_file = get_attached_file( $media_id ); |
| 51 | $current_file_parts = pathinfo( $current_file ); |
| 52 | $current_file_ext = $current_file_parts['extension']; |
| 53 | $current_file_dirname = $current_file_parts['dirname']; |
| 54 | |
| 55 | // Take out filename from the original file or from the current attachment. |
| 56 | $original_media = (array) self::get_original_media( $media_id ); |
| 57 | |
| 58 | if ( ! empty( $original_media ) ) { |
| 59 | $original_file_parts = pathinfo( $original_media['file'] ); |
| 60 | $filename_base = $original_file_parts['filename']; |
| 61 | } else { |
| 62 | $filename_base = $current_file_parts['filename']; |
| 63 | } |
| 64 | |
| 65 | // Add unique seed based on the filename. |
| 66 | $filename_base .= '-' . crc32( $filename_base ) . '-'; |
| 67 | |
| 68 | $number_suffix = time() . wp_rand( 100, 999 ); |
| 69 | |
| 70 | do { |
| 71 | $filename = $filename_base; |
| 72 | $filename .= "e{$number_suffix}"; |
| 73 | $file_ext = $new_file_ext ? $new_file_ext : $current_file_ext; |
| 74 | |
| 75 | $new_filename = "{$filename}.{$file_ext}"; |
| 76 | $new_path = "{$current_file_dirname}/$new_filename"; |
| 77 | ++$number_suffix; |
| 78 | } while ( file_exists( $new_path ) ); |
| 79 | |
| 80 | return $new_filename; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * File urls use the post (image item) date to generate a folder path. |
| 85 | * Post dates can change, so we use the original date used in the `guid` |
| 86 | * url so edits can remain in the same folder. In the following function |
| 87 | * we capture a string in the format of `YYYY/MM` from the guid. |
| 88 | * |
| 89 | * For example with a guid of |
| 90 | * "http://test.files.wordpress.com/2016/10/test.png" the resulting string |
| 91 | * would be: "2016/10" |
| 92 | * |
| 93 | * @param int $media_id Attachment ID. |
| 94 | * @return string |
| 95 | */ |
| 96 | private static function get_time_string_from_guid( $media_id ) { |
| 97 | $time = gmdate( 'Y/m', strtotime( current_time( 'mysql' ) ) ); |
| 98 | |
| 99 | $media = get_post( $media_id ); |
| 100 | if ( $media ) { |
| 101 | $pattern = '/\/(\d{4}\/\d{2})\//'; |
| 102 | preg_match( $pattern, $media->guid, $matches ); |
| 103 | if ( count( $matches ) > 1 ) { |
| 104 | $time = $matches[1]; |
| 105 | } |
| 106 | } |
| 107 | return $time; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Return an array of allowed mime_type items used to upload a media file. |
| 112 | * |
| 113 | * @param array $default_mime_types Array of mime types. |
| 114 | * |
| 115 | * @return array mime_type array |
| 116 | */ |
| 117 | public static function get_allowed_mime_types( $default_mime_types ) { |
| 118 | return array_unique( |
| 119 | array_merge( |
| 120 | $default_mime_types, |
| 121 | array( |
| 122 | 'application/msword', // .doc |
| 123 | 'application/vnd.ms-powerpoint', // .ppt, .pps |
| 124 | 'application/vnd.ms-excel', // .xls |
| 125 | 'application/vnd.openxmlformats-officedocument.presentationml.presentation', // .pptx |
| 126 | 'application/vnd.openxmlformats-officedocument.presentationml.slideshow', // .ppsx |
| 127 | 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', // .xlsx |
| 128 | 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', // .docx |
| 129 | 'application/vnd.oasis.opendocument.text', // .odt |
| 130 | 'application/pdf', // .pdf |
| 131 | ) |
| 132 | ) |
| 133 | ); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Checks that the mime type of the file |
| 138 | * is among those in a filterable list of mime types. |
| 139 | * |
| 140 | * @param string $file Path to file to get its mime type. |
| 141 | * @return bool |
| 142 | */ |
| 143 | protected static function is_file_supported_for_sideloading( $file ) { |
| 144 | return jetpack_is_file_supported_for_sideloading( $file ); |
| 145 | } |
| 146 | |
| 147 | /** |
| 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, |
| 160 | * correct location according to the original file path, etc. |
| 161 | * The file type control is done through of `jetpack_supported_media_sideload_types` filter, |
| 162 | * which allows define to the users their own file types list. |
| 163 | * |
| 164 | * @param array $file_array file to save. |
| 165 | * @param int $media_id Attachment ID. |
| 166 | * @return array|WP_Error an array with information about the new file saved or a WP_Error is something went wrong. |
| 167 | */ |
| 168 | public static function save_temporary_file( $file_array, $media_id ) { |
| 169 | $tmp_filename = $file_array['tmp_name']; |
| 170 | |
| 171 | if ( ! file_exists( $tmp_filename ) ) { |
| 172 | return new WP_Error( 'invalid_input', 'No media provided in input.' ); |
| 173 | } |
| 174 | |
| 175 | // add additional mime_types through of the `jetpack_supported_media_sideload_types` filter. |
| 176 | $mime_type_static_filter = array( |
| 177 | 'Jetpack_Media', |
| 178 | 'get_allowed_mime_types', |
| 179 | ); |
| 180 | |
| 181 | add_filter( 'jetpack_supported_media_sideload_types', $mime_type_static_filter ); |
| 182 | if ( |
| 183 | ! self::is_file_supported_for_sideloading( $tmp_filename ) && |
| 184 | ! file_is_displayable_image( $tmp_filename ) |
| 185 | ) { |
| 186 | wp_delete_file( $tmp_filename ); |
| 187 | return new WP_Error( 'invalid_input', 'Invalid file type.', 403 ); |
| 188 | } |
| 189 | remove_filter( 'jetpack_supported_media_sideload_types', $mime_type_static_filter ); |
| 190 | |
| 191 | // generate a new file name. |
| 192 | $tmp_new_filename = self::generate_new_filename( $media_id, $file_array['name'] ); |
| 193 | |
| 194 | // start to create the parameters to move the temporal file. |
| 195 | $overrides = array( 'test_form' => false ); |
| 196 | |
| 197 | // get time according to the original filaname. |
| 198 | $time = self::get_time_string_from_guid( $media_id ); |
| 199 | |
| 200 | $file_array['name'] = $tmp_new_filename; |
| 201 | $file = wp_handle_sideload( $file_array, $overrides, $time ); |
| 202 | |
| 203 | self::remove_tmp_file( $file_array ); |
| 204 | |
| 205 | if ( isset( $file['error'] ) ) { |
| 206 | return new WP_Error( 'upload_error', $file['error'] ); |
| 207 | } |
| 208 | |
| 209 | return $file; |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Return an object with an snapshot of a revision item. |
| 214 | * |
| 215 | * @param object $media_item - media post object. |
| 216 | * @return object a revision item |
| 217 | */ |
| 218 | public static function get_snapshot( $media_item ) { |
| 219 | $current_file = get_attached_file( $media_item->ID ); |
| 220 | $file_paths = pathinfo( $current_file ); |
| 221 | |
| 222 | $snapshot = array( |
| 223 | 'date' => (string) WPCOM_JSON_API_Date::format_date( $media_item->post_modified_gmt, $media_item->post_modified ), |
| 224 | 'URL' => (string) wp_get_attachment_url( $media_item->ID ), |
| 225 | 'file' => (string) $file_paths['basename'], |
| 226 | 'extension' => (string) $file_paths['extension'], |
| 227 | 'mime_type' => (string) $media_item->post_mime_type, |
| 228 | 'size' => (int) filesize( $current_file ), |
| 229 | ); |
| 230 | |
| 231 | return (object) $snapshot; |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Add a new item into revision_history array. |
| 236 | * |
| 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. |
| 240 | * @return bool `true` if the item has been added. Otherwise `false`. |
| 241 | */ |
| 242 | public static function register_revision( $media_item, $file, $has_original_media ) { |
| 243 | if ( is_wp_error( $file ) || ! $has_original_media ) { |
| 244 | return false; |
| 245 | } |
| 246 | |
| 247 | add_post_meta( $media_item->ID, self::WP_REVISION_HISTORY, self::get_snapshot( $media_item ) ); |
| 248 | } |
| 249 | /** |
| 250 | * Return the `revision_history` of the given media. |
| 251 | * |
| 252 | * @param number $media_id - media post ID. |
| 253 | * @return array `revision_history` array |
| 254 | */ |
| 255 | public static function get_revision_history( $media_id ) { |
| 256 | return array_reverse( get_post_meta( $media_id, self::WP_REVISION_HISTORY ) ); |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Return the original media data. |
| 261 | * |
| 262 | * @param int $media_id Attachment ID. |
| 263 | */ |
| 264 | public static function get_original_media( $media_id ) { |
| 265 | $original = get_post_meta( $media_id, self::WP_ORIGINAL_MEDIA, true ); |
| 266 | $original = $original ? $original : array(); |
| 267 | return $original; |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Delete a file. |
| 272 | * |
| 273 | * @param string $pathname Path name. |
| 274 | */ |
| 275 | public static function delete_file( $pathname ) { |
| 276 | 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 |
| 279 | } |
| 280 | |
| 281 | return wp_delete_file( $pathname ); |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Try to delete a file according to the dirname of |
| 286 | * the media attached file and the filename. |
| 287 | * |
| 288 | * @param int $media_id - media post ID. |
| 289 | * @param string $filename - basename of the file ( name-of-file.ext ). |
| 290 | * |
| 291 | * @return void |
| 292 | */ |
| 293 | private static function delete_media_history_file( $media_id, $filename ) { |
| 294 | $attached_path = get_attached_file( $media_id ); |
| 295 | $attached_parts = pathinfo( $attached_path ); |
| 296 | $dirname = $attached_parts['dirname']; |
| 297 | |
| 298 | $pathname = $dirname . '/' . $filename; |
| 299 | |
| 300 | // remove thumbnails. |
| 301 | $metadata = wp_generate_attachment_metadata( $media_id, $pathname ); |
| 302 | |
| 303 | if ( isset( $metadata ) && isset( $metadata['sizes'] ) ) { |
| 304 | foreach ( $metadata['sizes'] as $properties ) { |
| 305 | self::delete_file( $dirname . '/' . $properties['file'] ); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | // remove primary file. |
| 310 | self::delete_file( $pathname ); |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * Remove specific items from the `revision history` array |
| 315 | * depending on the given criteria: array( |
| 316 | * 'from' => (int) <from>, |
| 317 | * 'to' => (int) <to>, |
| 318 | * ) |
| 319 | * |
| 320 | * Also, it removes the file defined in each item. |
| 321 | * |
| 322 | * @param int $media_id - media post ID. |
| 323 | * @param array $criteria - criteria to remove the items. |
| 324 | * @param array $revision_history - revision history array. |
| 325 | * |
| 326 | * @return array `revision_history` array updated. |
| 327 | */ |
| 328 | public static function remove_items_from_revision_history( $media_id, $criteria, $revision_history ) { |
| 329 | if ( ! isset( $revision_history ) ) { |
| 330 | $revision_history = self::get_revision_history( $media_id ); |
| 331 | } |
| 332 | |
| 333 | $from = $criteria['from']; |
| 334 | $to = $criteria['to'] ? $criteria['to'] : ( $from + 1 ); |
| 335 | |
| 336 | for ( $i = $from; $i < $to; $i++ ) { |
| 337 | $removed_item = array_slice( $revision_history, $from, 1 ); |
| 338 | if ( ! $removed_item ) { |
| 339 | break; |
| 340 | } |
| 341 | |
| 342 | array_splice( $revision_history, $from, 1 ); |
| 343 | self::delete_media_history_file( $media_id, $removed_item[0]->file ); |
| 344 | } |
| 345 | |
| 346 | // override all history items. |
| 347 | delete_post_meta( $media_id, self::WP_REVISION_HISTORY ); |
| 348 | $revision_history = array_reverse( $revision_history ); |
| 349 | foreach ( $revision_history as &$item ) { |
| 350 | add_post_meta( $media_id, self::WP_REVISION_HISTORY, $item ); |
| 351 | } |
| 352 | |
| 353 | return $revision_history; |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * Limit the number of items of the `revision_history` array. |
| 358 | * When the stack is overflowing the oldest item is remove from there (FIFO). |
| 359 | * |
| 360 | * @param int $media_id - media post ID. |
| 361 | * @param null|int $limit - maximun amount of items. 20 as default. |
| 362 | * |
| 363 | * @return array items removed from `revision_history` |
| 364 | */ |
| 365 | public static function limit_revision_history( $media_id, $limit = null ) { |
| 366 | if ( $limit === null ) { |
| 367 | $limit = self::REVISION_HISTORY_MAXIMUM_AMOUNT; |
| 368 | } |
| 369 | |
| 370 | $revision_history = self::get_revision_history( $media_id ); |
| 371 | |
| 372 | $total = count( $revision_history ); |
| 373 | |
| 374 | if ( $total < $limit ) { |
| 375 | return array(); |
| 376 | } |
| 377 | |
| 378 | self::remove_items_from_revision_history( |
| 379 | $media_id, |
| 380 | array( |
| 381 | 'from' => $limit, |
| 382 | 'to' => $total, |
| 383 | ), |
| 384 | $revision_history |
| 385 | ); |
| 386 | |
| 387 | return self::get_revision_history( $media_id ); |
| 388 | } |
| 389 | |
| 390 | /** |
| 391 | * Remove the original file and clean the post metadata. |
| 392 | * |
| 393 | * @param int $media_id - media post ID. |
| 394 | */ |
| 395 | public static function clean_original_media( $media_id ) { |
| 396 | $original_file = self::get_original_media( $media_id ); |
| 397 | |
| 398 | if ( ! $original_file ) { |
| 399 | return null; |
| 400 | } |
| 401 | |
| 402 | self::delete_media_history_file( $media_id, $original_file->file ); |
| 403 | return delete_post_meta( $media_id, self::WP_ORIGINAL_MEDIA ); |
| 404 | } |
| 405 | |
| 406 | /** |
| 407 | * Clean `revision_history` of the given $media_id. it means: |
| 408 | * - remove all media files tied to the `revision_history` items. |
| 409 | * - clean `revision_history` meta data. |
| 410 | * - remove and clean the `original_media` |
| 411 | * |
| 412 | * @param int $media_id - media post ID. |
| 413 | * |
| 414 | * @return array results of removing these files |
| 415 | */ |
| 416 | public static function clean_revision_history( $media_id ) { |
| 417 | self::clean_original_media( $media_id ); |
| 418 | |
| 419 | $revision_history = self::get_revision_history( $media_id ); |
| 420 | $total = count( $revision_history ); |
| 421 | $updated_history = array(); |
| 422 | |
| 423 | if ( $total < 1 ) { |
| 424 | return $updated_history; |
| 425 | } |
| 426 | |
| 427 | $updated_history = self::remove_items_from_revision_history( |
| 428 | $media_id, |
| 429 | array( |
| 430 | 'from' => 0, |
| 431 | 'to' => $total, |
| 432 | ), |
| 433 | $revision_history |
| 434 | ); |
| 435 | |
| 436 | return $updated_history; |
| 437 | } |
| 438 | |
| 439 | /** |
| 440 | * Edit media item process: |
| 441 | * |
| 442 | * - update attachment file |
| 443 | * - preserve original media file |
| 444 | * - trace revision history |
| 445 | * |
| 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. |
| 449 | */ |
| 450 | public static function edit_media_file( $media_id, $file_array ) { |
| 451 | $media_item = get_post( $media_id ); |
| 452 | $has_original_media = self::get_original_media( $media_id ); |
| 453 | |
| 454 | if ( ! $has_original_media ) { |
| 455 | |
| 456 | // The first time that the media is updated |
| 457 | // the original media is stored into the revision_history. |
| 458 | $snapshot = self::get_snapshot( $media_item ); |
| 459 | add_post_meta( $media_id, self::WP_ORIGINAL_MEDIA, $snapshot, true ); |
| 460 | } |
| 461 | |
| 462 | // Save temporary file in the correct location. |
| 463 | $uploaded_file = self::save_temporary_file( $file_array, $media_id ); |
| 464 | |
| 465 | if ( is_wp_error( $uploaded_file ) ) { |
| 466 | self::remove_tmp_file( $file_array ); |
| 467 | return $uploaded_file; |
| 468 | } |
| 469 | |
| 470 | // Revision_history control. |
| 471 | self::register_revision( $media_item, $uploaded_file, $has_original_media ); |
| 472 | |
| 473 | $uploaded_path = $uploaded_file['file']; |
| 474 | $udpated_mime_type = $uploaded_file['type']; |
| 475 | $was_updated = update_attached_file( $media_id, $uploaded_path ); |
| 476 | |
| 477 | if ( ! $was_updated ) { |
| 478 | return new WP_Error( 'update_error', 'Media update error' ); |
| 479 | } |
| 480 | |
| 481 | // Check maximum amount of revision_history before updating the attachment metadata. |
| 482 | self::limit_revision_history( $media_id ); |
| 483 | |
| 484 | $new_metadata = wp_generate_attachment_metadata( $media_id, $uploaded_path ); |
| 485 | wp_update_attachment_metadata( $media_id, $new_metadata ); |
| 486 | |
| 487 | $edited_action = wp_update_post( |
| 488 | (object) array( |
| 489 | 'ID' => $media_id, |
| 490 | 'post_mime_type' => $udpated_mime_type, |
| 491 | ), |
| 492 | true |
| 493 | ); |
| 494 | |
| 495 | if ( is_wp_error( $edited_action ) ) { |
| 496 | return $edited_action; |
| 497 | } |
| 498 | |
| 499 | return $media_item; |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | // phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed -- TODO: Move these functions to some other file. |
| 504 | |
| 505 | /** |
| 506 | * Clean revision history when the media item is deleted. |
| 507 | * |
| 508 | * @param int $media_id Attachment ID. |
| 509 | */ |
| 510 | function jetpack_clean_revision_history( $media_id ) { |
| 511 | Jetpack_Media::clean_revision_history( $media_id ); |
| 512 | } |
| 513 | add_action( 'delete_attachment', 'jetpack_clean_revision_history' ); |
| 514 |