Skip to content

Commit

Permalink
Prestashop 8.0.0 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
magentix committed Dec 14, 2022
1 parent 92722e1 commit 1797cbb
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 48 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.1.0

- Prestashop 8.0.0 compatibility

## 1.0.3

- Custom or third party form validation
Expand Down
104 changes: 56 additions & 48 deletions pixel_cloudflare_turnstile.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Pixel_cloudflare_turnstile extends Module implements WidgetInterface
public function __construct()
{
$this->name = 'pixel_cloudflare_turnstile';
$this->version = '1.0.3';
$this->version = '1.1.0';
$this->author = 'Pixel Open';
$this->tab = 'front_office_features';
$this->need_instance = 0;
Expand All @@ -60,7 +60,9 @@ public function __construct()
$this->templateFile = 'module:' . $this->name . '/pixel_cloudflare_turnstile.tpl';
}

/***************************/
/** MODULE INITIALIZATION **/
/***************************/

/**
* Install the module
Expand Down Expand Up @@ -95,7 +97,9 @@ public function isUsingNewTranslationSystem(): bool
return true;
}

/***********/
/** HOOKS **/
/***********/

/**
* Adds CSS and JS
Expand All @@ -108,17 +112,17 @@ public function hookActionFrontControllerSetMedia(): void
'cloudflare-turnstile',
'modules/' . $this->name . '/views/css/turnstile.css',
[
'position' => 'head',
'priority' => 100,
'position' => 'head',
'priority' => 100,
]
);
$this->context->controller->registerJavascript(
'cloudflare-turnstile',
'https://challenges.cloudflare.com/turnstile/v0/api.js',
[
'server' => 'remote',
'position' => 'head',
'priority' => 100,
'server' => 'remote',
'position' => 'head',
'priority' => 100,
'attributes' => 'async',
]
);
Expand All @@ -137,7 +141,6 @@ public function hookDisplayCustomerAccountForm(): string
if (!$this->isAvailable(self::FORM_REGISTER)) {
return '';
}

return $this->renderWidget('displayCustomerAccountForm', []);
}

Expand All @@ -147,7 +150,6 @@ public function hookDisplayCustomerAccountForm(): string
* @param array $params
*
* @return void
*
* @throws Exception
*/
public function hookActionFrontControllerInitBefore(array $params): void
Expand All @@ -162,7 +164,6 @@ public function hookActionFrontControllerInitBefore(array $params): void
[],
'Modules.Pixelcloudflareturnstile.Shop'
);

return;
}
if (!$this->getSitekey()) {
Expand All @@ -171,7 +172,6 @@ public function hookActionFrontControllerInitBefore(array $params): void
[],
'Modules.Pixelcloudflareturnstile.Shop'
);

return;
}

Expand All @@ -195,7 +195,7 @@ public function hookActionFrontControllerInitBefore(array $params): void
* Check if turnstile is available for current action
*
* @param string $controllerClass
* @param bool $validate
* @param bool $validate
*
* @return bool
*/
Expand All @@ -212,7 +212,6 @@ protected function canProcess(string $controllerClass, bool $validate = false):
if ($validate && !Tools::isSubmit('submitMessage')) {
return false;
}

return true;
}

Expand All @@ -225,7 +224,17 @@ protected function canProcess(string $controllerClass, bool $validate = false):
if ($validate && !Tools::isSubmit('submitCreate')) {
return false;
}
return true;
}

// Register Prestashop >= 8.0.0
if ($controllerClass === 'RegistrationController' &&
$this->isAvailable(self::FORM_REGISTER) &&
!$isLoggedIn
) {
if ($validate && !Tools::isSubmit('submitCreate')) {
return false;
}
return true;
}

Expand All @@ -238,7 +247,6 @@ protected function canProcess(string $controllerClass, bool $validate = false):
if ($validate && !Tools::isSubmit('submitLogin')) {
return false;
}

return true;
}

Expand All @@ -247,7 +255,6 @@ protected function canProcess(string $controllerClass, bool $validate = false):
if ($validate && empty($_POST)) {
return false;
}

return true;
}

