| @@ -85,45 +85,34 @@ | ||
| 85 | 85 | * 'visualizer_xlsx_max_filesize') before handing the path to the parser, |
| 86 | 86 | * guarding against ZIP-bomb and DoS attacks. |
| 87 | 87 | * |
| 88 | 88 | * @access protected |
| 89 | - * @return string Path to the temporary file, or the original URL if download failed. | |
| 89 | + * @return string|false Path to the temporary file, or false if download failed. | |
| 90 | 90 | */ |
| 91 | 91 | protected function _get_file_path() { |
| 92 | - if ( $this->_tmpfile && ! is_wp_error( $this->_tmpfile ) && is_readable( $this->_tmpfile ) ) { | |
| 92 | + if ( $this->_tmpfile && is_readable( $this->_tmpfile ) ) { | |
| 93 | 93 | return $this->_tmpfile; |
| 94 | 94 | } |
| 95 | 95 | |
| 96 | - require_once ABSPATH . 'wp-admin/includes/file.php'; | |
| 97 | - | |
| 98 | - $this->_tmpfile = download_url( $this->_filename ); | |
| 99 | - | |
| 100 | - if ( is_wp_error( $this->_tmpfile ) ) { | |
| 101 | - $this->_error = esc_html__( 'Could not download the XLSX file. Please check the URL and try again.', 'visualizer' ); | |
| 102 | - $this->_tmpfile = false; | |
| 103 | - // Return the original URL so the parent's open() call will fail | |
| 104 | - // gracefully and set an error rather than throwing a PHP error. | |
| 105 | - return $this->_filename; | |
| 96 | + $max_bytes = (int) apply_filters( 'visualizer_xlsx_max_filesize', 10 * 1024 * 1024 ); | |
| 97 | + $tmpfile = Visualizer_Remote_Fetch::download( | |
| 98 | + $this->_filename, | |
| 99 | + array( 'limit_response_size' => $max_bytes ) | |
| 100 | + ); | |
| 101 | + if ( is_wp_error( $tmpfile ) ) { | |
| 102 | + $this->_error = 'visualizer_remote_size' === $tmpfile->get_error_code() | |
| 103 | + ? esc_html__( 'The XLSX file exceeds the maximum allowed size and cannot be imported.', 'visualizer' ) | |
| 104 | + : esc_html__( 'Could not download the XLSX file. Please check the URL and try again.', 'visualizer' ); | |
| 105 | + return false; | |
| 106 | 106 | } |
| 107 | + $this->_tmpfile = $tmpfile; | |
| 107 | 108 | |
| 108 | 109 | if ( ! is_file( $this->_tmpfile ) ) { |
| 109 | 110 | $this->_tmpfile = false; |
| 110 | 111 | $this->_error = esc_html__( 'Could not access the downloaded XLSX file. Please try again.', 'visualizer' ); |
| 111 | - return $this->_filename; | |
| 112 | + return false; | |
| 112 | 113 | } |
| 113 | 114 | |
| 114 | - // Maximum allowed file size in bytes. Default 10 MB; override via filter. | |
| 115 | - $max_bytes = (int) apply_filters( 'visualizer_xlsx_max_filesize', 10 * 1024 * 1024 ); | |
| 116 | - if ( filesize( $this->_tmpfile ) > $max_bytes ) { | |
| 117 | - @unlink( $this->_tmpfile ); // phpcs:ignore WordPress.PHP.NoSilencedErrors | |
| 118 | - $this->_tmpfile = false; | |
| 119 | - $this->_error = esc_html__( | |
| 120 | - 'The XLSX file exceeds the maximum allowed size and cannot be imported.', | |
| 121 | - 'visualizer' | |
| 122 | - ); | |
| 123 | - return $this->_filename; | |
| 124 | - } | |
| 125 | - | |
| 126 | 115 | return $this->_tmpfile; |
| 127 | 116 | } |
| 128 | 117 | |
| 129 | 118 | /** |
| @@ -135,10 +124,10 @@ | ||
| 135 | 124 | public function fetch() { |
| 136 | 125 | $result = parent::fetch(); |
| 137 | 126 | |
| 138 | 127 | // Clean up the temporary file after parsing. |
| 139 | - if ( $this->_tmpfile && ! is_wp_error( $this->_tmpfile ) && is_file( $this->_tmpfile ) ) { | |
| 140 | - @unlink( $this->_tmpfile ); // phpcs:ignore WordPress.PHP.NoSilencedErrors | |
| 128 | + if ( $this->_tmpfile && is_file( $this->_tmpfile ) ) { | |
| 129 | + wp_delete_file( $this->_tmpfile ); | |
| 141 | 130 | $this->_tmpfile = false; |
| 142 | 131 | } |
| 143 | 132 | |
| 144 | 133 | return $result; |