Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion classes/Visualizer/Gutenberg/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
52 changes: 48 additions & 4 deletions classes/Visualizer/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Comment thread
pirate-bot marked this conversation as resolved.
Resolved
if ( ! empty( $series ) ) {
$row = array();
foreach ( $series as $array ) {
Expand Down Expand Up @@ -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 ) {
Comment thread
pirate-bot marked this conversation as resolved.
Resolved
// change HTML entities
$post_content = html_entity_decode( htmlentities( $chart->post_content ) );
$post_content = preg_replace_callback(
Expand Down Expand Up @@ -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 );
}
);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions classes/Visualizer/Module/AIBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] ) )
Expand Down Expand Up @@ -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'] ) );
Expand Down
4 changes: 2 additions & 2 deletions classes/Visualizer/Module/Abilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) ) {
Expand Down Expand Up @@ -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'] );

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note The set-data-source ability deletes the stored JSON URL in clearSourceMeta() before it calls update_json_url(). So a save with the same endpoint always records the calling user as the report user. If that user lacks report access, the scheduled WooCommerce refresh stops and keeps old data. Is this call meant to re-authorize the schedule it recreates? If yes, it is safe. If not, compare against the URL read before clearSourceMeta().

update_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_ROOT, $params['root'] );

$headers = array( 'method' => $params['method'] );
Expand Down
26 changes: 14 additions & 12 deletions classes/Visualizer/Module/Chart.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
Expand Down Expand Up @@ -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 );
Comment thread
pirate-bot marked this conversation as resolved.
Resolved
wp_send_json_success( array( 'table' => $data, 'root' => $params['root'], 'url' => $params['url'], 'paging' => $source->getPaginationElements() ) );
}

Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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] ) );
}
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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] ) );
}
}
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion classes/Visualizer/Module/Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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,
)
);