Expand All @@ -270,13 +277,12 @@ public function isAvailable(string $form): bool
* Validate turnstile
*
* @return void
*
* @throws Exception
*/
public static function turnstileValidation(): void
{
$referer = $_SERVER['HTTP_REFERER'] ?? 'index';
$cookie = Context::getContext()->cookie;
$cookie = Context::getContext()->cookie;

$response = Tools::getValue('cf-turnstile-response');
if (!$response) {
Expand All @@ -292,7 +298,7 @@ public static function turnstileValidation(): void
}

$data = [
'secret' => Configuration::get(self::CONFIG_CLOUDFLARE_TURNSTILE_SECRET_KEY),
'secret' => Configuration::get(self::CONFIG_CLOUDFLARE_TURNSTILE_SECRET_KEY),
'response' => $response,
];

Expand Down Expand Up @@ -333,20 +339,22 @@ public static function turnstileValidation(): void
protected static function getErrorMessage(string $code): string
{
$messages = [
'missing-input-secret' => 'the secret parameter was not passed.',
'invalid-input-secret' => 'the secret parameter was invalid or did not exist.',
'missing-input-secret' => 'the secret parameter was not passed.',
'invalid-input-secret' => 'the secret parameter was invalid or did not exist.',
'missing-input-response' => 'the response parameter was not passed.',
'invalid-input-response' => 'the response parameter is invalid or has expired.',
'bad-request' => 'the request was rejected because it was malformed.',
'timeout-or-duplicate' => 'the response parameter has already been validated before.',
'internal-error' => 'an internal error happened while validating the response. The request can be retried.',
'unavailable' => 'unable to contact Cloudflare to validate the form',
'bad-request' => 'the request was rejected because it was malformed.',
'timeout-or-duplicate' => 'the response parameter has already been validated before.',
'internal-error' => 'an internal error happened while validating the response. The request can be retried.',
'unavailable' => 'unable to contact Cloudflare to validate the form',
];

return $messages[$code] ?? 'unknown error';
}

/*********************/
/** FRONTEND WIDGET **/
/*********************/

/**
* Render the turnstile widget
Expand Down Expand Up @@ -383,8 +391,8 @@ public function getWidgetVariables($hookName, array $configuration): array
{
return [
'sitekey' => $this->getSitekey(),
'theme' => $configuration['theme'] ?? $this->getTheme(),
'action' => $configuration['action'] ?? $this->getFormName(),
'theme' => $configuration['theme'] ?? $this->getTheme(),
'action' => $configuration['action'] ?? $this->getFormName(),
];
}

Expand Down Expand Up @@ -413,7 +421,9 @@ public function getFormName(): string
return $action;
}

/*************************/
/** ADMIN CONFIGURATION **/
/*************************/

/**
* Retrieve config fields
Expand All @@ -424,67 +434,67 @@ protected function getConfigFields(): array
{
return [
self::CONFIG_CLOUDFLARE_TURNSTILE_SITEKEY => [
'type' => 'text',
'label' => $this->trans('Sitekey', [], 'Modules.Pixelcloudflareturnstile.Admin'),
'name' => self::CONFIG_CLOUDFLARE_TURNSTILE_SITEKEY,
'type' => 'text',
'label' => $this->trans('Sitekey', [], 'Modules.Pixelcloudflareturnstile.Admin'),
'name' => self::CONFIG_CLOUDFLARE_TURNSTILE_SITEKEY,
'required' => true,
],
self::CONFIG_CLOUDFLARE_TURNSTILE_SECRET_KEY => [
'type' => 'text',
'label' => $this->trans('Secret key', [], 'Modules.Pixelcloudflareturnstile.Admin'),
'name' => self::CONFIG_CLOUDFLARE_TURNSTILE_SECRET_KEY,
'type' => 'text',
'label' => $this->trans('Secret key', [], 'Modules.Pixelcloudflareturnstile.Admin'),
'name' => self::CONFIG_CLOUDFLARE_TURNSTILE_SECRET_KEY,
'required' => true,
],
self::CONFIG_CLOUDFLARE_TURNSTILE_THEME => [
'type' => 'select',
'label' => $this->trans('Theme', [], 'Modules.Pixelcloudflareturnstile.Admin'),
'name' => self::CONFIG_CLOUDFLARE_TURNSTILE_THEME,
'type' => 'select',
'label' => $this->trans('Theme', [], 'Modules.Pixelcloudflareturnstile.Admin'),
'name' => self::CONFIG_CLOUDFLARE_TURNSTILE_THEME,
'required' => true,
'options' => [
'query' => [
[
'value' => 'auto',
'name' => 'Auto',
'name' => 'Auto',
],
[
'value' => 'light',
'name' => 'Light',
'name' => 'Light',
],
[
'value' => 'dark',
'name' => 'Dark',
'name' => 'Dark',
],
],
'id' => 'value',
'id' => 'value',
'name' => 'name',
],
],
self::CONFIG_CLOUDFLARE_TURNSTILE_FORMS => [
'type' => 'select',
'type' => 'select',
'multiple' => true,
'label' => $this->trans('Forms to validate', [], 'Modules.Pixelcloudflareturnstile.Admin'),
'name' => self::CONFIG_CLOUDFLARE_TURNSTILE_FORMS . '[]',
'label' => $this->trans('Forms to validate', [], 'Modules.Pixelcloudflareturnstile.Admin'),
'name' => self::CONFIG_CLOUDFLARE_TURNSTILE_FORMS . '[]',
'required' => false,
'options' => [
'query' => [
[
'value' => self::FORM_CONTACT,
'name' => $this->trans('Contact', [], 'Modules.Pixelcloudflareturnstile.Admin'),
'name' => $this->trans('Contact', [], 'Modules.Pixelcloudflareturnstile.Admin'),
],
[
'value' => self::FORM_LOGIN,
'name' => $this->trans('Login', [], 'Modules.Pixelcloudflareturnstile.Admin'),
'name' => $this->trans('Login', [], 'Modules.Pixelcloudflareturnstile.Admin'),
],
[
'value' => self::FORM_REGISTER,
'name' => $this->trans('Register', [], 'Modules.Pixelcloudflareturnstile.Admin'),
'name' => $this->trans('Register', [], 'Modules.Pixelcloudflareturnstile.Admin'),
],
[
'value' => self::FORM_PASSWORD,
'name' => $this->trans('Reset Password', [], 'Modules.Pixelcloudflareturnstile.Admin'),
'name' => $this->trans('Reset Password', [], 'Modules.Pixelcloudflareturnstile.Admin'),
],
],
'id' => 'value',
'id' => 'value',
'name' => 'name',
],
'desc' => $this->trans(
Expand All @@ -500,7 +510,6 @@ protected function getConfigFields(): array
* This method handles the module's configuration page
*
* @return string
*
* @throws Exception
*/
public function getContent(): string
Expand Down Expand Up @@ -549,7 +558,6 @@ public function getContent(): string
* Retrieve current theme name
*
* @return string
*
* @throws Exception
*/
public function getCurrentThemeName(): string
Expand Down

0 comments on commit 1797cbb

Please sign in to comment.