Skip to content

Commit

Permalink
Add api cache setting
Browse files Browse the repository at this point in the history
  • Loading branch information
sparkalow committed Jul 27, 2024
1 parent 51e5e82 commit 071eee7
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 12 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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.

Expand All @@ -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
];
```

Expand All @@ -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 }}
```
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -29,7 +29,7 @@
},
"require": {
"php": ">=8.0.2",
"craftcms/cms": "^4.4.0"
"craftcms/cms": "^4.0|^5.0"
},
"autoload": {
"psr-4": {
Expand Down
24 changes: 18 additions & 6 deletions src/fields/ActiveCampaignForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 <a href="%s">plugin settings</a>.', 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 <a href="%s">settings</a>?',Plugin::getInstance()->getSettingsUrl());
if (empty($this->options)) {
return sprintf('error - No forms found. Are the correct ActiveCampaign API credentials used in <a href="%s">settings</a>?', Plugin::getInstance()->getSettingsUrl());
}
return parent::getInputHtml($value, $element);
}
Expand All @@ -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[] = [
Expand All @@ -64,7 +77,6 @@ protected function options(): array
];
}
$this->options = $options;

} catch (\Exception $e) {
Craft::error($e->getMessage(), __METHOD__);
}
Expand Down
4 changes: 3 additions & 1 deletion src/models/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']
];
}
}
18 changes: 18 additions & 0 deletions src/templates/_settings.twig
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,24 @@
value: settings.apiKey})
}}

<hr>

<p>
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.
<br>Most sites do not need to alter the default setting.
</p>
{{ 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})
}}



<p>
Expand Down

0 comments on commit 071eee7

Please sign in to comment.