Skip to content

Commit

Permalink
feat: add preventPublish() method
Browse files Browse the repository at this point in the history
  • Loading branch information
BernhardBaumrock committed Jan 18, 2024
1 parent 17b1404 commit de7a39b
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions RockMigrations.module.php
Expand Up @@ -4870,6 +4870,72 @@ public function path(string $path, $slash = null): string
return $path;
}

/**
* Manually prevent publish of a page for non-superusers
* @return void
*/
public function preventPublish(
Page|string|int $selector,
$allowForSuperusers = false,
): void {
$sudo = $this->wire->user->isSuperuser();
$page = $this->wire->page;
if (!$page) {
if ($sudo) throw new WireException("Call preventPublish() on ready() so that \$page is available");
else return; // silent exit
}

// only on admin pages
if ($page->template != 'admin') return;
$preventPage = $this->wire->pages->get($selector);
if (!$preventPage->id) {
if ($sudo) throw new WireException("Page $selector not found");
else return; // silent exit
}

// allow publish for superusers
if ($sudo and $allowForSuperusers) return;

// Remove publish button and unpublished checkbox in Page Edit
$this->wire->addHookAfter(
'ProcessPageEdit::buildForm',
function (HookEvent $event) use ($selector) {
$form = $event->return;
$page = $event->object->getPage();
if (!$page->isUnpublished()) return;
if (!$page->matches($selector)) return;
$form->remove("submit_publish");
}
);

// Remove publish button from Page List
$this->wire->addHookAfter(
'ProcessPageListActions::getExtraActions',
function (HookEvent $event) use ($selector) {
$page = $event->arguments(0);
$extras = $event->return;
if (!$page->isUnpublished()) return;
if (!$page->matches($selector)) return;
unset($extras['pub']);
$event->return = $extras;
}
);

// There might still be the checkbox in the settings tab
// We can't remove it because this will always publish the page.
$trace = Debug::backtrace()[0]['file'];
$this->wire->addHookAfter(
"Pages::published",
function (HookEvent $event) use ($selector, $trace) {
$page = $event->arguments('page');
if (!$page->matches($selector)) return;
$page->addStatus(Page::statusUnpublished);
$page->save();
$this->error("Publishing page $selector not allowed by $trace");
}
);
}

/**
* Trigger migrate() method if it exists
*/
Expand Down

0 comments on commit de7a39b

Please sign in to comment.