diff --git a/classes/Visualizer/Gutenberg/Block.php b/classes/Visualizer/Gutenberg/Block.php index 5a152351d..d8acb4904 100644 --- a/classes/Visualizer/Gutenberg/Block.php +++ b/classes/Visualizer/Gutenberg/Block.php @@ -327,7 +327,14 @@ public function get_visualizer_data( $post ) { $data['visualizer-settings'] = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SETTINGS, $data['visualizer-settings'], $post_id, $data['visualizer-chart-type'] ); // handle data filter hooks - $data['visualizer-data'] = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA, Visualizer_Module::decode_content( html_entity_decode( get_post_field( 'post_content', $post_id, 'raw' ) ) ), $post_id, $data['visualizer-chart-type'] ); + $data['visualizer-data'] = Visualizer_Module::sanitize_chart_data( + apply_filters( + Visualizer_Plugin::FILTER_GET_CHART_DATA, + Visualizer_Module::decode_content( html_entity_decode( get_post_field( 'post_content', $post_id, 'raw' ) ) ), + $post_id, + $data['visualizer-chart-type'] + ) + ); // we are going to format only for tabular charts, because we are not sure of the effect on others. // this is to solve the case where boolean data shows up as all-ticks on gutenberg. diff --git a/classes/Visualizer/Module.php b/classes/Visualizer/Module.php index ff9fa2c33..d00b2517a 100644 --- a/classes/Visualizer/Module.php +++ b/classes/Visualizer/Module.php @@ -190,7 +190,7 @@ public function _getDataAs( $chart_id, $type ) { $settings = get_post_meta( $chart_id, Visualizer_Plugin::CF_SETTINGS, true ); $rows = array(); $series = get_post_meta( $chart_id, Visualizer_Plugin::CF_SERIES, true ); - $data = self::get_chart_data( $chart, $type, false ); + $data = self::get_chart_data( $chart, $type, false, 'print' === $type ); if ( ! empty( $series ) ) { $row = array(); foreach ( $series as $array ) { @@ -880,8 +880,14 @@ public static function maybe_decode_content( $value ) { /** * Gets the chart content after common manipulations. + * + * @param WP_Post|object $chart The chart post. + * @param string $type The chart type passed to the data filter. + * @param bool $run_filter Whether to run the chart data filter. + * @param bool $sanitize Whether to prepare the cells for HTML-capable renderers; exports and editors need the stored values. + * @return mixed The chart data. */ - public static function get_chart_data( $chart, $type, $run_filter = true ) { + public static function get_chart_data( $chart, $type, $run_filter = true, $sanitize = true ) { // change HTML entities $post_content = html_entity_decode( htmlentities( $chart->post_content ) ); $post_content = preg_replace_callback( @@ -912,9 +918,47 @@ function ( $matches ) { $altered = $data; } if ( $run_filter ) { - return apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA, $altered, $chart->ID, $type ); + $altered = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA, $altered, $chart->ID, $type ); + } + + return $sanitize ? self::sanitize_chart_data( $altered ) : $altered; + } + + /** + * Saves a chart's JSON endpoint, recording the user who changes it as the WooCommerce report user. + * + * A scheduled refresh of the endpoint then uses this user's report access, + * not the access of whoever authorized an earlier endpoint. + * + * @param int $chart_id The chart ID. + * @param string $url The JSON endpoint. + * @return void + */ + public static function update_json_url( $chart_id, $url ) { + if ( get_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_URL, true ) !== $url ) { + update_post_meta( $chart_id, Visualizer_Plugin::CF_WOOCOMMERCE_REPORT_USER, get_current_user_id() ); } - return $altered; + update_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_URL, $url ); + } + + /** + * Sanitize chart data before passing it to HTML-capable renderers. + * + * @param mixed $data Chart data. + * @return mixed Sanitized chart data. + */ + public static function sanitize_chart_data( $data ) { + // Preserve normal text and supported formatting while removing active + // markup from imports, scheduled remote sources, and filtered data. + return map_deep( + $data, + static function ( $value ) { + if ( ! is_string( $value ) || ! preg_match( '/<\s*\/?\s*[a-zA-Z!]/', $value ) ) { + return $value; + } + return wp_kses_post( $value ); + } + ); } /** diff --git a/classes/Visualizer/Module/AIBuilder.php b/classes/Visualizer/Module/AIBuilder.php index c97be184f..931e78146 100644 --- a/classes/Visualizer/Module/AIBuilder.php +++ b/classes/Visualizer/Module/AIBuilder.php @@ -195,7 +195,7 @@ public function fetchChart(): void { $this->_verify_chart_access( $chart_id ); $series = get_post_meta( $chart_id, Visualizer_Plugin::CF_SERIES, true ); - $data = Visualizer_Module::get_chart_data( $chart, '', false ); + $data = Visualizer_Module::get_chart_data( $chart, '', false, false ); $code = get_post_meta( $chart_id, self::CF_D3_CODE, true ); $settings = get_post_meta( $chart_id, Visualizer_Plugin::CF_SETTINGS, true ); $title = ( is_array( $settings ) && ! empty( $settings['backend-title'] ) ) @@ -360,7 +360,7 @@ public function uploadData(): void { $source = new Visualizer_Source_Json( $params ); // Store config for sync - update_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_URL, $params['url'] ); + Visualizer_Module::update_json_url( $chart_id, $params['url'] ); update_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_ROOT, $params['root'] ); if ( ! empty( $_POST['json_schedule'] ) ) { update_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_SCHEDULE, intval( $_POST['json_schedule'] ) ); diff --git a/classes/Visualizer/Module/Abilities.php b/classes/Visualizer/Module/Abilities.php index 95c2cb430..87aefe175 100644 --- a/classes/Visualizer/Module/Abilities.php +++ b/classes/Visualizer/Module/Abilities.php @@ -598,7 +598,7 @@ public function executeGetChart( $input = array() ) { $type = (string) get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true ); $series = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SERIES, get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true ), $chart->ID, $type ); - $data = self::get_chart_data( $chart, $type ); + $data = self::get_chart_data( $chart, $type, true, false ); $settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true ); $settings = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SETTINGS, $settings, $chart->ID, $type ); if ( ! is_array( $settings ) ) { @@ -1048,7 +1048,7 @@ private function setJsonSource( $chart, $input, $hours, $dry_run ) { $this->clearSourceMeta( $chart->ID, true ); update_post_meta( $chart->ID, Visualizer_Plugin::CF_EDITABLE_TABLE, true ); $this->persistSource( $chart->ID, $source ); - update_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_URL, $params['url'] ); + self::update_json_url( $chart->ID, $params['url'] ); update_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_ROOT, $params['root'] ); $headers = array( 'method' => $params['method'] ); diff --git a/classes/Visualizer/Module/Admin.php b/classes/Visualizer/Module/Admin.php index e7ef36e5f..d255eb1ca 100644 --- a/classes/Visualizer/Module/Admin.php +++ b/classes/Visualizer/Module/Admin.php @@ -1119,6 +1119,8 @@ public function enqueueSettingsScripts( string $hook_suffix ): void { } wp_enqueue_style( 'wp-color-picker' ); wp_enqueue_script( 'wp-color-picker' ); + + do_action( 'themeisle_internal_page', VISUALIZER_DIRNAME, 'settings' ); } /** @@ -1409,7 +1411,7 @@ public function getPluginMetaLinks( $plugin_meta, $plugin_file ) { $is_black_friday = apply_filters( 'themeisle_sdk_is_black_friday_sale', false ); // knowledge base link $plugin_meta[] = sprintf( - '%s', + '%s', esc_html__( 'Docs', 'visualizer' ) ); if ( ! $is_black_friday ) { @@ -1609,7 +1611,7 @@ public function add_black_friday_data( $configs ) { $pro_product_slug = defined( 'VISUALIZER_PRO_BASEFILE' ) ? basename( dirname( VISUALIZER_PRO_BASEFILE ) ) : ''; $is_pro = 'valid' === $status; - $is_expired = 'expired' === $status || 'active-expired' === $status; + $is_expired = in_array( $status, array( 'expired', 'active_expired', 'active-expired' ), true ); if ( $is_pro ) { // translators: %s is the discount percentage. diff --git a/classes/Visualizer/Module/Chart.php b/classes/Visualizer/Module/Chart.php index bbe126e0a..34d615ab0 100644 --- a/classes/Visualizer/Module/Chart.php +++ b/classes/Visualizer/Module/Chart.php @@ -138,14 +138,14 @@ public function setJsonSchedule() { ); if ( Visualizer_Module::is_pro() ) { - $is_woocommerce_report = filter_input( - INPUT_POST, - 'is_woocommerce_report', - FILTER_VALIDATE_BOOLEAN - ); + $is_woocommerce_report = isset( $_POST['is_woocommerce_report'] ) ? filter_var( wp_unslash( $_POST['is_woocommerce_report'] ), FILTER_VALIDATE_BOOLEAN ) : false; if ( $is_woocommerce_report ) { + if ( ! current_user_can( 'view_woocommerce_reports' ) ) { + wp_send_json_error( array( 'msg' => esc_html__( 'You do not have permission to access WooCommerce reports.', 'visualizer' ) ), 403 ); + } update_post_meta( $chart_id, Visualizer_Plugin::CF_IS_WOOCOMMERCE_SOURCE, true ); + update_post_meta( $chart_id, Visualizer_Plugin::CF_WOOCOMMERCE_REPORT_USER, get_current_user_id() ); } else { delete_post_meta( $chart_id, Visualizer_Plugin::CF_IS_WOOCOMMERCE_SOURCE ); } @@ -220,7 +220,7 @@ public function getJsonData() { wp_send_json_error( array( 'msg' => esc_html__( 'Unable to fetch data from the endpoint. Please try again.', 'visualizer' ) ) ); } - $data = Visualizer_Render_Layout::show( 'editor-table', $data, $chart_id, 'viz-json-table', false, false ); + $data = Visualizer_Render_Layout::show( 'editor-table', self::sanitize_chart_data( $data ), $chart_id, 'viz-json-table', false, false ); wp_send_json_success( array( 'table' => $data, 'root' => $params['root'], 'url' => $params['url'], 'paging' => $source->getPaginationElements() ) ); } @@ -253,7 +253,7 @@ public function setJsonData() { update_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, $source->getSeries() ); update_post_meta( $chart->ID, Visualizer_Plugin::CF_SOURCE, $source->getSourceName() ); update_post_meta( $chart->ID, Visualizer_Plugin::CF_DEFAULT_DATA, 0 ); - update_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_URL, $params['url'] ); + self::update_json_url( $chart->ID, $params['url'] ); update_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_ROOT, $params['root'] ); delete_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_HEADERS ); @@ -304,7 +304,7 @@ public function setJsonData() { $render = new Visualizer_Render_Page_Update(); $render->id = $chart->ID; - $render->data = json_encode( $source->getRawData( get_post_meta( $chart_id, Visualizer_Plugin::CF_EDITABLE_TABLE, true ) ) ); + $render->data = json_encode( self::sanitize_chart_data( $source->getRawData( get_post_meta( $chart_id, Visualizer_Plugin::CF_EDITABLE_TABLE, true ) ) ) ); $render->series = json_encode( $source->getSeries() ); $render->render(); @@ -631,7 +631,8 @@ public function renderChartPages() { $post_meta = get_post_meta( $parent_chart_id ); $chart_id = $new_chart_id; foreach ( $post_meta as $key => $value ) { - if ( strpos( $key, 'visualizer-' ) !== false ) { + // A copy refreshes reports with its own author's access. + if ( strpos( $key, 'visualizer-' ) !== false && Visualizer_Plugin::CF_WOOCOMMERCE_REPORT_USER !== $key ) { add_post_meta( $new_chart_id, $key, self::maybe_decode_content( $value[0] ) ); } } @@ -1420,7 +1421,7 @@ public function uploadData() { } $render->id = $chart->ID; - $render->data = json_encode( $source->getRawData( get_post_meta( $chart->ID, Visualizer_Plugin::CF_EDITABLE_TABLE, true ) ) ); + $render->data = json_encode( self::sanitize_chart_data( $source->getRawData( get_post_meta( $chart->ID, Visualizer_Plugin::CF_EDITABLE_TABLE, true ) ) ) ); $render->series = json_encode( $source->getSeries() ); $render->settings = json_encode( $settings ); } else { @@ -1475,7 +1476,8 @@ public function cloneChart() { } else { $post_meta = get_post_meta( $chart_id ); foreach ( $post_meta as $key => $value ) { - if ( strpos( $key, 'visualizer-' ) !== false ) { + // A copy refreshes reports with its own author's access. + if ( strpos( $key, 'visualizer-' ) !== false && Visualizer_Plugin::CF_WOOCOMMERCE_REPORT_USER !== $key ) { add_post_meta( $new_chart_id, $key, self::maybe_decode_content( $value[0] ) ); } } @@ -1682,7 +1684,7 @@ public function saveQuery() { 'post_content' => $source->getData( get_post_meta( $chart_id, Visualizer_Plugin::CF_EDITABLE_TABLE, true ) ), ) ); - $render->data = json_encode( $source->getRawData( get_post_meta( $chart_id, Visualizer_Plugin::CF_EDITABLE_TABLE, true ) ) ); + $render->data = json_encode( self::sanitize_chart_data( $source->getRawData( get_post_meta( $chart_id, Visualizer_Plugin::CF_EDITABLE_TABLE, true ) ) ) ); $render->series = json_encode( $source->getSeries() ); $render->id = $chart_id; } else { diff --git a/classes/Visualizer/Module/Setup.php b/classes/Visualizer/Module/Setup.php index 00a3fbcae..3f530b358 100644 --- a/classes/Visualizer/Module/Setup.php +++ b/classes/Visualizer/Module/Setup.php @@ -384,6 +384,8 @@ public function refresh_db_for_chart( $chart, $chart_id, $force = false ) { return $chart; } + // Editable-table charts fetch again here, so check the error after this read. + $content = $source->getData( get_post_meta( $chart_id, Visualizer_Plugin::CF_EDITABLE_TABLE, true ) ); $error = $source->get_error(); if ( empty( $error ) ) { $this->disableRevisionsTemporarily(); @@ -406,7 +408,7 @@ public function refresh_db_for_chart( $chart, $chart_id, $force = false ) { wp_update_post( array( 'ID' => $chart_id, - 'post_content' => $source->getData( get_post_meta( $chart_id, Visualizer_Plugin::CF_EDITABLE_TABLE, true ) ), + 'post_content' => $content, ) ); diff --git a/classes/Visualizer/Plugin.php b/classes/Visualizer/Plugin.php index 162672c0f..8d632e2bf 100644 --- a/classes/Visualizer/Plugin.php +++ b/classes/Visualizer/Plugin.php @@ -106,6 +106,7 @@ class Visualizer_Plugin { const CF_CHART_CACHE = 'visualizer-chart-cache'; const CF_JSON_WOOCOMMERCE_SOURCE = 'visualizer-woocommerce-source'; const CF_IS_WOOCOMMERCE_SOURCE = 'visualizer-is-woocommerce-source'; + const CF_WOOCOMMERCE_REPORT_USER = 'visualizer-woocommerce-report-user'; /** * Name of the option for WordPress DB. diff --git a/classes/Visualizer/Render/Layout.php b/classes/Visualizer/Render/Layout.php index dedce5036..ed9b5b211 100644 --- a/classes/Visualizer/Render/Layout.php +++ b/classes/Visualizer/Render/Layout.php @@ -95,7 +95,7 @@ public static function _renderDbQuery( $args ) { echo sprintf( // translators: %1$s - HTML link tag, %2$s - HTML closing link tag. __( 'For examples of queries and links to resources that you can use with this feature, please click %1$shere%2$s', 'visualizer' ), - '', + '', '' ); ?> @@ -208,7 +208,7 @@ public static function _renderJsonScreen( $args ) { echo sprintf( // translators: %1$s - HTML link tag, %2$s - HTML closing link tag. __( 'If you want to add authentication or headers to the endpoint or change the request in any way, please refer to our document %1$shere%2$s.', 'visualizer' ), - '', + '', '' ); ?> @@ -411,7 +411,7 @@ public static function _renderTextEditor( $args ) { } ?>
- +
'; } else { - echo '' . $header . ''; + echo '' . esc_html( $header ) . ''; } } ?> @@ -479,8 +479,8 @@ public static function _renderEditorTable( $args ) { if ( $editable_data ) { echo ''; - echo ''; + echo ' - + diff --git a/classes/Visualizer/Render/Sidebar.php b/classes/Visualizer/Render/Sidebar.php index 91e28a38e..61cfd2985 100644 --- a/classes/Visualizer/Render/Sidebar.php +++ b/classes/Visualizer/Render/Sidebar.php @@ -144,7 +144,7 @@ protected function _renderManualConfigDescription() { // translators: %1$s - HTML link tag, %2$s - HTML closing link tag, %3$s - HTML link tag, %4$s - HTML closing link tag. __( 'Configure the graph by providing configuration variables right from the %1$sGoogle Visualization API%2$s. You can refer to %3$sCommon Snippets%4$s for examples.', 'visualizer' ), '', '', - '', + '', '' ) . '' ); diff --git a/classes/Visualizer/Render/Sidebar/ChartJS.php b/classes/Visualizer/Render/Sidebar/ChartJS.php index 7d4a433a1..a7021f718 100644 --- a/classes/Visualizer/Render/Sidebar/ChartJS.php +++ b/classes/Visualizer/Render/Sidebar/ChartJS.php @@ -393,7 +393,7 @@ protected function _renderManualConfigDescription() { __( 'Configure the graph by providing configuration variables right from the %1$sChartJS API%2$s. You can refer to %3$sCommon Snippets%4$s for examples.', 'visualizer' ), '', '', - '', + '', '' ) . '' ); diff --git a/classes/Visualizer/Render/Sidebar/Google.php b/classes/Visualizer/Render/Sidebar/Google.php index 2684e297c..04a7d3329 100644 --- a/classes/Visualizer/Render/Sidebar/Google.php +++ b/classes/Visualizer/Render/Sidebar/Google.php @@ -155,7 +155,7 @@ protected function _renderRoleField( $index ) { esc_html__( 'Determines whether the series has to be used for a special role as mentioned in %1$shere%2$s. You can view a few examples %3$shere%4$s.', 'visualizer' ), '', '', - '', + '', '' ) ); diff --git a/classes/Visualizer/Source/Json.php b/classes/Visualizer/Source/Json.php index 0da21be8f..43147ae1d 100644 --- a/classes/Visualizer/Source/Json.php +++ b/classes/Visualizer/Source/Json.php @@ -207,6 +207,7 @@ public function getPaginationElements() { * @since ? * * @access public + * @return bool False when a WooCommerce report request failed, true otherwise. */ public function fetch() { $url = $this->_url; @@ -285,7 +286,7 @@ public function fetch() { do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, 'JSON endpoint data parsed.', 'debug', __FILE__, __LINE__ ); - return true; + return empty( $this->_error ); } /** @@ -399,6 +400,10 @@ private function getJSON( $url = null ) { if ( is_wp_error( $response ) || ! in_array( intval( $response['response']['code'] ), array( 200, 201 ), true ) ) { $error_code = is_wp_error( $response ) ? $response->get_error_code() : (string) wp_remote_retrieve_response_code( $response ); do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Error while fetching JSON endpoint: %s', $error_code ), 'error', __FILE__, __LINE__ ); + if ( $this->is_woocommerce_request( $url ) ) { + // Report access can be missing for this request, so keep the stored chart data. + $this->_error = esc_html__( 'Unable to fetch the WooCommerce report. The user who saved this chart needs access to WooCommerce reports.', 'visualizer' ); + } return null; } @@ -457,8 +462,13 @@ function ( $headers ) { } } - // Check if this is a WooCommerce endpoint request and add verification token. + // Check if this is an authorized WooCommerce endpoint request and add a verification token. + $can_fetch_woocommerce_reports = false; if ( $this->is_woocommerce_request( $url ) ) { + $can_fetch_woocommerce_reports = current_user_can( 'view_woocommerce_reports' ); + $can_fetch_woocommerce_reports = apply_filters( 'visualizer_can_fetch_woocommerce_reports', $can_fetch_woocommerce_reports, $url ); + } + if ( ! empty( $can_fetch_woocommerce_reports ) ) { // Generate a unique token for this specific request. $token = wp_generate_password( 32, false ); set_transient( 'visualizer_wc_token_' . $token, time(), 60 ); diff --git a/classes/Visualizer/Source/Query.php b/classes/Visualizer/Source/Query.php index b08798c8e..b72a142a8 100644 --- a/classes/Visualizer/Source/Query.php +++ b/classes/Visualizer/Source/Query.php @@ -256,7 +256,7 @@ private function get_col_type( $col_num ) { * @return string */ private function html( $headers, $results ) { - return Visualizer_Render_Layout::show( 'db-wizard-results', $headers, $results ); + return Visualizer_Render_Layout::show( 'db-wizard-results', Visualizer_Module::sanitize_chart_data( $headers ), Visualizer_Module::sanitize_chart_data( $results ) ); } /** diff --git a/composer.lock b/composer.lock index 47f7fb9d5..2d9c46c00 100644 --- a/composer.lock +++ b/composer.lock @@ -8,16 +8,16 @@ "packages": [ { "name": "codeinwp/themeisle-sdk", - "version": "3.3.67", + "version": "3.3.72", "source": { "type": "git", "url": "https://github.com/Codeinwp/themeisle-sdk.git", - "reference": "e1dda44811660a65c3da6994349c054a46686d69" + "reference": "66036fa0ef8168f7e5a3f7bf06dc88733e1d46db" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/Codeinwp/themeisle-sdk/zipball/e1dda44811660a65c3da6994349c054a46686d69", - "reference": "e1dda44811660a65c3da6994349c054a46686d69", + "url": "https://api.github.com/repos/Codeinwp/themeisle-sdk/zipball/66036fa0ef8168f7e5a3f7bf06dc88733e1d46db", + "reference": "66036fa0ef8168f7e5a3f7bf06dc88733e1d46db", "shasum": "" }, "require-dev": { @@ -43,9 +43,9 @@ ], "support": { "issues": "https://github.com/Codeinwp/themeisle-sdk/issues", - "source": "https://github.com/Codeinwp/themeisle-sdk/tree/v3.3.67" + "source": "https://github.com/Codeinwp/themeisle-sdk/tree/v3.3.72" }, - "time": "2026-10-13T12:16:34+00:00" + "time": "2026-11-06T13:01:52+00:00" }, { "name": "neitanod/forceutf8", @@ -1066,11 +1066,11 @@ }, { "name": "phpstan/phpstan", - "version": "2.3.1", + "version": "2.3.5", "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan/zipball/6155239b488e95d26d1ebd95c6b4bb294d7f09db", - "reference": "6155239b488e95d26d1ebd95c6b4bb294d7f09db", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/645a1bd6bead15fc1b8474137467961d0fa30a79", + "reference": "645a1bd6bead15fc1b8474137467961d0fa30a79", "shasum": "" }, "require": { @@ -1126,7 +1126,7 @@ "type": "github" } ], - "time": "2026-10-08T21:09:11+00:00" + "time": "2026-11-03T12:14:22+00:00" }, { "name": "phpunit/php-code-coverage", diff --git a/index.php b/index.php index b5f211080..82d53b338 100644 --- a/index.php +++ b/index.php @@ -94,11 +94,11 @@ function visualizer_launch() { define( 'VISUALIZER_ENABLE_BETA_FEATURES', '' ); // the link to pre-build queries. - define( 'VISUALIZER_DB_QUERY_DOC_URL', 'https://docs.themeisle.com/article/970-visualizer-sample-queries-to-generate-charts' ); - define( 'VISUALIZER_MAIN_DOC', 'https://docs.themeisle.com/category/657-visualizer' ); + define( 'VISUALIZER_DB_QUERY_DOC_URL', 'https://docs.themeisle.com/visualizer-charts-and-graphs/visualizer-sample-queries-to-generate-charts' ); + define( 'VISUALIZER_MAIN_DOC', 'https://docs.themeisle.com/visualizer-charts-and-graphs/' ); define( 'VISUALIZER_DOC_COLLECTION', 'https://docs.themeisle.com/visualizer-charts-and-graphs/?query=#' ); define( 'VISUALIZER_DEMO_URL', 'https://demo.themeisle.com/visualizer/#' ); - define( 'VISUALIZER_CODE_SNIPPETS_URL', 'https://docs.themeisle.com/category/726-visualizer' ); + define( 'VISUALIZER_CODE_SNIPPETS_URL', 'https://docs.themeisle.com/visualizer-charts-and-graphs/' ); define( 'VISUALIZER_SUBSCRIBE_API', 'https://api.themeisle.com/tracking/subscribe' ); define( 'VISUALIZER_AGENTS_URL', 'https://ai.themeisle.com' ); @@ -189,6 +189,7 @@ function () { ); } ); + add_filter( 'visualizer_support_hub_metadata', 'visualizer_support_hub_metadata' ); add_filter( 'visualizer_ai_connect_metadata', function () { @@ -274,6 +275,38 @@ function visualizer_action_scheduler_store_class( $class_name ) { return 'ActionScheduler_DBStore' === $class_name ? 'Visualizer_ActionScheduler_Store' : $class_name; } +/** + * Opts into the SDK Support Hub: one Help button on the plugin's own screens + * and the deactivation window. + * + * Visualizer Pro registers nothing here: it has no admin screens of its own + * and the SDK shows one Help button per screen, so the free plugin owns the + * hub for both and tags the message as pro from the Pro license status. + * + * The plugin screens need no `pages` entry: each one fires + * `themeisle_internal_page`, which the SDK already treats as a product screen. + * + * @return array{is_pro: bool, license_namespace: string, links: array} + */ +function visualizer_support_hub_metadata() { + return array( + 'is_pro' => Visualizer_Module::is_pro(), + 'license_namespace' => 'visualizer', + 'links' => array( + array( + 'icon' => 'book-alt', + 'text' => __( 'Docs', 'visualizer' ), + 'url' => VISUALIZER_MAIN_DOC, + ), + array( + 'icon' => 'groups', + 'text' => __( 'Forum', 'visualizer' ), + 'url' => 'https://wordpress.org/support/plugin/visualizer/', + ), + ), + ); +} + /** * Registers with the SDK * diff --git a/js/frame.js b/js/frame.js index 6fd62e269..9984a0b63 100644 --- a/js/frame.js +++ b/js/frame.js @@ -67,9 +67,11 @@ break; } - if($('span.viz-gvlink').length > 0) { - $('span.viz-gvlink').html($('span.viz-gvlink').html().replace('?', chart_type_in_api_link)); - } + // Only the Google API link has the "gallery/?" placeholder. Other links in the + // description (e.g. Common Snippets) carry UTM query strings and must stay as they are. + $('span.viz-gvlink a[href*="/chart/interactive/docs/gallery/?"]').attr('href', function (i, href) { + return href.replace('gallery/?', 'gallery/' + chart_type_in_api_link); + }); $('.type-radio').change(function () { $('.type-label-selected').removeClass('type-label-selected'); diff --git a/js/render-datatables.js b/js/render-datatables.js index 3a2106943..923156d03 100644 --- a/js/render-datatables.js +++ b/js/render-datatables.js @@ -108,6 +108,9 @@ var select = { info: false }; + var nestedSettings = { + select: select + }; // we cannot use { select } below because https://github.com/Codeinwp/visualizer/issues/357 $.extend( settings, { info: false } ); // jshint ignore:line @@ -135,7 +138,7 @@ } } - // remove this so that the eval does not fail. + // internal_title is the chart's admin label, not a DataTables option. delete chart.settings['internal_title']; var additional = []; @@ -163,15 +166,15 @@ // if the setting name has an '_' this means it is a sub-setting e.g. select_items means { select: { items: ... } }. var array = i.split('_'); - if(array.length === 2){ - i = eval( array[0] ); // jshint ignore:line - i[ array[1] ] = valoo; - additional[ array[0] ] = i; + if(array.length === 2 && Object.prototype.hasOwnProperty.call(nestedSettings, array[0])){ + var nestedSetting = nestedSettings[ array[0] ]; + nestedSetting[ array[1] ] = valoo; + additional[ array[0] ] = nestedSetting; if(array[0] === 'select' && array[1] === 'info' ){ // enable main info or the selection info will not show. $.extend( settings, {info: true} ); } - } else { + } else if(array.length === 1) { settings[i] = valoo; } } @@ -450,4 +453,4 @@ $.fn.dataTable.render.moment = function ( from, to, locale ) { })); -/* jshint ignore:end */ \ No newline at end of file +/* jshint ignore:end */ diff --git a/package-lock.json b/package-lock.json index fd175637e..176e26aa2 100755 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "visualizer", - "version": "3.11.15", + "version": "4.0.10", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "visualizer", - "version": "3.11.15", + "version": "4.0.10", "license": "GPL-2.0+", "dependencies": { "@chakra-ui/react": "^3.34.0", @@ -16,12 +16,11 @@ "@uiw/react-codemirror": "^4.23.6", "d3": "^7.9.0", "deep-filter": "^1.0.2", - "handsontable": "^13.0.0", "is-plain-object": "^2.0.4", - "merge": "^1.2.1", + "merge": "^2.1.1", "react-google-charts": "^3.0.8", "topojson-client": "^3.1.0", - "uuid": "^8.3.2" + "uuid": "^11.1.1" }, "devDependencies": { "@semantic-release/changelog": "^5.0.1", @@ -7633,6 +7632,7 @@ "resolved": "https://registry.npmjs.org/@types/pikaday/-/pikaday-1.7.4.tgz", "integrity": "sha512-0KsHVyw5pTG829nqG4IRu7m+BFQlFEBdbE/1i3S5182HeKUKv1uEW0gyEmkJVp5i4IV+9pyh23O83+KpRkSQbw==", "license": "MIT", + "peer": true, "dependencies": { "moment": ">=2.14.0" } @@ -12718,6 +12718,7 @@ "resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-8.1.1.tgz", "integrity": "sha512-QD46ppGintwPGuL1KqmwhR0O+N2cZUg8JG/VzwI2e28sM9TqHjQB10lI4QAaMHVbLzwVLLAwEglpKPViWX+5NQ==", "license": "MIT", + "peer": true, "engines": { "node": "*" } @@ -13369,6 +13370,7 @@ "integrity": "sha512-BwqQ/AgmKJ8jcMEjaSnfMybnKMgGTrtDKowfTP3pX4jwVy0kNjRsT/AP6h+wC3+3NC+X8X15VWBnTCQlX+wQFg==", "license": "Apache-2.0", "optional": true, + "peer": true, "dependencies": { "regexp-to-ast": "0.4.0" } @@ -15879,7 +15881,8 @@ "version": "2.5.9", "resolved": "https://registry.npmjs.org/dompurify/-/dompurify-2.5.9.tgz", "integrity": "sha512-i6mvVmWN4xo9LrhCOZrDgSs9noW6nOahbrmzjRbPF36YPyj5Ue5lgok0MHDWkG7xzpWFO2OYttXdzM7rJxHvNA==", - "license": "(MPL-2.0 OR Apache-2.0)" + "license": "(MPL-2.0 OR Apache-2.0)", + "peer": true }, "node_modules/domutils": { "version": "3.1.0", @@ -19108,6 +19111,7 @@ "resolved": "https://registry.npmjs.org/handsontable/-/handsontable-13.1.0.tgz", "integrity": "sha512-KqJtS3rJeOWsFWCffnDlM8fcLMTqmW+Vbc7OCVM4X7dYDpfefgFerjNLIxLfT9xohcQoUOS1jcvXwV8dwSppVQ==", "license": "SEE LICENSE IN LICENSE.txt", + "peer": true, "dependencies": { "@types/pikaday": "1.7.4", "core-js": "^3.31.1", @@ -19567,6 +19571,7 @@ "integrity": "sha512-mpVF5zOyNpksZzgTaCQyRAzdC/X43+taz5x1n7zNbs/PUUv0AuLmsy2yfihCr+vihUzN/pk+gXBbOfNpXKOpgA==", "license": "GPL-3.0-only", "optional": true, + "peer": true, "dependencies": { "chevrotain": "^6.5.0", "tiny-emitter": "^2.1.0" @@ -23120,9 +23125,9 @@ } }, "node_modules/merge": { - "version": "1.2.1", - "resolved": "https://registry.npmjs.org/merge/-/merge-1.2.1.tgz", - "integrity": "sha512-VjFo4P5Whtj4vsLzsYBu5ayHhoHJ0UqNm7ibvShmbmoz7tGi0vXaoJbGdB+GmDMLUdg8DpQXEIeVDAe8MaABvQ==", + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/merge/-/merge-2.1.1.tgz", + "integrity": "sha512-jz+Cfrg9GWOZbQAnDQ4hlVnQky+341Yk5ru8bZSe6sIDTCIg8n9i/u7hSQGSVOF3C7lH6mGtqjkiT9G4wFLL0w==", "license": "MIT" }, "node_modules/merge-deep": { @@ -23465,6 +23470,7 @@ "resolved": "https://registry.npmjs.org/moment/-/moment-2.29.4.tgz", "integrity": "sha512-5LC9SOxjSc2HF6vO2CyuTDNivEdoz2IvyJJGj6X8DJ0eFyfszE0QiEd+iXmBvUP3WHxSjFH/vIsA0EN00cgr8w==", "license": "MIT", + "peer": true, "engines": { "node": "*" } @@ -26857,6 +26863,7 @@ "resolved": "https://registry.npmjs.org/numbro/-/numbro-2.1.2.tgz", "integrity": "sha512-7w833BxZmKGLE9HI0aREtNVRVH6WTYUUlWf4qgA5gKNhPQ4F/MRZ14sc0v8eoLORprk9ZTVwYaLwj8N3Zgxwiw==", "license": "MIT", + "peer": true, "dependencies": { "bignumber.js": "^8.0.1" }, @@ -28167,7 +28174,8 @@ "version": "1.8.2", "resolved": "https://registry.npmjs.org/pikaday/-/pikaday-1.8.2.tgz", "integrity": "sha512-TNtsE+34BIax3WtkB/qqu5uepV1McKYEgvL3kWzU7aqPCpMEN6rBF3AOwu4WCwAealWlBGobXny/9kJb49C1ew==", - "license": "(0BSD OR MIT)" + "license": "(0BSD OR MIT)", + "peer": true }, "node_modules/pinkie": { "version": "2.0.4", @@ -29835,7 +29843,8 @@ "resolved": "https://registry.npmjs.org/regexp-to-ast/-/regexp-to-ast-0.4.0.tgz", "integrity": "sha512-4qf/7IsIKfSNHQXSwial1IFmfM1Cc/whNBQqRwe0V2stPe7KmN1U0tWQiIx6JiirgSrisjE0eECdNf7Tav1Ntw==", "license": "MIT", - "optional": true + "optional": true, + "peer": true }, "node_modules/regexp.prototype.flags": { "version": "1.5.2", @@ -31337,6 +31346,17 @@ "websocket-driver": "^0.7.4" } }, + "node_modules/sockjs/node_modules/uuid": { + "version": "8.3.2", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", + "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==", + "deprecated": "uuid@10 and below is no longer supported. For ESM codebases, update to uuid@latest. For CommonJS codebases, use uuid@11 (but be aware this version will likely be deprecated in 2028).", + "dev": true, + "license": "MIT", + "bin": { + "uuid": "dist/bin/uuid" + } + }, "node_modules/socks": { "version": "2.8.1", "resolved": "https://registry.npmjs.org/socks/-/socks-2.8.1.tgz", @@ -32872,7 +32892,8 @@ "resolved": "https://registry.npmjs.org/tiny-emitter/-/tiny-emitter-2.1.0.tgz", "integrity": "sha512-NB6Dk1A9xgQPMoGqC5CVXn123gWyte215ONT5Pp5a0yt4nlEoO1ZWeCwpncaekPHXO60i47ihFnZPiRPjRMq4Q==", "license": "MIT", - "optional": true + "optional": true, + "peer": true }, "node_modules/tmp": { "version": "0.2.1", @@ -33684,12 +33705,16 @@ } }, "node_modules/uuid": { - "version": "8.3.2", - "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", - "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==", + "version": "11.1.1", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-11.1.1.tgz", + "integrity": "sha512-vIYxrBCC/N/K+Js3qSN88go7kIfNPssr/hHCesKCQNAjmgvYS2oqr69kIufEG+O4+PfezOH4EbIeHCfFov8ZgQ==", + "funding": [ + "https://github.com/sponsors/broofa", + "https://github.com/sponsors/ctavan" + ], "license": "MIT", "bin": { - "uuid": "dist/bin/uuid" + "uuid": "dist/esm/bin/uuid" } }, "node_modules/v8-compile-cache": { @@ -40280,6 +40305,7 @@ "version": "1.7.4", "resolved": "https://registry.npmjs.org/@types/pikaday/-/pikaday-1.7.4.tgz", "integrity": "sha512-0KsHVyw5pTG829nqG4IRu7m+BFQlFEBdbE/1i3S5182HeKUKv1uEW0gyEmkJVp5i4IV+9pyh23O83+KpRkSQbw==", + "peer": true, "requires": { "moment": ">=2.14.0" } @@ -44064,7 +44090,8 @@ "bignumber.js": { "version": "8.1.1", "resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-8.1.1.tgz", - "integrity": "sha512-QD46ppGintwPGuL1KqmwhR0O+N2cZUg8JG/VzwI2e28sM9TqHjQB10lI4QAaMHVbLzwVLLAwEglpKPViWX+5NQ==" + "integrity": "sha512-QD46ppGintwPGuL1KqmwhR0O+N2cZUg8JG/VzwI2e28sM9TqHjQB10lI4QAaMHVbLzwVLLAwEglpKPViWX+5NQ==", + "peer": true }, "binary-extensions": { "version": "2.2.0", @@ -44547,6 +44574,7 @@ "resolved": "https://registry.npmjs.org/chevrotain/-/chevrotain-6.5.0.tgz", "integrity": "sha512-BwqQ/AgmKJ8jcMEjaSnfMybnKMgGTrtDKowfTP3pX4jwVy0kNjRsT/AP6h+wC3+3NC+X8X15VWBnTCQlX+wQFg==", "optional": true, + "peer": true, "requires": { "regexp-to-ast": "0.4.0" } @@ -46370,7 +46398,8 @@ "dompurify": { "version": "2.5.9", "resolved": "https://registry.npmjs.org/dompurify/-/dompurify-2.5.9.tgz", - "integrity": "sha512-i6mvVmWN4xo9LrhCOZrDgSs9noW6nOahbrmzjRbPF36YPyj5Ue5lgok0MHDWkG7xzpWFO2OYttXdzM7rJxHvNA==" + "integrity": "sha512-i6mvVmWN4xo9LrhCOZrDgSs9noW6nOahbrmzjRbPF36YPyj5Ue5lgok0MHDWkG7xzpWFO2OYttXdzM7rJxHvNA==", + "peer": true }, "domutils": { "version": "3.1.0", @@ -48731,6 +48760,7 @@ "version": "13.1.0", "resolved": "https://registry.npmjs.org/handsontable/-/handsontable-13.1.0.tgz", "integrity": "sha512-KqJtS3rJeOWsFWCffnDlM8fcLMTqmW+Vbc7OCVM4X7dYDpfefgFerjNLIxLfT9xohcQoUOS1jcvXwV8dwSppVQ==", + "peer": true, "requires": { "@types/pikaday": "1.7.4", "core-js": "^3.31.1", @@ -49061,6 +49091,7 @@ "resolved": "https://registry.npmjs.org/hyperformula/-/hyperformula-2.7.1.tgz", "integrity": "sha512-mpVF5zOyNpksZzgTaCQyRAzdC/X43+taz5x1n7zNbs/PUUv0AuLmsy2yfihCr+vihUzN/pk+gXBbOfNpXKOpgA==", "optional": true, + "peer": true, "requires": { "chevrotain": "^6.5.0", "tiny-emitter": "^2.1.0" @@ -51711,9 +51742,9 @@ } }, "merge": { - "version": "1.2.1", - "resolved": "https://registry.npmjs.org/merge/-/merge-1.2.1.tgz", - "integrity": "sha512-VjFo4P5Whtj4vsLzsYBu5ayHhoHJ0UqNm7ibvShmbmoz7tGi0vXaoJbGdB+GmDMLUdg8DpQXEIeVDAe8MaABvQ==" + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/merge/-/merge-2.1.1.tgz", + "integrity": "sha512-jz+Cfrg9GWOZbQAnDQ4hlVnQky+341Yk5ru8bZSe6sIDTCIg8n9i/u7hSQGSVOF3C7lH6mGtqjkiT9G4wFLL0w==" }, "merge-deep": { "version": "3.0.3", @@ -51968,7 +51999,8 @@ "moment": { "version": "2.29.4", "resolved": "https://registry.npmjs.org/moment/-/moment-2.29.4.tgz", - "integrity": "sha512-5LC9SOxjSc2HF6vO2CyuTDNivEdoz2IvyJJGj6X8DJ0eFyfszE0QiEd+iXmBvUP3WHxSjFH/vIsA0EN00cgr8w==" + "integrity": "sha512-5LC9SOxjSc2HF6vO2CyuTDNivEdoz2IvyJJGj6X8DJ0eFyfszE0QiEd+iXmBvUP3WHxSjFH/vIsA0EN00cgr8w==", + "peer": true }, "mrmime": { "version": "2.0.0", @@ -54413,6 +54445,7 @@ "version": "2.1.2", "resolved": "https://registry.npmjs.org/numbro/-/numbro-2.1.2.tgz", "integrity": "sha512-7w833BxZmKGLE9HI0aREtNVRVH6WTYUUlWf4qgA5gKNhPQ4F/MRZ14sc0v8eoLORprk9ZTVwYaLwj8N3Zgxwiw==", + "peer": true, "requires": { "bignumber.js": "^8.0.1" } @@ -55413,7 +55446,8 @@ "pikaday": { "version": "1.8.2", "resolved": "https://registry.npmjs.org/pikaday/-/pikaday-1.8.2.tgz", - "integrity": "sha512-TNtsE+34BIax3WtkB/qqu5uepV1McKYEgvL3kWzU7aqPCpMEN6rBF3AOwu4WCwAealWlBGobXny/9kJb49C1ew==" + "integrity": "sha512-TNtsE+34BIax3WtkB/qqu5uepV1McKYEgvL3kWzU7aqPCpMEN6rBF3AOwu4WCwAealWlBGobXny/9kJb49C1ew==", + "peer": true }, "pinkie": { "version": "2.0.4", @@ -56557,7 +56591,8 @@ "version": "0.4.0", "resolved": "https://registry.npmjs.org/regexp-to-ast/-/regexp-to-ast-0.4.0.tgz", "integrity": "sha512-4qf/7IsIKfSNHQXSwial1IFmfM1Cc/whNBQqRwe0V2stPe7KmN1U0tWQiIx6JiirgSrisjE0eECdNf7Tav1Ntw==", - "optional": true + "optional": true, + "peer": true }, "regexp.prototype.flags": { "version": "1.5.2", @@ -57644,6 +57679,14 @@ "faye-websocket": "^0.11.3", "uuid": "^8.3.2", "websocket-driver": "^0.7.4" + }, + "dependencies": { + "uuid": { + "version": "8.3.2", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", + "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==", + "dev": true + } } }, "socks": { @@ -58835,7 +58878,8 @@ "version": "2.1.0", "resolved": "https://registry.npmjs.org/tiny-emitter/-/tiny-emitter-2.1.0.tgz", "integrity": "sha512-NB6Dk1A9xgQPMoGqC5CVXn123gWyte215ONT5Pp5a0yt4nlEoO1ZWeCwpncaekPHXO60i47ihFnZPiRPjRMq4Q==", - "optional": true + "optional": true, + "peer": true }, "tmp": { "version": "0.2.1", @@ -59434,9 +59478,9 @@ "dev": true }, "uuid": { - "version": "8.3.2", - "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", - "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==" + "version": "11.1.1", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-11.1.1.tgz", + "integrity": "sha512-vIYxrBCC/N/K+Js3qSN88go7kIfNPssr/hHCesKCQNAjmgvYS2oqr69kIufEG+O4+PfezOH4EbIeHCfFov8ZgQ==" }, "v8-compile-cache": { "version": "2.4.0", diff --git a/package.json b/package.json index 64f7d0576..afe1b8bf9 100755 --- a/package.json +++ b/package.json @@ -50,10 +50,10 @@ "d3": "^7.9.0", "deep-filter": "^1.0.2", "is-plain-object": "^2.0.4", - "merge": "^1.2.1", + "merge": "^2.1.1", "react-google-charts": "^3.0.8", "topojson-client": "^3.1.0", - "uuid": "^8.3.2" + "uuid": "^11.1.1" }, "devDependencies": { "@semantic-release/changelog": "^5.0.1", diff --git a/templates/docs.php b/templates/docs.php index a094e05b4..619addeab 100644 --- a/templates/docs.php +++ b/templates/docs.php @@ -44,10 +44,10 @@

Documentation

-

To get started with Visualizer, we recommend you first bookmark our main documentation page here.

+

To get started with Visualizer, we recommend you first bookmark our main documentation page here.

- Notably, you could take a look at this first introductory tutorial: How to create my first chart. + Notably, you could take a look at this first introductory tutorial: How to create my first chart.

If you prefer learning through video, this could prove useful. It is a little dated however.

diff --git a/tests/e2e/specs/admin.spec.js b/tests/e2e/specs/admin.spec.js index 3534105ac..3a6b9488a 100644 --- a/tests/e2e/specs/admin.spec.js +++ b/tests/e2e/specs/admin.spec.js @@ -211,6 +211,41 @@ test.describe( 'Chart Library', () => { await expect( page.getByText(backendName) ).toBeVisible(); await expect( page.locator('g').filter({ hasText: 'Test Chart Name' }).locator('rect') ).toBeVisible(); } ); + + // frame.js rewrites the Google API link for the chart type. It must leave the + // "Common Snippets" docs link and its UTMs alone, on both libraries. + for ( const library of [ 'GoogleCharts', 'ChartJS' ] ) { + test( `manual configuration links (${ library })`, async ( { admin, page } ) => { + await admin.visitAdminPage( 'admin.php?page=visualizer&vaction=addnew' ); + await page.waitForURL( '**/admin.php?page=visualizer&vaction=addnew' ); + await expect( page.getByRole('button', { name: 'Classic Builder Step-by-step' }) ).toBeVisible({ timeout: 5000 }); + await page.getByRole('button', { name: 'Classic Builder Step-by-step' }).click(); + await page.waitForSelector('h1:text("Visualizer")'); + + const frame = page.frameLocator('iframe'); + await frame.getByRole('combobox').selectOption({ value: library }); + await selectChartAdmin( frame, CHART_JS_LABELS.pie ); + await expect( frame.getByRole('button', { name: 'Create Chart' }) ).toBeVisible({ timeout: 5000 }); + + const snippets = frame.locator('span.viz-gvlink a').filter({ hasText: 'Common Snippets' }); + await expect( snippets ).toHaveAttribute( + 'href', + 'https://docs.themeisle.com/visualizer-charts-and-graphs/manual-configuration?utm_source=wpadmin&utm_medium=chart-editor&utm_campaign=manual-configuration&utm_content=docsvisualizer-charts-and-graphsmanual-configuration' + ); + + if ( library === 'GoogleCharts' ) { + await expect( frame.locator('span.viz-gvlink a').filter({ hasText: 'Google Visualization API' }) ).toHaveAttribute( + 'href', + 'https://developers.google.com/chart/interactive/docs/gallery/piechart#configuration-options' + ); + // The data filter box keeps its own text and link. + await expect( frame.locator('span.viz-gvlink a').filter({ hasText: 'Chart Controls API' }) ).toHaveAttribute( + 'href', + 'https://developers.google.com/chart/interactive/docs/gallery/controls#controls-gallery' + ); + } + } ); + } } ); test.describe( 'Support', () => { diff --git a/tests/js/render-datatables-settings.test.js b/tests/js/render-datatables-settings.test.js new file mode 100644 index 000000000..394e56a4a --- /dev/null +++ b/tests/js/render-datatables-settings.test.js @@ -0,0 +1,100 @@ +const assert = require( 'node:assert/strict' ); +const path = require( 'path' ); +const { afterEach, beforeEach, describe, it } = require( 'node:test' ); + +describe( 'DataTables nested settings', () => { + let handlers; + let capturedSettings; + let alertCalls; + + beforeEach( () => { + handlers = {}; + capturedSettings = null; + alertCalls = 0; + + global.window = {}; + global.document = { + getElementById: () => ( {} ), + }; + global.alert = () => { + alertCalls++; + }; + + const collection = { + length: 0, + hasClass: () => false, + children: () => ( { length: 0 } ), + find: () => ( { length: 0 } ), + empty() { + return this; + }, + append() { + return this; + }, + css: () => '400px', + addClass() { + return this; + }, + remove() { + return this; + }, + on( event, callback ) { + handlers[ event ] = callback; + return this; + }, + trigger() { + return this; + }, + DataTable( settings ) { + capturedSettings = settings; + return this; + }, + }; + + const jquery = () => collection; + jquery.extend = ( target, ...sources ) => Object.assign( target, ...sources ); + jquery.fn = { + dataTable: { + render: { + number: () => null, + }, + }, + }; + global.jQuery = jquery; + + const renderer = path.resolve( __dirname, '../../js/render-datatables.js' ); + delete require.cache[ renderer ]; + require( renderer ); + } ); + + afterEach( () => { + delete global.alert; + delete global.document; + delete global.jQuery; + delete global.window; + } ); + + function renderWithSettings( settings ) { + handlers[ 'visualizer:render:specificchart:start' ]( {}, { + id: 'chart', + chart: { + library: 'datatables', + settings, + series: [ { label: 'Value', type: 'string' } ], + data: [ [ 'Example' ] ], + }, + v: { page_type: 'frontend' }, + } ); + } + + it( 'does not execute an unsupported nested setting name', () => { + assert.doesNotThrow( () => renderWithSettings( { 'alert(1)_x': 'value' } ) ); + assert.equal( alertCalls, 0 ); + } ); + + it( 'continues to apply the supported select nested setting', () => { + renderWithSettings( { select_info: 'true' } ); + assert.equal( capturedSettings.select.info, true ); + assert.equal( capturedSettings.info, true ); + } ); +} ); diff --git a/tests/php/static-analysis-stubs/symbols.php b/tests/php/static-analysis-stubs/symbols.php index 0e17329a9..9df272ecc 100644 --- a/tests/php/static-analysis-stubs/symbols.php +++ b/tests/php/static-analysis-stubs/symbols.php @@ -31,11 +31,11 @@ define( 'VISUALIZER_ENABLE_BETA_FEATURES', '' ); // the link to pre-build queries. -define( 'VISUALIZER_DB_QUERY_DOC_URL', 'https://docs.themeisle.com/article/970-visualizer-sample-queries-to-generate-charts' ); -define( 'VISUALIZER_MAIN_DOC', 'https://docs.themeisle.com/category/657-visualizer' ); +define( 'VISUALIZER_DB_QUERY_DOC_URL', 'https://docs.themeisle.com/visualizer-charts-and-graphs/visualizer-sample-queries-to-generate-charts' ); +define( 'VISUALIZER_MAIN_DOC', 'https://docs.themeisle.com/visualizer-charts-and-graphs/' ); define( 'VISUALIZER_DOC_COLLECTION', 'https://docs.themeisle.com/search?collectionId=561ec249c69791452ed4bceb&query=#+visualizer' ); define( 'VISUALIZER_DEMO_URL', 'https://demo.themeisle.com/visualizer/#' ); -define( 'VISUALIZER_CODE_SNIPPETS_URL', 'https://docs.themeisle.com/category/726-visualizer' ); +define( 'VISUALIZER_CODE_SNIPPETS_URL', 'https://docs.themeisle.com/visualizer-charts-and-graphs/' ); define( 'VISUALIZER_SUBSCRIBE_API', 'https://api.themeisle.com/tracking/subscribe' ); // to redirect all themeisle_log_event to error log. diff --git a/tests/test-ajax.php b/tests/test-ajax.php index 20a0ed8c4..60fdda461 100644 --- a/tests/test-ajax.php +++ b/tests/test-ajax.php @@ -1122,6 +1122,375 @@ public function test_json_schedule_denied_for_chart_user_cannot_edit() { $this->assertSame( '', get_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_SCHEDULE, true ) ); } + /** + * A chart editor without report access cannot schedule a WooCommerce report import. + */ + public function test_woocommerce_json_schedule_denied_without_report_capability() { + $this->enable_pro(); + $chart_id = $this->create_chart_for_user( $this->contibutor_user_id ); + wp_set_current_user( $this->contibutor_user_id ); + $_POST = array( + 'chart' => $chart_id, + 'time' => 12, + 'is_woocommerce_report' => 'true', + 'security' => wp_create_nonce( Visualizer_Plugin::ACTION_JSON_SET_SCHEDULE . Visualizer_Plugin::VERSION ), + ); + + try { + $this->_handleAjax( Visualizer_Plugin::ACTION_JSON_SET_SCHEDULE ); + } catch ( WPAjaxDieContinueException $e ) { + // Expected after the JSON response. + } + + $response = json_decode( $this->_last_response ); + $this->assertFalse( $response->success ); + $this->assertSame( '', get_post_meta( $chart_id, Visualizer_Plugin::CF_WOOCOMMERCE_REPORT_USER, true ) ); + } + + /** + * A report-capable chart editor can authorize future scheduled imports. + */ + public function test_woocommerce_json_schedule_records_authorizing_user() { + $this->enable_pro(); + $chart_id = $this->create_chart_for_user( $this->admin_user_id ); + wp_set_current_user( $this->admin_user_id ); + wp_get_current_user()->add_cap( 'view_woocommerce_reports' ); + $this->assertTrue( current_user_can( 'view_woocommerce_reports' ) ); + $_POST = array( + 'chart' => $chart_id, + 'time' => 12, + 'is_woocommerce_report' => 'true', + 'security' => wp_create_nonce( Visualizer_Plugin::ACTION_JSON_SET_SCHEDULE . Visualizer_Plugin::VERSION ), + ); + + try { + $this->_handleAjax( Visualizer_Plugin::ACTION_JSON_SET_SCHEDULE ); + } catch ( WPAjaxDieContinueException $e ) { + // Expected after the JSON response. + } + + $response = json_decode( $this->_last_response ); + $this->assertTrue( $response->success, $this->_last_response ); + $this->assertSame( $this->admin_user_id, (int) get_post_meta( $chart_id, Visualizer_Plugin::CF_WOOCOMMERCE_REPORT_USER, true ) ); + } + + /** + * Switching a chart to another JSON source keeps the recorded report user. + */ + public function test_json_schedule_for_other_source_keeps_report_user() { + $this->enable_pro(); + $chart_id = $this->create_chart_for_user( $this->admin_user_id ); + update_post_meta( $chart_id, Visualizer_Plugin::CF_IS_WOOCOMMERCE_SOURCE, true ); + update_post_meta( $chart_id, Visualizer_Plugin::CF_WOOCOMMERCE_REPORT_USER, $this->admin_user_id ); + wp_set_current_user( $this->admin_user_id ); + $_POST = array( + 'chart' => $chart_id, + 'time' => 12, + 'is_woocommerce_report' => 'false', + 'security' => wp_create_nonce( Visualizer_Plugin::ACTION_JSON_SET_SCHEDULE . Visualizer_Plugin::VERSION ), + ); + + try { + $this->_handleAjax( Visualizer_Plugin::ACTION_JSON_SET_SCHEDULE ); + } catch ( WPAjaxDieContinueException $e ) { + // Expected after the JSON response. + } + + $this->assertSame( '', get_post_meta( $chart_id, Visualizer_Plugin::CF_IS_WOOCOMMERCE_SOURCE, true ) ); + $this->assertSame( $this->admin_user_id, (int) get_post_meta( $chart_id, Visualizer_Plugin::CF_WOOCOMMERCE_REPORT_USER, true ) ); + } + + /** + * Saving a new JSON endpoint makes the saving user the chart's report user. + */ + public function test_json_source_change_records_saving_user() { + $chart_id = $this->create_chart_for_user( $this->admin_user_id ); + update_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_URL, 'https://example.com/old.json' ); + update_post_meta( $chart_id, Visualizer_Plugin::CF_WOOCOMMERCE_REPORT_USER, $this->admin_user_id ); + $editor_id = $this->factory->user->create( array( 'role' => 'editor' ) ); + wp_set_current_user( $editor_id ); + + $this->handle_json_set_data( $chart_id, array( 'url' => 'https://example.com/new.json' ) ); + + $this->assertSame( 'https://example.com/new.json', get_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_URL, true ) ); + $this->assertSame( $editor_id, (int) get_post_meta( $chart_id, Visualizer_Plugin::CF_WOOCOMMERCE_REPORT_USER, true ) ); + } + + /** + * Saving the same JSON endpoint again keeps the recorded report user. + */ + public function test_json_source_resave_keeps_report_user() { + $chart_id = $this->create_chart_for_user( $this->admin_user_id ); + update_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_URL, 'https://example.com/data.json' ); + update_post_meta( $chart_id, Visualizer_Plugin::CF_WOOCOMMERCE_REPORT_USER, $this->admin_user_id ); + wp_set_current_user( $this->factory->user->create( array( 'role' => 'editor' ) ) ); + + $this->handle_json_set_data( $chart_id, array( 'url' => 'https://example.com/data.json' ) ); + + $this->assertSame( $this->admin_user_id, (int) get_post_meta( $chart_id, Visualizer_Plugin::CF_WOOCOMMERCE_REPORT_USER, true ) ); + } + + /** + * Restoring a revision keeps the recorded report user. + */ + public function test_revision_restore_keeps_report_user() { + $chart_id = $this->create_chart_for_user( $this->admin_user_id ); + update_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_URL, 'https://example.com/data.json' ); + update_post_meta( $chart_id, Visualizer_Plugin::CF_WOOCOMMERCE_REPORT_USER, $this->admin_user_id ); + $revision_id = $this->factory->post->create( + array( + 'post_type' => 'revision', + 'post_parent' => $chart_id, + 'post_status' => 'inherit', + ) + ); + add_metadata( 'post', $revision_id, Visualizer_Plugin::CF_JSON_URL, 'https://example.com/data.json' ); + add_metadata( 'post', $revision_id, Visualizer_Plugin::CF_WOOCOMMERCE_REPORT_USER, $this->admin_user_id ); + $editor_id = $this->factory->user->create( array( 'role' => 'editor' ) ); + wp_set_current_user( $editor_id ); + + ( new Visualizer_Module_Admin( Visualizer_Plugin::instance() ) )->restoreRevision( $chart_id, $revision_id ); + + $this->assertSame( $this->admin_user_id, (int) get_post_meta( $chart_id, Visualizer_Plugin::CF_WOOCOMMERCE_REPORT_USER, true ) ); + } + + /** + * A cloned chart does not carry the original chart's report user. + */ + public function test_clone_does_not_copy_report_user() { + $chart_id = $this->create_chart_for_user( $this->admin_user_id ); + update_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_URL, 'https://example.com/data.json' ); + update_post_meta( $chart_id, Visualizer_Plugin::CF_WOOCOMMERCE_REPORT_USER, $this->admin_user_id ); + $editor_id = $this->factory->user->create( array( 'role' => 'editor' ) ); + wp_set_current_user( $editor_id ); + $_GET = array( + 'nonce' => wp_create_nonce( Visualizer_Plugin::ACTION_CLONE_CHART ), + 'chart' => $chart_id, + ); + + try { + $this->_handleAjax( Visualizer_Plugin::ACTION_CLONE_CHART ); + } catch ( WPAjaxDieContinueException $e ) { + // Expected after the redirect. + } catch ( WPAjaxDieStopException $e ) { + // Expected after the redirect. + } + + $clones = get_posts( + array( + 'post_type' => Visualizer_Plugin::CPT_VISUALIZER, + 'post_status' => 'any', + 'author' => $editor_id, + 'fields' => 'ids', + ) + ); + $this->assertCount( 1, $clones ); + $this->assertSame( 'https://example.com/data.json', get_post_meta( $clones[0], Visualizer_Plugin::CF_JSON_URL, true ) ); + $this->assertSame( '', get_post_meta( $clones[0], Visualizer_Plugin::CF_WOOCOMMERCE_REPORT_USER, true ) ); + } + + /** + * A translated copy of a chart does not carry the original chart's report user. + */ + public function test_translation_copy_does_not_copy_report_user() { + $chart_id = $this->create_chart_for_user( $this->admin_user_id ); + update_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_URL, 'https://example.com/data.json' ); + update_post_meta( $chart_id, Visualizer_Plugin::CF_WOOCOMMERCE_REPORT_USER, $this->admin_user_id ); + $editor_id = $this->factory->user->create( array( 'role' => 'editor' ) ); + wp_set_current_user( $editor_id ); + $_GET = array( + 'lang' => 'de', + 'parent_chart_id' => $chart_id, + ); + + try { + $this->_handleAjax( Visualizer_Plugin::ACTION_CREATE_CHART ); + } catch ( WPAjaxDieContinueException $e ) { + // Expected after the redirect. + } catch ( WPAjaxDieStopException $e ) { + // Expected after the redirect. + } + + $copies = get_posts( + array( + 'post_type' => Visualizer_Plugin::CPT_VISUALIZER, + 'post_status' => 'any', + 'author' => $editor_id, + 'fields' => 'ids', + ) + ); + $this->assertCount( 1, $copies ); + $this->assertSame( 'https://example.com/data.json', get_post_meta( $copies[0], Visualizer_Plugin::CF_JSON_URL, true ) ); + $this->assertSame( '', get_post_meta( $copies[0], Visualizer_Plugin::CF_WOOCOMMERCE_REPORT_USER, true ) ); + } + + /** + * The AI builder edits the stored cell text. + */ + public function test_ai_builder_fetch_returns_stored_cells() { + wp_set_current_user( $this->admin_user_id ); + $chart_id = $this->factory->post->create( + array( + 'post_type' => Visualizer_Plugin::CPT_VISUALIZER, + 'post_author' => $this->admin_user_id, + 'post_content' => wp_slash( serialize( array( array( 'Q1Q4' ) ) ) ), + ) + ); + $_POST = array( + 'nonce' => wp_create_nonce( 'visualizer-ai-builder' ), + 'chart_id' => $chart_id, + ); + + try { + $this->_handleAjax( 'visualizer-ai-fetch' ); + } catch ( WPAjaxDieContinueException $e ) { + // Expected after the JSON response. + } + + $response = json_decode( $this->_last_response ); + $this->assertTrue( $response->success, $this->_last_response ); + $this->assertSame( 'Q1Q4', $response->data->data[0][0] ); + } + + /** + * The JSON import preview prints endpoint keys as text. + */ + public function test_json_preview_escapes_endpoint_keys() { + $chart_id = $this->create_chart_for_user( $this->admin_user_id ); + wp_set_current_user( $this->admin_user_id ); + $filter = $this->mock_json_endpoint( array( array( '' => 'a' ) ) ); + + $_GET['security'] = wp_create_nonce( Visualizer_Plugin::ACTION_JSON_GET_DATA . Visualizer_Plugin::VERSION ); + $_POST['params'] = array( + 'url' => 'https://example.com/data.json', + 'method' => 'GET', + 'chart' => $chart_id, + 'root' => 'root', + ); + try { + $this->_handleAjax( Visualizer_Plugin::ACTION_JSON_GET_DATA ); + } catch ( WPAjaxDieContinueException $e ) { + // Expected after the JSON response. + } finally { + remove_filter( 'pre_http_request', $filter ); + } + + $response = json_decode( $this->_last_response ); + $this->assertTrue( $response->success, $this->_last_response ); + $this->assertStringContainsString( '<img', $response->data->table ); + $this->assertStringNotContainsString( 'data->table ); + } + + /** + * The JSON import preview table carries no event attributes from the endpoint. + */ + public function test_json_preview_removes_event_attributes() { + $chart_id = $this->create_chart_for_user( $this->admin_user_id ); + wp_set_current_user( $this->admin_user_id ); + $filter = $this->mock_json_endpoint( array( array( 'name' => '' ) ) ); + + $_GET['security'] = wp_create_nonce( Visualizer_Plugin::ACTION_JSON_GET_DATA . Visualizer_Plugin::VERSION ); + $_POST['params'] = array( + 'url' => 'https://example.com/data.json', + 'method' => 'GET', + 'chart' => $chart_id, + 'root' => 'root', + ); + try { + $this->_handleAjax( Visualizer_Plugin::ACTION_JSON_GET_DATA ); + } catch ( WPAjaxDieContinueException $e ) { + // Expected after the JSON response. + } finally { + remove_filter( 'pre_http_request', $filter ); + } + + $response = json_decode( $this->_last_response ); + $this->assertTrue( $response->success, $this->_last_response ); + $this->assertStringContainsString( 'data->table ); + $this->assertStringNotContainsString( 'onerror', $response->data->table ); + } + + /** + * The preview rendered after saving a JSON import carries no event attributes. + */ + public function test_json_save_preview_removes_event_attributes() { + $chart_id = $this->create_chart_for_user( $this->admin_user_id ); + wp_set_current_user( $this->admin_user_id ); + + $this->handle_json_set_data( + $chart_id, + array( + 'header' => array( 'name' ), + 'type' => array( 'name' => 'string' ), + ), + array( array( 'name' => '' ) ) + ); + + $this->assertStringContainsString( 'canvas.data', $this->_last_response ); + $this->assertStringNotContainsString( 'onerror', $this->_last_response ); + } + + /** + * The preview rendered after a CSV upload carries no event attributes. + */ + public function test_csv_upload_preview_removes_event_attributes() { + $chart_id = $this->create_chart_for_user( $this->admin_user_id ); + wp_set_current_user( $this->admin_user_id ); + $file = wp_tempnam( 'visualizer.csv' ); + file_put_contents( $file, "Name,Value\nstring,number\n\"\",1\n" ); + $_FILES = array( + 'local_data' => array( + 'tmp_name' => $file, + 'name' => 'data.csv', + 'error' => 0, + ), + ); + $_GET = array( + 'nonce' => wp_create_nonce( 'visualizer-upload-data' ), + 'chart' => $chart_id, + ); + + try { + $this->_handleAjax( Visualizer_Plugin::ACTION_UPLOAD_DATA ); + } catch ( WPAjaxDieContinueException $e ) { + // Expected once the update page has rendered. + } catch ( WPAjaxDieStopException $e ) { + // Expected when the handler stops after rendering. + } finally { + unlink( $file ); + } + + $this->assertStringContainsString( 'canvas.data', $this->_last_response ); + $this->assertStringNotContainsString( 'onerror', $this->_last_response ); + } + + /** + * The database query preview table carries no event attributes from the results. + */ + public function test_query_preview_removes_event_attributes() { + $this->_setRole( 'administrator' ); + $this->enable_pro(); + $post_id = $this->factory->post->create( array( 'post_title' => '' ) ); + $this->assertStringContainsString( 'onerror', get_post_field( 'post_title', $post_id ) ); + + global $wpdb; + $_GET['security'] = wp_create_nonce( Visualizer_Plugin::ACTION_FETCH_DB_DATA . Visualizer_Plugin::VERSION ); + $_POST['params'] = array( + 'query' => 'SELECT post_title FROM ' . $wpdb->prefix . 'posts WHERE ID = ' . $post_id, + 'chart_id' => 1, + ); + try { + $this->_handleAjax( Visualizer_Plugin::ACTION_FETCH_DB_DATA ); + } catch ( WPAjaxDieContinueException $e ) { + // Expected after the JSON response. + } + + $response = json_decode( $this->_last_response ); + $this->assertTrue( $response->success, $this->_last_response ); + $this->assertStringContainsString( 'data->table ); + $this->assertStringNotContainsString( 'onerror', $response->data->table ); + } + /** * A user cannot replace another user's JSON chart data. */ @@ -1151,13 +1520,14 @@ public function test_json_set_data_denied_for_chart_user_cannot_edit() { /** * Mock the JSON endpoint so the set-data handler does not hit the network. * + * @param array> $rows The rows the endpoint returns. * @return callable The filter callback, for removal. */ - private function mock_json_endpoint() { - $filter = function () { + private function mock_json_endpoint( array $rows = array( array( 'name' => 'a', 'value' => 1 ) ) ) { + $filter = function () use ( $rows ) { return array( 'headers' => array(), - 'body' => wp_json_encode( array( array( 'name' => 'a', 'value' => 1 ) ) ), + 'body' => wp_json_encode( $rows ), 'response' => array( 'code' => 200, 'message' => '' ), 'cookies' => array(), 'filename' => null, @@ -1170,10 +1540,11 @@ private function mock_json_endpoint() { /** * Run the JSON set-data handler as the current user. * - * @param int $chart_id The chart being saved. - * @param array $post The POST fields to send on top of the defaults. + * @param int $chart_id The chart being saved. + * @param array $post The POST fields to send on top of the defaults. + * @param array> $rows The rows the endpoint returns. */ - private function handle_json_set_data( $chart_id, array $post ) { + private function handle_json_set_data( $chart_id, array $post, array $rows = array( array( 'name' => 'a', 'value' => 1 ) ) ) { $_GET = array( 'chart' => $chart_id, 'security' => wp_create_nonce( Visualizer_Plugin::ACTION_JSON_SET_DATA . Visualizer_Plugin::VERSION ), @@ -1190,7 +1561,7 @@ private function handle_json_set_data( $chart_id, array $post ) { $post ); - $filter = $this->mock_json_endpoint(); + $filter = $this->mock_json_endpoint( $rows ); try { $this->_handleAjax( Visualizer_Plugin::ACTION_JSON_SET_DATA ); } catch ( WPAjaxDieContinueException $e ) { diff --git a/tests/test-chart-data-sanitization.php b/tests/test-chart-data-sanitization.php new file mode 100644 index 000000000..9de74f186 --- /dev/null +++ b/tests/test-chart-data-sanitization.php @@ -0,0 +1,167 @@ + 123, + 'post_content' => serialize( + array( + array( + 'Revenue', + '', + '', + 'Research & Development', + '3 < 5', + 'assertSame( 'Revenue', $result[0][0] ); + $this->assertStringNotContainsString( 'onerror', $result[0][1] ); + $this->assertStringNotContainsString( 'assertSame( 'Research & Development', $result[0][3] ); + $this->assertSame( '3 < 5', $result[0][4] ); + $this->assertStringNotContainsString( ' 123, + 'post_content' => serialize( array( array( 'Revenue' ) ) ), + ); + $filter = array( $this, 'add_script_cell' ); + add_filter( Visualizer_Plugin::FILTER_GET_CHART_DATA, $filter ); + + $result = Visualizer_Module::get_chart_data( $chart, 'table' ); + + remove_filter( Visualizer_Plugin::FILTER_GET_CHART_DATA, $filter ); + $this->assertSame( 'Revenue', $result[0][0] ); + $this->assertStringNotContainsString( 'create_chart( array( array( 'Revenue', '' ) ) ); + $filter = array( $this, 'add_script_cell' ); + add_filter( Visualizer_Plugin::FILTER_GET_CHART_DATA, $filter ); + + $result = Visualizer_Gutenberg_Block::get_instance()->get_visualizer_data( array( 'id' => $chart_id ) ); + + remove_filter( Visualizer_Plugin::FILTER_GET_CHART_DATA, $filter ); + $this->assertSame( 'Revenue', $result['visualizer-data'][0][0] ); + $this->assertStringNotContainsString( 'assertStringNotContainsString( 'create_chart( array( array( 'Q1Q4', '' ) ) ); + $module = Visualizer_Plugin::instance()->getModule( Visualizer_Module_Chart::NAME ); + + $csv = $module->_getDataAs( $chart_id, 'csv' ); + $this->assertStringContainsString( 'Q1Q4', $csv['string'] ); + + $print = $module->_getDataAs( $chart_id, 'print' ); + $this->assertStringNotContainsString( 'onerror', $print['csv'] ); + + $editor = Visualizer_Render_Layout::show( 'editor-table', null, $chart_id, 'viz-html-table', false, true ); + $this->assertStringContainsString( esc_attr( 'Q1Q4' ), $editor ); + } + + /** + * The text editor prints the stored cells as textarea text. + */ + public function test_text_editor_escapes_stored_cells() { + $chart_id = $this->create_chart( array( array( '', '1' ) ) ); + + ob_start(); + Visualizer_Render_Layout::show( 'text-editor', $chart_id ); + $editor = ob_get_clean(); + + $this->assertStringNotContainsString( 'assertStringContainsString( '</textarea><img', $editor ); + } + + /** + * The chart abilities API returns the stored cell text for editing. + */ + public function test_get_chart_ability_returns_stored_cells() { + $chart_id = $this->create_chart( array( array( 'Q1Q4', '1' ) ) ); + $module = Visualizer_Plugin::instance()->getModule( Visualizer_Module_Abilities::NAME ); + + $result = $module->executeGetChart( array( 'chart_id' => $chart_id ) ); + + $this->assertSame( 'Q1Q4', $result['rows'][0][0] ); + } + + /** + * Appends a row with a script cell. + * + * @param array> $data The chart data. + * @return array> + */ + public function add_script_cell( $data ) { + $data[] = array( '' ); + return $data; + } + + /** + * Creates a line chart with the given rows, stored as an administrator would. + * + * @param array> $rows The chart rows. + * @return int The chart ID. + */ + private function create_chart( $rows ) { + $author_id = self::factory()->user->create( array( 'role' => 'administrator' ) ); + // Post content filtering leaves the serialized data intact for an administrator. + wp_set_current_user( $author_id ); + $chart_id = self::factory()->post->create( + array( + 'post_type' => Visualizer_Plugin::CPT_VISUALIZER, + 'post_status' => 'publish', + 'post_author' => $author_id, + 'post_content' => wp_slash( serialize( $rows ) ), + ) + ); + update_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_TYPE, 'line' ); + update_post_meta( $chart_id, Visualizer_Plugin::CF_SETTINGS, array() ); + update_post_meta( + $chart_id, + Visualizer_Plugin::CF_SERIES, + array( + array( + 'label' => 'Label', + 'type' => 'string', + ), + array( + 'label' => 'Value', + 'type' => 'string', + ), + ) + ); + return $chart_id; + } +} diff --git a/tests/test-remote-fetch.php b/tests/test-remote-fetch.php index 308ffc403..a5ee5a7cd 100644 --- a/tests/test-remote-fetch.php +++ b/tests/test-remote-fetch.php @@ -680,6 +680,196 @@ public function test_download_blocks_non_public_destination() { $this->assertSame( 0, $requests ); } + /** + * Chart editors without WooCommerce report access must not receive the internal token. + */ + public function test_json_source_does_not_mint_woocommerce_token_for_contributor() { + $user_id = self::factory()->user->create( array( 'role' => 'contributor' ) ); + wp_set_current_user( $user_id ); + + $headers = $this->capture_woocommerce_request_headers(); + + $this->assertArrayNotHasKey( 'X-Visualizer-Token', $headers ); + } + + /** + * Shop users with report access retain the WooCommerce chart source feature. + */ + public function test_json_source_mints_woocommerce_token_for_authorized_user() { + $user_id = self::factory()->user->create( array( 'role' => 'contributor' ) ); + $user = new WP_User( $user_id ); + $user->add_cap( 'view_woocommerce_reports' ); + wp_set_current_user( $user_id ); + + $headers = $this->capture_woocommerce_request_headers(); + + $this->assertArrayHasKey( 'X-Visualizer-Token', $headers ); + $this->assertNotSame( '', $headers['X-Visualizer-Token'] ); + delete_transient( 'visualizer_wc_token_' . $headers['X-Visualizer-Token'] ); + } + + /** + * The report access filter can grant the token to a user without the capability. + */ + public function test_report_access_filter_can_grant_token() { + $user_id = self::factory()->user->create( array( 'role' => 'contributor' ) ); + wp_set_current_user( $user_id ); + add_filter( 'visualizer_can_fetch_woocommerce_reports', '__return_true' ); + + $headers = $this->capture_woocommerce_request_headers(); + + remove_filter( 'visualizer_can_fetch_woocommerce_reports', '__return_true' ); + $this->assertArrayHasKey( 'X-Visualizer-Token', $headers ); + delete_transient( 'visualizer_wc_token_' . $headers['X-Visualizer-Token'] ); + } + + /** + * The report access filter can withhold the token from a user with the capability. + */ + public function test_report_access_filter_can_withhold_token() { + $user_id = self::factory()->user->create( array( 'role' => 'contributor' ) ); + $user = new WP_User( $user_id ); + $user->add_cap( 'view_woocommerce_reports' ); + wp_set_current_user( $user_id ); + add_filter( 'visualizer_can_fetch_woocommerce_reports', '__return_false' ); + + $headers = $this->capture_woocommerce_request_headers(); + + remove_filter( 'visualizer_can_fetch_woocommerce_reports', '__return_false' ); + $this->assertArrayNotHasKey( 'X-Visualizer-Token', $headers ); + } + + /** + * A failed WooCommerce report fetch is reported as a failure. + */ + public function test_failed_woocommerce_fetch_returns_false() { + $filter = function () { + return $this->response( 401, array(), '{"code":"woocommerce_rest_cannot_view"}' ); + }; + add_filter( 'pre_http_request', $filter ); + + $source = new Visualizer_Source_Json( + array( + 'url' => home_url( '/wp-json/wc/v3/reports/sales' ), + 'root' => 'root', + ) + ); + $result = $source->fetch(); + + remove_filter( 'pre_http_request', $filter ); + $this->assertFalse( $result ); + $this->assertNotEmpty( $source->get_error() ); + } + + /** + * A visitor's live refresh of a WooCommerce report chart keeps the stored data when the fetch fails. + */ + public function test_visitor_refresh_keeps_stored_woocommerce_data() { + $admin_id = self::factory()->user->create( array( 'role' => 'administrator' ) ); + wp_set_current_user( $admin_id ); + $content = serialize( array( array( '10.00' ) ) ); + $chart_id = self::factory()->post->create( + array( + 'post_type' => Visualizer_Plugin::CPT_VISUALIZER, + 'post_status' => 'publish', + 'post_content' => wp_slash( $content ), + ) + ); + update_post_meta( $chart_id, Visualizer_Plugin::CF_SOURCE, 'Visualizer_Source_Json' ); + update_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_URL, home_url( '/wp-json/wc/v3/reports/sales' ) ); + update_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_ROOT, 'root' ); + update_post_meta( + $chart_id, + Visualizer_Plugin::CF_SERIES, + array( + array( + 'label' => 'total_sales', + 'type' => 'number', + ), + ) + ); + wp_set_current_user( 0 ); + $filter = function () { + return $this->response( 401, array(), '{"code":"woocommerce_rest_cannot_view"}' ); + }; + add_filter( 'pre_http_request', $filter ); + + apply_filters( 'visualizer_schedule_refresh_chart', get_post( $chart_id ), $chart_id, false ); + + remove_filter( 'pre_http_request', $filter ); + $this->assertSame( $content, get_post_field( 'post_content', $chart_id, 'raw' ) ); + $this->assertNotEmpty( get_post_meta( $chart_id, Visualizer_Plugin::CF_ERROR, true ) ); + } + + /** + * An editable-table chart keeps its stored data when the second WooCommerce read fails. + */ + public function test_refresh_keeps_stored_data_when_second_woocommerce_read_fails() { + $admin_id = self::factory()->user->create( array( 'role' => 'administrator' ) ); + wp_set_current_user( $admin_id ); + $content = serialize( array( array( '10.00' ) ) ); + $chart_id = self::factory()->post->create( + array( + 'post_type' => Visualizer_Plugin::CPT_VISUALIZER, + 'post_status' => 'publish', + 'post_content' => wp_slash( $content ), + ) + ); + update_post_meta( $chart_id, Visualizer_Plugin::CF_SOURCE, 'Visualizer_Source_Json' ); + update_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_URL, home_url( '/wp-json/wc/v3/reports/sales' ) ); + update_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_ROOT, 'root' ); + update_post_meta( $chart_id, Visualizer_Plugin::CF_EDITABLE_TABLE, true ); + update_post_meta( + $chart_id, + Visualizer_Plugin::CF_SERIES, + array( + array( + 'label' => 'total_sales', + 'type' => 'number', + ), + ) + ); + $requests = 0; + $filter = function () use ( &$requests ) { + ++$requests; + return 1 === $requests + ? $this->response( 200, array(), '[{"total_sales":"20.00"}]' ) + : $this->response( 401, array(), '{"code":"woocommerce_rest_cannot_view"}' ); + }; + add_filter( 'pre_http_request', $filter ); + + apply_filters( 'visualizer_schedule_refresh_chart', get_post( $chart_id ), $chart_id, false ); + + remove_filter( 'pre_http_request', $filter ); + $this->assertSame( 2, $requests ); + $this->assertSame( $content, get_post_field( 'post_content', $chart_id, 'raw' ) ); + } + + /** + * Captures the headers generated for a same-site WooCommerce reports request. + * + * @return array + */ + private function capture_woocommerce_request_headers() { + $headers = array(); + $filter = function ( $preempt, $args ) use ( &$headers ) { + $headers = $args['headers']; + return $this->response( 200, array(), '[{"total_sales":"10.00"}]' ); + }; + add_filter( 'pre_http_request', $filter, 10, 2 ); + + $source = new Visualizer_Source_Json( + array( + 'url' => home_url( '/wp-json/wc/v3/reports/sales' ), + 'root' => 'root', + ) + ); + $source->fetchRoots(); + + remove_filter( 'pre_http_request', $filter, 10 ); + return $headers; + } + /** * Builds a WordPress HTTP response fixture. * diff --git a/tests/test-support-hub.php b/tests/test-support-hub.php new file mode 100644 index 000000000..312ca80e8 --- /dev/null +++ b/tests/test-support-hub.php @@ -0,0 +1,84 @@ +assertNotFalse( has_filter( 'visualizer_support_hub_metadata', 'visualizer_support_hub_metadata' ) ); + + $data = apply_filters( 'visualizer_support_hub_metadata', array() ); + + $this->assertIsArray( $data ); + $this->assertArrayHasKey( 'is_pro', $data ); + $this->assertIsBool( $data['is_pro'] ); + $this->assertArrayNotHasKey( 'setup_complete', $data, 'The setup nudge stays off for now.' ); + $this->assertArrayNotHasKey( 'pages', $data, 'Screens are declared through themeisle_internal_page, not pages.' ); + $this->assertArrayNotHasKey( 'primary_color', $data, 'The hub keeps the shared SDK accent.' ); + } + + /** + * Every footer link has an icon, a text and a URL. + */ + public function test_links_are_complete() { + $data = apply_filters( 'visualizer_support_hub_metadata', array() ); + + $this->assertNotEmpty( $data['links'] ); + $this->assertLessThanOrEqual( 6, count( $data['links'] ) ); + foreach ( $data['links'] as $link ) { + $this->assertNotEmpty( $link['icon'] ); + $this->assertNotEmpty( $link['text'] ); + $this->assertNotFalse( filter_var( $link['url'], FILTER_VALIDATE_URL ) ); + } + $this->assertSame( VISUALIZER_MAIN_DOC, $data['links'][0]['url'] ); + } + + /** + * `is_pro` follows the `visualizer_is_pro` filter Pro answers from its license. + */ + public function test_is_pro_follows_the_license_filter() { + $this->assertFalse( apply_filters( 'visualizer_support_hub_metadata', array() )['is_pro'] ); + + add_filter( 'visualizer_is_pro', '__return_true' ); + $this->assertTrue( apply_filters( 'visualizer_support_hub_metadata', array() )['is_pro'] ); + remove_filter( 'visualizer_is_pro', '__return_true' ); + } + + /** + * The Settings screen declares itself a product screen like the Library and Support screens. + */ + public function test_settings_screen_fires_internal_page() { + wp_set_current_user( 1 ); + set_current_screen( 'dashboard' ); + + $admin = Visualizer_Plugin::instance()->getModule( Visualizer_Module_Admin::NAME ); + $admin->registerAdminMenu(); + + $fired = array(); + add_action( + 'themeisle_internal_page', + function ( $product, $page ) use ( &$fired ) { + $fired[] = array( $product, $page ); + }, + 10, + 2 + ); + + // The suffix add_submenu_page() returned, whatever menu state earlier tests left behind. + $admin->enqueueSettingsScripts( get_plugin_page_hookname( 'viz-settings', Visualizer_Plugin::NAME ) ); + $admin->enqueueSettingsScripts( 'edit-post' ); + + $this->assertSame( array( array( VISUALIZER_DIRNAME, 'settings' ) ), $fired ); + } +}