Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
f9b0fab
fix: give the stored rows of an unpublished post-type chart only to u…
vytisbulkevicius Dec 1, 2026
176ff0c
test: give the chart of the rows test the settings that the chart edi…
vytisbulkevicius Dec 1, 2026
d6ec928
fix: keep the stored rows when a user who cannot read them saves a ma…
vytisbulkevicius Dec 2, 2026
f05ac70
fix: refuse every upload that removes a query its user cannot read
vytisbulkevicius Dec 2, 2026
d3b6643
fix: keep unreadable rows out of the revision comparison and the char…
vytisbulkevicius Dec 2, 2026
88c1d05
fix: check both sides of a chart revision comparison
vytisbulkevicius Dec 2, 2026
243d590
test: date the older chart revision before the chart
vytisbulkevicius Dec 2, 2026
1557394
test: compare two chart revisions
vytisbulkevicius Dec 2, 2026
0c99490
Merge remote-tracking branch 'origin/development' into bugfix/pro/669
vytisbulkevicius Dec 2, 2026
5fafc69
Merge remote-tracking branch 'origin/development' into bugfix/pro/669
vytisbulkevicius Dec 2, 2026
8f6eb6c
fix: keep no saved image for an unpublished chart with a post-type query
vytisbulkevicius Dec 2, 2026
dd72a30
fix: delete the image of an unpublished chart for each order of its meta
vytisbulkevicius Dec 2, 2026
56fb503
refactor: delete the chart image without a retry branch
vytisbulkevicius Dec 2, 2026
2d39dca
fix: apply the rows rule to XML-RPC and keep the slice settings of a …
vytisbulkevicius Dec 2, 2026
a37c367
test: read the post fields through XML-RPC
vytisbulkevicius Dec 2, 2026
d3db263
fix: apply the rows rule to each XML-RPC method and to the revisions …
vytisbulkevicius Dec 2, 2026
4fb6d13
Correct the comment on when the XML-RPC filter runs
vytisbulkevicius Dec 2, 2026
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
2 changes: 1 addition & 1 deletion classes/Visualizer/Gutenberg/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ public function get_visualizer_data( $post ) {
$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' ) ) ),
Visualizer_Module::can_read_chart_rows( $post_id ) ? Visualizer_Module::decode_content( html_entity_decode( get_post_field( 'post_content', $post_id, 'raw' ) ) ) : array(),
Comment thread
pirate-bot marked this conversation as resolved.
Resolved
$post_id,
$data['visualizer-chart-type']
)
Expand Down
62 changes: 61 additions & 1 deletion classes/Visualizer/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -878,16 +878,76 @@ public static function maybe_decode_content( $value ) {
return is_serialized( $value ) ? self::decode_content( $value ) : $value;
}

/**
* Tells whether the current user can read the stored rows of a chart.
*
* A post-type chart of the Pro plugin stores post titles and meta values of posts in any status.
* A published chart shows its rows to everyone who can see it. The rows of an unpublished chart are for
* users who can read every post of the type: the post type has an admin screen, and the user can edit
* its posts of other users and read its private posts. This is the rule the Pro plugin has for the query.
*
* @param int $chart_id The chart ID, or the ID of a chart revision.
* @return bool
*/
public static function can_read_chart_rows( $chart_id ) {
if ( self::has_public_rows( $chart_id ) ) {
return true;
}

$filter_config = get_post_meta( $chart_id, Visualizer_Plugin::CF_FILTER_CONFIG, true );
$post_type = is_array( $filter_config ) && isset( $filter_config['type'] ) && is_string( $filter_config['type'] ) ? get_post_type_object( $filter_config['type'] ) : null;
return $post_type instanceof WP_Post_Type
&& $post_type->show_ui
&& current_user_can( $post_type->cap->edit_others_posts )
&& current_user_can( $post_type->cap->read_private_posts );
}
Comment thread
pirate-bot marked this conversation as resolved.
Resolved

