Skip to content

Commit

Permalink
feature: add settings for api keys
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Waldstein authored and Jon Waldstein committed Oct 1, 2024
1 parent 2a5a844 commit 7f3073b
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 6 deletions.
31 changes: 27 additions & 4 deletions src/FormExtension/DonationForm/Actions/AddFieldToFormSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,28 @@

use Give\Framework\FieldsAPI\DonationForm;
use Give\Framework\FieldsAPI\Exceptions\EmptyNameException;
use Give\Log\Log;
use GiveCloudflareTurnstile\FormExtension\DonationForm\Fields\TurnstileField;
use GiveCloudflareTurnstile\FormExtension\DonationForm\Rules\TurnstileFieldRule;
use GiveCloudflareTurnstile\Settings\Repositories\GlobalSettings;

/**
* @since 1.0.0
*/
class AddFieldToFormSchema
{
/**
* @var GlobalSettings $settings
*/
public $settings;

/**
* @since 1.0.0
*/
public function __construct(GlobalSettings $settings) {
$this->settings = $settings;
}

/**
* @since 1.0.0
* @throws EmptyNameException
Expand All @@ -20,7 +34,7 @@ public function __invoke(DonationForm $form, int $formId)
{
if (!apply_filters(
'give_cloudflare_turnstile_enabled',
give_is_setting_enabled(give_get_option('givewp_donation_forms_cloudflare_turnstile_enabled', false)),
$this->settings->isEnabled(),
$formId
)) {
return;
Expand All @@ -35,11 +49,20 @@ public function __invoke(DonationForm $form, int $formId)
$formNodes = $form->all();
$lastSection = $form->count() ? $formNodes[$form->count() - 1] : null;

$siteKey = defined('GIVE_TURNSTILE_SITE_KEY') ? GIVE_TURNSTILE_SITE_KEY : '';
$secretKey = defined('GIVE_TURNSTILE_SECRET_KEY') ? GIVE_TURNSTILE_SECRET_KEY : '';
if (empty($this->settings->getSiteKey())) {
Log::error('Cloudflare Turnstile Site Key Missing');

return;
}

if (empty($this->settings->getSecretKey())) {
Log::error('Cloudflare Turnstile Secret Key Missing');

return;
}


if ($lastSection && !empty($siteKey) && !empty($secretKey)) {
if ($lastSection) {
$lastSection->append($field);
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/FormExtension/DonationForm/Actions/EnqueueScripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace GiveCloudflareTurnstile\FormExtension\DonationForm\Actions;

use Give\Framework\Support\Facades\Scripts\ScriptAsset;
use GiveCloudflareTurnstile\Settings\Repositories\GlobalSettings;

/**
* @since 1.0.0
Expand Down Expand Up @@ -31,7 +32,7 @@ public function __invoke()
wp_add_inline_script(
$turnstileFieldScriptHandle,
'window.giveTurnstileFieldSettings = ' . wp_json_encode([
'siteKey' => defined('GIVE_TURNSTILE_SITE_KEY') ? GIVE_TURNSTILE_SITE_KEY : '',
'siteKey' => give(GlobalSettings::class)->getSiteKey(),
]) . ';',
'before'
);
Expand Down
38 changes: 37 additions & 1 deletion src/Settings/Actions/RegisterGlobalSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace GiveCloudflareTurnstile\Settings\Actions;

use GiveCloudflareTurnstile\Settings\ValueObjects\SettingKeys;

/**
* @unreleased
*/
Expand Down Expand Up @@ -30,6 +32,8 @@ protected function getSettings(): array
'type' => 'title',
],
$this->getEnableSettings(),
$this->getApiSiteKeySettings(),
$this->getApiSecretKeySettings(),
[
'id' => 'give_title_settings_cloudflare_turnstile_1',
'type' => 'sectionend',
Expand All @@ -48,7 +52,7 @@ public function getEnableSettings(): array
'If enabled, this option will add a Cloudflare Turnstile widget to all donation forms',
'give'
),
'id' => 'givewp_donation_forms_cloudflare_turnstile_enabled',
'id' => SettingKeys::ENABLED,
'type' => 'radio_inline',
'default' => 'disabled',
'options' => [
Expand All @@ -57,4 +61,36 @@ public function getEnableSettings(): array
],
];
}

/**
* @since 1.0.0
*/
public function getApiSiteKeySettings(): array
{
return [
'id' => SettingKeys::SITE_KEY,
'name' => __('Cloudflare Turnstile Site Key', 'give'),
'desc' => __(
'Enter your Cloudflare Site Key here. This key is required to connect to the Cloudflare API.',
'give'
),
'type' => 'api_key',
];
}

/**
* @since 1.0.0
*/
public function getApiSecretKeySettings(): array
{
return [
'id' => SettingKeys::SECRET_KEY,
'name' => __('Cloudflare Turnstile Secret Key', 'give'),
'desc' => __(
'Enter your Cloudflare Turnstile Secret key here. This key is required to connect to the Cloudflare API.',
'give'
),
'type' => 'api_key',
];
}
}
32 changes: 32 additions & 0 deletions src/Settings/Repositories/GlobalSettings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace GiveCloudflareTurnstile\Settings\Repositories;

/**
* @since 1.0.0
*/
class GlobalSettings {
/**
* @since 1.0.0
*/
public function getSiteKey(): string
{
return (string)defined('GIVE_TURNSTILE_SITE_KEY') ? GIVE_TURNSTILE_SITE_KEY : give_get_option('givewp_cloudflare_turnstile_site_key', '');
}

/**
* @since 1.0.0
*/
public function getSecretKey(): string
{
return (string)defined('GIVE_TURNSTILE_SECRET_KEY') ? GIVE_TURNSTILE_SECRET_KEY : give_get_option('givewp_cloudflare_turnstile_secret_key', '');
}

/**
* @since 1.0.0
*/
public function isEnabled(): bool
{
return give_is_setting_enabled(give_get_option('givewp_donation_forms_cloudflare_turnstile_enabled', 'disabled'));
}
}
2 changes: 2 additions & 0 deletions src/Settings/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Give\Helpers\Hooks;
use GiveCloudflareTurnstile\Settings\Actions\RegisterGlobalSettings;
use GiveCloudflareTurnstile\Settings\Repositories\GlobalSettings;

/**
* @since 1.0.0
Expand All @@ -16,6 +17,7 @@ class ServiceProvider implements \Give\ServiceProviders\ServiceProvider
*/
public function register()
{
give()->singleton(GlobalSettings::class);
}

/**
Expand Down
12 changes: 12 additions & 0 deletions src/Settings/ValueObjects/SettingKeys.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace GiveCloudflareTurnstile\Settings\ValueObjects;

/**
* @since 1.0.0
*/
class SettingKeys {
public const SITE_KEY = 'givewp_cloudflare_turnstile_site_key';
public const SECRET_KEY = 'givewp_cloudflare_turnstile_secret_key';
public const ENABLED = 'givewp_donation_forms_cloudflare_turnstile_enabled';
}

0 comments on commit 7f3073b

Please sign in to comment.