diff --git a/classes/Visualizer/Gutenberg/Block.php b/classes/Visualizer/Gutenberg/Block.php
index 5a152351..d8acb490 100644
--- a/classes/Visualizer/Gutenberg/Block.php
+++ b/classes/Visualizer/Gutenberg/Block.php
@@ -327,7 +327,14 @@ public function get_visualizer_data( $post ) {
$data['visualizer-settings'] = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SETTINGS, $data['visualizer-settings'], $post_id, $data['visualizer-chart-type'] );
// handle data filter hooks
- $data['visualizer-data'] = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA, Visualizer_Module::decode_content( html_entity_decode( get_post_field( 'post_content', $post_id, 'raw' ) ) ), $post_id, $data['visualizer-chart-type'] );
+ $data['visualizer-data'] = Visualizer_Module::sanitize_chart_data(
+ apply_filters(
+ Visualizer_Plugin::FILTER_GET_CHART_DATA,
+ Visualizer_Module::decode_content( html_entity_decode( get_post_field( 'post_content', $post_id, 'raw' ) ) ),
+ $post_id,
+ $data['visualizer-chart-type']
+ )
+ );
// we are going to format only for tabular charts, because we are not sure of the effect on others.
// this is to solve the case where boolean data shows up as all-ticks on gutenberg.
diff --git a/classes/Visualizer/Module.php b/classes/Visualizer/Module.php
index ff9fa2c3..d00b2517 100644
--- a/classes/Visualizer/Module.php
+++ b/classes/Visualizer/Module.php
@@ -190,7 +190,7 @@ public function _getDataAs( $chart_id, $type ) {
$settings = get_post_meta( $chart_id, Visualizer_Plugin::CF_SETTINGS, true );
$rows = array();
$series = get_post_meta( $chart_id, Visualizer_Plugin::CF_SERIES, true );
- $data = self::get_chart_data( $chart, $type, false );
+ $data = self::get_chart_data( $chart, $type, false, 'print' === $type );
if ( ! empty( $series ) ) {
$row = array();
foreach ( $series as $array ) {
@@ -880,8 +880,14 @@ public static function maybe_decode_content( $value ) {
/**
* Gets the chart content after common manipulations.
+ *
+ * @param WP_Post|object $chart The chart post.
+ * @param string $type The chart type passed to the data filter.
+ * @param bool $run_filter Whether to run the chart data filter.
+ * @param bool $sanitize Whether to prepare the cells for HTML-capable renderers; exports and editors need the stored values.
+ * @return mixed The chart data.
*/
- public static function get_chart_data( $chart, $type, $run_filter = true ) {
+ public static function get_chart_data( $chart, $type, $run_filter = true, $sanitize = true ) {
// change HTML entities
$post_content = html_entity_decode( htmlentities( $chart->post_content ) );
$post_content = preg_replace_callback(
@@ -912,9 +918,47 @@ function ( $matches ) {
$altered = $data;
}
if ( $run_filter ) {
- return apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA, $altered, $chart->ID, $type );
+ $altered = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA, $altered, $chart->ID, $type );
+ }
+
+ return $sanitize ? self::sanitize_chart_data( $altered ) : $altered;
+ }
+
+ /**
+ * Saves a chart's JSON endpoint, recording the user who changes it as the WooCommerce report user.
+ *
+ * A scheduled refresh of the endpoint then uses this user's report access,
+ * not the access of whoever authorized an earlier endpoint.
+ *
+ * @param int $chart_id The chart ID.
+ * @param string $url The JSON endpoint.
+ * @return void
+ */
+ public static function update_json_url( $chart_id, $url ) {
+ if ( get_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_URL, true ) !== $url ) {
+ update_post_meta( $chart_id, Visualizer_Plugin::CF_WOOCOMMERCE_REPORT_USER, get_current_user_id() );
}
- return $altered;
+ update_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_URL, $url );
+ }
+
+ /**
+ * Sanitize chart data before passing it to HTML-capable renderers.
+ *
+ * @param mixed $data Chart data.
+ * @return mixed Sanitized chart data.
+ */
+ public static function sanitize_chart_data( $data ) {
+ // Preserve normal text and supported formatting while removing active
+ // markup from imports, scheduled remote sources, and filtered data.
+ return map_deep(
+ $data,
+ static function ( $value ) {
+ if ( ! is_string( $value ) || ! preg_match( '/<\s*\/?\s*[a-zA-Z!]/', $value ) ) {
+ return $value;
+ }
+ return wp_kses_post( $value );
+ }
+ );
}
/**
diff --git a/classes/Visualizer/Module/AIBuilder.php b/classes/Visualizer/Module/AIBuilder.php
index c97be184..931e7814 100644
--- a/classes/Visualizer/Module/AIBuilder.php
+++ b/classes/Visualizer/Module/AIBuilder.php
@@ -195,7 +195,7 @@ public function fetchChart(): void {
$this->_verify_chart_access( $chart_id );
$series = get_post_meta( $chart_id, Visualizer_Plugin::CF_SERIES, true );
- $data = Visualizer_Module::get_chart_data( $chart, '', false );
+ $data = Visualizer_Module::get_chart_data( $chart, '', false, false );
$code = get_post_meta( $chart_id, self::CF_D3_CODE, true );
$settings = get_post_meta( $chart_id, Visualizer_Plugin::CF_SETTINGS, true );
$title = ( is_array( $settings ) && ! empty( $settings['backend-title'] ) )
@@ -360,7 +360,7 @@ public function uploadData(): void {
$source = new Visualizer_Source_Json( $params );
// Store config for sync
- update_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_URL, $params['url'] );
+ Visualizer_Module::update_json_url( $chart_id, $params['url'] );
update_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_ROOT, $params['root'] );
if ( ! empty( $_POST['json_schedule'] ) ) {
update_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_SCHEDULE, intval( $_POST['json_schedule'] ) );
diff --git a/classes/Visualizer/Module/Abilities.php b/classes/Visualizer/Module/Abilities.php
index 95c2cb43..87aefe17 100644
--- a/classes/Visualizer/Module/Abilities.php
+++ b/classes/Visualizer/Module/Abilities.php
@@ -598,7 +598,7 @@ public function executeGetChart( $input = array() ) {
$type = (string) get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true );
$series = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SERIES, get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true ), $chart->ID, $type );
- $data = self::get_chart_data( $chart, $type );
+ $data = self::get_chart_data( $chart, $type, true, false );
$settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true );
$settings = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SETTINGS, $settings, $chart->ID, $type );
if ( ! is_array( $settings ) ) {
@@ -1048,7 +1048,7 @@ private function setJsonSource( $chart, $input, $hours, $dry_run ) {
$this->clearSourceMeta( $chart->ID, true );
update_post_meta( $chart->ID, Visualizer_Plugin::CF_EDITABLE_TABLE, true );
$this->persistSource( $chart->ID, $source );
- update_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_URL, $params['url'] );
+ self::update_json_url( $chart->ID, $params['url'] );
update_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_ROOT, $params['root'] );
$headers = array( 'method' => $params['method'] );
diff --git a/classes/Visualizer/Module/Chart.php b/classes/Visualizer/Module/Chart.php
index bbe126e0..34d615ab 100644
--- a/classes/Visualizer/Module/Chart.php
+++ b/classes/Visualizer/Module/Chart.php
@@ -138,14 +138,14 @@ public function setJsonSchedule() {
);
if ( Visualizer_Module::is_pro() ) {
- $is_woocommerce_report = filter_input(
- INPUT_POST,
- 'is_woocommerce_report',
- FILTER_VALIDATE_BOOLEAN
- );
+ $is_woocommerce_report = isset( $_POST['is_woocommerce_report'] ) ? filter_var( wp_unslash( $_POST['is_woocommerce_report'] ), FILTER_VALIDATE_BOOLEAN ) : false;
if ( $is_woocommerce_report ) {
+ if ( ! current_user_can( 'view_woocommerce_reports' ) ) {
+ wp_send_json_error( array( 'msg' => esc_html__( 'You do not have permission to access WooCommerce reports.', 'visualizer' ) ), 403 );
+ }
update_post_meta( $chart_id, Visualizer_Plugin::CF_IS_WOOCOMMERCE_SOURCE, true );
+ update_post_meta( $chart_id, Visualizer_Plugin::CF_WOOCOMMERCE_REPORT_USER, get_current_user_id() );
} else {
delete_post_meta( $chart_id, Visualizer_Plugin::CF_IS_WOOCOMMERCE_SOURCE );
}
@@ -220,7 +220,7 @@ public function getJsonData() {
wp_send_json_error( array( 'msg' => esc_html__( 'Unable to fetch data from the endpoint. Please try again.', 'visualizer' ) ) );
}
- $data = Visualizer_Render_Layout::show( 'editor-table', $data, $chart_id, 'viz-json-table', false, false );
+ $data = Visualizer_Render_Layout::show( 'editor-table', self::sanitize_chart_data( $data ), $chart_id, 'viz-json-table', false, false );
wp_send_json_success( array( 'table' => $data, 'root' => $params['root'], 'url' => $params['url'], 'paging' => $source->getPaginationElements() ) );
}
@@ -253,7 +253,7 @@ public function setJsonData() {
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 );
- update_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_URL, $params['url'] );
+ self::update_json_url( $chart->ID, $params['url'] );
update_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_ROOT, $params['root'] );
delete_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_HEADERS );
@@ -304,7 +304,7 @@ public function setJsonData() {
$render = new Visualizer_Render_Page_Update();
$render->id = $chart->ID;
- $render->data = json_encode( $source->getRawData( get_post_meta( $chart_id, Visualizer_Plugin::CF_EDITABLE_TABLE, true ) ) );
+ $render->data = json_encode( self::sanitize_chart_data( $source->getRawData( get_post_meta( $chart_id, Visualizer_Plugin::CF_EDITABLE_TABLE, true ) ) ) );
$render->series = json_encode( $source->getSeries() );
$render->render();
@@ -631,7 +631,8 @@ public function renderChartPages() {
$post_meta = get_post_meta( $parent_chart_id );
$chart_id = $new_chart_id;
foreach ( $post_meta as $key => $value ) {
- if ( strpos( $key, 'visualizer-' ) !== false ) {
+ // A copy refreshes reports with its own author's access.
+ if ( strpos( $key, 'visualizer-' ) !== false && Visualizer_Plugin::CF_WOOCOMMERCE_REPORT_USER !== $key ) {
add_post_meta( $new_chart_id, $key, self::maybe_decode_content( $value[0] ) );
}
}
@@ -1420,7 +1421,7 @@ public function uploadData() {
}
$render->id = $chart->ID;
- $render->data = json_encode( $source->getRawData( get_post_meta( $chart->ID, Visualizer_Plugin::CF_EDITABLE_TABLE, true ) ) );
+ $render->data = json_encode( self::sanitize_chart_data( $source->getRawData( get_post_meta( $chart->ID, Visualizer_Plugin::CF_EDITABLE_TABLE, true ) ) ) );
$render->series = json_encode( $source->getSeries() );
$render->settings = json_encode( $settings );
} else {
@@ -1475,7 +1476,8 @@ public function cloneChart() {
} else {
$post_meta = get_post_meta( $chart_id );
foreach ( $post_meta as $key => $value ) {
- if ( strpos( $key, 'visualizer-' ) !== false ) {
+ // A copy refreshes reports with its own author's access.
+ if ( strpos( $key, 'visualizer-' ) !== false && Visualizer_Plugin::CF_WOOCOMMERCE_REPORT_USER !== $key ) {
add_post_meta( $new_chart_id, $key, self::maybe_decode_content( $value[0] ) );
}
}
@@ -1682,7 +1684,7 @@ public function saveQuery() {
'post_content' => $source->getData( get_post_meta( $chart_id, Visualizer_Plugin::CF_EDITABLE_TABLE, true ) ),
)
);
- $render->data = json_encode( $source->getRawData( get_post_meta( $chart_id, Visualizer_Plugin::CF_EDITABLE_TABLE, true ) ) );
+ $render->data = json_encode( self::sanitize_chart_data( $source->getRawData( get_post_meta( $chart_id, Visualizer_Plugin::CF_EDITABLE_TABLE, true ) ) ) );
$render->series = json_encode( $source->getSeries() );
$render->id = $chart_id;
} else {
diff --git a/classes/Visualizer/Module/Setup.php b/classes/Visualizer/Module/Setup.php
index 00a3fbca..3f530b35 100644
--- a/classes/Visualizer/Module/Setup.php
+++ b/classes/Visualizer/Module/Setup.php
@@ -384,6 +384,8 @@ public function refresh_db_for_chart( $chart, $chart_id, $force = false ) {
return $chart;
}
+ // Editable-table charts fetch again here, so check the error after this read.
+ $content = $source->getData( get_post_meta( $chart_id, Visualizer_Plugin::CF_EDITABLE_TABLE, true ) );
$error = $source->get_error();
if ( empty( $error ) ) {
$this->disableRevisionsTemporarily();
@@ -406,7 +408,7 @@ public function refresh_db_for_chart( $chart, $chart_id, $force = false ) {
wp_update_post(
array(
'ID' => $chart_id,
- 'post_content' => $source->getData( get_post_meta( $chart_id, Visualizer_Plugin::CF_EDITABLE_TABLE, true ) ),
+ 'post_content' => $content,
)
);
diff --git a/classes/Visualizer/Plugin.php b/classes/Visualizer/Plugin.php
index 162672c0..8d632e2b 100644
--- a/classes/Visualizer/Plugin.php
+++ b/classes/Visualizer/Plugin.php
@@ -106,6 +106,7 @@ class Visualizer_Plugin {
const CF_CHART_CACHE = 'visualizer-chart-cache';
const CF_JSON_WOOCOMMERCE_SOURCE = 'visualizer-woocommerce-source';
const CF_IS_WOOCOMMERCE_SOURCE = 'visualizer-is-woocommerce-source';
+ const CF_WOOCOMMERCE_REPORT_USER = 'visualizer-woocommerce-report-user';
/**
* Name of the option for WordPress DB.
diff --git a/classes/Visualizer/Render/Layout.php b/classes/Visualizer/Render/Layout.php
index 18e0aeb8..ed9b5b21 100644
--- a/classes/Visualizer/Render/Layout.php
+++ b/classes/Visualizer/Render/Layout.php
@@ -411,7 +411,7 @@ public static function _renderTextEditor( $args ) {
}
?>
-
+
';
} else {
- echo '' . $header . ' | ';
+ echo '' . esc_html( $header ) . ' | ';
}
}
?>
@@ -479,8 +479,8 @@ public static function _renderEditorTable( $args ) {
if ( $editable_data ) {
echo '