| @@ -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 | */ |
| @@ -111,8 +138,11 @@ | ||
| 111 | 138 | 'attributes' => array( |
| 112 | 139 | 'id' => array( |
| 113 | 140 | 'type' => 'number', |
| 114 | 141 | ), |
| 142 | + 'lazy' => array( | |
| 143 | + 'type' => 'string', | |
| 144 | + ), | |
| 115 | 145 | ), |
| 116 | 146 | ) |
| 117 | 147 | ); |
| 118 | 148 | } |
| @@ -119,16 +149,46 @@ | ||
| 119 | 149 | |
| 120 | 150 | /** |
| 121 | 151 | * Gutenberg Block Callback Function |
| 122 | 152 | */ |
| 123 | - public function gutenberg_block_callback( $attr ) { | |
| 124 | - if ( isset( $attr['id'] ) ) { | |
| 125 | - $id = $attr['id']; | |
| 126 | - if ( empty( $id ) || $id === 'none' ) { | |
| 127 | - return ''; // no id = no fun | |
| 128 | - } | |
| 129 | - return '[visualizer id="' . $id . '"]'; | |
| 153 | + public function gutenberg_block_callback( $atts ) { | |
| 154 | + // no id, no fun. | |
| 155 | + if ( ! isset( $atts['id'] ) ) { | |
| 156 | + return ''; | |
| 130 | 157 | } |
| 158 | + | |
| 159 | + $atts = shortcode_atts( | |
| 160 | + array( | |
| 161 | + 'id' => false, | |
| 162 | + 'lazy' => apply_filters( 'visualizer_lazy_by_default', false, $atts['id'] ), | |
| 163 | + // we are deliberating excluding the class attribute from here | |
| 164 | + // as this will be handled by the custom class in Gutenberg | |
| 165 | + ), | |
| 166 | + $atts | |
| 167 | + ); | |
| 168 | + | |
| 169 | + // no id, no fun. | |
| 170 | + if ( ! $atts['id'] ) { | |
| 171 | + return ''; | |
| 172 | + } | |
| 173 | + | |
| 174 | + // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison | |
| 175 | + if ( $atts['lazy'] == -1 || $atts['lazy'] == false ) { | |
| 176 | + $atts['lazy'] = 'no'; | |
| 177 | + } | |
| 178 | + | |
| 179 | + // we don't want the chart in the editor lazy-loading. | |
| 180 | + if ( is_admin() ) { | |
| 181 | + unset( $atts['lazy'] ); | |
| 182 | + } | |
| 183 | + | |
| 184 | + $shortcode = '[visualizer'; | |
| 185 | + foreach ( $atts as $name => $value ) { | |
| 186 | + $shortcode .= sprintf( ' %s="%s"', $name, $value ); | |
| 187 | + } | |
| 188 | + $shortcode .= ']'; | |
| 189 | + | |
| 190 | + return $shortcode; | |
| 131 | 191 | } |
| 132 | 192 | |
| 133 | 193 | /** |
| 134 | 194 | * Hook server side rendering into render callback |
| @@ -143,8 +203,74 @@ | ||
| 143 | 203 | ); |
| 144 | 204 | |
| 145 | 205 | register_rest_route( |
| 146 | 206 | 'visualizer/v' . VISUALIZER_REST_VERSION, |
| 207 | + '/get-query-data', | |
| 208 | + array( | |
| 209 | + 'methods' => 'GET', | |
| 210 | + 'callback' => array( $this, 'get_query_data' ), | |
| 211 | + 'permission_callback' => function () { | |
| 212 | + return current_user_can( 'edit_posts' ); | |
| 213 | + }, | |
| 214 | + ) | |
| 215 | + ); | |
| 216 | + | |
| 217 | + register_rest_route( | |
| 218 | + 'visualizer/v' . VISUALIZER_REST_VERSION, | |
| 219 | + '/get-json-root', | |
| 220 | + array( | |
| 221 | + 'methods' => 'GET', | |
| 222 | + 'callback' => array( $this, 'get_json_root_data' ), | |
| 223 | + 'args' => array( | |
| 224 | + 'url' => array( | |
| 225 | + 'sanitize_callback' => 'esc_url_raw', | |
| 226 | + ), | |
| 227 | + ), | |
| 228 | + 'permission_callback' => function () { | |
| 229 | + return current_user_can( 'edit_posts' ); | |
| 230 | + }, | |
| 231 | + ) | |
| 232 | + ); | |
| 233 | + | |
| 234 | + register_rest_route( | |
| 235 | + 'visualizer/v' . VISUALIZER_REST_VERSION, | |
| 236 | + '/get-json-data', | |
| 237 | + array( | |
| 238 | + 'methods' => 'GET', | |
| 239 | + 'callback' => array( $this, 'get_json_data' ), | |
| 240 | + 'args' => array( | |
| 241 | + 'url' => array( | |
| 242 | + 'sanitize_callback' => 'esc_url_raw', | |
| 243 | + ), | |
| 244 | + 'chart' => array( | |
| 245 | + 'sanitize_callback' => 'absint', | |
| 246 | + ), | |
| 247 | + ), | |
| 248 | + 'permission_callback' => function () { | |
| 249 | + return current_user_can( 'edit_posts' ); | |
| 250 | + }, | |
| 251 | + ) | |
| 252 | + ); | |
| 253 | + | |
| 254 | + register_rest_route( | |
| 255 | + 'visualizer/v' . VISUALIZER_REST_VERSION, | |
| 256 | + '/set-json-data', | |
| 257 | + array( | |
| 258 | + 'methods' => 'GET', | |
| 259 | + 'callback' => array( $this, 'set_json_data' ), | |
| 260 | + 'args' => array( | |
| 261 | + 'url' => array( | |
| 262 | + 'sanitize_callback' => 'esc_url_raw', | |
| 263 | + ), | |
| 264 | + ), | |
| 265 | + 'permission_callback' => function () { | |
| 266 | + return current_user_can( 'edit_posts' ); | |
| 267 | + }, | |
| 268 | + ) | |
| 269 | + ); | |
| 270 | + | |
| 271 | + register_rest_route( | |
| 272 | + 'visualizer/v' . VISUALIZER_REST_VERSION, | |
| 147 | 273 | '/update-chart', |
| 148 | 274 | array( |
| 149 | 275 | 'methods' => 'POST', |
| 150 | 276 | 'callback' => array( $this, 'update_chart_data' ), |
| @@ -152,8 +278,11 @@ | ||
| 152 | 278 | 'id' => array( |
| 153 | 279 | 'sanitize_callback' => 'absint', |
| 154 | 280 | ), |
| 155 | 281 | ), |
| 282 | + 'permission_callback' => function () { | |
| 283 | + return current_user_can( 'edit_posts' ); | |
| 284 | + }, | |
| 156 | 285 | ) |
| 157 | 286 | ); |
| 158 | 287 | |
| 159 | 288 | register_rest_route( |
| @@ -166,8 +295,11 @@ | ||
| 166 | 295 | 'url' => array( |
| 167 | 296 | 'sanitize_callback' => 'esc_url_raw', |
| 168 | 297 | ), |
| 169 | 298 | ), |
| 299 | + 'permission_callback' => function () { | |
| 300 | + return current_user_can( 'edit_posts' ); | |
| 301 | + }, | |
| 170 | 302 | ) |
| 171 | 303 | ); |
| 172 | 304 | |
| 173 | 305 | register_rest_route( |
| @@ -180,8 +312,11 @@ | ||
| 180 | 312 | 'type' => array( |
| 181 | 313 | 'sanitize_callback' => 'sanitize_text_field', |
| 182 | 314 | ), |
| 183 | 315 | ), |
| 316 | + 'permission_callback' => function () { | |
| 317 | + return current_user_can( 'edit_posts' ); | |
| 318 | + }, | |
| 184 | 319 | ) |
| 185 | 320 | ); |
| 186 | 321 | } |
| 187 | 322 | |
| @@ -188,13 +323,20 @@ | ||
| 188 | 323 | /** |
| 189 | 324 | * Get Post Meta Fields |
| 190 | 325 | */ |
| 191 | 326 | public function get_visualizer_data( $post ) { |
| 327 | + if ( ! current_user_can( 'edit_posts' ) ) { | |
| 328 | + return false; | |
| 329 | + } | |
| 330 | + | |
| 192 | 331 | $data = array(); |
| 193 | 332 | $post_id = $post['id']; |
| 194 | 333 | |
| 195 | 334 | $data['visualizer-chart-type'] = get_post_meta( $post_id, Visualizer_Plugin::CF_CHART_TYPE, true ); |
| 196 | 335 | |
| 336 | + $library = get_post_meta( $post_id, Visualizer_Plugin::CF_CHART_LIBRARY, true ); | |
| 337 | + $data['visualizer-chart-library'] = $library; | |
| 338 | + | |
| 197 | 339 | $data['visualizer-source'] = get_post_meta( $post_id, Visualizer_Plugin::CF_SOURCE, true ); |
| 198 | 340 | |
| 199 | 341 | $data['visualizer-default-data'] = get_post_meta( $post_id, Visualizer_Plugin::CF_DEFAULT_DATA, true ); |
| 200 | 342 | |
| @@ -209,23 +351,112 @@ | ||
| 209 | 351 | |
| 210 | 352 | // handle data filter hooks |
| 211 | 353 | $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 | 354 | |
| 355 | + // we are going to format only for tabular charts, because we are not sure of the effect on others. | |
| 356 | + // this is to solve the case where boolean data shows up as all-ticks on gutenberg. | |
| 357 | + if ( in_array( $data['visualizer-chart-type'], array( 'tabular' ), true ) ) { | |
| 358 | + $data['visualizer-data'] = $this->format_chart_data( $data['visualizer-data'], $data['visualizer-series'] ); | |
| 359 | + } | |
| 360 | + | |
| 361 | + $data['visualizer-data-exploded'] = ''; | |
| 362 | + // handle annotations for google charts | |
| 363 | + if ( 'GoogleCharts' === $library ) { | |
| 364 | + // this will contain the data of both axis. | |
| 365 | + $settings = $data['visualizer-settings']; | |
| 366 | + // this will contain data only of Y axis. | |
| 367 | + $series = $data['visualizer-series']; | |
| 368 | + $annotations = array(); | |
| 369 | + if ( isset( $settings['series'] ) ) { | |
| 370 | + foreach ( $settings['series'] as $index => $serie ) { | |
| 371 | + // skip X axis data. | |
| 372 | + if ( $index === 0 ) { | |
| 373 | + continue; | |
| 374 | + } | |
| 375 | + if ( ! empty( $serie['role'] ) ) { | |
| 376 | + // this series is some kind of annotation, so let's collect its index. | |
| 377 | + // the index will be +1 because the X axis value is index 0, which is being ignored. | |
| 378 | + $annotations[ 'role' . ( intval( $index ) + 1 ) ] = $serie['role']; | |
| 379 | + } | |
| 380 | + } | |
| 381 | + } | |
| 382 | + if ( ! empty( $annotations ) ) { | |
| 383 | + $exploded_data = array(); | |
| 384 | + $series_names = array(); | |
| 385 | + foreach ( $series as $index => $serie ) { | |
| 386 | + // skip X axis data. | |
| 387 | + if ( $index === 0 ) { | |
| 388 | + continue; | |
| 389 | + } | |
| 390 | + if ( array_key_exists( 'role' . $index, $annotations ) ) { | |
| 391 | + $series_names[] = (object) array( 'role' => $annotations[ 'role' . $index ], 'type' => $serie['type'] ); | |
| 392 | + } else { | |
| 393 | + $series_names[] = $serie['label']; | |
| 394 | + } | |
| 395 | + } | |
| 396 | + $exploded_data[] = $series_names; | |
| 397 | + | |
| 398 | + foreach ( $data['visualizer-data'] as $datum ) { | |
| 399 | + // skip X axis data. | |
| 400 | + unset( $datum[0] ); | |
| 401 | + $exploded_data[] = $datum; | |
| 402 | + } | |
| 403 | + $data['visualizer-data-exploded'] = array( $exploded_data ); | |
| 404 | + } | |
| 405 | + } | |
| 406 | + | |
| 213 | 407 | $import = get_post_meta( $post_id, Visualizer_Plugin::CF_CHART_URL, true ); |
| 214 | 408 | |
| 215 | 409 | $schedule = get_post_meta( $post_id, Visualizer_Plugin::CF_CHART_SCHEDULE, true ); |
| 216 | 410 | |
| 411 | + $db_schedule = get_post_meta( $post_id, Visualizer_Plugin::CF_DB_SCHEDULE, true ); | |
| 412 | + | |
| 413 | + $db_query = get_post_meta( $post_id, Visualizer_Plugin::CF_DB_QUERY, true ); | |
| 414 | + | |
| 415 | + $json_url = get_post_meta( $post_id, Visualizer_Plugin::CF_JSON_URL, true ); | |
| 416 | + | |
| 417 | + $json_headers = get_post_meta( $post_id, Visualizer_Plugin::CF_JSON_HEADERS, true ); | |
| 418 | + | |
| 419 | + $json_schedule = get_post_meta( $post_id, Visualizer_Plugin::CF_JSON_SCHEDULE, true ); | |
| 420 | + | |
| 421 | + $json_root = get_post_meta( $post_id, Visualizer_Plugin::CF_JSON_ROOT, true ); | |
| 422 | + | |
| 423 | + $json_paging = get_post_meta( $post_id, Visualizer_Plugin::CF_JSON_PAGING, true ); | |
| 424 | + | |
| 217 | 425 | if ( ! empty( $import ) && ! empty( $schedule ) ) { |
| 218 | 426 | $data['visualizer-chart-url'] = $import; |
| 219 | 427 | $data['visualizer-chart-schedule'] = $schedule; |
| 220 | 428 | } |
| 221 | 429 | |
| 222 | - if ( VISUALIZER_PRO ) { | |
| 430 | + if ( ! empty( $db_schedule ) && ! empty( $db_query ) ) { | |
| 431 | + $data['visualizer-db-schedule'] = $db_schedule; | |
| 432 | + $data['visualizer-db-query'] = $db_query; | |
| 433 | + } | |
| 434 | + | |
| 435 | + if ( ! empty( $json_url ) ) { | |
| 436 | + $data['visualizer-json-schedule'] = $json_schedule; | |
| 437 | + $data['visualizer-json-url'] = $json_url; | |
| 438 | + $data['visualizer-json-headers'] = $json_headers; | |
| 439 | + $data['visualizer-json-root'] = $json_root; | |
| 440 | + | |
| 441 | + if ( Visualizer_Module::is_pro() && ! empty( $json_paging ) ) { | |
| 442 | + $data['visualizer-json-paging'] = $json_paging; | |
| 443 | + } | |
| 444 | + } | |
| 445 | + | |
| 446 | + if ( Visualizer_Module::is_pro() ) { | |
| 223 | 447 | $permissions = get_post_meta( $post_id, Visualizer_PRO::CF_PERMISSIONS, true ); |
| 224 | 448 | |
| 225 | - if ( ! empty( $permissions ) ) { | |
| 226 | - $data['visualizer-permissions'] = $permissions; | |
| 449 | + if ( empty( $permissions ) ) { | |
| 450 | + $permissions = array( 'permissions' => array( | |
| 451 | + 'read' => 'all', | |
| 452 | + 'edit' => 'roles', | |
| 453 | + 'edit-specific' => array( 'administrator' ), | |
| 454 | + ), | |
| 455 | + ); | |
| 227 | 456 | } |
| 457 | + | |
| 458 | + $data['visualizer-permissions'] = $permissions; | |
| 228 | 459 | } |
| 229 | 460 | |
| 230 | 461 | return $data; |
| 231 | 462 | } |
| @@ -230,33 +461,176 @@ | ||
| 230 | 461 | return $data; |
| 231 | 462 | } |
| 232 | 463 | |
| 233 | 464 | /** |
| 465 | + * Returns the data for the query. | |
| 466 | + * | |
| 467 | + * @access public | |
| 468 | + */ | |
| 469 | + public function get_query_data( $data ) { | |
| 470 | + if ( ! current_user_can( 'edit_posts' ) ) { | |
| 471 | + return false; | |
| 472 | + } | |
| 473 | + | |
| 474 | + $source = new Visualizer_Source_Query( stripslashes( $data['query'] ) ); | |
| 475 | + $html = $source->fetch( true ); | |
| 476 | + $source->fetch( false ); | |
| 477 | + $name = $source->getSourceName(); | |
| 478 | + $series = $source->getSeries(); | |
| 479 | + $data = $source->getRawData(); | |
| 480 | + $error = ''; | |
| 481 | + if ( empty( $html ) ) { | |
| 482 | + $error = $source->get_error(); | |
| 483 | + wp_send_json_error( array( 'msg' => $error ) ); | |
| 484 | + } | |
| 485 | + wp_send_json_success( array( 'table' => $html, 'name' => $name, 'series' => $series, 'data' => $data ) ); | |
| 486 | + } | |
| 487 | + | |
| 488 | + /** | |
| 489 | + * Returns the JSON root. | |
| 490 | + * | |
| 491 | + * @access public | |
| 492 | + */ | |
| 493 | + public function get_json_root_data( $data ) { | |
| 494 | + if ( ! current_user_can( 'edit_posts' ) ) { | |
| 495 | + return false; | |
| 496 | + } | |
| 497 | + | |
| 498 | + $source = new Visualizer_Source_Json( $data ); | |
| 499 | + | |
| 500 | + $roots = $source->fetchRoots(); | |
| 501 | + if ( empty( $roots ) ) { | |
| 502 | + wp_send_json_error( array( 'msg' => $source->get_error() ) ); | |
| 503 | + } | |
| 504 | + | |
| 505 | + wp_send_json_success( array( 'url' => $data['url'], 'roots' => $roots ) ); | |
| 506 | + } | |
| 507 | + | |
| 508 | + /** | |
| 509 | + * Returns the JSON data. | |
| 510 | + * | |
| 511 | + * @access public | |
| 512 | + */ | |
| 513 | + public function get_json_data( $data ) { | |
| 514 | + if ( ! current_user_can( 'edit_posts' ) ) { | |
| 515 | + return false; | |
| 516 | + } | |
| 517 | + | |
| 518 | + $chart_id = $data['chart']; | |
| 519 | + | |
| 520 | + if ( empty( $chart_id ) ) { | |
| 521 | + wp_die(); | |
| 522 | + } | |
| 523 | + | |
| 524 | + $source = new Visualizer_Source_Json( $data ); | |
| 525 | + $source->fetch(); | |
| 526 | + $table = $source->getRawData(); | |
| 527 | + | |
| 528 | + if ( empty( $table ) ) { | |
| 529 | + wp_send_json_error( array( 'msg' => esc_html__( 'Unable to fetch data from the endpoint. Please try again.', 'visualizer' ) ) ); | |
| 530 | + } | |
| 531 | + | |
| 532 | + $table = Visualizer_Render_Layout::show( 'editor-table', $table, $chart_id, 'viz-json-table', false, false ); | |
| 533 | + wp_send_json_success( array( 'table' => $table, 'root' => $data['root'], 'url' => $data['url'], 'paging' => $source->getPaginationElements() ) ); | |
| 534 | + } | |
| 535 | + | |
| 536 | + /** | |
| 537 | + * Set the JSON data. | |
| 538 | + * | |
| 539 | + * @access public | |
| 540 | + */ | |
| 541 | + public function set_json_data( $data ) { | |
| 542 | + if ( ! current_user_can( 'edit_posts' ) ) { | |
| 543 | + return false; | |
| 544 | + } | |
| 545 | + | |
| 546 | + $source = new Visualizer_Source_Json( $data ); | |
| 547 | + | |
| 548 | + $table = $source->fetch(); | |
| 549 | + if ( empty( $table ) ) { | |
| 550 | + wp_send_json_error( array( 'msg' => esc_html__( 'Unable to fetch data from the endpoint. Please try again.', 'visualizer' ) ) ); | |
| 551 | + } | |
| 552 | + | |
| 553 | + $source->fetchFromEditableTable(); | |
| 554 | + $name = $source->getSourceName(); | |
| 555 | + $series = json_encode( $source->getSeries() ); | |
| 556 | + $data = json_encode( $source->getRawData() ); | |
| 557 | + wp_send_json_success( array( 'name' => $name, 'series' => $series, 'data' => $data ) ); | |
| 558 | + } | |
| 559 | + | |
| 560 | + /** | |
| 234 | 561 | * Rest Callback Method |
| 235 | 562 | */ |
| 236 | 563 | public function update_chart_data( $data ) { |
| 564 | + if ( ! current_user_can( 'edit_posts' ) ) { | |
| 565 | + return false; | |
| 566 | + } | |
| 567 | + | |
| 237 | 568 | if ( $data['id'] && ! is_wp_error( $data['id'] ) ) { |
| 569 | + if ( get_post_type( $data['id'] ) !== Visualizer_Plugin::CPT_VISUALIZER ) { | |
| 570 | + return new WP_Error( 'invalid_post_type', 'Invalid post type.' ); | |
| 571 | + } | |
| 572 | + $chart_type = sanitize_text_field( $data['visualizer-chart-type'] ); | |
| 573 | + $source_type = sanitize_text_field( $data['visualizer-source'] ); | |
| 238 | 574 | |
| 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'] ); | |
| 575 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_TYPE, $chart_type ); | |
| 576 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_SOURCE, $source_type ); | |
| 241 | 577 | update_post_meta( $data['id'], Visualizer_Plugin::CF_DEFAULT_DATA, $data['visualizer-default-data'] ); |
| 242 | 578 | update_post_meta( $data['id'], Visualizer_Plugin::CF_SERIES, $data['visualizer-series'] ); |
| 243 | 579 | update_post_meta( $data['id'], Visualizer_Plugin::CF_SETTINGS, $data['visualizer-settings'] ); |
| 244 | 580 | |
| 245 | 581 | 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'] ); | |
| 582 | + $chart_url = esc_url_raw( $data['visualizer-chart-url'] ); | |
| 583 | + $chart_schedule = intval( $data['visualizer-chart-schedule'] ); | |
| 584 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_URL, $chart_url ); | |
| 585 | + apply_filters( 'visualizer_pro_chart_schedule', $data['id'], $chart_url, $chart_schedule ); | |
| 248 | 586 | } else { |
| 249 | 587 | delete_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_URL ); |
| 250 | 588 | apply_filters( 'visualizer_pro_remove_schedule', $data['id'] ); |
| 251 | 589 | } |
| 252 | 590 | |
| 253 | - if ( VISUALIZER_PRO ) { | |
| 591 | + if ( $source_type === 'Visualizer_Source_Query' ) { | |
| 592 | + $db_schedule = intval( $data['visualizer-db-schedule'] ); | |
| 593 | + $db_query = $data['visualizer-db-query']; | |
| 594 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_DB_SCHEDULE, $db_schedule ); | |
| 595 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_DB_QUERY, stripslashes( $db_query ) ); | |
| 596 | + } else { | |
| 597 | + delete_post_meta( $data['id'], Visualizer_Plugin::CF_DB_SCHEDULE ); | |
| 598 | + delete_post_meta( $data['id'], Visualizer_Plugin::CF_DB_QUERY ); | |
| 599 | + } | |
| 600 | + | |
| 601 | + if ( $source_type === 'Visualizer_Source_Json' ) { | |
| 602 | + $json_schedule = intval( $data['visualizer-json-schedule'] ); | |
| 603 | + $json_url = esc_url_raw( $data['visualizer-json-url'] ); | |
| 604 | + $json_headers = esc_url_raw( $data['visualizer-json-headers'] ); | |
| 605 | + $json_root = $data['visualizer-json-root']; | |
| 606 | + $json_paging = $data['visualizer-json-paging']; | |
| 607 | + | |
| 608 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_SCHEDULE, $json_schedule ); | |
| 609 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_URL, $json_url ); | |
| 610 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_HEADERS, $json_headers ); | |
| 611 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_ROOT, $json_root ); | |
| 612 | + | |
| 613 | + if ( ! empty( $json_paging ) ) { | |
| 614 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_PAGING, $json_paging ); | |
| 615 | + } else { | |
| 616 | + delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_PAGING ); | |
| 617 | + } | |
| 618 | + } else { | |
| 619 | + delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_SCHEDULE ); | |
| 620 | + delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_URL ); | |
| 621 | + delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_HEADERS ); | |
| 622 | + delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_ROOT ); | |
| 623 | + delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_PAGING ); | |
| 624 | + } | |
| 625 | + | |
| 626 | + if ( Visualizer_Module::is_pro() ) { | |
| 254 | 627 | update_post_meta( $data['id'], Visualizer_PRO::CF_PERMISSIONS, $data['visualizer-permissions'] ); |
| 255 | 628 | } |
| 256 | 629 | |
| 257 | 630 | if ( $data['visualizer-chart-url'] ) { |
| 258 | - $content['source'] = $data['visualizer-chart-url']; | |
| 631 | + $chart_url = esc_url_raw( $data['visualizer-chart-url'] ); | |
| 632 | + $content['source'] = $chart_url; | |
| 259 | 633 | $content['data'] = $this->format_chart_data( $data['visualizer-data'], $data['visualizer-series'] ); |
| 260 | 634 | } else { |
| 261 | 635 | $content = $this->format_chart_data( $data['visualizer-data'], $data['visualizer-series'] ); |
| 262 | 636 | } |
| @@ -284,8 +658,10 @@ | ||
| 284 | 658 | } |
| 285 | 659 | |
| 286 | 660 | /** |
| 287 | 661 | * Format chart data. |
| 662 | + * | |
| 663 | + * Note: No matter how tempted, don't use the similar method from Visualizer_Source. That works on a different structure. | |
| 288 | 664 | */ |
| 289 | 665 | public function format_chart_data( $data, $series ) { |
| 290 | 666 | foreach ( $series as $i => $row ) { |
| 291 | 667 | // if no value exists for the seires, then add null |
| @@ -296,39 +672,38 @@ | ||
| 296 | 672 | if ( is_null( $series[ $i ] ) ) { |
| 297 | 673 | continue; |
| 298 | 674 | } |
| 299 | 675 | |
| 300 | - if ( $row['type'] == 'number' ) { | |
| 301 | - foreach ( $data as $o => $col ) { | |
| 302 | - $data[ $o ][ $i ] = ( is_numeric( $col[ $i ] ) ) ? floatval( $col[ $i ] ) : ( is_numeric( str_replace( ',', '', $col[ $i ] ) ) ? floatval( str_replace( ',', '', $col[ $i ] ) ) : null ); | |
| 303 | - } | |
| 304 | - } | |
| 305 | - | |
| 306 | - if ( $row['type'] == 'boolean' ) { | |
| 307 | - foreach ( $data as $o => $col ) { | |
| 308 | - $data[ $o ][ $i ] = ! empty( $col[ $i ] ) ? filter_validate( $col[ $i ], FILTER_VALIDATE_BOOLEAN ) : null; | |
| 309 | - } | |
| 310 | - } | |
| 311 | - | |
| 312 | - if ( $row['type'] == 'timeofday' ) { | |
| 313 | - foreach ( $data as $o => $col ) { | |
| 314 | - $date = new DateTime( '1984-03-16T' . $col[ $i ] ); | |
| 315 | - if ( $date ) { | |
| 316 | - $data[ $o ][ $i ] = array( | |
| 317 | - intval( $date->format( 'H' ) ), | |
| 318 | - intval( $date->format( 'i' ) ), | |
| 319 | - intval( $date->format( 's' ) ), | |
| 320 | - 0, | |
| 321 | - ); | |
| 676 | + switch ( $row['type'] ) { | |
| 677 | + case 'number': | |
| 678 | + foreach ( $data as $o => $col ) { | |
| 679 | + $data[ $o ][ $i ] = ( is_numeric( $col[ $i ] ) ) ? floatval( $col[ $i ] ) : ( is_numeric( str_replace( ',', '', $col[ $i ] ) ) ? floatval( str_replace( ',', '', $col[ $i ] ) ) : null ); | |
| 322 | 680 | } |
| 323 | - } | |
| 681 | + break; | |
| 682 | + case 'boolean': | |
| 683 | + foreach ( $data as $o => $col ) { | |
| 684 | + $data[ $o ][ $i ] = ! empty( $col[ $i ] ) ? filter_var( $col[ $i ], FILTER_VALIDATE_BOOLEAN ) : false; | |
| 685 | + } | |
| 686 | + break; | |
| 687 | + case 'timeofday': | |
| 688 | + foreach ( $data as $o => $col ) { | |
| 689 | + $date = new DateTime( '1984-03-16T' . $col[ $i ] ); | |
| 690 | + if ( $date ) { | |
| 691 | + $data[ $o ][ $i ] = array( | |
| 692 | + intval( $date->format( 'H' ) ), | |
| 693 | + intval( $date->format( 'i' ) ), | |
| 694 | + intval( $date->format( 's' ) ), | |
| 695 | + 0, | |
| 696 | + ); | |
| 697 | + } | |
| 698 | + } | |
| 699 | + break; | |
| 700 | + case 'string': | |
| 701 | + foreach ( $data as $o => $col ) { | |
| 702 | + $data[ $o ][ $i ] = $this->toUTF8( $col[ $i ] ); | |
| 703 | + } | |
| 704 | + break; | |
| 324 | 705 | } |
| 325 | - | |
| 326 | - if ( $row['type'] == 'string' ) { | |
| 327 | - foreach ( $data as $o => $col ) { | |
| 328 | - $data[ $o ][ $i ] = $this->toUTF8( $col[ $i ] ); | |
| 329 | - } | |
| 330 | - } | |
| 331 | 706 | } |
| 332 | 707 | |
| 333 | 708 | return $data; |
| 334 | 709 | } |
| @@ -346,8 +721,12 @@ | ||
| 346 | 721 | /** |
| 347 | 722 | * Handle remote CSV data |
| 348 | 723 | */ |
| 349 | 724 | public function upload_csv_data( $data ) { |
| 725 | + if ( ! current_user_can( 'edit_posts' ) ) { | |
| 726 | + return false; | |
| 727 | + } | |
| 728 | + | |
| 350 | 729 | if ( $data['url'] && ! is_wp_error( $data['url'] ) && filter_var( $data['url'], FILTER_VALIDATE_URL ) ) { |
| 351 | 730 | $source = new Visualizer_Source_Csv_Remote( $data['url'] ); |
| 352 | 731 | if ( $source->fetch() ) { |
| 353 | 732 | $temp = $source->getData(); |
| @@ -369,8 +748,12 @@ | ||
| 369 | 748 | /** |
| 370 | 749 | * Get permission data |
| 371 | 750 | */ |
| 372 | 751 | public function get_permission_data( $data ) { |
| 752 | + if ( ! current_user_can( 'edit_posts' ) ) { | |
| 753 | + return false; | |
| 754 | + } | |
| 755 | + | |
| 373 | 756 | $options = array(); |
| 374 | 757 | switch ( $data['type'] ) { |
| 375 | 758 | case 'users': |
| 376 | 759 | $query = new WP_User_Query( |
| @@ -408,5 +791,26 @@ | ||
| 408 | 791 | } |
| 409 | 792 | return $options; |
| 410 | 793 | } |
| 411 | 794 | |
| 795 | + /** | |
| 796 | + * Filter Rest Query | |
| 797 | + */ | |
| 798 | + public function add_rest_query_vars( $args, \WP_REST_Request $request ) { | |
| 799 | + if ( isset( $request['meta_key'] ) && isset( $request['meta_value'] ) ) { | |
| 800 | + $args['meta_query'] = array( | |
| 801 | + 'relation' => 'OR', | |
| 802 | + array( | |
| 803 | + 'key' => $request->get_param( 'meta_key' ), | |
| 804 | + 'value' => $request->get_param( 'meta_value' ), | |
| 805 | + 'compare' => '!=', | |
| 806 | + ), | |
| 807 | + array( | |
| 808 | + 'key' => $request->get_param( 'meta_key' ), | |
| 809 | + 'value' => $request->get_param( 'meta_value' ), | |
| 810 | + 'compare' => 'NOT EXISTS', | |
| 811 | + ), | |
| 812 | + ); | |
| 813 | + } | |
| 814 | + return $args; | |
| 815 | + } | |
| 412 | 816 | } |