| @@ -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. |
| @@ -73,9 +71,12 @@ | ||
| 73 | 71 | $query_args = array( |
| 74 | 72 | 'post_type' => Visualizer_Plugin::CPT_VISUALIZER, |
| 75 | 73 | 'posts_per_page' => 9, |
| 76 | 74 | 'paged' => filter_input( |
| 77 | - INPUT_GET, 'page', FILTER_VALIDATE_INT, array( | |
| 75 | + INPUT_GET, | |
| 76 | + 'page', | |
| 77 | + FILTER_VALIDATE_INT, | |
| 78 | + array( | |
| 78 | 79 | 'options' => array( |
| 79 | 80 | 'min_range' => 1, |
| 80 | 81 | 'default' => 1, |
| 81 | 82 | ), |
| @@ -82,8 +83,13 @@ | ||
| 82 | 83 | ) |
| 83 | 84 | ), |
| 84 | 85 | ); |
| 85 | 86 | $filter = filter_input( INPUT_GET, 's', FILTER_SANITIZE_STRING ); |
| 87 | + if ( empty( $filter ) ) { | |
| 88 | + // 'filter' is from the modal from the add media button. | |
| 89 | + $filter = filter_input( INPUT_GET, 'filter', FILTER_SANITIZE_STRING ); | |
| 90 | + } | |
| 91 | + | |
| 86 | 92 | if ( $filter && in_array( $filter, Visualizer_Plugin::getChartTypes() ) ) { |
| 87 | 93 | $query_args['meta_query'] = array( |
| 88 | 94 | array( |
| 89 | 95 | 'key' => Visualizer_Plugin::CF_CHART_TYPE, |
| @@ -167,9 +173,12 @@ | ||
| 167 | 173 | $nonce = wp_verify_nonce( filter_input( $input_method, 'nonce' ) ); |
| 168 | 174 | $capable = current_user_can( 'delete_posts' ); |
| 169 | 175 | if ( $nonce && $capable ) { |
| 170 | 176 | $chart_id = filter_input( |
| 171 | - $input_method, 'chart', FILTER_VALIDATE_INT, array( | |
| 177 | + $input_method, | |
| 178 | + 'chart', | |
| 179 | + FILTER_VALIDATE_INT, | |
| 180 | + array( | |
| 172 | 181 | 'options' => array( |
| 173 | 182 | 'min_range' => 1, |
| 174 | 183 | ), |
| 175 | 184 | ) |
| @@ -205,9 +214,9 @@ | ||
| 205 | 214 | defined( 'IFRAME_REQUEST' ) || define( 'IFRAME_REQUEST', 1 ); |
| 206 | 215 | // check chart, if chart not exists, will create new one and redirects to the same page with proper chart id |
| 207 | 216 | $chart_id = isset( $_GET['chart'] ) ? filter_var( $_GET['chart'], FILTER_VALIDATE_INT ) : ''; |
| 208 | 217 | if ( ! $chart_id || ! ( $chart = get_post( $chart_id ) ) || $chart->post_type != Visualizer_Plugin::CPT_VISUALIZER ) { |
| 209 | - $default_type = 'line'; | |
| 218 | + $default_type = isset( $_GET['type'] ) && ! empty( $_GET['type'] ) ? $_GET['type'] : 'line'; | |
| 210 | 219 | $source = new Visualizer_Source_Csv( VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples' . DIRECTORY_SEPARATOR . $default_type . '.csv' ); |
| 211 | 220 | $source->fetch(); |
| 212 | 221 | $chart_id = wp_insert_post( |
| 213 | 222 | array( |
| @@ -223,9 +232,11 @@ | ||
| 223 | 232 | add_post_meta( $chart_id, Visualizer_Plugin::CF_DEFAULT_DATA, 1 ); |
| 224 | 233 | add_post_meta( $chart_id, Visualizer_Plugin::CF_SOURCE, $source->getSourceName() ); |
| 225 | 234 | add_post_meta( $chart_id, Visualizer_Plugin::CF_SERIES, $source->getSeries() ); |
| 226 | 235 | add_post_meta( |
| 227 | - $chart_id, Visualizer_Plugin::CF_SETTINGS, array( | |
| 236 | + $chart_id, | |
| 237 | + Visualizer_Plugin::CF_SETTINGS, | |
| 238 | + array( | |
| 228 | 239 | 'focusTarget' => 'datum', |
| 229 | 240 | ) |
| 230 | 241 | ); |
| 231 | 242 | do_action( 'visualizer_pro_new_chart_defaults', $chart_id ); |
| @@ -240,20 +251,30 @@ | ||
| 240 | 251 | wp_register_style( 'visualizer-frame', VISUALIZER_ABSURL . 'css/frame.css', array( 'visualizer-chosen' ), Visualizer_Plugin::VERSION ); |
| 241 | 252 | wp_register_script( 'visualizer-frame', VISUALIZER_ABSURL . 'js/frame.js', array( 'visualizer-chosen' ), Visualizer_Plugin::VERSION, true ); |
| 242 | 253 | wp_register_script( 'google-jsapi-new', '//www.gstatic.com/charts/loader.js', array(), null, true ); |
| 243 | 254 | wp_register_script( 'google-jsapi-old', '//www.google.com/jsapi', array( 'google-jsapi-new' ), null, true ); |
| 255 | + wp_register_script( 'visualizer-customization', $this->get_user_customization_js(), array(), null, true ); | |
| 244 | 256 | wp_register_script( |
| 245 | - 'visualizer-render', VISUALIZER_ABSURL . 'js/render.js', array( | |
| 257 | + 'visualizer-render', | |
| 258 | + VISUALIZER_ABSURL . 'js/render.js', | |
| 259 | + array( | |
| 246 | 260 | 'google-jsapi-old', |
| 247 | 261 | 'google-jsapi-new', |
| 248 | 262 | 'visualizer-frame', |
| 249 | - ), Visualizer_Plugin::VERSION, true | |
| 263 | + 'visualizer-customization', | |
| 264 | + ), | |
| 265 | + Visualizer_Plugin::VERSION, | |
| 266 | + true | |
| 250 | 267 | ); |
| 251 | 268 | wp_register_script( |
| 252 | - 'visualizer-preview', VISUALIZER_ABSURL . 'js/preview.js', array( | |
| 269 | + 'visualizer-preview', | |
| 270 | + VISUALIZER_ABSURL . 'js/preview.js', | |
| 271 | + array( | |
| 253 | 272 | 'wp-color-picker', |
| 254 | 273 | 'visualizer-render', |
| 255 | - ), Visualizer_Plugin::VERSION, true | |
| 274 | + ), | |
| 275 | + Visualizer_Plugin::VERSION, | |
| 276 | + true | |
| 256 | 277 | ); |
| 257 | 278 | // added by Ash/Upwork |
| 258 | 279 | if ( VISUALIZER_PRO ) { |
| 259 | 280 | global $Visualizer_Pro; |
| @@ -261,11 +282,27 @@ | ||
| 261 | 282 | } |
| 262 | 283 | // dispatch pages |
| 263 | 284 | $this->_chart = get_post( $chart_id ); |
| 264 | 285 | $tab = isset( $_GET['tab'] ) && ! empty( $_GET['tab'] ) ? $_GET['tab'] : 'visualizer'; |
| 286 | + | |
| 287 | + // skip chart type pages only for existing charts. | |
| 288 | + if ( VISUALIZER_SKIP_CHART_TYPE_PAGE && 'auto-draft' !== $this->_chart->post_status && ( ! empty( $_GET['tab'] ) && 'visualizer' === $_GET['tab'] ) ) { | |
| 289 | + $tab = 'settings'; | |
| 290 | + } | |
| 291 | + | |
| 292 | + if ( isset( $_POST['cancel'] ) && 1 === intval( $_POST['cancel'] ) ) { | |
| 293 | + // if the cancel button is clicked. | |
| 294 | + $this->undoRevisions( $chart_id, true ); | |
| 295 | + } elseif ( isset( $_POST['save'] ) && 1 === intval( $_POST['save'] ) ) { | |
| 296 | + // if the save button is clicked. | |
| 297 | + $this->undoRevisions( $chart_id, false ); | |
| 298 | + } else { | |
| 299 | + // if the edit button is clicked. | |
| 300 | + $this->_chart = $this->handleExistingRevisions( $chart_id, $this->_chart ); | |
| 301 | + } | |
| 302 | + | |
| 265 | 303 | switch ( $tab ) { |
| 266 | 304 | case 'settings': |
| 267 | - // changed by Ash/Upwork | |
| 268 | 305 | $this->_handleDataAndSettingsPage(); |
| 269 | 306 | break; |
| 270 | 307 | case 'type': // fall through. |
| 271 | 308 | case 'visualizer': // fall through. |
| @@ -287,11 +324,18 @@ | ||
| 287 | 324 | } |
| 288 | 325 | if ( $_SERVER['REQUEST_METHOD'] == 'POST' && isset( $_GET['nonce'] ) && wp_verify_nonce( $_GET['nonce'] ) ) { |
| 289 | 326 | if ( $this->_chart->post_status == 'auto-draft' ) { |
| 290 | 327 | $this->_chart->post_status = 'publish'; |
| 328 | + | |
| 329 | + // 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. | |
| 330 | + // we do not want any difference in data so disable revisions temporarily. | |
| 331 | + add_filter( 'wp_revisions_to_keep', '__return_false' ); | |
| 291 | 332 | wp_update_post( $this->_chart->to_array() ); |
| 292 | 333 | } |
| 293 | - update_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_SETTINGS, $_POST ); | |
| 334 | + // save meta data only when it is NOT being canceled. | |
| 335 | + if ( ! ( isset( $_POST['cancel'] ) && 1 === intval( $_POST['cancel'] ) ) ) { | |
| 336 | + update_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_SETTINGS, $_POST ); | |
| 337 | + } | |
| 294 | 338 | $render = new Visualizer_Render_Page_Send(); |
| 295 | 339 | $render->text = sprintf( '[visualizer id="%d"]', $this->_chart->ID ); |
| 296 | 340 | wp_iframe( array( $render, 'render' ) ); |
| 297 | 341 | |
| @@ -317,9 +361,11 @@ | ||
| 317 | 361 | wp_enqueue_style( 'visualizer-frame' ); |
| 318 | 362 | wp_enqueue_script( 'visualizer-preview' ); |
| 319 | 363 | wp_enqueue_script( 'visualizer-render' ); |
| 320 | 364 | wp_localize_script( |
| 321 | - 'visualizer-render', 'visualizer', array( | |
| 365 | + 'visualizer-render', | |
| 366 | + 'visualizer', | |
| 367 | + array( | |
| 322 | 368 | 'l10n' => array( |
| 323 | 369 | 'invalid_source' => esc_html__( 'You have entered invalid URL. Please, insert proper URL.', 'visualizer' ), |
| 324 | 370 | 'loading' => esc_html__( 'Loading...', 'visualizer' ), |
| 325 | 371 | ), |
| @@ -325,8 +371,9 @@ | ||
| 325 | 371 | ), |
| 326 | 372 | 'charts' => array( |
| 327 | 373 | 'canvas' => $data, |
| 328 | 374 | ), |
| 375 | + 'language' => $this->get_language(), | |
| 329 | 376 | 'map_api_key' => get_option( 'visualizer-map-api-key' ), |
| 330 | 377 | 'ajax' => array( |
| 331 | 378 | 'url' => admin_url( 'admin-ajax.php' ), |
| 332 | 379 | 'nonces' => array( |
| @@ -345,8 +392,11 @@ | ||
| 345 | 392 | if ( filter_input( INPUT_GET, 'library', FILTER_VALIDATE_BOOLEAN ) ) { |
| 346 | 393 | $render->button = filter_input( INPUT_GET, 'action' ) == Visualizer_Plugin::ACTION_EDIT_CHART |
| 347 | 394 | ? esc_html__( 'Save Chart', 'visualizer' ) |
| 348 | 395 | : esc_html__( 'Create Chart', 'visualizer' ); |
| 396 | + if ( filter_input( INPUT_GET, 'action' ) == Visualizer_Plugin::ACTION_EDIT_CHART ) { | |
| 397 | + $render->cancel_button = esc_html__( 'Cancel', 'visualizer' ); | |
| 398 | + } | |
| 349 | 399 | } else { |
| 350 | 400 | $render->button = esc_attr__( 'Insert Chart', 'visualizer' ); |
| 351 | 401 | } |
| 352 | 402 | if ( VISUALIZER_PRO ) { |
| @@ -414,20 +464,32 @@ | ||
| 414 | 464 | * |
| 415 | 465 | * @access public |
| 416 | 466 | */ |
| 417 | 467 | public function uploadData() { |
| 468 | + // if this is being called internally from pro and VISUALIZER_DO_NOT_DIE is set. | |
| 469 | + // otherwise, assume this is a normal web request. | |
| 470 | + $can_die = ! ( defined( 'VISUALIZER_DO_NOT_DIE' ) && VISUALIZER_DO_NOT_DIE ); | |
| 471 | + | |
| 418 | 472 | // validate nonce |
| 419 | - // do not use filter_input as it does not work for phpunit test cases, use filter_var instead | |
| 420 | 473 | if ( ! isset( $_GET['nonce'] ) || ! wp_verify_nonce( $_GET['nonce'] ) ) { |
| 474 | + if ( ! $can_die ) { | |
| 475 | + return; | |
| 476 | + } | |
| 421 | 477 | status_header( 403 ); |
| 422 | 478 | exit; |
| 423 | 479 | } |
| 480 | + | |
| 424 | 481 | // check chart, if chart exists |
| 482 | + // do not use filter_input as it does not work for phpunit test cases, use filter_var instead | |
| 425 | 483 | $chart_id = isset( $_GET['chart'] ) ? filter_var( $_GET['chart'], FILTER_VALIDATE_INT ) : ''; |
| 426 | 484 | if ( ! $chart_id || ! ( $chart = get_post( $chart_id ) ) || $chart->post_type != Visualizer_Plugin::CPT_VISUALIZER ) { |
| 485 | + if ( ! $can_die ) { | |
| 486 | + return; | |
| 487 | + } | |
| 427 | 488 | status_header( 400 ); |
| 428 | 489 | exit; |
| 429 | 490 | } |
| 491 | + | |
| 430 | 492 | if ( ! isset( $_POST['vz-import-time'] ) ) { |
| 431 | 493 | apply_filters( 'visualizer_pro_remove_schedule', $chart_id ); |
| 432 | 494 | } |
| 433 | 495 | |
| @@ -444,18 +506,29 @@ | ||
| 444 | 506 | apply_filters( 'visualizer_pro_chart_schedule', $chart_id, $_POST['remote_data'], $_POST['vz-import-time'] ); |
| 445 | 507 | } |
| 446 | 508 | } elseif ( isset( $_FILES['local_data'] ) && $_FILES['local_data']['error'] == 0 ) { |
| 447 | 509 | $source = new Visualizer_Source_Csv( $_FILES['local_data']['tmp_name'] ); |
| 448 | - // Added by Ash/Upwork | |
| 449 | 510 | } elseif ( isset( $_POST['chart_data'] ) && strlen( $_POST['chart_data'] ) > 0 ) { |
| 450 | 511 | $source = apply_filters( 'visualizer_pro_handle_chart_data', $_POST['chart_data'], '' ); |
| 451 | - // Added by Ash/Upwork | |
| 452 | 512 | } else { |
| 453 | 513 | $render->message = esc_html__( 'CSV file with chart data was not uploaded. Please, try again.', 'visualizer' ); |
| 454 | 514 | } |
| 455 | 515 | if ( $source ) { |
| 456 | 516 | if ( $source->fetch() ) { |
| 457 | - $chart->post_content = $source->getData(); | |
| 517 | + $content = $source->getData(); | |
| 518 | + $populate = true; | |
| 519 | + if ( is_string( $content ) && is_array( unserialize( $content ) ) ) { | |
| 520 | + $json = unserialize( $content ); | |
| 521 | + // if source exists, so should data. if source exists but data is blank, do not populate the chart. | |
| 522 | + // if we populate the data even if it is empty, the chart will show "Table has no columns". | |
| 523 | + if ( array_key_exists( 'source', $json ) && ! empty( $json['source'] ) && ( ! array_key_exists( 'data', $json ) || empty( $json['data'] ) ) ) { | |
| 524 | + 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__ ); | |
| 525 | + $populate = false; | |
| 526 | + } | |
| 527 | + } | |
| 528 | + if ( $populate ) { | |
| 529 | + $chart->post_content = $content; | |
| 530 | + } | |
| 458 | 531 | wp_update_post( $chart->to_array() ); |
| 459 | 532 | update_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, $source->getSeries() ); |
| 460 | 533 | update_post_meta( $chart->ID, Visualizer_Plugin::CF_SOURCE, $source->getSourceName() ); |
| 461 | 534 | update_post_meta( $chart->ID, Visualizer_Plugin::CF_DEFAULT_DATA, 0 ); |
| @@ -465,11 +538,12 @@ | ||
| 465 | 538 | $render->message = esc_html__( 'CSV file is broken or invalid. Please, try again.', 'visualizer' ); |
| 466 | 539 | } |
| 467 | 540 | } |
| 468 | 541 | $render->render(); |
| 469 | - if ( ! ( defined( 'VISUALIZER_DO_NOT_DIE' ) && VISUALIZER_DO_NOT_DIE ) ) { | |
| 470 | - defined( 'WP_TESTS_DOMAIN' ) ? wp_die() : exit(); | |
| 542 | + if ( ! $can_die ) { | |
| 543 | + return; | |
| 471 | 544 | } |
| 545 | + defined( 'WP_TESTS_DOMAIN' ) ? wp_die() : exit(); | |
| 472 | 546 | } |
| 473 | 547 | |
| 474 | 548 | /** |
| 475 | 549 | * Clones the chart. |
| @@ -483,9 +557,12 @@ | ||
| 483 | 557 | $nonce = wp_verify_nonce( filter_input( INPUT_GET, 'nonce' ), Visualizer_Plugin::ACTION_CLONE_CHART ); |
| 484 | 558 | $capable = current_user_can( 'edit_posts' ); |
| 485 | 559 | if ( $nonce && $capable ) { |
| 486 | 560 | $chart_id = filter_input( |
| 487 | - INPUT_GET, 'chart', FILTER_VALIDATE_INT, array( | |
| 561 | + INPUT_GET, | |
| 562 | + 'chart', | |
| 563 | + FILTER_VALIDATE_INT, | |
| 564 | + array( | |
| 488 | 565 | 'options' => array( |
| 489 | 566 | 'min_range' => 1, |
| 490 | 567 | ), |
| 491 | 568 | ) |
| @@ -515,9 +592,10 @@ | ||
| 515 | 592 | $redirect = add_query_arg( |
| 516 | 593 | array( |
| 517 | 594 | 'page' => 'visualizer', |
| 518 | 595 | 'type' => filter_input( INPUT_GET, 'type' ), |
| 519 | - ), admin_url( 'upload.php' ) | |
| 596 | + ), | |
| 597 | + admin_url( 'upload.php' ) | |
| 520 | 598 | ); |
| 521 | 599 | } |
| 522 | 600 | } |
| 523 | 601 | wp_redirect( $redirect ); |
| @@ -535,9 +613,11 @@ | ||
| 535 | 613 | check_ajax_referer( Visualizer_Plugin::ACTION_EXPORT_DATA . Visualizer_Plugin::VERSION, 'security' ); |
| 536 | 614 | $capable = current_user_can( 'edit_posts' ); |
| 537 | 615 | if ( $capable ) { |
| 538 | 616 | $chart_id = isset( $_GET['chart'] ) ? filter_var( |
| 539 | - $_GET['chart'], FILTER_VALIDATE_INT, array( | |
| 617 | + $_GET['chart'], | |
| 618 | + FILTER_VALIDATE_INT, | |
| 619 | + array( | |
| 540 | 620 | 'options' => array( |
| 541 | 621 | 'min_range' => 1, |
| 542 | 622 | ), |
| 543 | 623 | ) |
| @@ -568,9 +648,11 @@ | ||
| 568 | 648 | unset( $data['settings']['width'], $data['settings']['height'] ); |
| 569 | 649 | wp_enqueue_style( 'visualizer-frame' ); |
| 570 | 650 | wp_enqueue_script( 'visualizer-render' ); |
| 571 | 651 | wp_localize_script( |
| 572 | - 'visualizer-render', 'visualizer', array( | |
| 652 | + 'visualizer-render', | |
| 653 | + 'visualizer', | |
| 654 | + array( | |
| 573 | 655 | 'l10n' => array( |
| 574 | 656 | 'invalid_source' => esc_html__( 'You have entered invalid URL. Please, insert proper URL.', 'visualizer' ), |
| 575 | 657 | 'loading' => esc_html__( 'Loading...', 'visualizer' ), |
| 576 | 658 | ), |
| @@ -587,5 +669,6 @@ | ||
| 587 | 669 | // Added by Ash/Upwork |
| 588 | 670 | $this->_addAction( 'admin_head', 'renderFlattrScript' ); |
| 589 | 671 | wp_iframe( array( $render, 'render' ) ); |
| 590 | 672 | } |
| 673 | + | |
| 591 | 674 | } |