Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/freeform_next/Controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Solspace\Addons\FreeformNext\Library\Helpers\UrlHelper;
use Solspace\Addons\FreeformNext\Services\FieldsService;
use Solspace\Addons\FreeformNext\Services\PermissionsService;
use Solspace\Addons\FreeformNext\Services\SettingsService;

abstract class Controller
{
Expand Down Expand Up @@ -70,4 +71,18 @@ protected function getPermissionsService()

return $instance;
}

/**
* @return SettingsService
*/
protected function getSettingsService()
{
static $instance;

if (null === $instance) {
$instance = new SettingsService();
}

return $instance;
}
}
85 changes: 50 additions & 35 deletions src/freeform_next/Controllers/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,54 @@ private function spamProtectionAction(): CpView
{
$settings = $this->getSettings();

$sections = [
[
[
'title' => 'Freeform Honeypot',
'desc' => 'Enable this to use Freeform\'s built in Honeypot spam protection.',
'fields' => [
'spamProtectionEnabled' => [
'type' => 'yes_no',
'value' => $settings->isSpamProtectionEnabled(),
],
],
],
[
'title' => 'Javascript Enhancement',
'desc' => 'Enable this to use Freeform\'s built-in Javascript enhancement for the Honeypot feature. This will require users to have JS enabled for their browser and help fight spambots more aggressively.',
'fields' => [
'freeformHoneypotEnhancement' => [
'type' => 'yes_no',
'value' => $settings->isFreeformHoneypotEnhanced(),
],
],
],
[
'title' => 'Spam protection simulates a successful submission?',
'desc' => 'Enable this to change the spam protection behavior to simulate a successful submission instead of just reloading the form.',
'fields' => [
'spamBlockLikeSuccessfulPost' => [
'type' => 'yes_no',
'value' => $settings->isSpamBlockLikeSuccessfulPost(),
],
],
],
],
];

if (FreeformHelper::isFreeformAtLeast('3.3.5')) {
$sections[0][] = [
'title' => 'Spam Folder',
'desc' => 'When enabled, all submissions caught by spam protection measures will be flagged as spam and stored in the database, but available to manage in a separate menu inside Freeform.',
'fields' => [
'spamFolderEnabled' => [
'type' => 'yes_no',
'value' => $settings->isSpamFolderEnabled(),
],
],
];
}

$view = new CpView('settings/common', []);
$view
->setHeading(lang('Spam Protection'))
Expand All @@ -303,41 +351,8 @@ private function spamProtectionAction(): CpView
'cp_page_title' => $view->getHeading(),
'save_btn_text' => 'btn_save_settings',
'save_btn_text_working' => 'btn_saving',
'sections' => [
[
[
'title' => 'Freeform Honeypot',
'desc' => 'Enable this to use Freeform\'s built in Honeypot spam protection.',
'fields' => [
'spamProtectionEnabled' => [
'type' => 'yes_no',
'value' => $settings->isSpamProtectionEnabled(),
],
],
],
[
'title' => 'Javascript Enhancement',
'desc' => 'Enable this to use Freeform\'s built-in Javascript enhancement for the Honeypot feature. This will require users to have JS enabled for their browser and help fight spambots more aggressively.',
'fields' => [
'freeformHoneypotEnhancement' => [
'type' => 'yes_no',
'value' => $settings->isFreeformHoneypotEnhanced(),
],
],
],
[
'title' => 'Spam protection simulates a successful submission?',
'desc' => 'Enable this to change the spam protection behavior to simulate a successful submission instead of just reloading the form.',
'fields' => [
'spamBlockLikeSuccessfulPost' => [
'type' => 'yes_no',
'value' => $settings->isSpamBlockLikeSuccessfulPost(),
],
],
],
],
],
]
'sections' => $sections,
]
);

return $view;
Expand Down
5 changes: 4 additions & 1 deletion src/freeform_next/Library/Composer/Components/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
use Solspace\Addons\FreeformNext\Library\Mailing\MailHandlerInterface;
use Solspace\Addons\FreeformNext\Library\Session\FormValueContext;
use Solspace\Addons\FreeformNext\Library\Translations\TranslatorInterface;
use Solspace\Addons\FreeformNext\Model\SpamReasonModel;
use Solspace\Addons\FreeformNext\Model\SubmissionModel;
use Solspace\Addons\FreeformNext\Repositories\SubmissionRepository;

