Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
Merge pull request #128 from Dolphiq/feature/make-compatible-with-php…
Browse files Browse the repository at this point in the history
…-8.1

Make plugin compatible with php 8.1 and Craft 4.0
  • Loading branch information
johanzandstra authored Jun 24, 2022
2 parents 17e9e37 + bafab0c commit 989b051
Show file tree
Hide file tree
Showing 14 changed files with 83 additions and 82 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Redirect Changelog
## 2.0.0 - 2022-06-24
### Changed
- Make compatible with Craft 4.0
- Make compatible php 8.0

## 1.1.1 - 2020-08-06

### Fixed
- Fixed plugin name in Composer to be in line with the plugin store

Expand Down
2 changes: 1 addition & 1 deletion INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Requirements

This plugin requires Craft CMS 3.0.14 or later.
This plugin requires Craft CMS 4.0 or later.

## Installation

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Craft CMS 3.x Redirect Manager
# Craft CMS 4.x Redirect Manager

Craft plugin that provides an easy way to enter and maintain 301 and 302 redirects. The plugin provides a clear user interface for admin and non-admin users.

Expand All @@ -11,7 +11,7 @@ As a new (experimental) feature the redirect also give you the ability to enable
**Note**: This plugin may become a paid add-on when the Craft Plugin store becomes available.

## Requirements
This plugin requires Craft CMS 3.4 or later.
This plugin requires Craft CMS 4.0 or later.

## Installation

