Skip to content

Commit

Permalink
Merge pull request #75 from dxw/fix/check-variale-before-trim
Browse files Browse the repository at this point in the history
Fix / check variable before trim
  • Loading branch information
sbrody authored Dec 12, 2024
2 parents 5924a78 + f3201ca commit 849e23f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.5.5] - 2024-12-12

### Changed

- Check variable before trim to prevent PHP warning

## [1.5.4] - 2024-11-28

### Added
Expand Down
2 changes: 1 addition & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* Plugin URI: https://github.com/dxw/analytics-with-consent
* Description: Google Analytics + CIVIC Cookie Control
* Author: dxw
* Version: 1.5.4
* Version: 1.5.5
* Network: True
*/

Expand Down
28 changes: 14 additions & 14 deletions src/Scripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ public function addActionLinks(array $links): array

public function enqueueScripts(): void
{
$apiKey = trim(get_field('civic_cookie_control_api_key', 'option'));
$productType = trim(get_field('civic_cookie_control_product_type', 'option'));
$googleAnalyticsId = trim(get_field('google_analytics_id', 'option'));
$ga4Id = trim(get_field('ga_4_id', 'option'));
$gtmId = trim(get_field('google_analytics_gtm', 'option'));
$hotjarId = trim(get_field('hotjar_id', 'option'));
$apiKey = trim(get_field('civic_cookie_control_api_key', 'option') ?? '');
$productType = trim(get_field('civic_cookie_control_product_type', 'option') ?? '');
$googleAnalyticsId = trim(get_field('google_analytics_id', 'option') ?? '');
$ga4Id = trim(get_field('ga_4_id', 'option') ?? '');
$gtmId = trim(get_field('google_analytics_gtm', 'option') ?? '');
$hotjarId = trim(get_field('hotjar_id', 'option') ?? '');
if ($apiKey && $productType) {
wp_enqueue_script('civicCookieControl', 'https://cc.cdn.civiccomputing.com/9/cookieControl-9.x.min.js');
wp_enqueue_script('civicCookieControlDefaultAnalytics', plugins_url('/assets/js/analytics.js', dirname(__FILE__)), ['civicCookieControl']);
Expand All @@ -49,19 +49,19 @@ public function enqueueStyles(): void

public function addGA4(): void
{
$apiKey = trim(get_field('civic_cookie_control_api_key', 'option'));
$productType = trim(get_field('civic_cookie_control_product_type', 'option'));
$ga4Id = trim(get_field('ga_4_id', 'option'));
$apiKey = trim(get_field('civic_cookie_control_api_key', 'option') ?? '');
$productType = trim(get_field('civic_cookie_control_product_type', 'option') ?? '');
$ga4Id = trim(get_field('ga_4_id', 'option') ?? '');
if ($apiKey && $productType && $ga4Id) {
printf('<script async id="awc_gtag" src="https://www.googletagmanager.com/gtag/js?id=%s"></script>', esc_attr($ga4Id));
}
}

public function addGTM(): void
{
$apiKey = trim(get_field('civic_cookie_control_api_key', 'option'));
$productType = trim(get_field('civic_cookie_control_product_type', 'option'));
$gtmId = trim(get_field('google_analytics_gtm', 'option'));
$apiKey = trim(get_field('civic_cookie_control_api_key', 'option') ?? '');
$productType = trim(get_field('civic_cookie_control_product_type', 'option') ?? '');
$gtmId = trim(get_field('google_analytics_gtm', 'option') ?? '');
if ($apiKey && $productType && $gtmId) {
printf("<script>window.dataLayer = window.dataLayer || []; (function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start': new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0], j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src='https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);})(window,document,'script','dataLayer','%s')</script>", esc_js($gtmId));
}
Expand Down Expand Up @@ -90,8 +90,8 @@ private function defaultConfig(): array
];
}
return apply_filters('awc_civic_cookie_control_config', [
'apiKey' => trim(get_field('civic_cookie_control_api_key', 'option')),
'product' => trim(get_field('civic_cookie_control_product_type', 'option')),
'apiKey' => trim(get_field('civic_cookie_control_api_key', 'option') ?? ''),
'product' => trim(get_field('civic_cookie_control_product_type', 'option') ?? ''),
'closeStyle' => 'button',
'initialState' => 'open',
'text' => [
Expand Down

0 comments on commit 849e23f

Please sign in to comment.