Expand Down Expand Up @@ -478,6 +477,10 @@ public function submit()

if ($this->isMarkedAsSpam()) {
$this->formSaved = true;

if (FreeformHelper::isFreeformAtLeast('3.3.5') && !$this->formHandler->isSpamFolderEnabled()) {
return null;
}
}

if ($formValueContext->shouldFormWalkToPreviousPage()) {
Expand Down
5 changes: 5 additions & 0 deletions src/freeform_next/Library/Database/FormHandlerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ public function isSpamBehaviourReloadForm();
*/
public function isSpamProtectionEnabled();

/**
* @return bool
*/
public function isSpamFolderEnabled();

/**
* Do something before the form is saved
* Return bool determines whether the form should be saved or not
Expand Down
12 changes: 12 additions & 0 deletions src/freeform_next/Library/Helpers/FreeformHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,16 @@ public static function get($name)

return $return;
}

public static function isFreeformAtLeast(string $minVersion): bool
{
$addon = ee('Addon')->get('freeform_next');

$installed = $addon->getInstalledVersion();
if (!$installed) {
return false;
}

return version_compare($installed, $minVersion, '>=');
}
}
12 changes: 12 additions & 0 deletions src/freeform_next/Model/SettingsModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* @property bool $spamProtectionEnabled
* @property bool $freeformHoneypotEnhancement
* @property bool $spamBlockLikeSuccessfulPost
* @property bool $spamFolderEnabled
* @property bool $showTutorial
* @property string $fieldDisplayOrder
* @property string $formattingTemplatePath
Expand Down Expand Up @@ -52,6 +53,7 @@ class SettingsModel extends Model

public const DEFAULT_SPAM_PROTECTION_ENABLED = true;
public const DEFAULT_SPAM_BLOCK_LIKE_SUCCESSFUL_POST = false;
public const DEFAULT_SPAM_FOLDER_ENABLED = true;
public const DEFAULT_SHOW_TUTORIAL = true;
public const DEFAULT_FIELD_DISPLAY_ORDER = self::FIELD_DISPLAY_ORDER_TYPE;
public const DEFAULT_FORMATTING_TEMPLATE_PATH = null;
Expand Down Expand Up @@ -79,6 +81,7 @@ class SettingsModel extends Model
protected $spamProtectionEnabled;
protected $freeformHoneypotEnhancement;
protected $spamBlockLikeSuccessfulPost;
protected $spamFolderEnabled;
protected $showTutorial;
protected $fieldDisplayOrder;
protected $formattingTemplatePath;
Expand Down Expand Up @@ -110,6 +113,7 @@ public static function create()
'siteId' => ee()->config->item('site_id'),
'spamProtectionEnabled' => self::DEFAULT_SPAM_PROTECTION_ENABLED,
'spamBlockLikeSuccessfulPost' => self::DEFAULT_SPAM_BLOCK_LIKE_SUCCESSFUL_POST,
'spamFolderEnabled' => self::DEFAULT_SPAM_FOLDER_ENABLED,
'showTutorial' => self::DEFAULT_SHOW_TUTORIAL,
'fieldDisplayOrder' => self::DEFAULT_FIELD_DISPLAY_ORDER,
'formattingTemplatePath' => self::DEFAULT_FORMATTING_TEMPLATE_PATH,
Expand Down Expand Up @@ -286,6 +290,14 @@ public function isSpamBlockLikeSuccessfulPost(): bool
return (bool) $this->spamBlockLikeSuccessfulPost;
}

/**
* @return bool
*/
public function isSpamFolderEnabled(): bool
{
return (bool) $this->spamFolderEnabled;
}

/**
* @return bool
*/
Expand Down
24 changes: 24 additions & 0 deletions src/freeform_next/Repositories/SettingsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public static function getInstance()
*/
public function getOrCreate()
{
$this->ensureSpamFolderEnabledColumnExists();

$siteId = ee()->config->item('site_id');

if (!isset(self::$cache[$siteId])) {
Expand All @@ -40,4 +42,26 @@ public function getOrCreate()

return self::$cache[$siteId];
}

private function ensureSpamFolderEnabledColumnExists(): void
{
$settingsTable = ee()->db->dbprefix('freeform_next_settings');

if (!ee()->db->table_exists($settingsTable)) {
return;
}

if (ee()->db->field_exists('spamFolderEnabled', $settingsTable)) {
return;
}

try {
ee()->db->query("ALTER TABLE `{$settingsTable}` ADD COLUMN `spamFolderEnabled` TINYINT(1) UNSIGNED NOT NULL DEFAULT 1 AFTER `spamBlockLikeSuccessfulPost`");
} catch (\Exception $exception) {
// swallow race conditions
if (strpos($exception->getMessage(), 'Duplicate column name') === false) {
throw $exception;
}
}
}
}
8 changes: 8 additions & 0 deletions src/freeform_next/Services/FormsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ public function isSpamBlockLikeSuccessfulPost(): bool
return SettingsRepository::getInstance()->getOrCreate()->isSpamBlockLikeSuccessfulPost();
}

