Skip to content
Open
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
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(),

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.

correctness The gate empties the rows but keeps the rendered chart image. The editor renders the chart to a PNG data URI and saves it in the chart-img setting when it posts the settings. The block REST field, the chart editor and the chart library send all settings, so a refused Author still gets a picture of the private titles. The shortcode with use_image="yes" and AMP also return the saved image attachment despite the row refusal. Whenever can_read_chart_rows() refuses, remove chart-img from every settings output and skip the saved image. Add cases to tests/test-chart-rows-permissions.php for the REST settings and the use_image shortcode as the Author.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in d3b6643 and 8f6eb6c. remove_image_of_unreadable_rows() on the chart settings filter removes chart-img for a refused user; the block REST field case is test_reader_gives_the_chart_image_only_to_a_user_who_passes_the_rule. For the saved attachment there is one rule in place of a check in each reader: an unpublished chart with a post-type query keeps no saved image (see the reply to the image finding of the review). test_image_is_deleted_when_the_chart_is_unpublished renders the use_image shortcode as the Author.

$post_id,
$data['visualizer-chart-type']
)
Expand Down
61 changes: 60 additions & 1 deletion classes/Visualizer/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -878,16 +878,75 @@ 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 on lines +892 to +903

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.

correctness The rows still reach a refused user through the core revision diff. An Author who owns an unpublished post-type chart can post action=get-revision-diffs&post_id=<chart>&compare[]=0:<chart> to admin-ajax.php. Core checks only edit_post and diffs the chart's own post_content against empty text. The response contains the private post titles. Opening the chart in the editor creates the revision that this request needs. Filter wp_get_revision_ui_diff, and drop the post_content field whenever can_read_chart_rows() refuses the compared post or its parent chart. Add a case to tests/test-chart-rows-permissions.php that calls wp_ajax_get_revision_diffs() as the Author and asserts the private title is absent.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in d3b6643 and 88c1d05. remove_unreadable_rows_from_revision_diff() on wp_get_revision_ui_diff drops the content field when the rule refuses the user for one of the two compared versions. test_revision_comparison_gives_rows_only_to_a_user_who_passes_the_rule and test_revision_comparison_keeps_the_rows_of_an_older_revision cover it.


/**
* 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 );
if ( $image_id ) {
wp_delete_attachment( $image_id, true );
}
delete_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_IMAGE );
}

/**
* 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();
}

// change HTML entities
$post_content = html_entity_decode( htmlentities( $chart->post_content ) );
$post_content = preg_replace_callback(
Expand Down
12 changes: 10 additions & 2 deletions classes/Visualizer/Module/Chart.php
Original file line number Diff line number Diff line change
Expand Up @@ -1345,6 +1345,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 +1788,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 +1800,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
73 changes: 73 additions & 0 deletions classes/Visualizer/Module/Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ 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->_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 );
$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 +319,74 @@ 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;
}

/**
* 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