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

Craft 4 compatibility #26

Open
wants to merge 1 commit into
base: v2
Choose a base branch
from
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ 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
### Added
- Craft CMS 4 compatibility

### Changed
- Requires Craft CMS >= 4.0

## 3.0.4.1 - 2020.12.24
### Fixed
- Snitch should again work without Two Factor Authentication
Expand Down
11 changes: 2 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "marionnewlevant/snitch",
"description": "Report when two people might be editing the same element (eg entry, category, or global) or field",
"type": "craft-plugin",
"version": "3.0.4.1",
"version": "4.0.0",
"keywords": [
"craft",
"cms",
Expand All @@ -22,7 +22,7 @@
}
],
"require": {
"craftcms/cms": "^3.5.0"
"craftcms/cms": "^4.0.0"
},
"autoload": {
"psr-4": {
Expand All @@ -32,13 +32,6 @@
"extra": {
"name": "Snitch",
"handle": "snitch",
"hasCpSettings": false,
"hasCpSection": false,
"documentationUrl": "https://github.com/marionnewlevant/craft-snitch/blob/v2/README.md",
"changelogUrl": "https://raw.githubusercontent.com/marionnewlevant/craft-snitch/v2/CHANGELOG.md",
"components": {
"collision": "marionnewlevant\\snitch\\services\\Collision"
},
"class": "marionnewlevant\\snitch\\Snitch"
}
}
13 changes: 6 additions & 7 deletions src/Snitch.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Snitch plugin for Craft CMS 3.x
* Snitch plugin for Craft CMS
*
* Report when two people might be editing the same entry, category, or global
*
Expand Down Expand Up @@ -47,9 +47,9 @@ class Snitch extends Plugin
* Static property that is an instance of this plugin class so that it can be accessed via
* Snitch::$plugin
*
* @var Snitch
* @var Snitch|null
*/
public static $plugin;
public static ?Snitch $plugin;

// Public Properties
// =========================================================================
Expand All @@ -59,7 +59,7 @@ class Snitch extends Plugin
*
* @var string
*/
public $schemaVersion = '2.1.0';
public string $schemaVersion = '2.1.0';

// Public Methods
// =========================================================================
Expand All @@ -81,7 +81,6 @@ public function init()
self::$plugin = $this;

$request = Craft::$app->getRequest();
$response = Craft::$app->getResponse();

if (!$this->isInstalled || $request->getIsConsoleRequest()) {
return;
Expand Down Expand Up @@ -133,9 +132,9 @@ public function init()
/**
* Creates and returns the model used to store the plugin’s settings.
*
* @return \craft\base\Model|null
* @return Settings
*/
protected function createSettingsModel()
protected function createSettingsModel(): Settings
{
return new Settings();
}
Expand Down
1 change: 0 additions & 1 deletion src/assetbundles/snitch/SnitchAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

namespace marionnewlevant\snitch\assetbundles\snitch;

use Craft;
use craft\web\AssetBundle;
use craft\web\assets\cp\CpAsset;

Expand Down
20 changes: 19 additions & 1 deletion src/assetbundles/snitch/dist/css/snitch.css
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ body.rtl .snitch {
background-color: #bf5035;
background-image: linear-gradient(#bf5035, #b34b3b);
color: white;
/* we are going to close them by animating max-height */
max-height: 3em;
}

.snitch--main>div {
/* we are going to close them by animating max-height */
transition: max-height .5s;
overflow: hidden;
}
Expand All @@ -47,6 +50,11 @@ body.rtl .snitch {
max-height: 0;
}

.snitch--modal>div.snitch--hidden {
padding: 0;
margin: 0;
}

/* the x for closing */
.snitch span {
cursor: pointer;
Expand All @@ -60,3 +68,13 @@ body.rtl .snitch {
color: white;
text-decoration: underline;
}

/* the style for the new slideouts */
.snitch--modal>div {
margin: calc(var(--xl)*-1) var(--neg-padding) var(--xl);
padding: var(--s) var(--xl);
}

.snitch--modal span {
padding: 0 0 0 .5em;
}
4 changes: 2 additions & 2 deletions src/assetbundles/snitch/dist/js/snitch.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ $('body').on('click', '.snitch span', function() {
var lookForConflicts = function(next) {
var myThis = this;
var $warnContainer = this.isModal ? this.$form.children('.snitch--modal') : $('.snitch--main');
if (this.isModal && !this.$form.closest('.hud.has-footer[style*="display: block;"]').length) {
if (this.isModal && !this.$form.closest('.slideout-container:not(.hidden)').length) {
// our modal is gone.
next();
} else {
Expand Down Expand Up @@ -118,7 +118,7 @@ var lookForEditFormsByType = function(snitchType, selector) {
var $thisIdInput = $(this);
var snitchId = $thisIdInput.val();
var $form = $thisIdInput.closest('form');
var isModal = $form.hasClass('body');
var isModal = $form.hasClass('slideout');
var snitchData = $form.data('snitch');

if (!snitchData) {
Expand Down
2 changes: 1 addition & 1 deletion src/config.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Snitch plugin for Craft CMS 3.x
* Snitch plugin for Craft CMS
*
* Report when two people might be editing the same entry, category, or global
*
Expand Down
26 changes: 13 additions & 13 deletions src/controllers/CollisionController.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Snitch plugin for Craft CMS 3.x
* Snitch plugin for Craft CMS
*
* Report when two people might be editing the same entry, category, or global
*
Expand All @@ -14,6 +14,7 @@

use Craft;
use craft\web\Controller;
use yii\web\Response;

/**
* Collision Controller
Expand Down Expand Up @@ -42,11 +43,11 @@ class CollisionController extends Controller
// =========================================================================

/**
* @var bool|array Allows anonymous access to this controller's actions.
* @var int|bool|array Allows anonymous access to this controller's actions.
* The actions must be in 'kebab-case'
* @access protected
*/
protected $allowAnonymous = ['ajax-enter', 'get-config'];
protected int|bool|array $allowAnonymous = ['ajax-enter', 'get-config'];

// Public Methods
// =========================================================================
Expand All @@ -58,20 +59,20 @@ class CollisionController extends Controller
* Called from the javascript regularly (every 2 seconds)
* to report that the thing is indeed being edited.
*
* @return mixed
* @return \yii\web\Response
* @throws \yii\web\BadRequestHttpException
*/
public function actionAjaxEnter()
public function actionAjaxEnter(): Response
{
$this->requireAcceptsJson();

// require login (gracefully)
$userSession = Craft::$app->getUser();
if ($userSession->getIsGuest()) {
$json = $this->asJson([
return $this->asJson([
'success' => false,
'error' => 'not logged in',
]);
return $json;
}

$snitchId = (int)(Craft::$app->getRequest()->getBodyParam('snitchId'));
Expand All @@ -87,29 +88,28 @@ public function actionAjaxEnter()
$collidingUsers = Snitch::$plugin->collision->collidingUsers($collisionModels);
$collisionMessages = Snitch::$plugin->collision->collisionMessages($collidingUsers, $messageTemplate);
// and return
$json = $this->asJson([
return $this->asJson([
'success' => true,
'collisions' => $collisionMessages,
]);
return $json;
}

/**
* Handle a request going to our plugin's actionGetConfig URL,
* e.g.: actions/snitch/collision/get-config
*
* @return mixed
* @return \yii\web\Response
* @throws \yii\web\BadRequestHttpException
*/
public function actionGetConfig()
public function actionGetConfig(): Response
{
$this->requireAcceptsJson();
$settings = Snitch::$plugin->getSettings();
$json = $this->asJson([
return $this->asJson([
'messageTemplate' => $settings['messageTemplate'],
'serverPollInterval' => $settings['serverPollInterval'],
'elementInputIdSelector' => $settings['elementInputIdSelector'],
'fieldInputIdSelector' => $settings['fieldInputIdSelector'],
]);
return $json;
}
}
5 changes: 2 additions & 3 deletions src/migrations/m190408_195351_support_multiple_types.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace marionnewlevant\snitch\migrations;

use Craft;
use craft\db\Migration;

/**
Expand Down Expand Up @@ -31,8 +30,8 @@ public function safeUp()

// foreign keys: our userId must be a user id
$this->addForeignKey(
$this->db->getForeignKeyName('{{%snitch_collisions}}', 'userId'),
'{{%snitch_collisions}}', 'userId', '{{%users}}', 'id', 'CASCADE', null);
$this->db->getForeignKeyName(),
'{{%snitch_collisions}}', 'userId', '{{%users}}', 'id', 'CASCADE');
}

/**
Expand Down
22 changes: 8 additions & 14 deletions src/models/Settings.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Snitch plugin for Craft CMS 3.x
* Snitch plugin for Craft CMS
*
* Report when two people might be editing the same entry, category, or global
*
Expand All @@ -10,11 +10,7 @@

namespace marionnewlevant\snitch\models;

use marionnewlevant\snitch\Snitch;

use Craft;
use craft\base\Model;
use craft\validators\ArrayValidator;

/**
* Snitch Settings Model
Expand All @@ -39,7 +35,7 @@ class Settings extends Model
const SERVERPOLLINTERVAL = 2;
const MESSAGE = 'May also be edited by: <a href="mailto:{{user.email}}">{{user.username}}</a>.';
const ELEMENT_INPUTIDSELECTOR = 'form input[type="hidden"][name="sourceId"]' // entry forms
.', form input[type="hidden"][name="elementId"]' // modals entry forms
.', form input[type="hidden"][name*="elementId"]' // slideout entry forms
.', form input[type="hidden"][name="setId"]' // global set
.', form input[type="hidden"][name="categoryId"]' // category
.', form input[type="hidden"][name="userId"]' // user
Expand All @@ -51,22 +47,22 @@ class Settings extends Model
*
* @var int
*/
public $serverPollInterval = self::SERVERPOLLINTERVAL;
public int $serverPollInterval = self::SERVERPOLLINTERVAL;

/**
* @var string
*/
public $messageTemplate = self::MESSAGE;
public string $messageTemplate = self::MESSAGE;

/**
* @var string
*/
public $elementInputIdSelector = self::ELEMENT_INPUTIDSELECTOR;
public string $elementInputIdSelector = self::ELEMENT_INPUTIDSELECTOR;

/**
* @var string
*/
public $fieldInputIdSelector = self::FIELD_INPUTIDSELECTOR;
public string $fieldInputIdSelector = self::FIELD_INPUTIDSELECTOR;


// Public Methods
Expand All @@ -82,7 +78,7 @@ class Settings extends Model
*
* @return array
*/
public function rules()
public function rules(): array
{
$rules = parent::rules();
$myRules = [
Expand All @@ -93,8 +89,6 @@ public function rules()
['elementInputIdSelector', 'string'],
['fieldInputIdSelector', 'string'],
];
$rules = array_merge($rules, $myRules);

return $rules;
return array_merge($rules, $myRules);
}
}
Loading