Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add statuses to Submission elements to indicate whether it is spam #185

Open
wants to merge 3 commits into
base: develop-v4
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased
- Add statuses to Submission elements: the status indicates whether the submission is spam (fixes [#58](https://github.com/hybridinteractive/craft-contact-form-extensions/issues/58))
- Use unified element editor

## [4.2.2] - 2022-11-14
Fixed an issue where the settings tabs were not showing [#164](https://github.com/hybridinteractive/craft-contact-form-extensions/issues/164)

40 changes: 22 additions & 18 deletions src/ContactFormExtensions.php
Original file line number Diff line number Diff line change
@@ -18,11 +18,15 @@
use craft\web\View;
use hybridinteractive\contactformextensions\base\Routes;
use hybridinteractive\contactformextensions\models\Settings;
use hybridinteractive\contactformextensions\services\ContactFormExtensionsService;
use hybridinteractive\contactformextensions\variables\ContactFormExtensionsVariable;
use yii\base\Event;

/**
* Class ContactFormExtensions.
*
* @property-read Settings $settings
* @property-read ContactFormExtensionsService $contactFormExtensionsService
*/
class ContactFormExtensions extends Plugin
{
@@ -173,25 +177,21 @@ private function _registerContactFormEventListeners(): void
{
// Capture Before Send Event from Craft Contact Form plugin
Event::on(CraftContactFormMailer::class, CraftContactFormMailer::EVENT_BEFORE_SEND, function (CraftContactFormSendEvent $e) {
if ($e->isSpam) {
return;
}

// Disable Recaptcha
$disableRecaptcha = false;
if (is_array($e->submission->message) && array_key_exists('disableRecaptcha', $e->submission->message)) {
$disableRecaptcha = filter_var($e->submission->message['disableRecaptcha'], FILTER_VALIDATE_BOOLEAN);
}

if ($this->settings->recaptcha && $disableRecaptcha != true) {
$recaptcha = $this->contactFormExtensionsService->getRecaptcha();
$captchaResponse = Craft::$app->request->getParam('g-recaptcha-response');
if (!$e->isSpam) {
// Disable Recaptcha
$disableRecaptcha = false;
if (is_array($e->submission->message) && array_key_exists('disableRecaptcha', $e->submission->message)) {
$disableRecaptcha = filter_var($e->submission->message['disableRecaptcha'], FILTER_VALIDATE_BOOLEAN);
}

if (!$recaptcha->verifyResponse($captchaResponse, $_SERVER['REMOTE_ADDR'])) {
$e->isSpam = true;
$e->handled = true;
if ($this->settings->recaptcha && $disableRecaptcha != true) {
$recaptcha = $this->contactFormExtensionsService->getRecaptcha();
$captchaResponse = Craft::$app->request->getParam('g-recaptcha-response');

return;
if (!$recaptcha->verifyResponse($captchaResponse, $_SERVER['REMOTE_ADDR'])) {
$e->isSpam = true;
$e->handled = true;
}
}
}

@@ -203,7 +203,11 @@ private function _registerContactFormEventListeners(): void

$submission = $e->submission;
if ($this->settings->enableDatabase && $disableSaveSubmission != true) {
$this->contactFormExtensionsService->saveSubmission($submission);
$this->contactFormExtensionsService->saveSubmission($submission, $e->isSpam);
}

if ($e->isSpam) {
return;
}

// Override toEmail setting
4 changes: 2 additions & 2 deletions src/base/Routes.php
Original file line number Diff line number Diff line change
@@ -19,8 +19,8 @@ trait Routes
public function _registerCpRoutes(): void
{
Event::on(UrlManager::class, UrlManager::EVENT_REGISTER_CP_URL_RULES, function (RegisterUrlRulesEvent $event) {
$event->rules['contact-form-extensions/submissions/<submissionId:\d+>'] = 'contact-form-extensions/submissions/show-submission';
$event->rules['contact-form-extensions/submissions/<submissionId:\d+>/<siteHandle:{handle}>'] = 'contact-form-extensions/submissions/show-submission';
$event->rules['contact-form-extensions/submissions/<elementId:\d+>'] = 'elements/edit';
$event->rules['contact-form-extensions/submissions/<elementId:\d+>/<siteHandle:{handle}>'] = 'elements/edit';
});
}
}
46 changes: 0 additions & 46 deletions src/controllers/SubmissionsController.php

This file was deleted.

41 changes: 41 additions & 0 deletions src/elements/Submission.php
Original file line number Diff line number Diff line change
@@ -13,10 +13,19 @@
use craft\elements\db\ElementQueryInterface;
use craft\helpers\StringHelper;
use craft\helpers\UrlHelper;
use craft\web\CpScreenResponseBehavior;
use hybridinteractive\contactformextensions\ContactFormExtensions;
use hybridinteractive\contactformextensions\elements\db\SubmissionQuery;
use yii\web\Response;

/**
* @method SubmissionQuery find()
*/
class Submission extends Element
{
public const STATUS_IS_SPAM = 'spam';
public const STATUS_IS_NOT_SPAM = 'not-spam';

// Public Properties
// =========================================================================

@@ -25,6 +34,7 @@ class Submission extends Element
public ?string $fromEmail;
public ?string $subject;
public $message;
public $isSpam;

// Static Methods
// =========================================================================
@@ -70,6 +80,17 @@ public function getCpEditUrl(): ?string
return UrlHelper::cpUrl('contact-form-extensions/submissions/'.$this->id);
}

public function prepareEditScreen(Response $response, string $containerId): void
{
/** @var CpScreenResponseBehavior $response */
$response->addCrumb('Contact form submissions', '/contact-form-extensions');
$response->title($this->id);
$response->contentTemplate('contact-form-extensions/submissions/_show', [
'submission' => $this,
'messageObject' => ContactFormExtensions::$plugin->contactFormExtensionsService->utf8AllTheThings(json_decode($this->message, true)),
]);
}

/**
* @inheritDoc
*/
@@ -116,6 +137,24 @@ protected static function defineActions(string $source = null): array
return $actions;
}

public static function hasStatuses(): bool
{
return true;
}

public function getStatus(): ?string
{
return $this->isSpam ? self::STATUS_IS_SPAM : self::STATUS_IS_NOT_SPAM;
}

public static function statuses(): array
{
return [
self::STATUS_IS_SPAM => ['label' => Craft::t('contact-form-extensions', 'Spam'), 'color' => 'red'],
self::STATUS_IS_NOT_SPAM => ['label' => Craft::t('contact-form-extensions', 'Not spam'), 'color' => 'green'],
];
}

/**
* @inheritDoc
*/
@@ -198,6 +237,7 @@ public function afterSave(bool $isNew): void
'fromName' => $this->fromName,
'fromEmail' => $this->fromEmail,
'message' => $this->message,
'isSpam' => $this->isSpam,
])
->execute();
} else {
@@ -208,6 +248,7 @@ public function afterSave(bool $isNew): void
'fromName' => $this->fromName,
'fromEmail' => $this->fromEmail,
'message' => $this->message,
'isSpam' => $this->isSpam,
], ['id' => $this->id])
->execute();
}
22 changes: 22 additions & 0 deletions src/elements/db/SubmissionQuery.php
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@

use craft\elements\db\ElementQuery;
use craft\helpers\Db;
use hybridinteractive\contactformextensions\elements\Submission;

class SubmissionQuery extends ElementQuery
{
@@ -12,6 +13,7 @@ class SubmissionQuery extends ElementQuery
public $fromName;
public $fromEmail;
public $message;
public $isSpam;

public function form($value)
{
@@ -48,6 +50,25 @@ public function message($value)
return $this;
}

public function isSpam($value)
{
$this->isSpam = $value;

return $this;
}

protected function statusCondition(string $status): mixed
{
switch ($status) {
case Submission::STATUS_IS_SPAM:
return ['isSpam' => true];
case Submission::STATUS_IS_NOT_SPAM:
return ['isSpam' => false];
default:
return parent::statusCondition($status);
}
}

protected function beforePrepare(): bool
{
// join in the products table
@@ -60,6 +81,7 @@ protected function beforePrepare(): bool
'contactform_submissions.fromName',
'contactform_submissions.fromEmail',
'contactform_submissions.message',
'contactform_submissions.isSpam',
]);

if ($this->form) {
33 changes: 33 additions & 0 deletions src/migrations/m240110_190349_add_isSpam_column.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace hybridinteractive\contactformextensions\migrations;

use craft\db\Migration;

/**
* m240110_190349_add_isSpam_column migration.
*/
class m240110_190349_add_isSpam_column extends Migration
{
public const COLUMN_IS_SPAM = 'isSpam';

/**
* @inheritdoc
*/
public function safeUp(): bool
{
$this->addColumn('{{%contactform_submissions}}', self::COLUMN_IS_SPAM, $this->boolean()->defaultValue(false));

return true;
}

/**
* @inheritdoc
*/
public function safeDown(): bool
{
$this->dropColumn('{{%contactform_submissions}}', self::COLUMN_IS_SPAM);

return false;
}
}
3 changes: 2 additions & 1 deletion src/services/ContactFormExtensionsService.php
Original file line number Diff line number Diff line change
@@ -37,13 +37,14 @@ class ContactFormExtensionsService extends Component
*
* @return mixed
*/
public function saveSubmission(CraftContactFormSubmission $submission)
public function saveSubmission(CraftContactFormSubmission $submission, bool $isSpam = false)
{
$contactFormSubmission = new Submission();
$contactFormSubmission->form = $submission->message['formName'] ?? 'contact';
$contactFormSubmission->fromName = $submission->fromName;
$contactFormSubmission->fromEmail = $submission->fromEmail;
$contactFormSubmission->subject = $submission->subject;
$contactFormSubmission->isSpam = $isSpam;

if (!is_array($submission->message)) {
$submission->message = ['message' => $this->utf8Value($submission->message)];
Loading