Skip to content

Commit

Permalink
Refactor basic datalayer for simplicity and maintainability
Browse files Browse the repository at this point in the history
  • Loading branch information
TorbenLundsgaard committed Nov 23, 2023
1 parent 2e9e86a commit 85ac08c
Showing 1 changed file with 21 additions and 47 deletions.
68 changes: 21 additions & 47 deletions src/Frontend/BasicDatalayerData.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,32 +58,38 @@ public function get_datalayer_content( array $datalayer ): array {
$datalayer['pagePostType'] = get_post_type();
}

if ( $this->options->get( 'general', 'datalayer_page_type' ) ) {
if ( $set_datalayer_page_type ) {
$datalayer['pageType'] = get_post_type();
}

if ( is_singular() ) {
$datalayer = $this->get_singular_datalayer_content( $datalayer );
}
if ( is_front_page() ) {
if ( $set_datalayer_post_type ) {
$datalayer['pagePostType'] = 'frontpage';
}
if ( $set_datalayer_page_type ) {
$datalayer['pageType'] = 'frontpage';
}
} elseif ( is_home() ) {
if ( $set_datalayer_post_type ) {
$datalayer['pagePostType'] = 'blog_home';
}
if ( $set_datalayer_page_type ) {
$datalayer['pageType'] = 'blog_home';
}
} elseif ( is_search() ) {
$datalayer = $this->get_site_search_datalayer_content( $datalayer, $set_datalayer_post_type, $set_datalayer_page_type );
}

if ( is_archive() || is_post_type_archive() ) {
$datalayer = $this->get_singular_datalayer_content( $datalayer );
} elseif ( is_archive() || is_post_type_archive() ) {
if ( ( is_tax() || is_category() ) && $this->options->get( 'general', 'datalayer_categories' ) ) {
$categories = get_the_category();
foreach ( $categories as $category ) {
$datalayer['pageCategory'][] = $category->slug;
}
}
}

if ( is_search() ) {
$datalayer = $this->get_site_search_datalayer_content( $datalayer, $set_datalayer_post_type, $set_datalayer_page_type );
}

if ( is_front_page() || is_home() ) {
$datalayer = $this->get_frontpage_datalayer_content( $datalayer, $set_datalayer_post_type, $set_datalayer_page_type );
}

if ( is_404() ) {
} elseif ( is_404() ) {
if ( $set_datalayer_post_type ) {
$datalayer['pagePostType'] = '404-error';
}
Expand Down Expand Up @@ -166,38 +172,6 @@ private function get_singular_datalayer_content( array $datalayer ): array {
return $datalayer;
}

/**
* Get the dataLayer data for the frontpage and home page
*
* @param array $datalayer The datalayer.
* @param bool $set_datalayer_post_type Whether the pagePostType property is active.
* @param bool $set_datalayer_page_type Whether the pageType property is active.
*
* @return array
*/
private function get_frontpage_datalayer_content( array $datalayer, bool $set_datalayer_post_type, bool $set_datalayer_page_type ): array {

if ( is_front_page() ) {
if ( $set_datalayer_post_type ) {
$datalayer['pagePostType'] = 'frontpage';
}
if ( $set_datalayer_page_type ) {
$datalayer['pageType'] = 'frontpage';
}
}

if ( ! is_front_page() && is_home() ) {
if ( $set_datalayer_post_type ) {
$datalayer['pagePostType'] = 'blog_home';
}
if ( $set_datalayer_page_type ) {
$datalayer['pageType'] = 'blog_home';
}
}

return $datalayer;
}

/**
* Get the dataLayer data for site search
*
Expand Down

0 comments on commit 85ac08c

Please sign in to comment.