Skip to content

Commit

Permalink
Check that ACF functions exist before use
Browse files Browse the repository at this point in the history
If this plugin is run via a request to wp-activate.php
it will produce a 5xx error in our logs. This commit
checks for the existance of ACF functions before they
are used, and returns early from methods if ACF is
not available.
  • Loading branch information
snim2 committed Feb 4, 2025
1 parent 8dd2288 commit 6ff408b
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 7 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).

## [Unreleased]

## Changed

- When ACF is not available the plugin does not produce an error

## [1.5.4] - 2024-11-28

### Added
Expand Down
27 changes: 21 additions & 6 deletions spec/options.spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,29 @@
});

describe('->acfInit()', function () {
it('registers options', function () {
allow('acf_add_options_sub_page')->toBeCalled();
expect('acf_add_options_sub_page')->toBeCalled();
context('ACF is available', function () {
it('registers options', function () {
allow('function_exists')->toBeCalled()->andReturn(true);

allow('acf_add_local_field_group')->toBeCalled();
expect('acf_add_local_field_group')->toBeCalled();
allow('acf_add_options_sub_page')->toBeCalled();
expect('acf_add_options_sub_page')->toBeCalled();

$this->options->acfInit();
allow('acf_add_local_field_group')->toBeCalled();
expect('acf_add_local_field_group')->toBeCalled();

$this->options->acfInit();
});
});
context('ACF is not available', function () {
it('registers options', function () {
allow('function_exists')->toBeCalled()->andReturn(false);

expect('acf_add_options_sub_page')->not->toBeCalled();

expect('acf_add_local_field_group')->not->toBeCalled();

$this->options->acfInit();
});
});
});
});
36 changes: 35 additions & 1 deletion spec/scripts.spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,15 @@
});

