File size: 6,236 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import { isMagnificentLocale, addLocaleToPath } from '@automattic/i18n-utils';
import { mapValues } from 'lodash';
import titlecase from 'to-title-case';
import { THEME_TIERS } from 'calypso/components/theme-tier/constants';
import { gaRecordEvent } from 'calypso/lib/analytics/ga';
import { buildRelativeSearchUrl } from 'calypso/lib/build-url';
import {
	RETIRED_THEME_SLUGS_SET,
	STATIC_FILTERS,
	DEFAULT_STATIC_FILTER,
} from 'calypso/state/themes/constants';

export function trackClick( componentName, eventName, verb = 'click' ) {
	const stat = `${ componentName } ${ eventName } ${ verb }`;
	gaRecordEvent( 'Themes', titlecase( stat ) );
}

export function addTracking( options ) {
	return mapValues( options, appendActionTracking );
}

function appendActionTracking( option, name ) {
	const { action } = option;

	return Object.assign( {}, option, {
		action: ( t, context ) => {
			action && action( t );
			context && trackClick( context, name );
		},
	} );
}

export function getAnalyticsData( path, { filter, vertical, tier, site_id } ) {
	let analyticsPath = '/themes';
	let analyticsPageTitle = 'Themes';

	if ( vertical ) {
		analyticsPath += `/${ vertical }`;
	}

	if ( tier ) {
		analyticsPath += `/${ tier }`;
	}

	if ( filter ) {
		analyticsPath += `/filter/${ filter }`;
	}

	if ( site_id ) {
		analyticsPath += '/:site';
		analyticsPageTitle += ' > Single Site';
	}

	if ( tier ) {
		analyticsPageTitle += ` > Type > ${ titlecase( tier ) }`;
	}

	return { analyticsPath, analyticsPageTitle };
}

export function localizeThemesPath( path, locale, isLoggedOut = true ) {
	const shouldLocalizePath = isLoggedOut && isMagnificentLocale( locale );

	if ( ! shouldLocalizePath ) {
		return path;
	}

	if ( path.startsWith( '/theme' ) ) {
		return `/${ locale }${ path }`;
	}

	if ( path.startsWith( '/start/with-theme' ) ) {
		return addLocaleToPath( path, locale );
	}

	return path;
}

export function addOptionsToGetUrl( options, { tabFilter, tierFilter, styleVariationSlug } ) {
	return mapValues( options, ( option ) =>
		Object.assign( {}, option, {
			...( option.getUrl && {
				getUrl: ( t ) => option.getUrl( t, { tabFilter, tierFilter, styleVariationSlug } ),
			} ),
		} )
	);
}

/**
 * Creates the billing product slug for a given theme ID.
 * @param themeId Theme ID
 * @returns string
 */
export function marketplaceThemeBillingProductSlug( themeId ) {
	return `wp-mp-theme-${ themeId }`;
}

export function getSubjectsFromTermTable( filterToTermTable ) {
	return Object.keys( filterToTermTable )
		.filter( ( key ) => key.indexOf( 'subject:' ) !== -1 )
		.reduce( ( obj, key ) => {
			obj[ key ] = filterToTermTable[ key ];
			return obj;
		}, {} );
}

/**
 * Interlace WP.com themes with WP.org themes according to the logic below:
 * - WP.org themes are only included if there is a search term.
 * - If the search term has an exact match (either a WP.com or a WP.org theme), that theme is the first result.
 * - WP.com themes are prioritized over WP.org themes.
 * - Retired WP.org themes or duplicate WP.org themes (those that are also WP.com themes) are excluded.
 * - WP.org block themes are prioritized over WP.org classic themes.
 * @param wpComThemes List of WP.com themes.
 * @param wpOrgThemes List of WP.org themes.
 * @param searchTerm Search term.
 * @param isLastPage Whether the list of WP.com themes has reached the last page.
 */
export function interlaceThemes( wpComThemes, wpOrgThemes, searchTerm, isLastPage ) {
	const isMatchingTheme = ( theme ) => {
		if ( ! searchTerm || theme.retired ) {
			return false;
		}

		return (
			theme.name?.toLowerCase?.() === searchTerm?.toLowerCase() ||
			theme.id?.toLowerCase?.() === searchTerm?.toLowerCase()
		);
	};

	const includeWpOrgThemes = !! searchTerm;
	const wpComThemesSlugs = wpComThemes.map( ( theme ) => theme.id );
	const validWpOrgThemes = includeWpOrgThemes
		? wpOrgThemes.filter(
				( theme ) =>
					! wpComThemesSlugs.includes( theme?.id?.toLowerCase() ) && // Avoid duplicate themes. Some free themes are available in both wpcom and wporg.
					! RETIRED_THEME_SLUGS_SET.has( theme?.id?.toLowerCase() ) // Avoid retired themes.
		  )
		: [];

	const interlacedThemes = [];

	// 1. Exact match.
	const matchingTheme =
		wpComThemes.find( isMatchingTheme ) || validWpOrgThemes.find( isMatchingTheme );
	if ( matchingTheme ) {
		interlacedThemes.push( matchingTheme );
	}

	// 2. WP.com themes.
	const restWpComThemes = matchingTheme
		? wpComThemes.filter( ( theme ) => theme.id !== matchingTheme.id )
		: wpComThemes;

	// The themes endpoint returns retired themes when search term exists.
	// See: https://github.com/Automattic/wp-calypso/pull/78231
	interlacedThemes.push(
		...( searchTerm ? restWpComThemes.filter( ( theme ) => ! theme.retired ) : restWpComThemes )
	);

	// 3. WP.org themes (only if the list of WP.com themes has reached the last page).
	if ( isLastPage ) {
		interlacedThemes.push(
			...validWpOrgThemes.filter( ( theme ) => theme.id !== matchingTheme?.id )
		);
	}

	return interlacedThemes;
}

export function getTierRouteParam() {
	return `:tier(${ Object.keys( THEME_TIERS ).join( '|' ) })?`;
}

export function isStaticFilter( currentFilter ) {
	return Object.values( STATIC_FILTERS ).includes( currentFilter );
}

export function constructThemeShowcaseUrl( {
	category,
	vertical,
	tier,
	filter,
	siteSlug,
	search,
	locale,
	isLoggedIn,
	isCollectionView,
} ) {
	const siteIdSection = siteSlug ? `/${ siteSlug }` : '';
	const categorySection = category && category !== DEFAULT_STATIC_FILTER ? `/${ category }` : '';
	const verticalSection = vertical ? `/${ vertical }` : '';
	const tierSection = tier && tier !== 'all' ? `/${ tier }` : '';

	let filterSection = filter ? `/filter/${ filter }` : '';
	filterSection = filterSection.replace( /\s/g, '+' );

	const collectionSection = isCollectionView ? `/collection` : '';

	let url = `/themes${ categorySection }${ verticalSection }${ tierSection }${ filterSection }${ collectionSection }${ siteIdSection }`;

	url = localizeThemesPath( url, locale, ! isLoggedIn );

	return buildRelativeSearchUrl( url, search );
}

export function shouldSelectSite( { isLoggedIn, siteCount, siteId } ) {
	return isLoggedIn && ! siteId && siteCount > 1;
}