Expand Down
1 change: 1 addition & 0 deletions classes/Visualizer/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 5 additions & 5 deletions classes/Visualizer/Render/Layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ public static function _renderTextEditor( $args ) {
}
?>
<div class="viz-simple-editor-type viz-text-editor">
<textarea id="edited_text"><?php echo $data; ?></textarea>
<textarea id="edited_text"><?php echo esc_textarea( $data ); ?></textarea>
</div>
<?php
}
Expand All @@ -436,7 +436,7 @@ public static function _renderEditorTable( $args ) {
}
$chart = get_post( $chart_id );
$type = get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_TYPE, true );
$data = Visualizer_Module::get_chart_data( $chart, $type );
$data = Visualizer_Module::get_chart_data( $chart, $type, true, ! $editable_data );
} else {
$headers = array_keys( $data[0] );
}
Expand All @@ -463,7 +463,7 @@ public static function _renderEditorTable( $args ) {
if ( $editable_data ) {
echo '<th><input type="text" name="header[]" value="' . esc_attr( $header ) . '"></th>';
} else {
echo '<th>' . $header . '</th>';
echo '<th>' . esc_html( $header ) . '</th>';
}
}
?>
Expand All @@ -479,8 +479,8 @@ public static function _renderEditorTable( $args ) {
if ( $editable_data ) {
echo '<select name="type[]">';
} else {
echo '<input name="header[]" type="hidden" value="' . $header . '">';
echo '<select name="type[' . $header . ']" class="viz-select-data-type">';
echo '<input name="header[]" type="hidden" value="' . esc_attr( $header ) . '">';
echo '<select name="type[' . esc_attr( $header ) . ']" class="viz-select-data-type">';
}
echo '<option value="" title="' . __( 'Exclude from chart', 'visualizer' ) . '">' . __( 'Exclude', 'visualizer' ) . '</option>';
echo '<option value="0" disabled title="' . __( 'Include in chart and select data type', 'visualizer' ) . '">--' . __( 'OR', 'visualizer' ) . '--</option>';
Expand Down
14 changes: 12 additions & 2 deletions classes/Visualizer/Source/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ public function getPaginationElements() {
* @since ?
*
* @access public
* @return bool False when a WooCommerce report request failed, true otherwise.
*/
public function fetch() {
$url = $this->_url;
Expand Down Expand Up @@ -285,7 +286,7 @@ public function fetch() {

do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, 'JSON endpoint data parsed.', 'debug', __FILE__, __LINE__ );

return true;
return empty( $this->_error );
Comment thread
pirate-bot marked this conversation as resolved.
Resolved
}

/**
Expand Down Expand Up @@ -399,6 +400,10 @@ private function getJSON( $url = null ) {
if ( is_wp_error( $response ) || ! in_array( intval( $response['response']['code'] ), array( 200, 201 ), true ) ) {
$error_code = is_wp_error( $response ) ? $response->get_error_code() : (string) wp_remote_retrieve_response_code( $response );
do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Error while fetching JSON endpoint: %s', $error_code ), 'error', __FILE__, __LINE__ );
if ( $this->is_woocommerce_request( $url ) ) {
// Report access can be missing for this request, so keep the stored chart data.
$this->_error = esc_html__( 'Unable to fetch the WooCommerce report. The user who saved this chart needs access to WooCommerce reports.', 'visualizer' );
}
return null;
}

Expand Down Expand Up @@ -457,8 +462,13 @@ function ( $headers ) {
}
}

// Check if this is a WooCommerce endpoint request and add verification token.
// Check if this is an authorized WooCommerce endpoint request and add a verification token.
$can_fetch_woocommerce_reports = false;
if ( $this->is_woocommerce_request( $url ) ) {
$can_fetch_woocommerce_reports = current_user_can( 'view_woocommerce_reports' );
$can_fetch_woocommerce_reports = apply_filters( 'visualizer_can_fetch_woocommerce_reports', $can_fetch_woocommerce_reports, $url );
Comment thread
pirate-bot marked this conversation as resolved.
Resolved
}
if ( ! empty( $can_fetch_woocommerce_reports ) ) {
Comment thread
pirate-bot marked this conversation as resolved.
Resolved
// Generate a unique token for this specific request.
$token = wp_generate_password( 32, false );
set_transient( 'visualizer_wc_token_' . $token, time(), 60 );
Expand Down
2 changes: 1 addition & 1 deletion classes/Visualizer/Source/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ private function get_col_type( $col_num ) {
* @return string
*/
private function html( $headers, $results ) {
return Visualizer_Render_Layout::show( 'db-wizard-results', $headers, $results );
return Visualizer_Render_Layout::show( 'db-wizard-results', Visualizer_Module::sanitize_chart_data( $headers ), Visualizer_Module::sanitize_chart_data( $results ) );
}

/**
Expand Down
17 changes: 10 additions & 7 deletions js/render-datatables.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@
var select = {
info: false
};
var nestedSettings = {
select: select
};
// we cannot use { select } below because https://github.com/Codeinwp/visualizer/issues/357
$.extend( settings, { info: false } ); // jshint ignore:line

Expand Down Expand Up @@ -135,7 +138,7 @@
}
}

// remove this so that the eval does not fail.
// internal_title is the chart's admin label, not a DataTables option.
delete chart.settings['internal_title'];

var additional = [];
Expand Down Expand Up @@ -163,15 +166,15 @@

// if the setting name has an '_' this means it is a sub-setting e.g. select_items means { select: { items: ... } }.
var array = i.split('_');
if(array.length === 2){
i = eval( array[0] ); // jshint ignore:line
i[ array[1] ] = valoo;
additional[ array[0] ] = i;
if(array.length === 2 && Object.prototype.hasOwnProperty.call(nestedSettings, array[0])){
var nestedSetting = nestedSettings[ array[0] ];
nestedSetting[ array[1] ] = valoo;
additional[ array[0] ] = nestedSetting;
if(array[0] === 'select' && array[1] === 'info' ){
// enable main info or the selection info will not show.
$.extend( settings, {info: true} );
}
} else {
} else if(array.length === 1) {
settings[i] = valoo;
}
}
Expand Down Expand Up @@ -450,4 +453,4 @@ $.fn.dataTable.render.moment = function ( from, to, locale ) {

}));

/* jshint ignore:end */
/* jshint ignore:end */
Loading
Loading