| @@ -29,14 +29,14 @@ | ||
| 29 | 29 | */ |
| 30 | 30 | class Visualizer_Source_Csv_Remote extends Visualizer_Source_Csv { |
| 31 | 31 | |
| 32 | 32 | /** |
| 33 | - * Temporary file name used when allow_url_fopen option is disabled. | |
| 33 | + * Path to the safely downloaded temporary file. | |
| 34 | 34 | * |
| 35 | 35 | * @since 1.4.2 |
| 36 | 36 | * |
| 37 | 37 | * @access private |
| 38 | - * @var string | |
| 38 | + * @var string|false | |
| 39 | 39 | */ |
| 40 | 40 | private $_tmpfile = false; |
| 41 | 41 | |
| 42 | 42 | /** |
| @@ -46,9 +46,9 @@ | ||
| 46 | 46 | * |
| 47 | 47 | * @access public |
| 48 | 48 | * @return string The serialized array of data. |
| 49 | 49 | */ |
| 50 | - public function getData() { | |
| 50 | + public function getData( $dumb = false ) { | |
| 51 | 51 | return serialize( |
| 52 | 52 | array( |
| 53 | 53 | 'source' => $this->_filename, |
| 54 | 54 | 'data' => $this->_data, |
| @@ -73,9 +73,9 @@ | ||
| 73 | 73 | |
| 74 | 74 | // if filename is empty, extract it from chart content |
| 75 | 75 | if ( empty( $this->_filename ) ) { |
| 76 | 76 | $chart = get_post( $chart_id ); |
| 77 | - $data = unserialize( html_entity_decode( $chart->post_content ) ); | |
| 77 | + $data = Visualizer_Module::decode_content( html_entity_decode( $chart->post_content ) ); | |
| 78 | 78 | if ( ! isset( $data['source'] ) ) { |
| 79 | 79 | return false; |
| 80 | 80 | } |
| 81 | 81 | |
| @@ -96,9 +96,12 @@ | ||
| 96 | 96 | * @param int $chart_id The chart id. |
| 97 | 97 | * @return array The re populated array of data or old one. |
| 98 | 98 | */ |
| 99 | 99 | public function repopulateData( $data, $chart_id ) { |
| 100 | - return $this->_repopulate( $chart_id ) ? $this->_data : $data; | |
| 100 | + if ( ! is_array( $data ) ) { | |
| 101 | + $data = array(); | |
| 102 | + } | |
| 103 | + return array_key_exists( 'data', $data ) ? $data['data'] : $data; | |
| 101 | 104 | } |
| 102 | 105 | |
| 103 | 106 | /** |
| 104 | 107 | * Re populates series. |
| @@ -110,9 +113,9 @@ | ||
| 110 | 113 | * @param int $chart_id The chart id. |
| 111 | 114 | * @return array The re populated array of series or old one. |
| 112 | 115 | */ |
| 113 | 116 | public function repopulateSeries( $series, $chart_id ) { |
| 114 | - return $this->_repopulate( $chart_id ) ? $this->_series : $series; | |
| 117 | + return $series; | |
| 115 | 118 | } |
| 116 | 119 | |
| 117 | 120 | /** |
| 118 | 121 | * Returns source name. |
| @@ -126,37 +129,44 @@ | ||
| 126 | 129 | return __CLASS__; |
| 127 | 130 | } |
| 128 | 131 | |
| 129 | 132 | /** |
| 133 | + * Fetches the remote file and removes its temporary copy. | |
| 134 | + * | |
| 135 | + * @access public | |
| 136 | + * @return boolean TRUE on success, otherwise FALSE. | |
| 137 | + */ | |
| 138 | + public function fetch() { | |
| 139 | + $result = parent::fetch(); | |
| 140 | + | |
| 141 | + if ( $this->_tmpfile && is_file( $this->_tmpfile ) ) { | |
| 142 | + wp_delete_file( $this->_tmpfile ); | |
| 143 | + $this->_tmpfile = false; | |
| 144 | + } | |
| 145 | + | |
| 146 | + return $result; | |
| 147 | + } | |
| 148 | + | |
| 149 | + /** | |
| 130 | 150 | * Returns file handle to fetch data from. |
| 131 | 151 | * |
| 132 | 152 | * @since 1.4.2 |
| 133 | 153 | * |
| 134 | 154 | * @access protected |
| 135 | - * @staticvar boolean $allow_url_fopen Determines whether or not allow_url_fopen option is enabled. | |
| 136 | 155 | * @param string $filename Optional file name to get handle. If omitted, $_filename is used. |
| 137 | - * @return resource File handle resource on success, otherwise FALSE. | |
| 156 | + * @return resource|false File handle resource on success, otherwise FALSE. | |
| 138 | 157 | */ |
| 139 | 158 | protected function _get_file_handle( $filename = false ) { |
| 140 | - static $allow_url_fopen = null; | |
| 141 | - | |
| 142 | - if ( ! is_wp_error( $this->_tmpfile ) && $this->_tmpfile && is_readable( $this->_tmpfile ) ) { | |
| 159 | + if ( $this->_tmpfile && is_readable( $this->_tmpfile ) ) { | |
| 143 | 160 | return parent::_get_file_handle( $this->_tmpfile ); |
| 144 | 161 | } |
| 145 | 162 | |
| 146 | - if ( is_null( $allow_url_fopen ) ) { | |
| 147 | - $allow_url_fopen = filter_var( ini_get( 'allow_url_fopen' ), FILTER_VALIDATE_BOOLEAN ); | |
| 163 | + $tmpfile = Visualizer_Remote_Fetch::download( $this->_filename ); | |
| 164 | + if ( is_wp_error( $tmpfile ) ) { | |
| 165 | + $this->_error = esc_html__( 'Could not download the file. Please check the URL and try again.', 'visualizer' ); | |
| 166 | + return false; | |
| 148 | 167 | } |
| 149 | 168 | |
| 150 | - $scheme = parse_url( $this->_filename, PHP_URL_SCHEME ); | |
| 151 | - if ( $allow_url_fopen && in_array( $scheme, stream_get_wrappers() ) ) { | |
| 152 | - return parent::_get_file_handle( $filename ); | |
| 153 | - } | |
| 154 | - | |
| 155 | - require_once ABSPATH . 'wp-admin/includes/file.php'; | |
| 156 | - | |
| 157 | - $this->_tmpfile = download_url( $this->_filename ); | |
| 158 | - | |
| 159 | - return ! is_wp_error( $this->_tmpfile ) ? parent::_get_file_handle( $this->_tmpfile ) : false; | |
| 169 | + $this->_tmpfile = $tmpfile; | |
| 170 | + return parent::_get_file_handle( $this->_tmpfile ); | |
| 160 | 171 | } |
| 161 | - | |
| 162 | 172 | } |