| @@ -60,11 +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 | + | |
| 70 | + $this->_addAjaxAction( Visualizer_Plugin::ACTION_SAVE_FILTER_QUERY, 'saveFilter' ); | |
| 71 | + | |
| 64 | 72 | } |
| 65 | 73 | |
| 66 | 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 | + /** | |
| 67 | 215 | * Fetches charts from database. |
| 68 | 216 | * |
| 69 | 217 | * This method is also called from the media pop-up (classic editor: create a post and add chart from insert content). |
| 70 | 218 | * |
| @@ -93,9 +241,9 @@ | ||
| 93 | 241 | // 'filter' is from the modal from the add media button. |
| 94 | 242 | $filter = filter_input( INPUT_GET, 'filter', FILTER_SANITIZE_STRING ); |
| 95 | 243 | } |
| 96 | 244 | |
| 97 | - if ( $filter && in_array( $filter, Visualizer_Plugin::getChartTypes() ) ) { | |
| 245 | + if ( $filter && in_array( $filter, Visualizer_Plugin::getChartTypes(), true ) ) { | |
| 98 | 246 | $query_args['meta_query'] = array( |
| 99 | 247 | array( |
| 100 | 248 | 'key' => Visualizer_Plugin::CF_CHART_TYPE, |
| 101 | 249 | 'value' => $filter, |
| @@ -162,8 +310,10 @@ | ||
| 162 | 310 | $css = $arguments[0]; |
| 163 | 311 | $settings = $arguments[1]; |
| 164 | 312 | } |
| 165 | 313 | |
| 314 | + $date_formats = Visualizer_Source::get_date_formats_if_exists( $series, $data ); | |
| 315 | + | |
| 166 | 316 | return array( |
| 167 | 317 | 'type' => $type, |
| 168 | 318 | 'series' => $series, |
| 169 | 319 | 'settings' => $settings, |
| @@ -169,8 +319,9 @@ | ||
| 169 | 319 | 'settings' => $settings, |
| 170 | 320 | 'data' => $data, |
| 171 | 321 | 'library' => $library, |
| 172 | 322 | 'css' => $css, |
| 323 | + 'date_formats' => $date_formats, | |
| 173 | 324 | ); |
| 174 | 325 | } |
| 175 | 326 | |
| 176 | 327 | /** |
| @@ -197,9 +348,9 @@ | ||
| 197 | 348 | * |
| 198 | 349 | * @access public |
| 199 | 350 | */ |
| 200 | 351 | public function deleteChart() { |
| 201 | - $is_post = $_SERVER['REQUEST_METHOD'] == 'POST'; | |
| 352 | + $is_post = $_SERVER['REQUEST_METHOD'] === 'POST'; | |
| 202 | 353 | $input_method = $is_post ? INPUT_POST : INPUT_GET; |
| 203 | 354 | $chart_id = $success = false; |
| 204 | 355 | $nonce = wp_verify_nonce( filter_input( $input_method, 'nonce' ) ); |
| 205 | 356 | $capable = current_user_can( 'delete_posts' ); |
| @@ -215,9 +366,9 @@ | ||
| 215 | 366 | ) |
| 216 | 367 | ); |
| 217 | 368 | if ( $chart_id ) { |
| 218 | 369 | $chart = get_post( $chart_id ); |
| 219 | - $success = $chart && $chart->post_type == Visualizer_Plugin::CPT_VISUALIZER; | |
| 370 | + $success = $chart && $chart->post_type === Visualizer_Plugin::CPT_VISUALIZER; | |
| 220 | 371 | } |
| 221 | 372 | } |
| 222 | 373 | if ( $success ) { |
| 223 | 374 | wp_delete_post( $chart_id, true ); |
| @@ -233,8 +384,36 @@ | ||
| 233 | 384 | exit; |
| 234 | 385 | } |
| 235 | 386 | |
| 236 | 387 | /** |
| 388 | + * Delete charts that are still in auto-draft mode. | |
| 389 | + */ | |
| 390 | + private function deleteOldCharts() { | |
| 391 | + $query = new WP_Query( | |
| 392 | + array( | |
| 393 | + 'post_type' => Visualizer_Plugin::CPT_VISUALIZER, | |
| 394 | + 'post_status' => 'auto-draft', | |
| 395 | + 'fields' => 'ids', | |
| 396 | + 'update_post_meta_cache' => false, | |
| 397 | + 'update_post_term_cache' => false, | |
| 398 | + 'posts_per_page' => 50, | |
| 399 | + 'date_query' => array( | |
| 400 | + array( | |
| 401 | + 'before' => 'today', | |
| 402 | + ), | |
| 403 | + ), | |
| 404 | + ) | |
| 405 | + ); | |
| 406 | + | |
| 407 | + if ( $query->have_posts() ) { | |
| 408 | + $ids = array(); | |
| 409 | + while ( $query->have_posts() ) { | |
| 410 | + wp_delete_post( $query->next_post(), true ); | |
| 411 | + } | |
| 412 | + } | |
| 413 | + } | |
| 414 | + | |
| 415 | + /** | |
| 237 | 416 | * Renders appropriate page for chart builder. Creates new auto draft chart |
| 238 | 417 | * if no chart has been specified. |
| 239 | 418 | * |
| 240 | 419 | * @since 1.0.0 |
| @@ -244,9 +423,10 @@ | ||
| 244 | 423 | public function renderChartPages() { |
| 245 | 424 | defined( 'IFRAME_REQUEST' ) || define( 'IFRAME_REQUEST', 1 ); |
| 246 | 425 | // check chart, if chart not exists, will create new one and redirects to the same page with proper chart id |
| 247 | 426 | $chart_id = isset( $_GET['chart'] ) ? filter_var( $_GET['chart'], FILTER_VALIDATE_INT ) : ''; |
| 248 | - 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 ) { | |
| 428 | + $this->deleteOldCharts(); | |
| 249 | 429 | $default_type = isset( $_GET['type'] ) && ! empty( $_GET['type'] ) ? $_GET['type'] : 'line'; |
| 250 | 430 | $source = new Visualizer_Source_Csv( VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples' . DIRECTORY_SEPARATOR . $default_type . '.csv' ); |
| 251 | 431 | $source->fetch(); |
| 252 | 432 | $chart_id = wp_insert_post( |
| @@ -282,9 +462,9 @@ | ||
| 282 | 462 | wp_register_script( 'visualizer-chosen', VISUALIZER_ABSURL . 'js/lib/chosen.jquery.min.js', array( 'jquery' ), Visualizer_Plugin::VERSION ); |
| 283 | 463 | wp_register_style( 'visualizer-chosen', VISUALIZER_ABSURL . 'css/lib/chosen.min.css', array(), Visualizer_Plugin::VERSION ); |
| 284 | 464 | |
| 285 | 465 | wp_register_style( 'visualizer-frame', VISUALIZER_ABSURL . 'css/frame.css', array( 'visualizer-chosen' ), Visualizer_Plugin::VERSION ); |
| 286 | - 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 ); | |
| 287 | 467 | wp_register_script( 'visualizer-customization', $this->get_user_customization_js(), array(), null, true ); |
| 288 | 468 | wp_register_script( |
| 289 | 469 | 'visualizer-render', |
| 290 | 470 | VISUALIZER_ABSURL . 'js/render-facade.js', |
| @@ -301,13 +481,16 @@ | ||
| 301 | 481 | ), |
| 302 | 482 | Visualizer_Plugin::VERSION, |
| 303 | 483 | true |
| 304 | 484 | ); |
| 485 | + wp_register_script( 'visualizer-editor-simple', VISUALIZER_ABSURL . 'js/simple-editor.js', array( 'jquery' ), Visualizer_Plugin::VERSION, true ); | |
| 486 | + | |
| 305 | 487 | // added by Ash/Upwork |
| 306 | 488 | if ( VISUALIZER_PRO ) { |
| 307 | 489 | global $Visualizer_Pro; |
| 308 | 490 | $Visualizer_Pro->_addScriptsAndStyles(); |
| 309 | 491 | } |
| 492 | + | |
| 310 | 493 | // dispatch pages |
| 311 | 494 | $this->_chart = get_post( $chart_id ); |
| 312 | 495 | $tab = isset( $_GET['tab'] ) && ! empty( $_GET['tab'] ) ? $_GET['tab'] : 'visualizer'; |
| 313 | 496 | |
| @@ -347,8 +530,17 @@ | ||
| 347 | 530 | */ |
| 348 | 531 | private function loadCodeEditorAssets() { |
| 349 | 532 | global $wp_version; |
| 350 | 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 | + | |
| 351 | 543 | if ( ! VISUALIZER_PRO ) { |
| 352 | 544 | return; |
| 353 | 545 | } |
| 354 | 546 | |
| @@ -353,16 +545,8 @@ | ||
| 353 | 545 | } |
| 354 | 546 | |
| 355 | 547 | $table_col_mapping = Visualizer_Source_Query_Params::get_all_db_tables_column_mapping(); |
| 356 | 548 | |
| 357 | - // data tables assets. | |
| 358 | - wp_register_script( 'visualizer-datatables', '//cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js', array( 'jquery-ui-core' ), Visualizer_Plugin::VERSION ); | |
| 359 | - wp_register_style( 'visualizer-datatables', '//cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css', array(), Visualizer_Plugin::VERSION ); | |
| 360 | - wp_register_style( 'visualizer-datatables-ui', '//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css', array( 'visualizer-datatables' ), Visualizer_Plugin::VERSION ); | |
| 361 | - | |
| 362 | - wp_enqueue_script( 'visualizer-datatables' ); | |
| 363 | - wp_enqueue_style( 'visualizer-datatables-ui' ); | |
| 364 | - | |
| 365 | 549 | if ( version_compare( $wp_version, '4.9.0', '<' ) ) { |
| 366 | 550 | // code mirror assets. |
| 367 | 551 | wp_register_script( 'visualizer-codemirror-core', '//codemirror.net/lib/codemirror.js', array( 'jquery' ), Visualizer_Plugin::VERSION ); |
| 368 | 552 | wp_register_script( 'visualizer-codemirror-placeholder', '//codemirror.net/addon/display/placeholder.js', array( 'visualizer-codemirror-core' ), Visualizer_Plugin::VERSION ); |
| @@ -402,10 +586,10 @@ | ||
| 402 | 586 | private function _handleDataAndSettingsPage() { |
| 403 | 587 | if ( isset( $_POST['map_api_key'] ) ) { |
| 404 | 588 | update_option( 'visualizer-map-api-key', $_POST['map_api_key'] ); |
| 405 | 589 | } |
| 406 | - if ( $_SERVER['REQUEST_METHOD'] == 'POST' && isset( $_GET['nonce'] ) && wp_verify_nonce( $_GET['nonce'] ) ) { | |
| 407 | - 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' ) { | |
| 408 | 592 | $this->_chart->post_status = 'publish'; |
| 409 | 593 | |
| 410 | 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. |
| 411 | 595 | // we do not want any difference in data so disable revisions temporarily. |
| @@ -430,9 +614,9 @@ | ||
| 430 | 614 | $sidebar->__series = $data['series']; |
| 431 | 615 | $sidebar->__data = $data['data']; |
| 432 | 616 | } else { |
| 433 | 617 | $sidebar = apply_filters( 'visualizer_pro_chart_type_sidebar', '', $data ); |
| 434 | - if ( $sidebar != '' ) { | |
| 618 | + if ( $sidebar !== '' ) { | |
| 435 | 619 | $sidebar->__series = $data['series']; |
| 436 | 620 | $sidebar->__data = $data['data']; |
| 437 | 621 | } |
| 438 | 622 | } |
| @@ -442,8 +626,25 @@ | ||
| 442 | 626 | wp_enqueue_script( 'visualizer-preview' ); |
| 443 | 627 | wp_enqueue_script( 'visualizer-chosen' ); |
| 444 | 628 | wp_enqueue_script( 'visualizer-render' ); |
| 445 | 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 | + | |
| 446 | 647 | $table_col_mapping = $this->loadCodeEditorAssets(); |
| 447 | 648 | |
| 448 | 649 | wp_localize_script( |
| 449 | 650 | 'visualizer-render', |
| @@ -451,8 +652,9 @@ | ||
| 451 | 652 | array( |
| 452 | 653 | 'l10n' => array( |
| 453 | 654 | 'invalid_source' => esc_html__( 'You have entered invalid URL. Please, insert proper URL.', 'visualizer' ), |
| 454 | 655 | 'loading' => esc_html__( 'Loading...', 'visualizer' ), |
| 656 | + 'json_error' => esc_html__( 'An error occured in fetching data.', 'visualizer' ), | |
| 455 | 657 | ), |
| 456 | 658 | 'charts' => array( |
| 457 | 659 | 'canvas' => $data, |
| 458 | 660 | 'id' => $this->_chart->ID, |
| @@ -463,12 +665,18 @@ | ||
| 463 | 665 | 'url' => admin_url( 'admin-ajax.php' ), |
| 464 | 666 | 'nonces' => array( |
| 465 | 667 | 'permissions' => wp_create_nonce( Visualizer_Plugin::ACTION_FETCH_PERMISSIONS_DATA ), |
| 466 | 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 ), | |
| 467 | 672 | ), |
| 468 | 673 | 'actions' => array( |
| 469 | 674 | 'permissions' => Visualizer_Plugin::ACTION_FETCH_PERMISSIONS_DATA, |
| 470 | 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, | |
| 471 | 679 | ), |
| 472 | 680 | ), |
| 473 | 681 | 'db_query' => array( |
| 474 | 682 | 'tables' => $table_col_mapping, |
| @@ -474,8 +682,11 @@ | ||
| 474 | 682 | 'tables' => $table_col_mapping, |
| 475 | 683 | ), |
| 476 | 684 | 'is_pro' => VISUALIZER_PRO, |
| 477 | 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, | |
| 478 | 689 | ) |
| 479 | 690 | ); |
| 480 | 691 | |
| 481 | 692 | $render = new Visualizer_Render_Page_Data(); |
| @@ -483,12 +694,12 @@ | ||
| 483 | 694 | $render->type = $data['type']; |
| 484 | 695 | $render->custom_css = $data['css']; |
| 485 | 696 | $render->sidebar = $sidebar; |
| 486 | 697 | if ( filter_input( INPUT_GET, 'library', FILTER_VALIDATE_BOOLEAN ) ) { |
| 487 | - $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 | |
| 488 | 699 | ? esc_html__( 'Save Chart', 'visualizer' ) |
| 489 | 700 | : esc_html__( 'Create Chart', 'visualizer' ); |
| 490 | - if ( filter_input( INPUT_GET, 'action' ) == Visualizer_Plugin::ACTION_EDIT_CHART ) { | |
| 701 | + if ( filter_input( INPUT_GET, 'action' ) === Visualizer_Plugin::ACTION_EDIT_CHART ) { | |
| 491 | 702 | $render->cancel_button = esc_html__( 'Cancel', 'visualizer' ); |
| 492 | 703 | } |
| 493 | 704 | } else { |
| 494 | 705 | $render->button = esc_attr__( 'Insert Chart', 'visualizer' ); |
| @@ -509,11 +720,11 @@ | ||
| 509 | 720 | * @access private |
| 510 | 721 | */ |
| 511 | 722 | private function _handleTypesPage() { |
| 512 | 723 | // process post request |
| 513 | - 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' ) ) ) { | |
| 514 | 725 | $type = filter_input( INPUT_POST, 'type' ); |
| 515 | - if ( in_array( $type, Visualizer_Plugin::getChartTypes() ) ) { | |
| 726 | + if ( in_array( $type, Visualizer_Plugin::getChartTypes(), true ) ) { | |
| 516 | 727 | // save new chart type |
| 517 | 728 | update_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_CHART_TYPE, $type ); |
| 518 | 729 | // if the chart has default data, update it with appropriate default data for new type |
| 519 | 730 | if ( filter_var( get_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_DEFAULT_DATA, true ), FILTER_VALIDATE_BOOLEAN ) ) { |
| @@ -549,10 +760,108 @@ | ||
| 549 | 760 | */ |
| 550 | 761 | public function renderFlattrScript() { |
| 551 | 762 | echo ''; |
| 552 | 763 | } |
| 553 | - // changed by Ash/Upwork | |
| 764 | + | |
| 554 | 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 | + /** | |
| 555 | 864 | * Parses uploaded CSV file and saves new data for the chart. |
| 556 | 865 | * |
| 557 | 866 | * @since 1.0.0 |
| 558 | 867 | * |
| @@ -574,9 +883,9 @@ | ||
| 574 | 883 | |
| 575 | 884 | // check chart, if chart exists |
| 576 | 885 | // do not use filter_input as it does not work for phpunit test cases, use filter_var instead |
| 577 | 886 | $chart_id = isset( $_GET['chart'] ) ? filter_var( $_GET['chart'], FILTER_VALIDATE_INT ) : ''; |
| 578 | - 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 ) { | |
| 579 | 888 | if ( ! $can_die ) { |
| 580 | 889 | return; |
| 581 | 890 | } |
| 582 | 891 | status_header( 400 ); |
| @@ -588,9 +897,11 @@ | ||
| 588 | 897 | } |
| 589 | 898 | |
| 590 | 899 | if ( ! isset( $_POST['chart_data_src'] ) || Visualizer_Plugin::CF_SOURCE_FILTER !== $_POST['chart_data_src'] ) { |
| 591 | 900 | // delete the filters in case this chart is being uploaded from other data sources |
| 592 | - delete_post_meta( $chart_id, 'visualizer-filter-config' ); | |
| 901 | + delete_post_meta( $chart_id, Visualizer_Plugin::CF_FILTER_CONFIG ); | |
| 902 | + delete_post_meta( $chart_id, '__transient-' . Visualizer_Plugin::CF_FILTER_CONFIG ); | |
| 903 | + delete_post_meta( $chart_id, '__transient-' . Visualizer_Plugin::CF_DB_QUERY ); | |
| 593 | 904 | |
| 594 | 905 | // delete "import from db" specific parameters. |
| 595 | 906 | delete_post_meta( $chart_id, Visualizer_Plugin::CF_DB_QUERY ); |
| 596 | 907 | delete_post_meta( $chart_id, Visualizer_Plugin::CF_DB_SCHEDULE ); |
| @@ -595,8 +906,13 @@ | ||
| 595 | 906 | delete_post_meta( $chart_id, Visualizer_Plugin::CF_DB_QUERY ); |
| 596 | 907 | delete_post_meta( $chart_id, Visualizer_Plugin::CF_DB_SCHEDULE ); |
| 597 | 908 | } |
| 598 | 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 | + | |
| 599 | 915 | $source = null; |
| 600 | 916 | $render = new Visualizer_Render_Page_Update(); |
| 601 | 917 | if ( isset( $_POST['remote_data'] ) && filter_var( $_POST['remote_data'], FILTER_VALIDATE_URL ) ) { |
| 602 | 918 | $source = new Visualizer_Source_Csv_Remote( $_POST['remote_data'] ); |
| @@ -602,12 +918,15 @@ | ||
| 602 | 918 | $source = new Visualizer_Source_Csv_Remote( $_POST['remote_data'] ); |
| 603 | 919 | if ( isset( $_POST['vz-import-time'] ) ) { |
| 604 | 920 | apply_filters( 'visualizer_pro_chart_schedule', $chart_id, $_POST['remote_data'], $_POST['vz-import-time'] ); |
| 605 | 921 | } |
| 922 | + // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison | |
| 606 | 923 | } elseif ( isset( $_FILES['local_data'] ) && $_FILES['local_data']['error'] == 0 ) { |
| 607 | 924 | $source = new Visualizer_Source_Csv( $_FILES['local_data']['tmp_name'] ); |
| 608 | 925 | } elseif ( isset( $_POST['chart_data'] ) && strlen( $_POST['chart_data'] ) > 0 ) { |
| 609 | - $source = apply_filters( 'visualizer_pro_handle_chart_data', $_POST['chart_data'], '' ); | |
| 926 | + $source = $this->handleCSVasString( $_POST['chart_data'] ); | |
| 927 | + } elseif ( isset( $_POST['table_data'] ) && 'yes' === $_POST['table_data'] ) { | |
| 928 | + $source = $this->handleTabularData(); | |
| 610 | 929 | } else { |
| 611 | 930 | $render->message = esc_html__( 'CSV file with chart data was not uploaded. Please, try again.', 'visualizer' ); |
| 612 | 931 | } |
| 613 | 932 | if ( $source ) { |
| @@ -667,9 +986,9 @@ | ||
| 667 | 986 | ) |
| 668 | 987 | ); |
| 669 | 988 | if ( $chart_id ) { |
| 670 | 989 | $chart = get_post( $chart_id ); |
| 671 | - $success = $chart && $chart->post_type == Visualizer_Plugin::CPT_VISUALIZER; | |
| 990 | + $success = $chart && $chart->post_type === Visualizer_Plugin::CPT_VISUALIZER; | |
| 672 | 991 | } |
| 673 | 992 | } |
| 674 | 993 | $redirect = wp_get_referer(); |
| 675 | 994 | if ( $success ) { |
| @@ -838,8 +1157,37 @@ | ||
| 838 | 1157 | $render->message = $error; |
| 839 | 1158 | } |
| 840 | 1159 | } |
| 841 | 1160 | $render->render(); |
| 1161 | + if ( ! ( defined( 'VISUALIZER_DO_NOT_DIE' ) && VISUALIZER_DO_NOT_DIE ) ) { | |
| 1162 | + defined( 'WP_TESTS_DOMAIN' ) ? wp_die() : exit(); | |
| 1163 | + } | |
| 1164 | + } | |
| 1165 | + | |
| 1166 | + | |
| 1167 | + /** | |
| 1168 | + * Saves the filter query and the schedule. | |
| 1169 | + * | |
| 1170 | + * @access public | |
| 1171 | + */ | |
| 1172 | + public function saveFilter() { | |
| 1173 | + check_ajax_referer( Visualizer_Plugin::ACTION_SAVE_FILTER_QUERY . Visualizer_Plugin::VERSION, 'security' ); | |
| 1174 | + | |
| 1175 | + $chart_id = filter_input( | |
| 1176 | + INPUT_GET, | |
| 1177 | + 'chart', | |
| 1178 | + FILTER_VALIDATE_INT, | |
| 1179 | + array( | |
| 1180 | + 'options' => array( | |
| 1181 | + 'min_range' => 1, | |
| 1182 | + ), | |
| 1183 | + ) | |
| 1184 | + ); | |
| 1185 | + | |
| 1186 | + $hours = $_POST['refresh']; | |
| 1187 | + | |
| 1188 | + do_action( 'visualizer_save_filter', $chart_id, $hours ); | |
| 1189 | + | |
| 842 | 1190 | if ( ! ( defined( 'VISUALIZER_DO_NOT_DIE' ) && VISUALIZER_DO_NOT_DIE ) ) { |
| 843 | 1191 | defined( 'WP_TESTS_DOMAIN' ) ? wp_die() : exit(); |
| 844 | 1192 | } |
| 845 | 1193 | } |