diff --git a/readme.txt b/readme.txt index 0ef1d48..a4d2731 100644 --- a/readme.txt +++ b/readme.txt @@ -96,6 +96,7 @@ Find out about what's new in our [our release post](https://gtmkit.com/gtm-kit-1 #### Enhancements: * Added option to specify the Google Tag Manager environment. +* Performance optimization through optimized database queries and general code improvements. #### Bugfixes: * Add missing _sbp cookie in the cookie keeper. diff --git a/src/Frontend/Frontend.php b/src/Frontend/Frontend.php index df939e0..6e16561 100644 --- a/src/Frontend/Frontend.php +++ b/src/Frontend/Frontend.php @@ -45,8 +45,8 @@ public function __construct( Options $options ) { */ public static function register( Options $options ): void { $page = new Frontend( $options ); - $container_active = Options::init()->get( 'general', 'container_active' ); - $noscript_implementation = Options::init()->get( 'general', 'noscript_implementation' ); + $container_active = $options->get( 'general', 'container_active' ); + $noscript_implementation = $options->get( 'general', 'noscript_implementation' ); if ( empty( $options->get( 'general', 'just_the_container' ) ) ) { add_action( 'wp_enqueue_scripts', [ $page, 'enqueue_settings_and_data_script' ], 1, 0 ); @@ -55,7 +55,7 @@ public static function register( Options $options ): void { if ( $container_active ) { add_action( 'wp_enqueue_scripts', [ $page, 'enqueue_header_script' ] ); - } elseif ( Options::init()->get( 'general', 'console_log' ) ) { + } elseif ( $options->get( 'general', 'console_log' ) ) { add_action( 'wp_head', [ $page, 'container_disabled' ] ); } @@ -76,7 +76,7 @@ public static function register( Options $options ): void { public function enqueue_settings_and_data_script(): void { $settings = [ 'datalayer_name' => $this->datalayer_name, - 'console_log' => Options::init()->get( 'general', 'console_log' ), + 'console_log' => $this->options->get( 'general', 'console_log' ), ]; ob_start(); @@ -125,13 +125,13 @@ public function enqueue_datalayer_content(): void { */ public function enqueue_header_script(): void { - $gtm_id = Options::init()->get( 'general', 'gtm_id' ); + $gtm_id = $this->options->get( 'general', 'gtm_id' ); if ( empty( $gtm_id ) ) { return; } - $script_implementation = (int) Options::init()->get( 'general', 'script_implementation' ); + $script_implementation = (int) $this->options->get( 'general', 'script_implementation' ); ob_start(); @@ -171,10 +171,10 @@ function (cb) { * @param string $gtm_id The GTM container ID. */ public function get_gtm_script( string $gtm_id ): void { - $domain = Options::init()->get( 'general', 'sgtm_domain' ) ? Options::init()->get( 'general', 'sgtm_domain' ) : 'www.googletagmanager.com'; - $loader = Options::init()->get( 'general', 'sgtm_container_identifier' ) ? Options::init()->get( 'general', 'sgtm_container_identifier' ) : 'gtm'; + $domain = $this->options->get( 'general', 'sgtm_domain' ) ? $this->options->get( 'general', 'sgtm_domain' ) : 'www.googletagmanager.com'; + $loader = $this->options->get( 'general', 'sgtm_container_identifier' ) ? $this->options->get( 'general', 'sgtm_container_identifier' ) : 'gtm'; - if ( Options::init()->get( 'general', 'sgtm_cookie_keeper' ) ) { + if ( $this->options->get( 'general', 'sgtm_cookie_keeper' ) ) { echo "/* Google Tag Manager with Stape.io Cookie Keeper */\n"; echo '!function(){"use strict";function e(e,t,o){return void 0===t&&(t=""),"cookie"===e?function(e){for(var t=0,o=document.cookie.split(";");t=16.4)?e(l,"_sbp",""):void 0}catch(e){console.error(e)}var g=t;g[n]=g[n]||[];g[n].push({"gtm.start":(new Date).getTime(),event:"gtm.js"});var m=o.getElementsByTagName(r)[0],T=v?"&bi="+encodeURIComponent(v):"",_=o.createElement(r),f=v?"kp"+c:c,dl=n!="dataLayer"?"&l="+n:"";_.async=!0,_.src="https://' . esc_attr( $domain ) . '/"+f+".js?id=' . esc_attr( $gtm_id ) . '"+dl+T' . ( ( ! empty( Options::init()->get( 'general', 'gtm_auth' ) ) && ! empty( Options::init()->get( 'general', 'gtm_preview' ) ) ) ? "+'>m_auth=" . esc_attr( Options::init()->get( 'general', 'gtm_auth' ) ) . '>m_preview=' . esc_attr( Options::init()->get( 'general', 'gtm_preview' ) ) . ">m_cookies_win=x'" : '' ) . ',null===(d=m.parentNode)||void 0===d||d.insertBefore(_,m)}(window,document,"script","' . esc_js( $this->datalayer_name ) . '",0,0,"' . esc_attr( $loader ) . '","cookie")}();'; echo "\n/* End Google Tag Manager */\n"; @@ -184,7 +184,7 @@ public function get_gtm_script( string $gtm_id ): void { echo "new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],\n"; echo "j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=\n"; echo "'https://" . esc_attr( $domain ) . '/' . esc_attr( $loader ) . ".js?id='+i+dl"; - echo ( ! empty( Options::init()->get( 'general', 'gtm_auth' ) ) && ! empty( Options::init()->get( 'general', 'gtm_preview' ) ) ) ? "+'>m_auth=" . esc_attr( Options::init()->get( 'general', 'gtm_auth' ) ) . '>m_preview=' . esc_attr( Options::init()->get( 'general', 'gtm_preview' ) ) . ">m_cookies_win=x'" : ''; + echo ( ! empty( $this->options->get( 'general', 'gtm_auth' ) ) && ! empty( $this->options->get( 'general', 'gtm_preview' ) ) ) ? "+'>m_auth=" . esc_attr( $this->options->get( 'general', 'gtm_auth' ) ) . '>m_preview=' . esc_attr( $this->options->get( 'general', 'gtm_preview' ) ) . ">m_cookies_win=x'" : ''; echo ";f.parentNode.insertBefore(j,f);\n"; echo "})(window,document,'script','" . esc_js( $this->datalayer_name ) . "','" . esc_attr( $gtm_id ) . "');\n"; echo "/* End Google Tag Manager */\n"; @@ -231,7 +231,7 @@ public static function get_body_script(): void { /** * Console warning */ - public static function container_disabled(): void { + public function container_disabled(): void { echo ''; } @@ -259,7 +259,7 @@ public function wp_rocket_exclude_javascript( array $pattern ): array { */ public function dns_prefetch( array $hints, string $relation_type ): array { - $domain = Options::init()->get( 'general', 'sgtm_domain' ) ? Options::init()->get( 'general', 'sgtm_domain' ) : 'www.googletagmanager.com'; + $domain = $this->options->get( 'general', 'sgtm_domain' ) ? $this->options->get( 'general', 'sgtm_domain' ) : 'www.googletagmanager.com'; if ( 'dns-prefetch' === $relation_type ) { $hints[] = '//' . $domain; diff --git a/src/Integration/AbstractEcommerce.php b/src/Integration/AbstractEcommerce.php index 6d31f05..0b2fabe 100644 --- a/src/Integration/AbstractEcommerce.php +++ b/src/Integration/AbstractEcommerce.php @@ -120,48 +120,27 @@ public function get_primary_product_category( int $product_id, string $taxonomy * @return array The category path elements as an array. */ public function get_category_breadcrumb( int $category_id, string $taxonomy ): array { - $category_hierarchy = []; - - $category = get_term( $category_id, $taxonomy ); + static $categories = []; - if ( ! $category ) { - return $category_hierarchy; + if ( isset( $categories[ $category_id ] ) ) { + return $categories[ $category_id ]; } - $parents = get_ancestors( $category_id, $taxonomy, 'taxonomy' ); - - array_unshift( $parents, $category_id ); - - foreach ( array_reverse( $parents ) as $category_id ) { - $parent = get_term( $category_id, $taxonomy ); - $category_hierarchy[] = $parent->name; - + $category_hierarchy = []; + $category = get_term( $category_id, $taxonomy ); + if ( $category ) { + $parents = get_ancestors( $category_id, $taxonomy, 'taxonomy' ); + array_unshift( $parents, $category_id ); + foreach ( $parents as $category_id ) { + $parent = get_term( $category_id, $taxonomy ); + if ( $parent ) { + array_unshift( $category_hierarchy, $parent->name ); + } + } } + $categories[ $category_id ] = $category_hierarchy; - return $category_hierarchy; - } - - /** - * Get product term value. - * - * @param int $product_id A product ID. - * @param string $taxonomy The taxonomy slug. - * - * @return string Returns the first assigned taxonomy value. - */ - public function get_product_term( int $product_id, string $taxonomy ): string { - - $product_terms = wp_get_post_terms( - $product_id, - $taxonomy, - [ - 'orderby' => 'parent', - 'order' => 'ASC', - ] - ); - - return ( is_array( $product_terms ) && count( $product_terms ) ) ? $product_terms[0]->name : ''; - } + return $category_hierarchy; } /** * Prefix an item ID diff --git a/src/Integration/WooCommerce.php b/src/Integration/WooCommerce.php index 2de5bc8..a021b45 100644 --- a/src/Integration/WooCommerce.php +++ b/src/Integration/WooCommerce.php @@ -239,10 +239,10 @@ public function get_global_settings( array $global_settings ): array { $global_settings['wc']['use_sku'] = (bool) $this->options->get( 'integrations', 'woocommerce_use_sku' ); $global_settings['wc']['pid_prefix'] = $this->prefix_item_id(); - $global_settings['wc']['add_shipping_info']['config'] = (int) Options::init()->get( 'integrations', 'woocommerce_shipping_info' ); - $global_settings['wc']['add_payment_info']['config'] = (int) Options::init()->get( 'integrations', 'woocommerce_payment_info' ); - $global_settings['wc']['view_item']['config'] = (int) Options::init()->get( 'integrations', 'woocommerce_variable_product_tracking' ); - $global_settings['wc']['view_item_list']['config'] = (int) Options::init()->get( 'integrations', 'woocommerce_view_item_list_limit' ); + $global_settings['wc']['add_shipping_info']['config'] = (int) $this->options->get( 'integrations', 'woocommerce_shipping_info' ); + $global_settings['wc']['add_payment_info']['config'] = (int) $this->options->get( 'integrations', 'woocommerce_payment_info' ); + $global_settings['wc']['view_item']['config'] = (int) $this->options->get( 'integrations', 'woocommerce_variable_product_tracking' ); + $global_settings['wc']['view_item_list']['config'] = (int) $this->options->get( 'integrations', 'woocommerce_view_item_list_limit' ); $global_settings['wc']['text'] = [ 'wp-block-handpicked-products' => __( 'Handpicked Products', 'gtm-kit' ), 'wp-block-product-best-sellers' => __( 'Best Sellers', 'gtm-kit' ), @@ -360,7 +360,7 @@ public function get_datalayer_content_product_page( array $data_layer ): array { $data_layer['pageType'] = 'product-page'; } - if ( $product->get_type() === 'variable' && (int) Options::init()->get( 'integrations', 'woocommerce_variable_product_tracking' ) === 2 ) { + if ( $product->get_type() === 'variable' && (int) $this->options->get( 'integrations', 'woocommerce_variable_product_tracking' ) === 2 ) { return $data_layer; } @@ -657,10 +657,7 @@ public function get_item_data( WC_Product $product, array $additional_item_attri ]; if ( $this->options->get( 'integrations', 'woocommerce_brand' ) ) { - $item_data['item_brand'] = $this->get_product_term( - $product_id_to_query, - $this->options->get( 'integrations', 'woocommerce_brand' ) - ); + $item_data['item_brand'] = $product->get_attribute( $this->options->get( 'integrations', 'woocommerce_brand' ) ); } if ( $this->options->get( 'integrations', 'woocommerce_google_business_vertical' ) ) { @@ -992,7 +989,7 @@ public function cart_item_remove_link( string $woocommerce_cart_item_remove_link * @return string */ public function prefix_item_id( string $item_id = '' ): string { - return Options::init()->get( 'integrations', 'woocommerce_product_id_prefix' ) . $item_id; + return $this->options->get( 'integrations', 'woocommerce_product_id_prefix' ) . $item_id; } /**