From f9b0fabf1be6ecb6ac61dd884406eb486768f854 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vytis=20Bulkevi=C4=8Dius?= Date: Tue, 1 Dec 2026 22:22:13 +0200 Subject: [PATCH 01/11] fix: give the stored rows of an unpublished post-type chart only to users who can read every post of the type Co-Authored-By: Claude Mythos 5.2 --- classes/Visualizer/Gutenberg/Block.php | 2 +- classes/Visualizer/Module.php | 32 +++- tests/test-chart-rows-permissions.php | 253 +++++++++++++++++++++++++ 3 files changed, 285 insertions(+), 2 deletions(-) create mode 100644 tests/test-chart-rows-permissions.php diff --git a/classes/Visualizer/Gutenberg/Block.php b/classes/Visualizer/Gutenberg/Block.php index d8acb490..ebc0edbc 100644 --- a/classes/Visualizer/Gutenberg/Block.php +++ b/classes/Visualizer/Gutenberg/Block.php @@ -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(), $post_id, $data['visualizer-chart-type'] ) diff --git a/classes/Visualizer/Module.php b/classes/Visualizer/Module.php index d00b2517..c98b3e80 100644 --- a/classes/Visualizer/Module.php +++ b/classes/Visualizer/Module.php @@ -878,6 +878,31 @@ 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 ) { + $filter_config = get_post_meta( $chart_id, Visualizer_Plugin::CF_FILTER_CONFIG, true ); + $parent_id = wp_is_post_revision( $chart_id ); + if ( empty( $filter_config ) || 'publish' === get_post_status( $parent_id ? $parent_id : $chart_id ) ) { + return 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 ); + } + /** * Gets the chart content after common manipulations. * @@ -885,9 +910,14 @@ public static function maybe_decode_content( $value ) { * @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( diff --git a/tests/test-chart-rows-permissions.php b/tests/test-chart-rows-permissions.php new file mode 100644 index 00000000..1464f3fa --- /dev/null +++ b/tests/test-chart-rows-permissions.php @@ -0,0 +1,253 @@ +author_id = $this->factory->user->create( array( 'role' => 'author' ) ); + $this->editor_id = $this->factory->user->create( array( 'role' => 'editor' ) ); + $this->chart_id = $this->factory->post->create( + array( + 'post_type' => Visualizer_Plugin::CPT_VISUALIZER, + 'post_status' => 'draft', + 'post_author' => $this->author_id, + 'post_content' => serialize( array( array( self::PRIVATE_CELL, 1 ) ) ), + ) + ); + update_post_meta( $this->chart_id, Visualizer_Plugin::CF_CHART_TYPE, 'line' ); + update_post_meta( $this->chart_id, Visualizer_Plugin::CF_CHART_LIBRARY, 'ChartJS' ); + update_post_meta( $this->chart_id, Visualizer_Plugin::CF_SETTINGS, array() ); + update_post_meta( + $this->chart_id, + Visualizer_Plugin::CF_SERIES, + array( + array( + 'label' => 'x', + 'type' => 'string', + ), + array( + 'label' => 'y', + 'type' => 'number', + ), + ) + ); + update_post_meta( $this->chart_id, Visualizer_Plugin::CF_FILTER_CONFIG, array( 'type' => 'post' ) ); + + // The library gives the chart data to this script. Its admin page registers it. + if ( ! wp_script_is( 'visualizer-library', 'registered' ) ) { + wp_register_script( 'visualizer-library', 'https://example.com/library.js', array(), '1', true ); + } + } + + /** + * The readers of the stored rows. + * + * @return array The name of the method that returns what a reader gives to the current user. + */ + public function readers() { + return array( + 'block editor field' => array( 'read_block_field' ), + 'get-chart ability' => array( 'read_ability' ), + 'export' => array( 'read_export' ), + 'shortcode' => array( 'read_shortcode' ), + 'chart editor' => array( 'read_chart_editor' ), + 'chart library' => array( 'read_library' ), + ); + } + + /** + * A reader gives the rows of the unpublished chart to the Editor, and not to the Author who owns the chart. + * After the chart is published, the Author gets them too. + * + * @dataProvider readers + * + * @param string $reader The method that reads. + */ + public function test_reader_gives_unpublished_rows_only_to_a_user_who_passes_the_rule( $reader ) { + wp_set_current_user( $this->editor_id ); + $this->assertStringContainsString( self::PRIVATE_CELL, $this->$reader() ); + + wp_set_current_user( $this->author_id ); + $this->assertStringNotContainsString( self::PRIVATE_CELL, $this->$reader() ); + + wp_publish_post( $this->chart_id ); + $this->assertStringContainsString( self::PRIVATE_CELL, $this->$reader() ); + } + + /** + * The rule holds for a chart without a usable post type, and does not touch a chart without a post-type query. + */ + public function test_rule_needs_a_post_type_with_an_admin_screen() { + wp_set_current_user( $this->editor_id ); + + update_post_meta( $this->chart_id, Visualizer_Plugin::CF_FILTER_CONFIG, array( 'type' => 'revision' ) ); + $this->assertFalse( Visualizer_Module::can_read_chart_rows( $this->chart_id ) ); + + update_post_meta( $this->chart_id, Visualizer_Plugin::CF_FILTER_CONFIG, 'broken' ); + $this->assertFalse( Visualizer_Module::can_read_chart_rows( $this->chart_id ) ); + + delete_post_meta( $this->chart_id, Visualizer_Plugin::CF_FILTER_CONFIG ); + wp_set_current_user( $this->author_id ); + $this->assertStringContainsString( self::PRIVATE_CELL, $this->read_block_field() ); + } + + /** + * A revision of a published chart follows the status of the chart. + */ + public function test_revision_follows_the_status_of_its_chart() { + wp_set_current_user( $this->author_id ); + $revision_id = $this->factory->post->create( + array( + 'post_type' => 'revision', + 'post_status' => 'inherit', + 'post_parent' => $this->chart_id, + 'post_name' => $this->chart_id . '-revision-v1', + ) + ); + // update_post_meta() writes to the chart, not to its revision. + update_metadata( 'post', $revision_id, Visualizer_Plugin::CF_FILTER_CONFIG, array( 'type' => 'post' ) ); + + $this->assertFalse( Visualizer_Module::can_read_chart_rows( $revision_id ) ); + + wp_publish_post( $this->chart_id ); + $this->assertTrue( Visualizer_Module::can_read_chart_rows( $revision_id ) ); + } + + /** + * Read the REST field of the block editor. + * + * @return string + */ + private function read_block_field() { + return (string) wp_json_encode( Visualizer_Gutenberg_Block::get_instance()->get_visualizer_data( array( 'id' => $this->chart_id ) ) ); + } + + /** + * Read the chart through the get-chart ability. + * + * @return string + */ + private function read_ability() { + $module = Visualizer_Plugin::instance()->getModule( Visualizer_Module_Abilities::NAME ); + return (string) wp_json_encode( $module->executeGetChart( array( 'chart_id' => $this->chart_id ) ) ); + } + + /** + * Read the CSV export of the chart. + * + * @return string + */ + private function read_export() { + $module = Visualizer_Plugin::instance()->getModule( Visualizer_Module_Chart::NAME ); + return (string) wp_json_encode( $module->_getDataAs( $this->chart_id, 'csv' ) ); + } + + /** + * Render the shortcode of the chart and read the charts that the front end collected for the page scripts. + * + * @return string + */ + private function read_shortcode() { + do_shortcode( '[visualizer id="' . $this->chart_id . '"]' ); + + $charts = new ReflectionProperty( Visualizer_Module_Frontend::class, '_charts' ); + $charts->setAccessible( true ); + $module = Visualizer_Plugin::instance()->getModule( Visualizer_Module_Frontend::NAME ); + $data = (string) wp_json_encode( $charts->getValue( $module ) ); + $charts->setValue( $module, array() ); + return $data; + } + + /** + * Open the chart editor and read its page. + * + * @return string + */ + private function read_chart_editor() { + $_GET = array( + 'chart' => $this->chart_id, + 'tab' => 'settings', + 'nonce' => wp_create_nonce(), + ); + $_REQUEST = $_GET; + $this->_last_response = ''; + try { + $this->_handleAjax( Visualizer_Plugin::ACTION_EDIT_CHART ); + } catch ( WPAjaxDieContinueException $e ) { + // Expected after the page is printed. + unset( $e ); + } catch ( WPAjaxDieStopException $e ) { + unset( $e ); + } + return $this->_last_response . $this->script_data(); + } + + /** + * Render the chart library and read the chart data that it gives to the page scripts. + * + * @return string + */ + private function read_library() { + $_GET = array(); + ob_start(); + Visualizer_Plugin::instance()->getModule( Visualizer_Module_Admin::NAME )->renderLibraryPage(); + ob_end_clean(); + return $this->script_data(); + } + + /** + * Collect the data that the registered scripts get, and forget it for the next read. + * + * @return string + */ + private function script_data() { + $data = ''; + foreach ( wp_scripts()->registered as $script ) { + if ( ! empty( $script->extra['data'] ) ) { + $data .= $script->extra['data']; + unset( $script->extra['data'] ); + } + } + return $data; + } +} From 176ff0c1ef907518f9fddc68870a1a3c08704f32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vytis=20Bulkevi=C4=8Dius?= Date: Tue, 1 Dec 2026 22:41:10 +0200 Subject: [PATCH 02/11] test: give the chart of the rows test the settings that the chart editor reads Co-Authored-By: Claude Mythos 5.2 --- tests/test-chart-rows-permissions.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/tests/test-chart-rows-permissions.php b/tests/test-chart-rows-permissions.php index 1464f3fa..cdc077e5 100644 --- a/tests/test-chart-rows-permissions.php +++ b/tests/test-chart-rows-permissions.php @@ -56,7 +56,22 @@ public function set_up() { ); update_post_meta( $this->chart_id, Visualizer_Plugin::CF_CHART_TYPE, 'line' ); update_post_meta( $this->chart_id, Visualizer_Plugin::CF_CHART_LIBRARY, 'ChartJS' ); - update_post_meta( $this->chart_id, Visualizer_Plugin::CF_SETTINGS, array() ); + // The settings that the chart editor reads without a default. + $font = array( + 'fontName' => '', + 'fontFamily' => '', + 'fontSize' => '', + ); + update_post_meta( + $this->chart_id, + Visualizer_Plugin::CF_SETTINGS, + array( + 'legend' => array( 'labels' => $font ), + 'tooltip' => array( 'intersect' => '' ), + 'xAxes' => array( 'scaleLabel' => $font ), + 'yAxes' => array( 'scaleLabel' => $font ), + ) + ); update_post_meta( $this->chart_id, Visualizer_Plugin::CF_SERIES, From d6ec928162671b021e555ddd822a076f83d02d73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vytis=20Bulkevi=C4=8Dius?= Date: Wed, 2 Dec 2026 02:12:20 +0200 Subject: [PATCH 03/11] fix: keep the stored rows when a user who cannot read them saves a manual editor Co-Authored-By: Claude Mythos 5.2 --- classes/Visualizer/Module/Chart.php | 9 ++++++ tests/test-chart-rows-permissions.php | 42 +++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/classes/Visualizer/Module/Chart.php b/classes/Visualizer/Module/Chart.php index 390400e8..9fbfa89e 100644 --- a/classes/Visualizer/Module/Chart.php +++ b/classes/Visualizer/Module/Chart.php @@ -1313,6 +1313,15 @@ 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 manual editor starts from the stored rows. It does not get rows that the user cannot read, so its content must not replace them. + $from_new_query = isset( $_POST['chart_data_src'] ) && Visualizer_Plugin::CF_SOURCE_FILTER === $_POST['chart_data_src']; + $from_editor = ( isset( $_POST['chart_data'] ) && strlen( $_POST['chart_data'] ) > 0 ) || ( isset( $_POST['table_data'] ) && 'yes' === $_POST['table_data'] ); + if ( $can_die && $from_editor && ! $from_new_query && ! self::can_read_chart_rows( $chart_id ) ) { + $render = new Visualizer_Render_Page_Update( array( 'message' => esc_html__( 'You cannot edit 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 ); } diff --git a/tests/test-chart-rows-permissions.php b/tests/test-chart-rows-permissions.php index cdc077e5..7d3eddf9 100644 --- a/tests/test-chart-rows-permissions.php +++ b/tests/test-chart-rows-permissions.php @@ -168,6 +168,48 @@ public function test_revision_follows_the_status_of_its_chart() { $this->assertTrue( Visualizer_Module::can_read_chart_rows( $revision_id ) ); } + /** + * A manual editor does not have the rows that its user cannot read. Its content does not replace them and + * does not remove the post-type query. The content of a user who can read them is saved. + */ + public function test_manual_editor_does_not_replace_rows_that_its_user_cannot_read() { + wp_set_current_user( $this->author_id ); + $this->save_text_editor(); + $this->assertStringContainsString( self::PRIVATE_CELL, get_post_field( 'post_content', $this->chart_id, 'raw' ) ); + $this->assertSame( array( 'type' => 'post' ), get_post_meta( $this->chart_id, Visualizer_Plugin::CF_FILTER_CONFIG, true ) ); + + wp_set_current_user( $this->editor_id ); + $this->save_text_editor(); + $content = get_post_field( 'post_content', $this->chart_id, 'raw' ); + $this->assertStringContainsString( 'Edited cell', $content ); + $this->assertStringNotContainsString( self::PRIVATE_CELL, $content ); + } + + /** + * Save the text editor of the chart with one edited row, as the current user. + */ + private function save_text_editor() { + $_GET = array( + 'nonce' => wp_create_nonce( 'visualizer-upload-data' ), + 'chart' => $this->chart_id, + ); + // An earlier upload test can leave its file here, and a file comes before the editor content. + $_FILES = array(); + $_POST = array( + 'chart_data' => "x,y\nstring,number\nEdited cell,2", + 'editor-type' => 'text', + ); + ob_start(); + try { + $this->_handleAjax( Visualizer_Plugin::ACTION_UPLOAD_DATA ); + } catch ( WPAjaxDieContinueException $e ) { + // The handler ends the request after it answers. + } catch ( WPAjaxDieStopException $e ) { + // The handler ends the request after it answers. + } + ob_end_clean(); + } + /** * Read the REST field of the block editor. * From f05ac707c310da4baf293d9eb239c42736e1d17c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vytis=20Bulkevi=C4=8Dius?= Date: Wed, 2 Dec 2026 07:06:22 +0200 Subject: [PATCH 04/11] fix: refuse every upload that removes a query its user cannot read Co-Authored-By: Claude Mythos 5.2 --- classes/Visualizer/Module/Chart.php | 8 ++++---- tests/test-chart-rows-permissions.php | 21 +++++++++++++-------- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/classes/Visualizer/Module/Chart.php b/classes/Visualizer/Module/Chart.php index 9fbfa89e..bb086e58 100644 --- a/classes/Visualizer/Module/Chart.php +++ b/classes/Visualizer/Module/Chart.php @@ -1313,11 +1313,11 @@ 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 manual editor starts from the stored rows. It does not get rows that the user cannot read, so its content must not replace them. + // Any other 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 the query. $from_new_query = isset( $_POST['chart_data_src'] ) && Visualizer_Plugin::CF_SOURCE_FILTER === $_POST['chart_data_src']; - $from_editor = ( isset( $_POST['chart_data'] ) && strlen( $_POST['chart_data'] ) > 0 ) || ( isset( $_POST['table_data'] ) && 'yes' === $_POST['table_data'] ); - if ( $can_die && $from_editor && ! $from_new_query && ! self::can_read_chart_rows( $chart_id ) ) { - $render = new Visualizer_Render_Page_Update( array( 'message' => esc_html__( 'You cannot edit the data of this chart, because it comes from posts that you cannot read.', 'visualizer' ) ) ); + if ( $can_die && ! $from_new_query && ! 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(); } diff --git a/tests/test-chart-rows-permissions.php b/tests/test-chart-rows-permissions.php index 7d3eddf9..5cd3018c 100644 --- a/tests/test-chart-rows-permissions.php +++ b/tests/test-chart-rows-permissions.php @@ -169,14 +169,17 @@ public function test_revision_follows_the_status_of_its_chart() { } /** - * A manual editor does not have the rows that its user cannot read. Its content does not replace them and - * does not remove the post-type query. The content of a user who can read them is saved. + * A manual editor does not have the rows that its user cannot read. Its content does not replace them, and + * no upload of that user removes the post-type query. The content of a user who can read them is saved. */ public function test_manual_editor_does_not_replace_rows_that_its_user_cannot_read() { wp_set_current_user( $this->author_id ); - $this->save_text_editor(); - $this->assertStringContainsString( self::PRIVATE_CELL, get_post_field( 'post_content', $this->chart_id, 'raw' ) ); - $this->assertSame( array( 'type' => 'post' ), get_post_meta( $this->chart_id, Visualizer_Plugin::CF_FILTER_CONFIG, true ) ); + foreach ( array( "x,y\nstring,number\nEdited cell,2", '' ) as $chart_data ) { + $this->save_text_editor( $chart_data ); + $this->assertStringContainsString( self::PRIVATE_CELL, get_post_field( 'post_content', $this->chart_id, 'raw' ) ); + $this->assertSame( array( 'type' => 'post' ), get_post_meta( $this->chart_id, Visualizer_Plugin::CF_FILTER_CONFIG, true ) ); + $this->assertFalse( Visualizer_Module::can_read_chart_rows( $this->chart_id ) ); + } wp_set_current_user( $this->editor_id ); $this->save_text_editor(); @@ -186,9 +189,11 @@ public function test_manual_editor_does_not_replace_rows_that_its_user_cannot_re } /** - * Save the text editor of the chart with one edited row, as the current user. + * Save the text editor of the chart, as the current user. + * + * @param string $chart_data The content of the editor. */ - private function save_text_editor() { + private function save_text_editor( $chart_data = "x,y\nstring,number\nEdited cell,2" ) { $_GET = array( 'nonce' => wp_create_nonce( 'visualizer-upload-data' ), 'chart' => $this->chart_id, @@ -196,7 +201,7 @@ private function save_text_editor() { // An earlier upload test can leave its file here, and a file comes before the editor content. $_FILES = array(); $_POST = array( - 'chart_data' => "x,y\nstring,number\nEdited cell,2", + 'chart_data' => $chart_data, 'editor-type' => 'text', ); ob_start(); From d3b664332c14ca6f791ea4ad12c383805fdd6cea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vytis=20Bulkevi=C4=8Dius?= Date: Wed, 2 Dec 2026 10:36:44 +0200 Subject: [PATCH 05/11] fix: keep unreadable rows out of the revision comparison and the chart image setting Co-Authored-By: Claude Mythos 5.2 --- classes/Visualizer/Module/Chart.php | 7 ++- classes/Visualizer/Module/Setup.php | 36 ++++++++++++++++ tests/test-chart-rows-permissions.php | 61 ++++++++++++++++++++++++--- 3 files changed, 93 insertions(+), 11 deletions(-) diff --git a/classes/Visualizer/Module/Chart.php b/classes/Visualizer/Module/Chart.php index bb086e58..57807d94 100644 --- a/classes/Visualizer/Module/Chart.php +++ b/classes/Visualizer/Module/Chart.php @@ -1313,10 +1313,9 @@ 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__ ); - // Any other 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 the query. - $from_new_query = isset( $_POST['chart_data_src'] ) && Visualizer_Plugin::CF_SOURCE_FILTER === $_POST['chart_data_src']; - if ( $can_die && ! $from_new_query && ! self::can_read_chart_rows( $chart_id ) ) { + // 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(); diff --git a/classes/Visualizer/Module/Setup.php b/classes/Visualizer/Module/Setup.php index cea683fd..5766a57c 100644 --- a/classes/Visualizer/Module/Setup.php +++ b/classes/Visualizer/Module/Setup.php @@ -69,6 +69,8 @@ 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( 'admin_init', 'adminInit' ); $this->_addAction( 'init', 'setupCustomPostTypes' ); @@ -312,6 +314,40 @@ 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 $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 + */ + 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 ) || self::can_read_chart_rows( $compare_to->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; + } + /** * Marks the chart meta as protected. * diff --git a/tests/test-chart-rows-permissions.php b/tests/test-chart-rows-permissions.php index 5cd3018c..1c86862b 100644 --- a/tests/test-chart-rows-permissions.php +++ b/tests/test-chart-rows-permissions.php @@ -18,6 +18,13 @@ class Test_Visualizer_Chart_Rows_Permissions extends WP_Ajax_UnitTestCase { */ const PRIVATE_CELL = 'Private post title 4f1c'; + /** + * The data of the picture that the chart editor saves in the settings. The picture shows the rows. + * + * @var string + */ + const PRIVATE_IMAGE = 'UFJJVkFURQ'; + /** * The chart owner: an Author, who cannot read the private posts of other users. * @@ -66,7 +73,8 @@ public function set_up() { $this->chart_id, Visualizer_Plugin::CF_SETTINGS, array( - 'legend' => array( 'labels' => $font ), + 'legend' => array( 'labels' => $font ), + 'chart-img' => 'data:image/png;base64,' . self::PRIVATE_IMAGE, 'tooltip' => array( 'intersect' => '' ), 'xAxes' => array( 'scaleLabel' => $font ), 'yAxes' => array( 'scaleLabel' => $font ), @@ -123,7 +131,9 @@ public function test_reader_gives_unpublished_rows_only_to_a_user_who_passes_the $this->assertStringContainsString( self::PRIVATE_CELL, $this->$reader() ); wp_set_current_user( $this->author_id ); - $this->assertStringNotContainsString( self::PRIVATE_CELL, $this->$reader() ); + $read = $this->$reader(); + $this->assertStringNotContainsString( self::PRIVATE_CELL, $read ); + $this->assertStringNotContainsString( self::PRIVATE_IMAGE, $read ); wp_publish_post( $this->chart_id ); $this->assertStringContainsString( self::PRIVATE_CELL, $this->$reader() ); @@ -168,14 +178,49 @@ public function test_revision_follows_the_status_of_its_chart() { $this->assertTrue( Visualizer_Module::can_read_chart_rows( $revision_id ) ); } + /** + * The block editor field gives the rendered chart image to a user who can read the rows. + */ + public function test_reader_gives_the_chart_image_to_a_user_who_passes_the_rule() { + wp_set_current_user( $this->editor_id ); + $this->assertStringContainsString( self::PRIVATE_IMAGE, $this->read_block_field() ); + } + + /** + * The revision comparison of WordPress gives the chart content only to a user who can read the rows. + */ + public function test_revision_comparison_gives_rows_only_to_a_user_who_passes_the_rule() { + wp_set_current_user( $this->editor_id ); + $this->assertStringContainsString( self::PRIVATE_CELL, $this->read_revision_comparison() ); + + wp_set_current_user( $this->author_id ); + $this->assertStringNotContainsString( self::PRIVATE_CELL, $this->read_revision_comparison() ); + } + + /** + * Compare the chart with no older revision, as the revisions screen does through `wp_ajax_get_revision_diffs()`. + * + * @return string + */ + private function read_revision_comparison() { + require_once ABSPATH . 'wp-admin/includes/revision.php'; + return (string) wp_json_encode( wp_get_revision_ui_diff( $this->chart_id, 0, $this->chart_id ) ); + } + /** * A manual editor does not have the rows that its user cannot read. Its content does not replace them, and * no upload of that user removes the post-type query. The content of a user who can read them is saved. */ public function test_manual_editor_does_not_replace_rows_that_its_user_cannot_read() { wp_set_current_user( $this->author_id ); - foreach ( array( "x,y\nstring,number\nEdited cell,2", '' ) as $chart_data ) { - $this->save_text_editor( $chart_data ); + $saves = array( + array( "x,y\nstring,number\nEdited cell,2", '' ), + array( '', '' ), + // A request that names the post-type query as its source brings other data too. + array( "x,y\nstring,number\nEdited cell,2", Visualizer_Plugin::CF_SOURCE_FILTER ), + ); + foreach ( $saves as list( $chart_data, $source ) ) { + $this->save_text_editor( $chart_data, $source ); $this->assertStringContainsString( self::PRIVATE_CELL, get_post_field( 'post_content', $this->chart_id, 'raw' ) ); $this->assertSame( array( 'type' => 'post' ), get_post_meta( $this->chart_id, Visualizer_Plugin::CF_FILTER_CONFIG, true ) ); $this->assertFalse( Visualizer_Module::can_read_chart_rows( $this->chart_id ) ); @@ -192,8 +237,9 @@ public function test_manual_editor_does_not_replace_rows_that_its_user_cannot_re * Save the text editor of the chart, as the current user. * * @param string $chart_data The content of the editor. + * @param string $source The data source that the request names. */ - private function save_text_editor( $chart_data = "x,y\nstring,number\nEdited cell,2" ) { + private function save_text_editor( $chart_data = "x,y\nstring,number\nEdited cell,2", $source = '' ) { $_GET = array( 'nonce' => wp_create_nonce( 'visualizer-upload-data' ), 'chart' => $this->chart_id, @@ -201,8 +247,9 @@ private function save_text_editor( $chart_data = "x,y\nstring,number\nEdited cel // An earlier upload test can leave its file here, and a file comes before the editor content. $_FILES = array(); $_POST = array( - 'chart_data' => $chart_data, - 'editor-type' => 'text', + 'chart_data' => $chart_data, + 'chart_data_src' => $source, + 'editor-type' => 'text', ); ob_start(); try { From 88c1d05f6e15a6f2346baf4620f2992267518556 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vytis=20Bulkevi=C4=8Dius?= Date: Wed, 2 Dec 2026 10:51:41 +0200 Subject: [PATCH 06/11] fix: check both sides of a chart revision comparison Co-Authored-By: Claude Mythos 5.2 --- classes/Visualizer/Module/Setup.php | 6 +++- tests/test-chart-rows-permissions.php | 47 ++++++++++++++++++++++----- 2 files changed, 44 insertions(+), 9 deletions(-) diff --git a/classes/Visualizer/Module/Setup.php b/classes/Visualizer/Module/Setup.php index 5766a57c..950bdf44 100644 --- a/classes/Visualizer/Module/Setup.php +++ b/classes/Visualizer/Module/Setup.php @@ -326,7 +326,11 @@ public function adminInit() { */ 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 ) || self::can_read_chart_rows( $compare_to->ID ) ) { + 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' ) ); diff --git a/tests/test-chart-rows-permissions.php b/tests/test-chart-rows-permissions.php index 1c86862b..93d8e9a9 100644 --- a/tests/test-chart-rows-permissions.php +++ b/tests/test-chart-rows-permissions.php @@ -131,9 +131,7 @@ public function test_reader_gives_unpublished_rows_only_to_a_user_who_passes_the $this->assertStringContainsString( self::PRIVATE_CELL, $this->$reader() ); wp_set_current_user( $this->author_id ); - $read = $this->$reader(); - $this->assertStringNotContainsString( self::PRIVATE_CELL, $read ); - $this->assertStringNotContainsString( self::PRIVATE_IMAGE, $read ); + $this->assertStringNotContainsString( self::PRIVATE_CELL, $this->$reader() ); wp_publish_post( $this->chart_id ); $this->assertStringContainsString( self::PRIVATE_CELL, $this->$reader() ); @@ -179,11 +177,14 @@ public function test_revision_follows_the_status_of_its_chart() { } /** - * The block editor field gives the rendered chart image to a user who can read the rows. + * The block editor field gives the rendered chart image only to a user who can read the rows. */ - public function test_reader_gives_the_chart_image_to_a_user_who_passes_the_rule() { + public function test_reader_gives_the_chart_image_only_to_a_user_who_passes_the_rule() { wp_set_current_user( $this->editor_id ); $this->assertStringContainsString( self::PRIVATE_IMAGE, $this->read_block_field() ); + + wp_set_current_user( $this->author_id ); + $this->assertStringNotContainsString( self::PRIVATE_IMAGE, $this->read_block_field() ); } /** @@ -198,13 +199,43 @@ public function test_revision_comparison_gives_rows_only_to_a_user_who_passes_th } /** - * Compare the chart with no older revision, as the revisions screen does through `wp_ajax_get_revision_diffs()`. + * The comparison does not give the rows of an older revision with a post-type query after the chart got other data. + */ + public function test_revision_comparison_keeps_the_rows_of_an_older_revision() { + $revision_id = $this->factory->post->create( + array( + 'post_type' => 'revision', + 'post_status' => 'inherit', + 'post_parent' => $this->chart_id, + 'post_name' => $this->chart_id . '-revision-v1', + 'post_content' => get_post_field( 'post_content', $this->chart_id, 'raw' ), + ) + ); + update_metadata( 'post', $revision_id, Visualizer_Plugin::CF_FILTER_CONFIG, array( 'type' => 'post' ) ); + delete_post_meta( $this->chart_id, Visualizer_Plugin::CF_FILTER_CONFIG ); + wp_update_post( + array( + 'ID' => $this->chart_id, + 'post_content' => 'other data', + ) + ); + + wp_set_current_user( $this->editor_id ); + $this->assertStringContainsString( self::PRIVATE_CELL, $this->read_revision_comparison( $revision_id ) ); + + wp_set_current_user( $this->author_id ); + $this->assertStringNotContainsString( self::PRIVATE_CELL, $this->read_revision_comparison( $revision_id ) ); + } + + /** + * Compare the chart with an older revision, as the revisions screen does through `wp_ajax_get_revision_diffs()`. * + * @param int $compare_from The ID of the older revision, or 0 for none. * @return string */ - private function read_revision_comparison() { + private function read_revision_comparison( $compare_from = 0 ) { require_once ABSPATH . 'wp-admin/includes/revision.php'; - return (string) wp_json_encode( wp_get_revision_ui_diff( $this->chart_id, 0, $this->chart_id ) ); + return (string) wp_json_encode( wp_get_revision_ui_diff( $this->chart_id, $compare_from, $this->chart_id ) ); } /** From 243d590e0f163b1f9483319739c8ded28ddc2423 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vytis=20Bulkevi=C4=8Dius?= Date: Wed, 2 Dec 2026 10:52:23 +0200 Subject: [PATCH 07/11] test: date the older chart revision before the chart Co-Authored-By: Claude Mythos 5.2 --- tests/test-chart-rows-permissions.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test-chart-rows-permissions.php b/tests/test-chart-rows-permissions.php index 93d8e9a9..f2c5fc10 100644 --- a/tests/test-chart-rows-permissions.php +++ b/tests/test-chart-rows-permissions.php @@ -209,6 +209,8 @@ public function test_revision_comparison_keeps_the_rows_of_an_older_revision() { 'post_parent' => $this->chart_id, 'post_name' => $this->chart_id . '-revision-v1', 'post_content' => get_post_field( 'post_content', $this->chart_id, 'raw' ), + // WordPress takes the revision with the later date as the newer side. + 'post_date' => '2020-01-01 00:00:00', ) ); update_metadata( 'post', $revision_id, Visualizer_Plugin::CF_FILTER_CONFIG, array( 'type' => 'post' ) ); From 1557394b34bcf4ea898d9fd8fb4d685024272686 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vytis=20Bulkevi=C4=8Dius?= Date: Wed, 2 Dec 2026 10:53:23 +0200 Subject: [PATCH 08/11] test: compare two chart revisions Co-Authored-By: Claude Mythos 5.2 --- tests/test-chart-rows-permissions.php | 51 +++++++++++++++------------ 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/tests/test-chart-rows-permissions.php b/tests/test-chart-rows-permissions.php index f2c5fc10..114b6b80 100644 --- a/tests/test-chart-rows-permissions.php +++ b/tests/test-chart-rows-permissions.php @@ -199,45 +199,50 @@ public function test_revision_comparison_gives_rows_only_to_a_user_who_passes_th } /** - * The comparison does not give the rows of an older revision with a post-type query after the chart got other data. + * The comparison of two revisions does not give the rows of the older one after the chart got other data. */ public function test_revision_comparison_keeps_the_rows_of_an_older_revision() { - $revision_id = $this->factory->post->create( + $older_id = $this->create_revision( '2020-01-01 00:00:00', get_post_field( 'post_content', $this->chart_id, 'raw' ) ); + $newer_id = $this->create_revision( '2021-01-01 00:00:00', 'other data' ); + // update_post_meta() writes to the chart, not to its revision. + update_metadata( 'post', $older_id, Visualizer_Plugin::CF_FILTER_CONFIG, array( 'type' => 'post' ) ); + + wp_set_current_user( $this->editor_id ); + $this->assertStringContainsString( self::PRIVATE_CELL, $this->read_revision_comparison( $older_id, $newer_id ) ); + + wp_set_current_user( $this->author_id ); + $this->assertStringNotContainsString( self::PRIVATE_CELL, $this->read_revision_comparison( $older_id, $newer_id ) ); + } + + /** + * Create a revision of the chart. + * + * @param string $date The date of the revision. + * @param string $content The content of the revision. + * @return int + */ + private function create_revision( $date, $content ) { + return $this->factory->post->create( array( 'post_type' => 'revision', 'post_status' => 'inherit', 'post_parent' => $this->chart_id, - 'post_name' => $this->chart_id . '-revision-v1', - 'post_content' => get_post_field( 'post_content', $this->chart_id, 'raw' ), - // WordPress takes the revision with the later date as the newer side. - 'post_date' => '2020-01-01 00:00:00', - ) - ); - update_metadata( 'post', $revision_id, Visualizer_Plugin::CF_FILTER_CONFIG, array( 'type' => 'post' ) ); - delete_post_meta( $this->chart_id, Visualizer_Plugin::CF_FILTER_CONFIG ); - wp_update_post( - array( - 'ID' => $this->chart_id, - 'post_content' => 'other data', + 'post_content' => $content, + 'post_date' => $date, ) ); - - wp_set_current_user( $this->editor_id ); - $this->assertStringContainsString( self::PRIVATE_CELL, $this->read_revision_comparison( $revision_id ) ); - - wp_set_current_user( $this->author_id ); - $this->assertStringNotContainsString( self::PRIVATE_CELL, $this->read_revision_comparison( $revision_id ) ); } /** - * Compare the chart with an older revision, as the revisions screen does through `wp_ajax_get_revision_diffs()`. + * Compare two versions of the chart, as the revisions screen does through `wp_ajax_get_revision_diffs()`. * * @param int $compare_from The ID of the older revision, or 0 for none. + * @param int $compare_to The ID of the newer revision, or 0 for the chart. * @return string */ - private function read_revision_comparison( $compare_from = 0 ) { + private function read_revision_comparison( $compare_from = 0, $compare_to = 0 ) { require_once ABSPATH . 'wp-admin/includes/revision.php'; - return (string) wp_json_encode( wp_get_revision_ui_diff( $this->chart_id, $compare_from, $this->chart_id ) ); + return (string) wp_json_encode( wp_get_revision_ui_diff( $this->chart_id, $compare_from, $compare_to ? $compare_to : $this->chart_id ) ); } /** From 8f6eb6c103037f78bc810cb3891e8a43f55048f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vytis=20Bulkevi=C4=8Dius?= Date: Wed, 2 Dec 2026 11:25:44 +0200 Subject: [PATCH 09/11] fix: keep no saved image for an unpublished chart with a post-type query Co-Authored-By: Claude Mythos 5.2 --- classes/Visualizer/Module.php | 35 ++++++++++- classes/Visualizer/Module/Chart.php | 4 +- classes/Visualizer/Module/Setup.php | 31 ++++++++++ classes/Visualizer/Module/Upgrade.php | 32 ++++++++++ tests/test-chart-rows-permissions.php | 88 +++++++++++++++++++++++++++ 5 files changed, 185 insertions(+), 5 deletions(-) diff --git a/classes/Visualizer/Module.php b/classes/Visualizer/Module.php index d2a4e4ff..4e27fdc9 100644 --- a/classes/Visualizer/Module.php +++ b/classes/Visualizer/Module.php @@ -890,12 +890,11 @@ public static function maybe_decode_content( $value ) { * @return bool */ public static function can_read_chart_rows( $chart_id ) { - $filter_config = get_post_meta( $chart_id, Visualizer_Plugin::CF_FILTER_CONFIG, true ); - $parent_id = wp_is_post_revision( $chart_id ); - if ( empty( $filter_config ) || 'publish' === get_post_status( $parent_id ? $parent_id : $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 @@ -903,6 +902,36 @@ public static function can_read_chart_rows( $chart_id ) { && current_user_can( $post_type->cap->read_private_posts ); } + /** + * 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. + * @return void + */ + public static function delete_image_of_private_rows( $chart_id ) { + if ( 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. * diff --git a/classes/Visualizer/Module/Chart.php b/classes/Visualizer/Module/Chart.php index a7aedcad..c6ed9c83 100644 --- a/classes/Visualizer/Module/Chart.php +++ b/classes/Visualizer/Module/Chart.php @@ -1788,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 ) { @@ -1800,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; diff --git a/classes/Visualizer/Module/Setup.php b/classes/Visualizer/Module/Setup.php index 7821c036..27546c3b 100644 --- a/classes/Visualizer/Module/Setup.php +++ b/classes/Visualizer/Module/Setup.php @@ -71,6 +71,9 @@ public function __construct( Visualizer_Plugin $plugin ) { $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 ); @@ -354,6 +357,34 @@ public function remove_image_of_unreadable_rows( $settings, $chart_id ) { 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. + * + * @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 ( Visualizer_Plugin::CF_FILTER_CONFIG === $meta_key ) { + self::delete_image_of_private_rows( $chart_id ); + } + } + /** * Marks the chart meta as protected. * diff --git a/classes/Visualizer/Module/Upgrade.php b/classes/Visualizer/Module/Upgrade.php index 1dcf3b9e..647b5a71 100644 --- a/classes/Visualizer/Module/Upgrade.php +++ b/classes/Visualizer/Module/Upgrade.php @@ -24,6 +24,11 @@ public static function upgrade() { $upgraded = true; } + if ( version_compare( $last_version, '4.0.13', '<' ) ) { + self::deleteImagesOfPrivateRows(); + $upgraded = true; + } + if ( ! $upgraded ) { return; } @@ -34,6 +39,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. diff --git a/tests/test-chart-rows-permissions.php b/tests/test-chart-rows-permissions.php index 114b6b80..b5386725 100644 --- a/tests/test-chart-rows-permissions.php +++ b/tests/test-chart-rows-permissions.php @@ -25,6 +25,13 @@ class Test_Visualizer_Chart_Rows_Permissions extends WP_Ajax_UnitTestCase { */ const PRIVATE_IMAGE = 'UFJJVkFURQ'; + /** + * A 1x1 PNG as the chart editor sends it. + * + * @var string + */ + const PNG = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg=='; + /** * The chart owner: an Author, who cannot read the private posts of other users. * @@ -397,4 +404,85 @@ private function script_data() { } return $data; } + + /** + * A visitor can read each image in the uploads folder, so an unpublished chart with a post-type query gets no saved image. + */ + public function test_image_is_not_saved_for_an_unpublished_chart() { + $module = Visualizer_Plugin::instance()->getModule( Visualizer_Module_Chart::NAME ); + $this->assertSame( 0, $module->save_chart_image( self::PNG, $this->chart_id ) ); + + $image_id = $this->save_image_of_published_chart(); + $this->assertSame( 200, $this->read_image_as_visitor( $image_id ) ); + } + + /** + * The saved image is deleted when the chart is no longer published. + */ + public function test_image_is_deleted_when_the_chart_is_unpublished() { + $image_id = $this->save_image_of_published_chart(); + wp_update_post( + array( + 'ID' => $this->chart_id, + 'post_status' => 'draft', + ) + ); + + $this->assertSame( 404, $this->read_image_as_visitor( $image_id ) ); + wp_set_current_user( $this->author_id ); + $this->assertStringNotContainsString( 'chart_id, Visualizer_Plugin::CF_FILTER_CONFIG ); + $module = Visualizer_Plugin::instance()->getModule( Visualizer_Module_Chart::NAME ); + $image_id = $module->save_chart_image( self::PNG, $this->chart_id ); + update_post_meta( $this->chart_id, Visualizer_Plugin::CF_CHART_IMAGE, $image_id ); + $this->assertSame( 200, $this->read_image_as_visitor( $image_id ) ); + + update_post_meta( $this->chart_id, Visualizer_Plugin::CF_FILTER_CONFIG, array( 'type' => 'post' ) ); + $this->assertSame( 404, $this->read_image_as_visitor( $image_id ) ); + } + + /** + * The upgrade deletes the image that an earlier version saved for an unpublished chart. + */ + public function test_upgrade_deletes_the_image_of_an_unpublished_chart() { + global $wpdb; + $image_id = $this->save_image_of_published_chart(); + // An earlier version kept the image after the status change. + $wpdb->update( $wpdb->posts, array( 'post_status' => 'draft' ), array( 'ID' => $this->chart_id ) ); + clean_post_cache( $this->chart_id ); + $this->assertSame( 200, $this->read_image_as_visitor( $image_id ) ); + + delete_option( 'visualizer-upgraded' ); + Visualizer_Module_Upgrade::upgrade(); + $this->assertSame( 404, $this->read_image_as_visitor( $image_id ) ); + } + + /** + * Publish the chart and save its image. + * + * @return int The attachment ID. + */ + private function save_image_of_published_chart() { + wp_publish_post( $this->chart_id ); + $image_id = Visualizer_Plugin::instance()->getModule( Visualizer_Module_Chart::NAME )->save_chart_image( self::PNG, $this->chart_id ); + update_post_meta( $this->chart_id, Visualizer_Plugin::CF_CHART_IMAGE, $image_id ); + return $image_id; + } + + /** + * Read the image through the media REST route, as a visitor. + * + * @param int $image_id The attachment ID. + * @return int The HTTP status. + */ + private function read_image_as_visitor( $image_id ) { + wp_set_current_user( 0 ); + return rest_do_request( new WP_REST_Request( 'GET', '/wp/v2/media/' . $image_id ) )->get_status(); + } } From dd72a304f01b333c7a72f1f0f2f2927d2ada913e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vytis=20Bulkevi=C4=8Dius?= Date: Wed, 2 Dec 2026 12:11:09 +0200 Subject: [PATCH 10/11] fix: delete the image of an unpublished chart for each order of its meta Co-Authored-By: Claude Mythos 5.2 --- classes/Visualizer/Module.php | 9 ++-- classes/Visualizer/Module/Setup.php | 4 +- classes/Visualizer/Module/Upgrade.php | 3 +- tests/test-chart-rows-permissions.php | 60 +++++++++++++++++++++++---- 4 files changed, 61 insertions(+), 15 deletions(-) diff --git a/classes/Visualizer/Module.php b/classes/Visualizer/Module.php index 4e27fdc9..27b0d47d 100644 --- a/classes/Visualizer/Module.php +++ b/classes/Visualizer/Module.php @@ -918,16 +918,17 @@ public static function has_public_rows( $chart_id ) { * * The image shows the rows, and each visitor can read a file in the uploads folder. * - * @param int $chart_id The chart ID. + * @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 ( self::has_public_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 ); + // An image that could not be deleted 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 ); } diff --git a/classes/Visualizer/Module/Setup.php b/classes/Visualizer/Module/Setup.php index 27546c3b..e89b943e 100644 --- a/classes/Visualizer/Module/Setup.php +++ b/classes/Visualizer/Module/Setup.php @@ -374,13 +374,15 @@ public function delete_image_after_status_change( $new_status, $old_status, $pos /** * 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 ( Visualizer_Plugin::CF_FILTER_CONFIG === $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 ); } } diff --git a/classes/Visualizer/Module/Upgrade.php b/classes/Visualizer/Module/Upgrade.php index 647b5a71..39c8b7c9 100644 --- a/classes/Visualizer/Module/Upgrade.php +++ b/classes/Visualizer/Module/Upgrade.php @@ -24,7 +24,8 @@ public static function upgrade() { $upgraded = true; } - if ( version_compare( $last_version, '4.0.13', '<' ) ) { + // 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; } diff --git a/tests/test-chart-rows-permissions.php b/tests/test-chart-rows-permissions.php index b5386725..c2725f9f 100644 --- a/tests/test-chart-rows-permissions.php +++ b/tests/test-chart-rows-permissions.php @@ -421,6 +421,7 @@ public function test_image_is_not_saved_for_an_unpublished_chart() { */ public function test_image_is_deleted_when_the_chart_is_unpublished() { $image_id = $this->save_image_of_published_chart(); + $file = get_attached_file( $image_id ); wp_update_post( array( 'ID' => $this->chart_id, @@ -429,26 +430,62 @@ public function test_image_is_deleted_when_the_chart_is_unpublished() { ); $this->assertSame( 404, $this->read_image_as_visitor( $image_id ) ); + $this->assertFileDoesNotExist( $file ); wp_set_current_user( $this->author_id ); $this->assertStringNotContainsString( 'chart_id, Visualizer_Plugin::CF_FILTER_CONFIG ); - $module = Visualizer_Plugin::instance()->getModule( Visualizer_Module_Chart::NAME ); - $image_id = $module->save_chart_image( self::PNG, $this->chart_id ); - update_post_meta( $this->chart_id, Visualizer_Plugin::CF_CHART_IMAGE, $image_id ); + public function test_image_is_deleted_when_an_unpublished_chart_gets_a_post_type_query( $query_first ) { + // An empty query is no query. It is there, so the next write is an update. + update_post_meta( $this->chart_id, Visualizer_Plugin::CF_FILTER_CONFIG, '' ); + $image_id = Visualizer_Plugin::instance()->getModule( Visualizer_Module_Chart::NAME )->save_chart_image( self::PNG, $this->chart_id ); $this->assertSame( 200, $this->read_image_as_visitor( $image_id ) ); - update_post_meta( $this->chart_id, Visualizer_Plugin::CF_FILTER_CONFIG, array( 'type' => 'post' ) ); + if ( $query_first ) { + update_post_meta( $this->chart_id, Visualizer_Plugin::CF_FILTER_CONFIG, array( 'type' => 'post' ) ); + add_post_meta( $this->chart_id, Visualizer_Plugin::CF_CHART_IMAGE, $image_id ); + } else { + add_post_meta( $this->chart_id, Visualizer_Plugin::CF_CHART_IMAGE, $image_id ); + $this->assertSame( 200, $this->read_image_as_visitor( $image_id ) ); + update_post_meta( $this->chart_id, Visualizer_Plugin::CF_FILTER_CONFIG, array( 'type' => 'post' ) ); + } $this->assertSame( 404, $this->read_image_as_visitor( $image_id ) ); } /** - * The upgrade deletes the image that an earlier version saved for an unpublished chart. + * The orders of the query and the image. + * + * @return array + */ + public function meta_orders() { + return array( + 'query first' => array( true ), + 'image first' => array( false ), + ); + } + + /** + * A query on a revision does not remove the image of its chart. + */ + public function test_query_of_a_revision_keeps_the_image_of_the_chart() { + delete_post_meta( $this->chart_id, Visualizer_Plugin::CF_FILTER_CONFIG ); + $image_id = Visualizer_Plugin::instance()->getModule( Visualizer_Module_Chart::NAME )->save_chart_image( self::PNG, $this->chart_id ); + update_post_meta( $this->chart_id, Visualizer_Plugin::CF_CHART_IMAGE, $image_id ); + + update_metadata( 'post', $this->create_revision( '2020-01-01 00:00:00', '' ), Visualizer_Plugin::CF_FILTER_CONFIG, array( 'type' => 'post' ) ); + $this->assertSame( $image_id, Visualizer_Module::get_chart_image_id( $this->chart_id ) ); + } + + /** + * The upgrade deletes the image that an earlier version saved for an unpublished chart, once for each version. */ public function test_upgrade_deletes_the_image_of_an_unpublished_chart() { global $wpdb; @@ -458,9 +495,14 @@ public function test_upgrade_deletes_the_image_of_an_unpublished_chart() { clean_post_cache( $this->chart_id ); $this->assertSame( 200, $this->read_image_as_visitor( $image_id ) ); - delete_option( 'visualizer-upgraded' ); + update_option( 'visualizer-upgraded', Visualizer_Plugin::VERSION ); + Visualizer_Module_Upgrade::upgrade(); + $this->assertSame( 200, $this->read_image_as_visitor( $image_id ) ); + + update_option( 'visualizer-upgraded', '4.0.0' ); Visualizer_Module_Upgrade::upgrade(); $this->assertSame( 404, $this->read_image_as_visitor( $image_id ) ); + $this->assertSame( Visualizer_Plugin::VERSION, get_option( 'visualizer-upgraded' ) ); } /** From 56fb503f54d868b631f060a8ba19e95787961081 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vytis=20Bulkevi=C4=8Dius?= Date: Wed, 2 Dec 2026 12:14:29 +0200 Subject: [PATCH 11/11] refactor: delete the chart image without a retry branch Co-Authored-By: Claude Mythos 5.2 --- classes/Visualizer/Module.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/classes/Visualizer/Module.php b/classes/Visualizer/Module.php index 27b0d47d..8be0865f 100644 --- a/classes/Visualizer/Module.php +++ b/classes/Visualizer/Module.php @@ -926,9 +926,8 @@ public static function delete_image_of_private_rows( $chart_id ) { return; } $image_id = self::get_chart_image_id( $chart_id ); - // An image that could not be deleted keeps its meta, so a later call finds it. - if ( $image_id && ! wp_delete_attachment( $image_id, true ) ) { - return; + if ( $image_id ) { + wp_delete_attachment( $image_id, true ); } delete_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_IMAGE ); }