@@ -4,6 +4,39 @@ function usesBlockTheme() {
44 return ! ! document . querySelector ( 'div.wp-site-blocks' ) ;
55}
66
7+ /**
8+ * Detects the WordPress parent and child theme slugs.
9+ *
10+ * See https://core.trac.wordpress.org/changeset/59698.
11+ *
12+ * @returns {object } Object with fields `theme` and `parent_theme`.
13+ */
14+ function getWordPressTheme ( ) {
15+ const theme = {
16+ theme : 'unknown' ,
17+ parent_theme : '' ,
18+ } ;
19+ try {
20+ const bodyClass = document . body . classList ;
21+
22+ const parentTheme = Array . from ( bodyClass ) . find ( c => c . startsWith ( 'wp-theme-' ) ) ;
23+ const childTheme = Array . from ( bodyClass ) . find ( c => c . startsWith ( 'wp-child-theme-' ) ) ;
24+
25+ if ( childTheme ) {
26+ theme . theme = childTheme ;
27+
28+ // If there is a child theme, there should always be a parent theme.
29+ if ( parentTheme ) {
30+ theme . parent_theme = parentTheme ;
31+ }
32+ } else if ( parentTheme ) {
33+ // There is no child theme, only a parent theme.
34+ theme . theme = parentTheme ;
35+ }
36+ } catch ( e ) { }
37+ return theme ;
38+ }
39+
740// Detects if a WordPress embed block is on the page
841function hasWordPressEmbedBlock ( ) {
942 return ! ! document . querySelector ( 'figure.wp-block-embed' ) ;
@@ -195,6 +228,7 @@ function usesInteractivityAPI() {
195228}
196229
197230const wordpress = {
231+ theme : getWordPressTheme ( ) ,
198232 block_theme : usesBlockTheme ( ) ,
199233 has_embed_block : hasWordPressEmbedBlock ( ) ,
200234 embed_block_count : getWordPressEmbedBlockCounts ( ) ,
0 commit comments