Skip to content
Open

Release #1421

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
7f6ddb4
Sync branch [skip ci]
pirate-bot Nov 25, 2026
aa77bd9
Sync branch [skip ci]
pirate-bot Nov 25, 2026
5a425f1
fix: escape the attributes of chart action buttons
vytisbulkevicius Nov 26, 2026
94cbd14
fix: protect the chart meta keys
vytisbulkevicius Nov 26, 2026
3944494
fix: delete only the chart's own image when the image changes
vytisbulkevicius Nov 26, 2026
34d8ed6
chore(deps-dev): bump phpstan/phpstan from 2.3.6 to 2.3.7 (#1420)
dependabot[bot] Nov 26, 2026
9c38d23
chore(deps): bump codeinwp/themeisle-sdk from 3.3.73 to 3.3.74 (#1423)
dependabot[bot] Nov 27, 2026
b9dbb61
test: cover the key and title of a filtered action
vytisbulkevicius Nov 27, 2026
02e5897
fix: clear the chart image reference when image saving is off
vytisbulkevicius Nov 28, 2026
313eed7
Merge pull request #1424 from Codeinwp/bugfix/pro/652
vytisbulkevicius Dec 1, 2026
2983567
Merge pull request #1426 from Codeinwp/bugfix/pro/654
vytisbulkevicius Dec 1, 2026
56c9201
Merge remote-tracking branch 'origin/development' into bugfix/pro/653
vytisbulkevicius Dec 1, 2026
1ae1c4c
fix: a copied chart does not take the image of its source chart
vytisbulkevicius Dec 1, 2026
9708978
test: cover deleting the chart's own image and keeping another chart'…
vytisbulkevicius Dec 1, 2026
f3b8301
Merge pull request #1425 from Codeinwp/bugfix/pro/653
vytisbulkevicius Dec 1, 2026
672790f
fix: keep the saved JSON source credentials on the server
vytisbulkevicius Dec 1, 2026
bc1b558
fix: use saved JSON credentials for their exact endpoint and its orig…
vytisbulkevicius Dec 1, 2026
35d8e82
fix: show a saved chart image only for the chart it was saved for
vytisbulkevicius Dec 1, 2026
c140af8
test: give the chart of the credentials test stored rows
vytisbulkevicius Dec 1, 2026
73e94c5
fix: keep the chart image after WordPress renames its edited file
vytisbulkevicius Dec 2, 2026
f3d4be1
fix: use GET for a JSON request that brings no method with the saved …
vytisbulkevicius Dec 2, 2026
19b1284
fix: accept only the image name suffixes that WordPress adds
vytisbulkevicius Dec 2, 2026
2407951
test: describe every accepted suffix
vytisbulkevicius Dec 2, 2026
a4b27ef
fix: bind saved JSON credentials to their endpoint and keep them out …
vytisbulkevicius Dec 2, 2026
1cc2ae9
fix: keep saved JSON credentials out of the ability log and match the…
vytisbulkevicius Dec 2, 2026
0c66115
fix: save the endpoint of JSON credentials beside them
vytisbulkevicius Dec 2, 2026
37c1ded
test: cover the deletion of an edited chart image
vytisbulkevicius Dec 2, 2026
2ffa5cf
ci: install the plugins of the e2e tests after wp-env starts
vytisbulkevicius Dec 2, 2026
149cea3
Merge pull request #1432 from Codeinwp/bugfix/1431
vytisbulkevicius Dec 2, 2026
2fb7ef9
Merge remote-tracking branch 'origin/development' into HEAD
vytisbulkevicius Dec 2, 2026
f6ed9f6
Merge remote-tracking branch 'origin/development' into bugfix/pro/668
vytisbulkevicius Dec 2, 2026
03a2b67
Merge pull request #1428 from Codeinwp/bugfix/pro/666
vytisbulkevicius Dec 2, 2026
5751f11
Merge pull request #1429 from Codeinwp/bugfix/pro/668
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
3 changes: 3 additions & 0 deletions .github/workflows/test-e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ jobs:
run: composer install --no-dev
- name: Start wp-env
run: npx wp-env start
- name: Install the plugins the tests need
# The afterStart script of .wp-env.json installs them too, but they do not stay on the runner.
run: npx wp-env run tests-cli wp plugin install classic-editor elementor --activate
- name: Run the tests
run: |
npm run test:e2e:playwright
Expand Down
4 changes: 4 additions & 0 deletions classes/Visualizer/Gutenberg/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,10 @@ public function get_visualizer_data( $post ) {
if ( ! empty( $json_url ) ) {
$data['visualizer-json-schedule'] = $json_schedule;
$data['visualizer-json-url'] = $json_url;
// The saved credentials stay on the server.
if ( is_array( $json_headers ) ) {
unset( $json_headers['auth'], $json_headers['auth_url'] );
}
$data['visualizer-json-headers'] = $json_headers;
$data['visualizer-json-root'] = $json_root;

Expand Down
91 changes: 84 additions & 7 deletions classes/Visualizer/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -924,20 +924,68 @@ function ( $matches ) {
return $sanitize ? self::sanitize_chart_data( $altered ) : $altered;
}

/**
* Returns the saved credentials of a JSON chart for a request to the endpoint they were saved for.
*
* A user who does not know the credentials can then not send them to another address, or read them
* back from an address that returns its request headers. A save writes that endpoint beside the
* credentials as `auth_url`, so a later change of the chart endpoint does not move them. Headers of an
* older version have no `auth_url`; their credentials belong to the chart endpoint.
*
* @param int $chart_id The chart ID.
* @param string $url The JSON endpoint of the request.
* @return array{username: string, password: string}|string|null The credentials, or null when the chart has none for that endpoint.
*/
public static function get_saved_json_auth( $chart_id, $url ) {
$headers = get_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_HEADERS, true );
$saved_url = is_array( $headers ) && isset( $headers['auth_url'] ) ? $headers['auth_url'] : get_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_URL, true );
$saved_url = is_string( $saved_url ) ? trim( $saved_url ) : '';
if ( ! is_array( $headers ) || empty( $headers['auth'] ) || ! self::is_json_endpoint( $saved_url, $url ) ) {
return null;
}
$auth = $headers['auth'];
if ( is_string( $auth ) || ( is_array( $auth ) && isset( $auth['username'], $auth['password'] ) && is_string( $auth['username'] ) && is_string( $auth['password'] ) ) ) {
return $auth;
}
return null;
}

/**
* Tells whether a request address is the saved JSON endpoint of a chart.
*
* The chart editor prints the saved endpoint with esc_url(), which encodes some characters, and the browser
* decodes the HTML entities. The form then sends that address, so it counts as the saved endpoint too.
*
* @param string $saved_url The saved JSON endpoint.
* @param string $url The address of the request.
* @return bool
*/
private static function is_json_endpoint( $saved_url, $url ) {
$url = trim( $url );
return '' !== $saved_url && ( $url === $saved_url || $url === html_entity_decode( esc_url( $saved_url ), ENT_QUOTES ) );
}

/**
* 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.
* not the access of whoever authorized an earlier endpoint. Saved credentials belong to the endpoint
* they were saved for, so a new endpoint removes them.
*
* @param int $chart_id The chart ID.
* @param string $url The JSON endpoint.
* @param string $url The JSON endpoint, with slashes.
* @return void
*/
public static function update_json_url( $chart_id, $url ) {
if ( get_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_URL, true ) !== $url ) {
$saved_url = trim( (string) get_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_URL, true ) );
if ( $saved_url !== $url ) {
update_post_meta( $chart_id, Visualizer_Plugin::CF_WOOCOMMERCE_REPORT_USER, get_current_user_id() );
}
$headers = get_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_HEADERS, true );
if ( is_array( $headers ) && isset( $headers['auth'] ) && ! self::is_json_endpoint( $saved_url, wp_unslash( $url ) ) ) {
unset( $headers['auth'], $headers['auth_url'] );
update_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_HEADERS, wp_slash( $headers ) );
}
update_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_URL, $url );
}

