| @@ -56,11 +56,9 @@ | ||
| 56 | 56 | $this->_addAjaxAction( Visualizer_Plugin::ACTION_CREATE_CHART, 'renderChartPages' ); |
| 57 | 57 | $this->_addAjaxAction( Visualizer_Plugin::ACTION_EDIT_CHART, 'renderChartPages' ); |
| 58 | 58 | $this->_addAjaxAction( Visualizer_Plugin::ACTION_UPLOAD_DATA, 'uploadData' ); |
| 59 | 59 | $this->_addAjaxAction( Visualizer_Plugin::ACTION_CLONE_CHART, 'cloneChart' ); |
| 60 | - // Added by Ash/Upwork | |
| 61 | 60 | $this->_addAjaxAction( Visualizer_Plugin::ACTION_EXPORT_DATA, 'exportData' ); |
| 62 | - // Added by Ash/Upwork | |
| 63 | 61 | } |
| 64 | 62 | |
| 65 | 63 | /** |
| 66 | 64 | * Fetches charts from database. |
| @@ -210,9 +208,9 @@ | ||
| 210 | 208 | defined( 'IFRAME_REQUEST' ) || define( 'IFRAME_REQUEST', 1 ); |
| 211 | 209 | // check chart, if chart not exists, will create new one and redirects to the same page with proper chart id |
| 212 | 210 | $chart_id = isset( $_GET['chart'] ) ? filter_var( $_GET['chart'], FILTER_VALIDATE_INT ) : ''; |
| 213 | 211 | if ( ! $chart_id || ! ( $chart = get_post( $chart_id ) ) || $chart->post_type != Visualizer_Plugin::CPT_VISUALIZER ) { |
| 214 | - $default_type = 'line'; | |
| 212 | + $default_type = isset( $_GET['type'] ) && ! empty( $_GET['type'] ) ? $_GET['type'] : 'line'; | |
| 215 | 213 | $source = new Visualizer_Source_Csv( VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples' . DIRECTORY_SEPARATOR . $default_type . '.csv' ); |
| 216 | 214 | $source->fetch(); |
| 217 | 215 | $chart_id = wp_insert_post( |
| 218 | 216 | array( |
| @@ -268,12 +266,23 @@ | ||
| 268 | 266 | $this->_chart = get_post( $chart_id ); |
| 269 | 267 | $tab = isset( $_GET['tab'] ) && ! empty( $_GET['tab'] ) ? $_GET['tab'] : 'visualizer'; |
| 270 | 268 | |
| 271 | 269 | // skip chart type pages only for existing charts. |
| 272 | - if ( VISUALIZER_SKIP_CHART_TYPE_PAGE && 'auto-draft' !== $this->_chart->post_status ) { | |
| 270 | + if ( VISUALIZER_SKIP_CHART_TYPE_PAGE && 'auto-draft' !== $this->_chart->post_status && ( ! empty( $_GET['tab'] ) && 'visualizer' === $_GET['tab'] ) ) { | |
| 273 | 271 | $tab = 'settings'; |
| 274 | 272 | } |
| 275 | 273 | |
| 274 | + if ( isset( $_POST['cancel'] ) && 1 === intval( $_POST['cancel'] ) ) { | |
| 275 | + // if the cancel button is clicked. | |
| 276 | + $this->undoRevisions( $chart_id, true ); | |
| 277 | + } elseif ( isset( $_POST['save'] ) && 1 === intval( $_POST['save'] ) ) { | |
| 278 | + // if the save button is clicked. | |
| 279 | + $this->undoRevisions( $chart_id, false ); | |
| 280 | + } else { | |
| 281 | + // if the edit button is clicked. | |
| 282 | + $this->_chart = $this->handleExistingRevisions( $chart_id, $this->_chart ); | |
| 283 | + } | |
| 284 | + | |
| 276 | 285 | switch ( $tab ) { |
| 277 | 286 | case 'settings': |
| 278 | 287 | $this->_handleDataAndSettingsPage(); |
| 279 | 288 | break; |
| @@ -297,11 +306,18 @@ | ||
| 297 | 306 | } |
| 298 | 307 | if ( $_SERVER['REQUEST_METHOD'] == 'POST' && isset( $_GET['nonce'] ) && wp_verify_nonce( $_GET['nonce'] ) ) { |
| 299 | 308 | if ( $this->_chart->post_status == 'auto-draft' ) { |
| 300 | 309 | $this->_chart->post_status = 'publish'; |
| 310 | + | |
| 311 | + // ensure that a revision is not created. If a revision is created it will have the proper data and the parent of the revision will have default data. | |
| 312 | + // we do not want any difference in data so disable revisions temporarily. | |
| 313 | + add_filter( 'wp_revisions_to_keep', '__return_false' ); | |
| 301 | 314 | wp_update_post( $this->_chart->to_array() ); |
| 302 | 315 | } |
| 303 | - update_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_SETTINGS, $_POST ); | |
| 316 | + // save meta data only when it is NOT being canceled. | |
| 317 | + if ( ! ( isset( $_POST['cancel'] ) && 1 === intval( $_POST['cancel'] ) ) ) { | |
| 318 | + update_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_SETTINGS, $_POST ); | |
| 319 | + } | |
| 304 | 320 | $render = new Visualizer_Render_Page_Send(); |
| 305 | 321 | $render->text = sprintf( '[visualizer id="%d"]', $this->_chart->ID ); |
| 306 | 322 | wp_iframe( array( $render, 'render' ) ); |
| 307 | 323 | |
| @@ -335,8 +351,9 @@ | ||
| 335 | 351 | ), |
| 336 | 352 | 'charts' => array( |
| 337 | 353 | 'canvas' => $data, |
| 338 | 354 | ), |
| 355 | + 'language' => $this->get_language(), | |
| 339 | 356 | 'map_api_key' => get_option( 'visualizer-map-api-key' ), |
| 340 | 357 | 'ajax' => array( |
| 341 | 358 | 'url' => admin_url( 'admin-ajax.php' ), |
| 342 | 359 | 'nonces' => array( |
| @@ -355,8 +372,11 @@ | ||
| 355 | 372 | if ( filter_input( INPUT_GET, 'library', FILTER_VALIDATE_BOOLEAN ) ) { |
| 356 | 373 | $render->button = filter_input( INPUT_GET, 'action' ) == Visualizer_Plugin::ACTION_EDIT_CHART |
| 357 | 374 | ? esc_html__( 'Save Chart', 'visualizer' ) |
| 358 | 375 | : esc_html__( 'Create Chart', 'visualizer' ); |
| 376 | + if ( filter_input( INPUT_GET, 'action' ) == Visualizer_Plugin::ACTION_EDIT_CHART ) { | |
| 377 | + $render->cancel_button = esc_html__( 'Cancel', 'visualizer' ); | |
| 378 | + } | |
| 359 | 379 | } else { |
| 360 | 380 | $render->button = esc_attr__( 'Insert Chart', 'visualizer' ); |
| 361 | 381 | } |
| 362 | 382 | if ( VISUALIZER_PRO ) { |
| @@ -424,20 +444,32 @@ | ||
| 424 | 444 | * |
| 425 | 445 | * @access public |
| 426 | 446 | */ |
| 427 | 447 | public function uploadData() { |
| 448 | + // if this is being called internally from pro and VISUALIZER_DO_NOT_DIE is set. | |
| 449 | + // otherwise, assume this is a normal web request. | |
| 450 | + $can_die = ! ( defined( 'VISUALIZER_DO_NOT_DIE' ) && VISUALIZER_DO_NOT_DIE ); | |
| 451 | + | |
| 428 | 452 | // validate nonce |
| 429 | - // do not use filter_input as it does not work for phpunit test cases, use filter_var instead | |
| 430 | 453 | if ( ! isset( $_GET['nonce'] ) || ! wp_verify_nonce( $_GET['nonce'] ) ) { |
| 454 | + if ( ! $can_die ) { | |
| 455 | + return; | |
| 456 | + } | |
| 431 | 457 | status_header( 403 ); |
| 432 | 458 | exit; |
| 433 | 459 | } |
| 460 | + | |
| 434 | 461 | // check chart, if chart exists |
| 462 | + // do not use filter_input as it does not work for phpunit test cases, use filter_var instead | |
| 435 | 463 | $chart_id = isset( $_GET['chart'] ) ? filter_var( $_GET['chart'], FILTER_VALIDATE_INT ) : ''; |
| 436 | 464 | if ( ! $chart_id || ! ( $chart = get_post( $chart_id ) ) || $chart->post_type != Visualizer_Plugin::CPT_VISUALIZER ) { |
| 465 | + if ( ! $can_die ) { | |
| 466 | + return; | |
| 467 | + } | |
| 437 | 468 | status_header( 400 ); |
| 438 | 469 | exit; |
| 439 | 470 | } |
| 471 | + | |
| 440 | 472 | if ( ! isset( $_POST['vz-import-time'] ) ) { |
| 441 | 473 | apply_filters( 'visualizer_pro_remove_schedule', $chart_id ); |
| 442 | 474 | } |
| 443 | 475 | |
| @@ -454,18 +486,29 @@ | ||
| 454 | 486 | apply_filters( 'visualizer_pro_chart_schedule', $chart_id, $_POST['remote_data'], $_POST['vz-import-time'] ); |
| 455 | 487 | } |
| 456 | 488 | } elseif ( isset( $_FILES['local_data'] ) && $_FILES['local_data']['error'] == 0 ) { |
| 457 | 489 | $source = new Visualizer_Source_Csv( $_FILES['local_data']['tmp_name'] ); |
| 458 | - // Added by Ash/Upwork | |
| 459 | 490 | } elseif ( isset( $_POST['chart_data'] ) && strlen( $_POST['chart_data'] ) > 0 ) { |
| 460 | 491 | $source = apply_filters( 'visualizer_pro_handle_chart_data', $_POST['chart_data'], '' ); |
| 461 | - // Added by Ash/Upwork | |
| 462 | 492 | } else { |
| 463 | 493 | $render->message = esc_html__( 'CSV file with chart data was not uploaded. Please, try again.', 'visualizer' ); |
| 464 | 494 | } |
| 465 | 495 | if ( $source ) { |
| 466 | 496 | if ( $source->fetch() ) { |
| 467 | - $chart->post_content = $source->getData(); | |
| 497 | + $content = $source->getData(); | |
| 498 | + $populate = true; | |
| 499 | + if ( is_string( $content ) && is_array( unserialize( $content ) ) ) { | |
| 500 | + $json = unserialize( $content ); | |
| 501 | + // if source exists, so should data. if source exists but data is blank, do not populate the chart. | |
| 502 | + // if we populate the data even if it is empty, the chart will show "Table has no columns". | |
| 503 | + if ( array_key_exists( 'source', $json ) && ! empty( $json['source'] ) && ( ! array_key_exists( 'data', $json ) || empty( $json['data'] ) ) ) { | |
| 504 | + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Not populating chart data as source exists (%s) but data is empty!', $json['source'] ), 'warn', __FILE__, __LINE__ ); | |
| 505 | + $populate = false; | |
| 506 | + } | |
| 507 | + } | |
| 508 | + if ( $populate ) { | |
| 509 | + $chart->post_content = $content; | |
| 510 | + } | |
| 468 | 511 | wp_update_post( $chart->to_array() ); |
| 469 | 512 | update_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, $source->getSeries() ); |
| 470 | 513 | update_post_meta( $chart->ID, Visualizer_Plugin::CF_SOURCE, $source->getSourceName() ); |
| 471 | 514 | update_post_meta( $chart->ID, Visualizer_Plugin::CF_DEFAULT_DATA, 0 ); |
| @@ -475,11 +518,12 @@ | ||
| 475 | 518 | $render->message = esc_html__( 'CSV file is broken or invalid. Please, try again.', 'visualizer' ); |
| 476 | 519 | } |
| 477 | 520 | } |
| 478 | 521 | $render->render(); |
| 479 | - if ( ! ( defined( 'VISUALIZER_DO_NOT_DIE' ) && VISUALIZER_DO_NOT_DIE ) ) { | |
| 480 | - defined( 'WP_TESTS_DOMAIN' ) ? wp_die() : exit(); | |
| 522 | + if ( ! $can_die ) { | |
| 523 | + return; | |
| 481 | 524 | } |
| 525 | + defined( 'WP_TESTS_DOMAIN' ) ? wp_die() : exit(); | |
| 482 | 526 | } |
| 483 | 527 | |
| 484 | 528 | /** |
| 485 | 529 | * Clones the chart. |
| @@ -597,5 +641,6 @@ | ||
| 597 | 641 | // Added by Ash/Upwork |
| 598 | 642 | $this->_addAction( 'admin_head', 'renderFlattrScript' ); |
| 599 | 643 | wp_iframe( array( $render, 'render' ) ); |
| 600 | 644 | } |
| 645 | + | |
| 601 | 646 | } |