|
| 1 | +<?php |
| 2 | + |
| 3 | +use Kirby\Cms\App; |
| 4 | +use Kirby\Cms\Page; |
| 5 | +use Kirby\Cms\Site; |
| 6 | +use Kirby\Form\Form; |
| 7 | +use Kirby\Toolkit\Str; |
| 8 | + |
| 9 | +return [ |
| 10 | + 'data' => [ |
| 11 | + 'dirtyPageOrSite' => function (string $slug) { |
| 12 | + $kirby = kirby(); |
| 13 | + $page = $slug == 'site' ? $kirby->site() : $kirby->page(Str::replace($slug, '+', '/')); |
| 14 | + |
| 15 | + if ($this->requestBody()) { |
| 16 | + $form = Form::for($page, [ // Form class handles transformation of changed items |
| 17 | + 'ignoreDisabled' => true, |
| 18 | + 'input' => array_merge(['title' => $page->title()], $page->content()->data(), $this->requestBody()), |
| 19 | + 'language' => $kirby->language()->code() |
| 20 | + ]); |
| 21 | + |
| 22 | + $page = $page->clone(['content' => $form->data()]); |
| 23 | + } |
| 24 | + |
| 25 | + return $page; |
| 26 | + } |
| 27 | + ], |
| 28 | + 'routes' => [ |
| 29 | + [ |
| 30 | + 'pattern' => '/k-seo/(:any)/heading-structure', |
| 31 | + 'method' => 'POST', |
| 32 | + 'action' => function (string $slug) { |
| 33 | + $model = $this->dirtyPageOrSite($slug); |
| 34 | + |
| 35 | + if ($model instanceof Page) { |
| 36 | + $page = $model->render(); |
| 37 | + $dom = new DOMDocument(); |
| 38 | + $dom->loadHTML(htmlspecialchars_decode(iconv('UTF-8', 'ISO-8859-1', htmlentities($page, ENT_COMPAT, 'UTF-8')), ENT_QUOTES), libxml_use_internal_errors(true)); |
| 39 | + |
| 40 | + $xpath = new DOMXPath($dom); |
| 41 | + $headings = $xpath->query('//h1|//h2|//h3|//h4|//h5|//h6'); |
| 42 | + $data = []; |
| 43 | + |
| 44 | + foreach ($headings as $heading) { |
| 45 | + $data[] = [ |
| 46 | + 'level' => (int) str_replace('h', '', $heading->nodeName), |
| 47 | + 'text' => $heading->textContent, |
| 48 | + ]; |
| 49 | + } |
| 50 | + |
| 51 | + return $data; |
| 52 | + } |
| 53 | + |
| 54 | + return null; |
| 55 | + } |
| 56 | + ], |
| 57 | + [ |
| 58 | + 'pattern' => '/k-seo/(:any)/seo-preview', |
| 59 | + 'method' => 'POST', |
| 60 | + 'action' => function (string $slug) { |
| 61 | + $model = $this->dirtyPageOrSite($slug); |
| 62 | + |
| 63 | + if ($model instanceof Site) { |
| 64 | + $model = $model->homePage(); |
| 65 | + } |
| 66 | + |
| 67 | + if ($model instanceof Page) { |
| 68 | + $meta = $model->metadata(); |
| 69 | + |
| 70 | + $ogImage = $meta->ogImage()->toFile()?->thumb([ |
| 71 | + 'width' => 1200, |
| 72 | + 'height' => 630, |
| 73 | + 'crop' => true, |
| 74 | + ]); |
| 75 | + |
| 76 | + return [ |
| 77 | + 'page' => $model->slug(), |
| 78 | + 'url' => $model->url(), |
| 79 | + 'title' => $meta->title()->value(), |
| 80 | + 'description' => $meta->metaDescription()->value(), |
| 81 | + 'ogTitle' => $meta->ogTitle()->value(), |
| 82 | + 'ogDescription' => $meta->ogDescription()->value(), |
| 83 | + 'ogImage' => $ogImage?->url(), |
| 84 | + ]; |
| 85 | + } |
| 86 | + |
| 87 | + return null; |
| 88 | + } |
| 89 | + ] |
| 90 | + ] |
| 91 | +]; |
0 commit comments