File size: 5,617 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
import isA8CForAgencies from 'calypso/lib/a8c-for-agencies/is-a8c-for-agencies';
import isJetpackCloud from 'calypso/lib/jetpack/is-jetpack-cloud';

const pathIncludes = ( currentPath, term, position ) =>
	currentPath.split( /[/,?]/ )?.[ position ]?.includes( term );

const fragmentIsEqual = ( path, currentPath, position ) =>
	currentPath.split( /[/,?]/ )?.[ position ] === path.split( /[/,?]/ )?.[ position ];

const isManageAllSitesPluginsPath = ( path ) =>
	path.match( /^\/plugins\/(?:manage|active|inactive|updates|scheduled-updates)/ ) !== null;

/**
 * Checks if `currentPath` starts with the first fragment of `path`
 * @param {string} path The path to check against.
 * @param {string} currentPath The current path.
 * @returns {boolean} True if paths match, false otherwise.
 */
export const itemLinkMatches = ( path, currentPath ) => {
	// Accounts for jetpack custom post types, eg portfolio, testimonials.
	if ( pathIncludes( currentPath, 'types', 1 ) ) {
		return fragmentIsEqual( path, currentPath, 2 );
	}
	// Account for taxonomies, eg. tags, categories
	if ( pathIncludes( currentPath, 'taxonomies', 2 ) ) {
		return fragmentIsEqual( path, currentPath, 3 );
	}

	if ( pathIncludes( currentPath, 'people', 1 ) ) {
		if ( pathIncludes( currentPath, 'new', 2 ) ) {
			return fragmentIsEqual( path, currentPath, 2 );
		} else if (
			pathIncludes( currentPath, 'add-subscribers', 2 ) &&
			pathIncludes( path, 'team', 2 )
		) {
			return fragmentIsEqual( path, currentPath, 2 );
		} else if ( pathIncludes( currentPath, 'subscribers', 2 ) && pathIncludes( path, 'team', 2 ) ) {
			return fragmentIsEqual( path, currentPath, 1 );
		}
	}

	if ( pathIncludes( currentPath, 'plugins', 1 ) ) {
		if ( pathIncludes( currentPath, 'scheduled-updates', 2 ) ) {
			return pathIncludes( path, 'plugins', 1 ) && pathIncludes( path, 'scheduled-updates', 2 );
		}

		return pathIncludes( path, 'plugins', 1 ) && fragmentIsEqual( path, currentPath, 1 );
	}

	if ( pathIncludes( currentPath, 'settings', 1 ) ) {
		// Jetpack Cloud uses a simpler /settings/:site pattern, and A4A uses /settings/:tab, for the settings page.
		if ( isJetpackCloud() || isA8CForAgencies() ) {
			return fragmentIsEqual( path, currentPath, 1 );
		}

		/*
		 * If the menu item URL contains 'taxonomies', ignore it when on Settings screens.
		 * Taxonomies are located under the Posts parent menu; this prevents the Posts parent
		 * from being highlighted when Settings screens are active.
		 */
		if ( pathIncludes( currentPath, 'settings', 1 ) && pathIncludes( path, 'taxonomies', 2 ) ) {
			return false;
		}

		// Account for rest of settings pages.
		if ( pathIncludes( currentPath, 'settings', 1 ) ) {
			return fragmentIsEqual( path, currentPath, 2 );
		}
	}

	// All URLs in the Licensing Portal start with 'partner-portal', so we need to compare them at the
	// second position (i.e., compare whatever comes after partner-portal/).
	if ( isJetpackCloud() && pathIncludes( currentPath, 'partner-portal', 1 ) ) {
		const isAssignLicensePath = pathIncludes( currentPath, 'assign-license', 2 );

		// For Assign license path, we will override it to be license path.
		if ( isAssignLicensePath ) {
			return fragmentIsEqual( path, '/partner-portal/licenses', 2 );
		}

		return fragmentIsEqual( path, currentPath, 2 );
	}

	// All URLs in the A4A Purchases start with 'purchases' or 'marketplace' will need to compare at the second position.
	if (
		isA8CForAgencies() &&
		( pathIncludes( currentPath, 'purchases', 1 ) ||
			pathIncludes( currentPath, 'marketplace', 1 ) ||
			pathIncludes( currentPath, 'client', 1 ) )
	) {
		return fragmentIsEqual( path, currentPath, 2 );
	}

	// Account for plugins in all-sites view where '/plugins/manage' isn't a child view of '/plugins'.
	if ( isManageAllSitesPluginsPath( currentPath ) ) {
		return isManageAllSitesPluginsPath( path );
	}

	// For `/store/stats/*` and `/google-my-business/stats/*` paths, show Stats menu as selected.
	if (
		currentPath.startsWith( '/store/stats/' ) ||
		currentPath.startsWith( '/google-my-business/stats/' )
	) {
		return path.startsWith( '/stats/' );
	}

	// For `/theme/*` paths, show Themes menu as selected.
	if ( pathIncludes( currentPath, 'theme', 1 ) ) {
		return pathIncludes( path, 'themes', 1 );
	}

	if (
		currentPath.startsWith( '/earn/jetpack-monetize/' ) ||
		path.startsWith( '/earn/jetpack-monetize/' )
	) {
		return fragmentIsEqual( path, currentPath, 2 );
	}

	if (
		currentPath.startsWith( '/subscribers/jetpack-subscribers/' ) ||
		path.startsWith( '/subscribers/jetpack-subscribers/' )
	) {
		return fragmentIsEqual( path, currentPath, 2 );
	}

	return fragmentIsEqual( path, currentPath, 1 );
};

/**
 * Checks if the current menu item should be selected for a given path.
 * @param {Object} menuItem The current menu item.
 * @param {string} path The path to check against.
 * @param {string} site The current site.
 * @param {boolean} isP2Site True if this site is a P2, false otherwise.
 * @returns {boolean} True if the current menu item should be selected, false otherwise.
 */
export const isItemSelected = ( menuItem, path, site, isP2Site = false ) => {
	let isSelected = menuItem?.url && itemLinkMatches( menuItem.url, path );

	if (
		isSelected ||
		! [ 'sites', 'sites-p2' ].includes( menuItem.slug ) ||
		! site ||
		path.startsWith( '/domains' )
	) {
		return isSelected;
	}

	// Ensure the Sites and P2s icons are selected as appropriate.
	if ( menuItem.slug === 'sites' && ! isP2Site ) {
		isSelected = true;
	} else if ( menuItem.slug === 'sites-p2' && isP2Site ) {
		isSelected = true;
	}

	return isSelected;
};