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
visualizer / classes / Visualizer / Module / Chart.php

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

597 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 // validate nonce
383 // do not use filter_input as it does not work for phpunit test cases, use filter_var instead
384 if ( ! isset( $_GET['nonce'] ) || ! wp_verify_nonce( $_GET['nonce'] ) ) {
385 status_header( 403 );
386 exit;
387 }
388 // check chart, if chart exists
389 $chart_id = isset( $_GET['chart'] ) ? filter_var( $_GET['chart'], FILTER_VALIDATE_INT ) : '';
390 if ( ! $chart_id || ! ( $chart = get_post( $chart_id ) ) || $chart->post_type != Visualizer_Plugin::CPT_VISUALIZER ) {
391 status_header( 400 );
392 exit;
393 }
394 if ( ! isset( $_POST['vz-import-time'] ) ) {
395 apply_filters( 'visualizer_pro_remove_schedule', $chart_id );
396 }
397 $source = null;
398 $render = new Visualizer_Render_Page_Update();
399 if ( isset( $_POST['remote_data'] ) && filter_var( $_POST['remote_data'], FILTER_VALIDATE_URL ) ) {
400 $source = new Visualizer_Source_Csv_Remote( $_POST['remote_data'] );
401 if ( isset( $_POST['vz-import-time'] ) ) {
402 apply_filters( 'visualizer_pro_chart_schedule', $chart_id, $_POST['remote_data'], $_POST['vz-import-time'] );
403 }
404 } elseif ( isset( $_FILES['local_data'] ) && $_FILES['local_data']['error'] == 0 ) {
405 $source = new Visualizer_Source_Csv( $_FILES['local_data']['tmp_name'] );
406 // Added by Ash/Upwork
407 } elseif ( isset( $_POST['chart_data'] ) && strlen( $_POST['chart_data'] ) > 0 ) {
408 $source = apply_filters( 'visualizer_pro_handle_chart_data', $_POST['chart_data'], '' );
409 // Added by Ash/Upwork
410 } else {
411 $render->message = esc_html__( 'CSV file with chart data was not uploaded. Please, try again.', 'visualizer' );
412 }
413 if ( $source ) {
414 if ( $source->fetch() ) {
415 $chart->post_content = $source->getData();
416 wp_update_post( $chart->to_array() );
417 update_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, $source->getSeries() );
418 update_post_meta( $chart->ID, Visualizer_Plugin::CF_SOURCE, $source->getSourceName() );
419 update_post_meta( $chart->ID, Visualizer_Plugin::CF_DEFAULT_DATA, 0 );
420 $render->data = json_encode( $source->getRawData() );
421 $render->series = json_encode( $source->getSeries() );
422 } else {
423 $render->message = esc_html__( 'CSV file is broken or invalid. Please, try again.', 'visualizer' );
424 }
425 }
426 $render->render();
427 if ( ! ( defined( 'VISUALIZER_DO_NOT_DIE' ) && VISUALIZER_DO_NOT_DIE ) ) {
428 defined( 'WP_TESTS_DOMAIN' ) ? wp_die() : exit();
429 }
430 }
431
432 /**
433 * Clones the chart.
434 *
435 * @since 1.0.0
436 *
437 * @access public
438 */
439 public function cloneChart() {
440 $chart_id = $success = false;
441 $nonce = wp_verify_nonce( filter_input( INPUT_GET, 'nonce' ), Visualizer_Plugin::ACTION_CLONE_CHART );
442 $capable = current_user_can( 'edit_posts' );
443 if ( $nonce && $capable ) {
444 $chart_id = filter_input( INPUT_GET, 'chart', FILTER_VALIDATE_INT, array(
445 'options' => array(
446 'min_range' => 1,
447 ),
448 ) );
449 if ( $chart_id ) {
450 $chart = get_post( $chart_id );
451 $success = $chart && $chart->post_type == Visualizer_Plugin::CPT_VISUALIZER;
452 }
453 }
454 $redirect = wp_get_referer();
455 if ( $success ) {
456 $new_chart_id = wp_insert_post( array(
457 'post_type' => Visualizer_Plugin::CPT_VISUALIZER,
458 'post_title' => 'Visualization',
459 'post_author' => get_current_user_id(),
460 'post_status' => $chart->post_status,
461 'post_content' => $chart->post_content,
462 ) );
463 if ( $new_chart_id && ! is_wp_error( $new_chart_id ) ) {
464 add_post_meta( $new_chart_id, Visualizer_Plugin::CF_CHART_TYPE, get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_TYPE, true ) );
465 add_post_meta( $new_chart_id, Visualizer_Plugin::CF_DEFAULT_DATA, get_post_meta( $chart_id, Visualizer_Plugin::CF_DEFAULT_DATA, true ) );
466 add_post_meta( $new_chart_id, Visualizer_Plugin::CF_SOURCE, get_post_meta( $chart_id, Visualizer_Plugin::CF_SOURCE, true ) );
467 add_post_meta( $new_chart_id, Visualizer_Plugin::CF_SERIES, get_post_meta( $chart_id, Visualizer_Plugin::CF_SERIES, true ) );
468 add_post_meta( $new_chart_id, Visualizer_Plugin::CF_SETTINGS, get_post_meta( $chart_id, Visualizer_Plugin::CF_SETTINGS, true ) );
469 $redirect = add_query_arg( array(
470 'page' => 'visualizer',
471 'type' => filter_input( INPUT_GET, 'type' ),
472 ), admin_url( 'upload.php' ) );
473 }
474 }
475 wp_redirect( $redirect );
476 exit;
477 }
478
479 /**
480 * Exports the chart data
481 *
482 * @since 1.0.0
483 *
484 * @access public
485 */
486 public function exportData() {
487 check_ajax_referer( Visualizer_Plugin::ACTION_EXPORT_DATA . Visualizer_Plugin::VERSION, 'security' );
488 $chart_id = $success = false;
489 $capable = current_user_can( 'edit_posts' );
490 if ( $capable ) {
491 $chart_id = isset( $_GET['chart'] ) ? filter_var( $_GET['chart'], FILTER_VALIDATE_INT, array(
492 'options' => array(
493 'min_range' => 1,
494 ),
495 ) ) : '';
496 if ( $chart_id ) {
497 $chart = get_post( $chart_id );
498 $success = $chart && $chart->post_type == Visualizer_Plugin::CPT_VISUALIZER;
499 }
500 }
501 if ( $success ) {
502 $settings = get_post_meta( $chart_id, Visualizer_Plugin::CF_SETTINGS, true );
503 $filename = isset( $settings['title'] ) ? $settings['title'] : '';
504 if ( empty( $filename ) ) {
505 $filename = 'export.csv';
506 } else {
507 $filename .= '.csv';
508 }
509 $rows = array();
510 $series = get_post_meta( $chart_id, Visualizer_Plugin::CF_SERIES, true );
511 $data = unserialize( $chart->post_content );
512 if ( ! empty( $series ) ) {
513 $row = array();
514 foreach ( $series as $array ) {
515 $row[] = $array['label'];
516 }
517 $rows[] = $row;
518 $row = array();
519 foreach ( $series as $array ) {
520 $row[] = $array['type'];
521 }
522 $rows[] = $row;
523 }
524 if ( ! empty( $data ) ) {
525 foreach ( $data as $array ) {
526 // ignore strings
527 if ( ! is_array( $array ) ) {
528 continue;
529 }
530 // if this is an array of arrays...
531 if ( is_array( $array[0] ) ) {
532 foreach ( $array as $arr ) {
533 $rows[] = $arr;
534 }
535 } else {
536 // just an array
537 $rows[] = $array;
538 }
539 }
540 }
541 $fp = tmpfile();
542 // support for MS Excel
543 fprintf( $fp, $bom = ( chr( 0xEF ) . chr( 0xBB ) . chr( 0xBF ) ) );
544 foreach ( $rows as $row ) {
545 fputcsv( $fp, $row );
546 }
547 rewind( $fp );
548 $csv = '';
549 while ( ( $array = fgetcsv( $fp ) ) !== false ) {
550 if ( strlen( $csv ) > 0 ) {
551 $csv .= PHP_EOL;
552 }
553 $csv .= implode( ',', $array );
554 }
555 fclose( $fp );
556 echo wp_send_json_success( array(
557 'csv' => $csv,
558 'name' => $filename,
559 ) );
560 }// End if().
561 defined( 'WP_TESTS_DOMAIN' ) ? wp_die() : exit();
562 }
563
564 /**
565 * Handles chart data page.
566 *
567 * @since 1.0.0
568 *
569 * @access private
570 */
571 private function _handleDataPage() {
572 $data = $this->_getChartArray();
573 $render = new Visualizer_Render_Page_Data();
574 $render->chart = $this->_chart;
575 $render->type = $data['type'];
576 unset( $data['settings']['width'], $data['settings']['height'] );
577 wp_enqueue_style( 'visualizer-frame' );
578 wp_enqueue_script( 'visualizer-render' );
579 wp_localize_script( 'visualizer-render', 'visualizer', array(
580 'l10n' => array(
581 'invalid_source' => esc_html__( 'You have entered invalid URL. Please, insert proper URL.', 'visualizer' ),
582 ),
583 'charts' => array(
584 'canvas' => $data,
585 ),
586 ) );
587 // Added by Ash/Upwork
588 if ( VISUALIZER_PRO ) {
589 global $Visualizer_Pro;
590 $Visualizer_Pro->_enqueueScriptsAndStyles( $data );
591 }
592 // Added by Ash/Upwork
593 $this->_addAction( 'admin_head', 'renderFlattrScript' );
594 wp_iframe( array( $render, 'render' ) );
595 }
596 }
597