Skip to content

Commit

Permalink
Detect WordPress content type (#96)
Browse files Browse the repository at this point in the history
* Add function to detect WordPress content type, including post type or taxonomy.

* Add function doc block.

* Rename contentType to template.

* Use snake case for consistency.

---------

Co-authored-by: Barry Pollard <[email protected]>
  • Loading branch information
felixarntz and tunetheweb authored Oct 25, 2023
1 parent 944dbc3 commit 6ed11fb
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions dist/cms.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,86 @@ function getWordPressScripts() {
return entries;
}

/**
* Detects the type of WordPress content for the current document.
*
* @returns {object} Object with fields `template`, `post_type`, and `taxonomy`.
*/
function getWordPressContentType() {
const content = {
template: 'unknown',
post_type: '',
taxonomy: '',
};
try {
const bodyClass = document.body.classList;

if ( bodyClass.contains( 'home' ) ) {
/*
* The home page, either containing the blog,
* or a "static front page".
*/
if ( bodyClass.contains( 'blog' ) ) {
content.template = 'home-blog';
content.post_type = 'post';
} else if ( bodyClass.contains( 'page' ) ) {
content.template = 'home-page';
content.post_type = 'page';
}
} else if ( bodyClass.contains( 'blog' ) ) {
/*
* The blog, separate from the home page.
* Only relevant if the home page contains a "static front page".
*/
content.template = 'blog';
content.post_type = 'post';
} else if ( bodyClass.contains( 'singular' ) ) {
/*
* Any singular content (other than the "static front page").
* Either a page, or content of another post type.
*/
content.template = 'singular';
if ( bodyClass.contains( 'page' ) ) {
content.post_type = 'page';
} else if ( bodyClass.contains( 'single' ) ) {
const postTypeClass = Array.from( bodyClass ).find( c => c.startsWith( 'single-' ) && ! c.startsWith( 'single-format-' ) );
if ( postTypeClass ) {
content.post_type = postTypeClass.replace( 'single-', '' );
}
}
} else if ( bodyClass.contains( 'archive' ) ) {
/*
* Any archive (other than the blog).
* Either a category archive, a tag archive, a custom post type archive,
* or a custom taxonomy archive.
*/
content.template = 'archive';
if ( bodyClass.contains( 'category' ) ) {
content.taxonomy = 'category';
} else if ( bodyClass.contains( 'tag' ) ) {
content.taxonomy = 'tag';
} else if ( bodyClass.contains( 'post-type-archive' ) ) {
const postTypeClass = Array.from( bodyClass ).find( c => c.startsWith( 'post-type-archive-' ) );
if ( postTypeClass ) {
content.post_type = postTypeClass.replace( 'post-type-archive-', '' );
}
} else {
const taxonomyClass = Array.from( bodyClass ).find( c => c.startsWith( 'tax-' ) );
if ( taxonomyClass ) {
content.taxonomy = taxonomyClass.replace( 'tax-', '' );
}
}
}
} catch ( e ) {}
return content;
}

const wordpress = {
block_theme: usesBlockTheme(),
has_embed_block: hasWordPressEmbedBlock(),
embed_block_count: getWordPressEmbedBlockCounts(),
scripts: getWordPressScripts(),
content_type: getWordPressContentType(),
};

return {
Expand Down

0 comments on commit 6ed11fb

Please sign in to comment.