/** * WordPress dependencies */ import { _x, sprintf } from '@safe-wordpress/i18n'; /** * External dependencies */ import type { CustomPhpScopeRule, Experiment, Maybe, ScopeRule, ScopeType, } from '@nab/types'; export function getScopeExplanation( scopeSupport: ScopeType, rules: ReadonlyArray< ScopeRule >, status: Experiment[ 'status' ] ): string { switch ( scopeSupport ) { case 'tested-post-with-consistency': return getConsistencyExplanation( rules, status ); case 'tested-product-with-consistency': return getProductConsistencyExplanation( rules, status ); case 'tested-url-with-query-args': return getTestedUrlWithQueryArgsExplanation( rules, status ); case 'urls-with-tested-post': return getCustomWithTestedPostExplanation( rules, status ); case 'urls': return getCustomExplanation( rules, status ); case 'urls-or-php': return getPhpExplanation( rules, status ); } } function getConsistencyExplanation( rules: ReadonlyArray< ScopeRule >, status: Experiment[ 'status' ] ) { if ( 'running' === status ) { if ( ! rules.length ) { return _x( 'Variants are visible on the tested element’s page. All other pages also show the appropriate variant of this test whenever the tested element appears on them.', 'text', 'nelio-ab-testing' ); } return _x( 'Variants are visible only on the tested element’s page.', 'text', 'nelio-ab-testing' ); } if ( ! rules.length ) { return _x( 'Variants were visible on the tested element’s page. All other pages also showed the appropriate variant of this test whenever the tested element appeared on them.', 'text', 'nelio-ab-testing' ); } return _x( 'Variants were visible only on the tested element’s page.', 'text', 'nelio-ab-testing' ); } function getProductConsistencyExplanation( rules: ReadonlyArray< ScopeRule >, status: Experiment[ 'status' ] ) { if ( 'running' === status ) { if ( ! rules.length ) { return _x( 'Variants are visible everywhere on your store: the product’s page, shopping carts, the checkout page, on a “related products” section, etc.', 'text', 'nelio-ab-testing' ); } return _x( 'Variants are visible only on the tested product’s page.', 'text', 'nelio-ab-testing' ); } if ( ! rules.length ) { return _x( 'Variants were visible everywhere on your store: the product’s page, shopping carts, the checkout page, on a “related products” section, etc.', 'text', 'nelio-ab-testing' ); } return _x( 'Variants were visible only on the tested product’s page.', 'text', 'nelio-ab-testing' ); } function getTestedUrlWithQueryArgsExplanation( rules: ReadonlyArray< ScopeRule >, status: Experiment[ 'status' ] ) { if ( 'running' === status ) { if ( ! rules.length ) { return _x( 'Test is active on all the tested URLs.', 'text', 'nelio-ab-testing' ); } if ( 1 === rules.length ) { return _x( 'Test is active on a tested URL if the query arguments in said URL satisfy all the following conditions:', 'text', 'nelio-ab-testing' ); } return _x( 'Test is active on a tested URL if the query arguments in said URL satisfy all the conditions in any of the following groups:', 'text', 'nelio-ab-testing' ); } if ( ! rules.length ) { return _x( 'Test was active on all the tested URLs.', 'text', 'nelio-ab-testing' ); } if ( 1 === rules.length ) { return _x( 'Test was active on a tested URL if the query arguments in said URL satisfied all the following conditions:', 'text', 'nelio-ab-testing' ); } return _x( 'Test was active on a tested URL if the query arguments in said URL satisfied all the conditions in any of the following groups:', 'text', 'nelio-ab-testing' ); } function getCustomWithTestedPostExplanation( rules: ReadonlyArray< ScopeRule >, status: Experiment[ 'status' ] ) { if ( 'running' === status ) { if ( ! rules.length ) { return _x( 'Variants are visible on all the pages of your website, including that of the tested element.', 'text', 'nelio-ab-testing' ); } return _x( 'Variants are visible on the tested element’s page, as well as pages matching one of the following criteria.', 'text', 'nelio-ab-testing' ); } if ( ! rules.length ) { return _x( 'Variants were visible on all the pages of your website, including that of the tested element.', 'text', 'nelio-ab-testing' ); } return _x( 'Variants were visible on the tested element’s page, as well as pages matching one of the following criteria.', 'text', 'nelio-ab-testing' ); } function getCustomExplanation( rules: ReadonlyArray< ScopeRule >, status: Experiment[ 'status' ] ) { if ( 'running' === status ) { if ( ! rules.length ) { return _x( 'Variants are visible on all the pages of your website.', 'text', 'nelio-ab-testing' ); } return _x( 'Variants are visible only on pages matching one of the following criteria:', 'text', 'nelio-ab-testing' ); } if ( ! rules.length ) { return _x( 'Variants were visible on all the pages of your website.', 'text', 'nelio-ab-testing' ); } return _x( 'Variants were visible only on pages matching one of the following criteria:', 'text', 'nelio-ab-testing' ); } function getPhpExplanation( rules: ReadonlyArray< ScopeRule >, status: Experiment[ 'status' ] ) { const isCustomPhpScopeRule = ( r: Maybe< ScopeRule > ): r is CustomPhpScopeRule => 'php-snippet' === r?.attributes.type; const [ firstRule ] = rules; if ( ! isCustomPhpScopeRule( firstRule ) ) { return ''; } const PRIORITY_TO_HOOK = { high: 'plugins_loaded', mid: 'parse_query', low: 'wp', }; return 'running' === status ? sprintf( /* translators: %s: Hook name. */ _x( 'This test applies to pages that satisfy the following function, evaluated on “%s”:', 'command', 'nelio-ab-testing' ), PRIORITY_TO_HOOK[ firstRule.attributes.value.priority ] ) : sprintf( /* translators: %s: Hook name. */ _x( 'This test applied to pages that satisfied the following function, evaluated on “%s”:', 'command', 'nelio-ab-testing' ), PRIORITY_TO_HOOK[ firstRule.attributes.value.priority ] ); }