-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathhelpers.js
154 lines (123 loc) · 3.86 KB
/
helpers.js
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
/** @ssr-ready **/
/**
* External dependencies
*/
import analytics from 'lib/analytics';
import titlecase from 'to-title-case';
import page from 'page';
import startsWith from 'lodash/startsWith';
import assign from 'lodash/assign';
import mapValues from 'lodash/mapValues';
/**
* Internal dependencies
*/
import config from 'config';
import route from 'lib/route';
const oldShowcaseUrl = '//wordpress.com/themes/';
export function getSignupUrl( theme ) {
let url = '/start/with-theme?ref=calypshowcase&theme=' + theme.id;
if ( isPremium( theme ) ) {
url += '&premium=true';
}
return url;
}
export function getPreviewUrl( theme, site ) {
if ( site && site.jetpack ) {
return site.options.admin_url + 'customize.php?theme=' + theme.id + '&return=' + encodeURIComponent( window.location );
}
return `${theme.demo_uri}?demo=true&iframe=true&theme_preview=true`;
}
export function getCustomizeUrl( theme, site ) {
if ( ! site ) {
return '/customize/';
}
if ( site.jetpack ) {
return site.options.admin_url + 'customize.php?return=' + encodeURIComponent( window.location ) + ( theme ? '&theme=' + theme.id : '' );
}
return '/customize/' + site.slug + ( theme ? '?theme=' + theme.stylesheet : '' );
}
export function getDetailsUrl( theme, site ) {
if ( site && site.jetpack ) {
return site.options.admin_url + 'themes.php?theme=' + theme.id;
}
let baseUrl = oldShowcaseUrl + theme.id;
if ( config.isEnabled( 'manage/themes/details' ) ) {
baseUrl = `/theme/${ theme.id }/overview`;
}
return baseUrl + ( site ? `/${ site.slug }` : '' );
}
export function getSupportUrl( theme, site ) {
if ( site && site.jetpack ) {
return '//wordpress.org/support/theme/' + theme.id;
}
if ( config.isEnabled( 'manage/themes/details' ) ) {
const sitePart = site ? `/${ site.slug }` : '';
return `/theme/${ theme.id }/setup${ sitePart }`;
}
const sitePart = site ? `${ site.slug }/` : '';
return `${ oldShowcaseUrl }${ sitePart }${ theme.id }/support`;
}
export function getForumUrl( theme ) {
return isPremium( theme ) ? '//premium-themes.forums.wordpress.com/forum/' + theme.id : '//en.forums.wordpress.com/forum/themes';
}
export function getExternalThemesUrl( site ) {
if ( ! site ) {
return oldShowcaseUrl;
}
if ( site.jetpack ) {
return site.options.admin_url + 'theme-install.php';
}
return oldShowcaseUrl + site.slug;
}
export function isPremium( theme ) {
if ( ! theme ) {
return false;
}
if ( theme.stylesheet && startsWith( theme.stylesheet, 'premium/' ) ) {
return true;
}
// The /v1.1/sites/:site_id/themes/mine endpoint (which is used by the
// current-theme reducer, selector, and component) does not return a
// `stylesheet` attribute. However, it does return a `cost` field (which
// contains the correct price even if the user has already purchased that
// theme, or if they have an upgrade that includes all premium themes).
return !! ( theme.cost && theme.cost.number );
}
export function trackClick( componentName, eventName, verb = 'click' ) {
const stat = `${componentName} ${eventName} ${verb}`;
analytics.ga.recordEvent( 'Themes', titlecase( stat ) );
}
export function addTracking( options ) {
return mapValues( options, appendActionTracking );
}
function appendActionTracking( option, name ) {
const { action } = option;
if ( ! action ) {
return option;
}
return assign( {}, option, {
action: t => {
action( t );
trackClick( 'more button', name );
}
} );
}
export function navigateTo( url, external ) {
if ( external ) {
window.open( url );
} else {
page( url );
}
}
export function getAnalyticsData( path, tier, site_id ) {
let basePath = route.sectionify( path );
let analyticsPageTitle = 'Themes';
if ( site_id ) {
basePath = basePath + '/:site_id';
analyticsPageTitle += ' > Single Site';
}
if ( tier ) {
analyticsPageTitle += ` > Type > ${titlecase( tier )}`;
}
return { basePath, analyticsPageTitle };
}