Expand All @@ -962,16 +1010,45 @@ static function ( $value ) {
}

/**
* Get chart image.
* Returns the name of the file that holds the saved image of a chart.
*
* @param object $chart Chart data.
* @param int $chart_id The chart ID.
* @return string
*/
protected static function chart_image_filename( $chart_id ) {
return 'visualization-' . $chart_id . '.png';
}

/**
* Returns the ID of the image that was saved for a chart.
*
* A chart copied by an older version can point to the image of its source chart, also after that image is deleted.
* The file of an image keeps the name it was saved with. WordPress adds a suffix for a name that is in use, for an
* image that a user edits, and for a large image that it scales or rotates.
*
* @param int $chart_id The chart ID.
* @return int The attachment ID, or 0 when the chart has no image of its own.
*/
public static function get_chart_image_id( $chart_id ) {
$attachment_id = (int) get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_IMAGE, true );
$saved_name = preg_quote( pathinfo( self::chart_image_filename( $chart_id ), PATHINFO_FILENAME ), '/' );
if ( $attachment_id && preg_match( '/^' . $saved_name . '(-(\d+|e\d+|scaled|rotated))*\.png\z/', basename( (string) get_attached_file( $attachment_id ) ) ) ) {
return $attachment_id;
}
return 0;
}

