| @@ -66,9 +66,9 @@ | ||
| 66 | 66 | /** |
| 67 | 67 | * Enqueue front end and editor JavaScript and CSS |
| 68 | 68 | */ |
| 69 | 69 | public function enqueue_gutenberg_scripts() { |
| 70 | - global $wp_version, $pagenow; | |
| 70 | + global $wp_version; | |
| 71 | 71 | |
| 72 | 72 | $blockPath = VISUALIZER_ABSURL . 'classes/Visualizer/Gutenberg/build/block.js'; |
| 73 | 73 | $handsontableJS = VISUALIZER_ABSURL . 'classes/Visualizer/Gutenberg/build/handsontable.js'; |
| 74 | 74 | $stylePath = VISUALIZER_ABSURL . 'classes/Visualizer/Gutenberg/build/block.css'; |
| @@ -79,19 +79,11 @@ | ||
| 79 | 79 | } else { |
| 80 | 80 | $version = $this->version; |
| 81 | 81 | } |
| 82 | 82 | |
| 83 | - if ( ! wp_script_is( 'visualizer-datatables', 'registered' ) ) { | |
| 84 | - wp_register_script( 'visualizer-datatables', VISUALIZER_ABSURL . 'js/lib/datatables.min.js', array( 'jquery-ui-core' ), Visualizer_Plugin::VERSION ); | |
| 85 | - } | |
| 86 | - | |
| 87 | - if ( ! wp_style_is( 'visualizer-datatables', 'registered' ) ) { | |
| 88 | - wp_register_style( 'visualizer-datatables', VISUALIZER_ABSURL . 'css/lib/datatables.min.css', array(), Visualizer_Plugin::VERSION ); | |
| 89 | - } | |
| 90 | - | |
| 91 | 83 | // Enqueue the bundled block JS file |
| 92 | 84 | wp_enqueue_script( 'handsontable', $handsontableJS ); |
| 93 | - wp_enqueue_script( 'visualizer-gutenberg-block', $blockPath, array( 'wp-api', 'handsontable', 'visualizer-datatables', 'moment', 'lodash' ), $version, true ); | |
| 85 | + wp_enqueue_script( 'visualizer-gutenberg-block', $blockPath, array( 'wp-api', 'handsontable', 'visualizer-datatables', 'moment' ), $version, true ); | |
| 94 | 86 | |
| 95 | 87 | $type = 'community'; |
| 96 | 88 | |
| 97 | 89 | if ( Visualizer_Module::is_pro() ) { |
| @@ -104,16 +96,14 @@ | ||
| 104 | 96 | $table_col_mapping = Visualizer_Source_Query_Params::get_all_db_tables_column_mapping( null, false ); |
| 105 | 97 | |
| 106 | 98 | $translation_array = array( |
| 107 | 99 | 'isPro' => $type, |
| 108 | - 'proTeaser' => tsdk_utmify( Visualizer_Plugin::PRO_TEASER_URL, 'blockupsell'), | |
| 100 | + 'proTeaser' => Visualizer_Plugin::PRO_TEASER_URL, | |
| 109 | 101 | 'absurl' => VISUALIZER_ABSURL, |
| 110 | 102 | 'charts' => Visualizer_Module_Admin::_getChartTypesLocalized(), |
| 111 | 103 | 'adminPage' => menu_page_url( 'visualizer', false ), |
| 112 | 104 | 'sqlTable' => $table_col_mapping, |
| 113 | 105 | 'chartsPerPage' => defined( 'TI_CYPRESS_TESTING' ) ? 20 : 6, |
| 114 | - 'proFeaturesLocked' => Visualizer_Module_Admin::proFeaturesLocked(), | |
| 115 | - 'isFullSiteEditor' => 'site-editor.php' === $pagenow, | |
| 116 | 106 | ); |
| 117 | 107 | wp_localize_script( 'visualizer-gutenberg-block', 'visualizerLocalize', $translation_array ); |
| 118 | 108 | |
| 119 | 109 | // Enqueue frontend and editor block styles |
| @@ -148,11 +138,8 @@ | ||
| 148 | 138 | 'attributes' => array( |
| 149 | 139 | 'id' => array( |
| 150 | 140 | 'type' => 'number', |
| 151 | 141 | ), |
| 152 | - 'lazy' => array( | |
| 153 | - 'type' => 'string', | |
| 154 | - ), | |
| 155 | 142 | ), |
| 156 | 143 | ) |
| 157 | 144 | ); |
| 158 | 145 | } |
| @@ -159,46 +146,16 @@ | ||
| 159 | 146 | |
| 160 | 147 | /** |
| 161 | 148 | * Gutenberg Block Callback Function |
| 162 | 149 | */ |
| 163 | - public function gutenberg_block_callback( $atts ) { | |
| 164 | - // no id, no fun. | |
| 165 | - if ( ! isset( $atts['id'] ) ) { | |
| 166 | - return ''; | |
| 150 | + public function gutenberg_block_callback( $attr ) { | |
| 151 | + if ( isset( $attr['id'] ) ) { | |
| 152 | + $id = $attr['id']; | |
| 153 | + if ( empty( $id ) || $id === 'none' ) { | |
| 154 | + return ''; // no id = no fun | |
| 155 | + } | |
| 156 | + return '[visualizer id="' . $id . '"]'; | |
| 167 | 157 | } |
| 168 | - | |
| 169 | - $atts = shortcode_atts( | |
| 170 | - array( | |
| 171 | - 'id' => false, | |
| 172 | - 'lazy' => apply_filters( 'visualizer_lazy_by_default', false, $atts['id'] ), | |
| 173 | - // we are deliberating excluding the class attribute from here | |
| 174 | - // as this will be handled by the custom class in Gutenberg | |
| 175 | - ), | |
| 176 | - $atts | |
| 177 | - ); | |
| 178 | - | |
| 179 | - // no id, no fun. | |
| 180 | - if ( ! $atts['id'] ) { | |
| 181 | - return ''; | |
| 182 | - } | |
| 183 | - | |
| 184 | - // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison | |
| 185 | - if ( $atts['lazy'] == -1 || $atts['lazy'] == false ) { | |
| 186 | - $atts['lazy'] = 'no'; | |
| 187 | - } | |
| 188 | - | |
| 189 | - // we don't want the chart in the editor lazy-loading. | |
| 190 | - if ( is_admin() ) { | |
| 191 | - unset( $atts['lazy'] ); | |
| 192 | - } | |
| 193 | - | |
| 194 | - $shortcode = '[visualizer'; | |
| 195 | - foreach ( $atts as $name => $value ) { | |
| 196 | - $shortcode .= sprintf( ' %s="%s"', $name, $value ); | |
| 197 | - } | |
| 198 | - $shortcode .= ']'; | |
| 199 | - | |
| 200 | - return $shortcode; | |
| 201 | 158 | } |
| 202 | 159 | |
| 203 | 160 | /** |
| 204 | 161 | * Hook server side rendering into render callback |
| @@ -351,11 +308,9 @@ | ||
| 351 | 308 | $data['visualizer-default-data'] = get_post_meta( $post_id, Visualizer_Plugin::CF_DEFAULT_DATA, true ); |
| 352 | 309 | |
| 353 | 310 | // faetch and update settings |
| 354 | 311 | $data['visualizer-settings'] = get_post_meta( $post_id, Visualizer_Plugin::CF_SETTINGS, true ); |
| 355 | - if ( empty( $data['visualizer-settings']['pagination'] ) ) { | |
| 356 | - $data['visualizer-settings']['pageSize'] = ''; | |
| 357 | - } | |
| 312 | + | |
| 358 | 313 | // handle series filter hooks |
| 359 | 314 | $data['visualizer-series'] = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SERIES, get_post_meta( $post_id, Visualizer_Plugin::CF_SERIES, true ), $post_id, $data['visualizer-chart-type'] ); |
| 360 | 315 | |
| 361 | 316 | // handle settings filter hooks |
| @@ -363,18 +318,8 @@ | ||
| 363 | 318 | |
| 364 | 319 | // handle data filter hooks |
| 365 | 320 | $data['visualizer-data'] = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA, unserialize( html_entity_decode( get_the_content( $post_id ) ) ), $post_id, $data['visualizer-chart-type'] ); |
| 366 | 321 | |
| 367 | - // we are going to format only for tabular charts, because we are not sure of the effect on others. | |
| 368 | - // this is to solve the case where boolean data shows up as all-ticks on gutenberg. | |
| 369 | - if ( in_array( $data['visualizer-chart-type'], array( 'tabular' ), true ) ) { | |
| 370 | - $data['visualizer-data'] = $this->format_chart_data( $data['visualizer-data'], $data['visualizer-series'] ); | |
| 371 | - } | |
| 372 | - | |
| 373 | - if ( ! isset( $data['visualizer-settings']['hAxis']['format'] ) ) { | |
| 374 | - $data['visualizer-settings']['hAxis']['format'] = ''; | |
| 375 | - } | |
| 376 | - | |
| 377 | 322 | $data['visualizer-data-exploded'] = ''; |
| 378 | 323 | // handle annotations for google charts |
| 379 | 324 | if ( 'GoogleCharts' === $library ) { |
| 380 | 325 | // this will contain the data of both axis. |
| @@ -391,12 +336,8 @@ | ||
| 391 | 336 | if ( ! empty( $serie['role'] ) ) { |
| 392 | 337 | // this series is some kind of annotation, so let's collect its index. |
| 393 | 338 | // the index will be +1 because the X axis value is index 0, which is being ignored. |
| 394 | 339 | $annotations[ 'role' . ( intval( $index ) + 1 ) ] = $serie['role']; |
| 395 | - | |
| 396 | - if ( isset( $data['visualizer-series'][ intval( $index ) + 1 ] ) ) { | |
| 397 | - $data['visualizer-series'][ intval( $index ) + 1 ]['role'] = $serie['role']; | |
| 398 | - } | |
| 399 | 340 | } |
| 400 | 341 | } |
| 401 | 342 | } |
| 402 | 343 | if ( ! empty( $annotations ) ) { |
| @@ -441,10 +382,10 @@ | ||
| 441 | 382 | $json_root = get_post_meta( $post_id, Visualizer_Plugin::CF_JSON_ROOT, true ); |
| 442 | 383 | |
| 443 | 384 | $json_paging = get_post_meta( $post_id, Visualizer_Plugin::CF_JSON_PAGING, true ); |
| 444 | 385 | |
| 445 | - if ( ! empty( $import ) && $schedule >= 0 ) { | |
| 446 | - $data['visualizer-chart-url'] = $import; | |
| 386 | + if ( ! empty( $import ) && ! empty( $schedule ) ) { | |
| 387 | + $data['visualizer-chart-url'] = $import; | |
| 447 | 388 | $data['visualizer-chart-schedule'] = $schedule; |
| 448 | 389 | } |
| 449 | 390 | |
| 450 | 391 | if ( ! empty( $db_schedule ) && ! empty( $db_query ) ) { |
| @@ -465,18 +406,11 @@ | ||
| 465 | 406 | |
| 466 | 407 | if ( Visualizer_Module::is_pro() ) { |
| 467 | 408 | $permissions = get_post_meta( $post_id, Visualizer_PRO::CF_PERMISSIONS, true ); |
| 468 | 409 | |
| 469 | - if ( empty( $permissions ) ) { | |
| 470 | - $permissions = array( 'permissions' => array( | |
| 471 | - 'read' => 'all', | |
| 472 | - 'edit' => 'roles', | |
| 473 | - 'edit-specific' => array( 'administrator' ), | |
| 474 | - ), | |
| 475 | - ); | |
| 410 | + if ( ! empty( $permissions ) ) { | |
| 411 | + $data['visualizer-permissions'] = $permissions; | |
| 476 | 412 | } |
| 477 | - | |
| 478 | - $data['visualizer-permissions'] = $permissions; | |
| 479 | 413 | } |
| 480 | 414 | |
| 481 | 415 | return $data; |
| 482 | 416 | } |
| @@ -597,9 +531,9 @@ | ||
| 597 | 531 | update_post_meta( $data['id'], Visualizer_Plugin::CF_DEFAULT_DATA, $data['visualizer-default-data'] ); |
| 598 | 532 | update_post_meta( $data['id'], Visualizer_Plugin::CF_SERIES, $data['visualizer-series'] ); |
| 599 | 533 | update_post_meta( $data['id'], Visualizer_Plugin::CF_SETTINGS, $data['visualizer-settings'] ); |
| 600 | 534 | |
| 601 | - if ( $data['visualizer-chart-url'] && $data['visualizer-chart-schedule'] >= 0 ) { | |
| 535 | + if ( $data['visualizer-chart-url'] && $data['visualizer-chart-schedule'] ) { | |
| 602 | 536 | $chart_url = esc_url_raw( $data['visualizer-chart-url'] ); |
| 603 | 537 | $chart_schedule = intval( $data['visualizer-chart-schedule'] ); |
| 604 | 538 | update_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_URL, $chart_url ); |
| 605 | 539 | apply_filters( 'visualizer_pro_chart_schedule', $data['id'], $chart_url, $chart_schedule ); |
| @@ -607,31 +541,16 @@ | ||
| 607 | 541 | delete_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_URL ); |
| 608 | 542 | apply_filters( 'visualizer_pro_remove_schedule', $data['id'] ); |
| 609 | 543 | } |
| 610 | 544 | |
| 611 | - // let's check if this is not an external db chart | |
| 612 | - // as there is no support for that in the block editor interface | |
| 613 | - $external_params = get_post_meta( $data['id'], Visualizer_Plugin::CF_REMOTE_DB_PARAMS, true ); | |
| 614 | - if ( empty( $external_params ) ) { | |
| 615 | - if ( $source_type === 'Visualizer_Source_Query' ) { | |
| 616 | - $db_schedule = intval( $data['visualizer-db-schedule'] ); | |
| 617 | - $db_query = $data['visualizer-db-query']; | |
| 618 | - update_post_meta( $data['id'], Visualizer_Plugin::CF_DB_SCHEDULE, $db_schedule ); | |
| 619 | - update_post_meta( $data['id'], Visualizer_Plugin::CF_DB_QUERY, stripslashes( $db_query ) ); | |
| 620 | - } else { | |
| 621 | - delete_post_meta( $data['id'], Visualizer_Plugin::CF_DB_SCHEDULE ); | |
| 622 | - delete_post_meta( $data['id'], Visualizer_Plugin::CF_DB_QUERY ); | |
| 623 | - } | |
| 624 | - | |
| 625 | - if ( 'Visualizer_Source_Csv_Remote' === $source_type ) { | |
| 626 | - $schedule_url = $data['visualizer-chart-url']; | |
| 627 | - $schedule_id = $data['visualizer-chart-schedule']; | |
| 628 | - update_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_URL, $schedule_url ); | |
| 629 | - update_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_SCHEDULE, $schedule_id ); | |
| 630 | - } else { | |
| 631 | - delete_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_URL ); | |
| 632 | - delete_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_SCHEDULE ); | |
| 633 | - } | |
| 545 | + if ( $source_type === 'Visualizer_Source_Query' ) { | |
| 546 | + $db_schedule = intval( $data['visualizer-db-schedule'] ); | |
| 547 | + $db_query = $data['visualizer-db-query']; | |
| 548 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_DB_SCHEDULE, $db_schedule ); | |
| 549 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_DB_QUERY, stripslashes( $db_query ) ); | |
| 550 | + } else { | |
| 551 | + delete_post_meta( $data['id'], Visualizer_Plugin::CF_DB_SCHEDULE ); | |
| 552 | + delete_post_meta( $data['id'], Visualizer_Plugin::CF_DB_QUERY ); | |
| 634 | 553 | } |
| 635 | 554 | |
| 636 | 555 | if ( $source_type === 'Visualizer_Source_Json' ) { |
| 637 | 556 | $json_schedule = intval( $data['visualizer-json-schedule'] ); |
| @@ -676,14 +595,8 @@ | ||
| 676 | 595 | ); |
| 677 | 596 | |
| 678 | 597 | wp_update_post( $chart ); |
| 679 | 598 | |
| 680 | - // Clear existing chart cache. | |
| 681 | - $cache_key = Visualizer_Plugin::CF_CHART_CACHE . '_' . $data['id']; | |
| 682 | - if ( get_transient( $cache_key ) ) { | |
| 683 | - delete_transient( $cache_key ); | |
| 684 | - } | |
| 685 | - | |
| 686 | 599 | $revisions = wp_get_post_revisions( $data['id'], array( 'order' => 'ASC' ) ); |
| 687 | 600 | |
| 688 | 601 | if ( count( $revisions ) > 1 ) { |
| 689 | 602 | $revision_ids = array_keys( $revisions ); |
| @@ -699,10 +612,8 @@ | ||
| 699 | 612 | } |
| 700 | 613 | |
| 701 | 614 | /** |
| 702 | 615 | * Format chart data. |
| 703 | - * | |
| 704 | - * Note: No matter how tempted, don't use the similar method from Visualizer_Source. That works on a different structure. | |
| 705 | 616 | */ |
| 706 | 617 | public function format_chart_data( $data, $series ) { |
| 707 | 618 | foreach ( $series as $i => $row ) { |
| 708 | 619 | // if no value exists for the seires, then add null |
| @@ -713,38 +624,39 @@ | ||
| 713 | 624 | if ( is_null( $series[ $i ] ) ) { |
| 714 | 625 | continue; |
| 715 | 626 | } |
| 716 | 627 | |
| 717 | - switch ( $row['type'] ) { | |
| 718 | - case 'number': | |
| 719 | - foreach ( $data as $o => $col ) { | |
| 720 | - $data[ $o ][ $i ] = ( is_numeric( $col[ $i ] ) ) ? floatval( $col[ $i ] ) : ( is_numeric( str_replace( ',', '', $col[ $i ] ) ) ? floatval( str_replace( ',', '', $col[ $i ] ) ) : null ); | |
| 628 | + if ( $row['type'] === 'number' ) { | |
| 629 | + foreach ( $data as $o => $col ) { | |
| 630 | + $data[ $o ][ $i ] = ( is_numeric( $col[ $i ] ) ) ? floatval( $col[ $i ] ) : ( is_numeric( str_replace( ',', '', $col[ $i ] ) ) ? floatval( str_replace( ',', '', $col[ $i ] ) ) : null ); | |
| 631 | + } | |
| 632 | + } | |
| 633 | + | |
| 634 | + if ( $row['type'] === 'boolean' ) { | |
| 635 | + foreach ( $data as $o => $col ) { | |
| 636 | + $data[ $o ][ $i ] = ! empty( $col[ $i ] ) ? filter_var( $col[ $i ], FILTER_VALIDATE_BOOLEAN ) : null; | |
| 637 | + } | |
| 638 | + } | |
| 639 | + | |
| 640 | + if ( $row['type'] === 'timeofday' ) { | |
| 641 | + foreach ( $data as $o => $col ) { | |
| 642 | + $date = new DateTime( '1984-03-16T' . $col[ $i ] ); | |
| 643 | + if ( $date ) { | |
| 644 | + $data[ $o ][ $i ] = array( | |
| 645 | + intval( $date->format( 'H' ) ), | |
| 646 | + intval( $date->format( 'i' ) ), | |
| 647 | + intval( $date->format( 's' ) ), | |
| 648 | + 0, | |
| 649 | + ); | |
| 721 | 650 | } |
| 722 | - break; | |
| 723 | - case 'boolean': | |
| 724 | - foreach ( $data as $o => $col ) { | |
| 725 | - $data[ $o ][ $i ] = ! empty( $col[ $i ] ) ? filter_var( $col[ $i ], FILTER_VALIDATE_BOOLEAN ) : false; | |
| 726 | - } | |
| 727 | - break; | |
| 728 | - case 'timeofday': | |
| 729 | - foreach ( $data as $o => $col ) { | |
| 730 | - $date = new DateTime( '1984-03-16T' . $col[ $i ] ); | |
| 731 | - if ( $date ) { | |
| 732 | - $data[ $o ][ $i ] = array( | |
| 733 | - intval( $date->format( 'H' ) ), | |
| 734 | - intval( $date->format( 'i' ) ), | |
| 735 | - intval( $date->format( 's' ) ), | |
| 736 | - 0, | |
| 737 | - ); | |
| 738 | - } | |
| 739 | - } | |
| 740 | - break; | |
| 741 | - case 'string': | |
| 742 | - foreach ( $data as $o => $col ) { | |
| 743 | - $data[ $o ][ $i ] = $this->toUTF8( $col[ $i ] ); | |
| 744 | - } | |
| 745 | - break; | |
| 651 | + } | |
| 746 | 652 | } |
| 653 | + | |
| 654 | + if ( $row['type'] === 'string' ) { | |
| 655 | + foreach ( $data as $o => $col ) { | |
| 656 | + $data[ $o ][ $i ] = $this->toUTF8( $col[ $i ] ); | |
| 657 | + } | |
| 658 | + } | |
| 747 | 659 | } |
| 748 | 660 | |
| 749 | 661 | return $data; |
| 750 | 662 | } |
| @@ -766,14 +678,10 @@ | ||
| 766 | 678 | if ( ! current_user_can( 'edit_posts' ) ) { |
| 767 | 679 | return false; |
| 768 | 680 | } |
| 769 | 681 | |
| 770 | - $remote_data = false; | |
| 771 | - if ( isset( $data['url'] ) && function_exists( 'wp_http_validate_url' ) ) { | |
| 772 | - $remote_data = wp_http_validate_url( $data['url'] ); | |
| 773 | - } | |
| 774 | - if ( false !== $remote_data && ! is_wp_error( $remote_data ) ) { | |
| 775 | - $source = new Visualizer_Source_Csv_Remote( $remote_data ); | |
| 682 | + if ( $data['url'] && ! is_wp_error( $data['url'] ) && filter_var( $data['url'], FILTER_VALIDATE_URL ) ) { | |
| 683 | + $source = new Visualizer_Source_Csv_Remote( $data['url'] ); | |
| 776 | 684 | if ( $source->fetch() ) { |
| 777 | 685 | $temp = $source->getData(); |
| 778 | 686 | if ( is_string( $temp ) && is_array( unserialize( $temp ) ) ) { |
| 779 | 687 | $content['series'] = $source->getSeries(); |