/** * Parses uploaded CSV file and saves new data for the chart. * * @since 1.0.0 * * @access public */ public function uploadData() { // validate nonce if (!wp_verify_nonce(filter_input(INPUT_GET, 'nonce'))) { status_header(403); exit; } // check chart, if chart exists $chart_id = filter_input(INPUT_GET, 'chart', FILTER_VALIDATE_INT); if (!$chart_id || !($chart = get_post($chart_id)) || $chart->post_type != Visualizer_Plugin::CPT_VISUALIZER) { status_header(400); exit; } $source = null; $render = new Visualizer_Render_Page_Update(); if (filter_input(INPUT_POST, 'remote_data', FILTER_VALIDATE_URL)) { $source = new Visualizer_Source_Csv_Remote($_POST['remote_data']); } elseif (isset($_FILES['local_data']) && $_FILES['local_data']['error'] == 0) { $source = new Visualizer_Source_Csv($_FILES['local_data']['tmp_name']); } else { $render->message = esc_html__("CSV file with chart data was not uploaded. Please, try again.", Visualizer_Plugin::NAME); } if ($source) { if ($source->fetch()) { $chart->post_content = $source->getData(); wp_update_post($chart->to_array()); update_post_meta($chart->ID, Visualizer_Plugin::CF_SERIES, $source->getSeries()); update_post_meta($chart->ID, Visualizer_Plugin::CF_SOURCE, $source->getSourceName()); update_post_meta($chart->ID, Visualizer_Plugin::CF_DEFAULT_DATA, 0); $render->data = json_encode($source->getRawData()); $render->series = json_encode($source->getSeries()); } else { $render->message = esc_html__("CSV file is broken or invalid. Please, try again.", Visualizer_Plugin::NAME); } } $render->render(); exit; }
/** * Renders appropriate page for chart builder. Creates new auto draft chart * if no chart has been specified. * * @since 1.0.0 * * @access public */ public function renderChartPages() { define('IFRAME_REQUEST', 1); // check chart, if chart not exists, will create new one and redirects to the same page with proper chart id $chart_id = filter_input(INPUT_GET, 'chart', FILTER_VALIDATE_INT); if (!$chart_id || !($chart = get_post($chart_id)) || $chart->post_type != Visualizer_Plugin::CPT_VISUALIZER) { $default_type = 'line'; $source = new Visualizer_Source_Csv(VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples' . DIRECTORY_SEPARATOR . $default_type . '.csv'); $source->fetch(); $chart_id = wp_insert_post(array('post_type' => Visualizer_Plugin::CPT_VISUALIZER, 'post_title' => 'Visualization', 'post_author' => get_current_user_id(), 'post_status' => 'auto-draft', 'post_content' => $source->getData())); if ($chart_id && !is_wp_error($chart_id)) { add_post_meta($chart_id, Visualizer_Plugin::CF_CHART_TYPE, $default_type); add_post_meta($chart_id, Visualizer_Plugin::CF_DEFAULT_DATA, 1); add_post_meta($chart_id, Visualizer_Plugin::CF_SOURCE, $source->getSourceName()); add_post_meta($chart_id, Visualizer_Plugin::CF_SERIES, $source->getSeries()); add_post_meta($chart_id, Visualizer_Plugin::CF_SETTINGS, array()); } wp_redirect(add_query_arg('chart', (int) $chart_id)); exit; } // enqueue and register scripts and styles wp_register_style('visualizer-frame', VISUALIZER_ABSURL . 'css/frame.css', array(), Visualizer_Plugin::VERSION); wp_register_script('visualizer-frame', VISUALIZER_ABSURL . 'js/frame.js', array('jquery'), Visualizer_Plugin::VERSION, true); wp_register_script('google-jsapi', '//www.google.com/jsapi', array(), null, true); wp_register_script('visualizer-render', VISUALIZER_ABSURL . 'js/render.js', array('google-jsapi', 'visualizer-frame'), Visualizer_Plugin::VERSION, true); wp_register_script('visualizer-preview', VISUALIZER_ABSURL . 'js/preview.js', array('wp-color-picker', 'visualizer-render'), Visualizer_Plugin::VERSION, true); // added by Ash/Upwork if (defined('Visualizer_Pro')) { global $Visualizer_Pro; $Visualizer_Pro->_addScriptsAndStyles(); } // dispatch pages $this->_chart = $chart; switch (filter_input(INPUT_GET, 'tab')) { case 'data': $this->_handleDataPage(); break; case 'settings': $this->_handleSettingsPage(); break; case 'type': default: $this->_handleTypesPage(); break; } exit; }