* @copyright Studio Espresso * @license MIT */ class StandardSite extends Plugin { public string $schemaVersion = '1.4.0'; public bool $hasCpSettings = true; public bool $hasCpSection = true; public static function config(): array { return [ 'components' => [ 'encryption' => EncryptionService::class, 'resolver' => ResolverService::class, 'dpop' => DPopService::class, 'oauth' => OAuthService::class, 'api' => ApiService::class, 'publisher' => PublisherService::class, 'connection' => ConnectionService::class, ], ]; } public function init(): void { parent::init(); $this->registerUrlRules(); $this->registerEventHandlers(); $this->registerTwigVariable(); $this->registerDocumentDiscoveryTag(); } public function getCpNavItem(): ?array { $item = parent::getCpNavItem(); $item['label'] = 'Standard.site'; $item['icon'] = '@studioespresso/standardsite/icon-mask.svg'; return $item; } protected function createSettingsModel(): ?Model { return Craft::createObject(Settings::class); } protected function settingsHtml(): ?string { return Craft::$app->view->renderTemplate('standard-site/_settings.twig', [ 'plugin' => $this, 'settings' => $this->getSettings(), ]); } private function registerEventHandlers(): void { // Publish entry on save Event::on( Entry::class, Entry::EVENT_AFTER_SAVE, function(ModelEvent $event) { /** @var Entry $entry */ $entry = $event->sender; // Skip propagating saves, resaving, drafts, and revisions if ($entry->propagating || $entry->resaving || $entry->getIsDraft() || $entry->getIsRevision()) { return; } $settings = $this->getSettings(); $site = Craft::$app->getSites()->getSiteById($entry->siteId); $siteSettings = $settings->getSiteSettings($site->uid); // Must be connected and have publish-on-save enabled for this site if (!$this->connection->isConnected($site->uid) || !$siteSettings->publishOnSave) { return; } // Must have a publication record for this site if (!PublicationRecord::getAtUri($site->uid)) { return; } // Check if this section is enabled for this site $section = $entry->getSection(); if (!$section || !in_array($section->uid, $siteSettings->enabledSections, true)) { return; } if ($entry->enabled) { // Push publish job to queue Craft::$app->getQueue()->push(new PublishEntryJob([ 'entryId' => $entry->id, 'siteId' => $entry->siteId, ])); } else { // Entry disabled — unpublish if it was published if ($this->publisher->isPublished($entry->id, $entry->siteId)) { $this->publisher->unpublishEntry($entry); } } } ); // Delete records when entry is deleted Event::on( Entry::class, Entry::EVENT_AFTER_DELETE, function(Event $event) { /** @var Entry $entry */ $entry = $event->sender; $site = Craft::$app->getSites()->getSiteById($entry->siteId); if ($site && $this->connection->isConnected($site->uid) && $this->publisher->isPublished($entry->id, $entry->siteId)) { $this->publisher->unpublishEntry($entry); } } ); // Entry sidebar widget Event::on( Entry::class, Entry::EVENT_DEFINE_SIDEBAR_HTML, function(DefineHtmlEvent $event) { /** @var Entry $entry */ $entry = $event->sender; // Only show for saved entries (not new ones) if (!$entry->id) { return; } $settings = $this->getSettings(); $site = Craft::$app->getSites()->getSiteById($entry->siteId); $siteSettings = $settings->getSiteSettings($site->uid); $section = $entry->getSection(); // Only show for enabled sections on this site if (!$section || !in_array($section->uid, $siteSettings->enabledSections, true)) { return; } try { $isConnected = $this->connection->isConnected($site->uid); $isPublished = $isConnected && $this->publisher->isPublished($entry->id, $entry->siteId); $atUri = $isPublished ? $this->publisher->getDocumentUri($entry->id, $entry->siteId) : null; $event->html .= Craft::$app->getView()->renderTemplate('standard-site/_entry-sidebar', [ 'entry' => $entry, 'isConnected' => $isConnected, 'isPublished' => $isPublished, 'atUri' => $atUri, ]); } catch (\Throwable $e) { Craft::error("[standard-site] Failed to render entry sidebar: {$e->getMessage()}", __METHOD__); } } ); } /** * Inject a tag into the * of published entry pages. This is how AT Protocol readers (e.g. * Bluesky's card service) discover the document record for a URL and render * a rich link card — the .well-known endpoint alone isn't enough. */ private function registerDocumentDiscoveryTag(): void { $request = Craft::$app->getRequest(); if ($request->getIsConsoleRequest() || !$request->getIsSiteRequest()) { return; } Event::on( View::class, View::EVENT_BEFORE_RENDER_PAGE_TEMPLATE, function() { $element = Craft::$app->getUrlManager()->getMatchedElement(); if (!$element instanceof Entry) { return; } $uri = $this->publisher->getDocumentUri($element->id, $element->siteId); if (!$uri) { return; } Craft::$app->getView()->registerHtml( Html::tag('link', '', ['rel' => 'site.standard.document', 'href' => $uri]), View::POS_HEAD, ); } ); } private function registerTwigVariable(): void { Event::on( CraftVariable::class, CraftVariable::EVENT_INIT, function(Event $event) { $event->sender->set('standardSite', StandardSiteVariable::class); } ); } private function registerUrlRules(): void { // CP routes Event::on( UrlManager::class, UrlManager::EVENT_REGISTER_CP_URL_RULES, function(RegisterUrlRulesEvent $event) { $event->rules['standard-site'] = 'standard-site/cp/index'; } ); // Site routes (publicly accessible — needed for PDS to fetch metadata and redirect back) Event::on( UrlManager::class, UrlManager::EVENT_REGISTER_SITE_URL_RULES, function(RegisterUrlRulesEvent $event) { $event->rules['.well-known/site.standard.publication'] = 'standard-site/well-known/publication'; $event->rules['standard-site/oauth/client-metadata'] = 'standard-site/oauth/client-metadata'; $event->rules['standard-site/oauth/callback'] = 'standard-site/oauth/callback'; } ); } }