| 1 |
<?php |
| 2 |
/** |
| 3 |
* MCP-only chart persistence: same DB write as WPDataChart::save() but runs prepareData() |
| 4 |
* and groupData() so json_render_data includes table rows (Chart.js labels/datasets). |
| 5 |
* |
| 6 |
* @package wpDataTables |
| 7 |
*/ |
| 8 |
|
| 9 |
defined( 'ABSPATH' ) or die('Access denied.'); |
| 10 |
|
| 11 |
/** |
| 12 |
* Chart.js pie/doughnut/polar must not use Cartesian x/y scales; core prepareRender only hides them when show_grid is off. |
| 13 |
* |
| 14 |
* @param \WPDataChart $chart |
| 15 |
* @return void |
| 16 |
*/ |
| 17 |
function wdtmcp_chartjs_strip_cartesian_scales_for_radial( \WPDataChart $chart ) { |
| 18 |
if ( strtolower( (string) $chart->getEngine() ) !== 'chartjs' ) { |
| 19 |
return; |
| 20 |
} |
| 21 |
|
| 22 |
$radial_types = array( |
| 23 |
'chartjs_pie_chart', |
| 24 |
'chartjs_doughnut_chart', |
| 25 |
'chartjs_polar_area_chart', |
| 26 |
); |
| 27 |
|
| 28 |
if ( ! in_array( (string) $chart->getType(), $radial_types, true ) ) { |
| 29 |
return; |
| 30 |
} |
| 31 |
|
| 32 |
$ref = new \ReflectionObject( $chart ); |
| 33 |
if ( ! $ref->hasProperty( '_chartjs_render_data' ) ) { |
| 34 |
return; |
| 35 |
} |
| 36 |
|
| 37 |
$prop = $ref->getProperty( '_chartjs_render_data' ); |
| 38 |
$prop->setAccessible( true ); |
| 39 |
$data = $prop->getValue( $chart ); |
| 40 |
|
| 41 |
if ( ! is_array( $data ) || empty( $data['options']['options']['scales'] ) ) { |
| 42 |
return; |
| 43 |
} |
| 44 |
|
| 45 |
$scales = &$data['options']['options']['scales']; |
| 46 |
|
| 47 |
if ( isset( $scales['x'] ) && is_array( $scales['x'] ) ) { |
| 48 |
$scales['x']['display'] = false; |
| 49 |
if ( isset( $scales['x']['title'] ) && is_array( $scales['x']['title'] ) ) { |
| 50 |
$scales['x']['title']['display'] = false; |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
if ( isset( $scales['y'] ) && is_array( $scales['y'] ) ) { |
| 55 |
$scales['y']['display'] = false; |
| 56 |
if ( isset( $scales['y']['title'] ) && is_array( $scales['y']['title'] ) ) { |
| 57 |
$scales['y']['title']['display'] = false; |
| 58 |
} |
| 59 |
foreach ( array( 'min', 'max', 'beginAtZero' ) as $junk ) { |
| 60 |
unset( $scales['y'][ $junk ] ); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
$prop->setValue( $chart, $data ); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* @param \WPDataChart $chart |
| 69 |
* @return void |
| 70 |
*/ |
| 71 |
function wdtmcp_chart_save_persisting_table_rows( \WPDataChart $chart ) { |
| 72 |
global $wpdb; |
| 73 |
|
| 74 |
$chart->prepareSeriesData(); |
| 75 |
$chart->prepareData(); |
| 76 |
$chart->groupData(); |
| 77 |
$chart->shiftXAxisColumnUp(); |
| 78 |
$chart->prepareRender(); |
| 79 |
|
| 80 |
wdtmcp_chartjs_strip_cartesian_scales_for_radial( $chart ); |
| 81 |
|
| 82 |
$render_data = $chart->formRenderDataForDb(); |
| 83 |
|
| 84 |
if ( empty( $chart->getId() ) ) { |
| 85 |
$ok = $wpdb->insert( |
| 86 |
$wpdb->prefix . 'wpdatacharts', |
| 87 |
array( |
| 88 |
'wpdatatable_id' => $chart->getwpDataTableId(), |
| 89 |
'title' => $chart->getTitle(), |
| 90 |
'engine' => $chart->getEngine(), |
| 91 |
'type' => $chart->getType(), |
| 92 |
'json_render_data' => json_encode( $render_data ), |
| 93 |
) |
| 94 |
); |
| 95 |
if ( false === $ok || (int) $wpdb->insert_id <= 0 ) { |
| 96 |
throw new \RuntimeException( 'Failed to insert chart row: ' . (string) $wpdb->last_error ); |
| 97 |
} |
| 98 |
$chart->setId( (int) $wpdb->insert_id ); |
| 99 |
} else { |
| 100 |
$ok = $wpdb->update( |
| 101 |
$wpdb->prefix . 'wpdatacharts', |
| 102 |
array( |
| 103 |
'wpdatatable_id' => $chart->getwpDataTableId(), |
| 104 |
'title' => $chart->getTitle(), |
| 105 |
'engine' => $chart->getEngine(), |
| 106 |
'type' => $chart->getType(), |
| 107 |
'json_render_data' => json_encode( $render_data ), |
| 108 |
), |
| 109 |
array( 'id' => $chart->getId() ) |
| 110 |
); |
| 111 |
if ( false === $ok ) { |
| 112 |
throw new \RuntimeException( 'Failed to update chart row: ' . (string) $wpdb->last_error ); |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|