diff --git a/src/Retour.php b/src/Retour.php index 6c662ec7..112c52b2 100644 --- a/src/Retour.php +++ b/src/Retour.php @@ -56,6 +56,7 @@ * @author nystudio107 * @package Retour * @since 3.0.0 + * @method Settings getSettings() */ class Retour extends Plugin { @@ -241,7 +242,7 @@ protected function installEventListeners(): void Event::on( ClearCaches::class, ClearCaches::EVENT_REGISTER_CACHE_OPTIONS, - function(RegisterCacheOptionsEvent $event) { + function (RegisterCacheOptionsEvent $event) { Craft::debug( 'ClearCaches::EVENT_REGISTER_CACHE_OPTIONS', __METHOD__ @@ -257,7 +258,7 @@ function(RegisterCacheOptionsEvent $event) { Event::on( Plugins::class, Plugins::EVENT_AFTER_INSTALL_PLUGIN, - function(PluginEvent $event) { + function (PluginEvent $event) { if ($event->plugin === $this) { // Invalidate our caches after we've been installed $this->clearAllCaches(); @@ -296,7 +297,7 @@ protected function installGlobalEventListeners(): void Event::on( CraftVariable::class, CraftVariable::EVENT_INIT, - function(Event $event) { + function (Event $event) { /** @var CraftVariable $variable */ $variable = $event->sender; $variable->set('retour', [ @@ -306,10 +307,10 @@ function(Event $event) { } ); - $prepareRedirectOnElementChange = function(ElementEvent $event) { + $prepareRedirectOnElementChange = function (ElementEvent $event) { /** @var Element $element */ $element = $event->element; - if ($element !== null && !$event->isNew && $element->getUrl() !== null && !$element->propagating) { + if (!$event->isNew && $element->getUrl() !== null && !$element->propagating) { $checkElementSlug = true; // If we're running Craft 3.2 or later, also check that isn't not a draft or revision if (ElementHelper::isDraftOrRevision($element)) { @@ -320,14 +321,14 @@ function(Event $event) { if (self::$settings->createUriChangeRedirects && $checkElementSlug) { // Make sure this isn't a transitioning temporary draft/revision and that it's // not propagating to other sites - if (strpos($element->uri, '__temp_') === false && !$element->propagating) { + if (!str_contains($element->uri, '__temp_')) { Retour::$plugin->events->stashElementUris($element); } } } }; - $insertRedirectOnElementChange = function(ElementEvent $event) { + $insertRedirectOnElementChange = function (ElementEvent $event) { /** @var Element $element */ $element = $event->element; if ($element !== null && !$event->isNew && $element->getUrl() !== null) { @@ -345,7 +346,7 @@ function(Event $event) { Event::on( Elements::class, Elements::EVENT_BEFORE_SAVE_ELEMENT, - static function(ElementEvent $event) use ($prepareRedirectOnElementChange) { + static function (ElementEvent $event) use ($prepareRedirectOnElementChange) { Craft::debug( 'Elements::EVENT_BEFORE_SAVE_ELEMENT', __METHOD__ @@ -357,7 +358,7 @@ static function(ElementEvent $event) use ($prepareRedirectOnElementChange) { Event::on( Elements::class, Elements::EVENT_AFTER_SAVE_ELEMENT, - static function(ElementEvent $event) use ($insertRedirectOnElementChange) { + static function (ElementEvent $event) use ($insertRedirectOnElementChange) { Craft::debug( 'Elements::EVENT_AFTER_SAVE_ELEMENT', __METHOD__ @@ -369,7 +370,7 @@ static function(ElementEvent $event) use ($insertRedirectOnElementChange) { Event::on( Elements::class, Elements::EVENT_BEFORE_UPDATE_SLUG_AND_URI, - static function(ElementEvent $event) use ($prepareRedirectOnElementChange) { + static function (ElementEvent $event) use ($prepareRedirectOnElementChange) { Craft::debug( 'Elements::EVENT_BEFORE_UPDATE_SLUG_AND_URI', __METHOD__ @@ -381,7 +382,7 @@ static function(ElementEvent $event) use ($prepareRedirectOnElementChange) { Event::on( Elements::class, Elements::EVENT_AFTER_UPDATE_SLUG_AND_URI, - static function(ElementEvent $event) use ($insertRedirectOnElementChange) { + static function (ElementEvent $event) use ($insertRedirectOnElementChange) { Craft::debug( 'Elements::EVENT_AFTER_UPDATE_SLUG_AND_URI', __METHOD__ @@ -393,7 +394,7 @@ static function(ElementEvent $event) use ($insertRedirectOnElementChange) { Event::on( Plugins::class, Plugins::EVENT_AFTER_LOAD_PLUGINS, - function() { + function () { // Install these only after all other plugins have loaded $request = Craft::$app->getRequest(); // Only respond to non-console site requests @@ -410,7 +411,7 @@ function() { Event::on( Fields::class, Fields::EVENT_REGISTER_FIELD_TYPES, - function(RegisterComponentTypesEvent $event) { + function (RegisterComponentTypesEvent $event) { $event->types[] = ShortLinkField::class; } ); @@ -418,7 +419,7 @@ function(RegisterComponentTypesEvent $event) { Event::on( Gql::class, Gql::EVENT_REGISTER_GQL_TYPES, - static function(RegisterGqlTypesEvent $event) { + static function (RegisterGqlTypesEvent $event) { Craft::debug( 'Gql::EVENT_REGISTER_GQL_TYPES', __METHOD__ @@ -430,7 +431,7 @@ static function(RegisterGqlTypesEvent $event) { Event::on( Gql::class, Gql::EVENT_REGISTER_GQL_QUERIES, - static function(RegisterGqlQueriesEvent $event) { + static function (RegisterGqlQueriesEvent $event) { Craft::debug( 'Gql::EVENT_REGISTER_GQL_QUERIES', __METHOD__ @@ -445,7 +446,7 @@ static function(RegisterGqlQueriesEvent $event) { Event::on( Gql::class, Gql::EVENT_REGISTER_GQL_SCHEMA_COMPONENTS, - static function(RegisterGqlSchemaComponentsEvent $event) { + static function (RegisterGqlSchemaComponentsEvent $event) { Craft::debug( 'Gql::EVENT_REGISTER_GQL_SCHEMA_COMPONENTS', __METHOD__ @@ -467,7 +468,7 @@ protected function handleSiteRequest(): void Event::on( ErrorHandler::class, ErrorHandler::EVENT_BEFORE_HANDLE_EXCEPTION, - static function(ExceptionEvent $event) { + static function (ExceptionEvent $event) { Craft::debug( 'ErrorHandler::EVENT_BEFORE_HANDLE_EXCEPTION', __METHOD__ @@ -505,7 +506,7 @@ protected function installSiteEventListeners(): void Event::on( UrlManager::class, UrlManager::EVENT_REGISTER_SITE_URL_RULES, - function(RegisterUrlRulesEvent $event) { + function (RegisterUrlRulesEvent $event) { Craft::debug( 'UrlManager::EVENT_REGISTER_SITE_URL_RULES', __METHOD__ @@ -539,7 +540,7 @@ protected function installCpEventListeners(): void Event::on( Dashboard::class, Dashboard::EVENT_REGISTER_WIDGET_TYPES, - function(RegisterComponentTypesEvent $event) { + function (RegisterComponentTypesEvent $event) { $currentUser = Craft::$app->getUser()->getIdentity(); if ($currentUser->can('accessPlugin-retour')) { $event->types[] = RetourWidget::class; @@ -550,7 +551,7 @@ function(RegisterComponentTypesEvent $event) { Event::on( UrlManager::class, UrlManager::EVENT_REGISTER_CP_URL_RULES, - function(RegisterUrlRulesEvent $event) { + function (RegisterUrlRulesEvent $event) { Craft::debug( 'UrlManager::EVENT_REGISTER_CP_URL_RULES', __METHOD__ @@ -566,7 +567,7 @@ function(RegisterUrlRulesEvent $event) { Event::on( UserPermissions::class, UserPermissions::EVENT_REGISTER_PERMISSIONS, - function(RegisterUserPermissionsEvent $event) { + function (RegisterUserPermissionsEvent $event) { Craft::debug( 'UserPermissions::EVENT_REGISTER_PERMISSIONS', __METHOD__ diff --git a/src/controllers/ApiController.php b/src/controllers/ApiController.php index a2ff7e75..3dc34597 100644 --- a/src/controllers/ApiController.php +++ b/src/controllers/ApiController.php @@ -58,6 +58,6 @@ public function actionGetRedirects($siteId = null): Response { $redirects = Retour::$plugin->redirects->getAllStaticRedirects(null, $siteId); - return $this->asJson($redirects ?? []); + return $this->asJson($redirects); } } diff --git a/src/controllers/ChartsController.php b/src/controllers/ChartsController.php index 64f3855f..ae94849b 100644 --- a/src/controllers/ChartsController.php +++ b/src/controllers/ChartsController.php @@ -33,7 +33,7 @@ class ChartsController extends Controller // ========================================================================= /** - * @var bool|array + * @inheritdoc */ protected array|bool|int $allowAnonymous = [ ]; diff --git a/src/controllers/FileController.php b/src/controllers/FileController.php index fcc92fc3..a8cf494c 100644 --- a/src/controllers/FileController.php +++ b/src/controllers/FileController.php @@ -32,6 +32,7 @@ use yii\base\InvalidConfigException; use yii\web\BadRequestHttpException; use yii\web\ForbiddenHttpException; +use yii\web\MethodNotAllowedHttpException; use yii\web\NotFoundHttpException; use yii\web\Response; use yii\web\UploadedFile; @@ -95,8 +96,10 @@ class FileController extends Controller /** * @throws BadRequestHttpException + * @throws Exception * @throws ForbiddenHttpException * @throws MissingComponentException + * @throws MethodNotAllowedHttpException */ public function actionImportCsvColumns(): void { @@ -106,6 +109,7 @@ public function actionImportCsvColumns(): void if (!ini_get('auto_detect_line_endings')) { ini_set('auto_detect_line_endings', '1'); } + $csv = null; $this->requirePostRequest(); $filename = Craft::$app->getRequest()->getRequiredBodyParam('filename'); $columns = Craft::$app->getRequest()->getRequiredBodyParam('columns'); @@ -137,7 +141,7 @@ public function actionImportCsvColumns(): void } $hasErrors = false; // If we have headers, then we have a file, so parse it - if ($headers !== null) { + if ($csv && $headers) { switch (VersionHelper::getLeagueCsvVersion()) { case 8: $hasErrors = $this->importCsvApi8($csv, $columns, $headers); @@ -162,85 +166,6 @@ public function actionImportCsvColumns(): void } } - /** - * @param AbstractCsv $csv - * @param array $columns - * @param array $headers - * @return bool whether the import has any errors - */ - protected function importCsvApi8(AbstractCsv $csv, array $columns, array $headers): bool - { - $hasErrors = false; - $csv->setOffset(1); - $columns = ArrayHelper::filterEmptyStringsFromArray($columns); - $rowIndex = 1; - $csv->each(function($row) use ($headers, $columns, &$rowIndex, &$hasErrors) { - $redirectConfig = [ - 'id' => 0, - ]; - $index = 0; - foreach (self::IMPORT_REDIRECTS_CSV_FIELDS as $importField) { - if (isset($columns[$index], $headers[$columns[$index]])) { - $redirectConfig[$importField] = empty($row[$headers[$columns[$index]]]) - ? null - : $row[$headers[$columns[$index]]]; - } - $index++; - } - $redirectDump = print_r($redirectConfig, true); - Craft::debug("-> ROW #$rowIndex contents: " . $redirectDump, __METHOD__); - if (!Retour::$plugin->redirects->saveRedirect($redirectConfig)) { - Craft::info("-> ROW #$rowIndex contents: " . $redirectDump, __METHOD__); - $hasErrors = true; - } - $rowIndex++; - - return true; - }); - - return $hasErrors; - } - - /** - * @param AbstractCsv $csv - * @param array $columns - * @param array $headers - * @return bool whether the import has any errors - * @throws Exception - */ - protected function importCsvApi9(AbstractCsv $csv, array $columns, array $headers): bool - { - $hasErrors = false; - $stmt = (new Statement()) - ->offset(1); - $rows = $stmt->process($csv); - $columns = ArrayHelper::filterEmptyStringsFromArray($columns); - $rowIndex = 1; - foreach ($rows as $row) { - $redirectConfig = [ - 'id' => 0, - ]; - $index = 0; - foreach (self::IMPORT_REDIRECTS_CSV_FIELDS as $importField) { - if (isset($columns[$index], $headers[$columns[$index]])) { - $redirectConfig[$importField] = empty($row[$headers[$columns[$index]]]) - ? null - : $row[$headers[$columns[$index]]]; - } - $index++; - } - $redirectDump = print_r($redirectConfig, true); - Craft::debug("-> ROW #$rowIndex contents: " . $redirectDump, __METHOD__); - if (!Retour::$plugin->redirects->saveRedirect($redirectConfig)) { - Craft::info("-> ROW #$rowIndex contents: " . $redirectDump, __METHOD__); - $hasErrors = true; - } - $rowIndex++; - } - - return $hasErrors; - } - /** * @param string|null $siteHandle * @@ -370,10 +295,6 @@ public function actionDisplayErrors(): Response return $this->renderTemplate('retour/import/errors', $variables); } - - // Public Methods - // ========================================================================= - /** * Export the statistics table as a CSV file * @@ -390,6 +311,107 @@ public function actionExportStatistics(): void $this->exportCsvFile('retour-statistics', '{{%retour_stats}}', $fields); } + /** + * Export the redirects table as a CSV file + * + * @throws ForbiddenHttpException + */ + public function actionExportRedirects(): void + { + PermissionHelper::controllerPermissionCheck('retour:redirects'); + //Allow the fields to be localized + $fields = self::EXPORT_REDIRECTS_CSV_FIELDS; + foreach ($fields as $key => $field) { + $fields[$key] = Craft::t('retour', $field); + } + $this->exportCsvFile('retour-redirects', '{{%retour_static_redirects}}', $fields); + } + + + // Public Methods + // ========================================================================= + + /** + * @param AbstractCsv $csv + * @param array $columns + * @param array $headers + * @return bool whether the import has any errors + */ + protected function importCsvApi8(AbstractCsv $csv, array $columns, array $headers): bool + { + $hasErrors = false; + /** @phpstan-ignore-next-line */ + $csv->setOffset(1); + $columns = ArrayHelper::filterEmptyStringsFromArray($columns); + $rowIndex = 1; + /** @phpstan-ignore-next-line */ + $csv->each(function ($row) use ($headers, $columns, &$rowIndex, &$hasErrors) { + $redirectConfig = [ + 'id' => 0, + ]; + $index = 0; + foreach (self::IMPORT_REDIRECTS_CSV_FIELDS as $importField) { + if (isset($columns[$index], $headers[$columns[$index]])) { + $redirectConfig[$importField] = empty($row[$headers[$columns[$index]]]) + ? null + : $row[$headers[$columns[$index]]]; + } + $index++; + } + $redirectDump = print_r($redirectConfig, true); + Craft::debug("-> ROW #$rowIndex contents: " . $redirectDump, __METHOD__); + if (!Retour::$plugin->redirects->saveRedirect($redirectConfig)) { + Craft::info("-> ROW #$rowIndex contents: " . $redirectDump, __METHOD__); + $hasErrors = true; + } + $rowIndex++; + + return true; + }); + + return $hasErrors; + } + + /** + * @param Reader $csv + * @param array $columns + * @param array $headers + * @return bool whether the import has any errors + * @throws Exception + */ + protected function importCsvApi9(Reader $csv, array $columns, array $headers): bool + { + $hasErrors = false; + $stmt = (new Statement()) + ->offset(1); + $rows = $stmt->process($csv); + $columns = ArrayHelper::filterEmptyStringsFromArray($columns); + $rowIndex = 1; + foreach ($rows as $row) { + $redirectConfig = [ + 'id' => 0, + ]; + $index = 0; + foreach (self::IMPORT_REDIRECTS_CSV_FIELDS as $importField) { + if (isset($columns[$index], $headers[$columns[$index]])) { + $redirectConfig[$importField] = empty($row[$headers[$columns[$index]]]) + ? null + : $row[$headers[$columns[$index]]]; + } + $index++; + } + $redirectDump = print_r($redirectConfig, true); + Craft::debug("-> ROW #$rowIndex contents: " . $redirectDump, __METHOD__); + if (!Retour::$plugin->redirects->saveRedirect($redirectConfig)) { + Craft::info("-> ROW #$rowIndex contents: " . $redirectDump, __METHOD__); + $hasErrors = true; + } + $rowIndex++; + } + + return $hasErrors; + } + /** * @param string $filename * @param string $table @@ -420,20 +442,4 @@ protected function exportCsvFile(string $filename, string $table, array $columns $csv->output($filename . '.csv'); exit(0); } - - /** - * Export the redirects table as a CSV file - * - * @throws ForbiddenHttpException - */ - public function actionExportRedirects(): void - { - PermissionHelper::controllerPermissionCheck('retour:redirects'); - //Allow the fields to be localized - $fields = self::EXPORT_REDIRECTS_CSV_FIELDS; - foreach ($fields as $key => $field) { - $fields[$key] = Craft::t('retour', $field); - } - $this->exportCsvFile('retour-redirects', '{{%retour_static_redirects}}', $fields); - } } diff --git a/src/controllers/RedirectsController.php b/src/controllers/RedirectsController.php index 8184cf80..0462977e 100644 --- a/src/controllers/RedirectsController.php +++ b/src/controllers/RedirectsController.php @@ -12,18 +12,22 @@ namespace nystudio107\retour\controllers; use Craft; +use craft\errors\ElementNotFoundException; use craft\errors\MissingComponentException; use craft\helpers\UrlHelper; use craft\web\Controller; +use craft\web\UrlManager; use nystudio107\retour\assetbundles\retour\RetourAsset; use nystudio107\retour\assetbundles\retour\RetourRedirectsAsset; use nystudio107\retour\helpers\MultiSite as MultiSiteHelper; use nystudio107\retour\helpers\Permission as PermissionHelper; use nystudio107\retour\models\StaticRedirects as StaticRedirectsModel; use nystudio107\retour\Retour; +use yii\base\Exception; use yii\base\InvalidConfigException; use yii\web\BadRequestHttpException; use yii\web\ForbiddenHttpException; +use yii\web\MethodNotAllowedHttpException; use yii\web\NotFoundHttpException; use yii\web\Response; @@ -119,7 +123,8 @@ public function actionEditRedirect( string $defaultUrl = '', int $siteId = 0, StaticRedirectsModel $redirect = null, - ): Response { + ): Response + { $variables = []; PermissionHelper::controllerPermissionCheck('retour:redirects'); @@ -206,9 +211,10 @@ public function actionEditRedirect( } /** - * @return Response|void + * @return ?Response * @throws MissingComponentException * @throws ForbiddenHttpException + * @throws BadRequestHttpException */ public function actionDeleteRedirects(): ?Response { @@ -242,12 +248,12 @@ public function actionDeleteRedirects(): ?Response * @throws BadRequestHttpException * @throws ForbiddenHttpException * @throws NotFoundHttpException + * @throws MethodNotAllowedHttpException */ public function actionSaveRedirect(): ?Response { PermissionHelper::controllerPermissionCheck('retour:redirects'); $this->requirePostRequest(); - /** @var StaticRedirectsModel $redirect */ $redirectConfig = Craft::$app->getRequest()->getRequiredBodyParam('redirectConfig'); if ($redirectConfig === null) { throw new NotFoundHttpException('Redirect not found'); @@ -269,9 +275,12 @@ public function actionSaveRedirect(): ?Response if (!$redirect->validate()) { Craft::$app->getSession()->setError(Craft::t('app', "Couldn't save redirect settings.")); // Send the redirect back to the template - Craft::$app->getUrlManager()->setRouteParams([ - 'redirect' => $redirect, - ]); + $urlManager = Craft::$app->getUrlManager(); + if ($urlManager instanceof UrlManager) { + $urlManager->setRouteParams([ + 'redirect' => $redirect, + ]); + } return null; } @@ -286,9 +295,12 @@ public function actionSaveRedirect(): ?Response if ($testRedirectConfig === null) { Craft::$app->getSession()->setError(Craft::t('app', "Couldn't save redirect settings because it'd create a redirect loop.")); // Send the redirect back to the template - Craft::$app->getUrlManager()->setRouteParams([ - 'redirect' => $redirect, - ]); + $urlManager = Craft::$app->getUrlManager(); + if ($urlManager instanceof UrlManager) { + $urlManager->setRouteParams([ + 'redirect' => $redirect, + ]); + } return null; } @@ -356,8 +368,12 @@ public function actionShortlinks(string $siteHandle = null): Response /** * @return Response|void - * @throws \craft\errors\MissingComponentException - * @throws \yii\web\ForbiddenHttpException + * @throws BadRequestHttpException + * @throws ForbiddenHttpException + * @throws MissingComponentException + * @throws \Throwable + * @throws ElementNotFoundException + * @throws Exception */ public function actionDeleteShortlinks() { @@ -366,7 +382,7 @@ public function actionDeleteShortlinks() $redirectIds = $request->getRequiredBodyParam('redirectIds'); $stickyError = false; foreach ($redirectIds as $redirectId) { - if (Retour::$plugin->redirects->deleteShortlinkById($redirectId) === 0) { + if (Retour::$plugin->redirects->deleteShortlinkById($redirectId)) { $stickyError = true; } } diff --git a/src/controllers/SettingsController.php b/src/controllers/SettingsController.php index 52a0191e..c1367d43 100644 --- a/src/controllers/SettingsController.php +++ b/src/controllers/SettingsController.php @@ -15,6 +15,7 @@ use craft\errors\MissingComponentException; use craft\helpers\UrlHelper; use craft\web\Controller; +use craft\web\UrlManager; use nystudio107\retour\assetbundles\retour\RetourAsset; use nystudio107\retour\helpers\Permission as PermissionHelper; use nystudio107\retour\models\Settings; @@ -128,9 +129,12 @@ public function actionSavePluginSettings(): ?Response Craft::$app->getSession()->setError(Craft::t('app', "Couldn't save plugin settings.")); // Send the plugin back to the template - Craft::$app->getUrlManager()->setRouteParams([ - 'plugin' => $plugin, - ]); + $urlManager = Craft::$app->getUrlManager(); + if ($urlManager instanceof UrlManager) { + $urlManager->setRouteParams([ + 'plugin' => $plugin, + ]); + } return null; } diff --git a/src/controllers/StatisticsController.php b/src/controllers/StatisticsController.php index 305655f6..70072c01 100644 --- a/src/controllers/StatisticsController.php +++ b/src/controllers/StatisticsController.php @@ -131,7 +131,7 @@ public function actionClearStatistics(): Response } /** - * @return Response|void + * @return ?Response * @throws MissingComponentException * @throws BadRequestHttpException * @throws ForbiddenHttpException diff --git a/src/controllers/TablesController.php b/src/controllers/TablesController.php index 124bb85b..0bb26f1a 100644 --- a/src/controllers/TablesController.php +++ b/src/controllers/TablesController.php @@ -66,7 +66,7 @@ class TablesController extends Controller // ========================================================================= /** - * @var bool|array + * @inheritdoc */ protected array|bool|int $allowAnonymous = [ ]; @@ -95,7 +95,8 @@ public function actionDashboard( $filter = '', $siteId = 0, $handled = 'all', - ): Response { + ): Response + { PermissionHelper::controllerPermissionCheck('retour:dashboard'); $data = []; $sortField = 'hitCount'; @@ -177,7 +178,7 @@ public function actionDashboard( * @param int $page * @param int $per_page * @param string $filter - * @param null $siteId + * @param int $siteId * * @return Response * @throws ForbiddenHttpException @@ -190,7 +191,8 @@ public function actionRedirects( $filter = '', $siteId = 0, $shortLinks = false, - ): Response { + ): Response + { PermissionHelper::controllerPermissionCheck('retour:redirects'); $data = []; $sortField = 'hitCount'; diff --git a/src/gql/resolvers/RetourResolver.php b/src/gql/resolvers/RetourResolver.php index 5f510538..6b960cef 100644 --- a/src/gql/resolvers/RetourResolver.php +++ b/src/gql/resolvers/RetourResolver.php @@ -14,7 +14,6 @@ use Craft; use craft\base\Element; use craft\gql\base\Resolver; - use craft\helpers\UrlHelper; use GraphQL\Type\Definition\ResolveInfo; use nystudio107\retour\Retour; @@ -64,6 +63,7 @@ public static function resolve(mixed $source, array $arguments, mixed $context, if ($redirect === null && Craft::$app->getElements()->getElementByUri(trim($uri, '/'), $siteId) === null) { // Set the `site` virtual field $redirect['site'] = null; + $redirect['siteId'] = $siteId; if (isset($redirect['siteId']) && (int)$redirect['siteId'] !== 0) { $site = Craft::$app->getSites()->getSiteById((int)$redirect['siteId']); if ($site !== null) { diff --git a/src/helpers/MultiSite.php b/src/helpers/MultiSite.php index 03edd317..01049e1c 100644 --- a/src/helpers/MultiSite.php +++ b/src/helpers/MultiSite.php @@ -12,7 +12,6 @@ namespace nystudio107\retour\helpers; use Craft; -use craft\models\Site; use yii\web\ForbiddenHttpException; use yii\web\NotFoundHttpException; use function count; @@ -47,7 +46,6 @@ public static function setSitesMenuVariables(array &$variables): void $sites = Craft::$app->getSites(); if (Craft::$app->getIsMultiSite()) { $editableSites = $sites->getEditableSiteIds(); - /** @var Site $site */ foreach ($sites->getAllGroups() as $group) { $groupSites = $sites->getSitesByGroupId($group->id); $variables['sitesMenu'][$group->name] @@ -77,7 +75,6 @@ public static function setMultiSiteVariables($siteHandle, &$siteId, array &$vari $variables['enabledSiteIds'] = []; $variables['siteIds'] = []; - /** @var Site $site */ foreach ($sites->getEditableSiteIds() as $editableSiteId) { $variables['enabledSiteIds'][] = $editableSiteId; $variables['siteIds'][] = $editableSiteId; diff --git a/src/migrations/Install.php b/src/migrations/Install.php index fe6714b2..2030e59c 100644 --- a/src/migrations/Install.php +++ b/src/migrations/Install.php @@ -55,6 +55,20 @@ public function safeUp(): bool return true; } + /** + * @inheritdoc + */ + public function safeDown(): bool + { + $this->driver = Craft::$app->getConfig()->getDb()->driver; + $this->removeTables(); + + return true; + } + + // Protected Methods + // ========================================================================= + /** * @return bool */ @@ -145,9 +159,6 @@ protected function createTables(): bool return $tablesCreated; } - // Protected Methods - // ========================================================================= - /** * @return void */ @@ -230,7 +241,7 @@ protected function createIndexes(): void protected function addForeignKeys(): void { $this->addForeignKey( - $this->db->getForeignKeyName('{{%retour_redirects}}', 'associatedElementId'), + $this->db->getForeignKeyName(), '{{%retour_redirects}}', 'associatedElementId', '{{%elements}}', @@ -240,7 +251,7 @@ protected function addForeignKeys(): void ); $this->addForeignKey( - $this->db->getForeignKeyName('{{%retour_static_redirects}}', 'siteId'), + $this->db->getForeignKeyName(), '{{%retour_static_redirects}}', 'siteId', '{{%sites}}', @@ -250,7 +261,7 @@ protected function addForeignKeys(): void ); $this->addForeignKey( - $this->db->getForeignKeyName('{{%retour_stats}}', 'siteId'), + $this->db->getForeignKeyName(), '{{%retour_stats}}', 'siteId', '{{%sites}}', @@ -267,17 +278,6 @@ protected function insertDefaultData(): void { } - /** - * @inheritdoc - */ - public function safeDown(): bool - { - $this->driver = Craft::$app->getConfig()->getDb()->driver; - $this->removeTables(); - - return true; - } - /** * @return void */ diff --git a/src/migrations/m181213_233502_add_site_id.php b/src/migrations/m181213_233502_add_site_id.php index 2548f3b0..05375fe5 100644 --- a/src/migrations/m181213_233502_add_site_id.php +++ b/src/migrations/m181213_233502_add_site_id.php @@ -59,13 +59,22 @@ public function safeUp(): bool return true; } + /** + * @inheritdoc + */ + public function safeDown() + { + echo "m181213_233502_add_site_id cannot be reverted.\n"; + return false; + } + /** * @return void */ protected function addForeignKeys(): void { $this->addForeignKey( - $this->db->getForeignKeyName('{{%retour_static_redirects}}', 'siteId'), + $this->db->getForeignKeyName(), '{{%retour_static_redirects}}', 'siteId', '{{%sites}}', @@ -75,7 +84,7 @@ protected function addForeignKeys(): void ); $this->addForeignKey( - $this->db->getForeignKeyName('{{%retour_stats}}', 'siteId'), + $this->db->getForeignKeyName(), '{{%retour_stats}}', 'siteId', '{{%sites}}', @@ -111,13 +120,4 @@ protected function createIndexes(): void false ); } - - /** - * @inheritdoc - */ - public function safeDown() - { - echo "m181213_233502_add_site_id cannot be reverted.\n"; - return false; - } } diff --git a/src/services/Redirects.php b/src/services/Redirects.php index 1f1ce91c..c9309ee8 100644 --- a/src/services/Redirects.php +++ b/src/services/Redirects.php @@ -303,9 +303,7 @@ public function findRedirectMatch(string $fullUrl, string $pathOnly, $siteId = n $siteId = $currentSite->id; } else { $primarySite = Craft::$app->getSites()->primarySite; - if ($currentSite) { - $siteId = $primarySite->id; - } + $siteId = $primarySite->id; } } // Try getting the full URL redirect from the cache @@ -567,38 +565,6 @@ public function getAllExactMatchRedirects(int $limit = null, int $siteId = null, return $this->getRedirectsByMatchType($limit, $siteId, 'exactmatch', $enabledOnly); } - /** - * @param int|null $limit - * @param int|null $siteId - * @param string $type - * @return array - */ - protected function getRedirectsByMatchType(int $limit = null, int $siteId = null, string $type, bool $enabledOnly = false): array - { - // Query the db table - $query = (new Query()) - ->from(['{{%retour_static_redirects}}']) - ->orderBy('redirectMatchType ASC, priority ASC'); - - if ($siteId) { - $query - ->where(['siteId' => $siteId]) - ->orWhere(['siteId' => null]); - } - - if ($limit) { - $query->limit($limit); - } - - $query->andWhere(['redirectMatchType' => $type]); - - if ($enabledOnly) { - $query->andWhere(['enabled' => 1]); - } - - return $query->all(); - } - /** * @param int|null $limit * @param int|null $siteId @@ -790,7 +756,7 @@ public function resolveRedirect(string $fullUrl, string $pathOnly, array $redire /** * @param ResolveRedirectEvent $event * @param string|null $url - * @param null $redirect + * @param array $redirect * @return null|array */ public function resolveEventRedirect(ResolveRedirectEvent $event, ?string $url = null, $redirect = null): ?array @@ -979,7 +945,7 @@ public function deleteRedirectById(int $id): int ]); $this->trigger(self::EVENT_BEFORE_DELETE_REDIRECT, $event); if (!$event->isValid) { - return false; + return 0; } // Delete a row from the db table try { @@ -998,34 +964,6 @@ public function deleteRedirectById(int $id): int return $result; } - /** - * Updates an associated element short link value. - * - * @param array $redirectConfig - * @param array $existingData - */ - protected function updateAssociatedElementShortLink(array $redirectConfig, array $existingData): void - { - if (empty($redirectConfig['associatedElementId'])) { - return; - } - // Get the element and set the scenario - $associatedElement = Craft::$app->getElements()->getElementById($redirectConfig['associatedElementId']); - - if (!$associatedElement) { - return; - } - - $fieldUpdated = $this->setShortLinkFieldValue($associatedElement, $existingData['redirectSrcUrl'], $redirectConfig['redirectSrcUrl']); - - if ($fieldUpdated) { - // Prevent element from triggering an infinite loop. - ShortLink::preventShortLinkUpdates(); - Craft::$app->getElements()->saveElement($associatedElement); - ShortLink::allowShortLinkUpdates(); - } - } - /** * Save an element redirect. * @@ -1107,9 +1045,12 @@ public function getRedirectsByElementId(int $elementId, int $siteId = null) * @throws \craft\errors\ElementNotFoundException * @throws \yii\base\Exception */ - public function deleteShortlinkById(int $redirectId): void + public function deleteShortlinkById(int $redirectId): bool { $redirect = $this->getRedirectById($redirectId); + if (!$redirect) { + return false; + } $elementId = $redirect['associatedElementId']; $siteId = $redirect['siteId']; $element = Craft::$app->getElements()->getElementById($elementId, null, $siteId); @@ -1122,6 +1063,8 @@ public function deleteShortlinkById(int $redirectId): void Craft::$app->getElements()->saveElement($element); } } + + return true; } /** @@ -1319,6 +1262,66 @@ public function invalidateCaches(): void ); } + /** + * @param int|null $limit + * @param int|null $siteId + * @param string $type + * @return array + */ + protected function getRedirectsByMatchType(int $limit = null, int $siteId = null, string $type, bool $enabledOnly = false): array + { + // Query the db table + $query = (new Query()) + ->from(['{{%retour_static_redirects}}']) + ->orderBy('redirectMatchType ASC, priority ASC'); + + if ($siteId) { + $query + ->where(['siteId' => $siteId]) + ->orWhere(['siteId' => null]); + } + + if ($limit) { + $query->limit($limit); + } + + $query->andWhere(['redirectMatchType' => $type]); + + if ($enabledOnly) { + $query->andWhere(['enabled' => 1]); + } + + return $query->all(); + } + + /** + * Updates an associated element short link value. + * + * @param array $redirectConfig + * @param array $existingData + */ + protected function updateAssociatedElementShortLink(array $redirectConfig, array $existingData): void + { + if (empty($redirectConfig['associatedElementId'])) { + return; + } + // Get the element and set the scenario + $associatedElement = Craft::$app->getElements()->getElementById($redirectConfig['associatedElementId']); + + if (!$associatedElement) { + return; + } + + $fieldUpdated = $this->setShortLinkFieldValue($associatedElement, $existingData['redirectSrcUrl'], $redirectConfig['redirectSrcUrl']); + + if ($fieldUpdated) { + // Prevent element from triggering an infinite loop. + ShortLink::preventShortLinkUpdates(); + Craft::$app->getElements()->saveElement($associatedElement); + ShortLink::allowShortLinkUpdates(); + } + } + /** * Find all short link fields on an element that have a matching redirect source url and update the value * diff --git a/src/validators/UriValidator.php b/src/validators/UriValidator.php index 441b681a..255f8138 100644 --- a/src/validators/UriValidator.php +++ b/src/validators/UriValidator.php @@ -12,6 +12,7 @@ namespace nystudio107\retour\validators; use craft\helpers\UrlHelper; +use nystudio107\retour\models\StaticRedirects; use yii\validators\Validator; /** @@ -44,6 +45,7 @@ public function init(): void */ public function validateAttribute($model, $attribute): void { + /** @var StaticRedirects $model */ $value = $model->$attribute; $redirectMatchType = 'redirectMatchType'; // Always remove whitespace