| 1 |
<?php |
| 2 |
|
| 3 |
// +----------------------------------------------------------------------+ |
| 4 |
// | Copyright 2013 Madpixels (email : visualizer@madpixels.net) | |
| 5 |
// +----------------------------------------------------------------------+ |
| 6 |
// | This program is free software; you can redistribute it and/or modify | |
| 7 |
// | it under the terms of the GNU General Public License, version 2, as | |
| 8 |
// | published by the Free Software Foundation. | |
| 9 |
// | | |
| 10 |
// | This program is distributed in the hope that it will be useful, | |
| 11 |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 |
// | GNU General Public License for more details. | |
| 14 |
// | | |
| 15 |
// | You should have received a copy of the GNU General Public License | |
| 16 |
// | along with this program; if not, write to the Free Software | |
| 17 |
// | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, | |
| 18 |
// | MA 02110-1301 USA | |
| 19 |
// +----------------------------------------------------------------------+ |
| 20 |
// | Author: Eugene Manuilov <eugene@manuilov.org> | |
| 21 |
// +----------------------------------------------------------------------+ |
| 22 |
/** |
| 23 |
* The module for all stuff related to getting, editing, creating and deleting charts. |
| 24 |
* |
| 25 |
* @category Visualizer |
| 26 |
* @package Module |
| 27 |
* |
| 28 |
* @since 1.0.0 |
| 29 |
*/ |
| 30 |
class Visualizer_Module_Chart extends Visualizer_Module { |
| 31 |
|
| 32 |
const NAME = __CLASS__; |
| 33 |
|
| 34 |
/** |
| 35 |
* The chart object. |
| 36 |
* |
| 37 |
* @since 1.0.0 |
| 38 |
* |
| 39 |
* @access private |
| 40 |
* @var WP_Post |
| 41 |
*/ |
| 42 |
private $_chart; |
| 43 |
|
| 44 |
/** |
| 45 |
* Constructor. |
| 46 |
* |
| 47 |
* @since 1.0.0 |
| 48 |
* |
| 49 |
* @access public |
| 50 |
* @param Visualizer_Plugin $plugin The instance of the plugin. |
| 51 |
*/ |
| 52 |
public function __construct( Visualizer_Plugin $plugin ) { |
| 53 |
parent::__construct( $plugin ); |
| 54 |
|
| 55 |
$this->_addAjaxAction( Visualizer_Plugin::ACTION_GET_CHARTS, 'getCharts' ); |
| 56 |
$this->_addAjaxAction( Visualizer_Plugin::ACTION_DELETE_CHART, 'deleteChart' ); |
| 57 |
$this->_addAjaxAction( Visualizer_Plugin::ACTION_CREATE_CHART, 'renderChartPages' ); |
| 58 |
$this->_addAjaxAction( Visualizer_Plugin::ACTION_EDIT_CHART, 'renderChartPages' ); |
| 59 |
$this->_addAjaxAction( Visualizer_Plugin::ACTION_UPLOAD_DATA, 'uploadData' ); |
| 60 |
$this->_addAjaxAction( Visualizer_Plugin::ACTION_CLONE_CHART, 'cloneChart' ); |
| 61 |
|
| 62 |
// Added by Ash/Upwork |
| 63 |
$this->_addAjaxAction( Visualizer_Plugin::ACTION_EXPORT_DATA, 'exportData' ); |
| 64 |
if ( defined( 'Visualizer_Pro' ) ) { |
| 65 |
global $Visualizer_Pro; |
| 66 |
list($action, $name, $class) = $Visualizer_Pro->_getAjaxAction( $this ); |
| 67 |
$this->_addAjaxAction( $action, $name, $class ); |
| 68 |
} |
| 69 |
// Added by Ash/Upwork |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Sends json response. |
| 74 |
* |
| 75 |
* @since 1.0.0 |
| 76 |
* |
| 77 |
* @access private |
| 78 |
* @param array $results The response array. |
| 79 |
*/ |
| 80 |
public function _sendResponse( $results ) { |
| 81 |
header( 'Content-type: application/json' ); |
| 82 |
nocache_headers(); |
| 83 |
|
| 84 |
echo json_encode( $results ); |
| 85 |
exit; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Returns chart data required for rendering. |
| 90 |
* |
| 91 |
* @since 1.0.0 |
| 92 |
* |
| 93 |
* @access private |
| 94 |
* @param WP_Post $chart The chart object. |
| 95 |
* @return array The array of chart data. |
| 96 |
*/ |
| 97 |
private function _getChartArray( WP_Post $chart = null ) { |
| 98 |
if ( is_null( $chart ) ) { |
| 99 |
$chart = $this->_chart; |
| 100 |
} |
| 101 |
|
| 102 |
$type = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true ); |
| 103 |
$series = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SERIES, get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true ), $chart->ID, $type ); |
| 104 |
$data = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA, unserialize( $chart->post_content ), $chart->ID, $type ); |
| 105 |
|
| 106 |
return array( |
| 107 |
'type' => $type, |
| 108 |
'series' => $series, |
| 109 |
'settings' => get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true ), |
| 110 |
'data' => $data, |
| 111 |
); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Fetches charts from database. |
| 116 |
* |
| 117 |
* @since 1.0.0 |
| 118 |
* |
| 119 |
* @access public |
| 120 |
*/ |
| 121 |
public function getCharts() { |
| 122 |
$query_args = array( |
| 123 |
'post_type' => Visualizer_Plugin::CPT_VISUALIZER, |
| 124 |
'posts_per_page' => 9, |
| 125 |
'paged' => filter_input( INPUT_GET, 'page', FILTER_VALIDATE_INT, array( |
| 126 |
'options' => array( |
| 127 |
'min_range' => 1, |
| 128 |
'default' => 1, |
| 129 |
), |
| 130 |
) ), |
| 131 |
); |
| 132 |
|
| 133 |
$filter = filter_input( INPUT_GET, 'filter', FILTER_SANITIZE_STRING ); |
| 134 |
if ( $filter && in_array( $filter, Visualizer_Plugin::getChartTypes() ) ) { |
| 135 |
$query_args['meta_query'] = array( |
| 136 |
array( |
| 137 |
'key' => Visualizer_Plugin::CF_CHART_TYPE, |
| 138 |
'value' => $filter, |
| 139 |
'compare' => '=', |
| 140 |
), |
| 141 |
); |
| 142 |
} |
| 143 |
|
| 144 |
$query = new WP_Query( $query_args ); |
| 145 |
|
| 146 |
$charts = array(); |
| 147 |
while ( $query->have_posts() ) { |
| 148 |
$chart = $query->next_post(); |
| 149 |
|
| 150 |
$chart_data = $this->_getChartArray( $chart ); |
| 151 |
$chart_data['id'] = $chart->ID; |
| 152 |
|
| 153 |
$charts[] = $chart_data; |
| 154 |
} |
| 155 |
|
| 156 |
$this->_sendResponse( array( |
| 157 |
'success' => true, |
| 158 |
'data' => $charts, |
| 159 |
'total' => $query->max_num_pages, |
| 160 |
) ); |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Deletes a chart from database. |
| 165 |
* |
| 166 |
* @since 1.0.0 |
| 167 |
* @uses wp_delete_post() To delete a chart. |
| 168 |
* |
| 169 |
* @access public |
| 170 |
*/ |
| 171 |
public function deleteChart() { |
| 172 |
$is_post = $_SERVER['REQUEST_METHOD'] == 'POST'; |
| 173 |
$input_method = $is_post ? INPUT_POST : INPUT_GET; |
| 174 |
|
| 175 |
$chart_id = $success = false; |
| 176 |
$nonce = wp_verify_nonce( filter_input( $input_method, 'nonce' ) ); |
| 177 |
$capable = current_user_can( 'delete_posts' ); |
| 178 |
if ( $nonce && $capable ) { |
| 179 |
$chart_id = filter_input( $input_method, 'chart', FILTER_VALIDATE_INT, array( 'options' => array( 'min_range' => 1 ) ) ); |
| 180 |
if ( $chart_id ) { |
| 181 |
$chart = get_post( $chart_id ); |
| 182 |
$success = $chart && $chart->post_type == Visualizer_Plugin::CPT_VISUALIZER; |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
if ( $success ) { |
| 187 |
wp_delete_post( $chart_id, true ); |
| 188 |
} |
| 189 |
|
| 190 |
if ( $is_post ) { |
| 191 |
$this->_sendResponse( array( 'success' => $success ) ); |
| 192 |
} |
| 193 |
|
| 194 |
wp_redirect( wp_get_referer() ); |
| 195 |
exit; |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Renders appropriate page for chart builder. Creates new auto draft chart |
| 200 |
* if no chart has been specified. |
| 201 |
* |
| 202 |
* @since 1.0.0 |
| 203 |
* |
| 204 |
* @access public |
| 205 |
*/ |
| 206 |
public function renderChartPages() { |
| 207 |
define( 'IFRAME_REQUEST', 1 ); |
| 208 |
|
| 209 |
// check chart, if chart not exists, will create new one and redirects to the same page with proper chart id |
| 210 |
$chart_id = filter_input( INPUT_GET, 'chart', FILTER_VALIDATE_INT ); |
| 211 |
if ( ! $chart_id || ! ( $chart = get_post( $chart_id ) ) || $chart->post_type != Visualizer_Plugin::CPT_VISUALIZER ) { |
| 212 |
$default_type = 'line'; |
| 213 |
|
| 214 |
$source = new Visualizer_Source_Csv( VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples' . DIRECTORY_SEPARATOR . $default_type . '.csv' ); |
| 215 |
$source->fetch(); |
| 216 |
|
| 217 |
$chart_id = wp_insert_post( array( |
| 218 |
'post_type' => Visualizer_Plugin::CPT_VISUALIZER, |
| 219 |
'post_title' => 'Visualization', |
| 220 |
'post_author' => get_current_user_id(), |
| 221 |
'post_status' => 'auto-draft', |
| 222 |
'post_content' => $source->getData(), |
| 223 |
) ); |
| 224 |
|
| 225 |
if ( $chart_id && ! is_wp_error( $chart_id ) ) { |
| 226 |
add_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_TYPE, $default_type ); |
| 227 |
add_post_meta( $chart_id, Visualizer_Plugin::CF_DEFAULT_DATA, 1 ); |
| 228 |
add_post_meta( $chart_id, Visualizer_Plugin::CF_SOURCE, $source->getSourceName() ); |
| 229 |
add_post_meta( $chart_id, Visualizer_Plugin::CF_SERIES, $source->getSeries() ); |
| 230 |
add_post_meta( $chart_id, Visualizer_Plugin::CF_SETTINGS, array( 'focusTarget' => 'datum' ) ); |
| 231 |
} |
| 232 |
|
| 233 |
wp_redirect( add_query_arg( 'chart', (int) $chart_id ) ); |
| 234 |
exit; |
| 235 |
} |
| 236 |
|
| 237 |
// enqueue and register scripts and styles |
| 238 |
wp_register_style( 'visualizer-frame', VISUALIZER_ABSURL . 'css/frame.css', array(), Visualizer_Plugin::VERSION ); |
| 239 |
|
| 240 |
wp_register_script( 'visualizer-frame', VISUALIZER_ABSURL . 'js/frame.js', array( 'jquery' ), Visualizer_Plugin::VERSION, true ); |
| 241 |
wp_register_script( 'google-jsapi-new', '//www.gstatic.com/charts/loader.js', array(), null, true ); |
| 242 |
wp_register_script( 'google-jsapi-old', '//www.google.com/jsapi', array( 'google-jsapi-new' ), null, true ); |
| 243 |
wp_register_script( 'visualizer-render', VISUALIZER_ABSURL . 'js/render.js', array( 'google-jsapi-old', 'google-jsapi-new', 'visualizer-frame' ), Visualizer_Plugin::VERSION, true ); |
| 244 |
wp_register_script( 'visualizer-preview', VISUALIZER_ABSURL . 'js/preview.js', array( 'wp-color-picker', 'visualizer-render' ), Visualizer_Plugin::VERSION, true ); |
| 245 |
|
| 246 |
// added by Ash/Upwork |
| 247 |
if ( defined( 'Visualizer_Pro' ) ) { |
| 248 |
global $Visualizer_Pro; |
| 249 |
$Visualizer_Pro->_addScriptsAndStyles(); |
| 250 |
} |
| 251 |
|
| 252 |
// dispatch pages |
| 253 |
$this->_chart = $chart; |
| 254 |
switch ( filter_input( INPUT_GET, 'tab' ) ) { |
| 255 |
case 'settings': |
| 256 |
// changed by Ash/Upwork |
| 257 |
$this->_handleDataAndSettingsPage(); |
| 258 |
break; |
| 259 |
case 'type': |
| 260 |
default: |
| 261 |
$this->_handleTypesPage(); |
| 262 |
break; |
| 263 |
} |
| 264 |
|
| 265 |
exit; |
| 266 |
} |
| 267 |
|
| 268 |
/** |
| 269 |
* Handles chart type selection page. |
| 270 |
* |
| 271 |
* @since 1.0.0 |
| 272 |
* |
| 273 |
* @access private |
| 274 |
*/ |
| 275 |
private function _handleTypesPage() { |
| 276 |
// process post request |
| 277 |
if ( $_SERVER['REQUEST_METHOD'] == 'POST' && wp_verify_nonce( filter_input( INPUT_POST, 'nonce' ) ) ) { |
| 278 |
$type = filter_input( INPUT_POST, 'type' ); |
| 279 |
if ( in_array( $type, Visualizer_Plugin::getChartTypes() ) ) { |
| 280 |
// save new chart type |
| 281 |
update_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_CHART_TYPE, $type ); |
| 282 |
|
| 283 |
// if the chart has default data, update it with appropriate default data for new type |
| 284 |
if ( filter_var( get_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_DEFAULT_DATA, true ), FILTER_VALIDATE_BOOLEAN ) ) { |
| 285 |
$source = new Visualizer_Source_Csv( VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples' . DIRECTORY_SEPARATOR . $type . '.csv' ); |
| 286 |
$source->fetch(); |
| 287 |
|
| 288 |
$this->_chart->post_content = $source->getData(); |
| 289 |
wp_update_post( $this->_chart->to_array() ); |
| 290 |
|
| 291 |
update_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_SERIES, $source->getSeries() ); |
| 292 |
} |
| 293 |
|
| 294 |
// redirect to next tab |
| 295 |
// changed by Ash/Upwork |
| 296 |
wp_redirect( add_query_arg( 'tab', 'settings' ) ); |
| 297 |
return; |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
$render = new Visualizer_Render_Page_Types(); |
| 302 |
$render->type = get_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true ); |
| 303 |
$render->types = Visualizer_Module_Admin::_getChartTypesLocalized(); |
| 304 |
$render->chart = $this->_chart; |
| 305 |
|
| 306 |
wp_enqueue_style( 'visualizer-frame' ); |
| 307 |
wp_enqueue_script( 'visualizer-frame' ); |
| 308 |
|
| 309 |
wp_iframe( array( $render, 'render' ) ); |
| 310 |
} |
| 311 |
|
| 312 |
/** |
| 313 |
* Handles chart data page. |
| 314 |
* |
| 315 |
* @since 1.0.0 |
| 316 |
* |
| 317 |
* @access private |
| 318 |
*/ |
| 319 |
private function _handleDataPage() { |
| 320 |
$data = $this->_getChartArray(); |
| 321 |
|
| 322 |
$render = new Visualizer_Render_Page_Data(); |
| 323 |
$render->chart = $this->_chart; |
| 324 |
$render->type = $data['type']; |
| 325 |
|
| 326 |
unset( $data['settings']['width'], $data['settings']['height'] ); |
| 327 |
|
| 328 |
wp_enqueue_style( 'visualizer-frame' ); |
| 329 |
wp_enqueue_script( 'visualizer-render' ); |
| 330 |
wp_localize_script( 'visualizer-render', 'visualizer', array( |
| 331 |
'l10n' => array( |
| 332 |
'remotecsv_prompt' => esc_html__( 'Please, enter the URL of CSV file:', 'visualizer' ), |
| 333 |
'invalid_source' => esc_html__( 'You have entered invalid URL. Please, insert proper URL.', 'visualizer' ), |
| 334 |
), |
| 335 |
'charts' => array( |
| 336 |
'canvas' => $data, |
| 337 |
), |
| 338 |
) ); |
| 339 |
|
| 340 |
// Added by Ash/Upwork |
| 341 |
if ( defined( 'Visualizer_Pro' ) ) { |
| 342 |
global $Visualizer_Pro; |
| 343 |
$Visualizer_Pro->_enqueueScriptsAndStyles( $data ); |
| 344 |
} |
| 345 |
// Added by Ash/Upwork |
| 346 |
$this->_addAction( 'admin_head', 'renderFlattrScript' ); |
| 347 |
|
| 348 |
wp_iframe( array( $render, 'render' ) ); |
| 349 |
} |
| 350 |
|
| 351 |
/** |
| 352 |
* Handle data and settings page |
| 353 |
*/ |
| 354 |
private function _handleDataAndSettingsPage() { |
| 355 |
if ( $_SERVER['REQUEST_METHOD'] == 'POST' && wp_verify_nonce( filter_input( INPUT_GET, 'nonce' ) ) ) { |
| 356 |
if ( $this->_chart->post_status == 'auto-draft' ) { |
| 357 |
$this->_chart->post_status = 'publish'; |
| 358 |
wp_update_post( $this->_chart->to_array() ); |
| 359 |
} |
| 360 |
|
| 361 |
update_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_SETTINGS, $_POST ); |
| 362 |
|
| 363 |
$render = new Visualizer_Render_Page_Send(); |
| 364 |
$render->text = sprintf( '[visualizer id="%d"]', $this->_chart->ID ); |
| 365 |
|
| 366 |
wp_iframe( array( $render, 'render' ) ); |
| 367 |
return; |
| 368 |
} |
| 369 |
|
| 370 |
$data = $this->_getChartArray(); |
| 371 |
|
| 372 |
$sidebar = ''; |
| 373 |
$sidebar_class = 'Visualizer_Render_Sidebar_Type_' . ucfirst( $data['type'] ); |
| 374 |
if ( class_exists( $sidebar_class, true ) ) { |
| 375 |
$sidebar = new $sidebar_class( $data['settings'] ); |
| 376 |
$sidebar->__series = $data['series']; |
| 377 |
$sidebar->__data = $data['data']; |
| 378 |
} else { |
| 379 |
$sidebar = apply_filters( 'visualizer_pro_chart_type_sidebar', '', $data ); |
| 380 |
if ( $sidebar != '' ) { |
| 381 |
$sidebar->__series = $data['series']; |
| 382 |
$sidebar->__data = $data['data']; |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
unset( $data['settings']['width'], $data['settings']['height'] ); |
| 387 |
|
| 388 |
wp_enqueue_style( 'visualizer-frame' ); |
| 389 |
wp_enqueue_style( 'wp-color-picker' ); |
| 390 |
wp_enqueue_style( 'visualizer-frame' ); |
| 391 |
|
| 392 |
wp_enqueue_script( 'visualizer-preview' ); |
| 393 |
wp_enqueue_script( 'visualizer-render' ); |
| 394 |
wp_localize_script( 'visualizer-render', 'visualizer', array( |
| 395 |
'l10n' => array( |
| 396 |
'remotecsv_prompt' => esc_html__( 'Please, enter the URL of CSV file:', 'visualizer' ), |
| 397 |
'invalid_source' => esc_html__( 'You have entered invalid URL. Please, insert proper URL.', 'visualizer' ), |
| 398 |
), |
| 399 |
'charts' => array( |
| 400 |
'canvas' => $data, |
| 401 |
), |
| 402 |
) ); |
| 403 |
|
| 404 |
$render = new Visualizer_Render_Page_Data(); |
| 405 |
$render->chart = $this->_chart; |
| 406 |
$render->type = $data['type']; |
| 407 |
|
| 408 |
$render->sidebar = $sidebar; |
| 409 |
if ( filter_input( INPUT_GET, 'library', FILTER_VALIDATE_BOOLEAN ) ) { |
| 410 |
$render->button = filter_input( INPUT_GET, 'action' ) == Visualizer_Plugin::ACTION_EDIT_CHART |
| 411 |
? esc_html__( 'Save Chart', 'visualizer' ) |
| 412 |
: esc_html__( 'Create Chart', 'visualizer' ); |
| 413 |
} else { |
| 414 |
$render->button = esc_attr__( 'Insert Chart', 'visualizer' ); |
| 415 |
} |
| 416 |
|
| 417 |
if ( defined( 'Visualizer_Pro' ) ) { |
| 418 |
global $Visualizer_Pro; |
| 419 |
$Visualizer_Pro->_enqueueScriptsAndStyles( $data ); |
| 420 |
} |
| 421 |
|
| 422 |
$this->_addAction( 'admin_head', 'renderFlattrScript' ); |
| 423 |
|
| 424 |
wp_iframe( array( $render, 'render' ) ); |
| 425 |
} |
| 426 |
// changed by Ash/Upwork |
| 427 |
/** |
| 428 |
* Renders flattr script in the iframe <head> |
| 429 |
* |
| 430 |
* @since 1.4.2 |
| 431 |
* @action admin_head |
| 432 |
* |
| 433 |
* @access public |
| 434 |
*/ |
| 435 |
public function renderFlattrScript() { |
| 436 |
echo ''; |
| 437 |
} |
| 438 |
|
| 439 |
/** |
| 440 |
* Parses uploaded CSV file and saves new data for the chart. |
| 441 |
* |
| 442 |
* @since 1.0.0 |
| 443 |
* |
| 444 |
* @access public |
| 445 |
*/ |
| 446 |
public function uploadData() { |
| 447 |
// validate nonce |
| 448 |
if ( ! wp_verify_nonce( filter_input( INPUT_GET, 'nonce' ) ) ) { |
| 449 |
status_header( 403 ); |
| 450 |
exit; |
| 451 |
} |
| 452 |
|
| 453 |
// check chart, if chart exists |
| 454 |
$chart_id = filter_input( INPUT_GET, 'chart', FILTER_VALIDATE_INT ); |
| 455 |
if ( ! $chart_id || ! ( $chart = get_post( $chart_id ) ) || $chart->post_type != Visualizer_Plugin::CPT_VISUALIZER ) { |
| 456 |
status_header( 400 ); |
| 457 |
exit; |
| 458 |
} |
| 459 |
|
| 460 |
$source = null; |
| 461 |
$render = new Visualizer_Render_Page_Update(); |
| 462 |
if ( filter_input( INPUT_POST, 'remote_data', FILTER_VALIDATE_URL ) ) { |
| 463 |
$source = new Visualizer_Source_Csv_Remote( $_POST['remote_data'] ); |
| 464 |
} elseif ( isset( $_FILES['local_data'] ) && $_FILES['local_data']['error'] == 0 ) { |
| 465 |
$source = new Visualizer_Source_Csv( $_FILES['local_data']['tmp_name'] ); |
| 466 |
|
| 467 |
// Added by Ash/Upwork |
| 468 |
} elseif ( defined( 'Visualizer_Pro' ) && isset( $_POST['chart_data'] ) && strlen( $_POST['chart_data'] ) > 0 ) { |
| 469 |
global $Visualizer_Pro; |
| 470 |
$source = $Visualizer_Pro->_handleChartData( $_POST['chart_data'] ); |
| 471 |
// Added by Ash/Upwork |
| 472 |
} else { |
| 473 |
$render->message = esc_html__( 'CSV file with chart data was not uploaded. Please, try again.', 'visualizer' ); |
| 474 |
} |
| 475 |
|
| 476 |
if ( $source ) { |
| 477 |
if ( $source->fetch() ) { |
| 478 |
$chart->post_content = $source->getData(); |
| 479 |
wp_update_post( $chart->to_array() ); |
| 480 |
|
| 481 |
update_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, $source->getSeries() ); |
| 482 |
update_post_meta( $chart->ID, Visualizer_Plugin::CF_SOURCE, $source->getSourceName() ); |
| 483 |
update_post_meta( $chart->ID, Visualizer_Plugin::CF_DEFAULT_DATA, 0 ); |
| 484 |
|
| 485 |
$render->data = json_encode( $source->getRawData() ); |
| 486 |
$render->series = json_encode( $source->getSeries() ); |
| 487 |
} else { |
| 488 |
$render->message = esc_html__( 'CSV file is broken or invalid. Please, try again.', 'visualizer' ); |
| 489 |
} |
| 490 |
} |
| 491 |
|
| 492 |
$render->render(); |
| 493 |
exit; |
| 494 |
} |
| 495 |
|
| 496 |
/** |
| 497 |
* Clones the chart. |
| 498 |
* |
| 499 |
* @since 1.0.0 |
| 500 |
* |
| 501 |
* @access public |
| 502 |
*/ |
| 503 |
public function cloneChart() { |
| 504 |
$chart_id = $success = false; |
| 505 |
$nonce = wp_verify_nonce( filter_input( INPUT_GET, 'nonce' ), Visualizer_Plugin::ACTION_CLONE_CHART ); |
| 506 |
$capable = current_user_can( 'edit_posts' ); |
| 507 |
if ( $nonce && $capable ) { |
| 508 |
$chart_id = filter_input( INPUT_GET, 'chart', FILTER_VALIDATE_INT, array( 'options' => array( 'min_range' => 1 ) ) ); |
| 509 |
if ( $chart_id ) { |
| 510 |
$chart = get_post( $chart_id ); |
| 511 |
$success = $chart && $chart->post_type == Visualizer_Plugin::CPT_VISUALIZER; |
| 512 |
} |
| 513 |
} |
| 514 |
|
| 515 |
$redirect = wp_get_referer(); |
| 516 |
if ( $success ) { |
| 517 |
$new_chart_id = wp_insert_post( array( |
| 518 |
'post_type' => Visualizer_Plugin::CPT_VISUALIZER, |
| 519 |
'post_title' => 'Visualization', |
| 520 |
'post_author' => get_current_user_id(), |
| 521 |
'post_status' => $chart->post_status, |
| 522 |
'post_content' => $chart->post_content, |
| 523 |
) ); |
| 524 |
|
| 525 |
if ( $new_chart_id && ! is_wp_error( $new_chart_id ) ) { |
| 526 |
add_post_meta( $new_chart_id, Visualizer_Plugin::CF_CHART_TYPE, get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_TYPE, true ) ); |
| 527 |
add_post_meta( $new_chart_id, Visualizer_Plugin::CF_DEFAULT_DATA, get_post_meta( $chart_id, Visualizer_Plugin::CF_DEFAULT_DATA, true ) ); |
| 528 |
add_post_meta( $new_chart_id, Visualizer_Plugin::CF_SOURCE, get_post_meta( $chart_id, Visualizer_Plugin::CF_SOURCE, true ) ); |
| 529 |
add_post_meta( $new_chart_id, Visualizer_Plugin::CF_SERIES, get_post_meta( $chart_id, Visualizer_Plugin::CF_SERIES, true ) ); |
| 530 |
add_post_meta( $new_chart_id, Visualizer_Plugin::CF_SETTINGS, get_post_meta( $chart_id, Visualizer_Plugin::CF_SETTINGS, true ) ); |
| 531 |
|
| 532 |
$redirect = add_query_arg( array( |
| 533 |
'page' => 'visualizer', |
| 534 |
'type' => filter_input( INPUT_GET, 'type' ), |
| 535 |
), admin_url( 'upload.php' ) ); |
| 536 |
} |
| 537 |
} |
| 538 |
|
| 539 |
wp_redirect( $redirect ); |
| 540 |
exit; |
| 541 |
} |
| 542 |
|
| 543 |
/** |
| 544 |
* Exports the chart data |
| 545 |
* |
| 546 |
* @since 1.0.0 |
| 547 |
* |
| 548 |
* @access public |
| 549 |
*/ |
| 550 |
public function exportData() { |
| 551 |
check_ajax_referer( Visualizer_Plugin::ACTION_EXPORT_DATA . Visualizer_Plugin::VERSION, 'security' ); |
| 552 |
|
| 553 |
$chart_id = $success = false; |
| 554 |
$capable = current_user_can( 'edit_posts' ); |
| 555 |
if ( $capable ) { |
| 556 |
$chart_id = filter_input( INPUT_GET, 'chart', FILTER_VALIDATE_INT, array( 'options' => array( 'min_range' => 1 ) ) ); |
| 557 |
if ( $chart_id ) { |
| 558 |
$chart = get_post( $chart_id ); |
| 559 |
$success = $chart && $chart->post_type == Visualizer_Plugin::CPT_VISUALIZER; |
| 560 |
} |
| 561 |
} |
| 562 |
|
| 563 |
if ( $success ) { |
| 564 |
$settings = get_post_meta( $chart_id, Visualizer_Plugin::CF_SETTINGS, true ); |
| 565 |
$filename = $settings['title']; |
| 566 |
if ( empty( $filename ) ) { |
| 567 |
$filename = 'export.csv'; |
| 568 |
} else { |
| 569 |
$filename .= '.csv'; |
| 570 |
} |
| 571 |
$rows = array(); |
| 572 |
$series = get_post_meta( $chart_id, Visualizer_Plugin::CF_SERIES, true ); |
| 573 |
$data = unserialize( $chart->post_content ); |
| 574 |
if ( ! empty( $series ) ) { |
| 575 |
$row = array(); |
| 576 |
foreach ( $series as $array ) { |
| 577 |
$row[] = $array['label']; |
| 578 |
} |
| 579 |
$rows[] = $row; |
| 580 |
|
| 581 |
$row = array(); |
| 582 |
foreach ( $series as $array ) { |
| 583 |
$row[] = $array['type']; |
| 584 |
} |
| 585 |
$rows[] = $row; |
| 586 |
} |
| 587 |
|
| 588 |
if ( ! empty( $data ) ) { |
| 589 |
foreach ( $data as $array ) { |
| 590 |
// ignore strings |
| 591 |
if ( ! is_array( $array ) ) { continue; } |
| 592 |
|
| 593 |
// if this is an array of arrays... |
| 594 |
if ( is_array( $array[0] ) ) { |
| 595 |
foreach ( $array as $arr ) { |
| 596 |
$rows[] = $arr; |
| 597 |
} |
| 598 |
} else { |
| 599 |
// just an array |
| 600 |
$rows[] = $array; |
| 601 |
} |
| 602 |
} |
| 603 |
} |
| 604 |
|
| 605 |
$fp = tmpfile(); |
| 606 |
foreach ( $rows as $row ) { |
| 607 |
fputcsv( $fp, $row ); |
| 608 |
} |
| 609 |
rewind( $fp ); |
| 610 |
|
| 611 |
$csv = ''; |
| 612 |
while ( ($array = fgetcsv( $fp )) !== false ) { |
| 613 |
if ( strlen( $csv ) > 0 ) { $csv .= PHP_EOL; } |
| 614 |
$csv .= implode( ',', $array ); |
| 615 |
} |
| 616 |
fclose( $fp ); |
| 617 |
|
| 618 |
echo wp_send_json_success(array( |
| 619 |
'csv' => $csv, |
| 620 |
'name' => $filename, |
| 621 |
)); |
| 622 |
}// End if(). |
| 623 |
|
| 624 |
exit; |
| 625 |
} |
| 626 |
} |
| 627 |
|