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 24, 2023
1 parent 99ccae9 commit c8c467d
Showing 1 changed file with 45 additions and 42 deletions.
87 changes: 45 additions & 42 deletions src/Frontend/BasicDatalayerData.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,29 @@ final class BasicDatalayerData {
*/
protected $options;

/**
* Set datalayer post type.
*
* @var bool
*/
protected $set_datalayer_post_type;

/**
* Set datalayer page type.
*
* @var bool
*/
protected $set_datalayer_page_type;

/**
* Constructor.
*
* @param Options $options An instance of Options.
*/
public function __construct( Options $options ) {
$this->options = $options;
$this->options = $options;
$this->set_datalayer_post_type = (bool) $this->options->get( 'general', 'datalayer_post_type' );
$this->set_datalayer_page_type = (bool) $this->options->get( 'general', 'datalayer_page_type' );
}

/**
Expand All @@ -51,36 +67,17 @@ public static function register( Options $options ): void {
*/
public function get_datalayer_content( array $datalayer ): array {

$set_datalayer_post_type = (bool) $this->options->get( 'general', 'datalayer_post_type' );
$set_datalayer_page_type = (bool) $this->options->get( 'general', 'datalayer_page_type' );

if ( $set_datalayer_post_type ) {
$datalayer['pagePostType'] = get_post_type();
}

if ( $set_datalayer_page_type ) {
$datalayer['pageType'] = get_post_type();
}
$datalayer = $this->set_post_and_page_types( $datalayer, get_post_type() );

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

$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' ) ) {
Expand All @@ -90,12 +87,28 @@ public function get_datalayer_content( array $datalayer ): array {
}
}
} elseif ( is_404() ) {
if ( $set_datalayer_post_type ) {
$datalayer['pagePostType'] = '404-error';
}
if ( $set_datalayer_page_type ) {
$datalayer['pageType'] = '404-error';
}
$datalayer = $this->set_post_and_page_types( $datalayer, '404-error' );
}

return $datalayer;
}

/**
* Set post and page types in the datalayer
*
* @param array $datalayer The datalayer.
* @param string $post_type The post type.
* @param string $page_type The page type.
*
* @return array
*/
private function set_post_and_page_types( array $datalayer, string $post_type, string $page_type = '' ): array {

if ( $this->set_datalayer_post_type ) {
$datalayer['pagePostType'] = $post_type;
}
if ( $this->set_datalayer_page_type ) {
$datalayer['pageType'] = ( empty( $page_type ) ) ? $post_type : $page_type;
}

return $datalayer;
Expand Down Expand Up @@ -176,20 +189,10 @@ private function get_singular_datalayer_content( array $datalayer ): array {
* Get the dataLayer data for site search
*
* @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_site_search_datalayer_content( array $datalayer, bool $set_datalayer_post_type, bool $set_datalayer_page_type ): array {

if ( $set_datalayer_post_type ) {
$datalayer['pagePostType'] = 'search-results';
}
if ( $set_datalayer_page_type ) {
$datalayer['pageType'] = 'search-results';
}

private function get_site_search_datalayer_content( array $datalayer ): array {
global $wp_query;

$datalayer['siteSearchQuery'] = get_search_query();
Expand Down

0 comments on commit c8c467d

Please sign in to comment.