Skip to content

Commit

Permalink
Added craft 4 support
Browse files Browse the repository at this point in the history
  • Loading branch information
Roel van Hintum committed Mar 8, 2022
1 parent dcbf943 commit 0b89017
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 19 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.DS_Store
/vendor
/vendor
composer.lock
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/).

## 2.0.0-beta.1 - 2022-03-08
### Changed
- Added craft 4 support

## 1.3.1 - 2021-01-19
### Fixes
- Don't break twig when `enabled` setting is false. Thanks to @clarknelson
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
}
],
"require": {
"craftcms/cms": "^3.0.0"
"php": "^8.0.2",
"craftcms/cms": "^4.0.0-alpha"
},
"autoload": {
"psr-4": {
Expand Down
15 changes: 5 additions & 10 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,9 @@

class Plugin extends CraftPlugin
{
/**
* @var ContentSecurityPolicy
*/
public static $plugin;
public static Plugin $plugin;

/**
* @var string
*/
public $schemaVersion = '1.0.0';
public string $schemaVersion = '1.0.0';

/**
* Init the plugin.
Expand Down Expand Up @@ -62,8 +56,9 @@ function (Event $event) {

// Prevent loading when debug toolbar is on.
$user = Craft::$app->getUser()->getIdentity();

if ($user instanceof User && $user->getPreference('enableDebugToolbarForSite')) return;

Event::on(
View::class,
View::EVENT_END_PAGE,
Expand All @@ -80,7 +75,7 @@ function (Event $event) {
/**
* @inheritdoc
*/
protected function createSettingsModel()
protected function createSettingsModel(): Settings
{
return new Settings();
}
Expand Down
2 changes: 1 addition & 1 deletion src/models/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Settings extends Model
public $manifestSrc = [];
public $navigateTo = [];

public function rules()
public function rules(): array
{
return [
[['enabled'], 'boolean'],
Expand Down
7 changes: 4 additions & 3 deletions src/services/Headers.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,10 @@ public function setHeaders()

$cspValue = join('; ', $cspValues);

Craft::$app->response->headers->add('Content-Security-Policy', $cspValue . ';');
Craft::$app->response->headers->add('X-Content-Security-Policy', $cspValue . ';');
Craft::$app->response->headers->add('X-WebKit-CSP', $cspValue . ';');
Craft::$app->getResponse()->getHeaders()
->set('Content-Security-Policy', $cspValue . ';')
->set('X-Content-Security-Policy', $cspValue . ';')
->set('X-WebKit-CSP', $cspValue . ';');
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/twigextensions/ContentSecurityPolicyTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

use born05\contentsecuritypolicy\Plugin;

use Craft;
use Twig\TwigFunction;
use Twig\Extension\AbstractExtension;

class ContentSecurityPolicyTwigExtension extends \Twig_Extension
class ContentSecurityPolicyTwigExtension extends AbstractExtension
{
/**
* @return string
Expand All @@ -26,7 +27,7 @@ public function getName()
public function getFunctions()
{
return [
new \Twig_SimpleFunction('cspNonce', [$this, 'registerNonce']),
new TwigFunction('cspNonce', [$this, 'registerNonce']),
];
}

Expand Down

0 comments on commit 0b89017

Please sign in to comment.