/**
* Get chart image.
*
* @param WP_Post|null $chart The chart post.
* @return array{csv: string} The `csv` value is the image tag of the saved chart image, or an empty string.
*/
public function _getImage( $chart = null ) {
$image = '';
if ( $chart ) {
$chart_image = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_IMAGE, true );
if ( ! empty( $chart_image ) ) {
$chart_image = self::get_chart_image_id( $chart->ID );
if ( $chart_image ) {
$image = wp_get_attachment_image( $chart_image, 'full' );
}
}
Expand Down
29 changes: 13 additions & 16 deletions classes/Visualizer/Module/Abilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -968,22 +968,14 @@ private function setJsonSource( $chart, $input, $hours, $dry_run ) {
$params['paging'] = sanitize_text_field( $input['json_paging'] );
}

// Credentials saved from the chart editor are reused for the same host only; they are never accepted or returned here.
$saved_headers = get_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_HEADERS, true );
$saved_url = (string) get_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_URL, true );
$auth = array();
if ( is_array( $saved_headers ) && ! empty( $saved_headers['auth'] ) && '' !== $saved_url && wp_parse_url( $saved_url, PHP_URL_HOST ) === wp_parse_url( $url, PHP_URL_HOST ) && wp_parse_url( $saved_url, PHP_URL_SCHEME ) === wp_parse_url( $url, PHP_URL_SCHEME ) ) {
$auth = $saved_headers['auth'];
if ( is_array( $auth ) && isset( $auth['username'], $auth['password'] ) ) {
$params['username'] = $auth['username'];
$params['password'] = $auth['password'];
} elseif ( is_string( $auth ) ) {
$params['auth'] = $auth;
}
}
// Credentials saved from the chart editor are reused for the same endpoint only; they are never accepted or returned here.
// Each source gets them through setAuth(), because a source prints its parameters to the debug log.
$auth = self::get_saved_json_auth( $chart->ID, $url );

