Skip to content

Commit

Permalink
fix: revision regenerate of shortlink
Browse files Browse the repository at this point in the history
  • Loading branch information
michtio committed Sep 6, 2022
1 parent 86bc055 commit dd79cce
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 30 deletions.
33 changes: 21 additions & 12 deletions src/Shortlink.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use craft\events\RegisterComponentTypesEvent;
use craft\events\RegisterUrlRulesEvent;
use craft\events\RegisterUserPermissionsEvent;
use craft\helpers\ElementHelper;
use craft\helpers\UrlHelper;
use craft\services\Elements;
use craft\services\Plugins;
Expand Down Expand Up @@ -373,21 +374,22 @@ protected function customAdminCpPermissions(): array
protected function renderSidebar(Entry $entry): string
{
$user = Craft::$app->getUser();
$shortlink = self::getInstance()->shortlinks->getShortlink($entry);
// if element is a duplicate regenerate, fire before save?
if($entry->duplicateOf) {
/*if($entry->duplicateOf) {
$shortlink = self::getInstance()->shortlinks->getShortlink($entry);
}
}*/
$shortlink = $this->_getShortlinkFromContext($entry);
$shortlinkVars = [
'allowCustom' => self::$settings->allowCustom,
'currentSiteId' => $element->siteId ?? 0,
'redirectType' => self::$settings->redirectType,
'shortlink' => $shortlink->shortlinkUri ?? self::getInstance()->shortlinks->generateShortlink(),
'shortlinkId' => $shortlink->id ?? null,
'showRedirectOption' => $user->checkPermission('shortlink:entry-redirect'),
];
return PluginTemplate::renderPluginTemplate(
'_sidebars/entry-shortlink.twig',
[
'allowCustom' => self::$settings->allowCustom,
'currentSiteId' => $element->siteId ?? 0,
'redirectType' => self::$settings->redirectType,
'shortlink' => $shortlink,
'shortlinkId' => $this->_getShortlinkFromContext($entry)->id,
'showRedirectOption' => $user->checkPermission('shortlink:entry-redirect'),
]
$shortlinkVars
);
}

Expand All @@ -396,9 +398,16 @@ private function _getShortlinkFromContext($entry): ?ShortlinkElement
// Get existing shortlink
$ownerId = $entry->id ?? ':empty:';

if(!ElementHelper::isDraftOrRevision($entry)) {
return ShortlinkElement::find()
->ownerId($ownerId)
->one();
}

return ShortlinkElement::find()
->ownerId($ownerId)
->ownerRevisionId($ownerId)
->one();

}

}
30 changes: 13 additions & 17 deletions src/services/ShortlinkService.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,6 @@ public function getShortlinkById(int $id): ?ShortlinkElement
return Craft::$app->getElements()->getElementById($id, ShortlinkElement::class);
}

/**
* @throws ExitException
*/
public function getShortLink(Entry $element): array|string|null
{
$shortlink = ShortlinkElement::findOne(['ownerId' => $element->id]);

if ($shortlink) {
return $shortlink->shortlinkUri;
}

return $this->generateShortlink();
}

/**
* @throws ExitException
* @throws Exception
Expand Down Expand Up @@ -248,13 +234,21 @@ public function saveShortlink($entry = null, array $post): bool
{
$session = Craft::$app->getSession();

$shortlink = $this->_setShortlinkFromPost();
if(!ElementHelper::isDraftOrRevision($entry)) {
$shortlink = ShortlinkElement::findOne(['ownerId' => $entry->id]);
} else {
$shortlink = ShortlinkElement::findOne(['ownerRevisionId' => $entry->id]);
}

if(is_null($shortlink)) {
$shortlink = new ShortlinkElement();
}

//$shortlink = $this->_setShortlinkFromPost();
$shortlink->shortlinkUri = $post['shortlinkUri'] ?? null;
$shortlink->httpCode = $post['redirectType'] ?? null;
$shortlink->shortlinkStatus = ShortlinkElement::STATUS_ACTIVE;



if(!ElementHelper::isDraftOrRevision($entry)) {
$shortlink->ownerId = $entry->id;
$shortlink->ownerRevisionId = null;
Expand All @@ -263,6 +257,8 @@ public function saveShortlink($entry = null, array $post): bool
$shortlink->ownerRevisionId = $entry->id;
}

Craft::warning('Shortlink ID: ' . $shortlink->id . ' / Owner ID: ' . $shortlink->ownerId . ' / Owner Revision ID: ' . $shortlink->ownerRevisionId, __METHOD__);

if (!Craft::$app->getElements()->saveElement($shortlink)) {
$session->setError(Craft::t('shortlink', 'Could not save the shortlink.'));
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/templates/_sidebars/entry-shortlink.twig
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
redirectLabel="{{ 'Redirect Type'|t('shortlink') }}"
redirect-type="{{ redirectType }}"
shortlink="{{ shortlink }}"
{% if shortlinkId %}:shortlink-id="{{ shortlinkId }}"{% endif %}
:shortlink-id="{{ shortlinkId }}"
:show-redirect-option="{{ showRedirectOption ? 'true' : 'false' }}"
/>
</div>

0 comments on commit dd79cce

Please sign in to comment.