/** * 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; }
/** * Returns file handle to fetch data from. * * @since 1.4.2 * * @access protected * @staticvar boolean $allow_url_fopen Determines whether or not allow_url_fopen option is enabled. * @param string $filename Optional file name to get handle. If omitted, $_filename is used. * @return resource File handle resource on success, otherwise FALSE. */ protected function _get_file_handle($filename = false) { static $allow_url_fopen = null; if ($this->_tmpfile && is_readable($this->_tmpfile)) { return parent::_get_file_handle($this->_tmpfile); } if (is_null($allow_url_fopen)) { $allow_url_fopen = filter_var(ini_get('allow_url_fopen'), FILTER_VALIDATE_BOOLEAN); } $scheme = parse_url($this->_filename, PHP_URL_SCHEME); if ($allow_url_fopen && in_array($scheme, stream_get_wrappers())) { return parent::_get_file_handle($filename); } require_once ABSPATH . 'wp-admin/includes/file.php'; $this->_tmpfile = download_url($this->_filename); return !is_wp_error($this->_tmpfile) ? parent::_get_file_handle($this->_tmpfile) : false; }
/** * Handles chart type selection page. * * @since 1.0.0 * * @access private */ private function _handleTypesPage() { // process post request if ($_SERVER['REQUEST_METHOD'] == 'POST' && wp_verify_nonce(filter_input(INPUT_POST, 'nonce'))) { $type = filter_input(INPUT_POST, 'type'); if (in_array($type, Visualizer_Plugin::getChartTypes())) { // save new chart type update_post_meta($this->_chart->ID, Visualizer_Plugin::CF_CHART_TYPE, $type); // if the chart has default data, update it with appropriate default data for new type if (filter_var(get_post_meta($this->_chart->ID, Visualizer_Plugin::CF_DEFAULT_DATA, true), FILTER_VALIDATE_BOOLEAN)) { $source = new Visualizer_Source_Csv(VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples' . DIRECTORY_SEPARATOR . $type . '.csv'); $source->fetch(); $this->_chart->post_content = $source->getData(); wp_update_post($this->_chart->to_array()); update_post_meta($this->_chart->ID, Visualizer_Plugin::CF_SERIES, $source->getSeries()); } // redirect to next tab wp_redirect(add_query_arg('tab', 'data')); return; } } $render = new Visualizer_Render_Page_Types(); $render->type = get_post_meta($this->_chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true); $render->types = Visualizer_Plugin::getChartTypes(); $render->chart = $this->_chart; wp_enqueue_style('visualizer-frame'); wp_enqueue_script('visualizer-frame'); wp_iframe(array($render, 'render')); }