PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 4.0.4
Visualizer – Tables & Charts Manager with Built-in AI Generator v4.0.4
4.0.7 4.0.6 4.0.5 4.0.4 4.0.3 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.2 3.1.3 3.10.0 3.10.1 3.10.10 3.10.11 3.10.12 3.10.13 3.10.14 3.10.15 3.10.2 3.10.3 3.10.4 All 148 releases
visualizer / classes / Visualizer / Source / Xlsx / Remote.php

Remote.php in Visualizer – Tables & Charts Manager with Built-in AI Generator 4.0.4, at classes/Visualizer/Source/Xlsx/Remote.php

147 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Source manager for remote XLSX files loaded from a URL.
4 *
5 * Downloads the file to a temporary location before parsing so that
6 * OpenSpout (which does not support stream wrappers for XLSX) can read it.
7 * The source URL is stored in the chart's post_content so that scheduled
8 * re-imports can re-fetch and re-parse the file automatically.
9 *
10 * @category Visualizer
11 * @package Source
12 *
13 * @since 3.11.0
14 */
15 class Visualizer_Source_Xlsx_Remote extends Visualizer_Source_Xlsx {
16
17 /**
18 * Path to the downloaded temporary file.
19 *
20 * @access private
21 * @var string|false
22 */
23 private $_tmpfile = false;
24
25 /**
26 * Returns serialised data that also stores the source URL so that
27 * scheduled re-imports know where to fetch the file from.
28 *
29 * @access public
30 * @param bool $dumb Unused; kept for interface compatibility.
31 * @return string Serialised array with 'source' and 'data' keys.
32 */
33 public function getData( $dumb = false ) {
34 return serialize(
35 array(
36 'source' => $this->_filename,
37 'data' => $this->_data,
38 )
39 );
40 }
41
42
43 /**
44 * Re-populates data for scheduled refreshes.
45 *
46 * @access public
47 * @param array $data The current data array.
48 * @param int $chart_id The chart post ID.
49 * @return array The re-populated data or the original array.
50 */
51 public function repopulateData( $data, $chart_id ) {
52 if ( ! is_array( $data ) ) {
53 $data = array();
54 }
55 return array_key_exists( 'data', $data ) ? $data['data'] : $data;
56 }
57
58 /**
59 * Re-populates series (series are stored in post meta and remain stable).
60 *
61 * @access public
62 * @param array $series The current series array.
63 * @param int $chart_id The chart post ID.
64 * @return array The original series array.
65 */
66 public function repopulateSeries( $series, $chart_id ) {
67 return $series;
68 }
69
70 /**
71 * Returns the source name.
72 *
73 * @access public
74 * @return string
75 */
76 public function getSourceName() {
77 return __CLASS__;
78 }
79
80 /**
81 * Downloads the remote XLSX file to a temporary path and returns that path
82 * for the parent reader to use.
83 *
84 * Enforces a maximum file size (default 10 MB, filterable via
85 * 'visualizer_xlsx_max_filesize') before handing the path to the parser,
86 * guarding against ZIP-bomb and DoS attacks.
87 *
88 * @access protected
89 * @return string Path to the temporary file, or the original URL if download failed.
90 */
91 protected function _get_file_path() {
92 if ( $this->_tmpfile && ! is_wp_error( $this->_tmpfile ) && is_readable( $this->_tmpfile ) ) {
93 return $this->_tmpfile;
94 }
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;
106 }
107
108 if ( ! is_file( $this->_tmpfile ) ) {
109 $this->_tmpfile = false;
110 $this->_error = esc_html__( 'Could not access the downloaded XLSX file. Please try again.', 'visualizer' );
111 return $this->_filename;
112 }
113
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 return $this->_tmpfile;
127 }
128
129 /**
130 * Fetches the remote file, parses it, and cleans up the temp file.
131 *
132 * @access public
133 * @return boolean TRUE on success, FALSE on failure.
134 */
135 public function fetch() {
136 $result = parent::fetch();
137
138 // 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
141 $this->_tmpfile = false;
142 }
143
144 return $result;
145 }
146 }
147