| @@ -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,8 +66,10 @@ | ||
| 65 | 66 | /** |
| 66 | 67 | * Enqueue front end and editor JavaScript and CSS |
| 67 | 68 | */ |
| 68 | 69 | public function enqueue_gutenberg_scripts() { |
| 70 | + global $wp_version; | |
| 71 | + | |
| 69 | 72 | $blockPath = VISUALIZER_ABSURL . 'classes/Visualizer/Gutenberg/build/block.js'; |
| 70 | 73 | $handsontableJS = VISUALIZER_ABSURL . 'classes/Visualizer/Gutenberg/build/handsontable.js'; |
| 71 | 74 | $stylePath = VISUALIZER_ABSURL . 'classes/Visualizer/Gutenberg/build/block.css'; |
| 72 | 75 | $handsontableCSS = VISUALIZER_ABSURL . 'classes/Visualizer/Gutenberg/build/handsontable.css'; |
| @@ -71,9 +74,9 @@ | ||
| 71 | 74 | $stylePath = VISUALIZER_ABSURL . 'classes/Visualizer/Gutenberg/build/block.css'; |
| 72 | 75 | $handsontableCSS = VISUALIZER_ABSURL . 'classes/Visualizer/Gutenberg/build/handsontable.css'; |
| 73 | 76 | |
| 74 | 77 | if ( VISUALIZER_TEST_JS_CUSTOMIZATION ) { |
| 75 | - $version = filemtime( VISUALIZER_ABSPATH . 'classes/Visualizer/Gutenberg/build/block.js' ); | |
| 78 | + $version = filemtime( VISUALIZER_ABSPATH . '/classes/Visualizer/Gutenberg/build/block.js' ); | |
| 76 | 79 | } else { |
| 77 | 80 | $version = $this->version; |
| 78 | 81 | } |
| 79 | 82 | |
| @@ -78,29 +81,53 @@ | ||
| 78 | 81 | } |
| 79 | 82 | |
| 80 | 83 | // Enqueue the bundled block JS file |
| 81 | 84 | wp_enqueue_script( 'handsontable', $handsontableJS ); |
| 82 | - wp_enqueue_script( 'visualizer-gutenberg-block', $blockPath, array( 'wp-api', 'handsontable' ), $version, true ); | |
| 85 | + wp_enqueue_script( 'visualizer-gutenberg-block', $blockPath, array( 'wp-api', 'handsontable', 'visualizer-datatables', 'moment' ), $version, true ); | |
| 83 | 86 | |
| 84 | 87 | $type = 'community'; |
| 85 | 88 | |
| 86 | - if ( VISUALIZER_PRO ) { | |
| 89 | + if ( Visualizer_Module::is_pro() ) { | |
| 87 | 90 | $type = 'pro'; |
| 88 | 91 | if ( apply_filters( 'visualizer_is_business', false ) ) { |
| 89 | - $type = 'developer'; | |
| 92 | + $type = 'business'; | |
| 90 | 93 | } |
| 91 | 94 | } |
| 92 | 95 | |
| 96 | + $table_col_mapping = Visualizer_Source_Query_Params::get_all_db_tables_column_mapping( null, false ); | |
| 97 | + | |
| 93 | 98 | $translation_array = array( |
| 94 | 99 | 'isPro' => $type, |
| 95 | 100 | 'proTeaser' => Visualizer_Plugin::PRO_TEASER_URL, |
| 96 | 101 | 'absurl' => VISUALIZER_ABSURL, |
| 102 | + 'charts' => Visualizer_Module_Admin::_getChartTypesLocalized(), | |
| 103 | + 'adminPage' => menu_page_url( 'visualizer', false ), | |
| 104 | + 'sqlTable' => $table_col_mapping, | |
| 105 | + 'chartsPerPage' => defined( 'TI_CYPRESS_TESTING' ) ? 20 : 6, | |
| 97 | 106 | ); |
| 98 | 107 | wp_localize_script( 'visualizer-gutenberg-block', 'visualizerLocalize', $translation_array ); |
| 99 | 108 | |
| 100 | 109 | // Enqueue frontend and editor block styles |
| 101 | 110 | wp_enqueue_style( 'handsontable', $handsontableCSS ); |
| 102 | - wp_enqueue_style( 'visualizer-gutenberg-block', $stylePath, '', $version ); | |
| 111 | + wp_enqueue_style( 'visualizer-gutenberg-block', $stylePath, array( 'visualizer-datatables' ), $version ); | |
| 112 | + | |
| 113 | + if ( version_compare( $wp_version, '4.9.0', '>' ) ) { | |
| 114 | + | |
| 115 | + wp_enqueue_code_editor( | |
| 116 | + array( | |
| 117 | + 'type' => 'sql', | |
| 118 | + 'codemirror' => array( | |
| 119 | + 'autofocus' => true, | |
| 120 | + 'lineWrapping' => true, | |
| 121 | + 'dragDrop' => false, | |
| 122 | + 'matchBrackets' => true, | |
| 123 | + 'autoCloseBrackets' => true, | |
| 124 | + 'extraKeys' => array( 'Ctrl-Space' => 'autocomplete' ), | |
| 125 | + 'hintOptions' => array( 'tables' => $table_col_mapping ), | |
| 126 | + ), | |
| 127 | + ) | |
| 128 | + ); | |
| 129 | + } | |
| 103 | 130 | } |
| 104 | 131 | /** |
| 105 | 132 | * Hook server side rendering into render callback |
| 106 | 133 | */ |
| @@ -143,8 +170,74 @@ | ||
| 143 | 170 | ); |
| 144 | 171 | |
| 145 | 172 | register_rest_route( |
| 146 | 173 | 'visualizer/v' . VISUALIZER_REST_VERSION, |
| 174 | + '/get-query-data', | |
| 175 | + array( | |
| 176 | + 'methods' => 'GET', | |
| 177 | + 'callback' => array( $this, 'get_query_data' ), | |
| 178 | + 'permission_callback' => function () { | |
| 179 | + return current_user_can( 'edit_posts' ); | |
| 180 | + }, | |
| 181 | + ) | |
| 182 | + ); | |
| 183 | + | |
| 184 | + register_rest_route( | |
| 185 | + 'visualizer/v' . VISUALIZER_REST_VERSION, | |
| 186 | + '/get-json-root', | |
| 187 | + array( | |
| 188 | + 'methods' => 'GET', | |
| 189 | + 'callback' => array( $this, 'get_json_root_data' ), | |
| 190 | + 'args' => array( | |
| 191 | + 'url' => array( | |
| 192 | + 'sanitize_callback' => 'esc_url_raw', | |
| 193 | + ), | |
| 194 | + ), | |
| 195 | + 'permission_callback' => function () { | |
| 196 | + return current_user_can( 'edit_posts' ); | |
| 197 | + }, | |
| 198 | + ) | |
| 199 | + ); | |
| 200 | + | |
| 201 | + register_rest_route( | |
| 202 | + 'visualizer/v' . VISUALIZER_REST_VERSION, | |
| 203 | + '/get-json-data', | |
| 204 | + array( | |
| 205 | + 'methods' => 'GET', | |
| 206 | + 'callback' => array( $this, 'get_json_data' ), | |
| 207 | + 'args' => array( | |
| 208 | + 'url' => array( | |
| 209 | + 'sanitize_callback' => 'esc_url_raw', | |
| 210 | + ), | |
| 211 | + 'chart' => array( | |
| 212 | + 'sanitize_callback' => 'absint', | |
| 213 | + ), | |
| 214 | + ), | |
| 215 | + 'permission_callback' => function () { | |
| 216 | + return current_user_can( 'edit_posts' ); | |
| 217 | + }, | |
| 218 | + ) | |
| 219 | + ); | |
| 220 | + | |
| 221 | + register_rest_route( | |
| 222 | + 'visualizer/v' . VISUALIZER_REST_VERSION, | |
| 223 | + '/set-json-data', | |
| 224 | + array( | |
| 225 | + 'methods' => 'GET', | |
| 226 | + 'callback' => array( $this, 'set_json_data' ), | |
| 227 | + 'args' => array( | |
| 228 | + 'url' => array( | |
| 229 | + 'sanitize_callback' => 'esc_url_raw', | |
| 230 | + ), | |
| 231 | + ), | |
| 232 | + 'permission_callback' => function () { | |
| 233 | + return current_user_can( 'edit_posts' ); | |
| 234 | + }, | |
| 235 | + ) | |
| 236 | + ); | |
| 237 | + | |
| 238 | + register_rest_route( | |
| 239 | + 'visualizer/v' . VISUALIZER_REST_VERSION, | |
| 147 | 240 | '/update-chart', |
| 148 | 241 | array( |
| 149 | 242 | 'methods' => 'POST', |
| 150 | 243 | 'callback' => array( $this, 'update_chart_data' ), |
| @@ -152,8 +245,11 @@ | ||
| 152 | 245 | 'id' => array( |
| 153 | 246 | 'sanitize_callback' => 'absint', |
| 154 | 247 | ), |
| 155 | 248 | ), |
| 249 | + 'permission_callback' => function () { | |
| 250 | + return current_user_can( 'edit_posts' ); | |
| 251 | + }, | |
| 156 | 252 | ) |
| 157 | 253 | ); |
| 158 | 254 | |
| 159 | 255 | register_rest_route( |
| @@ -166,8 +262,11 @@ | ||
| 166 | 262 | 'url' => array( |
| 167 | 263 | 'sanitize_callback' => 'esc_url_raw', |
| 168 | 264 | ), |
| 169 | 265 | ), |
| 266 | + 'permission_callback' => function () { | |
| 267 | + return current_user_can( 'edit_posts' ); | |
| 268 | + }, | |
| 170 | 269 | ) |
| 171 | 270 | ); |
| 172 | 271 | |
| 173 | 272 | register_rest_route( |
| @@ -180,8 +279,11 @@ | ||
| 180 | 279 | 'type' => array( |
| 181 | 280 | 'sanitize_callback' => 'sanitize_text_field', |
| 182 | 281 | ), |
| 183 | 282 | ), |
| 283 | + 'permission_callback' => function () { | |
| 284 | + return current_user_can( 'edit_posts' ); | |
| 285 | + }, | |
| 184 | 286 | ) |
| 185 | 287 | ); |
| 186 | 288 | } |
| 187 | 289 | |
| @@ -188,13 +290,20 @@ | ||
| 188 | 290 | /** |
| 189 | 291 | * Get Post Meta Fields |
| 190 | 292 | */ |
| 191 | 293 | public function get_visualizer_data( $post ) { |
| 294 | + if ( ! current_user_can( 'edit_posts' ) ) { | |
| 295 | + return false; | |
| 296 | + } | |
| 297 | + | |
| 192 | 298 | $data = array(); |
| 193 | 299 | $post_id = $post['id']; |
| 194 | 300 | |
| 195 | 301 | $data['visualizer-chart-type'] = get_post_meta( $post_id, Visualizer_Plugin::CF_CHART_TYPE, true ); |
| 196 | 302 | |
| 303 | + $library = get_post_meta( $post_id, Visualizer_Plugin::CF_CHART_LIBRARY, true ); | |
| 304 | + $data['visualizer-chart-library'] = $library; | |
| 305 | + | |
| 197 | 306 | $data['visualizer-source'] = get_post_meta( $post_id, Visualizer_Plugin::CF_SOURCE, true ); |
| 198 | 307 | |
| 199 | 308 | $data['visualizer-default-data'] = get_post_meta( $post_id, Visualizer_Plugin::CF_DEFAULT_DATA, true ); |
| 200 | 309 | |
| @@ -209,18 +318,94 @@ | ||
| 209 | 318 | |
| 210 | 319 | // handle data filter hooks |
| 211 | 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'] ); |
| 212 | 321 | |
| 322 | + $data['visualizer-data-exploded'] = ''; | |
| 323 | + // handle annotations for google charts | |
| 324 | + if ( 'GoogleCharts' === $library ) { | |
| 325 | + // this will contain the data of both axis. | |
| 326 | + $settings = $data['visualizer-settings']; | |
| 327 | + // this will contain data only of Y axis. | |
| 328 | + $series = $data['visualizer-series']; | |
| 329 | + $annotations = array(); | |
| 330 | + if ( isset( $settings['series'] ) ) { | |
| 331 | + foreach ( $settings['series'] as $index => $serie ) { | |
| 332 | + // skip X axis data. | |
| 333 | + if ( $index === 0 ) { | |
| 334 | + continue; | |
| 335 | + } | |
| 336 | + if ( ! empty( $serie['role'] ) ) { | |
| 337 | + // this series is some kind of annotation, so let's collect its index. | |
| 338 | + // the index will be +1 because the X axis value is index 0, which is being ignored. | |
| 339 | + $annotations[ 'role' . ( intval( $index ) + 1 ) ] = $serie['role']; | |
| 340 | + } | |
| 341 | + } | |
| 342 | + } | |
| 343 | + if ( ! empty( $annotations ) ) { | |
| 344 | + $exploded_data = array(); | |
| 345 | + $series_names = array(); | |
| 346 | + foreach ( $series as $index => $serie ) { | |
| 347 | + // skip X axis data. | |
| 348 | + if ( $index === 0 ) { | |
| 349 | + continue; | |
| 350 | + } | |
| 351 | + if ( array_key_exists( 'role' . $index, $annotations ) ) { | |
| 352 | + $series_names[] = (object) array( 'role' => $annotations[ 'role' . $index ], 'type' => $serie['type'] ); | |
| 353 | + } else { | |
| 354 | + $series_names[] = $serie['label']; | |
| 355 | + } | |
| 356 | + } | |
| 357 | + $exploded_data[] = $series_names; | |
| 358 | + | |
| 359 | + foreach ( $data['visualizer-data'] as $datum ) { | |
| 360 | + // skip X axis data. | |
| 361 | + unset( $datum[0] ); | |
| 362 | + $exploded_data[] = $datum; | |
| 363 | + } | |
| 364 | + $data['visualizer-data-exploded'] = array( $exploded_data ); | |
| 365 | + } | |
| 366 | + } | |
| 367 | + | |
| 213 | 368 | $import = get_post_meta( $post_id, Visualizer_Plugin::CF_CHART_URL, true ); |
| 214 | 369 | |
| 215 | 370 | $schedule = get_post_meta( $post_id, Visualizer_Plugin::CF_CHART_SCHEDULE, true ); |
| 216 | 371 | |
| 372 | + $db_schedule = get_post_meta( $post_id, Visualizer_Plugin::CF_DB_SCHEDULE, true ); | |
| 373 | + | |
| 374 | + $db_query = get_post_meta( $post_id, Visualizer_Plugin::CF_DB_QUERY, true ); | |
| 375 | + | |
| 376 | + $json_url = get_post_meta( $post_id, Visualizer_Plugin::CF_JSON_URL, true ); | |
| 377 | + | |
| 378 | + $json_headers = get_post_meta( $post_id, Visualizer_Plugin::CF_JSON_HEADERS, true ); | |
| 379 | + | |
| 380 | + $json_schedule = get_post_meta( $post_id, Visualizer_Plugin::CF_JSON_SCHEDULE, true ); | |
| 381 | + | |
| 382 | + $json_root = get_post_meta( $post_id, Visualizer_Plugin::CF_JSON_ROOT, true ); | |
| 383 | + | |
| 384 | + $json_paging = get_post_meta( $post_id, Visualizer_Plugin::CF_JSON_PAGING, true ); | |
| 385 | + | |
| 217 | 386 | if ( ! empty( $import ) && ! empty( $schedule ) ) { |
| 218 | 387 | $data['visualizer-chart-url'] = $import; |
| 219 | 388 | $data['visualizer-chart-schedule'] = $schedule; |
| 220 | 389 | } |
| 221 | 390 | |
| 222 | - if ( VISUALIZER_PRO ) { | |
| 391 | + if ( ! empty( $db_schedule ) && ! empty( $db_query ) ) { | |
| 392 | + $data['visualizer-db-schedule'] = $db_schedule; | |
| 393 | + $data['visualizer-db-query'] = $db_query; | |
| 394 | + } | |
| 395 | + | |
| 396 | + if ( ! empty( $json_url ) ) { | |
| 397 | + $data['visualizer-json-schedule'] = $json_schedule; | |
| 398 | + $data['visualizer-json-url'] = $json_url; | |
| 399 | + $data['visualizer-json-headers'] = $json_headers; | |
| 400 | + $data['visualizer-json-root'] = $json_root; | |
| 401 | + | |
| 402 | + if ( Visualizer_Module::is_pro() && ! empty( $json_paging ) ) { | |
| 403 | + $data['visualizer-json-paging'] = $json_paging; | |
| 404 | + } | |
| 405 | + } | |
| 406 | + | |
| 407 | + if ( Visualizer_Module::is_pro() ) { | |
| 223 | 408 | $permissions = get_post_meta( $post_id, Visualizer_PRO::CF_PERMISSIONS, true ); |
| 224 | 409 | |
| 225 | 410 | if ( ! empty( $permissions ) ) { |
| 226 | 411 | $data['visualizer-permissions'] = $permissions; |
| @@ -230,33 +415,174 @@ | ||
| 230 | 415 | return $data; |
| 231 | 416 | } |
| 232 | 417 | |
| 233 | 418 | /** |
| 419 | + * Returns the data for the query. | |
| 420 | + * | |
| 421 | + * @access public | |
| 422 | + */ | |
| 423 | + public function get_query_data( $data ) { | |
| 424 | + if ( ! current_user_can( 'edit_posts' ) ) { | |
| 425 | + return false; | |
| 426 | + } | |
| 427 | + | |
| 428 | + $source = new Visualizer_Source_Query( stripslashes( $data['query'] ) ); | |
| 429 | + $html = $source->fetch( true ); | |
| 430 | + $source->fetch( false ); | |
| 431 | + $name = $source->getSourceName(); | |
| 432 | + $series = $source->getSeries(); | |
| 433 | + $data = $source->getRawData(); | |
| 434 | + $error = ''; | |
| 435 | + if ( empty( $html ) ) { | |
| 436 | + $error = $source->get_error(); | |
| 437 | + wp_send_json_error( array( 'msg' => $error ) ); | |
| 438 | + } | |
| 439 | + wp_send_json_success( array( 'table' => $html, 'name' => $name, 'series' => $series, 'data' => $data ) ); | |
| 440 | + } | |
| 441 | + | |
| 442 | + /** | |
| 443 | + * Returns the JSON root. | |
| 444 | + * | |
| 445 | + * @access public | |
| 446 | + */ | |
| 447 | + public function get_json_root_data( $data ) { | |
| 448 | + if ( ! current_user_can( 'edit_posts' ) ) { | |
| 449 | + return false; | |
| 450 | + } | |
| 451 | + | |
| 452 | + $source = new Visualizer_Source_Json( $data ); | |
| 453 | + | |
| 454 | + $roots = $source->fetchRoots(); | |
| 455 | + if ( empty( $roots ) ) { | |
| 456 | + wp_send_json_error( array( 'msg' => $source->get_error() ) ); | |
| 457 | + } | |
| 458 | + | |
| 459 | + wp_send_json_success( array( 'url' => $data['url'], 'roots' => $roots ) ); | |
| 460 | + } | |
| 461 | + | |
| 462 | + /** | |
| 463 | + * Returns the JSON data. | |
| 464 | + * | |
| 465 | + * @access public | |
| 466 | + */ | |
| 467 | + public function get_json_data( $data ) { | |
| 468 | + if ( ! current_user_can( 'edit_posts' ) ) { | |
| 469 | + return false; | |
| 470 | + } | |
| 471 | + | |
| 472 | + $chart_id = $data['chart']; | |
| 473 | + | |
| 474 | + if ( empty( $chart_id ) ) { | |
| 475 | + wp_die(); | |
| 476 | + } | |
| 477 | + | |
| 478 | + $source = new Visualizer_Source_Json( $data ); | |
| 479 | + $source->fetch(); | |
| 480 | + $table = $source->getRawData(); | |
| 481 | + | |
| 482 | + if ( empty( $table ) ) { | |
| 483 | + wp_send_json_error( array( 'msg' => esc_html__( 'Unable to fetch data from the endpoint. Please try again.', 'visualizer' ) ) ); | |
| 484 | + } | |
| 485 | + | |
| 486 | + $table = Visualizer_Render_Layout::show( 'editor-table', $table, $chart_id, 'viz-json-table', false, false ); | |
| 487 | + wp_send_json_success( array( 'table' => $table, 'root' => $data['root'], 'url' => $data['url'], 'paging' => $source->getPaginationElements() ) ); | |
| 488 | + } | |
| 489 | + | |
| 490 | + /** | |
| 491 | + * Set the JSON data. | |
| 492 | + * | |
| 493 | + * @access public | |
| 494 | + */ | |
| 495 | + public function set_json_data( $data ) { | |
| 496 | + if ( ! current_user_can( 'edit_posts' ) ) { | |
| 497 | + return false; | |
| 498 | + } | |
| 499 | + | |
| 500 | + $source = new Visualizer_Source_Json( $data ); | |
| 501 | + | |
| 502 | + $table = $source->fetch(); | |
| 503 | + if ( empty( $table ) ) { | |
| 504 | + wp_send_json_error( array( 'msg' => esc_html__( 'Unable to fetch data from the endpoint. Please try again.', 'visualizer' ) ) ); | |
| 505 | + } | |
| 506 | + | |
| 507 | + $source->fetchFromEditableTable(); | |
| 508 | + $name = $source->getSourceName(); | |
| 509 | + $series = json_encode( $source->getSeries() ); | |
| 510 | + $data = json_encode( $source->getRawData() ); | |
| 511 | + wp_send_json_success( array( 'name' => $name, 'series' => $series, 'data' => $data ) ); | |
| 512 | + } | |
| 513 | + | |
| 514 | + /** | |
| 234 | 515 | * Rest Callback Method |
| 235 | 516 | */ |
| 236 | 517 | public function update_chart_data( $data ) { |
| 518 | + if ( ! current_user_can( 'edit_posts' ) ) { | |
| 519 | + return false; | |
| 520 | + } | |
| 521 | + | |
| 237 | 522 | if ( $data['id'] && ! is_wp_error( $data['id'] ) ) { |
| 238 | 523 | |
| 239 | - update_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_TYPE, $data['visualizer-chart-type'] ); | |
| 240 | - update_post_meta( $data['id'], Visualizer_Plugin::CF_SOURCE, $data['visualizer-source'] ); | |
| 524 | + $chart_type = sanitize_text_field( $data['visualizer-chart-type'] ); | |
| 525 | + $source_type = sanitize_text_field( $data['visualizer-source'] ); | |
| 526 | + | |
| 527 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_TYPE, $chart_type ); | |
| 528 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_SOURCE, $source_type ); | |
| 241 | 529 | update_post_meta( $data['id'], Visualizer_Plugin::CF_DEFAULT_DATA, $data['visualizer-default-data'] ); |
| 242 | 530 | update_post_meta( $data['id'], Visualizer_Plugin::CF_SERIES, $data['visualizer-series'] ); |
| 243 | 531 | update_post_meta( $data['id'], Visualizer_Plugin::CF_SETTINGS, $data['visualizer-settings'] ); |
| 244 | 532 | |
| 245 | 533 | if ( $data['visualizer-chart-url'] && $data['visualizer-chart-schedule'] ) { |
| 246 | - update_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_URL, $data['visualizer-chart-url'] ); | |
| 247 | - apply_filters( 'visualizer_pro_chart_schedule', $data['id'], $data['visualizer-chart-url'], $data['visualizer-chart-schedule'] ); | |
| 534 | + $chart_url = esc_url_raw( $data['visualizer-chart-url'] ); | |
| 535 | + $chart_schedule = intval( $data['visualizer-chart-schedule'] ); | |
| 536 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_URL, $chart_url ); | |
| 537 | + apply_filters( 'visualizer_pro_chart_schedule', $data['id'], $chart_url, $chart_schedule ); | |
| 248 | 538 | } else { |
| 249 | 539 | delete_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_URL ); |
| 250 | 540 | apply_filters( 'visualizer_pro_remove_schedule', $data['id'] ); |
| 251 | 541 | } |
| 252 | 542 | |
| 253 | - if ( VISUALIZER_PRO ) { | |
| 543 | + if ( $source_type === 'Visualizer_Source_Query' ) { | |
| 544 | + $db_schedule = intval( $data['visualizer-db-schedule'] ); | |
| 545 | + $db_query = $data['visualizer-db-query']; | |
| 546 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_DB_SCHEDULE, $db_schedule ); | |
| 547 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_DB_QUERY, stripslashes( $db_query ) ); | |
| 548 | + } else { | |
| 549 | + delete_post_meta( $data['id'], Visualizer_Plugin::CF_DB_SCHEDULE ); | |
| 550 | + delete_post_meta( $data['id'], Visualizer_Plugin::CF_DB_QUERY ); | |
| 551 | + } | |
| 552 | + | |
| 553 | + if ( $source_type === 'Visualizer_Source_Json' ) { | |
| 554 | + $json_schedule = intval( $data['visualizer-json-schedule'] ); | |
| 555 | + $json_url = esc_url_raw( $data['visualizer-json-url'] ); | |
| 556 | + $json_headers = esc_url_raw( $data['visualizer-json-headers'] ); | |
| 557 | + $json_root = $data['visualizer-json-root']; | |
| 558 | + $json_paging = $data['visualizer-json-paging']; | |
| 559 | + | |
| 560 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_SCHEDULE, $json_schedule ); | |
| 561 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_URL, $json_url ); | |
| 562 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_HEADERS, $json_headers ); | |
| 563 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_ROOT, $json_root ); | |
| 564 | + | |
| 565 | + if ( ! empty( $json_paging ) ) { | |
| 566 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_PAGING, $json_paging ); | |
| 567 | + } else { | |
| 568 | + delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_PAGING ); | |
| 569 | + } | |
| 570 | + } else { | |
| 571 | + delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_SCHEDULE ); | |
| 572 | + delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_URL ); | |
| 573 | + delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_HEADERS ); | |
| 574 | + delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_ROOT ); | |
| 575 | + delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_PAGING ); | |
| 576 | + } | |
| 577 | + | |
| 578 | + if ( Visualizer_Module::is_pro() ) { | |
| 254 | 579 | update_post_meta( $data['id'], Visualizer_PRO::CF_PERMISSIONS, $data['visualizer-permissions'] ); |
| 255 | 580 | } |
| 256 | 581 | |
| 257 | 582 | if ( $data['visualizer-chart-url'] ) { |
| 258 | - $content['source'] = $data['visualizer-chart-url']; | |
| 583 | + $chart_url = esc_url_raw( $data['visualizer-chart-url'] ); | |
| 584 | + $content['source'] = $chart_url; | |
| 259 | 585 | $content['data'] = $this->format_chart_data( $data['visualizer-data'], $data['visualizer-series'] ); |
| 260 | 586 | } else { |
| 261 | 587 | $content = $this->format_chart_data( $data['visualizer-data'], $data['visualizer-series'] ); |
| 262 | 588 | } |
| @@ -296,21 +622,21 @@ | ||
| 296 | 622 | if ( is_null( $series[ $i ] ) ) { |
| 297 | 623 | continue; |
| 298 | 624 | } |
| 299 | 625 | |
| 300 | - if ( $row['type'] == 'number' ) { | |
| 626 | + if ( $row['type'] === 'number' ) { | |
| 301 | 627 | foreach ( $data as $o => $col ) { |
| 302 | 628 | $data[ $o ][ $i ] = ( is_numeric( $col[ $i ] ) ) ? floatval( $col[ $i ] ) : ( is_numeric( str_replace( ',', '', $col[ $i ] ) ) ? floatval( str_replace( ',', '', $col[ $i ] ) ) : null ); |
| 303 | 629 | } |
| 304 | 630 | } |
| 305 | 631 | |
| 306 | - if ( $row['type'] == 'boolean' ) { | |
| 632 | + if ( $row['type'] === 'boolean' ) { | |
| 307 | 633 | foreach ( $data as $o => $col ) { |
| 308 | 634 | $data[ $o ][ $i ] = ! empty( $col[ $i ] ) ? filter_validate( $col[ $i ], FILTER_VALIDATE_BOOLEAN ) : null; |
| 309 | 635 | } |
| 310 | 636 | } |
| 311 | 637 | |
| 312 | - if ( $row['type'] == 'timeofday' ) { | |
| 638 | + if ( $row['type'] === 'timeofday' ) { | |
| 313 | 639 | foreach ( $data as $o => $col ) { |
| 314 | 640 | $date = new DateTime( '1984-03-16T' . $col[ $i ] ); |
| 315 | 641 | if ( $date ) { |
| 316 | 642 | $data[ $o ][ $i ] = array( |
| @@ -322,9 +648,9 @@ | ||
| 322 | 648 | } |
| 323 | 649 | } |
| 324 | 650 | } |
| 325 | 651 | |
| 326 | - if ( $row['type'] == 'string' ) { | |
| 652 | + if ( $row['type'] === 'string' ) { | |
| 327 | 653 | foreach ( $data as $o => $col ) { |
| 328 | 654 | $data[ $o ][ $i ] = $this->toUTF8( $col[ $i ] ); |
| 329 | 655 | } |
| 330 | 656 | } |
| @@ -346,8 +672,12 @@ | ||
| 346 | 672 | /** |
| 347 | 673 | * Handle remote CSV data |
| 348 | 674 | */ |
| 349 | 675 | public function upload_csv_data( $data ) { |
| 676 | + if ( ! current_user_can( 'edit_posts' ) ) { | |
| 677 | + return false; | |
| 678 | + } | |
| 679 | + | |
| 350 | 680 | if ( $data['url'] && ! is_wp_error( $data['url'] ) && filter_var( $data['url'], FILTER_VALIDATE_URL ) ) { |
| 351 | 681 | $source = new Visualizer_Source_Csv_Remote( $data['url'] ); |
| 352 | 682 | if ( $source->fetch() ) { |
| 353 | 683 | $temp = $source->getData(); |
| @@ -369,8 +699,12 @@ | ||
| 369 | 699 | /** |
| 370 | 700 | * Get permission data |
| 371 | 701 | */ |
| 372 | 702 | public function get_permission_data( $data ) { |
| 703 | + if ( ! current_user_can( 'edit_posts' ) ) { | |
| 704 | + return false; | |
| 705 | + } | |
| 706 | + | |
| 373 | 707 | $options = array(); |
| 374 | 708 | switch ( $data['type'] ) { |
| 375 | 709 | case 'users': |
| 376 | 710 | $query = new WP_User_Query( |
| @@ -408,5 +742,26 @@ | ||
| 408 | 742 | } |
| 409 | 743 | return $options; |
| 410 | 744 | } |
| 411 | 745 | |
| 746 | + /** | |
| 747 | + * Filter Rest Query | |
| 748 | + */ | |
| 749 | + public function add_rest_query_vars( $args, \WP_REST_Request $request ) { | |
| 750 | + if ( isset( $request['meta_key'] ) && isset( $request['meta_value'] ) ) { | |
| 751 | + $args['meta_query'] = array( | |
| 752 | + 'relation' => 'OR', | |
| 753 | + array( | |
| 754 | + 'key' => $request->get_param( 'meta_key' ), | |
| 755 | + 'value' => $request->get_param( 'meta_value' ), | |
| 756 | + 'compare' => '!=', | |
| 757 | + ), | |
| 758 | + array( | |
| 759 | + 'key' => $request->get_param( 'meta_key' ), | |
| 760 | + 'value' => $request->get_param( 'meta_value' ), | |
| 761 | + 'compare' => 'NOT EXISTS', | |
| 762 | + ), | |
| 763 | + ); | |
| 764 | + } | |
| 765 | + return $args; | |
| 766 | + } | |
| 412 | 767 | } |