/**
* Tells whether everyone who can see a chart can read its stored rows: the chart has no post-type query, or it is published.
*
* @param int $chart_id The chart ID, or the ID of a chart revision.
* @return bool
*/
public static function has_public_rows( $chart_id ) {
$parent_id = wp_is_post_revision( $chart_id );
return empty( get_post_meta( $chart_id, Visualizer_Plugin::CF_FILTER_CONFIG, true ) ) || 'publish' === get_post_status( $parent_id ? $parent_id : $chart_id );
}

/**
* Deletes the saved image of a chart whose rows are not public.
*
* The image shows the rows, and each visitor can read a file in the uploads folder.
*
* @param int $chart_id The chart ID. A revision has no image of its own.
* @return void
*/
public static function delete_image_of_private_rows( $chart_id ) {
if ( wp_is_post_revision( $chart_id ) || self::has_public_rows( $chart_id ) ) {
return;
}
$image_id = self::get_chart_image_id( $chart_id );
// An image that WordPress did not delete keeps its meta, so a later call finds it.
if ( $image_id && ! wp_delete_attachment( $image_id, true ) ) {
return;
}
delete_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_IMAGE );
Comment thread
pirate-bot marked this conversation as resolved.
Resolved
}

/**
* 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.
* @return mixed The chart data; an empty array when the current user cannot read the stored rows.
*/
public static function get_chart_data( $chart, $type, $run_filter = true, $sanitize = true ) {
// A chart object that is not a saved post has no ID and no saved post-type query.
if ( isset( $chart->ID ) && ! self::can_read_chart_rows( $chart->ID ) ) {
return array();
}
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
16 changes: 14 additions & 2 deletions classes/Visualizer/Module/Chart.php
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,10 @@ private function _handleDataAndSettingsPage() {
if ( isset( $existing['colors'] ) && is_array( $existing['colors'] ) && ! isset( $post_settings['colors'] ) ) {
$post_settings['colors'] = $existing['colors'];
}
// The sidebar builds the slice fields from the rows, so a user who cannot read the rows posts none.
if ( isset( $existing['slices'] ) && ! isset( $post_settings['slices'] ) && ! self::can_read_chart_rows( $this->_chart->ID ) ) {
$post_settings['slices'] = $existing['slices'];
}
$post_settings = $this->sanitizeSettings( $post_settings );
update_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_SETTINGS, $post_settings );

Expand Down Expand Up @@ -1345,6 +1349,14 @@ public function uploadData() {

do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Uploading data for chart %d with POST = %s and GET = %s', $chart_id, print_r( $_POST, true ), print_r( $_GET, true ) ), 'debug', __FILE__, __LINE__ );

// A new data source removes the post-type query of the chart, and the stored rows stay when it gives no data.
// A user who cannot read those rows does not get them in an editor, so that user cannot replace them or the query.
if ( $can_die && ! self::can_read_chart_rows( $chart_id ) ) {
$render = new Visualizer_Render_Page_Update( array( 'message' => esc_html__( 'You cannot change the data of this chart, because it comes from posts that you cannot read.', 'visualizer' ) ) );
$render->render();
( defined( 'WP_TESTS_DOMAIN' ) && function_exists( 'tests_add_filter' ) ) ? wp_die() : exit();
}

if ( ! isset( $_POST['vz-import-time'] ) ) {
apply_filters( 'visualizer_pro_remove_schedule', $chart_id );
}
Expand Down Expand Up @@ -1780,7 +1792,7 @@ public function saveFilter() {
*
* @param string $base64_img Chart image.
* @param int $chart_id Chart ID.
* @param bool $save_attachment Save attachment.
* @param bool $save_attachment Save attachment. A chart whose rows are not public gets no image.
* @return int Attachment ID, or 0 when no attachment was saved.
*/
public function save_chart_image( $base64_img, $chart_id, $save_attachment = true ) {
Expand All @@ -1792,7 +1804,7 @@ public function save_chart_image( $base64_img, $chart_id, $save_attachment = tru
wp_delete_attachment( $old_attachment_id, true );
}

if ( ! $save_attachment ) {
if ( ! $save_attachment || ! self::has_public_rows( $chart_id ) ) {
// The chart has no image now. A chart copied by an older version can still point to the image of its source chart.
delete_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_IMAGE );
return 0;
Expand Down
109 changes: 109 additions & 0 deletions classes/Visualizer/Module/Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ public function __construct( Visualizer_Plugin $plugin ) {
$this->_addAction( 'action_scheduler_ensure_recurring_actions', 'ensure_refresh_db_action' );
$this->_addFilter( 'visualizer_schedule_refresh_chart', 'refresh_db_for_chart', 10, 3 );
$this->_addFilter( 'is_protected_meta', 'protect_chart_meta', 10, 3 );
$this->_addFilter( 'wp_get_revision_ui_diff', 'remove_unreadable_rows_from_revision_diff', 10, 3 );
$this->_addFilter( Visualizer_Plugin::FILTER_GET_CHART_SETTINGS, 'remove_image_of_unreadable_rows', 10, 2 );
$this->_addAction( 'transition_post_status', 'delete_image_after_status_change', null, 10, 3 );
$this->_addFilter( 'xmlrpc_enabled', 'restrict_xmlrpc_to_readable_rows' );
$this->_addAction( 'added_post_meta', 'delete_image_after_query_change', null, 10, 3 );
$this->_addAction( 'updated_post_meta', 'delete_image_after_query_change', null, 10, 3 );
Comment thread
pirate-bot marked this conversation as resolved.
Resolved
$this->_addFilter( 'map_meta_cap', 'deny_json_headers_meta_caps', 10, 4 );
$this->_addFilter( 'wxr_export_skip_postmeta', 'skip_json_headers_in_export', 10, 2 );

Expand Down Expand Up @@ -314,6 +320,109 @@ public function adminInit() {
}


/**
* Removes the chart content from a revision comparison for a user who cannot read the rows of the chart.
*
* WordPress compares the revisions of each post that its user can edit, and the content of a chart is its rows.
*
* @param array<int, array{id: string, name: string, diff: string}> $fields The compared fields.
* @param WP_Post|false $compare_from The older revision, or false for none.
* @param WP_Post $compare_to The newer revision, or the post.
* @return array<int, array{id: string, name: string, diff: string}>
*/
public function remove_unreadable_rows_from_revision_diff( $fields, $compare_from, $compare_to ) {
$parent_id = wp_is_post_revision( $compare_to );
if ( Visualizer_Plugin::CPT_VISUALIZER !== get_post_type( $parent_id ? $parent_id : $compare_to ) ) {
return $fields;
}
// The older revision can hold rows of a post-type query that the newer one no longer has.
if ( self::can_read_chart_rows( $compare_to->ID ) && ( ! $compare_from instanceof WP_Post || self::can_read_chart_rows( $compare_from->ID ) ) ) {
return $fields;
}
return array_values( wp_list_filter( $fields, array( 'id' => 'post_content' ), 'NOT' ) );
}

/**
* Removes the rendered chart image from the settings for a user who cannot read the rows of the chart.
*
* The chart editor saves a picture of the chart in the `chart-img` setting, and the picture shows the rows.
*
* @param mixed $settings The chart settings.
* @param int $chart_id The chart ID.
* @return mixed
*/
public function remove_image_of_unreadable_rows( $settings, $chart_id ) {
if ( is_array( $settings ) && isset( $settings['chart-img'] ) && ! self::can_read_chart_rows( $chart_id ) ) {
unset( $settings['chart-img'] );
}
return $settings;
}

/**
* Applies the rule for the stored rows to an XML-RPC request.
*
* The XML-RPC methods of WordPress return the content of each post that its user can edit, and the content
* of a chart is its rows. No screen of the plugin uses XML-RPC. The XML-RPC server runs this filter when it
* starts, before a method checks what its user can do.
*
* @param bool $is_enabled Whether XML-RPC is enabled.
* @return bool
*/
public function restrict_xmlrpc_to_readable_rows( $is_enabled ) {
$this->_addFilter( 'map_meta_cap', 'deny_chart_with_unreadable_rows', 10, 4 );
return $is_enabled;
}

/**
* Refuses a chart, or a chart revision, to a user who cannot read its rows.
*
* @param string[] $caps The primitive capabilities that the user needs.
* @param string $cap The capability that is checked.
* @param int $user_id The user ID.
* @param mixed[] $args The arguments of the check: the post ID comes first.
* @return string[]
*/
public function deny_chart_with_unreadable_rows( $caps, $cap, $user_id, $args ) {
if ( ! in_array( $cap, array( 'edit_post', 'read_post' ), true ) || empty( $args[0] ) ) {
return $caps;
}
$parent_id = wp_is_post_revision( $args[0] );
if ( Visualizer_Plugin::CPT_VISUALIZER === get_post_type( $parent_id ? $parent_id : $args[0] ) && ! self::can_read_chart_rows( $args[0] ) ) {
$caps[] = 'do_not_allow';
}
return $caps;
}

/**
* Deletes the saved image of a chart that is no longer published, when its rows are not public.
*
* @param string $new_status The new status.
* @param string $old_status The old status.
* @param WP_Post $post The post.
* @return void
*/
public function delete_image_after_status_change( $new_status, $old_status, $post ) {
if ( Visualizer_Plugin::CPT_VISUALIZER === $post->post_type ) {
self::delete_image_of_private_rows( $post->ID );
}
}

/**
* Deletes the saved image of an unpublished chart that gets a post-type query.
*
* A restored revision brings the query and the image in any order.
*
* @param int $meta_id The meta ID.
* @param int $chart_id The post ID.
* @param string $meta_key The meta key.
* @return void
*/
public function delete_image_after_query_change( $meta_id, $chart_id, $meta_key ) {
if ( in_array( $meta_key, array( Visualizer_Plugin::CF_FILTER_CONFIG, Visualizer_Plugin::CF_CHART_IMAGE ), true ) ) {
self::delete_image_of_private_rows( $chart_id );
}
}

/**
* Marks the chart meta as protected.
*
Expand Down
33 changes: 33 additions & 0 deletions classes/Visualizer/Module/Upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ public static function upgrade() {
$upgraded = true;
}

// Once after each update: an earlier version keeps such an image, and no version number marks the first one that does not.
if ( version_compare( $last_version, Visualizer_Plugin::VERSION, '<' ) ) {
self::deleteImagesOfPrivateRows();
$upgraded = true;
}

if ( ! $upgraded ) {
return;
}
Expand All @@ -34,6 +40,33 @@ public static function upgrade() {
update_option( 'visualizer-upgraded-from', $last_version );
}

/**
* Deletes the images that an earlier version saved for unpublished charts with a post-type query.
*
* @return void
*/
private static function deleteImagesOfPrivateRows() {
$chart_ids = get_posts(
array(
'post_type' => Visualizer_Plugin::CPT_VISUALIZER,
'post_status' => array_values( array_diff( get_post_stati(), array( 'publish' ) ) ),
'fields' => 'ids',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => Visualizer_Plugin::CF_FILTER_CONFIG,
'compare' => 'EXISTS',
),
array(
'key' => Visualizer_Plugin::CF_CHART_IMAGE,
'compare' => 'EXISTS',
),
),
)
);
array_map( array( 'Visualizer_Module', 'delete_image_of_private_rows' ), $chart_ids );
}

/**
* All 'dataTable' and 'table' charts to become 'tabular'.
* All charts that do not have a library, to get the default library.
Expand Down
Loading
Loading