| @@ -63,28 +63,22 @@ | ||
| 63 | 63 | add_filter( 'rest_visualizer_query', array( $this, 'add_rest_query_vars' ), 9, 2 ); |
| 64 | 64 | } |
| 65 | 65 | |
| 66 | 66 | /** |
| 67 | - * Enqueue Gutenberg block assets. | |
| 67 | + * Enqueue front end and editor JavaScript and CSS | |
| 68 | 68 | */ |
| 69 | 69 | public function enqueue_gutenberg_scripts() { |
| 70 | - global $pagenow; | |
| 70 | + global $wp_version, $pagenow; | |
| 71 | 71 | |
| 72 | - $blockPath = VISUALIZER_ABSURL . 'classes/Visualizer/Gutenberg/build/index.js'; | |
| 73 | - $stylePath = VISUALIZER_ABSURL . 'classes/Visualizer/Gutenberg/build/style-index.css'; | |
| 74 | - $asset_path = VISUALIZER_ABSPATH . '/classes/Visualizer/Gutenberg/build/index.asset.php'; | |
| 75 | - if ( file_exists( $asset_path ) ) { | |
| 76 | - // @phpstan-ignore-next-line | |
| 77 | - $asset = require $asset_path; | |
| 78 | - } else { | |
| 79 | - $asset = array( | |
| 80 | - 'dependencies' => array(), | |
| 81 | - 'version' => $this->version, | |
| 82 | - ); | |
| 83 | - } | |
| 72 | + $blockPath = VISUALIZER_ABSURL . 'classes/Visualizer/Gutenberg/build/block.js'; | |
| 73 | + $handsontableJS = VISUALIZER_ABSURL . 'classes/Visualizer/Gutenberg/build/handsontable.js'; | |
| 74 | + $stylePath = VISUALIZER_ABSURL . 'classes/Visualizer/Gutenberg/build/block.css'; | |
| 75 | + $handsontableCSS = VISUALIZER_ABSURL . 'classes/Visualizer/Gutenberg/build/handsontable.css'; | |
| 84 | 76 | |
| 85 | 77 | if ( VISUALIZER_TEST_JS_CUSTOMIZATION ) { |
| 86 | - $asset['version'] = filemtime( VISUALIZER_ABSPATH . '/classes/Visualizer/Gutenberg/build/index.js' ); | |
| 78 | + $version = filemtime( VISUALIZER_ABSPATH . '/classes/Visualizer/Gutenberg/build/block.js' ); | |
| 79 | + } else { | |
| 80 | + $version = $this->version; | |
| 87 | 81 | } |
| 88 | 82 | |
| 89 | 83 | if ( ! wp_script_is( 'visualizer-datatables', 'registered' ) ) { |
| 90 | 84 | wp_register_script( 'visualizer-datatables', VISUALIZER_ABSURL . 'js/lib/datatables.min.js', array( 'jquery-ui-core' ), Visualizer_Plugin::VERSION ); |
| @@ -94,109 +88,60 @@ | ||
| 94 | 88 | wp_register_style( 'visualizer-datatables', VISUALIZER_ABSURL . 'css/lib/datatables.min.css', array(), Visualizer_Plugin::VERSION ); |
| 95 | 89 | } |
| 96 | 90 | |
| 97 | 91 | // Enqueue the bundled block JS file |
| 98 | - $script_deps = array( | |
| 99 | - 'wp-api', | |
| 100 | - 'wp-blocks', | |
| 101 | - 'wp-block-editor', | |
| 102 | - 'wp-components', | |
| 103 | - 'wp-editor', | |
| 104 | - 'wp-element', | |
| 105 | - 'wp-i18n', | |
| 106 | - 'lodash', | |
| 107 | - 'moment', | |
| 108 | - 'react', | |
| 109 | - 'visualizer-datatables', | |
| 110 | - ); | |
| 111 | - if ( isset( $asset['dependencies'] ) && is_array( $asset['dependencies'] ) ) { | |
| 112 | - $script_deps = array_merge( $script_deps, $asset['dependencies'] ); | |
| 92 | + wp_enqueue_script( 'handsontable', $handsontableJS ); | |
| 93 | + wp_enqueue_script( 'visualizer-gutenberg-block', $blockPath, array( 'wp-api', 'handsontable', 'visualizer-datatables', 'moment', 'lodash' ), $version, true ); | |
| 94 | + | |
| 95 | + $type = 'community'; | |
| 96 | + | |
| 97 | + if ( Visualizer_Module::is_pro() ) { | |
| 98 | + $type = 'pro'; | |
| 99 | + if ( apply_filters( 'visualizer_is_business', false ) ) { | |
| 100 | + $type = 'business'; | |
| 101 | + } | |
| 113 | 102 | } |
| 114 | - $script_deps = array_values( array_unique( $script_deps ) ); | |
| 115 | - wp_enqueue_script( 'visualizer-gutenberg-block', $blockPath, $script_deps, $asset['version'], true ); | |
| 116 | 103 | |
| 104 | + $table_col_mapping = Visualizer_Source_Query_Params::get_all_db_tables_column_mapping( null, false ); | |
| 105 | + | |
| 117 | 106 | $translation_array = array( |
| 107 | + 'isPro' => $type, | |
| 108 | + 'proTeaser' => tsdk_utmify( Visualizer_Plugin::PRO_TEASER_URL, 'blockupsell'), | |
| 109 | + 'absurl' => VISUALIZER_ABSURL, | |
| 110 | + 'charts' => Visualizer_Module_Admin::_getChartTypesLocalized(), | |
| 118 | 111 | 'adminPage' => menu_page_url( 'visualizer', false ), |
| 119 | 112 | 'createChart' => add_query_arg( array( 'action' => 'visualizer-create-chart', 'library' => 'yes', 'type' => '', 'chart-library' => '', 'tab' => 'visualizer' ), admin_url( 'admin-ajax.php' ) ), |
| 120 | - 'chartsPerPage' => defined( 'TI_E2E_TESTING' ) ? 20 : 6, | |
| 113 | + 'sqlTable' => $table_col_mapping, | |
| 114 | + 'chartsPerPage' => defined( 'TI_CYPRESS_TESTING' ) ? 20 : 6, | |
| 115 | + 'proFeaturesLocked' => Visualizer_Module_Admin::proFeaturesLocked(), | |
| 121 | 116 | 'isFullSiteEditor' => 'site-editor.php' === $pagenow, |
| 117 | + 'legacyBlockEdit' => apply_filters( 'visualizer_legacy_block_edit', false ), | |
| 122 | 118 | /* translators: %1$s: opening tag, %2$s: closing tag */ |
| 119 | + 'blockEditDoc' => sprintf( __( 'The editor for managing chart settings has been removed from the block editor. You can find more information in this %1$sdocumentation%2$s', 'visualizer' ), '<a href="https://docs.themeisle.com/article/1705-how-can-i-display-a-chart#using-visualizer-block" target="_blank">', '</a>' ), | |
| 123 | 120 | 'chartEditUrl' => admin_url( 'admin-ajax.php' ), |
| 124 | 121 | ); |
| 125 | 122 | wp_localize_script( 'visualizer-gutenberg-block', 'visualizerLocalize', $translation_array ); |
| 126 | 123 | |
| 127 | - $d3_renderer_asset = VISUALIZER_ABSPATH . '/classes/Visualizer/D3Renderer/build/index.asset.php'; | |
| 128 | - if ( file_exists( $d3_renderer_asset ) && ! wp_script_is( 'visualizer-d3-renderer', 'registered' ) ) { | |
| 129 | - // @phpstan-ignore-next-line | |
| 130 | - $d3_asset = include $d3_renderer_asset; | |
| 131 | - wp_register_script( | |
| 132 | - 'visualizer-d3-renderer', | |
| 133 | - VISUALIZER_ABSURL . 'classes/Visualizer/D3Renderer/build/index.js', | |
| 134 | - array_merge( $d3_asset['dependencies'], array( 'jquery' ) ), | |
| 135 | - $d3_asset['version'], | |
| 136 | - true | |
| 137 | - ); | |
| 138 | - } | |
| 139 | - if ( wp_script_is( 'visualizer-d3-renderer', 'registered' ) ) { | |
| 140 | - wp_enqueue_script( 'visualizer-d3-renderer' ); | |
| 141 | - wp_localize_script( | |
| 142 | - 'visualizer-d3-renderer', | |
| 143 | - 'vizD3Renderer', | |
| 144 | - array( | |
| 145 | - 'iframeJsUrl' => VISUALIZER_ABSURL . 'classes/Visualizer/D3Renderer/build/iframe.js', | |
| 146 | - ) | |
| 147 | - ); | |
| 148 | - } | |
| 124 | + // Enqueue frontend and editor block styles | |
| 125 | + wp_enqueue_style( 'handsontable', $handsontableCSS ); | |
| 126 | + wp_enqueue_style( 'visualizer-gutenberg-block', $stylePath, array( 'visualizer-datatables' ), $version ); | |
| 149 | 127 | |
| 150 | - // Enqueue frontend and editor block styles | |
| 151 | - wp_enqueue_style( 'visualizer-gutenberg-block', $stylePath, array( 'visualizer-datatables' ), $asset['version'] ); | |
| 128 | + if ( version_compare( $wp_version, '4.9.0', '>' ) ) { | |
| 152 | 129 | |
| 153 | - // Enqueue ChartBuilder (AI Builder) so the D3 edit modal is available in the block editor. | |
| 154 | - $chart_builder_asset = VISUALIZER_ABSPATH . '/classes/Visualizer/ChartBuilder/build/index.asset.php'; | |
| 155 | - if ( file_exists( $chart_builder_asset ) && ! wp_script_is( 'visualizer-chart-builder', 'enqueued' ) ) { | |
| 156 | - /** | |
| 157 | - * Ignore missing build asset in source checkout. | |
| 158 | - * | |
| 159 | - * @phpstan-ignore-next-line | |
| 160 | - */ | |
| 161 | - $cb_asset = include $chart_builder_asset; | |
| 162 | - wp_enqueue_script( | |
| 163 | - 'visualizer-chart-builder', | |
| 164 | - VISUALIZER_ABSURL . 'classes/Visualizer/ChartBuilder/build/index.js', | |
| 165 | - $cb_asset['dependencies'], | |
| 166 | - $cb_asset['version'], | |
| 167 | - true | |
| 168 | - ); | |
| 169 | - wp_enqueue_style( | |
| 170 | - 'visualizer-chart-builder', | |
| 171 | - VISUALIZER_ABSURL . 'classes/Visualizer/ChartBuilder/build/style-index.css', | |
| 172 | - array(), | |
| 173 | - $cb_asset['version'] | |
| 174 | - ); | |
| 175 | - $chart_builder_css = VISUALIZER_ABSPATH . '/classes/Visualizer/ChartBuilder/build/index.css'; | |
| 176 | - if ( file_exists( $chart_builder_css ) ) { | |
| 177 | - wp_enqueue_style( | |
| 178 | - 'visualizer-chart-builder-runtime', | |
| 179 | - VISUALIZER_ABSURL . 'classes/Visualizer/ChartBuilder/build/index.css', | |
| 180 | - array( 'visualizer-chart-builder' ), | |
| 181 | - $cb_asset['version'] | |
| 182 | - ); | |
| 183 | - } | |
| 184 | - wp_localize_script( | |
| 185 | - 'visualizer-chart-builder', | |
| 186 | - 'vizAIBuilder', | |
| 130 | + wp_enqueue_code_editor( | |
| 187 | 131 | array( |
| 188 | - 'ajaxUrl' => admin_url( 'admin-ajax.php' ), | |
| 189 | - 'nonce' => wp_create_nonce( 'visualizer-ai-builder' ), | |
| 190 | - 'isPro' => Visualizer_Module::is_pro(), | |
| 132 | + 'type' => 'sql', | |
| 133 | + 'codemirror' => array( | |
| 134 | + 'autofocus' => true, | |
| 135 | + 'lineWrapping' => true, | |
| 136 | + 'dragDrop' => false, | |
| 137 | + 'matchBrackets' => true, | |
| 138 | + 'autoCloseBrackets' => true, | |
| 139 | + 'extraKeys' => array( 'Shift-Space' => 'autocomplete' ), | |
| 140 | + 'hintOptions' => array( 'tables' => $table_col_mapping ), | |
| 141 | + ), | |
| 191 | 142 | ) |
| 192 | 143 | ); |
| 193 | - // Inject the mount point the ChartBuilder React app needs. | |
| 194 | - wp_add_inline_script( | |
| 195 | - 'visualizer-chart-builder', | |
| 196 | - 'document.body.insertAdjacentHTML("beforeend","<div id=\"viz-chart-builder-root\"></div>");', | |
| 197 | - 'before' | |
| 198 | - ); | |
| 199 | 144 | } |
| 200 | 145 | } |
| 201 | 146 | /** |
| 202 | 147 | * Hook server side rendering into render callback |
| @@ -240,9 +185,10 @@ | ||
| 240 | 185 | if ( ! $atts['id'] ) { |
| 241 | 186 | return ''; |
| 242 | 187 | } |
| 243 | 188 | |
| 244 | - if ( $atts['lazy'] === '-1' || $atts['lazy'] === false ) { | |
| 189 | + // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison | |
| 190 | + if ( $atts['lazy'] == -1 || $atts['lazy'] == false ) { | |
| 245 | 191 | $atts['lazy'] = 'no'; |
| 246 | 192 | } |
| 247 | 193 | |
| 248 | 194 | // we don't want the chart in the editor lazy-loading. |
| @@ -269,8 +215,125 @@ | ||
| 269 | 215 | array( |
| 270 | 216 | 'get_callback' => array( $this, 'get_visualizer_data' ), |
| 271 | 217 | ) |
| 272 | 218 | ); |
| 219 | + | |
| 220 | + register_rest_route( | |
| 221 | + 'visualizer/v' . VISUALIZER_REST_VERSION, | |
| 222 | + '/get-query-data', | |
| 223 | + array( | |
| 224 | + 'methods' => 'GET', | |
| 225 | + 'callback' => array( $this, 'get_query_data' ), | |
| 226 | + 'permission_callback' => function () { | |
| 227 | + return current_user_can( 'edit_posts' ); | |
| 228 | + }, | |
| 229 | + ) | |
| 230 | + ); | |
| 231 | + | |
| 232 | + register_rest_route( | |
| 233 | + 'visualizer/v' . VISUALIZER_REST_VERSION, | |
| 234 | + '/get-json-root', | |
| 235 | + array( | |
| 236 | + 'methods' => 'GET', | |
| 237 | + 'callback' => array( $this, 'get_json_root_data' ), | |
| 238 | + 'args' => array( | |
| 239 | + 'url' => array( | |
| 240 | + 'sanitize_callback' => 'esc_url_raw', | |
| 241 | + ), | |
| 242 | + ), | |
| 243 | + 'permission_callback' => function () { | |
| 244 | + return current_user_can( 'edit_posts' ); | |
| 245 | + }, | |
| 246 | + ) | |
| 247 | + ); | |
| 248 | + | |
| 249 | + register_rest_route( | |
| 250 | + 'visualizer/v' . VISUALIZER_REST_VERSION, | |
| 251 | + '/get-json-data', | |
| 252 | + array( | |
| 253 | + 'methods' => 'GET', | |
| 254 | + 'callback' => array( $this, 'get_json_data' ), | |
| 255 | + 'args' => array( | |
| 256 | + 'url' => array( | |
| 257 | + 'sanitize_callback' => 'esc_url_raw', | |
| 258 | + ), | |
| 259 | + 'chart' => array( | |
| 260 | + 'sanitize_callback' => 'absint', | |
| 261 | + ), | |
| 262 | + ), | |
| 263 | + 'permission_callback' => function () { | |
| 264 | + return current_user_can( 'edit_posts' ); | |
| 265 | + }, | |
| 266 | + ) | |
| 267 | + ); | |
| 268 | + | |
| 269 | + register_rest_route( | |
| 270 | + 'visualizer/v' . VISUALIZER_REST_VERSION, | |
| 271 | + '/set-json-data', | |
| 272 | + array( | |
| 273 | + 'methods' => 'GET', | |
| 274 | + 'callback' => array( $this, 'set_json_data' ), | |
| 275 | + 'args' => array( | |
| 276 | + 'url' => array( | |
| 277 | + 'sanitize_callback' => 'esc_url_raw', | |
| 278 | + ), | |
| 279 | + ), | |
| 280 | + 'permission_callback' => function () { | |
| 281 | + return current_user_can( 'edit_posts' ); | |
| 282 | + }, | |
| 283 | + ) | |
| 284 | + ); | |
| 285 | + | |
| 286 | + register_rest_route( | |
| 287 | + 'visualizer/v' . VISUALIZER_REST_VERSION, | |
| 288 | + '/update-chart', | |
| 289 | + array( | |
| 290 | + 'methods' => 'POST', | |
| 291 | + 'callback' => array( $this, 'update_chart_data' ), | |
| 292 | + 'args' => array( | |
| 293 | + 'id' => array( | |
| 294 | + 'sanitize_callback' => 'absint', | |
| 295 | + ), | |
| 296 | + ), | |
| 297 | + 'permission_callback' => function () { | |
| 298 | + return current_user_can( 'edit_posts' ); | |
| 299 | + }, | |
| 300 | + ) | |
| 301 | + ); | |
| 302 | + | |
| 303 | + register_rest_route( | |
| 304 | + 'visualizer/v' . VISUALIZER_REST_VERSION, | |
| 305 | + '/upload-data', | |
| 306 | + array( | |
| 307 | + 'methods' => 'POST', | |
| 308 | + 'callback' => array( $this, 'upload_csv_data' ), | |
| 309 | + 'args' => array( | |
| 310 | + 'url' => array( | |
| 311 | + 'sanitize_callback' => 'esc_url_raw', | |
| 312 | + ), | |
| 313 | + ), | |
| 314 | + 'permission_callback' => function () { | |
| 315 | + return current_user_can( 'edit_posts' ); | |
| 316 | + }, | |
| 317 | + ) | |
| 318 | + ); | |
| 319 | + | |
| 320 | + register_rest_route( | |
| 321 | + 'visualizer/v' . VISUALIZER_REST_VERSION, | |
| 322 | + '/get-permission-data', | |
| 323 | + array( | |
| 324 | + 'methods' => 'GET', | |
| 325 | + 'callback' => array( $this, 'get_permission_data' ), | |
| 326 | + 'args' => array( | |
| 327 | + 'type' => array( | |
| 328 | + 'sanitize_callback' => 'sanitize_text_field', | |
| 329 | + ), | |
| 330 | + ), | |
| 331 | + 'permission_callback' => function () { | |
| 332 | + return current_user_can( 'edit_posts' ); | |
| 333 | + }, | |
| 334 | + ) | |
| 335 | + ); | |
| 273 | 336 | } |
| 274 | 337 | |
| 275 | 338 | /** |
| 276 | 339 | * Get Post Meta Fields |
| @@ -286,11 +349,8 @@ | ||
| 286 | 349 | $data['visualizer-chart-type'] = get_post_meta( $post_id, Visualizer_Plugin::CF_CHART_TYPE, true ); |
| 287 | 350 | |
| 288 | 351 | $library = get_post_meta( $post_id, Visualizer_Plugin::CF_CHART_LIBRARY, true ); |
| 289 | 352 | $data['visualizer-chart-library'] = $library; |
| 290 | - if ( 'd3' === $library ) { | |
| 291 | - $data['visualizer-d3-code'] = get_post_meta( $post_id, Visualizer_Module_AIBuilder::CF_D3_CODE, true ); | |
| 292 | - } | |
| 293 | 353 | |
| 294 | 354 | $data['visualizer-source'] = get_post_meta( $post_id, Visualizer_Plugin::CF_SOURCE, true ); |
| 295 | 355 | |
| 296 | 356 | $data['visualizer-default-data'] = get_post_meta( $post_id, Visualizer_Plugin::CF_DEFAULT_DATA, true ); |
| @@ -306,9 +366,9 @@ | ||
| 306 | 366 | // handle settings filter hooks |
| 307 | 367 | $data['visualizer-settings'] = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SETTINGS, $data['visualizer-settings'], $post_id, $data['visualizer-chart-type'] ); |
| 308 | 368 | |
| 309 | 369 | // handle data filter hooks |
| 310 | - $data['visualizer-data'] = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA, Visualizer_Module::decode_content( html_entity_decode( get_post_field( 'post_content', $post_id, 'raw' ) ) ), $post_id, $data['visualizer-chart-type'] ); | |
| 370 | + $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'] ); | |
| 311 | 371 | |
| 312 | 372 | // we are going to format only for tabular charts, because we are not sure of the effect on others. |
| 313 | 373 | // this is to solve the case where boolean data shows up as all-ticks on gutenberg. |
| 314 | 374 | if ( in_array( $data['visualizer-chart-type'], array( 'tabular' ), true ) ) { |
| @@ -408,17 +468,16 @@ | ||
| 408 | 468 | } |
| 409 | 469 | } |
| 410 | 470 | |
| 411 | 471 | if ( Visualizer_Module::is_pro() ) { |
| 412 | - $permissions = get_post_meta( $post_id, Visualizer_Pro::CF_PERMISSIONS, true ); | |
| 472 | + $permissions = get_post_meta( $post_id, Visualizer_PRO::CF_PERMISSIONS, true ); | |
| 413 | 473 | |
| 414 | 474 | if ( empty( $permissions ) ) { |
| 415 | - $permissions = array( | |
| 416 | - 'permissions' => array( | |
| 475 | + $permissions = array( 'permissions' => array( | |
| 417 | 476 | 'read' => 'all', |
| 418 | 477 | 'edit' => 'roles', |
| 419 | 478 | 'edit-specific' => array( 'administrator' ), |
| 420 | - ), | |
| 479 | + ), | |
| 421 | 480 | ); |
| 422 | 481 | } |
| 423 | 482 | |
| 424 | 483 | $data['visualizer-permissions'] = $permissions; |
| @@ -427,8 +486,225 @@ | ||
| 427 | 486 | return $data; |
| 428 | 487 | } |
| 429 | 488 | |
| 430 | 489 | /** |
| 490 | + * Returns the data for the query. | |
| 491 | + * | |
| 492 | + * @access public | |
| 493 | + */ | |
| 494 | + public function get_query_data( $data ) { | |
| 495 | + if ( ! current_user_can( 'edit_posts' ) ) { | |
| 496 | + return false; | |
| 497 | + } | |
| 498 | + | |
| 499 | + $source = new Visualizer_Source_Query( stripslashes( $data['query'] ) ); | |
| 500 | + $html = $source->fetch( true ); | |
| 501 | + $source->fetch( false ); | |
| 502 | + $name = $source->getSourceName(); | |
| 503 | + $series = $source->getSeries(); | |
| 504 | + $data = $source->getRawData(); | |
| 505 | + $error = ''; | |
| 506 | + if ( empty( $html ) ) { | |
| 507 | + $error = $source->get_error(); | |
| 508 | + wp_send_json_error( array( 'msg' => $error ) ); | |
| 509 | + } | |
| 510 | + wp_send_json_success( array( 'table' => $html, 'name' => $name, 'series' => $series, 'data' => $data ) ); | |
| 511 | + } | |
| 512 | + | |
| 513 | + /** | |
| 514 | + * Returns the JSON root. | |
| 515 | + * | |
| 516 | + * @access public | |
| 517 | + */ | |
| 518 | + public function get_json_root_data( $data ) { | |
| 519 | + if ( ! current_user_can( 'edit_posts' ) ) { | |
| 520 | + return false; | |
| 521 | + } | |
| 522 | + | |
| 523 | + $source = new Visualizer_Source_Json( $data ); | |
| 524 | + | |
| 525 | + $roots = $source->fetchRoots(); | |
| 526 | + if ( empty( $roots ) ) { | |
| 527 | + wp_send_json_error( array( 'msg' => $source->get_error() ) ); | |
| 528 | + } | |
| 529 | + | |
| 530 | + wp_send_json_success( array( 'url' => $data['url'], 'roots' => $roots ) ); | |
| 531 | + } | |
| 532 | + | |
| 533 | + /** | |
| 534 | + * Returns the JSON data. | |
| 535 | + * | |
| 536 | + * @access public | |
| 537 | + */ | |
| 538 | + public function get_json_data( $data ) { | |
| 539 | + if ( ! current_user_can( 'edit_posts' ) ) { | |
| 540 | + return false; | |
| 541 | + } | |
| 542 | + | |
| 543 | + $chart_id = $data['chart']; | |
| 544 | + | |
| 545 | + if ( empty( $chart_id ) ) { | |
| 546 | + wp_die(); | |
| 547 | + } | |
| 548 | + | |
| 549 | + $source = new Visualizer_Source_Json( $data ); | |
| 550 | + $source->fetch(); | |
| 551 | + $table = $source->getRawData(); | |
| 552 | + | |
| 553 | + if ( empty( $table ) ) { | |
| 554 | + wp_send_json_error( array( 'msg' => esc_html__( 'Unable to fetch data from the endpoint. Please try again.', 'visualizer' ) ) ); | |
| 555 | + } | |
| 556 | + | |
| 557 | + $table = Visualizer_Render_Layout::show( 'editor-table', $table, $chart_id, 'viz-json-table', false, false ); | |
| 558 | + wp_send_json_success( array( 'table' => $table, 'root' => $data['root'], 'url' => $data['url'], 'paging' => $source->getPaginationElements() ) ); | |
| 559 | + } | |
| 560 | + | |
| 561 | + /** | |
| 562 | + * Set the JSON data. | |
| 563 | + * | |
| 564 | + * @access public | |
| 565 | + */ | |
| 566 | + public function set_json_data( $data ) { | |
| 567 | + if ( ! current_user_can( 'edit_posts' ) ) { | |
| 568 | + return false; | |
| 569 | + } | |
| 570 | + | |
| 571 | + $source = new Visualizer_Source_Json( $data ); | |
| 572 | + | |
| 573 | + $table = $source->fetch(); | |
| 574 | + if ( empty( $table ) ) { | |
| 575 | + wp_send_json_error( array( 'msg' => esc_html__( 'Unable to fetch data from the endpoint. Please try again.', 'visualizer' ) ) ); | |
| 576 | + } | |
| 577 | + | |
| 578 | + $source->fetchFromEditableTable(); | |
| 579 | + $name = $source->getSourceName(); | |
| 580 | + $series = json_encode( $source->getSeries() ); | |
| 581 | + $data = json_encode( $source->getRawData() ); | |
| 582 | + wp_send_json_success( array( 'name' => $name, 'series' => $series, 'data' => $data ) ); | |
| 583 | + } | |
| 584 | + | |
| 585 | + /** | |
| 586 | + * Rest Callback Method | |
| 587 | + */ | |
| 588 | + public function update_chart_data( $data ) { | |
| 589 | + if ( ! current_user_can( 'edit_posts' ) ) { | |
| 590 | + return false; | |
| 591 | + } | |
| 592 | + | |
| 593 | + if ( $data['id'] && ! is_wp_error( $data['id'] ) ) { | |
| 594 | + if ( get_post_type( $data['id'] ) !== Visualizer_Plugin::CPT_VISUALIZER ) { | |
| 595 | + return new WP_Error( 'invalid_post_type', 'Invalid post type.' ); | |
| 596 | + } | |
| 597 | + $chart_type = sanitize_text_field( $data['visualizer-chart-type'] ); | |
| 598 | + $source_type = sanitize_text_field( $data['visualizer-source'] ); | |
| 599 | + | |
| 600 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_TYPE, $chart_type ); | |
| 601 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_SOURCE, $source_type ); | |
| 602 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_DEFAULT_DATA, $data['visualizer-default-data'] ); | |
| 603 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_SERIES, $data['visualizer-series'] ); | |
| 604 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_SETTINGS, $data['visualizer-settings'] ); | |
| 605 | + | |
| 606 | + if ( $data['visualizer-chart-url'] && $data['visualizer-chart-schedule'] >= 0 ) { | |
| 607 | + $chart_url = esc_url_raw( $data['visualizer-chart-url'] ); | |
| 608 | + $chart_schedule = intval( $data['visualizer-chart-schedule'] ); | |
| 609 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_URL, $chart_url ); | |
| 610 | + apply_filters( 'visualizer_pro_chart_schedule', $data['id'], $chart_url, $chart_schedule ); | |
| 611 | + } else { | |
| 612 | + delete_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_URL ); | |
| 613 | + apply_filters( 'visualizer_pro_remove_schedule', $data['id'] ); | |
| 614 | + } | |
| 615 | + | |
| 616 | + // let's check if this is not an external db chart | |
| 617 | + // as there is no support for that in the block editor interface | |
| 618 | + $external_params = get_post_meta( $data['id'], Visualizer_Plugin::CF_REMOTE_DB_PARAMS, true ); | |
| 619 | + if ( empty( $external_params ) ) { | |
| 620 | + if ( $source_type === 'Visualizer_Source_Query' ) { | |
| 621 | + $db_schedule = intval( $data['visualizer-db-schedule'] ); | |
| 622 | + $db_query = $data['visualizer-db-query']; | |
| 623 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_DB_SCHEDULE, $db_schedule ); | |
| 624 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_DB_QUERY, stripslashes( $db_query ) ); | |
| 625 | + } else { | |
| 626 | + delete_post_meta( $data['id'], Visualizer_Plugin::CF_DB_SCHEDULE ); | |
| 627 | + delete_post_meta( $data['id'], Visualizer_Plugin::CF_DB_QUERY ); | |
| 628 | + } | |
| 629 | + | |
| 630 | + if ( 'Visualizer_Source_Csv_Remote' === $source_type ) { | |
| 631 | + $schedule_url = $data['visualizer-chart-url']; | |
| 632 | + $schedule_id = $data['visualizer-chart-schedule']; | |
| 633 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_URL, $schedule_url ); | |
| 634 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_SCHEDULE, $schedule_id ); | |
| 635 | + } else { | |
| 636 | + delete_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_URL ); | |
| 637 | + delete_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_SCHEDULE ); | |
| 638 | + } | |
| 639 | + } | |
| 640 | + | |
| 641 | + if ( $source_type === 'Visualizer_Source_Json' ) { | |
| 642 | + $json_schedule = intval( $data['visualizer-json-schedule'] ); | |
| 643 | + $json_url = esc_url_raw( $data['visualizer-json-url'] ); | |
| 644 | + $json_headers = esc_url_raw( $data['visualizer-json-headers'] ); | |
| 645 | + $json_root = $data['visualizer-json-root']; | |
| 646 | + $json_paging = $data['visualizer-json-paging']; | |
| 647 | + | |
| 648 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_SCHEDULE, $json_schedule ); | |
| 649 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_URL, $json_url ); | |
| 650 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_HEADERS, $json_headers ); | |
| 651 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_ROOT, $json_root ); | |
| 652 | + | |
| 653 | + if ( ! empty( $json_paging ) ) { | |
| 654 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_PAGING, $json_paging ); | |
| 655 | + } else { | |
| 656 | + delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_PAGING ); | |
| 657 | + } | |
| 658 | + } else { | |
| 659 | + delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_SCHEDULE ); | |
| 660 | + delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_URL ); | |
| 661 | + delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_HEADERS ); | |
| 662 | + delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_ROOT ); | |
| 663 | + delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_PAGING ); | |
| 664 | + } | |
| 665 | + | |
| 666 | + if ( Visualizer_Module::is_pro() ) { | |
| 667 | + update_post_meta( $data['id'], Visualizer_PRO::CF_PERMISSIONS, $data['visualizer-permissions'] ); | |
| 668 | + } | |
| 669 | + | |
| 670 | + if ( $data['visualizer-chart-url'] ) { | |
| 671 | + $chart_url = esc_url_raw( $data['visualizer-chart-url'] ); | |
| 672 | + $content['source'] = $chart_url; | |
| 673 | + $content['data'] = $this->format_chart_data( $data['visualizer-data'], $data['visualizer-series'] ); | |
| 674 | + } else { | |
| 675 | + $content = $this->format_chart_data( $data['visualizer-data'], $data['visualizer-series'] ); | |
| 676 | + } | |
| 677 | + | |
| 678 | + $chart = array( | |
| 679 | + 'ID' => $data['id'], | |
| 680 | + 'post_content' => serialize( $content ), | |
| 681 | + ); | |
| 682 | + | |
| 683 | + wp_update_post( $chart ); | |
| 684 | + | |
| 685 | + // Clear existing chart cache. | |
| 686 | + $cache_key = Visualizer_Plugin::CF_CHART_CACHE . '_' . $data['id']; | |
| 687 | + if ( get_transient( $cache_key ) ) { | |
| 688 | + delete_transient( $cache_key ); | |
| 689 | + } | |
| 690 | + | |
| 691 | + $revisions = wp_get_post_revisions( $data['id'], array( 'order' => 'ASC' ) ); | |
| 692 | + | |
| 693 | + if ( count( $revisions ) > 1 ) { | |
| 694 | + $revision_ids = array_keys( $revisions ); | |
| 695 | + | |
| 696 | + // delete all revisions. | |
| 697 | + foreach ( $revision_ids as $id ) { | |
| 698 | + wp_delete_post_revision( $id ); | |
| 699 | + } | |
| 700 | + } | |
| 701 | + | |
| 702 | + return new \WP_REST_Response( array( 'success' => sprintf( 'Chart updated' ) ) ); | |
| 703 | + } | |
| 704 | + } | |
| 705 | + | |
| 706 | + /** | |
| 431 | 707 | * Format chart data. |
| 432 | 708 | * |
| 433 | 709 | * Note: No matter how tempted, don't use the similar method from Visualizer_Source. That works on a different structure. |
| 434 | 710 | */ |
| @@ -433,9 +709,9 @@ | ||
| 433 | 709 | * Note: No matter how tempted, don't use the similar method from Visualizer_Source. That works on a different structure. |
| 434 | 710 | */ |
| 435 | 711 | public function format_chart_data( $data, $series ) { |
| 436 | 712 | foreach ( $series as $i => $row ) { |
| 437 | - // if no value exists for the series, then add null | |
| 713 | + // if no value exists for the seires, then add null | |
| 438 | 714 | if ( ! isset( $series[ $i ] ) ) { |
| 439 | 715 | $series[ $i ] = null; |
| 440 | 716 | } |
| 441 | 717 | |
| @@ -485,8 +761,86 @@ | ||
| 485 | 761 | if ( ! function_exists( 'mb_detect_encoding' ) || mb_detect_encoding( $datum ) !== 'ASCII' ) { |
| 486 | 762 | $datum = \ForceUTF8\Encoding::toUTF8( $datum ); |
| 487 | 763 | } |
| 488 | 764 | return $datum; |
| 765 | + } | |
| 766 | + | |
| 767 | + /** | |
| 768 | + * Handle remote CSV data | |
| 769 | + */ | |
| 770 | + public function upload_csv_data( $data ) { | |
| 771 | + if ( ! current_user_can( 'edit_posts' ) ) { | |
| 772 | + return false; | |
| 773 | + } | |
| 774 | + | |
| 775 | + $remote_data = false; | |
| 776 | + if ( isset( $data['url'] ) && function_exists( 'wp_http_validate_url' ) ) { | |
| 777 | + $remote_data = wp_http_validate_url( $data['url'] ); | |
| 778 | + } | |
| 779 | + if ( false !== $remote_data && ! is_wp_error( $remote_data ) ) { | |
| 780 | + $source = new Visualizer_Source_Csv_Remote( $remote_data ); | |
| 781 | + if ( $source->fetch() ) { | |
| 782 | + $temp = $source->getData(); | |
| 783 | + if ( is_string( $temp ) && is_array( unserialize( $temp ) ) ) { | |
| 784 | + $content['series'] = $source->getSeries(); | |
| 785 | + $content['data'] = $source->getRawData(); | |
| 786 | + return $content; | |
| 787 | + } else { | |
| 788 | + return new \WP_REST_Response( array( 'failed' => sprintf( 'Invalid CSV URL' ) ) ); | |
| 789 | + } | |
| 790 | + } else { | |
| 791 | + return new \WP_REST_Response( array( 'failed' => sprintf( 'Invalid CSV URL' ) ) ); | |
| 792 | + } | |
| 793 | + } else { | |
| 794 | + return new \WP_REST_Response( array( 'failed' => sprintf( 'Invalid CSV URL' ) ) ); | |
| 795 | + } | |
| 796 | + } | |
| 797 | + | |
| 798 | + /** | |
| 799 | + * Get permission data | |
| 800 | + */ | |
| 801 | + public function get_permission_data( $data ) { | |
| 802 | + if ( ! current_user_can( 'edit_posts' ) ) { | |
| 803 | + return false; | |
| 804 | + } | |
| 805 | + | |
| 806 | + $options = array(); | |
| 807 | + switch ( $data['type'] ) { | |
| 808 | + case 'users': | |
| 809 | + $query = new WP_User_Query( | |
| 810 | + array( | |
| 811 | + 'number' => 1000, | |
| 812 | + 'orderby' => 'display_name', | |
| 813 | + 'fields' => array( 'ID', 'display_name' ), | |
| 814 | + 'count_total' => false, | |
| 815 | + ) | |
| 816 | + ); | |
| 817 | + $users = $query->get_results(); | |
| 818 | + if ( ! empty( $users ) ) { | |
| 819 | + $i = 0; | |
| 820 | + foreach ( $users as $user ) { | |
| 821 | + $options[ $i ]['value'] = $user->ID; | |
| 822 | + $options[ $i ]['label'] = $user->display_name; | |
| 823 | + $i++; | |
| 824 | + } | |
| 825 | + } | |
| 826 | + break; | |
| 827 | + case 'roles': | |
| 828 | + if ( ! function_exists( 'get_editable_roles' ) ) { | |
| 829 | + require_once ABSPATH . 'wp-admin/includes/user.php'; | |
| 830 | + } | |
| 831 | + $roles = get_editable_roles(); | |
| 832 | + if ( ! empty( $roles ) ) { | |
| 833 | + $i = 0; | |
| 834 | + foreach ( get_editable_roles() as $name => $info ) { | |
| 835 | + $options[ $i ]['value'] = $name; | |
| 836 | + $options[ $i ]['label'] = $name; | |
| 837 | + $i++; | |
| 838 | + } | |
| 839 | + } | |
| 840 | + break; | |
| 841 | + } | |
| 842 | + return $options; | |
| 489 | 843 | } |
| 490 | 844 | |
| 491 | 845 | /** |
| 492 | 846 | * Filter Rest Query |