/**
* @return bool
*/
public function isSpamFolderEnabled(): bool
{
return SettingsRepository::getInstance()->getOrCreate()->isSpamFolderEnabled();
}

/**
* @param Form $form
*
Expand Down
8 changes: 8 additions & 0 deletions src/freeform_next/Services/SettingsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ public function getSessionStorageImplementation(): DbSession|EESession
return new EESession();
}

/**
* @return bool
*/
public function isSpamFolderEnabled(): bool
{
return $this->getSettingsModel()->isSpamFolderEnabled();
}

/**
* @return SettingsModel
*/
Expand Down
4 changes: 2 additions & 2 deletions src/freeform_next/addon.setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
if (!$ftExists) {
ee()->db->insert('exp_fieldtypes', [
'name' => 'freeform_next',
'version' => '3.3.4',
'version' => '3.3.5',
'settings' => 'YTowOnt9',
'has_global_settings' => 'n',
]);
Expand All @@ -50,7 +50,7 @@
'name' => 'Freeform',
'module_name' => 'Freeform_next',
'description' => 'Powerful form builder',
'version' => '3.3.4',
'version' => '3.3.5',
'namespace' => 'Solspace\Addons\FreeformNext',
'settings_exist' => true,
'models' => [
Expand Down
2 changes: 1 addition & 1 deletion src/freeform_next/composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "solspace/ee-freeform-next",
"description": "The most reliable, intuitive and powerful form builder for ExpressionEngine.",
"version": "3.3.4",
"version": "3.3.5",
"type": "library",
"require": {
"php": "^8.0 || ^8.1 || ^8.2 || ^8.3 || ^8.4",
Expand Down
1 change: 1 addition & 0 deletions src/freeform_next/db.freeform_next.sql
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ CREATE TABLE IF NOT EXISTS `exp_freeform_next_settings` (
`spamProtectionEnabled` TINYINT(1) UNSIGNED NOT NULL DEFAULT '0',
`freeformHoneypotEnhancement` TINYINT(1) UNSIGNED NOT NULL DEFAULT '0',
`spamBlockLikeSuccessfulPost` TINYINT(1) UNSIGNED NOT NULL DEFAULT '0',
`spamFolderEnabled` TINYINT(1) UNSIGNED NOT NULL DEFAULT 1,
`showTutorial` TINYINT(1) UNSIGNED NOT NULL DEFAULT '0',
`fieldDisplayOrder` VARCHAR(30) NULL DEFAULT NULL,
`formattingTemplatePath` VARCHAR(255) NULL DEFAULT NULL,
Expand Down
17 changes: 13 additions & 4 deletions src/freeform_next/ext.freeform_next.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,19 @@ public function addCpCustomMenu($menu): void
ee('CP/URL', "addons/settings/freeform_next/submissions/{$formModel->handle}")
);

$sub->addItem(
lang('Spam'),
ee('CP/URL', "addons/settings/freeform_next/spam/{$formModel->handle}")
);
if (FreeformHelper::isFreeformAtLeast('3.3.5')) {
if ($this->getSettingsService()->isSpamFolderEnabled()) {
$sub->addItem(
lang('Spam'),
ee('CP/URL', "addons/settings/freeform_next/spam/{$formModel->handle}")
);
}
} else {
$sub->addItem(
lang('Spam'),
ee('CP/URL', "addons/settings/freeform_next/spam/{$formModel->handle}")
);
}
}
}

Expand Down
Loading