Skip to content

Commit bad9edc

Browse files
author
Eugene Manuilov
committed
Improved _get_file_handle() method to eliminate double downloading the same file.
1 parent 615c5bc commit bad9edc

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

classes/Visualizer/Source/Csv/Remote.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@
3030
*/
3131
class Visualizer_Source_Csv_Remote extends Visualizer_Source_Csv {
3232

33+
/**
34+
* Temporary file name used when allow_url_fopen option is disabled.
35+
*
36+
* @since 1.4.2
37+
*
38+
* @access private
39+
* @var string
40+
*/
41+
private $_tmpfile = false;
42+
3343
/**
3444
* Returns data parsed from source.
3545
*
@@ -121,19 +131,29 @@ public function getSourceName() {
121131
* @since 1.4.2
122132
*
123133
* @access protected
134+
* @staticvar boolean $allow_url_fopen Determines whether or not allow_url_fopen option is enabled.
124135
* @param string $filename Optional file name to get handle. If omitted, $_filename is used.
125136
* @return resource File handle resource on success, otherwise FALSE.
126137
*/
127138
protected function _get_file_handle( $filename = false ) {
139+
static $allow_url_fopen = null;
140+
141+
if ( $this->_tmpfile && is_readable( $this->_tmpfile ) ) {
142+
return parent::_get_file_handle( $this->_tmpfile );
143+
}
144+
145+
if ( is_null( $allow_url_fopen ) ) {
146+
$allow_url_fopen = filter_var( ini_get( 'allow_url_fopen' ), FILTER_VALIDATE_BOOLEAN );
147+
}
148+
128149
$scheme = parse_url( $this->_filename, PHP_URL_SCHEME );
129-
$allow_url_fopen = filter_var( ini_get( 'allow_url_fopen' ), FILTER_VALIDATE_BOOLEAN );
130150
if ( $allow_url_fopen && in_array( $scheme, stream_get_wrappers() ) ) {
131151
return parent::_get_file_handle( $filename );
132152
}
133153

134-
$filename = download_url( $this->_filename );
154+
$this->_tmpfile = download_url( $this->_filename );
135155

136-
return !is_wp_error( $filename ) ? parent::_get_file_handle( $filename ) : false;
156+
return !is_wp_error( $this->_tmpfile ) ? parent::_get_file_handle( $this->_tmpfile ) : false;
137157
}
138158

139159
}

0 commit comments

Comments
 (0)