Skip to content

Commit

Permalink
move to custom API route for better compatiblity
Browse files Browse the repository at this point in the history
  • Loading branch information
tobimori committed May 10, 2023
1 parent d169ce3 commit 1cb36a4
Show file tree
Hide file tree
Showing 11 changed files with 141 additions and 128 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "tobimori/kirby-seo",
"description": "The ultimate Kirby SEO toolkit",
"type": "kirby-plugin",
"version": "0.1.2",
"version": "0.1.3",
"license": "MIT",
"homepage": "https://github.com/tobimori/kirby-seo#readme",
"authors": [
Expand Down
91 changes: 91 additions & 0 deletions config/api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

use Kirby\Cms\App;
use Kirby\Cms\Page;
use Kirby\Cms\Site;
use Kirby\Form\Form;
use Kirby\Toolkit\Str;

return [
'data' => [
'dirtyPageOrSite' => function (string $slug) {
$kirby = kirby();
$page = $slug == 'site' ? $kirby->site() : $kirby->page(Str::replace($slug, '+', '/'));

if ($this->requestBody()) {
$form = Form::for($page, [ // Form class handles transformation of changed items
'ignoreDisabled' => true,
'input' => array_merge(['title' => $page->title()], $page->content()->data(), $this->requestBody()),
'language' => $kirby->language()->code()
]);

$page = $page->clone(['content' => $form->data()]);
}

return $page;
}
],
'routes' => [
[
'pattern' => '/k-seo/(:any)/heading-structure',
'method' => 'POST',
'action' => function (string $slug) {
$model = $this->dirtyPageOrSite($slug);

if ($model instanceof Page) {
$page = $model->render();
$dom = new DOMDocument();
$dom->loadHTML(htmlspecialchars_decode(iconv('UTF-8', 'ISO-8859-1', htmlentities($page, ENT_COMPAT, 'UTF-8')), ENT_QUOTES), libxml_use_internal_errors(true));

$xpath = new DOMXPath($dom);
$headings = $xpath->query('//h1|//h2|//h3|//h4|//h5|//h6');
$data = [];

foreach ($headings as $heading) {
$data[] = [
'level' => (int) str_replace('h', '', $heading->nodeName),
'text' => $heading->textContent,
];
}

return $data;
}

return null;
}
],
[
'pattern' => '/k-seo/(:any)/seo-preview',
'method' => 'POST',
'action' => function (string $slug) {
$model = $this->dirtyPageOrSite($slug);

if ($model instanceof Site) {
$model = $model->homePage();
}

if ($model instanceof Page) {
$meta = $model->metadata();

$ogImage = $meta->ogImage()->toFile()?->thumb([
'width' => 1200,
'height' => 630,
'crop' => true,
]);

return [
'page' => $model->slug(),
'url' => $model->url(),
'title' => $meta->title()->value(),
'description' => $meta->metaDescription()->value(),
'ogTitle' => $meta->ogTitle()->value(),
'ogDescription' => $meta->ogDescription()->value(),
'ogImage' => $ogImage?->url(),
];
}

return null;
}
]
]
];
10 changes: 10 additions & 0 deletions config/sections.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

return [
'seo-preview' => [
'mixins' => ['headline'],
],
'heading-structure' => [
'mixins' => ['headline']
]
];
45 changes: 0 additions & 45 deletions config/sections/heading-structure.php

This file was deleted.

52 changes: 0 additions & 52 deletions config/sections/seo-preview.php

This file was deleted.

Loading

0 comments on commit 1cb36a4

Please sign in to comment.