| 1 |
<?php |
| 2 |
|
| 3 |
// +----------------------------------------------------------------------+ |
| 4 |
// | Copyright 2013 Madpixels (email : visualizer@madpixels.net) | |
| 5 |
// +----------------------------------------------------------------------+ |
| 6 |
// | This program is free software; you can redistribute it and/or modify | |
| 7 |
// | it under the terms of the GNU General Public License, version 2, as | |
| 8 |
// | published by the Free Software Foundation. | |
| 9 |
// | | |
| 10 |
// | This program is distributed in the hope that it will be useful, | |
| 11 |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 |
// | GNU General Public License for more details. | |
| 14 |
// | | |
| 15 |
// | You should have received a copy of the GNU General Public License | |
| 16 |
// | along with this program; if not, write to the Free Software | |
| 17 |
// | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, | |
| 18 |
// | MA 02110-1301 USA | |
| 19 |
// +----------------------------------------------------------------------+ |
| 20 |
// | Author: Eugene Manuilov <eugene@manuilov.org> | |
| 21 |
// +----------------------------------------------------------------------+ |
| 22 |
/** |
| 23 |
* Source manager for remote CSV files. |
| 24 |
* |
| 25 |
* @category Visualizer |
| 26 |
* @package Source |
| 27 |
* |
| 28 |
* @since 1.1.0 |
| 29 |
*/ |
| 30 |
class Visualizer_Source_Csv_Remote extends Visualizer_Source_Csv { |
| 31 |
|
| 32 |
/** |
| 33 |
* Path to the safely downloaded temporary file. |
| 34 |
* |
| 35 |
* @since 1.4.2 |
| 36 |
* |
| 37 |
* @access private |
| 38 |
* @var string|false |
| 39 |
*/ |
| 40 |
private $_tmpfile = false; |
| 41 |
|
| 42 |
/** |
| 43 |
* Returns data parsed from source. |
| 44 |
* |
| 45 |
* @since 1.1.0 |
| 46 |
* |
| 47 |
* @access public |
| 48 |
* @return string The serialized array of data. |
| 49 |
*/ |
| 50 |
public function getData( $dumb = false ) { |
| 51 |
return serialize( |
| 52 |
array( |
| 53 |
'source' => $this->_filename, |
| 54 |
'data' => $this->_data, |
| 55 |
) |
| 56 |
); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Re populates data and series. |
| 61 |
* |
| 62 |
* @since 1.1.0 |
| 63 |
* |
| 64 |
* @access private |
| 65 |
* @param int $chart_id The chart id. |
| 66 |
* @return boolean TRUE on success, otherwise FALSE. |
| 67 |
*/ |
| 68 |
private function _repopulate( $chart_id ) { |
| 69 |
// if it has been already populated, then just return true |
| 70 |
if ( ! empty( $this->_data ) && ! empty( $this->_series ) ) { |
| 71 |
return true; |
| 72 |
} |
| 73 |
|
| 74 |
// if filename is empty, extract it from chart content |
| 75 |
if ( empty( $this->_filename ) ) { |
| 76 |
$chart = get_post( $chart_id ); |
| 77 |
$data = Visualizer_Module::decode_content( html_entity_decode( $chart->post_content ) ); |
| 78 |
if ( ! isset( $data['source'] ) ) { |
| 79 |
return false; |
| 80 |
} |
| 81 |
|
| 82 |
$this->_filename = $data['source']; |
| 83 |
} |
| 84 |
|
| 85 |
// populate series and data information |
| 86 |
return $this->fetch(); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Re populates data. |
| 91 |
* |
| 92 |
* @since 1.1.0 |
| 93 |
* |
| 94 |
* @access public |
| 95 |
* @param array $data The actual array of data. |
| 96 |
* @param int $chart_id The chart id. |
| 97 |
* @return array The re populated array of data or old one. |
| 98 |
*/ |
| 99 |
public function repopulateData( $data, $chart_id ) { |
| 100 |
if ( ! is_array( $data ) ) { |
| 101 |
$data = array(); |
| 102 |
} |
| 103 |
return array_key_exists( 'data', $data ) ? $data['data'] : $data; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Re populates series. |
| 108 |
* |
| 109 |
* @since 1.1.0 |
| 110 |
* |
| 111 |
* @access public |
| 112 |
* @param array $series The actual array of series. |
| 113 |
* @param int $chart_id The chart id. |
| 114 |
* @return array The re populated array of series or old one. |
| 115 |
*/ |
| 116 |
public function repopulateSeries( $series, $chart_id ) { |
| 117 |
return $series; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Returns source name. |
| 122 |
* |
| 123 |
* @since 1.0.0 |
| 124 |
* |
| 125 |
* @access public |
| 126 |
* @return string The name of source. |
| 127 |
*/ |
| 128 |
public function getSourceName() { |
| 129 |
return __CLASS__; |
| 130 |
} |
| 131 |
|
| 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 |
/** |
| 150 |
* Returns file handle to fetch data from. |
| 151 |
* |
| 152 |
* @since 1.4.2 |
| 153 |
* |
| 154 |
* @access protected |
| 155 |
* @param string $filename Optional file name to get handle. If omitted, $_filename is used. |
| 156 |
* @return resource|false File handle resource on success, otherwise FALSE. |
| 157 |
*/ |
| 158 |
protected function _get_file_handle( $filename = false ) { |
| 159 |
if ( $this->_tmpfile && is_readable( $this->_tmpfile ) ) { |
| 160 |
return parent::_get_file_handle( $this->_tmpfile ); |
| 161 |
} |
| 162 |
|
| 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; |
| 167 |
} |
| 168 |
|
| 169 |
$this->_tmpfile = $tmpfile; |
| 170 |
return parent::_get_file_handle( $this->_tmpfile ); |
| 171 |
} |
| 172 |
} |
| 173 |
|