diff --git a/CHANGELOG.md b/CHANGELOG.md index 747ac07..10a38a9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,11 @@ # Changelog +## 1.1.0 2024-07-27 + +- add API cache time setting + ## 1.0.1 2024-03-07 + - remove hard coded url path ## 1.0.0 2023-09-16 diff --git a/README.md b/README.md index 70408dc..0c02b75 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ Simplify managing embed snippets for content authors. ## Requirements -This plugin requires Craft CMS 4.4.0 or later, and PHP 8.0.2 or later. +This plugin requires Craft CMS 4.4.0, 5.0.0 or later, and PHP 8.0.2 or later. An ActiveCampaign account is also necessary. @@ -47,7 +47,7 @@ Once the plugin is installed: 3. Enter your **API Key**. 4. Click **Save**. -> See the [official docs](https://help.activecampaign.com/hc/en-us/articles/207317590-Getting-started-with-the-API) for info on how to get your key. +> See the [official docs](https://help.activecampaign.com/hc/en-us/articles/207317590-Getting-started-with-the-API) for info on how to get your API info. Alternatively, you can also create an `activecampaign-forms.php` config file in your `/config` directory with the following options. @@ -57,6 +57,7 @@ Alternatively, you can also create an `activecampaign-forms.php` config file in return [ 'account' => 'your_account_url', 'apiKey' => 'your_api_key', + 'apiCacheDuration' => 0 ]; ``` @@ -66,7 +67,7 @@ return [ This plugin provides a new field type for selecting forms created on the ActiveCampaign platform. Create a new ActiveCampaign form field and add it to your entry types. -Render a form field with: +Once a form is set on an entry, render a form field on the frontend with: ```twig {{ craft.acforms.renderForm(entry.yourFieldHandle) | raw }} ``` diff --git a/composer.json b/composer.json index 3f05c18..748949b 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "sparkalow/craft-activecampaign-forms", "description": "ActiveCampaign forms field for Craft CMS.", "type": "craft-plugin", - "version": "1.0.1", + "version": "1.1.0", "keywords": [ "craft", "cms", @@ -29,7 +29,7 @@ }, "require": { "php": ">=8.0.2", - "craftcms/cms": "^4.4.0" + "craftcms/cms": "^4.0|^5.0" }, "autoload": { "psr-4": { diff --git a/src/fields/ActiveCampaignForm.php b/src/fields/ActiveCampaignForm.php index d64f151..137da26 100644 --- a/src/fields/ActiveCampaignForm.php +++ b/src/fields/ActiveCampaignForm.php @@ -16,7 +16,7 @@ public static function displayName(): string { return Craft::t('activecampaign-forms', 'ActiveCampaign Form'); } - + /** * Get the input html. Will return error states in html if the api settings are not configured * @param mixed $value @@ -28,12 +28,12 @@ public function getInputHtml(mixed $value, ?ElementInterface $element = null): s $settings = Plugin::$plugin->getSettings(); // missing credentials - if (!$settings->apiKey || !$settings->account){ + if (!$settings->apiKey || !$settings->account) { return sprintf('You must setup your ActiveCampaign API key in plugin settings.', Plugin::getInstance()->getSettingsUrl()); } // a network error or bad credentials - if (empty($this->options)){ - return sprintf('error - No forms found. Are the correct ActiveCampaign API credentials used in settings?',Plugin::getInstance()->getSettingsUrl()); + if (empty($this->options)) { + return sprintf('error - No forms found. Are the correct ActiveCampaign API credentials used in settings?', Plugin::getInstance()->getSettingsUrl()); } return parent::getInputHtml($value, $element); } @@ -55,7 +55,20 @@ public function getSettingsHtml(): ?string protected function options(): array { try { - $forms = Plugin::getInstance()->activeCampaignApI->get('forms'); + $settings = Plugin::$plugin->getSettings(); + + if($settings->apiCacheDuration){ + $cacheKey = 'craft-activecampaign-forms'; + + // thic call is cached + $forms = \Craft::$app->cache->getOrSet($cacheKey, function () { + return Plugin::getInstance()->activeCampaignApI->get('forms'); + }, $settings->apiCacheDuration); + + }else{ + $forms = Plugin::getInstance()->activeCampaignApI->get('forms'); + } + $options = []; foreach ($forms->forms as $key => $formObj) { $options[] = [ @@ -64,7 +77,6 @@ protected function options(): array ]; } $this->options = $options; - } catch (\Exception $e) { Craft::error($e->getMessage(), __METHOD__); } diff --git a/src/models/Settings.php b/src/models/Settings.php index facdb1d..d2072d0 100644 --- a/src/models/Settings.php +++ b/src/models/Settings.php @@ -12,12 +12,14 @@ class Settings extends Model { public $account =''; public $apiKey =''; + public $apiCacheDuration = 0; public function defineRules(): array { return [ - [['account','apiKey'],'required'] + [['account','apiKey'],'required'], + [['apiCacheDuration'],'numeric'] ]; } } diff --git a/src/templates/_settings.twig b/src/templates/_settings.twig index 7898aa6..b1993e7 100644 --- a/src/templates/_settings.twig +++ b/src/templates/_settings.twig @@ -25,6 +25,24 @@ value: settings.apiKey}) }} +
+ +

+ API calls are only made to ActiveCampaign when loading an entry + for editing in the control panel (not the frontend). Configure a cache time below if needed. +
Most sites do not need to alter the default setting. +

+{{ forms.textField({ + label: 'ActiveCampaign Cache Duration', + id: 'apiCacheDuration', + type: 'number', + name: 'apiCacheDuration', + required: false, + instructions: 'How long in seconds to cache api requests.', + errors: settings.getErrors('apiCacheDuration'), + value: settings.apiCacheDuration}) +}} +