PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 2.1.8
Visualizer – Tables & Charts Manager with Built-in AI Generator v2.1.8
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
← All changes | classes/Visualizer/Module.php +0 -252 3.0.82.1.8 View file →
@@ -62,12 +62,8 @@
62 62 global $wpdb;
63 63
64 64 $this->_wpdb = $wpdb;
65 65 $this->_plugin = $plugin;
66 -
67 - $this->_addFilter( Visualizer_Plugin::FILTER_UNDO_REVISIONS, 'undoRevisions', 10, 2 );
68 - $this->_addFilter( Visualizer_Plugin::FILTER_HANDLE_REVISIONS, 'handleExistingRevisions', 10, 2 );
69 -
70 66 }
71 67
72 68 /**
73 69 * Registers an action hook.
@@ -144,255 +140,7 @@
144 140 */
145 141 protected function _addShortcode( $tag, $method ) {
146 142 add_shortcode( $tag, array( $this, $method ) );
147 143 return $this;
148 - }
149 -
150 - /**
151 - * Extracts the data for a chart and prepares it for the given type.
152 - *
153 - * @access public
154 - * @param int $chart_id The chart id.
155 - * @param string $type The exported type.
156 - */
157 - public function _getDataAs( $chart_id, $type ) {
158 - $final = null;
159 - $success = false;
160 - if ( $chart_id ) {
161 - $chart = get_post( $chart_id );
162 - $success = $chart && $chart->post_type == Visualizer_Plugin::CPT_VISUALIZER;
163 - }
164 - if ( $success ) {
165 - $settings = get_post_meta( $chart_id, Visualizer_Plugin::CF_SETTINGS, true );
166 - $rows = array();
167 - $series = get_post_meta( $chart_id, Visualizer_Plugin::CF_SERIES, true );
168 - $data = unserialize( $chart->post_content );
169 - if ( ! empty( $series ) ) {
170 - $row = array();
171 - foreach ( $series as $array ) {
172 - $row[] = $array['label'];
173 - }
174 - $rows[] = $row;
175 - $row = array();
176 - foreach ( $series as $array ) {
177 - $row[] = $array['type'];
178 - }
179 - $rows[] = $row;
180 - }
181 - if ( ! empty( $data ) ) {
182 - foreach ( $data as $array ) {
183 - // ignore strings
184 - if ( ! is_array( $array ) ) {
185 - continue;
186 - }
187 - // if this is an array of arrays...
188 - if ( is_array( $array[0] ) ) {
189 - foreach ( $array as $arr ) {
190 - $rows[] = $arr;
191 - }
192 - } else {
193 - // just an array
194 - $rows[] = $array;
195 - }
196 - }
197 - }
198 -
199 - $filename = isset( $settings['title'] ) && ! empty( $settings['title'] ) ? $settings['title'] : 'visualizer#' . $chart_id;
200 -
201 - switch ( $type ) {
202 - case 'csv':
203 - $final = $this->_getCSV( $rows, $filename );
204 - break;
205 - case 'xls':
206 - $final = $this->_getExcel( $rows, $filename );
207 - break;
208 - case 'print':
209 - $final = $this->_getHTML( $rows );
210 - break;
211 - }
212 - }
213 - return $final;
214 - }
215 -
216 - /**
217 - * Prepares a CSV.
218 - *
219 - * @access private
220 - * @param array $rows The array of data.
221 - * @param string $filename The name of the file to use.
222 - */
223 - private function _getCSV( $rows, $filename ) {
224 - $filename .= '.csv';
225 -
226 - $fp = tmpfile();
227 - // support for MS Excel
228 - fprintf( $fp, $bom = ( chr( 0xEF ) . chr( 0xBB ) . chr( 0xBF ) ) );
229 - foreach ( $rows as $row ) {
230 - fputcsv( $fp, $row );
231 - }
232 - rewind( $fp );
233 - $csv = '';
234 - while ( ( $array = fgetcsv( $fp ) ) !== false ) {
235 - if ( strlen( $csv ) > 0 ) {
236 - $csv .= PHP_EOL;
237 - }
238 - $csv .= implode( ',', $array );
239 - }
240 - fclose( $fp );
241 -
242 - return array(
243 - 'csv' => $csv,
244 - 'name' => $filename,
245 - );
246 - }
247 -
248 - /**
249 - * Prepares an Excel file.
250 - *
251 - * @access private
252 - * @param array $rows The array of data.
253 - * @param string $filename The name of the file to use.
254 - */
255 - private function _getExcel( $rows, $filename ) {
256 - // PHPExcel does not like sheet names longer than 31 characters.
257 - $chart = substr( $filename, 0, 30 );
258 - $filename .= '.xlsx';
259 -
260 - $xlsData = '';
261 - if ( class_exists( 'PHPExcel' ) ) {
262 - $doc = new PHPExcel();
263 - $doc->getActiveSheet()->fromArray( $rows, null, 'A1' );
264 - $doc->getActiveSheet()->setTitle( sanitize_title( $chart ) );
265 - $doc = apply_filters( 'visualizer_excel_doc', $doc );
266 - $writer = PHPExcel_IOFactory::createWriter( $doc, 'Excel2007' );
267 - ob_start();
268 - $writer->save( 'php://output' );
269 - $xlsData = ob_get_contents();
270 - ob_end_clean();
271 - } else {
272 - do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, 'Class PHPExcel does not exist!', 'error', __FILE__, __LINE__ );
273 - error_log( 'Class PHPExcel does not exist!' );
274 - }
275 - return array(
276 - 'csv' => 'data:application/vnd.ms-excel;base64,' . base64_encode( $xlsData ),
277 - 'name' => $filename,
278 - );
279 - }
280 -
281 - /**
282 - * Prepares an HTML table.
283 - *
284 - * @access private
285 - * @param array $rows The array of data.
286 - */
287 - private function _getHTML( $rows ) {
288 - $css = '
289 - table.visualizer-print {
290 - border-collapse: collapse;
291 - }
292 - table.visualizer-print, table.visualizer-print th, table.visualizer-print td {
293 - border: 1px solid #000;
294 - }
295 - ';
296 - $html = '';
297 - $html .= '
298 - <html>
299 - <head>
300 - <style>
301 - ' . apply_filters( 'visualizer_print_css', $css ) . '
302 - </style>
303 - </head>
304 - <body>';
305 -
306 - $table = '<table class="visualizer-print">';
307 - $index = 0;
308 - foreach ( $rows as $row ) {
309 - $table .= '<tr>';
310 - foreach ( $row as $col ) {
311 - if ( $index === 0 ) {
312 - $table .= '<th>' . $col . '</th>';
313 - } else {
314 - $table .= '<td>' . $col . '</td>';
315 - }
316 - }
317 - $table .= '</tr>';
318 - $index++;
319 - }
320 - $table .= '</table>';
321 -
322 - $html .= apply_filters( 'visualizer_print_table', $table ) . '
323 - </body>
324 - </html>';
325 - return array(
326 - 'csv' => $html,
327 - );
328 - }
329 -
330 - /**
331 - * Undo revisions for the chart, and if necessary, restore the earliest version.
332 - *
333 - * @return bool If any revisions were found.
334 - */
335 - public final function undoRevisions( $chart_id, $restore = false ) {
336 - do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'undoRevisions for %d with%s restore', $chart_id, ( $restore ? '' : 'out' ) ), 'debug', __FILE__, __LINE__ );
337 -
338 - $revisions = wp_get_post_revisions( $chart_id, array( 'order' => 'ASC' ) );
339 - if ( count( $revisions ) > 1 ) {
340 - $revision_ids = array_keys( $revisions );
341 -
342 - do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'found %d revisions = %s', count( $revisions ), print_r( $revision_ids, true ) ), 'debug', __FILE__, __LINE__ );
343 -
344 - // when we restore, a new revision is likely to be created. so, let's disable revisions for the time being.
345 - add_filter( 'wp_revisions_to_keep', '__return_false' );
346 -
347 - if ( $restore ) {
348 - // restore to the oldest one i.e. the first one.
349 - wp_restore_post_revision( array_shift( $revision_ids ) );
350 - }
351 -
352 - // delete all revisions.
353 - foreach ( $revision_ids as $id ) {
354 - wp_delete_post_revision( $id );
355 - }
356 -
357 - return true;
358 - }
359 - return false;
360 - }
361 -
362 - /**
363 - * If existing revisions exist for the chart, restore the earliest version and then create a new revision to initiate editing.
364 - */
365 - public final function handleExistingRevisions( $chart_id, $chart ) {
366 - do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'handleExistingRevisions for %d', $chart_id ), 'debug', __FILE__, __LINE__ );
367 -
368 - // undo revisions.
369 - $revisions_found = $this->undoRevisions( $chart_id, true );
370 -
371 - // create revision for the edit action.
372 - wp_save_post_revision( $chart_id );
373 -
374 - if ( $revisions_found ) {
375 - // fetch chart data again in case it was updated by an earlier revision.
376 - $chart = get_post( $chart_id );
377 - }
378 - return $chart;
379 - }
380 -
381 - /**
382 - * Returns the language of the locale.
383 - *
384 - * @access protected
385 - */
386 - protected function get_language() {
387 - $locale = get_locale();
388 - if ( empty( $locale ) ) {
389 - return '';
390 - }
391 - $array = explode( '_', $locale );
392 - if ( count( $array ) < 2 ) {
393 - return '';
394 - }
395 - return reset( $array );
396 144 }
397 145
398 146 }