Skip to content

Commit 1cb36a4

Browse files
committed
move to custom API route for better compatiblity
1 parent d169ce3 commit 1cb36a4

File tree

11 files changed

+141
-128
lines changed

11 files changed

+141
-128
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "tobimori/kirby-seo",
33
"description": "The ultimate Kirby SEO toolkit",
44
"type": "kirby-plugin",
5-
"version": "0.1.2",
5+
"version": "0.1.3",
66
"license": "MIT",
77
"homepage": "https://github.com/tobimori/kirby-seo#readme",
88
"authors": [

config/api.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
];

config/sections.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
return [
4+
'seo-preview' => [
5+
'mixins' => ['headline'],
6+
],
7+
'heading-structure' => [
8+
'mixins' => ['headline']
9+
]
10+
];

config/sections/heading-structure.php

Lines changed: 0 additions & 45 deletions
This file was deleted.

config/sections/seo-preview.php

Lines changed: 0 additions & 52 deletions
This file was deleted.

0 commit comments

Comments
 (0)