Expand Down
12 changes: 5 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "dolphiq/redirect",
"description": "Craft redirect plugin provides an easy way to enter and maintain 301 and 302 redirects and 404 error pages.",
"type": "craft-plugin",
"version": "1.1.1",
"version": "2.0.0",
"keywords": [
"craft",
"cms",
Expand All @@ -12,6 +12,9 @@
"seo",
"craft3"
],
"platform": {
"php": "8.0|8.1"
},
"license": "MIT",
"support": {
"docs": "https://github.com/Dolphiq/craft3-plugin-redirect/blob/master/README.md",
Expand All @@ -22,15 +25,10 @@
"name": "Dolphiq",
"email": "info@dolphiq.nl",
"homepage": "https://dolphiq.nl/"
},
{
"name": "RJ Hallsted",
"email": "rjhallsted@gmail.com",
"homepage": "https://github.com/rjhallsted/"
}
],
"require": {
"craftcms/cms": "^3.0.14"
"craftcms/cms": "^4.0.0"
},
"autoload": {
"psr-4": {
Expand Down
41 changes: 23 additions & 18 deletions src/RedirectPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,21 @@
namespace dolphiq\redirect;

use Craft;
use craft\base\Plugin;
use craft\events\RegisterUrlRulesEvent;
use craft\feedme\events\RegisterFeedMeElementsEvent;
use craft\feedme\Plugin as FeedmePlugin;
use craft\feedme\services\Elements as FeedmeElements;
use craft\helpers\UrlHelper;
use craft\web\UrlManager;
use dolphiq\redirect\elements\FeedMeRedirect;
use dolphiq\redirect\models\Settings;
use dolphiq\redirect\services\Redirects;
use dolphiq\redirect\services\CatchAll;
use craft\events\RegisterUrlRulesEvent;
use craft\web\UrlManager;
use dolphiq\redirect\services\Redirects;
use yii\base\Event;
use yii\web\Response;


class RedirectPlugin extends \craft\base\Plugin
class RedirectPlugin extends Plugin
{
public static $plugin;

Expand All @@ -30,7 +35,7 @@ class RedirectPlugin extends \craft\base\Plugin
/**
* Returns the Redirects service.
*
* @return \dolphiq\redirect\services\Redirects The Redirects service
* @return Redirects The Redirects service
*/
public function getRedirects()
{
Expand All @@ -50,19 +55,19 @@ public function getCatchAll()
return $this->_catchAallService;
}

public $hasCpSection = true;
public $hasCpSettings = true;
public bool $hasCpSection = true;
public bool $hasCpSettings = true;

// table schema version
public $schemaVersion = '1.0.5';
public string $schemaVersion = '1.0.5';

/*
*
* The Craft plugin documentation points to the EVENT_REGISTER_CP_NAV_ITEMS event to register navigation items.
* The getCpNavItem was found in the source and will check the user privilages already.
*
*/
public function getCpNavItem()
public function getCpNavItem(): array
{
return [
'url' => 'redirect',
Expand All @@ -82,10 +87,10 @@ protected function createSettingsModel(): Settings
*
*/

public function getSettingsResponse()
public function getSettingsResponse(): Response
{
$url = \craft\helpers\UrlHelper::cpUrl('settings/redirect/settings');
return \Craft::$app->controller->redirect($url);
$url = UrlHelper::cpUrl('settings/redirect/settings');
return Craft::$app->controller->redirect($url);
}

/**
Expand All @@ -97,7 +102,7 @@ public function getSettingsResponse()
public function registerCpUrlRules(RegisterUrlRulesEvent $event)
{
// only register CP URLs if the user is logged in
if (!\Craft::$app->user->identity)
if (!Craft::$app->user->identity)
return;
$rules = [
// register routes for the sub nav
Expand Down Expand Up @@ -137,8 +142,8 @@ public function registerCpUrlRules(RegisterUrlRulesEvent $event)
*/
private function registerFeedMeElement()
{
if (Craft::$app->plugins->isPluginEnabled('feed-me') && class_exists(\craft\feedme\Plugin::class)) {
Event::on(\craft\feedme\services\Elements::class, \craft\feedme\services\Elements::EVENT_REGISTER_FEED_ME_ELEMENTS, function (\craft\feedme\events\RegisterFeedMeElementsEvent $e) {
if (Craft::$app->plugins->isPluginEnabled('feed-me') && class_exists(FeedmePlugin::class)) {
Event::on(FeedmeElements::class, FeedmeElements::EVENT_REGISTER_FEED_ME_ELEMENTS, function (RegisterFeedMeElementsEvent $e) {
$e->elements[] = FeedMeRedirect::class;
});
}
Expand All @@ -155,7 +160,7 @@ public function init()
Event::on(UrlManager::class, UrlManager::EVENT_REGISTER_CP_URL_RULES, [$this, 'registerCpUrlRules']);

// Register FeedMe ElementType
$this->registerFeedMeElement();
$this->registerFeedMeElement();

$settings = RedirectPlugin::$plugin->getSettings();
if ($settings->redirectsActive) {
Expand Down Expand Up @@ -188,7 +193,7 @@ public function init()
];
}

$event->rules = array_merge($activeRules ,$event->rules);
$event->rules = array_merge($activeRules, $event->rules);
}
// 404?
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/RedirectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
class RedirectController extends Controller
{
private $_sourceRouteParams = [];
protected $allowAnonymous = ['index'];
protected array|int|bool $allowAnonymous = ['index'];

const EVENT_BEFORE_CATCHALL = 'beforeCatchall';

Expand Down
34 changes: 15 additions & 19 deletions src/controllers/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,11 @@
namespace dolphiq\redirect\controllers;

use Craft;
use craft\web\Controller;
use craft\helpers\UrlHelper;

use dolphiq\redirect\RedirectPlugin;
use dolphiq\redirect\elements\Redirect;

use craft\records\Element as ElementRecord;

use craft\db\Query;
use craft\web\Controller;
use dolphiq\redirect\elements\Redirect;
use dolphiq\redirect\RedirectPlugin;

class SettingsController extends Controller
{
Expand Down Expand Up @@ -186,7 +182,7 @@ public function actionSettings(): craft\web\Response
*
* @return Response
*/
public function actionSaveSettings(): craft\web\Response
public function actionSaveSettings(): ?craft\web\Response
{
$this->requirePostRequest();
$this->requireAdmin();
Expand Down Expand Up @@ -219,7 +215,7 @@ public function actionSaveSettings(): craft\web\Response

Craft::$app->getSession()->setNotice(Craft::t('app', 'Plugin settings saved.'));

return $this->redirectToPostedUrl($newSettings);
return $this->redirectToPostedUrl((object)$newSettings);
}

/**
Expand Down Expand Up @@ -335,8 +331,8 @@ public function actionSaveRedirect()
}

$elementRecord = ElementRecord::findOne($redirect->id);
if($elementRecord) {
$redirect->uid = $elementRecord->uid;
if ($elementRecord) {
$redirect->uid = $elementRecord->uid;
}

$redirect->siteId = $siteId;
Expand All @@ -359,14 +355,14 @@ public function actionSaveRedirect()

return null;
} else {
// remove form other sites
Craft::$app->getDb()->createCommand()
->delete('{{%elements_sites}}', [
'AND',
['elementId' => $redirect->id],
['!=', 'siteId', $siteId]
])
->execute();
// remove form other sites
Craft::$app->getDb()->createCommand()
->delete('{{%elements_sites}}', [
'AND',
['elementId' => $redirect->id],
['!=', 'siteId', $siteId]
])
->execute();

if ($request->getAcceptsJson()) {
return $this->asJson([
Expand Down
22 changes: 10 additions & 12 deletions src/elements/FeedMeRedirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

namespace dolphiq\redirect\elements;

use craft\feedme\Base\Element;
use craft\feedme\Base\ElementInterface;

use Cake\Utility\Hash;
use Craft;
use craft\base\Element as BaseElement;

use Cake\Utility\Hash;
use craft\feedme\Base\Element;
use craft\feedme\Base\ElementInterface;
use dolphiq\redirect\elements\Redirect as RedirectElement;

class FeedMeRedirect extends Element implements ElementInterface
Expand All @@ -25,17 +23,17 @@ class FeedMeRedirect extends Element implements ElementInterface
// Templates
// =========================================================================

public function getGroupsTemplate()
public function getGroupsTemplate(): string
{
return 'redirect/_feed-me/groups';
}

public function getColumnTemplate()
public function getColumnTemplate(): string
{
return 'redirect/_feed-me/column';
}

public function getMappingTemplate()
public function getMappingTemplate(): string
{
return 'redirect/_feed-me/map';
}
Expand All @@ -44,12 +42,12 @@ public function getMappingTemplate()
// Public Methods
// =========================================================================

public function getGroups()
public function getGroups(): array
{
return [];
}

public function getQuery($settings, $params = [])
public function getQuery($settings, $params = []): mixed
{
$query = RedirectElement::find();

Expand All @@ -70,7 +68,7 @@ public function getQuery($settings, $params = [])
return $query;
}

public function setModel($settings)
public function setModel($settings): Redirect
{
$this->element = new RedirectElement();

Expand All @@ -83,7 +81,7 @@ public function setModel($settings)
return $this->element;
}

public function save($element, $settings)
public function save($element, $settings): bool
{
$this->element = $element;

Expand Down
Loading

0 comments on commit 989b051

Please sign in to comment.