| @@ -57,13 +57,166 @@ | ||
| 57 | 57 | $this->_addAjaxAction( Visualizer_Plugin::ACTION_EDIT_CHART, 'renderChartPages' ); |
| 58 | 58 | $this->_addAjaxAction( Visualizer_Plugin::ACTION_UPLOAD_DATA, 'uploadData' ); |
| 59 | 59 | $this->_addAjaxAction( Visualizer_Plugin::ACTION_CLONE_CHART, 'cloneChart' ); |
| 60 | 60 | $this->_addAjaxAction( Visualizer_Plugin::ACTION_EXPORT_DATA, 'exportData' ); |
| 61 | + | |
| 62 | + $this->_addAjaxAction( Visualizer_Plugin::ACTION_FETCH_DB_DATA, 'getQueryData' ); | |
| 63 | + $this->_addAjaxAction( Visualizer_Plugin::ACTION_SAVE_DB_QUERY, 'saveQuery' ); | |
| 64 | + | |
| 65 | + $this->_addAjaxAction( Visualizer_Plugin::ACTION_JSON_GET_ROOTS, 'getJsonRoots' ); | |
| 66 | + $this->_addAjaxAction( Visualizer_Plugin::ACTION_JSON_GET_DATA, 'getJsonData' ); | |
| 67 | + $this->_addAjaxAction( Visualizer_Plugin::ACTION_JSON_SET_DATA, 'setJsonData' ); | |
| 68 | + $this->_addAjaxAction( Visualizer_Plugin::ACTION_JSON_SET_SCHEDULE, 'setJsonSchedule' ); | |
| 69 | + | |
| 70 | + $this->_addAjaxAction( Visualizer_Plugin::ACTION_SAVE_FILTER_QUERY, 'saveFilter' ); | |
| 71 | + | |
| 61 | 72 | } |
| 62 | 73 | |
| 63 | 74 | /** |
| 75 | + * Sets the schedule for how JSON-endpoint charts should be updated. | |
| 76 | + * | |
| 77 | + * @since ? | |
| 78 | + * | |
| 79 | + * @access public | |
| 80 | + */ | |
| 81 | + public function setJsonSchedule() { | |
| 82 | + check_ajax_referer( Visualizer_Plugin::ACTION_JSON_SET_SCHEDULE . Visualizer_Plugin::VERSION, 'security' ); | |
| 83 | + | |
| 84 | + $chart_id = filter_input( | |
| 85 | + INPUT_POST, | |
| 86 | + 'chart', | |
| 87 | + FILTER_VALIDATE_INT, | |
| 88 | + array( | |
| 89 | + 'options' => array( | |
| 90 | + 'min_range' => 1, | |
| 91 | + ), | |
| 92 | + ) | |
| 93 | + ); | |
| 94 | + | |
| 95 | + if ( ! $chart_id ) { | |
| 96 | + wp_send_json_error(); | |
| 97 | + } | |
| 98 | + | |
| 99 | + $time = filter_input( | |
| 100 | + INPUT_POST, | |
| 101 | + 'time', | |
| 102 | + FILTER_VALIDATE_INT, | |
| 103 | + array( | |
| 104 | + 'options' => array( | |
| 105 | + 'min_range' => -1, | |
| 106 | + ), | |
| 107 | + ) | |
| 108 | + ); | |
| 109 | + | |
| 110 | + delete_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_SCHEDULE ); | |
| 111 | + | |
| 112 | + if ( -1 < $time ) { | |
| 113 | + add_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_SCHEDULE, $time ); | |
| 114 | + } | |
| 115 | + wp_send_json_success(); | |
| 116 | + } | |
| 117 | + | |
| 118 | + /** | |
| 119 | + * Get the root elements for JSON-endpoint. | |
| 120 | + * | |
| 121 | + * @since ? | |
| 122 | + * | |
| 123 | + * @access public | |
| 124 | + */ | |
| 125 | + public function getJsonRoots() { | |
| 126 | + check_ajax_referer( Visualizer_Plugin::ACTION_JSON_GET_ROOTS . Visualizer_Plugin::VERSION, 'security' ); | |
| 127 | + | |
| 128 | + $params = wp_parse_args( $_POST['params'] ); | |
| 129 | + | |
| 130 | + $source = new Visualizer_Source_Json( $params ); | |
| 131 | + | |
| 132 | + $roots = $source->fetchRoots(); | |
| 133 | + if ( empty( $roots ) ) { | |
| 134 | + wp_send_json_error( array( 'msg' => $source->get_error() ) ); | |
| 135 | + } | |
| 136 | + | |
| 137 | + wp_send_json_success( array( 'url' => $params['url'], 'roots' => $roots ) ); | |
| 138 | + } | |
| 139 | + | |
| 140 | + /** | |
| 141 | + * Get the data for the JSON-endpoint corresponding to the chosen root. | |
| 142 | + * | |
| 143 | + * @since ? | |
| 144 | + * | |
| 145 | + * @access public | |
| 146 | + */ | |
| 147 | + public function getJsonData() { | |
| 148 | + check_ajax_referer( Visualizer_Plugin::ACTION_JSON_GET_DATA . Visualizer_Plugin::VERSION, 'security' ); | |
| 149 | + | |
| 150 | + $params = wp_parse_args( $_POST['params'] ); | |
| 151 | + | |
| 152 | + $chart_id = $params['chart']; | |
| 153 | + | |
| 154 | + if ( empty( $chart_id ) ) { | |
| 155 | + wp_die(); | |
| 156 | + } | |
| 157 | + | |
| 158 | + $source = new Visualizer_Source_Json( $params ); | |
| 159 | + | |
| 160 | + $data = $source->parse(); | |
| 161 | + if ( empty( $data ) ) { | |
| 162 | + wp_send_json_error( array( 'msg' => esc_html__( 'Unable to fetch data from the endpoint. Please try again.', 'visualizer' ) ) ); | |
| 163 | + } | |
| 164 | + | |
| 165 | + $data = Visualizer_Render_Layout::show( 'editor-table', $data, $chart_id, 'viz-json-table', false, false ); | |
| 166 | + wp_send_json_success( array( 'table' => $data, 'root' => $params['root'], 'url' => $params['url'], 'paging' => $source->getPaginationElements() ) ); | |
| 167 | + } | |
| 168 | + | |
| 169 | + /** | |
| 170 | + * Updates the database with the correct post parameters for JSON-endpoint charts. | |
| 171 | + * | |
| 172 | + * @since ? | |
| 173 | + * | |
| 174 | + * @access public | |
| 175 | + */ | |
| 176 | + public function setJsonData() { | |
| 177 | + check_ajax_referer( Visualizer_Plugin::ACTION_JSON_SET_DATA . Visualizer_Plugin::VERSION, 'security' ); | |
| 178 | + | |
| 179 | + $params = $_POST; | |
| 180 | + $chart_id = $_GET['chart']; | |
| 181 | + | |
| 182 | + if ( empty( $chart_id ) ) { | |
| 183 | + wp_die(); | |
| 184 | + } | |
| 185 | + | |
| 186 | + $chart = get_post( $chart_id ); | |
| 187 | + | |
| 188 | + $source = new Visualizer_Source_Json( $params ); | |
| 189 | + $source->fetch(); | |
| 190 | + | |
| 191 | + $content = $source->getData(); | |
| 192 | + $chart->post_content = $content; | |
| 193 | + wp_update_post( $chart->to_array() ); | |
| 194 | + update_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, $source->getSeries() ); | |
| 195 | + update_post_meta( $chart->ID, Visualizer_Plugin::CF_SOURCE, $source->getSourceName() ); | |
| 196 | + update_post_meta( $chart->ID, Visualizer_Plugin::CF_DEFAULT_DATA, 0 ); | |
| 197 | + update_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_URL, $params['url'] ); | |
| 198 | + update_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_ROOT, $params['root'] ); | |
| 199 | + delete_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_PAGING ); | |
| 200 | + if ( ! empty( $params['paging'] ) ) { | |
| 201 | + add_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_PAGING, $params['paging'] ); | |
| 202 | + } | |
| 203 | + | |
| 204 | + $render = new Visualizer_Render_Page_Update(); | |
| 205 | + $render->id = $chart->ID; | |
| 206 | + $render->data = json_encode( $source->getRawData() ); | |
| 207 | + $render->series = json_encode( $source->getSeries() ); | |
| 208 | + $render->render(); | |
| 209 | + | |
| 210 | + defined( 'WP_TESTS_DOMAIN' ) ? wp_die() : exit(); | |
| 211 | + } | |
| 212 | + | |
| 213 | + | |
| 214 | + /** | |
| 64 | 215 | * Fetches charts from database. |
| 65 | 216 | * |
| 217 | + * This method is also called from the media pop-up (classic editor: create a post and add chart from insert content). | |
| 218 | + * | |
| 66 | 219 | * @since 1.0.0 |
| 67 | 220 | * |
| 68 | 221 | * @access public |
| 69 | 222 | */ |
| @@ -71,9 +224,12 @@ | ||
| 71 | 224 | $query_args = array( |
| 72 | 225 | 'post_type' => Visualizer_Plugin::CPT_VISUALIZER, |
| 73 | 226 | 'posts_per_page' => 9, |
| 74 | 227 | 'paged' => filter_input( |
| 75 | - INPUT_GET, 'page', FILTER_VALIDATE_INT, array( | |
| 228 | + INPUT_GET, | |
| 229 | + 'page', | |
| 230 | + FILTER_VALIDATE_INT, | |
| 231 | + array( | |
| 76 | 232 | 'options' => array( |
| 77 | 233 | 'min_range' => 1, |
| 78 | 234 | 'default' => 1, |
| 79 | 235 | ), |
| @@ -85,9 +241,9 @@ | ||
| 85 | 241 | // 'filter' is from the modal from the add media button. |
| 86 | 242 | $filter = filter_input( INPUT_GET, 'filter', FILTER_SANITIZE_STRING ); |
| 87 | 243 | } |
| 88 | 244 | |
| 89 | - if ( $filter && in_array( $filter, Visualizer_Plugin::getChartTypes() ) ) { | |
| 245 | + if ( $filter && in_array( $filter, Visualizer_Plugin::getChartTypes(), true ) ) { | |
| 90 | 246 | $query_args['meta_query'] = array( |
| 91 | 247 | array( |
| 92 | 248 | 'key' => Visualizer_Plugin::CF_CHART_TYPE, |
| 93 | 249 | 'value' => $filter, |
| @@ -100,8 +256,18 @@ | ||
| 100 | 256 | while ( $query->have_posts() ) { |
| 101 | 257 | $chart = $query->next_post(); |
| 102 | 258 | $chart_data = $this->_getChartArray( $chart ); |
| 103 | 259 | $chart_data['id'] = $chart->ID; |
| 260 | + $chart_data['library'] = $this->load_chart_type( $chart->ID ); | |
| 261 | + $css = ''; | |
| 262 | + $settings = $chart_data['settings']; | |
| 263 | + $arguments = $this->get_inline_custom_css( 'visualizer-chart-' . $chart->ID, $settings ); | |
| 264 | + if ( ! empty( $arguments ) ) { | |
| 265 | + $css = $arguments[0]; | |
| 266 | + $settings = $arguments[1]; | |
| 267 | + } | |
| 268 | + $chart_data['settings'] = $settings; | |
| 269 | + $chart_data['css'] = $css; | |
| 104 | 270 | $charts[] = $chart_data; |
| 105 | 271 | } |
| 106 | 272 | self::_sendResponse( |
| 107 | 273 | array( |
| @@ -129,14 +295,33 @@ | ||
| 129 | 295 | } |
| 130 | 296 | $type = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true ); |
| 131 | 297 | $series = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SERIES, get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true ), $chart->ID, $type ); |
| 132 | 298 | $data = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA, unserialize( $chart->post_content ), $chart->ID, $type ); |
| 299 | + $library = $this->load_chart_type( $chart->ID ); | |
| 133 | 300 | |
| 301 | + $settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true ); | |
| 302 | + $settings = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SETTINGS, $settings, $chart->ID, $type ); | |
| 303 | + if ( ! empty( $atts['settings'] ) ) { | |
| 304 | + $settings = apply_filters( $atts['settings'], $settings, $chart->ID, $type ); | |
| 305 | + } | |
| 306 | + | |
| 307 | + $css = ''; | |
| 308 | + $arguments = $this->get_inline_custom_css( 'visualizer-' . $chart->ID, $settings ); | |
| 309 | + if ( ! empty( $arguments ) ) { | |
| 310 | + $css = $arguments[0]; | |
| 311 | + $settings = $arguments[1]; | |
| 312 | + } | |
| 313 | + | |
| 314 | + $date_formats = Visualizer_Source::get_date_formats_if_exists( $series, $data ); | |
| 315 | + | |
| 134 | 316 | return array( |
| 135 | 317 | 'type' => $type, |
| 136 | 318 | 'series' => $series, |
| 137 | - 'settings' => get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true ), | |
| 319 | + 'settings' => $settings, | |
| 138 | 320 | 'data' => $data, |
| 321 | + 'library' => $library, | |
| 322 | + 'css' => $css, | |
| 323 | + 'date_formats' => $date_formats, | |
| 139 | 324 | ); |
| 140 | 325 | } |
| 141 | 326 | |
| 142 | 327 | /** |
| @@ -163,9 +348,9 @@ | ||
| 163 | 348 | * |
| 164 | 349 | * @access public |
| 165 | 350 | */ |
| 166 | 351 | public function deleteChart() { |
| 167 | - $is_post = $_SERVER['REQUEST_METHOD'] == 'POST'; | |
| 352 | + $is_post = $_SERVER['REQUEST_METHOD'] === 'POST'; | |
| 168 | 353 | $input_method = $is_post ? INPUT_POST : INPUT_GET; |
| 169 | 354 | $chart_id = $success = false; |
| 170 | 355 | $nonce = wp_verify_nonce( filter_input( $input_method, 'nonce' ) ); |
| 171 | 356 | $capable = current_user_can( 'delete_posts' ); |
| @@ -170,9 +355,12 @@ | ||
| 170 | 355 | $nonce = wp_verify_nonce( filter_input( $input_method, 'nonce' ) ); |
| 171 | 356 | $capable = current_user_can( 'delete_posts' ); |
| 172 | 357 | if ( $nonce && $capable ) { |
| 173 | 358 | $chart_id = filter_input( |
| 174 | - $input_method, 'chart', FILTER_VALIDATE_INT, array( | |
| 359 | + $input_method, | |
| 360 | + 'chart', | |
| 361 | + FILTER_VALIDATE_INT, | |
| 362 | + array( | |
| 175 | 363 | 'options' => array( |
| 176 | 364 | 'min_range' => 1, |
| 177 | 365 | ), |
| 178 | 366 | ) |
| @@ -178,9 +366,9 @@ | ||
| 178 | 366 | ) |
| 179 | 367 | ); |
| 180 | 368 | if ( $chart_id ) { |
| 181 | 369 | $chart = get_post( $chart_id ); |
| 182 | - $success = $chart && $chart->post_type == Visualizer_Plugin::CPT_VISUALIZER; | |
| 370 | + $success = $chart && $chart->post_type === Visualizer_Plugin::CPT_VISUALIZER; | |
| 183 | 371 | } |
| 184 | 372 | } |
| 185 | 373 | if ( $success ) { |
| 186 | 374 | wp_delete_post( $chart_id, true ); |
| @@ -191,13 +379,41 @@ | ||
| 191 | 379 | 'success' => $success, |
| 192 | 380 | ) |
| 193 | 381 | ); |
| 194 | 382 | } |
| 195 | - wp_redirect( wp_get_referer() ); | |
| 383 | + wp_redirect( remove_query_arg( 'vaction', wp_get_referer() ) ); | |
| 196 | 384 | exit; |
| 197 | 385 | } |
| 198 | 386 | |
| 199 | 387 | /** |
| 388 | + * Delete charts that are still in auto-draft mode. | |
| 389 | + */ | |
| 390 | + private function deleteOldCharts() { | |
| 391 | + $query = new WP_Query( | |
| 392 | + array( | |
| 393 | + 'post_type' => Visualizer_Plugin::CPT_VISUALIZER, | |
| 394 | + 'post_status' => 'auto-draft', | |
| 395 | + 'fields' => 'ids', | |
| 396 | + 'update_post_meta_cache' => false, | |
| 397 | + 'update_post_term_cache' => false, | |
| 398 | + 'posts_per_page' => 50, | |
| 399 | + 'date_query' => array( | |
| 400 | + array( | |
| 401 | + 'before' => 'today', | |
| 402 | + ), | |
| 403 | + ), | |
| 404 | + ) | |
| 405 | + ); | |
| 406 | + | |
| 407 | + if ( $query->have_posts() ) { | |
| 408 | + $ids = array(); | |
| 409 | + while ( $query->have_posts() ) { | |
| 410 | + wp_delete_post( $query->next_post(), true ); | |
| 411 | + } | |
| 412 | + } | |
| 413 | + } | |
| 414 | + | |
| 415 | + /** | |
| 200 | 416 | * Renders appropriate page for chart builder. Creates new auto draft chart |
| 201 | 417 | * if no chart has been specified. |
| 202 | 418 | * |
| 203 | 419 | * @since 1.0.0 |
| @@ -207,9 +423,10 @@ | ||
| 207 | 423 | public function renderChartPages() { |
| 208 | 424 | defined( 'IFRAME_REQUEST' ) || define( 'IFRAME_REQUEST', 1 ); |
| 209 | 425 | // check chart, if chart not exists, will create new one and redirects to the same page with proper chart id |
| 210 | 426 | $chart_id = isset( $_GET['chart'] ) ? filter_var( $_GET['chart'], FILTER_VALIDATE_INT ) : ''; |
| 211 | - if ( ! $chart_id || ! ( $chart = get_post( $chart_id ) ) || $chart->post_type != Visualizer_Plugin::CPT_VISUALIZER ) { | |
| 427 | + if ( ! $chart_id || ! ( $chart = get_post( $chart_id ) ) || $chart->post_type !== Visualizer_Plugin::CPT_VISUALIZER ) { | |
| 428 | + $this->deleteOldCharts(); | |
| 212 | 429 | $default_type = isset( $_GET['type'] ) && ! empty( $_GET['type'] ) ? $_GET['type'] : 'line'; |
| 213 | 430 | $source = new Visualizer_Source_Csv( VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples' . DIRECTORY_SEPARATOR . $default_type . '.csv' ); |
| 214 | 431 | $source->fetch(); |
| 215 | 432 | $chart_id = wp_insert_post( |
| @@ -225,10 +442,13 @@ | ||
| 225 | 442 | add_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_TYPE, $default_type ); |
| 226 | 443 | add_post_meta( $chart_id, Visualizer_Plugin::CF_DEFAULT_DATA, 1 ); |
| 227 | 444 | add_post_meta( $chart_id, Visualizer_Plugin::CF_SOURCE, $source->getSourceName() ); |
| 228 | 445 | add_post_meta( $chart_id, Visualizer_Plugin::CF_SERIES, $source->getSeries() ); |
| 446 | + add_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_LIBRARY, '' ); | |
| 229 | 447 | add_post_meta( |
| 230 | - $chart_id, Visualizer_Plugin::CF_SETTINGS, array( | |
| 448 | + $chart_id, | |
| 449 | + Visualizer_Plugin::CF_SETTINGS, | |
| 450 | + array( | |
| 231 | 451 | 'focusTarget' => 'datum', |
| 232 | 452 | ) |
| 233 | 453 | ); |
| 234 | 454 | do_action( 'visualizer_pro_new_chart_defaults', $chart_id ); |
| @@ -235,34 +455,51 @@ | ||
| 235 | 455 | } |
| 236 | 456 | wp_redirect( add_query_arg( 'chart', (int) $chart_id ) ); |
| 237 | 457 | defined( 'WP_TESTS_DOMAIN' ) ? wp_die() : exit(); |
| 238 | 458 | } |
| 459 | + | |
| 460 | + $lib = $this->load_chart_type( $chart_id ); | |
| 461 | + | |
| 462 | + // the alpha color picker (RGBA) is not supported by google. | |
| 463 | + $color_picker_dep = 'wp-color-picker'; | |
| 464 | + if ( in_array( $lib, array( 'chartjs', 'datatables' ), true ) && ! wp_script_is( 'wp-color-picker-alpha', 'registered' ) ) { | |
| 465 | + wp_register_script( 'wp-color-picker-alpha', VISUALIZER_ABSURL . 'js/lib/wp-color-picker-alpha.min.js', array( 'wp-color-picker' ), Visualizer_Plugin::VERSION ); | |
| 466 | + $color_picker_dep = 'wp-color-picker-alpha'; | |
| 467 | + } | |
| 468 | + | |
| 239 | 469 | // enqueue and register scripts and styles |
| 240 | 470 | wp_register_script( 'visualizer-chosen', VISUALIZER_ABSURL . 'js/lib/chosen.jquery.min.js', array( 'jquery' ), Visualizer_Plugin::VERSION ); |
| 241 | 471 | wp_register_style( 'visualizer-chosen', VISUALIZER_ABSURL . 'css/lib/chosen.min.css', array(), Visualizer_Plugin::VERSION ); |
| 242 | 472 | |
| 243 | 473 | wp_register_style( 'visualizer-frame', VISUALIZER_ABSURL . 'css/frame.css', array( 'visualizer-chosen' ), Visualizer_Plugin::VERSION ); |
| 244 | - wp_register_script( 'visualizer-frame', VISUALIZER_ABSURL . 'js/frame.js', array( 'visualizer-chosen' ), Visualizer_Plugin::VERSION, true ); | |
| 245 | - wp_register_script( 'google-jsapi-new', '//www.gstatic.com/charts/loader.js', array(), null, true ); | |
| 246 | - wp_register_script( 'google-jsapi-old', '//www.google.com/jsapi', array( 'google-jsapi-new' ), null, true ); | |
| 474 | + wp_register_script( 'visualizer-frame', VISUALIZER_ABSURL . 'js/frame.js', array( 'visualizer-chosen', 'jquery-ui-accordion' ), Visualizer_Plugin::VERSION, true ); | |
| 475 | + wp_register_script( 'visualizer-customization', $this->get_user_customization_js(), array(), null, true ); | |
| 247 | 476 | wp_register_script( |
| 248 | - 'visualizer-render', VISUALIZER_ABSURL . 'js/render.js', array( | |
| 249 | - 'google-jsapi-old', | |
| 250 | - 'google-jsapi-new', | |
| 251 | - 'visualizer-frame', | |
| 252 | - ), Visualizer_Plugin::VERSION, true | |
| 477 | + 'visualizer-render', | |
| 478 | + VISUALIZER_ABSURL . 'js/render-facade.js', | |
| 479 | + apply_filters( 'visualizer_assets_render', array( 'visualizer-frame', 'visualizer-customization' ), false ), | |
| 480 | + Visualizer_Plugin::VERSION, | |
| 481 | + true | |
| 253 | 482 | ); |
| 254 | 483 | wp_register_script( |
| 255 | - 'visualizer-preview', VISUALIZER_ABSURL . 'js/preview.js', array( | |
| 256 | - 'wp-color-picker', | |
| 484 | + 'visualizer-preview', | |
| 485 | + VISUALIZER_ABSURL . 'js/preview.js', | |
| 486 | + array( | |
| 487 | + $color_picker_dep, | |
| 257 | 488 | 'visualizer-render', |
| 258 | - ), Visualizer_Plugin::VERSION, true | |
| 489 | + ), | |
| 490 | + Visualizer_Plugin::VERSION, | |
| 491 | + true | |
| 259 | 492 | ); |
| 260 | - // added by Ash/Upwork | |
| 261 | - if ( VISUALIZER_PRO ) { | |
| 493 | + wp_register_script( 'visualizer-editor-simple', VISUALIZER_ABSURL . 'js/simple-editor.js', array( 'jquery' ), Visualizer_Plugin::VERSION, true ); | |
| 494 | + | |
| 495 | + do_action( 'visualizer_add_scripts' ); | |
| 496 | + | |
| 497 | + if ( Visualizer_Module::is_pro() && Visualizer_Module::is_pro_older_than( '1.9.0' ) ) { | |
| 262 | 498 | global $Visualizer_Pro; |
| 263 | 499 | $Visualizer_Pro->_addScriptsAndStyles(); |
| 264 | 500 | } |
| 501 | + | |
| 265 | 502 | // dispatch pages |
| 266 | 503 | $this->_chart = get_post( $chart_id ); |
| 267 | 504 | $tab = isset( $_GET['tab'] ) && ! empty( $_GET['tab'] ) ? $_GET['tab'] : 'visualizer'; |
| 268 | 505 | |
| @@ -297,8 +534,63 @@ | ||
| 297 | 534 | defined( 'WP_TESTS_DOMAIN' ) ? wp_die() : exit(); |
| 298 | 535 | } |
| 299 | 536 | |
| 300 | 537 | /** |
| 538 | + * Load code editor assets. | |
| 539 | + */ | |
| 540 | + private function loadCodeEditorAssets() { | |
| 541 | + global $wp_version; | |
| 542 | + | |
| 543 | + $wp_scripts = wp_scripts(); | |
| 544 | + | |
| 545 | + // data tables assets. | |
| 546 | + wp_register_script( 'visualizer-datatables', VISUALIZER_ABSURL . 'js/lib/datatables.min.js', array( 'jquery-ui-core' ), Visualizer_Plugin::VERSION ); | |
| 547 | + wp_register_style( 'visualizer-datatables', VISUALIZER_ABSURL . 'css/lib/datatables.min.css', array(), Visualizer_Plugin::VERSION ); | |
| 548 | + wp_register_style( 'visualizer-jquery-ui', sprintf( '//ajax.googleapis.com/ajax/libs/jqueryui/%s/themes/smoothness/jquery-ui.css', $wp_scripts->registered['jquery-ui-core']->ver ), array( 'visualizer-datatables' ), Visualizer_Plugin::VERSION ); | |
| 549 | + wp_enqueue_script( 'visualizer-datatables' ); | |
| 550 | + wp_enqueue_style( 'visualizer-jquery-ui' ); | |
| 551 | + | |
| 552 | + if ( ! Visualizer_Module::is_pro() ) { | |
| 553 | + return; | |
| 554 | + } | |
| 555 | + | |
| 556 | + $table_col_mapping = Visualizer_Source_Query_Params::get_all_db_tables_column_mapping(); | |
| 557 | + | |
| 558 | + if ( version_compare( $wp_version, '4.9.0', '<' ) ) { | |
| 559 | + // code mirror assets. | |
| 560 | + wp_register_script( 'visualizer-codemirror-core', '//codemirror.net/lib/codemirror.js', array( 'jquery' ), Visualizer_Plugin::VERSION ); | |
| 561 | + wp_register_script( 'visualizer-codemirror-placeholder', '//codemirror.net/addon/display/placeholder.js', array( 'visualizer-codemirror-core' ), Visualizer_Plugin::VERSION ); | |
| 562 | + wp_register_script( 'visualizer-codemirror-matchbrackets', '//codemirror.net/addon/edit/matchbrackets.js', array( 'visualizer-codemirror-core' ), Visualizer_Plugin::VERSION ); | |
| 563 | + wp_register_script( 'visualizer-codemirror-closebrackets', '//codemirror.net/addon/edit/closebrackets.js', array( 'visualizer-codemirror-core' ), Visualizer_Plugin::VERSION ); | |
| 564 | + wp_register_script( 'visualizer-codemirror-sql', '//codemirror.net/mode/sql/sql.js', array( 'visualizer-codemirror-core' ), Visualizer_Plugin::VERSION ); | |
| 565 | + wp_register_script( 'visualizer-codemirror-sql-hint', '//codemirror.net/addon/hint/sql-hint.js', array( 'visualizer-codemirror-core' ), Visualizer_Plugin::VERSION ); | |
| 566 | + wp_register_script( 'visualizer-codemirror-hint', '//codemirror.net/addon/hint/show-hint.js', array( 'visualizer-codemirror-sql', 'visualizer-codemirror-sql-hint', 'visualizer-codemirror-placeholder', 'visualizer-codemirror-matchbrackets', 'visualizer-codemirror-closebrackets' ), Visualizer_Plugin::VERSION ); | |
| 567 | + wp_register_style( 'visualizer-codemirror-core', '//codemirror.net/lib/codemirror.css', array(), Visualizer_Plugin::VERSION ); | |
| 568 | + wp_register_style( 'visualizer-codemirror-hint', '//codemirror.net/addon/hint/show-hint.css', array( 'visualizer-codemirror-core' ), Visualizer_Plugin::VERSION ); | |
| 569 | + | |
| 570 | + wp_enqueue_script( 'visualizer-codemirror-hint' ); | |
| 571 | + wp_enqueue_style( 'visualizer-codemirror-hint' ); | |
| 572 | + } else { | |
| 573 | + wp_enqueue_code_editor( | |
| 574 | + array( | |
| 575 | + 'type' => 'sql', | |
| 576 | + 'codemirror' => array( | |
| 577 | + 'autofocus' => true, | |
| 578 | + 'lineWrapping' => true, | |
| 579 | + 'dragDrop' => false, | |
| 580 | + 'matchBrackets' => true, | |
| 581 | + 'autoCloseBrackets' => true, | |
| 582 | + 'extraKeys' => array( 'Ctrl-Space' => 'autocomplete' ), | |
| 583 | + 'hintOptions' => array( 'tables' => $table_col_mapping ), | |
| 584 | + ), | |
| 585 | + ) | |
| 586 | + ); | |
| 587 | + } | |
| 588 | + | |
| 589 | + return $table_col_mapping; | |
| 590 | + } | |
| 591 | + | |
| 592 | + /** | |
| 301 | 593 | * Handle data and settings page |
| 302 | 594 | */ |
| 303 | 595 | private function _handleDataAndSettingsPage() { |
| 304 | 596 | if ( isset( $_POST['map_api_key'] ) ) { |
| @@ -303,14 +595,37 @@ | ||
| 303 | 595 | private function _handleDataAndSettingsPage() { |
| 304 | 596 | if ( isset( $_POST['map_api_key'] ) ) { |
| 305 | 597 | update_option( 'visualizer-map-api-key', $_POST['map_api_key'] ); |
| 306 | 598 | } |
| 307 | - if ( $_SERVER['REQUEST_METHOD'] == 'POST' && isset( $_GET['nonce'] ) && wp_verify_nonce( $_GET['nonce'] ) ) { | |
| 308 | - if ( $this->_chart->post_status == 'auto-draft' ) { | |
| 599 | + if ( $_SERVER['REQUEST_METHOD'] === 'POST' && isset( $_GET['nonce'] ) && wp_verify_nonce( $_GET['nonce'] ) ) { | |
| 600 | + if ( $this->_chart->post_status === 'auto-draft' ) { | |
| 309 | 601 | $this->_chart->post_status = 'publish'; |
| 602 | + | |
| 603 | + // ensure that a revision is not created. If a revision is created it will have the proper data and the parent of the revision will have default data. | |
| 604 | + // we do not want any difference in data so disable revisions temporarily. | |
| 605 | + add_filter( 'wp_revisions_to_keep', '__return_false' ); | |
| 310 | 606 | wp_update_post( $this->_chart->to_array() ); |
| 311 | 607 | } |
| 312 | - update_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_SETTINGS, $_POST ); | |
| 608 | + // save meta data only when it is NOT being canceled. | |
| 609 | + if ( ! ( isset( $_POST['cancel'] ) && 1 === intval( $_POST['cancel'] ) ) ) { | |
| 610 | + update_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_SETTINGS, $_POST ); | |
| 611 | + | |
| 612 | + // we will keep a parameter called 'internal_title' that will be set to the given title or, if empty, the chart ID | |
| 613 | + // this will help in searching with the chart id. | |
| 614 | + $settings = get_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_SETTINGS, true ); | |
| 615 | + $title = null; | |
| 616 | + if ( isset( $settings['title'] ) && ! empty( $settings['title'] ) ) { | |
| 617 | + $title = $settings['title']; | |
| 618 | + if ( is_array( $title ) ) { | |
| 619 | + $title = $settings['title']['text']; | |
| 620 | + } | |
| 621 | + } | |
| 622 | + if ( empty( $title ) ) { | |
| 623 | + $title = $this->_chart->ID; | |
| 624 | + } | |
| 625 | + $settings['internal_title'] = $title; | |
| 626 | + update_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_SETTINGS, $settings ); | |
| 627 | + } | |
| 313 | 628 | $render = new Visualizer_Render_Page_Send(); |
| 314 | 629 | $render->text = sprintf( '[visualizer id="%d"]', $this->_chart->ID ); |
| 315 | 630 | wp_iframe( array( $render, 'render' ) ); |
| 316 | 631 | |
| @@ -317,9 +632,9 @@ | ||
| 317 | 632 | return; |
| 318 | 633 | } |
| 319 | 634 | $data = $this->_getChartArray(); |
| 320 | 635 | $sidebar = ''; |
| 321 | - $sidebar_class = 'Visualizer_Render_Sidebar_Type_' . ucfirst( $data['type'] ); | |
| 636 | + $sidebar_class = $this->load_chart_class_name( $this->_chart->ID ); | |
| 322 | 637 | if ( class_exists( $sidebar_class, true ) ) { |
| 323 | 638 | $sidebar = new $sidebar_class( $data['settings'] ); |
| 324 | 639 | $sidebar->__series = $data['series']; |
| 325 | 640 | $sidebar->__data = $data['data']; |
| @@ -324,59 +639,105 @@ | ||
| 324 | 639 | $sidebar->__series = $data['series']; |
| 325 | 640 | $sidebar->__data = $data['data']; |
| 326 | 641 | } else { |
| 327 | 642 | $sidebar = apply_filters( 'visualizer_pro_chart_type_sidebar', '', $data ); |
| 328 | - if ( $sidebar != '' ) { | |
| 643 | + if ( $sidebar !== '' ) { | |
| 329 | 644 | $sidebar->__series = $data['series']; |
| 330 | 645 | $sidebar->__data = $data['data']; |
| 331 | 646 | } |
| 332 | 647 | } |
| 333 | - unset( $data['settings']['width'], $data['settings']['height'] ); | |
| 334 | - wp_enqueue_style( 'visualizer-frame' ); | |
| 648 | + unset( $data['settings']['width'], $data['settings']['height'], $data['settings']['chartArea'] ); | |
| 335 | 649 | wp_enqueue_style( 'wp-color-picker' ); |
| 336 | 650 | wp_enqueue_style( 'visualizer-frame' ); |
| 337 | 651 | wp_enqueue_script( 'visualizer-preview' ); |
| 652 | + wp_enqueue_script( 'visualizer-chosen' ); | |
| 338 | 653 | wp_enqueue_script( 'visualizer-render' ); |
| 654 | + | |
| 655 | + if ( ! Visualizer_Module::is_pro() ) { | |
| 656 | + wp_enqueue_script( 'visualizer-editor-simple' ); | |
| 657 | + wp_localize_script( | |
| 658 | + 'visualizer-editor-simple', | |
| 659 | + 'visualizer1', | |
| 660 | + array( | |
| 661 | + 'ajax' => array( | |
| 662 | + 'url' => admin_url( 'admin-ajax.php' ), | |
| 663 | + 'nonces' => array( | |
| 664 | + ), | |
| 665 | + 'actions' => array( | |
| 666 | + ), | |
| 667 | + ), | |
| 668 | + ) | |
| 669 | + ); | |
| 670 | + } | |
| 671 | + | |
| 672 | + $table_col_mapping = $this->loadCodeEditorAssets(); | |
| 673 | + | |
| 339 | 674 | wp_localize_script( |
| 340 | - 'visualizer-render', 'visualizer', array( | |
| 675 | + 'visualizer-render', | |
| 676 | + 'visualizer', | |
| 677 | + array( | |
| 341 | 678 | 'l10n' => array( |
| 342 | 679 | 'invalid_source' => esc_html__( 'You have entered invalid URL. Please, insert proper URL.', 'visualizer' ), |
| 343 | 680 | 'loading' => esc_html__( 'Loading...', 'visualizer' ), |
| 681 | + 'json_error' => esc_html__( 'An error occured in fetching data.', 'visualizer' ), | |
| 344 | 682 | ), |
| 345 | 683 | 'charts' => array( |
| 346 | 684 | 'canvas' => $data, |
| 685 | + 'id' => $this->_chart->ID, | |
| 347 | 686 | ), |
| 348 | 687 | 'language' => $this->get_language(), |
| 349 | 688 | 'map_api_key' => get_option( 'visualizer-map-api-key' ), |
| 350 | - 'ajax' => array( | |
| 689 | + 'ajax' => array( | |
| 351 | 690 | 'url' => admin_url( 'admin-ajax.php' ), |
| 352 | 691 | 'nonces' => array( |
| 353 | 692 | 'permissions' => wp_create_nonce( Visualizer_Plugin::ACTION_FETCH_PERMISSIONS_DATA ), |
| 693 | + 'db_get_data' => wp_create_nonce( Visualizer_Plugin::ACTION_FETCH_DB_DATA . Visualizer_Plugin::VERSION ), | |
| 694 | + 'json_get_roots' => wp_create_nonce( Visualizer_Plugin::ACTION_JSON_GET_ROOTS . Visualizer_Plugin::VERSION ), | |
| 695 | + 'json_get_data' => wp_create_nonce( Visualizer_Plugin::ACTION_JSON_GET_DATA . Visualizer_Plugin::VERSION ), | |
| 696 | + 'json_set_schedule' => wp_create_nonce( Visualizer_Plugin::ACTION_JSON_SET_SCHEDULE . Visualizer_Plugin::VERSION ), | |
| 354 | 697 | ), |
| 355 | 698 | 'actions' => array( |
| 356 | 699 | 'permissions' => Visualizer_Plugin::ACTION_FETCH_PERMISSIONS_DATA, |
| 700 | + 'db_get_data' => Visualizer_Plugin::ACTION_FETCH_DB_DATA, | |
| 701 | + 'json_get_roots' => Visualizer_Plugin::ACTION_JSON_GET_ROOTS, | |
| 702 | + 'json_get_data' => Visualizer_Plugin::ACTION_JSON_GET_DATA, | |
| 703 | + 'json_set_schedule' => Visualizer_Plugin::ACTION_JSON_SET_SCHEDULE, | |
| 357 | 704 | ), |
| 358 | 705 | ), |
| 706 | + 'db_query' => array( | |
| 707 | + 'tables' => $table_col_mapping, | |
| 708 | + ), | |
| 709 | + 'is_pro' => Visualizer_Module::is_pro(), | |
| 710 | + 'page_type' => 'chart', | |
| 711 | + 'json_tag_separator' => Visualizer_Source_Json::TAG_SEPARATOR, | |
| 712 | + 'json_tag_separator_view' => Visualizer_Source_Json::TAG_SEPARATOR_VIEW, | |
| 713 | + 'is_front' => false, | |
| 359 | 714 | ) |
| 360 | 715 | ); |
| 716 | + | |
| 361 | 717 | $render = new Visualizer_Render_Page_Data(); |
| 362 | 718 | $render->chart = $this->_chart; |
| 363 | 719 | $render->type = $data['type']; |
| 720 | + $render->custom_css = $data['css']; | |
| 364 | 721 | $render->sidebar = $sidebar; |
| 365 | 722 | if ( filter_input( INPUT_GET, 'library', FILTER_VALIDATE_BOOLEAN ) ) { |
| 366 | - $render->button = filter_input( INPUT_GET, 'action' ) == Visualizer_Plugin::ACTION_EDIT_CHART | |
| 723 | + $render->button = filter_input( INPUT_GET, 'action' ) === Visualizer_Plugin::ACTION_EDIT_CHART | |
| 367 | 724 | ? esc_html__( 'Save Chart', 'visualizer' ) |
| 368 | 725 | : esc_html__( 'Create Chart', 'visualizer' ); |
| 369 | - if ( filter_input( INPUT_GET, 'action' ) == Visualizer_Plugin::ACTION_EDIT_CHART ) { | |
| 726 | + if ( filter_input( INPUT_GET, 'action' ) === Visualizer_Plugin::ACTION_EDIT_CHART ) { | |
| 370 | 727 | $render->cancel_button = esc_html__( 'Cancel', 'visualizer' ); |
| 371 | 728 | } |
| 372 | 729 | } else { |
| 373 | 730 | $render->button = esc_attr__( 'Insert Chart', 'visualizer' ); |
| 374 | 731 | } |
| 375 | - if ( VISUALIZER_PRO ) { | |
| 732 | + | |
| 733 | + do_action( 'visualizer_enqueue_scripts_and_styles', $data ); | |
| 734 | + | |
| 735 | + if ( Visualizer_Module::is_pro() && Visualizer_Module::is_pro_older_than( '1.9.0' ) ) { | |
| 376 | 736 | global $Visualizer_Pro; |
| 377 | 737 | $Visualizer_Pro->_enqueueScriptsAndStyles( $data ); |
| 378 | 738 | } |
| 739 | + | |
| 379 | 740 | $this->_addAction( 'admin_head', 'renderFlattrScript' ); |
| 380 | 741 | wp_iframe( array( $render, 'render' ) ); |
| 381 | 742 | } |
| 382 | 743 | |
| @@ -388,13 +749,21 @@ | ||
| 388 | 749 | * @access private |
| 389 | 750 | */ |
| 390 | 751 | private function _handleTypesPage() { |
| 391 | 752 | // process post request |
| 392 | - if ( $_SERVER['REQUEST_METHOD'] == 'POST' && wp_verify_nonce( filter_input( INPUT_POST, 'nonce' ) ) ) { | |
| 753 | + if ( $_SERVER['REQUEST_METHOD'] === 'POST' && wp_verify_nonce( filter_input( INPUT_POST, 'nonce' ) ) ) { | |
| 393 | 754 | $type = filter_input( INPUT_POST, 'type' ); |
| 394 | - if ( in_array( $type, Visualizer_Plugin::getChartTypes() ) ) { | |
| 755 | + $library = filter_input( INPUT_POST, 'chart-library' ); | |
| 756 | + if ( in_array( $type, Visualizer_Plugin::getChartTypes(), true ) ) { | |
| 757 | + if ( empty( $library ) ) { | |
| 758 | + // library cannot be empty. | |
| 759 | + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, 'Chart library empty while creating the chart! Aborting...', 'error', __FILE__, __LINE__ ); | |
| 760 | + return; | |
| 761 | + } | |
| 762 | + | |
| 395 | 763 | // save new chart type |
| 396 | 764 | update_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_CHART_TYPE, $type ); |
| 765 | + update_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_CHART_LIBRARY, $library ); | |
| 397 | 766 | // if the chart has default data, update it with appropriate default data for new type |
| 398 | 767 | if ( filter_var( get_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_DEFAULT_DATA, true ), FILTER_VALIDATE_BOOLEAN ) ) { |
| 399 | 768 | $source = new Visualizer_Source_Csv( VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples' . DIRECTORY_SEPARATOR . $type . '.csv' ); |
| 400 | 769 | $source->fetch(); |
| @@ -401,8 +770,11 @@ | ||
| 401 | 770 | $this->_chart->post_content = $source->getData(); |
| 402 | 771 | wp_update_post( $this->_chart->to_array() ); |
| 403 | 772 | update_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_SERIES, $source->getSeries() ); |
| 404 | 773 | } |
| 774 | + | |
| 775 | + Visualizer_Module_Utility::set_defaults( $this->_chart ); | |
| 776 | + | |
| 405 | 777 | // redirect to next tab |
| 406 | 778 | // changed by Ash/Upwork |
| 407 | 779 | wp_redirect( add_query_arg( 'tab', 'settings' ) ); |
| 408 | 780 | |
| @@ -410,9 +782,9 @@ | ||
| 410 | 782 | } |
| 411 | 783 | } |
| 412 | 784 | $render = new Visualizer_Render_Page_Types(); |
| 413 | 785 | $render->type = get_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true ); |
| 414 | - $render->types = Visualizer_Module_Admin::_getChartTypesLocalized(); | |
| 786 | + $render->types = Visualizer_Module_Admin::_getChartTypesLocalized( false, false, false, 'types' ); | |
| 415 | 787 | $render->chart = $this->_chart; |
| 416 | 788 | wp_enqueue_style( 'visualizer-frame' ); |
| 417 | 789 | wp_enqueue_script( 'visualizer-frame' ); |
| 418 | 790 | wp_iframe( array( $render, 'render' ) ); |
| @@ -428,10 +800,110 @@ | ||
| 428 | 800 | */ |
| 429 | 801 | public function renderFlattrScript() { |
| 430 | 802 | echo ''; |
| 431 | 803 | } |
| 432 | - // changed by Ash/Upwork | |
| 804 | + | |
| 433 | 805 | /** |
| 806 | + * Processes the CSV that is sent in the request as a string. | |
| 807 | + * | |
| 808 | + * @since 3.2.0 | |
| 809 | + */ | |
| 810 | + private function handleCSVasString( $data ) { | |
| 811 | + $source = null; | |
| 812 | + | |
| 813 | + // @issue https://github.com/Codeinwp/visualizer/issues/412 | |
| 814 | + if ( has_filter( 'visualizer_pro_handle_chart_data' ) ) { | |
| 815 | + $source = apply_filters( 'visualizer_pro_handle_chart_data', $data, '' ); | |
| 816 | + } else { | |
| 817 | + // data coming in from the text editor. | |
| 818 | + $tmpfile = tempnam( get_temp_dir(), Visualizer_Plugin::NAME ); | |
| 819 | + $handle = fopen( $tmpfile, 'w' ); | |
| 820 | + $values = preg_split( '/[\n\r]+/', stripslashes( trim( $data ) ) ); | |
| 821 | + if ( $values ) { | |
| 822 | + foreach ( $values as $row ) { | |
| 823 | + if ( empty( $row ) ) { | |
| 824 | + continue; | |
| 825 | + } | |
| 826 | + $columns = explode( ',', $row ); | |
| 827 | + fputcsv( $handle, $columns ); | |
| 828 | + } | |
| 829 | + } | |
| 830 | + $source = new Visualizer_Source_Csv( $tmpfile ); | |
| 831 | + fclose( $handle ); | |
| 832 | + } | |
| 833 | + return $source; | |
| 834 | + } | |
| 835 | + | |
| 836 | + /** | |
| 837 | + * Parses the data uploaded as an HTML table. | |
| 838 | + * | |
| 839 | + * @since 3.2.0 | |
| 840 | + * | |
| 841 | + * @access private | |
| 842 | + */ | |
| 843 | + private function handleTabularData() { | |
| 844 | + $csv = array(); | |
| 845 | + // the datatable mentions the headers twice, so lets remove the duplicates. | |
| 846 | + $headers = array_unique( array_filter( $_POST['header'] ) ); | |
| 847 | + $types = $_POST['type']; | |
| 848 | + | |
| 849 | + // capture all the indexes that correspond to excluded columns. | |
| 850 | + $exclude = array(); | |
| 851 | + $index = 0; | |
| 852 | + foreach ( $types as $type ) { | |
| 853 | + if ( empty( $type ) ) { | |
| 854 | + $exclude[] = $index; | |
| 855 | + } | |
| 856 | + $index++; | |
| 857 | + } | |
| 858 | + | |
| 859 | + // when N headers are being renamed, the number of headers increases by N | |
| 860 | + // because of the way datatable duplicates header information | |
| 861 | + // so unset the headers that have been renamed. | |
| 862 | + if ( count( $headers ) !== count( $types ) ) { | |
| 863 | + $to = count( $headers ); | |
| 864 | + for ( $i = count( $types ); $i < $to; $i++ ) { | |
| 865 | + unset( $headers[ $i + 1 ] ); | |
| 866 | + } | |
| 867 | + } | |
| 868 | + | |
| 869 | + $columns = array(); | |
| 870 | + for ( $i = 0; $i < count( $headers ); $i++ ) { | |
| 871 | + if ( ! isset( $_POST[ 'data' . $i ] ) ) { | |
| 872 | + continue; | |
| 873 | + } | |
| 874 | + $columns[ $i ] = $_POST[ 'data' . $i ]; | |
| 875 | + } | |
| 876 | + | |
| 877 | + $csv[] = $headers; | |
| 878 | + $csv[] = $types; | |
| 879 | + for ( $j = 0; $j < count( $columns[0] ); $j++ ) { | |
| 880 | + $row = array(); | |
| 881 | + for ( $i = 0; $i < count( $headers ); $i++ ) { | |
| 882 | + $row[] = $columns[ $i ][ $j ]; | |
| 883 | + } | |
| 884 | + $csv[] = $row; | |
| 885 | + } | |
| 886 | + | |
| 887 | + $tmpfile = tempnam( get_temp_dir(), Visualizer_Plugin::NAME ); | |
| 888 | + $handle = fopen( $tmpfile, 'w' ); | |
| 889 | + | |
| 890 | + if ( $csv ) { | |
| 891 | + $index = 0; | |
| 892 | + foreach ( $csv as $row ) { | |
| 893 | + // remove all the cells corresponding to the excluded headers. | |
| 894 | + foreach ( $exclude as $j ) { | |
| 895 | + unset( $row[ $j ] ); | |
| 896 | + } | |
| 897 | + fputcsv( $handle, $row ); | |
| 898 | + } | |
| 899 | + } | |
| 900 | + $source = new Visualizer_Source_Csv( $tmpfile ); | |
| 901 | + fclose( $handle ); | |
| 902 | + return $source; | |
| 903 | + } | |
| 904 | + | |
| 905 | + /** | |
| 434 | 906 | * Parses uploaded CSV file and saves new data for the chart. |
| 435 | 907 | * |
| 436 | 908 | * @since 1.0.0 |
| 437 | 909 | * |
| @@ -453,9 +925,9 @@ | ||
| 453 | 925 | |
| 454 | 926 | // check chart, if chart exists |
| 455 | 927 | // do not use filter_input as it does not work for phpunit test cases, use filter_var instead |
| 456 | 928 | $chart_id = isset( $_GET['chart'] ) ? filter_var( $_GET['chart'], FILTER_VALIDATE_INT ) : ''; |
| 457 | - if ( ! $chart_id || ! ( $chart = get_post( $chart_id ) ) || $chart->post_type != Visualizer_Plugin::CPT_VISUALIZER ) { | |
| 929 | + if ( ! $chart_id || ! ( $chart = get_post( $chart_id ) ) || $chart->post_type !== Visualizer_Plugin::CPT_VISUALIZER ) { | |
| 458 | 930 | if ( ! $can_die ) { |
| 459 | 931 | return; |
| 460 | 932 | } |
| 461 | 933 | status_header( 400 ); |
| @@ -467,11 +939,22 @@ | ||
| 467 | 939 | } |
| 468 | 940 | |
| 469 | 941 | if ( ! isset( $_POST['chart_data_src'] ) || Visualizer_Plugin::CF_SOURCE_FILTER !== $_POST['chart_data_src'] ) { |
| 470 | 942 | // delete the filters in case this chart is being uploaded from other data sources |
| 471 | - delete_post_meta( $chart_id, 'visualizer-filter-config' ); | |
| 943 | + delete_post_meta( $chart_id, Visualizer_Plugin::CF_FILTER_CONFIG ); | |
| 944 | + delete_post_meta( $chart_id, '__transient-' . Visualizer_Plugin::CF_FILTER_CONFIG ); | |
| 945 | + delete_post_meta( $chart_id, '__transient-' . Visualizer_Plugin::CF_DB_QUERY ); | |
| 946 | + | |
| 947 | + // delete "import from db" specific parameters. | |
| 948 | + delete_post_meta( $chart_id, Visualizer_Plugin::CF_DB_QUERY ); | |
| 949 | + delete_post_meta( $chart_id, Visualizer_Plugin::CF_DB_SCHEDULE ); | |
| 472 | 950 | } |
| 473 | 951 | |
| 952 | + // delete json related data. | |
| 953 | + delete_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_URL ); | |
| 954 | + delete_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_ROOT ); | |
| 955 | + delete_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_PAGING ); | |
| 956 | + | |
| 474 | 957 | $source = null; |
| 475 | 958 | $render = new Visualizer_Render_Page_Update(); |
| 476 | 959 | if ( isset( $_POST['remote_data'] ) && filter_var( $_POST['remote_data'], FILTER_VALIDATE_URL ) ) { |
| 477 | 960 | $source = new Visualizer_Source_Csv_Remote( $_POST['remote_data'] ); |
| @@ -477,26 +960,52 @@ | ||
| 477 | 960 | $source = new Visualizer_Source_Csv_Remote( $_POST['remote_data'] ); |
| 478 | 961 | if ( isset( $_POST['vz-import-time'] ) ) { |
| 479 | 962 | apply_filters( 'visualizer_pro_chart_schedule', $chart_id, $_POST['remote_data'], $_POST['vz-import-time'] ); |
| 480 | 963 | } |
| 964 | + // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison | |
| 481 | 965 | } elseif ( isset( $_FILES['local_data'] ) && $_FILES['local_data']['error'] == 0 ) { |
| 482 | 966 | $source = new Visualizer_Source_Csv( $_FILES['local_data']['tmp_name'] ); |
| 483 | 967 | } elseif ( isset( $_POST['chart_data'] ) && strlen( $_POST['chart_data'] ) > 0 ) { |
| 484 | - $source = apply_filters( 'visualizer_pro_handle_chart_data', $_POST['chart_data'], '' ); | |
| 968 | + $source = $this->handleCSVasString( $_POST['chart_data'] ); | |
| 969 | + } elseif ( isset( $_POST['table_data'] ) && 'yes' === $_POST['table_data'] ) { | |
| 970 | + $source = $this->handleTabularData(); | |
| 485 | 971 | } else { |
| 486 | - $render->message = esc_html__( 'CSV file with chart data was not uploaded. Please, try again.', 'visualizer' ); | |
| 972 | + $render->message = esc_html__( 'CSV file with chart data was not uploaded. Please try again.', 'visualizer' ); | |
| 487 | 973 | } |
| 488 | 974 | if ( $source ) { |
| 489 | 975 | if ( $source->fetch() ) { |
| 490 | - $chart->post_content = $source->getData(); | |
| 976 | + $content = $source->getData(); | |
| 977 | + $populate = true; | |
| 978 | + if ( is_string( $content ) && is_array( unserialize( $content ) ) ) { | |
| 979 | + $json = unserialize( $content ); | |
| 980 | + // if source exists, so should data. if source exists but data is blank, do not populate the chart. | |
| 981 | + // if we populate the data even if it is empty, the chart will show "Table has no columns". | |
| 982 | + if ( array_key_exists( 'source', $json ) && ! empty( $json['source'] ) && ( ! array_key_exists( 'data', $json ) || empty( $json['data'] ) ) ) { | |
| 983 | + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Not populating chart data as source exists (%s) but data is empty!', $json['source'] ), 'warn', __FILE__, __LINE__ ); | |
| 984 | + $populate = false; | |
| 985 | + } | |
| 986 | + } | |
| 987 | + if ( $populate ) { | |
| 988 | + $chart->post_content = $content; | |
| 989 | + } | |
| 491 | 990 | wp_update_post( $chart->to_array() ); |
| 492 | 991 | update_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, $source->getSeries() ); |
| 493 | 992 | update_post_meta( $chart->ID, Visualizer_Plugin::CF_SOURCE, $source->getSourceName() ); |
| 494 | 993 | update_post_meta( $chart->ID, Visualizer_Plugin::CF_DEFAULT_DATA, 0 ); |
| 994 | + | |
| 995 | + Visualizer_Module_Utility::set_defaults( $chart, null ); | |
| 996 | + | |
| 997 | + $settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true ); | |
| 998 | + | |
| 999 | + $render->id = $chart->ID; | |
| 495 | 1000 | $render->data = json_encode( $source->getRawData() ); |
| 496 | 1001 | $render->series = json_encode( $source->getSeries() ); |
| 1002 | + $render->settings = json_encode( $settings ); | |
| 497 | 1003 | } else { |
| 498 | - $render->message = esc_html__( 'CSV file is broken or invalid. Please, try again.', 'visualizer' ); | |
| 1004 | + $render->message = $source->get_error(); | |
| 1005 | + if ( empty( $render->message ) ) { | |
| 1006 | + $render->message = esc_html__( 'CSV file is broken or invalid. Please try again.', 'visualizer' ); | |
| 1007 | + } | |
| 499 | 1008 | } |
| 500 | 1009 | } |
| 501 | 1010 | $render->render(); |
| 502 | 1011 | if ( ! $can_die ) { |
| @@ -513,24 +1022,18 @@ | ||
| 513 | 1022 | * @access public |
| 514 | 1023 | */ |
| 515 | 1024 | public function cloneChart() { |
| 516 | 1025 | $chart_id = $success = false; |
| 517 | - $nonce = wp_verify_nonce( filter_input( INPUT_GET, 'nonce' ), Visualizer_Plugin::ACTION_CLONE_CHART ); | |
| 1026 | + $nonce = isset( $_GET['nonce'] ) && wp_verify_nonce( $_GET['nonce'], Visualizer_Plugin::ACTION_CLONE_CHART ); | |
| 518 | 1027 | $capable = current_user_can( 'edit_posts' ); |
| 519 | 1028 | if ( $nonce && $capable ) { |
| 520 | - $chart_id = filter_input( | |
| 521 | - INPUT_GET, 'chart', FILTER_VALIDATE_INT, array( | |
| 522 | - 'options' => array( | |
| 523 | - 'min_range' => 1, | |
| 524 | - ), | |
| 525 | - ) | |
| 526 | - ); | |
| 1029 | + $chart_id = isset( $_GET['chart'] ) ? filter_var( $_GET['chart'], FILTER_VALIDATE_INT ) : ''; | |
| 527 | 1030 | if ( $chart_id ) { |
| 528 | 1031 | $chart = get_post( $chart_id ); |
| 529 | - $success = $chart && $chart->post_type == Visualizer_Plugin::CPT_VISUALIZER; | |
| 1032 | + $success = $chart && $chart->post_type === Visualizer_Plugin::CPT_VISUALIZER; | |
| 530 | 1033 | } |
| 531 | 1034 | } |
| 532 | - $redirect = wp_get_referer(); | |
| 1035 | + $redirect = remove_query_arg( 'vaction', wp_get_referer() ); | |
| 533 | 1036 | if ( $success ) { |
| 534 | 1037 | $new_chart_id = wp_insert_post( |
| 535 | 1038 | array( |
| 536 | 1039 | 'post_type' => Visualizer_Plugin::CPT_VISUALIZER, |
| @@ -539,22 +1042,32 @@ | ||
| 539 | 1042 | 'post_status' => $chart->post_status, |
| 540 | 1043 | 'post_content' => $chart->post_content, |
| 541 | 1044 | ) |
| 542 | 1045 | ); |
| 543 | - if ( $new_chart_id && ! is_wp_error( $new_chart_id ) ) { | |
| 544 | - add_post_meta( $new_chart_id, Visualizer_Plugin::CF_CHART_TYPE, get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_TYPE, true ) ); | |
| 545 | - add_post_meta( $new_chart_id, Visualizer_Plugin::CF_DEFAULT_DATA, get_post_meta( $chart_id, Visualizer_Plugin::CF_DEFAULT_DATA, true ) ); | |
| 546 | - add_post_meta( $new_chart_id, Visualizer_Plugin::CF_SOURCE, get_post_meta( $chart_id, Visualizer_Plugin::CF_SOURCE, true ) ); | |
| 547 | - add_post_meta( $new_chart_id, Visualizer_Plugin::CF_SERIES, get_post_meta( $chart_id, Visualizer_Plugin::CF_SERIES, true ) ); | |
| 548 | - add_post_meta( $new_chart_id, Visualizer_Plugin::CF_SETTINGS, get_post_meta( $chart_id, Visualizer_Plugin::CF_SETTINGS, true ) ); | |
| 1046 | + if ( is_wp_error( $new_chart_id ) ) { | |
| 1047 | + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Error while cloning chart %d = %s', $chart_id, print_r( $new_chart_id, true ) ), 'error', __FILE__, __LINE__ ); | |
| 1048 | + } else { | |
| 1049 | + $post_meta = get_post_meta( $chart_id ); | |
| 1050 | + foreach ( $post_meta as $key => $value ) { | |
| 1051 | + if ( strpos( $key, 'visualizer-' ) !== false ) { | |
| 1052 | + add_post_meta( $new_chart_id, $key, maybe_unserialize( $value[0] ) ); | |
| 1053 | + } | |
| 1054 | + } | |
| 549 | 1055 | $redirect = add_query_arg( |
| 550 | 1056 | array( |
| 551 | 1057 | 'page' => 'visualizer', |
| 552 | 1058 | 'type' => filter_input( INPUT_GET, 'type' ), |
| 553 | - ), admin_url( 'upload.php' ) | |
| 1059 | + 'vaction' => false, | |
| 1060 | + ), | |
| 1061 | + admin_url( 'admin.php' ) | |
| 554 | 1062 | ); |
| 555 | 1063 | } |
| 556 | 1064 | } |
| 1065 | + | |
| 1066 | + if ( defined( 'WP_TESTS_DOMAIN' ) ) { | |
| 1067 | + wp_die(); | |
| 1068 | + } | |
| 1069 | + | |
| 557 | 1070 | wp_redirect( $redirect ); |
| 558 | 1071 | exit; |
| 559 | 1072 | } |
| 560 | 1073 | |
| @@ -569,9 +1082,11 @@ | ||
| 569 | 1082 | check_ajax_referer( Visualizer_Plugin::ACTION_EXPORT_DATA . Visualizer_Plugin::VERSION, 'security' ); |
| 570 | 1083 | $capable = current_user_can( 'edit_posts' ); |
| 571 | 1084 | if ( $capable ) { |
| 572 | 1085 | $chart_id = isset( $_GET['chart'] ) ? filter_var( |
| 573 | - $_GET['chart'], FILTER_VALIDATE_INT, array( | |
| 1086 | + $_GET['chart'], | |
| 1087 | + FILTER_VALIDATE_INT, | |
| 1088 | + array( | |
| 574 | 1089 | 'options' => array( |
| 575 | 1090 | 'min_range' => 1, |
| 576 | 1091 | ), |
| 577 | 1092 | ) |
| @@ -598,13 +1113,15 @@ | ||
| 598 | 1113 | $data = $this->_getChartArray(); |
| 599 | 1114 | $render = new Visualizer_Render_Page_Data(); |
| 600 | 1115 | $render->chart = $this->_chart; |
| 601 | 1116 | $render->type = $data['type']; |
| 602 | - unset( $data['settings']['width'], $data['settings']['height'] ); | |
| 1117 | + unset( $data['settings']['width'], $data['settings']['height'], $data['settings']['chartArea'] ); | |
| 603 | 1118 | wp_enqueue_style( 'visualizer-frame' ); |
| 604 | 1119 | wp_enqueue_script( 'visualizer-render' ); |
| 605 | 1120 | wp_localize_script( |
| 606 | - 'visualizer-render', 'visualizer', array( | |
| 1121 | + 'visualizer-render', | |
| 1122 | + 'visualizer', | |
| 1123 | + array( | |
| 607 | 1124 | 'l10n' => array( |
| 608 | 1125 | 'invalid_source' => esc_html__( 'You have entered invalid URL. Please, insert proper URL.', 'visualizer' ), |
| 609 | 1126 | 'loading' => esc_html__( 'Loading...', 'visualizer' ), |
| 610 | 1127 | ), |
| @@ -612,15 +1129,120 @@ | ||
| 612 | 1129 | 'canvas' => $data, |
| 613 | 1130 | ), |
| 614 | 1131 | ) |
| 615 | 1132 | ); |
| 616 | - // Added by Ash/Upwork | |
| 617 | - if ( VISUALIZER_PRO ) { | |
| 1133 | + | |
| 1134 | + do_action( 'visualizer_enqueue_scripts_and_styles', $data ); | |
| 1135 | + | |
| 1136 | + if ( Visualizer_Module::is_pro() && Visualizer_Module::is_pro_older_than( '1.9.0' ) ) { | |
| 618 | 1137 | global $Visualizer_Pro; |
| 619 | 1138 | $Visualizer_Pro->_enqueueScriptsAndStyles( $data ); |
| 620 | 1139 | } |
| 1140 | + | |
| 621 | 1141 | // Added by Ash/Upwork |
| 622 | 1142 | $this->_addAction( 'admin_head', 'renderFlattrScript' ); |
| 623 | 1143 | wp_iframe( array( $render, 'render' ) ); |
| 624 | 1144 | } |
| 625 | 1145 | |
| 1146 | + /** | |
| 1147 | + * Returns the data for the query. | |
| 1148 | + * | |
| 1149 | + * @access public | |
| 1150 | + */ | |
| 1151 | + public function getQueryData() { | |
| 1152 | + check_ajax_referer( Visualizer_Plugin::ACTION_FETCH_DB_DATA . Visualizer_Plugin::VERSION, 'security' ); | |
| 1153 | + | |
| 1154 | + $params = wp_parse_args( $_POST['params'] ); | |
| 1155 | + $source = new Visualizer_Source_Query( stripslashes( $params['query'] ) ); | |
| 1156 | + $html = $source->fetch( true ); | |
| 1157 | + $error = ''; | |
| 1158 | + if ( empty( $html ) ) { | |
| 1159 | + $error = $source->get_error(); | |
| 1160 | + wp_send_json_error( array( 'msg' => $error ) ); | |
| 1161 | + } | |
| 1162 | + wp_send_json_success( array( 'table' => $html ) ); | |
| 1163 | + } | |
| 1164 | + | |
| 1165 | + /** | |
| 1166 | + * Saves the query and the schedule. | |
| 1167 | + * | |
| 1168 | + * @access public | |
| 1169 | + */ | |
| 1170 | + public function saveQuery() { | |
| 1171 | + check_ajax_referer( Visualizer_Plugin::ACTION_SAVE_DB_QUERY . Visualizer_Plugin::VERSION, 'security' ); | |
| 1172 | + | |
| 1173 | + $chart_id = filter_input( | |
| 1174 | + INPUT_GET, | |
| 1175 | + 'chart', | |
| 1176 | + FILTER_VALIDATE_INT, | |
| 1177 | + array( | |
| 1178 | + 'options' => array( | |
| 1179 | + 'min_range' => 1, | |
| 1180 | + ), | |
| 1181 | + ) | |
| 1182 | + ); | |
| 1183 | + | |
| 1184 | + $render = new Visualizer_Render_Page_Update(); | |
| 1185 | + if ( $chart_id ) { | |
| 1186 | + $params = wp_parse_args( $_POST['params'] ); | |
| 1187 | + $source = new Visualizer_Source_Query( stripslashes( $params['query'] ) ); | |
| 1188 | + $source->fetch( false ); | |
| 1189 | + $error = $source->get_error(); | |
| 1190 | + if ( empty( $error ) ) { | |
| 1191 | + $hours = $_POST['refresh']; | |
| 1192 | + update_post_meta( $chart_id, Visualizer_Plugin::CF_DB_QUERY, stripslashes( $params['query'] ) ); | |
| 1193 | + update_post_meta( $chart_id, Visualizer_Plugin::CF_SOURCE, $source->getSourceName() ); | |
| 1194 | + update_post_meta( $chart_id, Visualizer_Plugin::CF_SERIES, $source->getSeries() ); | |
| 1195 | + update_post_meta( $chart_id, Visualizer_Plugin::CF_DB_SCHEDULE, $hours ); | |
| 1196 | + update_post_meta( $chart_id, Visualizer_Plugin::CF_DEFAULT_DATA, 0 ); | |
| 1197 | + | |
| 1198 | + $schedules = get_option( Visualizer_Plugin::CF_DB_SCHEDULE, array() ); | |
| 1199 | + $schedules[ $chart_id ] = time() + $hours * HOUR_IN_SECONDS; | |
| 1200 | + update_option( Visualizer_Plugin::CF_DB_SCHEDULE, $schedules ); | |
| 1201 | + | |
| 1202 | + wp_update_post( | |
| 1203 | + array( | |
| 1204 | + 'ID' => $chart_id, | |
| 1205 | + 'post_content' => $source->getData(), | |
| 1206 | + ) | |
| 1207 | + ); | |
| 1208 | + $render->data = json_encode( $source->getRawData() ); | |
| 1209 | + $render->series = json_encode( $source->getSeries() ); | |
| 1210 | + } else { | |
| 1211 | + $render->message = $error; | |
| 1212 | + } | |
| 1213 | + } | |
| 1214 | + $render->render(); | |
| 1215 | + if ( ! ( defined( 'VISUALIZER_DO_NOT_DIE' ) && VISUALIZER_DO_NOT_DIE ) ) { | |
| 1216 | + defined( 'WP_TESTS_DOMAIN' ) ? wp_die() : exit(); | |
| 1217 | + } | |
| 1218 | + } | |
| 1219 | + | |
| 1220 | + | |
| 1221 | + /** | |
| 1222 | + * Saves the filter query and the schedule. | |
| 1223 | + * | |
| 1224 | + * @access public | |
| 1225 | + */ | |
| 1226 | + public function saveFilter() { | |
| 1227 | + check_ajax_referer( Visualizer_Plugin::ACTION_SAVE_FILTER_QUERY . Visualizer_Plugin::VERSION, 'security' ); | |
| 1228 | + | |
| 1229 | + $chart_id = filter_input( | |
| 1230 | + INPUT_GET, | |
| 1231 | + 'chart', | |
| 1232 | + FILTER_VALIDATE_INT, | |
| 1233 | + array( | |
| 1234 | + 'options' => array( | |
| 1235 | + 'min_range' => 1, | |
| 1236 | + ), | |
| 1237 | + ) | |
| 1238 | + ); | |
| 1239 | + | |
| 1240 | + $hours = $_POST['refresh']; | |
| 1241 | + | |
| 1242 | + do_action( 'visualizer_save_filter', $chart_id, $hours ); | |
| 1243 | + | |
| 1244 | + if ( ! ( defined( 'VISUALIZER_DO_NOT_DIE' ) && VISUALIZER_DO_NOT_DIE ) ) { | |
| 1245 | + defined( 'WP_TESTS_DOMAIN' ) ? wp_die() : exit(); | |
| 1246 | + } | |
| 1247 | + } | |
| 626 | 1248 | } |