| @@ -45,9 +45,9 @@ | ||
| 45 | 45 | /** |
| 46 | 46 | * Returns an instance of this class. |
| 47 | 47 | */ |
| 48 | 48 | public static function get_instance() { |
| 49 | - if ( null == self::$instance ) { | |
| 49 | + if ( null === self::$instance ) { | |
| 50 | 50 | self::$instance = new Visualizer_Gutenberg_Block(); |
| 51 | 51 | } |
| 52 | 52 | return self::$instance; |
| 53 | 53 | } |
| @@ -59,8 +59,9 @@ | ||
| 59 | 59 | $this->version = Visualizer_Plugin::VERSION; |
| 60 | 60 | add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_gutenberg_scripts' ) ); |
| 61 | 61 | add_action( 'init', array( $this, 'register_block_type' ) ); |
| 62 | 62 | add_action( 'rest_api_init', array( $this, 'register_rest_endpoints' ) ); |
| 63 | + add_filter( 'rest_visualizer_query', array( $this, 'add_rest_query_vars' ), 9, 2 ); | |
| 63 | 64 | } |
| 64 | 65 | |
| 65 | 66 | /** |
| 66 | 67 | * Enqueue front end and editor JavaScript and CSS |
| @@ -65,13 +66,15 @@ | ||
| 65 | 66 | /** |
| 66 | 67 | * Enqueue front end and editor JavaScript and CSS |
| 67 | 68 | */ |
| 68 | 69 | public function enqueue_gutenberg_scripts() { |
| 69 | - $blockPath = VISUALIZER_ABSURL . '/classes/Visualizer/Gutenberg/build/block.js'; | |
| 70 | - $handsontableJS = VISUALIZER_ABSURL . '/classes/Visualizer/Gutenberg/build/handsontable.js'; | |
| 71 | - $stylePath = VISUALIZER_ABSURL . '/classes/Visualizer/Gutenberg/build/block.css'; | |
| 72 | - $handsontableCSS = VISUALIZER_ABSURL . '/classes/Visualizer/Gutenberg/build/handsontable.css'; | |
| 70 | + global $wp_version; | |
| 73 | 71 | |
| 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'; | |
| 76 | + | |
| 74 | 77 | if ( VISUALIZER_TEST_JS_CUSTOMIZATION ) { |
| 75 | 78 | $version = filemtime( VISUALIZER_ABSPATH . '/classes/Visualizer/Gutenberg/build/block.js' ); |
| 76 | 79 | } else { |
| 77 | 80 | $version = $this->version; |
| @@ -76,15 +79,23 @@ | ||
| 76 | 79 | } else { |
| 77 | 80 | $version = $this->version; |
| 78 | 81 | } |
| 79 | 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 | + | |
| 80 | 91 | // Enqueue the bundled block JS file |
| 81 | 92 | wp_enqueue_script( 'handsontable', $handsontableJS ); |
| 82 | - wp_enqueue_script( 'visualizer-gutenberg-block', $blockPath, array( 'wp-api', 'handsontable' ), $version ); | |
| 93 | + wp_enqueue_script( 'visualizer-gutenberg-block', $blockPath, array( 'wp-api', 'handsontable', 'visualizer-datatables', 'moment' ), $version, true ); | |
| 83 | 94 | |
| 84 | 95 | $type = 'community'; |
| 85 | 96 | |
| 86 | - if ( VISUALIZER_PRO ) { | |
| 97 | + if ( Visualizer_Module::is_pro() ) { | |
| 87 | 98 | $type = 'pro'; |
| 88 | 99 | if ( apply_filters( 'visualizer_is_business', false ) ) { |
| 89 | 100 | $type = 'business'; |
| 90 | 101 | } |
| @@ -89,18 +100,43 @@ | ||
| 89 | 100 | $type = 'business'; |
| 90 | 101 | } |
| 91 | 102 | } |
| 92 | 103 | |
| 104 | + $table_col_mapping = Visualizer_Source_Query_Params::get_all_db_tables_column_mapping( null, false ); | |
| 105 | + | |
| 93 | 106 | $translation_array = array( |
| 94 | 107 | 'isPro' => $type, |
| 95 | 108 | 'proTeaser' => Visualizer_Plugin::PRO_TEASER_URL, |
| 96 | 109 | 'absurl' => VISUALIZER_ABSURL, |
| 110 | + 'charts' => Visualizer_Module_Admin::_getChartTypesLocalized(), | |
| 111 | + 'adminPage' => menu_page_url( 'visualizer', false ), | |
| 112 | + 'sqlTable' => $table_col_mapping, | |
| 113 | + 'chartsPerPage' => defined( 'TI_CYPRESS_TESTING' ) ? 20 : 6, | |
| 114 | + 'proFeaturesLocked' => Visualizer_Module_Admin::proFeaturesLocked(), | |
| 97 | 115 | ); |
| 98 | 116 | wp_localize_script( 'visualizer-gutenberg-block', 'visualizerLocalize', $translation_array ); |
| 99 | 117 | |
| 100 | 118 | // Enqueue frontend and editor block styles |
| 101 | 119 | wp_enqueue_style( 'handsontable', $handsontableCSS ); |
| 102 | - wp_enqueue_style( 'visualizer-gutenberg-block', $stylePath, '', $version ); | |
| 120 | + wp_enqueue_style( 'visualizer-gutenberg-block', $stylePath, array( 'visualizer-datatables' ), $version ); | |
| 121 | + | |
| 122 | + if ( version_compare( $wp_version, '4.9.0', '>' ) ) { | |
| 123 | + | |
| 124 | + wp_enqueue_code_editor( | |
| 125 | + array( | |
| 126 | + 'type' => 'sql', | |
| 127 | + 'codemirror' => array( | |
| 128 | + 'autofocus' => true, | |
| 129 | + 'lineWrapping' => true, | |
| 130 | + 'dragDrop' => false, | |
| 131 | + 'matchBrackets' => true, | |
| 132 | + 'autoCloseBrackets' => true, | |
| 133 | + 'extraKeys' => array( 'Ctrl-Space' => 'autocomplete' ), | |
| 134 | + 'hintOptions' => array( 'tables' => $table_col_mapping ), | |
| 135 | + ), | |
| 136 | + ) | |
| 137 | + ); | |
| 138 | + } | |
| 103 | 139 | } |
| 104 | 140 | /** |
| 105 | 141 | * Hook server side rendering into render callback |
| 106 | 142 | */ |
| @@ -111,8 +147,11 @@ | ||
| 111 | 147 | 'attributes' => array( |
| 112 | 148 | 'id' => array( |
| 113 | 149 | 'type' => 'number', |
| 114 | 150 | ), |
| 151 | + 'lazy' => array( | |
| 152 | + 'type' => 'string', | |
| 153 | + ), | |
| 115 | 154 | ), |
| 116 | 155 | ) |
| 117 | 156 | ); |
| 118 | 157 | } |
| @@ -119,14 +158,46 @@ | ||
| 119 | 158 | |
| 120 | 159 | /** |
| 121 | 160 | * Gutenberg Block Callback Function |
| 122 | 161 | */ |
| 123 | - public function gutenberg_block_callback( $attr ) { | |
| 124 | - $id = $attr['id']; | |
| 125 | - if ( empty( $id ) || $id === 'none' ) { | |
| 126 | - return ''; // no id = no fun | |
| 162 | + public function gutenberg_block_callback( $atts ) { | |
| 163 | + // no id, no fun. | |
| 164 | + if ( ! isset( $atts['id'] ) ) { | |
| 165 | + return ''; | |
| 127 | 166 | } |
| 128 | - return '[visualizer id="' . $id . '"]'; | |
| 167 | + | |
| 168 | + $atts = shortcode_atts( | |
| 169 | + array( | |
| 170 | + 'id' => false, | |
| 171 | + 'lazy' => apply_filters( 'visualizer_lazy_by_default', false, $atts['id'] ), | |
| 172 | + // we are deliberating excluding the class attribute from here | |
| 173 | + // as this will be handled by the custom class in Gutenberg | |
| 174 | + ), | |
| 175 | + $atts | |
| 176 | + ); | |
| 177 | + | |
| 178 | + // no id, no fun. | |
| 179 | + if ( ! $atts['id'] ) { | |
| 180 | + return ''; | |
| 181 | + } | |
| 182 | + | |
| 183 | + // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison | |
| 184 | + if ( $atts['lazy'] == -1 || $atts['lazy'] == false ) { | |
| 185 | + $atts['lazy'] = 'no'; | |
| 186 | + } | |
| 187 | + | |
| 188 | + // we don't want the chart in the editor lazy-loading. | |
| 189 | + if ( is_admin() ) { | |
| 190 | + unset( $atts['lazy'] ); | |
| 191 | + } | |
| 192 | + | |
| 193 | + $shortcode = '[visualizer'; | |
| 194 | + foreach ( $atts as $name => $value ) { | |
| 195 | + $shortcode .= sprintf( ' %s="%s"', $name, $value ); | |
| 196 | + } | |
| 197 | + $shortcode .= ']'; | |
| 198 | + | |
| 199 | + return $shortcode; | |
| 129 | 200 | } |
| 130 | 201 | |
| 131 | 202 | /** |
| 132 | 203 | * Hook server side rendering into render callback |
| @@ -141,8 +212,74 @@ | ||
| 141 | 212 | ); |
| 142 | 213 | |
| 143 | 214 | register_rest_route( |
| 144 | 215 | 'visualizer/v' . VISUALIZER_REST_VERSION, |
| 216 | + '/get-query-data', | |
| 217 | + array( | |
| 218 | + 'methods' => 'GET', | |
| 219 | + 'callback' => array( $this, 'get_query_data' ), | |
| 220 | + 'permission_callback' => function () { | |
| 221 | + return current_user_can( 'edit_posts' ); | |
| 222 | + }, | |
| 223 | + ) | |
| 224 | + ); | |
| 225 | + | |
| 226 | + register_rest_route( | |
| 227 | + 'visualizer/v' . VISUALIZER_REST_VERSION, | |
| 228 | + '/get-json-root', | |
| 229 | + array( | |
| 230 | + 'methods' => 'GET', | |
| 231 | + 'callback' => array( $this, 'get_json_root_data' ), | |
| 232 | + 'args' => array( | |
| 233 | + 'url' => array( | |
| 234 | + 'sanitize_callback' => 'esc_url_raw', | |
| 235 | + ), | |
| 236 | + ), | |
| 237 | + 'permission_callback' => function () { | |
| 238 | + return current_user_can( 'edit_posts' ); | |
| 239 | + }, | |
| 240 | + ) | |
| 241 | + ); | |
| 242 | + | |
| 243 | + register_rest_route( | |
| 244 | + 'visualizer/v' . VISUALIZER_REST_VERSION, | |
| 245 | + '/get-json-data', | |
| 246 | + array( | |
| 247 | + 'methods' => 'GET', | |
| 248 | + 'callback' => array( $this, 'get_json_data' ), | |
| 249 | + 'args' => array( | |
| 250 | + 'url' => array( | |
| 251 | + 'sanitize_callback' => 'esc_url_raw', | |
| 252 | + ), | |
| 253 | + 'chart' => array( | |
| 254 | + 'sanitize_callback' => 'absint', | |
| 255 | + ), | |
| 256 | + ), | |
| 257 | + 'permission_callback' => function () { | |
| 258 | + return current_user_can( 'edit_posts' ); | |
| 259 | + }, | |
| 260 | + ) | |
| 261 | + ); | |
| 262 | + | |
| 263 | + register_rest_route( | |
| 264 | + 'visualizer/v' . VISUALIZER_REST_VERSION, | |
| 265 | + '/set-json-data', | |
| 266 | + array( | |
| 267 | + 'methods' => 'GET', | |
| 268 | + 'callback' => array( $this, 'set_json_data' ), | |
| 269 | + 'args' => array( | |
| 270 | + 'url' => array( | |
| 271 | + 'sanitize_callback' => 'esc_url_raw', | |
| 272 | + ), | |
| 273 | + ), | |
| 274 | + 'permission_callback' => function () { | |
| 275 | + return current_user_can( 'edit_posts' ); | |
| 276 | + }, | |
| 277 | + ) | |
| 278 | + ); | |
| 279 | + | |
| 280 | + register_rest_route( | |
| 281 | + 'visualizer/v' . VISUALIZER_REST_VERSION, | |
| 145 | 282 | '/update-chart', |
| 146 | 283 | array( |
| 147 | 284 | 'methods' => 'POST', |
| 148 | 285 | 'callback' => array( $this, 'update_chart_data' ), |
| @@ -150,8 +287,11 @@ | ||
| 150 | 287 | 'id' => array( |
| 151 | 288 | 'sanitize_callback' => 'absint', |
| 152 | 289 | ), |
| 153 | 290 | ), |
| 291 | + 'permission_callback' => function () { | |
| 292 | + return current_user_can( 'edit_posts' ); | |
| 293 | + }, | |
| 154 | 294 | ) |
| 155 | 295 | ); |
| 156 | 296 | |
| 157 | 297 | register_rest_route( |
| @@ -164,8 +304,11 @@ | ||
| 164 | 304 | 'url' => array( |
| 165 | 305 | 'sanitize_callback' => 'esc_url_raw', |
| 166 | 306 | ), |
| 167 | 307 | ), |
| 308 | + 'permission_callback' => function () { | |
| 309 | + return current_user_can( 'edit_posts' ); | |
| 310 | + }, | |
| 168 | 311 | ) |
| 169 | 312 | ); |
| 170 | 313 | |
| 171 | 314 | register_rest_route( |
| @@ -178,8 +321,11 @@ | ||
| 178 | 321 | 'type' => array( |
| 179 | 322 | 'sanitize_callback' => 'sanitize_text_field', |
| 180 | 323 | ), |
| 181 | 324 | ), |
| 325 | + 'permission_callback' => function () { | |
| 326 | + return current_user_can( 'edit_posts' ); | |
| 327 | + }, | |
| 182 | 328 | ) |
| 183 | 329 | ); |
| 184 | 330 | } |
| 185 | 331 | |
| @@ -186,13 +332,20 @@ | ||
| 186 | 332 | /** |
| 187 | 333 | * Get Post Meta Fields |
| 188 | 334 | */ |
| 189 | 335 | public function get_visualizer_data( $post ) { |
| 336 | + if ( ! current_user_can( 'edit_posts' ) ) { | |
| 337 | + return false; | |
| 338 | + } | |
| 339 | + | |
| 190 | 340 | $data = array(); |
| 191 | 341 | $post_id = $post['id']; |
| 192 | 342 | |
| 193 | 343 | $data['visualizer-chart-type'] = get_post_meta( $post_id, Visualizer_Plugin::CF_CHART_TYPE, true ); |
| 194 | 344 | |
| 345 | + $library = get_post_meta( $post_id, Visualizer_Plugin::CF_CHART_LIBRARY, true ); | |
| 346 | + $data['visualizer-chart-library'] = $library; | |
| 347 | + | |
| 195 | 348 | $data['visualizer-source'] = get_post_meta( $post_id, Visualizer_Plugin::CF_SOURCE, true ); |
| 196 | 349 | |
| 197 | 350 | $data['visualizer-default-data'] = get_post_meta( $post_id, Visualizer_Plugin::CF_DEFAULT_DATA, true ); |
| 198 | 351 | |
| @@ -207,23 +360,116 @@ | ||
| 207 | 360 | |
| 208 | 361 | // handle data filter hooks |
| 209 | 362 | $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'] ); |
| 210 | 363 | |
| 364 | + // we are going to format only for tabular charts, because we are not sure of the effect on others. | |
| 365 | + // this is to solve the case where boolean data shows up as all-ticks on gutenberg. | |
| 366 | + if ( in_array( $data['visualizer-chart-type'], array( 'tabular' ), true ) ) { | |
| 367 | + $data['visualizer-data'] = $this->format_chart_data( $data['visualizer-data'], $data['visualizer-series'] ); | |
| 368 | + } | |
| 369 | + | |
| 370 | + $data['visualizer-data-exploded'] = ''; | |
| 371 | + // handle annotations for google charts | |
| 372 | + if ( 'GoogleCharts' === $library ) { | |
| 373 | + // this will contain the data of both axis. | |
| 374 | + $settings = $data['visualizer-settings']; | |
| 375 | + // this will contain data only of Y axis. | |
| 376 | + $series = $data['visualizer-series']; | |
| 377 | + $annotations = array(); | |
| 378 | + if ( isset( $settings['series'] ) ) { | |
| 379 | + foreach ( $settings['series'] as $index => $serie ) { | |
| 380 | + // skip X axis data. | |
| 381 | + if ( $index === 0 ) { | |
| 382 | + continue; | |
| 383 | + } | |
| 384 | + if ( ! empty( $serie['role'] ) ) { | |
| 385 | + // this series is some kind of annotation, so let's collect its index. | |
| 386 | + // the index will be +1 because the X axis value is index 0, which is being ignored. | |
| 387 | + $annotations[ 'role' . ( intval( $index ) + 1 ) ] = $serie['role']; | |
| 388 | + | |
| 389 | + if ( isset( $data['visualizer-series'][ intval( $index ) + 1 ] ) ) { | |
| 390 | + $data['visualizer-series'][ intval( $index ) + 1 ]['role'] = $serie['role']; | |
| 391 | + } | |
| 392 | + } | |
| 393 | + } | |
| 394 | + } | |
| 395 | + if ( ! empty( $annotations ) ) { | |
| 396 | + $exploded_data = array(); | |
| 397 | + $series_names = array(); | |
| 398 | + foreach ( $series as $index => $serie ) { | |
| 399 | + // skip X axis data. | |
| 400 | + if ( $index === 0 ) { | |
| 401 | + continue; | |
| 402 | + } | |
| 403 | + if ( array_key_exists( 'role' . $index, $annotations ) ) { | |
| 404 | + $series_names[] = (object) array( 'role' => $annotations[ 'role' . $index ], 'type' => $serie['type'] ); | |
| 405 | + } else { | |
| 406 | + $series_names[] = $serie['label']; | |
| 407 | + } | |
| 408 | + } | |
| 409 | + $exploded_data[] = $series_names; | |
| 410 | + | |
| 411 | + foreach ( $data['visualizer-data'] as $datum ) { | |
| 412 | + // skip X axis data. | |
| 413 | + unset( $datum[0] ); | |
| 414 | + $exploded_data[] = $datum; | |
| 415 | + } | |
| 416 | + $data['visualizer-data-exploded'] = array( $exploded_data ); | |
| 417 | + } | |
| 418 | + } | |
| 419 | + | |
| 211 | 420 | $import = get_post_meta( $post_id, Visualizer_Plugin::CF_CHART_URL, true ); |
| 212 | 421 | |
| 213 | 422 | $schedule = get_post_meta( $post_id, Visualizer_Plugin::CF_CHART_SCHEDULE, true ); |
| 214 | 423 | |
| 424 | + $db_schedule = get_post_meta( $post_id, Visualizer_Plugin::CF_DB_SCHEDULE, true ); | |
| 425 | + | |
| 426 | + $db_query = get_post_meta( $post_id, Visualizer_Plugin::CF_DB_QUERY, true ); | |
| 427 | + | |
| 428 | + $json_url = get_post_meta( $post_id, Visualizer_Plugin::CF_JSON_URL, true ); | |
| 429 | + | |
| 430 | + $json_headers = get_post_meta( $post_id, Visualizer_Plugin::CF_JSON_HEADERS, true ); | |
| 431 | + | |
| 432 | + $json_schedule = get_post_meta( $post_id, Visualizer_Plugin::CF_JSON_SCHEDULE, true ); | |
| 433 | + | |
| 434 | + $json_root = get_post_meta( $post_id, Visualizer_Plugin::CF_JSON_ROOT, true ); | |
| 435 | + | |
| 436 | + $json_paging = get_post_meta( $post_id, Visualizer_Plugin::CF_JSON_PAGING, true ); | |
| 437 | + | |
| 215 | 438 | if ( ! empty( $import ) && ! empty( $schedule ) ) { |
| 216 | 439 | $data['visualizer-chart-url'] = $import; |
| 217 | 440 | $data['visualizer-chart-schedule'] = $schedule; |
| 218 | 441 | } |
| 219 | 442 | |
| 220 | - if ( VISUALIZER_PRO ) { | |
| 443 | + if ( ! empty( $db_schedule ) && ! empty( $db_query ) ) { | |
| 444 | + $data['visualizer-db-schedule'] = $db_schedule; | |
| 445 | + $data['visualizer-db-query'] = $db_query; | |
| 446 | + } | |
| 447 | + | |
| 448 | + if ( ! empty( $json_url ) ) { | |
| 449 | + $data['visualizer-json-schedule'] = $json_schedule; | |
| 450 | + $data['visualizer-json-url'] = $json_url; | |
| 451 | + $data['visualizer-json-headers'] = $json_headers; | |
| 452 | + $data['visualizer-json-root'] = $json_root; | |
| 453 | + | |
| 454 | + if ( Visualizer_Module::is_pro() && ! empty( $json_paging ) ) { | |
| 455 | + $data['visualizer-json-paging'] = $json_paging; | |
| 456 | + } | |
| 457 | + } | |
| 458 | + | |
| 459 | + if ( Visualizer_Module::is_pro() ) { | |
| 221 | 460 | $permissions = get_post_meta( $post_id, Visualizer_PRO::CF_PERMISSIONS, true ); |
| 222 | 461 | |
| 223 | - if ( ! empty( $permissions ) ) { | |
| 224 | - $data['visualizer-permissions'] = $permissions; | |
| 462 | + if ( empty( $permissions ) ) { | |
| 463 | + $permissions = array( 'permissions' => array( | |
| 464 | + 'read' => 'all', | |
| 465 | + 'edit' => 'roles', | |
| 466 | + 'edit-specific' => array( 'administrator' ), | |
| 467 | + ), | |
| 468 | + ); | |
| 225 | 469 | } |
| 470 | + | |
| 471 | + $data['visualizer-permissions'] = $permissions; | |
| 226 | 472 | } |
| 227 | 473 | |
| 228 | 474 | return $data; |
| 229 | 475 | } |
| @@ -228,33 +474,181 @@ | ||
| 228 | 474 | return $data; |
| 229 | 475 | } |
| 230 | 476 | |
| 231 | 477 | /** |
| 478 | + * Returns the data for the query. | |
| 479 | + * | |
| 480 | + * @access public | |
| 481 | + */ | |
| 482 | + public function get_query_data( $data ) { | |
| 483 | + if ( ! current_user_can( 'edit_posts' ) ) { | |
| 484 | + return false; | |
| 485 | + } | |
| 486 | + | |
| 487 | + $source = new Visualizer_Source_Query( stripslashes( $data['query'] ) ); | |
| 488 | + $html = $source->fetch( true ); | |
| 489 | + $source->fetch( false ); | |
| 490 | + $name = $source->getSourceName(); | |
| 491 | + $series = $source->getSeries(); | |
| 492 | + $data = $source->getRawData(); | |
| 493 | + $error = ''; | |
| 494 | + if ( empty( $html ) ) { | |
| 495 | + $error = $source->get_error(); | |
| 496 | + wp_send_json_error( array( 'msg' => $error ) ); | |
| 497 | + } | |
| 498 | + wp_send_json_success( array( 'table' => $html, 'name' => $name, 'series' => $series, 'data' => $data ) ); | |
| 499 | + } | |
| 500 | + | |
| 501 | + /** | |
| 502 | + * Returns the JSON root. | |
| 503 | + * | |
| 504 | + * @access public | |
| 505 | + */ | |
| 506 | + public function get_json_root_data( $data ) { | |
| 507 | + if ( ! current_user_can( 'edit_posts' ) ) { | |
| 508 | + return false; | |
| 509 | + } | |
| 510 | + | |
| 511 | + $source = new Visualizer_Source_Json( $data ); | |
| 512 | + | |
| 513 | + $roots = $source->fetchRoots(); | |
| 514 | + if ( empty( $roots ) ) { | |
| 515 | + wp_send_json_error( array( 'msg' => $source->get_error() ) ); | |
| 516 | + } | |
| 517 | + | |
| 518 | + wp_send_json_success( array( 'url' => $data['url'], 'roots' => $roots ) ); | |
| 519 | + } | |
| 520 | + | |
| 521 | + /** | |
| 522 | + * Returns the JSON data. | |
| 523 | + * | |
| 524 | + * @access public | |
| 525 | + */ | |
| 526 | + public function get_json_data( $data ) { | |
| 527 | + if ( ! current_user_can( 'edit_posts' ) ) { | |
| 528 | + return false; | |
| 529 | + } | |
| 530 | + | |
| 531 | + $chart_id = $data['chart']; | |
| 532 | + | |
| 533 | + if ( empty( $chart_id ) ) { | |
| 534 | + wp_die(); | |
| 535 | + } | |
| 536 | + | |
| 537 | + $source = new Visualizer_Source_Json( $data ); | |
| 538 | + $source->fetch(); | |
| 539 | + $table = $source->getRawData(); | |
| 540 | + | |
| 541 | + if ( empty( $table ) ) { | |
| 542 | + wp_send_json_error( array( 'msg' => esc_html__( 'Unable to fetch data from the endpoint. Please try again.', 'visualizer' ) ) ); | |
| 543 | + } | |
| 544 | + | |
| 545 | + $table = Visualizer_Render_Layout::show( 'editor-table', $table, $chart_id, 'viz-json-table', false, false ); | |
| 546 | + wp_send_json_success( array( 'table' => $table, 'root' => $data['root'], 'url' => $data['url'], 'paging' => $source->getPaginationElements() ) ); | |
| 547 | + } | |
| 548 | + | |
| 549 | + /** | |
| 550 | + * Set the JSON data. | |
| 551 | + * | |
| 552 | + * @access public | |
| 553 | + */ | |
| 554 | + public function set_json_data( $data ) { | |
| 555 | + if ( ! current_user_can( 'edit_posts' ) ) { | |
| 556 | + return false; | |
| 557 | + } | |
| 558 | + | |
| 559 | + $source = new Visualizer_Source_Json( $data ); | |
| 560 | + | |
| 561 | + $table = $source->fetch(); | |
| 562 | + if ( empty( $table ) ) { | |
| 563 | + wp_send_json_error( array( 'msg' => esc_html__( 'Unable to fetch data from the endpoint. Please try again.', 'visualizer' ) ) ); | |
| 564 | + } | |
| 565 | + | |
| 566 | + $source->fetchFromEditableTable(); | |
| 567 | + $name = $source->getSourceName(); | |
| 568 | + $series = json_encode( $source->getSeries() ); | |
| 569 | + $data = json_encode( $source->getRawData() ); | |
| 570 | + wp_send_json_success( array( 'name' => $name, 'series' => $series, 'data' => $data ) ); | |
| 571 | + } | |
| 572 | + | |
| 573 | + /** | |
| 232 | 574 | * Rest Callback Method |
| 233 | 575 | */ |
| 234 | 576 | public function update_chart_data( $data ) { |
| 577 | + if ( ! current_user_can( 'edit_posts' ) ) { | |
| 578 | + return false; | |
| 579 | + } | |
| 580 | + | |
| 235 | 581 | if ( $data['id'] && ! is_wp_error( $data['id'] ) ) { |
| 582 | + if ( get_post_type( $data['id'] ) !== Visualizer_Plugin::CPT_VISUALIZER ) { | |
| 583 | + return new WP_Error( 'invalid_post_type', 'Invalid post type.' ); | |
| 584 | + } | |
| 585 | + $chart_type = sanitize_text_field( $data['visualizer-chart-type'] ); | |
| 586 | + $source_type = sanitize_text_field( $data['visualizer-source'] ); | |
| 236 | 587 | |
| 237 | - update_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_TYPE, $data['visualizer-chart-type'] ); | |
| 238 | - update_post_meta( $data['id'], Visualizer_Plugin::CF_SOURCE, $data['visualizer-source'] ); | |
| 588 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_TYPE, $chart_type ); | |
| 589 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_SOURCE, $source_type ); | |
| 239 | 590 | update_post_meta( $data['id'], Visualizer_Plugin::CF_DEFAULT_DATA, $data['visualizer-default-data'] ); |
| 240 | 591 | update_post_meta( $data['id'], Visualizer_Plugin::CF_SERIES, $data['visualizer-series'] ); |
| 241 | 592 | update_post_meta( $data['id'], Visualizer_Plugin::CF_SETTINGS, $data['visualizer-settings'] ); |
| 242 | 593 | |
| 243 | 594 | if ( $data['visualizer-chart-url'] && $data['visualizer-chart-schedule'] ) { |
| 244 | - update_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_URL, $data['visualizer-chart-url'] ); | |
| 245 | - apply_filters( 'visualizer_pro_chart_schedule', $data['id'], $data['visualizer-chart-url'], $data['visualizer-chart-schedule'] ); | |
| 595 | + $chart_url = esc_url_raw( $data['visualizer-chart-url'] ); | |
| 596 | + $chart_schedule = intval( $data['visualizer-chart-schedule'] ); | |
| 597 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_URL, $chart_url ); | |
| 598 | + apply_filters( 'visualizer_pro_chart_schedule', $data['id'], $chart_url, $chart_schedule ); | |
| 246 | 599 | } else { |
| 247 | 600 | delete_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_URL ); |
| 248 | 601 | apply_filters( 'visualizer_pro_remove_schedule', $data['id'] ); |
| 249 | 602 | } |
| 250 | 603 | |
| 251 | - if ( VISUALIZER_PRO ) { | |
| 604 | + // let's check if this is not an external db chart | |
| 605 | + // as there is no support for that in the block editor interface | |
| 606 | + $external_params = get_post_meta( $data['id'], Visualizer_Plugin::CF_REMOTE_DB_PARAMS, true ); | |
| 607 | + if ( empty( $external_params ) ) { | |
| 608 | + if ( $source_type === 'Visualizer_Source_Query' ) { | |
| 609 | + $db_schedule = intval( $data['visualizer-db-schedule'] ); | |
| 610 | + $db_query = $data['visualizer-db-query']; | |
| 611 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_DB_SCHEDULE, $db_schedule ); | |
| 612 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_DB_QUERY, stripslashes( $db_query ) ); | |
| 613 | + } else { | |
| 614 | + delete_post_meta( $data['id'], Visualizer_Plugin::CF_DB_SCHEDULE ); | |
| 615 | + delete_post_meta( $data['id'], Visualizer_Plugin::CF_DB_QUERY ); | |
| 616 | + } | |
| 617 | + } | |
| 618 | + | |
| 619 | + if ( $source_type === 'Visualizer_Source_Json' ) { | |
| 620 | + $json_schedule = intval( $data['visualizer-json-schedule'] ); | |
| 621 | + $json_url = esc_url_raw( $data['visualizer-json-url'] ); | |
| 622 | + $json_headers = esc_url_raw( $data['visualizer-json-headers'] ); | |
| 623 | + $json_root = $data['visualizer-json-root']; | |
| 624 | + $json_paging = $data['visualizer-json-paging']; | |
| 625 | + | |
| 626 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_SCHEDULE, $json_schedule ); | |
| 627 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_URL, $json_url ); | |
| 628 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_HEADERS, $json_headers ); | |
| 629 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_ROOT, $json_root ); | |
| 630 | + | |
| 631 | + if ( ! empty( $json_paging ) ) { | |
| 632 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_PAGING, $json_paging ); | |
| 633 | + } else { | |
| 634 | + delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_PAGING ); | |
| 635 | + } | |
| 636 | + } else { | |
| 637 | + delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_SCHEDULE ); | |
| 638 | + delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_URL ); | |
| 639 | + delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_HEADERS ); | |
| 640 | + delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_ROOT ); | |
| 641 | + delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_PAGING ); | |
| 642 | + } | |
| 643 | + | |
| 644 | + if ( Visualizer_Module::is_pro() ) { | |
| 252 | 645 | update_post_meta( $data['id'], Visualizer_PRO::CF_PERMISSIONS, $data['visualizer-permissions'] ); |
| 253 | 646 | } |
| 254 | 647 | |
| 255 | 648 | if ( $data['visualizer-chart-url'] ) { |
| 256 | - $content['source'] = $data['visualizer-chart-url']; | |
| 649 | + $chart_url = esc_url_raw( $data['visualizer-chart-url'] ); | |
| 650 | + $content['source'] = $chart_url; | |
| 257 | 651 | $content['data'] = $this->format_chart_data( $data['visualizer-data'], $data['visualizer-series'] ); |
| 258 | 652 | } else { |
| 259 | 653 | $content = $this->format_chart_data( $data['visualizer-data'], $data['visualizer-series'] ); |
| 260 | 654 | } |
| @@ -265,8 +659,14 @@ | ||
| 265 | 659 | ); |
| 266 | 660 | |
| 267 | 661 | wp_update_post( $chart ); |
| 268 | 662 | |
| 663 | + // Clear existing chart cache. | |
| 664 | + $cache_key = Visualizer_Plugin::CF_CHART_CACHE . '_' . $data['id']; | |
| 665 | + if ( get_transient( $cache_key ) ) { | |
| 666 | + delete_transient( $cache_key ); | |
| 667 | + } | |
| 668 | + | |
| 269 | 669 | $revisions = wp_get_post_revisions( $data['id'], array( 'order' => 'ASC' ) ); |
| 270 | 670 | |
| 271 | 671 | if ( count( $revisions ) > 1 ) { |
| 272 | 672 | $revision_ids = array_keys( $revisions ); |
| @@ -282,8 +682,10 @@ | ||
| 282 | 682 | } |
| 283 | 683 | |
| 284 | 684 | /** |
| 285 | 685 | * Format chart data. |
| 686 | + * | |
| 687 | + * Note: No matter how tempted, don't use the similar method from Visualizer_Source. That works on a different structure. | |
| 286 | 688 | */ |
| 287 | 689 | public function format_chart_data( $data, $series ) { |
| 288 | 690 | foreach ( $series as $i => $row ) { |
| 289 | 691 | // if no value exists for the seires, then add null |
| @@ -294,39 +696,38 @@ | ||
| 294 | 696 | if ( is_null( $series[ $i ] ) ) { |
| 295 | 697 | continue; |
| 296 | 698 | } |
| 297 | 699 | |
| 298 | - if ( $row['type'] == 'number' ) { | |
| 299 | - foreach ( $data as $o => $col ) { | |
| 300 | - $data[ $o ][ $i ] = ( is_numeric( $col[ $i ] ) ) ? floatval( $col[ $i ] ) : ( is_numeric( str_replace( ',', '', $col[ $i ] ) ) ? floatval( str_replace( ',', '', $col[ $i ] ) ) : null ); | |
| 301 | - } | |
| 302 | - } | |
| 303 | - | |
| 304 | - if ( $row['type'] == 'boolean' ) { | |
| 305 | - foreach ( $data as $o => $col ) { | |
| 306 | - $data[ $o ][ $i ] = ! empty( $col[ $i ] ) ? filter_validate( $col[ $i ], FILTER_VALIDATE_BOOLEAN ) : null; | |
| 307 | - } | |
| 308 | - } | |
| 309 | - | |
| 310 | - if ( $row['type'] == 'timeofday' ) { | |
| 311 | - foreach ( $data as $o => $col ) { | |
| 312 | - $date = new DateTime( '1984-03-16T' . $col[ $i ] ); | |
| 313 | - if ( $date ) { | |
| 314 | - $data[ $o ][ $i ] = array( | |
| 315 | - intval( $date->format( 'H' ) ), | |
| 316 | - intval( $date->format( 'i' ) ), | |
| 317 | - intval( $date->format( 's' ) ), | |
| 318 | - 0, | |
| 319 | - ); | |
| 700 | + switch ( $row['type'] ) { | |
| 701 | + case 'number': | |
| 702 | + foreach ( $data as $o => $col ) { | |
| 703 | + $data[ $o ][ $i ] = ( is_numeric( $col[ $i ] ) ) ? floatval( $col[ $i ] ) : ( is_numeric( str_replace( ',', '', $col[ $i ] ) ) ? floatval( str_replace( ',', '', $col[ $i ] ) ) : null ); | |
| 320 | 704 | } |
| 321 | - } | |
| 705 | + break; | |
| 706 | + case 'boolean': | |
| 707 | + foreach ( $data as $o => $col ) { | |
| 708 | + $data[ $o ][ $i ] = ! empty( $col[ $i ] ) ? filter_var( $col[ $i ], FILTER_VALIDATE_BOOLEAN ) : false; | |
| 709 | + } | |
| 710 | + break; | |
| 711 | + case 'timeofday': | |
| 712 | + foreach ( $data as $o => $col ) { | |
| 713 | + $date = new DateTime( '1984-03-16T' . $col[ $i ] ); | |
| 714 | + if ( $date ) { | |
| 715 | + $data[ $o ][ $i ] = array( | |
| 716 | + intval( $date->format( 'H' ) ), | |
| 717 | + intval( $date->format( 'i' ) ), | |
| 718 | + intval( $date->format( 's' ) ), | |
| 719 | + 0, | |
| 720 | + ); | |
| 721 | + } | |
| 722 | + } | |
| 723 | + break; | |
| 724 | + case 'string': | |
| 725 | + foreach ( $data as $o => $col ) { | |
| 726 | + $data[ $o ][ $i ] = $this->toUTF8( $col[ $i ] ); | |
| 727 | + } | |
| 728 | + break; | |
| 322 | 729 | } |
| 323 | - | |
| 324 | - if ( $row['type'] == 'string' ) { | |
| 325 | - foreach ( $data as $o => $col ) { | |
| 326 | - $data[ $o ][ $i ] = $this->toUTF8( $col[ $i ] ); | |
| 327 | - } | |
| 328 | - } | |
| 329 | 730 | } |
| 330 | 731 | |
| 331 | 732 | return $data; |
| 332 | 733 | } |
| @@ -344,8 +745,12 @@ | ||
| 344 | 745 | /** |
| 345 | 746 | * Handle remote CSV data |
| 346 | 747 | */ |
| 347 | 748 | public function upload_csv_data( $data ) { |
| 749 | + if ( ! current_user_can( 'edit_posts' ) ) { | |
| 750 | + return false; | |
| 751 | + } | |
| 752 | + | |
| 348 | 753 | if ( $data['url'] && ! is_wp_error( $data['url'] ) && filter_var( $data['url'], FILTER_VALIDATE_URL ) ) { |
| 349 | 754 | $source = new Visualizer_Source_Csv_Remote( $data['url'] ); |
| 350 | 755 | if ( $source->fetch() ) { |
| 351 | 756 | $temp = $source->getData(); |
| @@ -367,8 +772,12 @@ | ||
| 367 | 772 | /** |
| 368 | 773 | * Get permission data |
| 369 | 774 | */ |
| 370 | 775 | public function get_permission_data( $data ) { |
| 776 | + if ( ! current_user_can( 'edit_posts' ) ) { | |
| 777 | + return false; | |
| 778 | + } | |
| 779 | + | |
| 371 | 780 | $options = array(); |
| 372 | 781 | switch ( $data['type'] ) { |
| 373 | 782 | case 'users': |
| 374 | 783 | $query = new WP_User_Query( |
| @@ -406,5 +815,26 @@ | ||
| 406 | 815 | } |
| 407 | 816 | return $options; |
| 408 | 817 | } |
| 409 | 818 | |
| 819 | + /** | |
| 820 | + * Filter Rest Query | |
| 821 | + */ | |
| 822 | + public function add_rest_query_vars( $args, \WP_REST_Request $request ) { | |
| 823 | + if ( isset( $request['meta_key'] ) && isset( $request['meta_value'] ) ) { | |
| 824 | + $args['meta_query'] = array( | |
| 825 | + 'relation' => 'OR', | |
| 826 | + array( | |
| 827 | + 'key' => $request->get_param( 'meta_key' ), | |
| 828 | + 'value' => $request->get_param( 'meta_value' ), | |
| 829 | + 'compare' => '!=', | |
| 830 | + ), | |
| 831 | + array( | |
| 832 | + 'key' => $request->get_param( 'meta_key' ), | |
| 833 | + 'value' => $request->get_param( 'meta_value' ), | |
| 834 | + 'compare' => 'NOT EXISTS', | |
| 835 | + ), | |
| 836 | + ); | |
| 837 | + } | |
| 838 | + return $args; | |
| 839 | + } | |
| 410 | 840 | } |