PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 2.1.2
Visualizer – Tables & Charts Manager with Built-in AI Generator v2.1.2
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 / Module / Chart.php

Chart.php in Visualizer – Tables & Charts Manager with Built-in AI Generator 2.1.2, at classes/Visualizer/Module/Chart.php

596 lines 20.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // +----------------------------------------------------------------------+
3 // | Copyright 2013 Madpixels (email : visualizer@madpixels.net) |
4 // +----------------------------------------------------------------------+
5 // | This program is free software; you can redistribute it and/or modify |
6 // | it under the terms of the GNU General Public License, version 2, as |
7 // | published by the Free Software Foundation. |
8 // | |
9 // | This program is distributed in the hope that it will be useful, |
10 // | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 // | GNU General Public License for more details. |
13 // | |
14 // | You should have received a copy of the GNU General Public License |
15 // | along with this program; if not, write to the Free Software |
16 // | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, |
17 // | MA 02110-1301 USA |
18 // +----------------------------------------------------------------------+
19 // | Author: Eugene Manuilov <eugene@manuilov.org> |
20 // +----------------------------------------------------------------------+
21 /**
22 * The module for all stuff related to getting, editing, creating and deleting charts.
23 *
24 * @category Visualizer
25 * @package Module
26 *
27 * @since 1.0.0
28 */
29 class Visualizer_Module_Chart extends Visualizer_Module {
30
31 const NAME = __CLASS__;
32
33 /**
34 * The chart object.
35 *
36 * @since 1.0.0
37 *
38 * @access private
39 * @var WP_Post
40 */
41 private $_chart;
42
43 /**
44 * Constructor.
45 *
46 * @since 1.0.0
47 *
48 * @access public
49 *
50 * @param Visualizer_Plugin $plugin The instance of the plugin.
51 */
52 public function __construct( Visualizer_Plugin $plugin ) {
53 parent::__construct( $plugin );
54 $this->_addAjaxAction( Visualizer_Plugin::ACTION_GET_CHARTS, 'getCharts' );
55 $this->_addAjaxAction( Visualizer_Plugin::ACTION_DELETE_CHART, 'deleteChart' );
56 $this->_addAjaxAction( Visualizer_Plugin::ACTION_CREATE_CHART, 'renderChartPages' );
57 $this->_addAjaxAction( Visualizer_Plugin::ACTION_EDIT_CHART, 'renderChartPages' );
58 $this->_addAjaxAction( Visualizer_Plugin::ACTION_UPLOAD_DATA, 'uploadData' );
59 $this->_addAjaxAction( Visualizer_Plugin::ACTION_CLONE_CHART, 'cloneChart' );
60 // Added by Ash/Upwork
61 $this->_addAjaxAction( Visualizer_Plugin::ACTION_EXPORT_DATA, 'exportData' );
62 // Added by Ash/Upwork
63 }
64
65 /**
66 * Fetches charts from database.
67 *
68 * @since 1.0.0
69 *
70 * @access public
71 */
72 public function getCharts() {
73 $query_args = array(
74 'post_type' => Visualizer_Plugin::CPT_VISUALIZER,
75 'posts_per_page' => 9,
76 'paged' => filter_input( INPUT_GET, 'page', FILTER_VALIDATE_INT, array(
77 'options' => array(
78 'min_range' => 1,
79 'default' => 1,
80 ),
81 ) ),
82 );
83 $filter = filter_input( INPUT_GET, 's', FILTER_SANITIZE_STRING );
84 if ( $filter && in_array( $filter, Visualizer_Plugin::getChartTypes() ) ) {
85 $query_args['meta_query'] = array(
86 array(
87 'key' => Visualizer_Plugin::CF_CHART_TYPE,
88 'value' => $filter,
89 'compare' => '=',
90 ),
91 );
92 }
93 $query = new WP_Query( $query_args );
94 $charts = array();
95 while ( $query->have_posts() ) {
96 $chart = $query->next_post();
97 $chart_data = $this->_getChartArray( $chart );
98 $chart_data['id'] = $chart->ID;
99 $charts[] = $chart_data;
100 }
101 self::_sendResponse( array(
102 'success' => true,
103 'data' => $charts,
104 'total' => $query->max_num_pages,
105 ) );
106 }
107
108 /**
109 * Returns chart data required for rendering.
110 *
111 * @since 1.0.0
112 *
113 * @access private
114 *
115 * @param WP_Post $chart The chart object.
116 *
117 * @return array The array of chart data.
118 */
119 private function _getChartArray( WP_Post $chart = null ) {
120 if ( is_null( $chart ) ) {
121 $chart = $this->_chart;
122 }
123 $type = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true );
124 $series = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SERIES, get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true ), $chart->ID, $type );
125 $data = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA, unserialize( $chart->post_content ), $chart->ID, $type );
126
127 return array(
128 'type' => $type,
129 'series' => $series,
130 'settings' => get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true ),
131 'data' => $data,
132 );
133 }
134
135 /**
136 * Sends json response.
137 *
138 * @since 1.0.0
139 *
140 * @access private
141 *
142 * @param array $results The response array.
143 */
144 public static function _sendResponse( $results ) {
145 header( 'Content-type: application/json' );
146 nocache_headers();
147 echo json_encode( $results );
148 defined( 'WP_TESTS_DOMAIN' ) ? wp_die() : exit();
149 }
150
151 /**
152 * Deletes a chart from database.
153 *
154 * @since 1.0.0
155 * @uses wp_delete_post() To delete a chart.
156 *
157 * @access public
158 */
159 public function deleteChart() {
160 $is_post = $_SERVER['REQUEST_METHOD'] == 'POST';
161 $input_method = $is_post ? INPUT_POST : INPUT_GET;
162 $chart_id = $success = false;
163 $nonce = wp_verify_nonce( filter_input( $input_method, 'nonce' ) );
164 $capable = current_user_can( 'delete_posts' );
165 if ( $nonce && $capable ) {
166 $chart_id = filter_input( $input_method, 'chart', FILTER_VALIDATE_INT, array(
167 'options' => array(
168 'min_range' => 1,
169 ),
170 ) );
171 if ( $chart_id ) {
172 $chart = get_post( $chart_id );
173 $success = $chart && $chart->post_type == Visualizer_Plugin::CPT_VISUALIZER;
174 }
175 }
176 if ( $success ) {
177 wp_delete_post( $chart_id, true );
178 }
179 if ( $is_post ) {
180 self::_sendResponse( array(
181 'success' => $success,
182 ) );
183 }
184 wp_redirect( wp_get_referer() );
185 exit;
186 }
187
188 /**
189 * Renders appropriate page for chart builder. Creates new auto draft chart
190 * if no chart has been specified.
191 *
192 * @since 1.0.0
193 *
194 * @access public
195 */
196 public function renderChartPages() {
197 defined( 'IFRAME_REQUEST' ) || define( 'IFRAME_REQUEST', 1 );
198 // check chart, if chart not exists, will create new one and redirects to the same page with proper chart id
199 $chart_id = isset( $_GET['chart'] ) ? filter_var( $_GET['chart'], FILTER_VALIDATE_INT ) : '';
200 if ( ! $chart_id || ! ( $chart = get_post( $chart_id ) ) || $chart->post_type != Visualizer_Plugin::CPT_VISUALIZER ) {
201 $default_type = 'line';
202 $source = new Visualizer_Source_Csv( VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples' . DIRECTORY_SEPARATOR . $default_type . '.csv' );
203 $source->fetch();
204 $chart_id = wp_insert_post( array(
205 'post_type' => Visualizer_Plugin::CPT_VISUALIZER,
206 'post_title' => 'Visualization',
207 'post_author' => get_current_user_id(),
208 'post_status' => 'auto-draft',
209 'post_content' => $source->getData(),
210 ) );
211 if ( $chart_id && ! is_wp_error( $chart_id ) ) {
212 add_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_TYPE, $default_type );
213 add_post_meta( $chart_id, Visualizer_Plugin::CF_DEFAULT_DATA, 1 );
214 add_post_meta( $chart_id, Visualizer_Plugin::CF_SOURCE, $source->getSourceName() );
215 add_post_meta( $chart_id, Visualizer_Plugin::CF_SERIES, $source->getSeries() );
216 add_post_meta( $chart_id, Visualizer_Plugin::CF_SETTINGS, array(
217 'focusTarget' => 'datum',
218 ) );
219 }
220 wp_redirect( add_query_arg( 'chart', (int) $chart_id ) );
221 defined( 'WP_TESTS_DOMAIN' ) ? wp_die() : exit();
222 }
223 // enqueue and register scripts and styles
224 wp_register_style( 'visualizer-frame', VISUALIZER_ABSURL . 'css/frame.css', array(), Visualizer_Plugin::VERSION );
225 wp_register_script( 'visualizer-frame', VISUALIZER_ABSURL . 'js/frame.js', array( 'jquery' ), Visualizer_Plugin::VERSION, true );
226 wp_register_script( 'google-jsapi-new', '//www.gstatic.com/charts/loader.js', array(), null, true );
227 wp_register_script( 'google-jsapi-old', '//www.google.com/jsapi', array( 'google-jsapi-new' ), null, true );
228 wp_register_script( 'visualizer-render', VISUALIZER_ABSURL . 'js/render.js', array(
229 'google-jsapi-old',
230 'google-jsapi-new',
231 'visualizer-frame',
232 ), Visualizer_Plugin::VERSION, true );
233 wp_register_script( 'visualizer-preview', VISUALIZER_ABSURL . 'js/preview.js', array(
234 'wp-color-picker',
235 'visualizer-render',
236 ), Visualizer_Plugin::VERSION, true );
237 // added by Ash/Upwork
238 if ( VISUALIZER_PRO ) {
239 global $Visualizer_Pro;
240 $Visualizer_Pro->_addScriptsAndStyles();
241 }
242 // dispatch pages
243 $this->_chart = get_post( $chart_id );
244 switch ( isset( $_GET['tab'] ) ? $_GET['tab'] : '' ) {
245 case 'settings':
246 // changed by Ash/Upwork
247 $this->_handleDataAndSettingsPage();
248 break;
249 case 'type':
250 default:
251 $this->_handleTypesPage();
252 break;
253 }
254 defined( 'WP_TESTS_DOMAIN' ) ? wp_die() : exit();
255 }
256
257 /**
258 * Handle data and settings page
259 */
260 private function _handleDataAndSettingsPage() {
261 if ( isset( $_POST['map_api_key'] ) ) {
262 update_option( 'visualizer-map-api-key', $_POST['map_api_key'] );
263 }
264 if ( $_SERVER['REQUEST_METHOD'] == 'POST' && isset( $_GET['nonce'] ) && wp_verify_nonce( $_GET['nonce'] ) ) {
265 if ( $this->_chart->post_status == 'auto-draft' ) {
266 $this->_chart->post_status = 'publish';
267 wp_update_post( $this->_chart->to_array() );
268 }
269 update_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_SETTINGS, $_POST );
270 $render = new Visualizer_Render_Page_Send();
271 $render->text = sprintf( '[visualizer id="%d"]', $this->_chart->ID );
272 wp_iframe( array( $render, 'render' ) );
273
274 return;
275 }
276 $data = $this->_getChartArray();
277 $sidebar = '';
278 $sidebar_class = 'Visualizer_Render_Sidebar_Type_' . ucfirst( $data['type'] );
279 if ( class_exists( $sidebar_class, true ) ) {
280 $sidebar = new $sidebar_class( $data['settings'] );
281 $sidebar->__series = $data['series'];
282 $sidebar->__data = $data['data'];
283 } else {
284 $sidebar = apply_filters( 'visualizer_pro_chart_type_sidebar', '', $data );
285 if ( $sidebar != '' ) {
286 $sidebar->__series = $data['series'];
287 $sidebar->__data = $data['data'];
288 }
289 }
290 unset( $data['settings']['width'], $data['settings']['height'] );
291 wp_enqueue_style( 'visualizer-frame' );
292 wp_enqueue_style( 'wp-color-picker' );
293 wp_enqueue_style( 'visualizer-frame' );
294 wp_enqueue_script( 'visualizer-preview' );
295 wp_enqueue_script( 'visualizer-render' );
296 wp_localize_script( 'visualizer-render', 'visualizer', array(
297 'l10n' => array(
298 'invalid_source' => esc_html__( 'You have entered invalid URL. Please, insert proper URL.', 'visualizer' ),
299 ),
300 'charts' => array(
301 'canvas' => $data,
302 ),
303 'map_api_key' => get_option( 'visualizer-map-api-key' ),
304 ) );
305 $render = new Visualizer_Render_Page_Data();
306 $render->chart = $this->_chart;
307 $render->type = $data['type'];
308 $render->sidebar = $sidebar;
309 if ( filter_input( INPUT_GET, 'library', FILTER_VALIDATE_BOOLEAN ) ) {
310 $render->button = filter_input( INPUT_GET, 'action' ) == Visualizer_Plugin::ACTION_EDIT_CHART
311 ? esc_html__( 'Save Chart', 'visualizer' )
312 : esc_html__( 'Create Chart', 'visualizer' );
313 } else {
314 $render->button = esc_attr__( 'Insert Chart', 'visualizer' );
315 }
316 if ( VISUALIZER_PRO ) {
317 global $Visualizer_Pro;
318 $Visualizer_Pro->_enqueueScriptsAndStyles( $data );
319 }
320 $this->_addAction( 'admin_head', 'renderFlattrScript' );
321 wp_iframe( array( $render, 'render' ) );
322 }
323
324 /**
325 * Handles chart type selection page.
326 *
327 * @since 1.0.0
328 *
329 * @access private
330 */
331 private function _handleTypesPage() {
332 // process post request
333 if ( $_SERVER['REQUEST_METHOD'] == 'POST' && wp_verify_nonce( filter_input( INPUT_POST, 'nonce' ) ) ) {
334 $type = filter_input( INPUT_POST, 'type' );
335 if ( in_array( $type, Visualizer_Plugin::getChartTypes() ) ) {
336 // save new chart type
337 update_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_CHART_TYPE, $type );
338 // if the chart has default data, update it with appropriate default data for new type
339 if ( filter_var( get_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_DEFAULT_DATA, true ), FILTER_VALIDATE_BOOLEAN ) ) {
340 $source = new Visualizer_Source_Csv( VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples' . DIRECTORY_SEPARATOR . $type . '.csv' );
341 $source->fetch();
342 $this->_chart->post_content = $source->getData();
343 wp_update_post( $this->_chart->to_array() );
344 update_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_SERIES, $source->getSeries() );
345 }
346 // redirect to next tab
347 // changed by Ash/Upwork
348 wp_redirect( add_query_arg( 'tab', 'settings' ) );
349
350 return;
351 }
352 }
353 $render = new Visualizer_Render_Page_Types();
354 $render->type = get_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true );
355 $render->types = Visualizer_Module_Admin::_getChartTypesLocalized();
356 $render->chart = $this->_chart;
357 wp_enqueue_style( 'visualizer-frame' );
358 wp_enqueue_script( 'visualizer-frame' );
359 wp_iframe( array( $render, 'render' ) );
360 }
361
362 /**
363 * Renders flattr script in the iframe <head>
364 *
365 * @since 1.4.2
366 * @action admin_head
367 *
368 * @access public
369 */
370 public function renderFlattrScript() {
371 echo '';
372 }
373 // changed by Ash/Upwork
374 /**
375 * Parses uploaded CSV file and saves new data for the chart.
376 *
377 * @since 1.0.0
378 *
379 * @access public
380 */
381 public function uploadData() {
382 error_log( 'in uploadData ' . print_r( $_GET,true ) . print_r( $_POST,true ) );
383 // validate nonce
384 // do not use filter_input as it does not work for phpunit test cases, use filter_var instead
385 if ( ! isset( $_GET['nonce'] ) || ! wp_verify_nonce( $_GET['nonce'] ) ) {
386 status_header( 403 );
387 exit;
388 }
389 // check chart, if chart exists
390 $chart_id = isset( $_GET['chart'] ) ? filter_var( $_GET['chart'], FILTER_VALIDATE_INT ) : '';
391 if ( ! $chart_id || ! ( $chart = get_post( $chart_id ) ) || $chart->post_type != Visualizer_Plugin::CPT_VISUALIZER ) {
392 status_header( 400 );
393 exit;
394 }
395 if ( ! isset( $_POST['vz-import-time'] ) ) {
396 apply_filters( 'visualizer_pro_remove_schedule', $chart_id );
397 }
398 $source = null;
399 $render = new Visualizer_Render_Page_Update();
400 if ( isset( $_POST['remote_data'] ) && filter_var( $_POST['remote_data'], FILTER_VALIDATE_URL ) ) {
401 $source = new Visualizer_Source_Csv_Remote( $_POST['remote_data'] );
402 if ( isset( $_POST['vz-import-time'] ) ) {
403 apply_filters( 'visualizer_pro_chart_schedule', $chart_id, $_POST['remote_data'], $_POST['vz-import-time'] );
404 }
405 } elseif ( isset( $_FILES['local_data'] ) && $_FILES['local_data']['error'] == 0 ) {
406 $source = new Visualizer_Source_Csv( $_FILES['local_data']['tmp_name'] );
407 // Added by Ash/Upwork
408 } elseif ( isset( $_POST['chart_data'] ) && strlen( $_POST['chart_data'] ) > 0 ) {
409 $source = apply_filters( 'visualizer_pro_handle_chart_data', $_POST['chart_data'], '' );
410 // Added by Ash/Upwork
411 } else {
412 $render->message = esc_html__( 'CSV file with chart data was not uploaded. Please, try again.', 'visualizer' );
413 }
414 if ( $source ) {
415 if ( $source->fetch() ) {
416 $chart->post_content = $source->getData();
417 wp_update_post( $chart->to_array() );
418 update_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, $source->getSeries() );
419 update_post_meta( $chart->ID, Visualizer_Plugin::CF_SOURCE, $source->getSourceName() );
420 update_post_meta( $chart->ID, Visualizer_Plugin::CF_DEFAULT_DATA, 0 );
421 $render->data = json_encode( $source->getRawData() );
422 $render->series = json_encode( $source->getSeries() );
423 } else {
424 $render->message = esc_html__( 'CSV file is broken or invalid. Please, try again.', 'visualizer' );
425 }
426 }
427 $render->render();
428 if ( ! ( defined( 'VISUALIZER_DO_NOT_DIE' ) && VISUALIZER_DO_NOT_DIE ) ) {
429 defined( 'WP_TESTS_DOMAIN' ) ? wp_die() : exit();
430 }
431 }
432
433 /**
434 * Clones the chart.
435 *
436 * @since 1.0.0
437 *
438 * @access public
439 */
440 public function cloneChart() {
441 $chart_id = $success = false;
442 $nonce = wp_verify_nonce( filter_input( INPUT_GET, 'nonce' ), Visualizer_Plugin::ACTION_CLONE_CHART );
443 $capable = current_user_can( 'edit_posts' );
444 if ( $nonce && $capable ) {
445 $chart_id = filter_input( INPUT_GET, 'chart', FILTER_VALIDATE_INT, array(
446 'options' => array(
447 'min_range' => 1,
448 ),
449 ) );
450 if ( $chart_id ) {
451 $chart = get_post( $chart_id );
452 $success = $chart && $chart->post_type == Visualizer_Plugin::CPT_VISUALIZER;
453 }
454 }
455 $redirect = wp_get_referer();
456 if ( $success ) {
457 $new_chart_id = wp_insert_post( array(
458 'post_type' => Visualizer_Plugin::CPT_VISUALIZER,
459 'post_title' => 'Visualization',
460 'post_author' => get_current_user_id(),
461 'post_status' => $chart->post_status,
462 'post_content' => $chart->post_content,
463 ) );
464 if ( $new_chart_id && ! is_wp_error( $new_chart_id ) ) {
465 add_post_meta( $new_chart_id, Visualizer_Plugin::CF_CHART_TYPE, get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_TYPE, true ) );
466 add_post_meta( $new_chart_id, Visualizer_Plugin::CF_DEFAULT_DATA, get_post_meta( $chart_id, Visualizer_Plugin::CF_DEFAULT_DATA, true ) );
467 add_post_meta( $new_chart_id, Visualizer_Plugin::CF_SOURCE, get_post_meta( $chart_id, Visualizer_Plugin::CF_SOURCE, true ) );
468 add_post_meta( $new_chart_id, Visualizer_Plugin::CF_SERIES, get_post_meta( $chart_id, Visualizer_Plugin::CF_SERIES, true ) );
469 add_post_meta( $new_chart_id, Visualizer_Plugin::CF_SETTINGS, get_post_meta( $chart_id, Visualizer_Plugin::CF_SETTINGS, true ) );
470 $redirect = add_query_arg( array(
471 'page' => 'visualizer',
472 'type' => filter_input( INPUT_GET, 'type' ),
473 ), admin_url( 'upload.php' ) );
474 }
475 }
476 wp_redirect( $redirect );
477 exit;
478 }
479
480 /**
481 * Exports the chart data
482 *
483 * @since 1.0.0
484 *
485 * @access public
486 */
487 public function exportData() {
488 check_ajax_referer( Visualizer_Plugin::ACTION_EXPORT_DATA . Visualizer_Plugin::VERSION, 'security' );
489 $chart_id = $success = false;
490 $capable = current_user_can( 'edit_posts' );
491 if ( $capable ) {
492 $chart_id = isset( $_GET['chart'] ) ? filter_var( $_GET['chart'], FILTER_VALIDATE_INT, array(
493 'options' => array(
494 'min_range' => 1,
495 ),
496 ) ) : '';
497 if ( $chart_id ) {
498 $chart = get_post( $chart_id );
499 $success = $chart && $chart->post_type == Visualizer_Plugin::CPT_VISUALIZER;
500 }
501 }
502 if ( $success ) {
503 $settings = get_post_meta( $chart_id, Visualizer_Plugin::CF_SETTINGS, true );
504 $filename = isset( $settings['title'] ) ? $settings['title'] : '';
505 if ( empty( $filename ) ) {
506 $filename = 'export.csv';
507 } else {
508 $filename .= '.csv';
509 }
510 $rows = array();
511 $series = get_post_meta( $chart_id, Visualizer_Plugin::CF_SERIES, true );
512 $data = unserialize( $chart->post_content );
513 if ( ! empty( $series ) ) {
514 $row = array();
515 foreach ( $series as $array ) {
516 $row[] = $array['label'];
517 }
518 $rows[] = $row;
519 $row = array();
520 foreach ( $series as $array ) {
521 $row[] = $array['type'];
522 }
523 $rows[] = $row;
524 }
525 if ( ! empty( $data ) ) {
526 foreach ( $data as $array ) {
527 // ignore strings
528 if ( ! is_array( $array ) ) {
529 continue;
530 }
531 // if this is an array of arrays...
532 if ( is_array( $array[0] ) ) {
533 foreach ( $array as $arr ) {
534 $rows[] = $arr;
535 }
536 } else {
537 // just an array
538 $rows[] = $array;
539 }
540 }
541 }
542 $fp = tmpfile();
543 foreach ( $rows as $row ) {
544 fputcsv( $fp, $row );
545 }
546 rewind( $fp );
547 $csv = '';
548 while ( ( $array = fgetcsv( $fp ) ) !== false ) {
549 if ( strlen( $csv ) > 0 ) {
550 $csv .= PHP_EOL;
551 }
552 $csv .= implode( ',', $array );
553 }
554 fclose( $fp );
555 echo wp_send_json_success( array(
556 'csv' => $csv,
557 'name' => $filename,
558 ) );
559 }// End if().
560 defined( 'WP_TESTS_DOMAIN' ) ? wp_die() : exit();
561 }
562
563 /**
564 * Handles chart data page.
565 *
566 * @since 1.0.0
567 *
568 * @access private
569 */
570 private function _handleDataPage() {
571 $data = $this->_getChartArray();
572 $render = new Visualizer_Render_Page_Data();
573 $render->chart = $this->_chart;
574 $render->type = $data['type'];
575 unset( $data['settings']['width'], $data['settings']['height'] );
576 wp_enqueue_style( 'visualizer-frame' );
577 wp_enqueue_script( 'visualizer-render' );
578 wp_localize_script( 'visualizer-render', 'visualizer', array(
579 'l10n' => array(
580 'invalid_source' => esc_html__( 'You have entered invalid URL. Please, insert proper URL.', 'visualizer' ),
581 ),
582 'charts' => array(
583 'canvas' => $data,
584 ),
585 ) );
586 // Added by Ash/Upwork
587 if ( VISUALIZER_PRO ) {
588 global $Visualizer_Pro;
589 $Visualizer_Pro->_enqueueScriptsAndStyles( $data );
590 }
591 // Added by Ash/Upwork
592 $this->_addAction( 'admin_head', 'renderFlattrScript' );
593 wp_iframe( array( $render, 'render' ) );
594 }
595 }
596