示例#1
0
 /**
  * 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;
 }