| @@ -60,12 +60,159 @@ | ||
| 60 | 60 | $this->_addAjaxAction( Visualizer_Plugin::ACTION_EXPORT_DATA, 'exportData' ); |
| 61 | 61 | |
| 62 | 62 | $this->_addAjaxAction( Visualizer_Plugin::ACTION_FETCH_DB_DATA, 'getQueryData' ); |
| 63 | 63 | $this->_addAjaxAction( Visualizer_Plugin::ACTION_SAVE_DB_QUERY, 'saveQuery' ); |
| 64 | + | |
| 65 | + $this->_addAjaxAction( Visualizer_Plugin::ACTION_JSON_GET_ROOTS, 'getJsonRoots' ); | |
| 66 | + $this->_addAjaxAction( Visualizer_Plugin::ACTION_JSON_GET_DATA, 'getJsonData' ); | |
| 67 | + $this->_addAjaxAction( Visualizer_Plugin::ACTION_JSON_SET_DATA, 'setJsonData' ); | |
| 68 | + $this->_addAjaxAction( Visualizer_Plugin::ACTION_JSON_SET_SCHEDULE, 'setJsonSchedule' ); | |
| 69 | + | |
| 64 | 70 | $this->_addAjaxAction( Visualizer_Plugin::ACTION_SAVE_FILTER_QUERY, 'saveFilter' ); |
| 71 | + | |
| 65 | 72 | } |
| 66 | 73 | |
| 67 | 74 | /** |
| 75 | + * Sets the schedule for how JSON-endpoint charts should be updated. | |
| 76 | + * | |
| 77 | + * @since ? | |
| 78 | + * | |
| 79 | + * @access public | |
| 80 | + */ | |
| 81 | + public function setJsonSchedule() { | |
| 82 | + check_ajax_referer( Visualizer_Plugin::ACTION_JSON_SET_SCHEDULE . Visualizer_Plugin::VERSION, 'security' ); | |
| 83 | + | |
| 84 | + $chart_id = filter_input( | |
| 85 | + INPUT_POST, | |
| 86 | + 'chart', | |
| 87 | + FILTER_VALIDATE_INT, | |
| 88 | + array( | |
| 89 | + 'options' => array( | |
| 90 | + 'min_range' => 1, | |
| 91 | + ), | |
| 92 | + ) | |
| 93 | + ); | |
| 94 | + | |
| 95 | + if ( ! $chart_id ) { | |
| 96 | + wp_send_json_error(); | |
| 97 | + } | |
| 98 | + | |
| 99 | + $time = filter_input( | |
| 100 | + INPUT_POST, | |
| 101 | + 'time', | |
| 102 | + FILTER_VALIDATE_INT, | |
| 103 | + array( | |
| 104 | + 'options' => array( | |
| 105 | + 'min_range' => -1, | |
| 106 | + ), | |
| 107 | + ) | |
| 108 | + ); | |
| 109 | + | |
| 110 | + delete_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_SCHEDULE ); | |
| 111 | + | |
| 112 | + if ( -1 < $time ) { | |
| 113 | + add_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_SCHEDULE, $time ); | |
| 114 | + } | |
| 115 | + wp_send_json_success(); | |
| 116 | + } | |
| 117 | + | |
| 118 | + /** | |
| 119 | + * Get the root elements for JSON-endpoint. | |
| 120 | + * | |
| 121 | + * @since ? | |
| 122 | + * | |
| 123 | + * @access public | |
| 124 | + */ | |
| 125 | + public function getJsonRoots() { | |
| 126 | + check_ajax_referer( Visualizer_Plugin::ACTION_JSON_GET_ROOTS . Visualizer_Plugin::VERSION, 'security' ); | |
| 127 | + | |
| 128 | + $params = wp_parse_args( $_POST['params'] ); | |
| 129 | + | |
| 130 | + $source = new Visualizer_Source_Json( $params ); | |
| 131 | + | |
| 132 | + $roots = $source->fetchRoots(); | |
| 133 | + if ( empty( $roots ) ) { | |
| 134 | + wp_send_json_error(); | |
| 135 | + } | |
| 136 | + | |
| 137 | + wp_send_json_success( array( 'url' => $params['url'], 'roots' => $roots ) ); | |
| 138 | + } | |
| 139 | + | |
| 140 | + /** | |
| 141 | + * Get the data for the JSON-endpoint corresponding to the chosen root. | |
| 142 | + * | |
| 143 | + * @since ? | |
| 144 | + * | |
| 145 | + * @access public | |
| 146 | + */ | |
| 147 | + public function getJsonData() { | |
| 148 | + check_ajax_referer( Visualizer_Plugin::ACTION_JSON_GET_DATA . Visualizer_Plugin::VERSION, 'security' ); | |
| 149 | + | |
| 150 | + $params = wp_parse_args( $_POST['params'] ); | |
| 151 | + | |
| 152 | + $chart_id = $params['chart']; | |
| 153 | + | |
| 154 | + if ( empty( $chart_id ) ) { | |
| 155 | + wp_die(); | |
| 156 | + } | |
| 157 | + | |
| 158 | + $source = new Visualizer_Source_Json( $params ); | |
| 159 | + | |
| 160 | + $data = $source->parse(); | |
| 161 | + if ( empty( $data ) ) { | |
| 162 | + wp_send_json_error(); | |
| 163 | + } | |
| 164 | + | |
| 165 | + $data = Visualizer_Render_Layout::show( 'editor-table', $data, $chart_id, 'viz-json-table', false, false ); | |
| 166 | + wp_send_json_success( array( 'table' => $data, 'root' => $params['root'], 'url' => $params['url'], 'paging' => $source->getPaginationElements() ) ); | |
| 167 | + } | |
| 168 | + | |
| 169 | + /** | |
| 170 | + * Updates the database with the correct post parameters for JSON-endpoint charts. | |
| 171 | + * | |
| 172 | + * @since ? | |
| 173 | + * | |
| 174 | + * @access public | |
| 175 | + */ | |
| 176 | + public function setJsonData() { | |
| 177 | + check_ajax_referer( Visualizer_Plugin::ACTION_JSON_SET_DATA . Visualizer_Plugin::VERSION, 'security' ); | |
| 178 | + | |
| 179 | + $params = $_POST; | |
| 180 | + $chart_id = $_GET['chart']; | |
| 181 | + | |
| 182 | + if ( empty( $chart_id ) ) { | |
| 183 | + wp_die(); | |
| 184 | + } | |
| 185 | + | |
| 186 | + $chart = get_post( $chart_id ); | |
| 187 | + | |
| 188 | + $source = new Visualizer_Source_Json( $params ); | |
| 189 | + $source->fetch(); | |
| 190 | + | |
| 191 | + $content = $source->getData(); | |
| 192 | + $chart->post_content = $content; | |
| 193 | + wp_update_post( $chart->to_array() ); | |
| 194 | + update_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, $source->getSeries() ); | |
| 195 | + update_post_meta( $chart->ID, Visualizer_Plugin::CF_SOURCE, $source->getSourceName() ); | |
| 196 | + update_post_meta( $chart->ID, Visualizer_Plugin::CF_DEFAULT_DATA, 0 ); | |
| 197 | + update_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_URL, $params['url'] ); | |
| 198 | + update_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_ROOT, $params['root'] ); | |
| 199 | + delete_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_PAGING ); | |
| 200 | + if ( ! empty( $params['paging'] ) ) { | |
| 201 | + add_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_PAGING, $params['paging'] ); | |
| 202 | + } | |
| 203 | + | |
| 204 | + $render = new Visualizer_Render_Page_Update(); | |
| 205 | + $render->id = $chart->ID; | |
| 206 | + $render->data = json_encode( $source->getRawData() ); | |
| 207 | + $render->series = json_encode( $source->getSeries() ); | |
| 208 | + $render->render(); | |
| 209 | + | |
| 210 | + defined( 'WP_TESTS_DOMAIN' ) ? wp_die() : exit(); | |
| 211 | + } | |
| 212 | + | |
| 213 | + | |
| 214 | + /** | |
| 68 | 215 | * Fetches charts from database. |
| 69 | 216 | * |
| 70 | 217 | * This method is also called from the media pop-up (classic editor: create a post and add chart from insert content). |
| 71 | 218 | * |
| @@ -94,9 +241,9 @@ | ||
| 94 | 241 | // 'filter' is from the modal from the add media button. |
| 95 | 242 | $filter = filter_input( INPUT_GET, 'filter', FILTER_SANITIZE_STRING ); |
| 96 | 243 | } |
| 97 | 244 | |
| 98 | - if ( $filter && in_array( $filter, Visualizer_Plugin::getChartTypes() ) ) { | |
| 245 | + if ( $filter && in_array( $filter, Visualizer_Plugin::getChartTypes(), true ) ) { | |
| 99 | 246 | $query_args['meta_query'] = array( |
| 100 | 247 | array( |
| 101 | 248 | 'key' => Visualizer_Plugin::CF_CHART_TYPE, |
| 102 | 249 | 'value' => $filter, |
| @@ -201,9 +348,9 @@ | ||
| 201 | 348 | * |
| 202 | 349 | * @access public |
| 203 | 350 | */ |
| 204 | 351 | public function deleteChart() { |
| 205 | - $is_post = $_SERVER['REQUEST_METHOD'] == 'POST'; | |
| 352 | + $is_post = $_SERVER['REQUEST_METHOD'] === 'POST'; | |
| 206 | 353 | $input_method = $is_post ? INPUT_POST : INPUT_GET; |
| 207 | 354 | $chart_id = $success = false; |
| 208 | 355 | $nonce = wp_verify_nonce( filter_input( $input_method, 'nonce' ) ); |
| 209 | 356 | $capable = current_user_can( 'delete_posts' ); |
| @@ -219,9 +366,9 @@ | ||
| 219 | 366 | ) |
| 220 | 367 | ); |
| 221 | 368 | if ( $chart_id ) { |
| 222 | 369 | $chart = get_post( $chart_id ); |
| 223 | - $success = $chart && $chart->post_type == Visualizer_Plugin::CPT_VISUALIZER; | |
| 370 | + $success = $chart && $chart->post_type === Visualizer_Plugin::CPT_VISUALIZER; | |
| 224 | 371 | } |
| 225 | 372 | } |
| 226 | 373 | if ( $success ) { |
| 227 | 374 | wp_delete_post( $chart_id, true ); |
| @@ -276,9 +423,9 @@ | ||
| 276 | 423 | public function renderChartPages() { |
| 277 | 424 | defined( 'IFRAME_REQUEST' ) || define( 'IFRAME_REQUEST', 1 ); |
| 278 | 425 | // check chart, if chart not exists, will create new one and redirects to the same page with proper chart id |
| 279 | 426 | $chart_id = isset( $_GET['chart'] ) ? filter_var( $_GET['chart'], FILTER_VALIDATE_INT ) : ''; |
| 280 | - if ( ! $chart_id || ! ( $chart = get_post( $chart_id ) ) || $chart->post_type != Visualizer_Plugin::CPT_VISUALIZER ) { | |
| 427 | + if ( ! $chart_id || ! ( $chart = get_post( $chart_id ) ) || $chart->post_type !== Visualizer_Plugin::CPT_VISUALIZER ) { | |
| 281 | 428 | $this->deleteOldCharts(); |
| 282 | 429 | $default_type = isset( $_GET['type'] ) && ! empty( $_GET['type'] ) ? $_GET['type'] : 'line'; |
| 283 | 430 | $source = new Visualizer_Source_Csv( VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples' . DIRECTORY_SEPARATOR . $default_type . '.csv' ); |
| 284 | 431 | $source->fetch(); |
| @@ -315,9 +462,9 @@ | ||
| 315 | 462 | wp_register_script( 'visualizer-chosen', VISUALIZER_ABSURL . 'js/lib/chosen.jquery.min.js', array( 'jquery' ), Visualizer_Plugin::VERSION ); |
| 316 | 463 | wp_register_style( 'visualizer-chosen', VISUALIZER_ABSURL . 'css/lib/chosen.min.css', array(), Visualizer_Plugin::VERSION ); |
| 317 | 464 | |
| 318 | 465 | wp_register_style( 'visualizer-frame', VISUALIZER_ABSURL . 'css/frame.css', array( 'visualizer-chosen' ), Visualizer_Plugin::VERSION ); |
| 319 | - wp_register_script( 'visualizer-frame', VISUALIZER_ABSURL . 'js/frame.js', array( 'visualizer-chosen' ), Visualizer_Plugin::VERSION, true ); | |
| 466 | + wp_register_script( 'visualizer-frame', VISUALIZER_ABSURL . 'js/frame.js', array( 'visualizer-chosen', 'jquery-ui-accordion' ), Visualizer_Plugin::VERSION, true ); | |
| 320 | 467 | wp_register_script( 'visualizer-customization', $this->get_user_customization_js(), array(), null, true ); |
| 321 | 468 | wp_register_script( |
| 322 | 469 | 'visualizer-render', |
| 323 | 470 | VISUALIZER_ABSURL . 'js/render-facade.js', |
| @@ -334,13 +481,16 @@ | ||
| 334 | 481 | ), |
| 335 | 482 | Visualizer_Plugin::VERSION, |
| 336 | 483 | true |
| 337 | 484 | ); |
| 485 | + wp_register_script( 'visualizer-editor-simple', VISUALIZER_ABSURL . 'js/simple-editor.js', array( 'jquery' ), Visualizer_Plugin::VERSION, true ); | |
| 486 | + | |
| 338 | 487 | // added by Ash/Upwork |
| 339 | 488 | if ( VISUALIZER_PRO ) { |
| 340 | 489 | global $Visualizer_Pro; |
| 341 | 490 | $Visualizer_Pro->_addScriptsAndStyles(); |
| 342 | 491 | } |
| 492 | + | |
| 343 | 493 | // dispatch pages |
| 344 | 494 | $this->_chart = get_post( $chart_id ); |
| 345 | 495 | $tab = isset( $_GET['tab'] ) && ! empty( $_GET['tab'] ) ? $_GET['tab'] : 'visualizer'; |
| 346 | 496 | |
| @@ -380,8 +530,17 @@ | ||
| 380 | 530 | */ |
| 381 | 531 | private function loadCodeEditorAssets() { |
| 382 | 532 | global $wp_version; |
| 383 | 533 | |
| 534 | + $wp_scripts = wp_scripts(); | |
| 535 | + | |
| 536 | + // data tables assets. | |
| 537 | + wp_register_script( 'visualizer-datatables', '//cdn.datatables.net/v/dt/dt-1.10.18/b-1.5.6/b-colvis-1.5.6/cr-1.5.0/fc-3.2.5/fh-3.1.4/r-2.2.2/sc-2.0.0/sl-1.3.0/datatables.min.js', array( 'jquery-ui-core' ), Visualizer_Plugin::VERSION ); | |
| 538 | + wp_register_style( 'visualizer-datatables', '//cdn.datatables.net/v/dt/dt-1.10.18/b-1.5.6/b-colvis-1.5.6/cr-1.5.0/fc-3.2.5/fh-3.1.4/r-2.2.2/sc-2.0.0/sl-1.3.0/datatables.min.css', array(), Visualizer_Plugin::VERSION ); | |
| 539 | + wp_register_style( 'visualizer-jquery-ui', sprintf( '//ajax.googleapis.com/ajax/libs/jqueryui/%s/themes/smoothness/jquery-ui.css', $wp_scripts->registered['jquery-ui-core']->ver ), array( 'visualizer-datatables' ), Visualizer_Plugin::VERSION ); | |
| 540 | + wp_enqueue_script( 'visualizer-datatables' ); | |
| 541 | + wp_enqueue_style( 'visualizer-jquery-ui' ); | |
| 542 | + | |
| 384 | 543 | if ( ! VISUALIZER_PRO ) { |
| 385 | 544 | return; |
| 386 | 545 | } |
| 387 | 546 | |
| @@ -386,16 +545,8 @@ | ||
| 386 | 545 | } |
| 387 | 546 | |
| 388 | 547 | $table_col_mapping = Visualizer_Source_Query_Params::get_all_db_tables_column_mapping(); |
| 389 | 548 | |
| 390 | - // data tables assets. | |
| 391 | - wp_register_script( 'visualizer-datatables', '//cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js', array( 'jquery-ui-core' ), Visualizer_Plugin::VERSION ); | |
| 392 | - wp_register_style( 'visualizer-datatables', '//cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css', array(), Visualizer_Plugin::VERSION ); | |
| 393 | - wp_register_style( 'visualizer-datatables-ui', '//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css', array( 'visualizer-datatables' ), Visualizer_Plugin::VERSION ); | |
| 394 | - | |
| 395 | - wp_enqueue_script( 'visualizer-datatables' ); | |
| 396 | - wp_enqueue_style( 'visualizer-datatables-ui' ); | |
| 397 | - | |
| 398 | 549 | if ( version_compare( $wp_version, '4.9.0', '<' ) ) { |
| 399 | 550 | // code mirror assets. |
| 400 | 551 | wp_register_script( 'visualizer-codemirror-core', '//codemirror.net/lib/codemirror.js', array( 'jquery' ), Visualizer_Plugin::VERSION ); |
| 401 | 552 | wp_register_script( 'visualizer-codemirror-placeholder', '//codemirror.net/addon/display/placeholder.js', array( 'visualizer-codemirror-core' ), Visualizer_Plugin::VERSION ); |
| @@ -435,10 +586,10 @@ | ||
| 435 | 586 | private function _handleDataAndSettingsPage() { |
| 436 | 587 | if ( isset( $_POST['map_api_key'] ) ) { |
| 437 | 588 | update_option( 'visualizer-map-api-key', $_POST['map_api_key'] ); |
| 438 | 589 | } |
| 439 | - if ( $_SERVER['REQUEST_METHOD'] == 'POST' && isset( $_GET['nonce'] ) && wp_verify_nonce( $_GET['nonce'] ) ) { | |
| 440 | - if ( $this->_chart->post_status == 'auto-draft' ) { | |
| 590 | + if ( $_SERVER['REQUEST_METHOD'] === 'POST' && isset( $_GET['nonce'] ) && wp_verify_nonce( $_GET['nonce'] ) ) { | |
| 591 | + if ( $this->_chart->post_status === 'auto-draft' ) { | |
| 441 | 592 | $this->_chart->post_status = 'publish'; |
| 442 | 593 | |
| 443 | 594 | // 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. |
| 444 | 595 | // we do not want any difference in data so disable revisions temporarily. |
| @@ -463,9 +614,9 @@ | ||
| 463 | 614 | $sidebar->__series = $data['series']; |
| 464 | 615 | $sidebar->__data = $data['data']; |
| 465 | 616 | } else { |
| 466 | 617 | $sidebar = apply_filters( 'visualizer_pro_chart_type_sidebar', '', $data ); |
| 467 | - if ( $sidebar != '' ) { | |
| 618 | + if ( $sidebar !== '' ) { | |
| 468 | 619 | $sidebar->__series = $data['series']; |
| 469 | 620 | $sidebar->__data = $data['data']; |
| 470 | 621 | } |
| 471 | 622 | } |
| @@ -475,8 +626,25 @@ | ||
| 475 | 626 | wp_enqueue_script( 'visualizer-preview' ); |
| 476 | 627 | wp_enqueue_script( 'visualizer-chosen' ); |
| 477 | 628 | wp_enqueue_script( 'visualizer-render' ); |
| 478 | 629 | |
| 630 | + if ( ! VISUALIZER_PRO ) { | |
| 631 | + wp_enqueue_script( 'visualizer-editor-simple' ); | |
| 632 | + wp_localize_script( | |
| 633 | + 'visualizer-editor-simple', | |
| 634 | + 'visualizer1', | |
| 635 | + array( | |
| 636 | + 'ajax' => array( | |
| 637 | + 'url' => admin_url( 'admin-ajax.php' ), | |
| 638 | + 'nonces' => array( | |
| 639 | + ), | |
| 640 | + 'actions' => array( | |
| 641 | + ), | |
| 642 | + ), | |
| 643 | + ) | |
| 644 | + ); | |
| 645 | + } | |
| 646 | + | |
| 479 | 647 | $table_col_mapping = $this->loadCodeEditorAssets(); |
| 480 | 648 | |
| 481 | 649 | wp_localize_script( |
| 482 | 650 | 'visualizer-render', |
| @@ -484,8 +652,9 @@ | ||
| 484 | 652 | array( |
| 485 | 653 | 'l10n' => array( |
| 486 | 654 | 'invalid_source' => esc_html__( 'You have entered invalid URL. Please, insert proper URL.', 'visualizer' ), |
| 487 | 655 | 'loading' => esc_html__( 'Loading...', 'visualizer' ), |
| 656 | + 'json_error' => esc_html__( 'An error occured in fetching data.', 'visualizer' ), | |
| 488 | 657 | ), |
| 489 | 658 | 'charts' => array( |
| 490 | 659 | 'canvas' => $data, |
| 491 | 660 | 'id' => $this->_chart->ID, |
| @@ -496,12 +665,18 @@ | ||
| 496 | 665 | 'url' => admin_url( 'admin-ajax.php' ), |
| 497 | 666 | 'nonces' => array( |
| 498 | 667 | 'permissions' => wp_create_nonce( Visualizer_Plugin::ACTION_FETCH_PERMISSIONS_DATA ), |
| 499 | 668 | 'db_get_data' => wp_create_nonce( Visualizer_Plugin::ACTION_FETCH_DB_DATA . Visualizer_Plugin::VERSION ), |
| 669 | + 'json_get_roots' => wp_create_nonce( Visualizer_Plugin::ACTION_JSON_GET_ROOTS . Visualizer_Plugin::VERSION ), | |
| 670 | + 'json_get_data' => wp_create_nonce( Visualizer_Plugin::ACTION_JSON_GET_DATA . Visualizer_Plugin::VERSION ), | |
| 671 | + 'json_set_schedule' => wp_create_nonce( Visualizer_Plugin::ACTION_JSON_SET_SCHEDULE . Visualizer_Plugin::VERSION ), | |
| 500 | 672 | ), |
| 501 | 673 | 'actions' => array( |
| 502 | 674 | 'permissions' => Visualizer_Plugin::ACTION_FETCH_PERMISSIONS_DATA, |
| 503 | 675 | 'db_get_data' => Visualizer_Plugin::ACTION_FETCH_DB_DATA, |
| 676 | + 'json_get_roots' => Visualizer_Plugin::ACTION_JSON_GET_ROOTS, | |
| 677 | + 'json_get_data' => Visualizer_Plugin::ACTION_JSON_GET_DATA, | |
| 678 | + 'json_set_schedule' => Visualizer_Plugin::ACTION_JSON_SET_SCHEDULE, | |
| 504 | 679 | ), |
| 505 | 680 | ), |
| 506 | 681 | 'db_query' => array( |
| 507 | 682 | 'tables' => $table_col_mapping, |
| @@ -507,8 +682,11 @@ | ||
| 507 | 682 | 'tables' => $table_col_mapping, |
| 508 | 683 | ), |
| 509 | 684 | 'is_pro' => VISUALIZER_PRO, |
| 510 | 685 | 'page_type' => 'chart', |
| 686 | + 'json_tag_separator' => Visualizer_Source_Json::TAG_SEPARATOR, | |
| 687 | + 'json_tag_separator_view' => Visualizer_Source_Json::TAG_SEPARATOR_VIEW, | |
| 688 | + 'is_front' => false, | |
| 511 | 689 | ) |
| 512 | 690 | ); |
| 513 | 691 | |
| 514 | 692 | $render = new Visualizer_Render_Page_Data(); |
| @@ -516,12 +694,12 @@ | ||
| 516 | 694 | $render->type = $data['type']; |
| 517 | 695 | $render->custom_css = $data['css']; |
| 518 | 696 | $render->sidebar = $sidebar; |
| 519 | 697 | if ( filter_input( INPUT_GET, 'library', FILTER_VALIDATE_BOOLEAN ) ) { |
| 520 | - $render->button = filter_input( INPUT_GET, 'action' ) == Visualizer_Plugin::ACTION_EDIT_CHART | |
| 698 | + $render->button = filter_input( INPUT_GET, 'action' ) === Visualizer_Plugin::ACTION_EDIT_CHART | |
| 521 | 699 | ? esc_html__( 'Save Chart', 'visualizer' ) |
| 522 | 700 | : esc_html__( 'Create Chart', 'visualizer' ); |
| 523 | - if ( filter_input( INPUT_GET, 'action' ) == Visualizer_Plugin::ACTION_EDIT_CHART ) { | |
| 701 | + if ( filter_input( INPUT_GET, 'action' ) === Visualizer_Plugin::ACTION_EDIT_CHART ) { | |
| 524 | 702 | $render->cancel_button = esc_html__( 'Cancel', 'visualizer' ); |
| 525 | 703 | } |
| 526 | 704 | } else { |
| 527 | 705 | $render->button = esc_attr__( 'Insert Chart', 'visualizer' ); |
| @@ -542,11 +720,11 @@ | ||
| 542 | 720 | * @access private |
| 543 | 721 | */ |
| 544 | 722 | private function _handleTypesPage() { |
| 545 | 723 | // process post request |
| 546 | - if ( $_SERVER['REQUEST_METHOD'] == 'POST' && wp_verify_nonce( filter_input( INPUT_POST, 'nonce' ) ) ) { | |
| 724 | + if ( $_SERVER['REQUEST_METHOD'] === 'POST' && wp_verify_nonce( filter_input( INPUT_POST, 'nonce' ) ) ) { | |
| 547 | 725 | $type = filter_input( INPUT_POST, 'type' ); |
| 548 | - if ( in_array( $type, Visualizer_Plugin::getChartTypes() ) ) { | |
| 726 | + if ( in_array( $type, Visualizer_Plugin::getChartTypes(), true ) ) { | |
| 549 | 727 | // save new chart type |
| 550 | 728 | update_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_CHART_TYPE, $type ); |
| 551 | 729 | // if the chart has default data, update it with appropriate default data for new type |
| 552 | 730 | if ( filter_var( get_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_DEFAULT_DATA, true ), FILTER_VALIDATE_BOOLEAN ) ) { |
| @@ -582,10 +760,108 @@ | ||
| 582 | 760 | */ |
| 583 | 761 | public function renderFlattrScript() { |
| 584 | 762 | echo ''; |
| 585 | 763 | } |
| 586 | - // changed by Ash/Upwork | |
| 764 | + | |
| 587 | 765 | /** |
| 766 | + * Processes the CSV that is sent in the request as a string. | |
| 767 | + * | |
| 768 | + * @since 3.2.0 | |
| 769 | + */ | |
| 770 | + private function handleCSVasString( $data ) { | |
| 771 | + $source = null; | |
| 772 | + if ( VISUALIZER_PRO ) { | |
| 773 | + $source = apply_filters( 'visualizer_pro_handle_chart_data', $data, '' ); | |
| 774 | + } else { | |
| 775 | + // data coming in from the text editor. | |
| 776 | + $tmpfile = tempnam( get_temp_dir(), Visualizer_Plugin::NAME ); | |
| 777 | + $handle = fopen( $tmpfile, 'w' ); | |
| 778 | + $values = preg_split( '/[\n\r]+/', stripslashes( trim( $data ) ) ); | |
| 779 | + if ( $values ) { | |
| 780 | + foreach ( $values as $row ) { | |
| 781 | + if ( empty( $row ) ) { | |
| 782 | + continue; | |
| 783 | + } | |
| 784 | + $columns = explode( ',', $row ); | |
| 785 | + fputcsv( $handle, $columns ); | |
| 786 | + } | |
| 787 | + } | |
| 788 | + $source = new Visualizer_Source_Csv( $tmpfile ); | |
| 789 | + fclose( $handle ); | |
| 790 | + } | |
| 791 | + return $source; | |
| 792 | + } | |
| 793 | + | |
| 794 | + /** | |
| 795 | + * Parses the data uploaded as an HTML table. | |
| 796 | + * | |
| 797 | + * @since 3.2.0 | |
| 798 | + * | |
| 799 | + * @access private | |
| 800 | + */ | |
| 801 | + private function handleTabularData() { | |
| 802 | + $csv = array(); | |
| 803 | + // the datatable mentions the headers twice, so lets remove the duplicates. | |
| 804 | + $headers = array_unique( array_filter( $_POST['header'] ) ); | |
| 805 | + $types = $_POST['type']; | |
| 806 | + | |
| 807 | + // capture all the indexes that correspond to excluded columns. | |
| 808 | + $exclude = array(); | |
| 809 | + $index = 0; | |
| 810 | + foreach ( $types as $type ) { | |
| 811 | + if ( empty( $type ) ) { | |
| 812 | + $exclude[] = $index; | |
| 813 | + } | |
| 814 | + $index++; | |
| 815 | + } | |
| 816 | + | |
| 817 | + // when N headers are being renamed, the number of headers increases by N | |
| 818 | + // because of the way datatable duplicates header information | |
| 819 | + // so unset the headers that have been renamed. | |
| 820 | + if ( count( $headers ) !== count( $types ) ) { | |
| 821 | + $to = count( $headers ); | |
| 822 | + for ( $i = count( $types ); $i < $to; $i++ ) { | |
| 823 | + unset( $headers[ $i + 1 ] ); | |
| 824 | + } | |
| 825 | + } | |
| 826 | + | |
| 827 | + $columns = array(); | |
| 828 | + for ( $i = 0; $i < count( $headers ); $i++ ) { | |
| 829 | + if ( ! isset( $_POST[ 'data' . $i ] ) ) { | |
| 830 | + continue; | |
| 831 | + } | |
| 832 | + $columns[ $i ] = $_POST[ 'data' . $i ]; | |
| 833 | + } | |
| 834 | + | |
| 835 | + $csv[] = $headers; | |
| 836 | + $csv[] = $types; | |
| 837 | + for ( $j = 0; $j < count( $columns[0] ); $j++ ) { | |
| 838 | + $row = array(); | |
| 839 | + for ( $i = 0; $i < count( $headers ); $i++ ) { | |
| 840 | + $row[] = $columns[ $i ][ $j ]; | |
| 841 | + } | |
| 842 | + $csv[] = $row; | |
| 843 | + } | |
| 844 | + | |
| 845 | + $tmpfile = tempnam( get_temp_dir(), Visualizer_Plugin::NAME ); | |
| 846 | + $handle = fopen( $tmpfile, 'w' ); | |
| 847 | + | |
| 848 | + if ( $csv ) { | |
| 849 | + $index = 0; | |
| 850 | + foreach ( $csv as $row ) { | |
| 851 | + // remove all the cells corresponding to the excluded headers. | |
| 852 | + foreach ( $exclude as $j ) { | |
| 853 | + unset( $row[ $j ] ); | |
| 854 | + } | |
| 855 | + fputcsv( $handle, $row ); | |
| 856 | + } | |
| 857 | + } | |
| 858 | + $source = new Visualizer_Source_Csv( $tmpfile ); | |
| 859 | + fclose( $handle ); | |
| 860 | + return $source; | |
| 861 | + } | |
| 862 | + | |
| 863 | + /** | |
| 588 | 864 | * Parses uploaded CSV file and saves new data for the chart. |
| 589 | 865 | * |
| 590 | 866 | * @since 1.0.0 |
| 591 | 867 | * |
| @@ -607,9 +883,9 @@ | ||
| 607 | 883 | |
| 608 | 884 | // check chart, if chart exists |
| 609 | 885 | // do not use filter_input as it does not work for phpunit test cases, use filter_var instead |
| 610 | 886 | $chart_id = isset( $_GET['chart'] ) ? filter_var( $_GET['chart'], FILTER_VALIDATE_INT ) : ''; |
| 611 | - if ( ! $chart_id || ! ( $chart = get_post( $chart_id ) ) || $chart->post_type != Visualizer_Plugin::CPT_VISUALIZER ) { | |
| 887 | + if ( ! $chart_id || ! ( $chart = get_post( $chart_id ) ) || $chart->post_type !== Visualizer_Plugin::CPT_VISUALIZER ) { | |
| 612 | 888 | if ( ! $can_die ) { |
| 613 | 889 | return; |
| 614 | 890 | } |
| 615 | 891 | status_header( 400 ); |
| @@ -630,8 +906,13 @@ | ||
| 630 | 906 | delete_post_meta( $chart_id, Visualizer_Plugin::CF_DB_QUERY ); |
| 631 | 907 | delete_post_meta( $chart_id, Visualizer_Plugin::CF_DB_SCHEDULE ); |
| 632 | 908 | } |
| 633 | 909 | |
| 910 | + // delete json related data. | |
| 911 | + delete_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_URL ); | |
| 912 | + delete_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_ROOT ); | |
| 913 | + delete_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_PAGING ); | |
| 914 | + | |
| 634 | 915 | $source = null; |
| 635 | 916 | $render = new Visualizer_Render_Page_Update(); |
| 636 | 917 | if ( isset( $_POST['remote_data'] ) && filter_var( $_POST['remote_data'], FILTER_VALIDATE_URL ) ) { |
| 637 | 918 | $source = new Visualizer_Source_Csv_Remote( $_POST['remote_data'] ); |
| @@ -637,12 +918,15 @@ | ||
| 637 | 918 | $source = new Visualizer_Source_Csv_Remote( $_POST['remote_data'] ); |
| 638 | 919 | if ( isset( $_POST['vz-import-time'] ) ) { |
| 639 | 920 | apply_filters( 'visualizer_pro_chart_schedule', $chart_id, $_POST['remote_data'], $_POST['vz-import-time'] ); |
| 640 | 921 | } |
| 922 | + // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison | |
| 641 | 923 | } elseif ( isset( $_FILES['local_data'] ) && $_FILES['local_data']['error'] == 0 ) { |
| 642 | 924 | $source = new Visualizer_Source_Csv( $_FILES['local_data']['tmp_name'] ); |
| 643 | 925 | } elseif ( isset( $_POST['chart_data'] ) && strlen( $_POST['chart_data'] ) > 0 ) { |
| 644 | - $source = apply_filters( 'visualizer_pro_handle_chart_data', $_POST['chart_data'], '', $chart_id, $_POST ); | |
| 926 | + $source = $this->handleCSVasString( $_POST['chart_data'] ); | |
| 927 | + } elseif ( isset( $_POST['table_data'] ) && 'yes' === $_POST['table_data'] ) { | |
| 928 | + $source = $this->handleTabularData(); | |
| 645 | 929 | } else { |
| 646 | 930 | $render->message = esc_html__( 'CSV file with chart data was not uploaded. Please, try again.', 'visualizer' ); |
| 647 | 931 | } |
| 648 | 932 | if ( $source ) { |
| @@ -702,9 +986,9 @@ | ||
| 702 | 986 | ) |
| 703 | 987 | ); |
| 704 | 988 | if ( $chart_id ) { |
| 705 | 989 | $chart = get_post( $chart_id ); |
| 706 | - $success = $chart && $chart->post_type == Visualizer_Plugin::CPT_VISUALIZER; | |
| 990 | + $success = $chart && $chart->post_type === Visualizer_Plugin::CPT_VISUALIZER; | |
| 707 | 991 | } |
| 708 | 992 | } |
| 709 | 993 | $redirect = wp_get_referer(); |
| 710 | 994 | if ( $success ) { |