describe('->enqueueScripts()', function () {
context('Civic Cookie API Key is not set', function () {
context('ACF is not available', function () {
it('does nothing', function () {
allow('function_exists')->toBeCalled()->andReturn(false);
expect('get_field')->not->toBeCalled();
$this->scripts->enqueueScripts();
});
}); context('Civic Cookie API Key is not set', function () {
it('does nothing', function () {
allow('function_exists')->toBeCalled()->andReturn(true);
allow('get_field')->toBeCalled()->andReturn('');
expect('get_field')->toBeCalled()->once()->with('civic_cookie_control_api_key', 'option');
$this->scripts->enqueueScripts();
Expand All @@ -47,6 +54,7 @@
context('Civic Cookie API Key is set', function () {
context('but Civic Product Type is not set', function () {
it('does nothing', function () {
allow('function_exists')->toBeCalled()->andReturn(true);
allow('get_field')->toBeCalled()->andReturn('an_api_key', '');
expect('get_field')->toBeCalled()->once()->with('civic_cookie_control_api_key', 'option');
expect('get_field')->toBeCalled()->once()->with('civic_cookie_control_product_type', 'option');
Expand All @@ -56,6 +64,7 @@
context('and Civic Product Type is set', function () {
context('but marketing scripts are not on', function () {
it('enqueues the Civic Cookie Control script and the config and analytics scripts, and injects our settings, with the option to filter them', function () {
allow('function_exists')->toBeCalled()->andReturn(true);
allow('get_field')->toBeCalled()->andReturn('an_api_key', 'a_product_type', 'a_ga_id', 'a_ga4_id', 'a_gtm_id', 'a_hjid', false, 'an_api_key', 'a_product_type');
expect('get_field')->toBeCalled()->times(2)->with('civic_cookie_control_api_key', 'option');
expect('get_field')->toBeCalled()->times(2)->with('civic_cookie_control_product_type', 'option');
Expand Down Expand Up @@ -88,6 +97,7 @@

context('and marketing scripts are on', function () {
it('enqueues the Civic Cookie Control script and the config and analytics scripts, and injects our settings including the additional optional cookies, with the option to filter them', function () {
allow('function_exists')->toBeCalled()->andReturn(true);
allow('get_field')->toBeCalled()->andReturn('an_api_key', 'a_product_type', 'a_ga_id', 'a_ga4_id', 'a_gtm_id', 'a_hjid', true, 'a list of marketing cookies', 'an_api_key', 'a_product_type');
allow('esc_js')->toBeCalled()->andRun(function ($input) {
return $input;
Expand Down Expand Up @@ -139,8 +149,17 @@
});

describe('->addGA4()', function () {
context('ACF is not available', function () {
it('does nothing', function () {
allow('function_exists')->toBeCalled()->andReturn(false);
expect('get_field')->not->toBeCalled();

$this->scripts->addGA4();
});
});
context('API Key is not set', function () {
it('does nothing', function () {
allow('function_exists')->toBeCalled()->andReturn(true);
allow('get_field')->toBeCalled()->andReturn(null, 'a_product_type', 'a_ga4_id');

ob_start();
Expand All @@ -152,6 +171,7 @@
});
context('product type is not set', function () {
it('does nothing', function () {
allow('function_exists')->toBeCalled()->andReturn(true);
allow('get_field')->toBeCalled()->andReturn('an_api_key', null, 'a_ga4_id');

ob_start();
Expand All @@ -163,6 +183,7 @@
});
context('GA4 ID is not set', function () {
it('does nothing', function () {
allow('function_exists')->toBeCalled()->andReturn(true);
allow('get_field')->toBeCalled()->andReturn('an_api_key', 'a_product_type', null);

ob_start();
Expand All @@ -174,6 +195,7 @@
});
context('API Key, product type and GA4 ID are set', function () {
it('outputs the GA4 script tag', function () {
allow('function_exists')->toBeCalled()->andReturn(true);
allow('get_field')->toBeCalled()->andReturn('an_api_key', 'a_product_type', '123456');
allow('esc_attr')->toBeCalled()->andRun(function ($input) {
return $input;
Expand All @@ -189,8 +211,17 @@
});

describe('->addGTM()', function () {
context('ACF is not available', function () {
it('does nothing', function () {
allow('function_exists')->toBeCalled()->andReturn(false);
expect('get_field')->not->toBeCalled();

$this->scripts->addGTM();
});
});
context('API Key is not set', function () {
it('does nothing', function () {
allow('function_exists')->toBeCalled()->andReturn(true);
allow('get_field')->toBeCalled()->andReturn(null, 'a_product_type', 'a_gtm_id');

ob_start();
Expand All @@ -202,6 +233,7 @@
});
context('product type is not set', function () {
it('does nothing', function () {
allow('function_exists')->toBeCalled()->andReturn(true);
allow('get_field')->toBeCalled()->andReturn('an_api_key', null, 'a_gtm_id');

ob_start();
Expand All @@ -213,6 +245,7 @@
});
context('GTM ID is not set', function () {
it('does nothing', function () {
allow('function_exists')->toBeCalled()->andReturn(true);
allow('get_field')->toBeCalled()->andReturn('an_api_key', 'a_product_type', null);

ob_start();
Expand All @@ -224,6 +257,7 @@
});
context('API Key, product type and GTM ID are set', function () {
it('outputs the GTM script tag', function () {
allow('function_exists')->toBeCalled()->andReturn(true);
allow('get_field')->toBeCalled()->andReturn('an_api_key', 'a_product_type', '123456');
allow('esc_js')->toBeCalled()->andRun(function ($input) {
return $input;
Expand Down
4 changes: 4 additions & 0 deletions src/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public function validateTrimmedText($valid, $value, $field, $input_name)

public function acfInit(): void
{
if (!function_exists('acf_add_options_sub_page')) {
return;
}

acf_add_options_sub_page([
'page_title' => 'Analytics with Consent',
'menu_slug' => 'analytics-with-consent',
Expand Down
16 changes: 16 additions & 0 deletions src/Scripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public function addActionLinks(array $links): array

public function enqueueScripts(): void
{
if (!function_exists('get_field')) {
return;
}

$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'));
Expand Down Expand Up @@ -49,6 +53,10 @@ public function enqueueStyles(): void

public function addGA4(): void
{
if (!function_exists('get_field')) {
return;
}

$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'));
Expand All @@ -59,6 +67,10 @@ public function addGA4(): void

public function addGTM(): void
{
if (!function_exists('get_field')) {
return;
}

$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'));
Expand All @@ -69,6 +81,10 @@ public function addGTM(): void

private function defaultConfig(): array
{
if (!function_exists('get_field')) {
return [];
}

$optionalCookies = [
[
'name' => 'analytics',
Expand Down

0 comments on commit 6ff408b

Please sign in to comment.