-
Notifications
You must be signed in to change notification settings - Fork 384
/
Copy pathget-sources-from-scannable-urls.js
61 lines (53 loc) · 1.9 KB
/
get-sources-from-scannable-urls.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
/**
* Internal dependencies
*/
import { getPluginSlugFromFile } from '../../common/helpers/get-plugin-slug-from-file';
/**
* From an array of scannable URLs, get plugin and theme slugs along with URLs for which AMP validation errors occur.
*
* See the corresponding PHP logic in `\AMP_Validated_URL_Post_Type::render_sources_column()`.
*
* @param {Array} scannableUrls Array of scannable URLs.
* @param {Object} options Additional options object.
* @param {boolean} options.useAmpUrls Whether to return `amp_url` instead of the regular URL in the lists of sources.
* @return {Object} An object consisting of `plugins` and `themes` arrays.
*/
export function getSourcesFromScannableUrls( scannableUrls = [], { useAmpUrls = false } = {} ) {
const plugins = new Map();
const themes = new Map();
for ( const scannableUrl of scannableUrls ) {
const {
amp_url: ampUrl,
url,
validation_errors: validationErrors,
} = scannableUrl;
if ( ! validationErrors?.length ) {
continue;
}
for ( const validationError of validationErrors ) {
for ( const source of validationError.sources ) {
if ( source.type === 'plugin' ) {
const pluginSlug = getPluginSlugFromFile( source.name );
if ( 'gutenberg' === pluginSlug && validationError.sources.length > 1 ) {
continue;
}
plugins.set(
pluginSlug,
new Set( [ ...( plugins.get( pluginSlug ) || [] ), useAmpUrls ? ampUrl : url ] ),
);
} else if ( source.type === 'theme' ) {
themes.set(
source.name,
new Set( [ ...( themes.get( source.name ) || [] ), useAmpUrls ? ampUrl : url ] ),
);
}
}
}
}
// Skip including AMP in the summary, since AMP is like core.
plugins.delete( 'amp' );
return {
plugins: [ ...plugins ].map( ( [ slug, urls ] ) => ( { slug, urls: [ ...urls ] } ) ),
themes: [ ...themes ].map( ( [ slug, urls ] ) => ( { slug, urls: [ ...urls ] } ) ),
};
}