helper
2 months ago
importers
1 year ago
list-tables
1 month ago
marketplace-suggestions
11 months ago
meta-boxes
1 month ago
notes
2 months ago
plugin-updates
2 years ago
reports
3 months ago
settings
2 weeks ago
views
1 month ago
class-wc-admin-addons.php
9 months ago
class-wc-admin-api-keys-table-list.php
3 years ago
class-wc-admin-api-keys.php
11 months ago
class-wc-admin-assets.php
1 month ago
class-wc-admin-attributes.php
1 month ago
class-wc-admin-brands.php
4 months ago
class-wc-admin-customize.php
5 years ago
class-wc-admin-dashboard-setup.php
1 month ago
class-wc-admin-dashboard.php
4 months ago
class-wc-admin-duplicate-product.php
6 months ago
class-wc-admin-exporters.php
1 year ago
class-wc-admin-help.php
2 years ago
class-wc-admin-importers.php
11 months ago
class-wc-admin-log-table-list.php
4 months ago
class-wc-admin-marketplace-promotions.php
4 months ago
class-wc-admin-menus.php
1 month ago
class-wc-admin-meta-boxes.php
1 month ago
class-wc-admin-notices.php
1 month ago
class-wc-admin-permalink-settings.php
5 years ago
class-wc-admin-pointers.php
3 years ago
class-wc-admin-post-types.php
1 year ago
class-wc-admin-profile.php
1 year ago
class-wc-admin-reports.php
4 months ago
class-wc-admin-settings.php
3 months ago
class-wc-admin-setup-wizard.php
1 month ago
class-wc-admin-status.php
1 year ago
class-wc-admin-taxonomies.php
1 month ago
class-wc-admin-upload-downloadable-product.php
2 years ago
class-wc-admin-webhooks-table-list.php
1 month ago
class-wc-admin-webhooks.php
1 month ago
class-wc-admin.php
3 months ago
wc-admin-functions.php
7 months ago
wc-meta-box-functions.php
1 year ago
woocommerce-legacy-reports.php
1 year ago
class-wc-admin-upload-downloadable-product.php
124 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Add hooks related to uploading downloadable products. |
| 4 | * |
| 5 | * @package WooCommerce\Admin |
| 6 | * @version 8.5.0 |
| 7 | */ |
| 8 | |
| 9 | if ( ! defined( 'ABSPATH' ) ) { |
| 10 | exit; |
| 11 | } |
| 12 | |
| 13 | if ( class_exists( 'WC_Admin_Upload_Downloadable_Product', false ) ) { |
| 14 | return new WC_Admin_Upload_Downloadable_Product(); |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * WC_Admin_Upload_Downloadable_Product Class. |
| 19 | */ |
| 20 | class WC_Admin_Upload_Downloadable_Product { |
| 21 | /** |
| 22 | * Add hooks. |
| 23 | */ |
| 24 | public function __construct() { |
| 25 | add_filter( 'upload_dir', array( $this, 'upload_dir' ) ); |
| 26 | add_filter( 'wp_unique_filename', array( $this, 'update_filename' ), 10, 3 ); |
| 27 | add_action( 'media_upload_downloadable_product', array( $this, 'media_upload_downloadable_product' ) ); |
| 28 | } |
| 29 | /** |
| 30 | * Change upload dir for downloadable files. |
| 31 | * |
| 32 | * @param array $pathdata Array of paths. |
| 33 | * @return array |
| 34 | */ |
| 35 | public function upload_dir( $pathdata ) { |
| 36 | // phpcs:disable WordPress.Security.NonceVerification.Missing |
| 37 | if ( isset( $_POST['type'] ) && 'downloadable_product' === $_POST['type'] ) { |
| 38 | |
| 39 | if ( empty( $pathdata['subdir'] ) ) { |
| 40 | $pathdata['path'] = $pathdata['path'] . '/woocommerce_uploads'; |
| 41 | $pathdata['url'] = $pathdata['url'] . '/woocommerce_uploads'; |
| 42 | $pathdata['subdir'] = '/woocommerce_uploads'; |
| 43 | } else { |
| 44 | $new_subdir = '/woocommerce_uploads' . $pathdata['subdir']; |
| 45 | |
| 46 | $pathdata['path'] = str_replace( $pathdata['subdir'], $new_subdir, $pathdata['path'] ); |
| 47 | $pathdata['url'] = str_replace( $pathdata['subdir'], $new_subdir, $pathdata['url'] ); |
| 48 | $pathdata['subdir'] = str_replace( $pathdata['subdir'], $new_subdir, $pathdata['subdir'] ); |
| 49 | } |
| 50 | } |
| 51 | return $pathdata; |
| 52 | // phpcs:enable WordPress.Security.NonceVerification.Missing |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Change filename for WooCommerce uploads and prepend unique chars for security. |
| 57 | * |
| 58 | * @param string $full_filename Original filename. |
| 59 | * @param string $ext Extension of file. |
| 60 | * @param string $dir Directory path. |
| 61 | * |
| 62 | * @return string New filename with unique hash. |
| 63 | * @since 4.0 |
| 64 | */ |
| 65 | public function update_filename( $full_filename, $ext, $dir ) { |
| 66 | // phpcs:disable WordPress.Security.NonceVerification.Missing |
| 67 | if ( ! isset( $_POST['type'] ) || ! 'downloadable_product' === $_POST['type'] ) { |
| 68 | return $full_filename; |
| 69 | } |
| 70 | |
| 71 | if ( ! strpos( $dir, 'woocommerce_uploads' ) ) { |
| 72 | return $full_filename; |
| 73 | } |
| 74 | |
| 75 | if ( 'no' === get_option( 'woocommerce_downloads_add_hash_to_filename' ) ) { |
| 76 | return $full_filename; |
| 77 | } |
| 78 | |
| 79 | return $this->unique_filename( $full_filename, $ext ); |
| 80 | // phpcs:enable WordPress.Security.NonceVerification.Missing |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Change filename to append random text. |
| 85 | * |
| 86 | * @param string $full_filename Original filename with extension. |
| 87 | * @param string $ext Extension. |
| 88 | * |
| 89 | * @return string Modified filename. |
| 90 | */ |
| 91 | public function unique_filename( $full_filename, $ext ) { |
| 92 | $ideal_random_char_length = 6; // Not going with a larger length because then downloaded filename will not be pretty. |
| 93 | $max_filename_length = 255; // Max file name length for most file systems. |
| 94 | $length_to_prepend = min( $ideal_random_char_length, $max_filename_length - strlen( $full_filename ) - 1 ); |
| 95 | |
| 96 | if ( 1 > $length_to_prepend ) { |
| 97 | return $full_filename; |
| 98 | } |
| 99 | |
| 100 | $suffix = strtolower( wp_generate_password( $length_to_prepend, false, false ) ); |
| 101 | $filename = $full_filename; |
| 102 | |
| 103 | if ( strlen( $ext ) > 0 ) { |
| 104 | $filename = substr( $filename, 0, strlen( $filename ) - strlen( $ext ) ); |
| 105 | } |
| 106 | |
| 107 | $full_filename = str_replace( |
| 108 | $filename, |
| 109 | "$filename-$suffix", |
| 110 | $full_filename |
| 111 | ); |
| 112 | |
| 113 | return $full_filename; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Run a filter when uploading a downloadable product. |
| 118 | */ |
| 119 | public function woocommerce_media_upload_downloadable_product() { |
| 120 | // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment |
| 121 | do_action( 'media_upload_file' ); |
| 122 | } |
| 123 | } |
| 124 |