// First pass: discover the keys available under the chosen root.
$probe = $this->fetchSource( new Visualizer_Source_Json( $params ) );
$probe_source = new Visualizer_Source_Json( $params );
$probe_source->setAuth( $auth );
$probe = $this->fetchSource( $probe_source );
if ( is_wp_error( $probe ) ) {
return $probe;
}
Expand All @@ -997,7 +989,9 @@ private function setJsonSource( $chart, $input, $hours, $dry_run ) {

if ( ! $mapping ) {
if ( $dry_run ) {
$roots = ( new Visualizer_Source_Json( $params ) )->fetchRoots();
$roots_source = new Visualizer_Source_Json( $params );
$roots_source->setAuth( $auth );
$roots = $roots_source->fetchRoots();
return array(
'id' => $chart->ID,
'source' => 'json',
Expand Down Expand Up @@ -1025,6 +1019,7 @@ private function setJsonSource( $chart, $input, $hours, $dry_run ) {
}

$source = new Visualizer_Source_Json( $params );
$source->setAuth( $auth );
try {
$source->fetchFromEditableTable();
} catch ( Exception $e ) {
Expand Down Expand Up @@ -1053,7 +1048,9 @@ private function setJsonSource( $chart, $input, $hours, $dry_run ) {

$headers = array( 'method' => $params['method'] );
if ( $auth ) {
$headers['auth'] = $auth;
// add_post_meta() removes slashes, and the saved value is already without them.
$headers['auth'] = wp_slash( $auth );
$headers['auth_url'] = wp_slash( $params['url'] );
}
add_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_HEADERS, $headers );
if ( ! empty( $params['paging'] ) ) {
Expand Down
53 changes: 44 additions & 9 deletions classes/Visualizer/Module/Chart.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,10 @@ public function getJsonRoots() {
wp_send_json_error( array( 'msg' => esc_html__( 'You do not have permission to perform this action.', 'visualizer' ) ) );
}

$params = wp_parse_args( $_POST['params'] );
$params = wp_parse_args( $_POST['params'] );

$source = new Visualizer_Source_Json( $params );
$source->setAuth( self::saved_json_auth_for_editor( $params, isset( $params['chart'] ) ? absint( $params['chart'] ) : 0 ) );

$roots = $source->fetchRoots();
if ( empty( $roots ) ) {
Expand Down Expand Up @@ -213,6 +214,7 @@ public function getJsonData() {
}

$source = new Visualizer_Source_Json( $params );
$source->setAuth( self::saved_json_auth_for_editor( $params, absint( $chart_id ) ) );
$source->fetch();
$data = $source->getRawData();

Expand All @@ -224,6 +226,24 @@ public function getJsonData() {
wp_send_json_success( array( 'table' => $data, 'root' => $params['root'], 'url' => $params['url'], 'paging' => $source->getPaginationElements() ) );
}

/**
* Returns the saved credentials that a JSON request from the chart editor uses.
*
* The chart editor does not show saved credentials, so its forms send empty credential fields for them.
* A request that brings credentials, or asks to remove the saved ones, does not use them.
*
* @param array<string, mixed> $params JSON source parameters from the chart editor, with slashes.
* @param int $chart_id The chart ID.
* @return array{username: string, password: string}|string|null The credentials, or null when the request uses none.
*/
private static function saved_json_auth_for_editor( $params, $chart_id ) {
$has_auth_input = ! empty( $params['clear_auth'] ) || ! empty( $params['auth'] ) || ! empty( $params['username'] ) || ! empty( $params['password'] );
if ( $has_auth_input || empty( $params['url'] ) || ! is_string( $params['url'] ) || ! self::can_edit_chart( $chart_id ) ) {
return null;
}
return self::get_saved_json_auth( $chart_id, wp_unslash( $params['url'] ) );
}

/**
* Updates the database with the correct post parameters for JSON-endpoint charts.
*
Expand All @@ -243,7 +263,11 @@ public function setJsonData() {

$chart = get_post( $chart_id );

// Read the saved credentials before the new endpoint replaces the one they belong to.
$saved_auth = self::saved_json_auth_for_editor( $params, $chart_id );

$source = new Visualizer_Source_Json( $params );
$source->setAuth( $saved_auth );
update_post_meta( $chart->ID, Visualizer_Plugin::CF_EDITABLE_TABLE, true );
$source->fetchFromEditableTable();

Expand All @@ -262,6 +286,14 @@ public function setJsonData() {
$headers['auth'] = $params['auth'];
} elseif ( ! empty( $params['username'] ) && ! empty( $params['password'] ) ) {
$headers['auth'] = array( 'username' => $params['username'], 'password' => $params['password'] );
} elseif ( null !== $saved_auth ) {
// add_post_meta() removes slashes, and the saved value is already without them.
$headers['auth'] = wp_slash( $saved_auth );
}

if ( isset( $headers['auth'] ) ) {
// The credentials belong to this endpoint. See get_saved_json_auth().
$headers['auth_url'] = $params['url'];
}

add_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_HEADERS, $headers );
Expand Down Expand Up @@ -631,8 +663,8 @@ public function renderChartPages() {
$post_meta = get_post_meta( $parent_chart_id );
$chart_id = $new_chart_id;
foreach ( $post_meta as $key => $value ) {
// A copy refreshes reports with its own author's access.
if ( strpos( $key, 'visualizer-' ) !== false && Visualizer_Plugin::CF_WOOCOMMERCE_REPORT_USER !== $key ) {
// A copy refreshes reports with its own author's access, and it has no image until it is saved.
if ( strpos( $key, 'visualizer-' ) !== false && ! in_array( $key, array( Visualizer_Plugin::CF_WOOCOMMERCE_REPORT_USER, Visualizer_Plugin::CF_CHART_IMAGE ), true ) ) {
add_post_meta( $new_chart_id, $key, self::maybe_decode_content( $value[0] ) );
}
}
Expand All @@ -651,7 +683,7 @@ public function renderChartPages() {
$_POST['save_chart_image'] = isset( $_POST['save_chart_image'] ) && 'yes' === $_POST['save_chart_image'] ? true : false;
$_POST['lazy_load_chart'] = isset( $_POST['lazy_load_chart'] ) && 'yes' === $_POST['lazy_load_chart'] ? true : false;

if ( isset( $_POST['chart-img'] ) && ! empty( $_POST['chart-img'] ) ) {
if ( ! empty( $_POST['chart-img'] ) && isset( $_GET['nonce'] ) && wp_verify_nonce( $_GET['nonce'] ) ) {
$attachment_id = $this->save_chart_image( $_POST['chart-img'], $chart_id, $_POST['save_chart_image'] );
if ( $attachment_id ) {
update_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_IMAGE, $attachment_id );
Expand Down Expand Up @@ -1476,8 +1508,8 @@ public function cloneChart() {
} else {
$post_meta = get_post_meta( $chart_id );
foreach ( $post_meta as $key => $value ) {
// A copy refreshes reports with its own author's access.
if ( strpos( $key, 'visualizer-' ) !== false && Visualizer_Plugin::CF_WOOCOMMERCE_REPORT_USER !== $key ) {
// A copy refreshes reports with its own author's access, and it has no image until it is saved.
if ( strpos( $key, 'visualizer-' ) !== false && ! in_array( $key, array( Visualizer_Plugin::CF_WOOCOMMERCE_REPORT_USER, Visualizer_Plugin::CF_CHART_IMAGE ), true ) ) {
add_post_meta( $new_chart_id, $key, self::maybe_decode_content( $value[0] ) );
}
}
Expand Down Expand Up @@ -1752,13 +1784,17 @@ public function saveFilter() {
* @return int Attachment ID, or 0 when no attachment was saved.
*/
public function save_chart_image( $base64_img, $chart_id, $save_attachment = true ) {
// Delete old chart image.
$old_attachment_id = get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_IMAGE, true );
$filename = self::chart_image_filename( $chart_id );

// Delete the old chart image, but only the image of this chart.
$old_attachment_id = self::get_chart_image_id( $chart_id );
if ( $old_attachment_id ) {
wp_delete_attachment( $old_attachment_id, true );
}

if ( ! $save_attachment ) {
// 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 All @@ -1773,7 +1809,6 @@ public function save_chart_image( $base64_img, $chart_id, $save_attachment = tru
if ( false === $decoded || 0 !== strncmp( $decoded, "\x89PNG\r\n\x1a\n", 8 ) ) {
return 0;
}
$filename = 'visualization-' . $chart_id . '.png';
$file_type = 'image/png';
$hashed_filename = $filename;

Expand Down
9 changes: 6 additions & 3 deletions classes/Visualizer/Module/Frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,8 @@ public function renderChart( $atts ) {
}

if ( 'yes' === $atts['use_image'] ) {
$chart_image = $chart_data['chart_image'];
// Read the image on each render: the cached chart data can outlive the image.
$chart_image = self::get_chart_image_id( $chart->ID );
if ( $chart_image ) {
return '<div id="' . $id . '"' . $this->getHtmlAttributes( $attributes ) . '>' . wp_get_attachment_image( $chart_image, 'full' ) . '</div>';
}
Expand Down Expand Up @@ -475,9 +476,12 @@ public function renderChart( $atts ) {
$key = $array[0];
$mime = end( $array );
}
if ( ! isset( $actions[ $key ] ) ) {
continue;
}
$label = $actions[ $key ]['label'];
$title = $actions[ $key ]['title'];
$actions_div .= sprintf( '<a href="#" class="visualizer-action visualizer-action-%s" data-visualizer-type="%s" data-visualizer-chart-id="%s" data-visualizer-container-id="%s" data-visualizer-mime="%s" title="%s"', $key, $key, $atts['id'], $id, $mime, $title );
$actions_div .= sprintf( '<a href="#" class="visualizer-action visualizer-action-%s" data-visualizer-type="%s" data-visualizer-chart-id="%s" data-visualizer-container-id="%s" data-visualizer-mime="%s" title="%s"', esc_attr( $key ), esc_attr( $key ), esc_attr( $atts['id'] ), esc_attr( $id ), esc_attr( $mime ), esc_attr( $title ) );

if ( 'copy' === $key ) {
$copy = $this->_getDataAs( $atts['id'], 'csv' );
Expand Down Expand Up @@ -763,7 +767,6 @@ private function getChartData( $cache_key = '', $chart_id = 0 ) {
'type' => get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true ),
'settings' => $settings,
'series' => $series,
'chart_image' => get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_IMAGE, true ),
'is_woocommerce_report' => $is_woocommerce_report,
);

Expand Down
Loading
Loading