Skip to content

Commit a8e0665

Browse files
committed
Upgrade to Kirby 3
1 parent 3606964 commit a8e0665

28 files changed

+131
-2929
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.DS_Store
2-
*.cache
2+
*.cache
3+
vendor

composer.json

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
11
{
2+
"name": "pedroborges/kirby-meta-tags",
3+
"description": "HTML meta tags generator for Kirby.",
4+
"version": "2.0.0-alpha-1",
5+
"keywords": ["kirby-plugin"],
6+
"authors": [
7+
{
8+
"name": "Pedro Borges",
9+
"email": "[email protected]"
10+
}
11+
],
212
"autoload": {
3-
"classmap": [
4-
"src"
5-
]
13+
"files": [
14+
"config.php"
15+
],
16+
"psr-4": {
17+
"PedroBorges\\KirbyMetaTags\\": "src/"
18+
}
619
},
720
"require": {
21+
"php": ">=7.1.0",
822
"pedroborges/meta-tags": "^0.0.2"
923
}
1024
}

composer.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
use Kirby\Cms\Page;
4+
use PedroBorges\KirbyMetaTags\MetaTags;
5+
6+
Kirby::plugin('pedroborges/metatags', [
7+
'pageMethods' => [
8+
'metaTags' => function ($groups = null) {
9+
return metaTags($this)->render($groups);
10+
}
11+
]
12+
]);
13+
14+
if (! function_exists('metaTags')) {
15+
/**
16+
* Generate meta tags for a given page.
17+
*
18+
* @param Page $page
19+
* @return MetaTags
20+
*/
21+
function metaTags(Page $page) : MetaTags
22+
{
23+
return MetaTags::instance($page);
24+
}
25+
}

index.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
require_once __DIR__ . '/vendor/autoload.php';

meta-tags.php

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

package.json

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

readme.md

Lines changed: 67 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,8 @@
33
HTML meta tags generator for Kirby. Supports [Open Graph](http://ogp.me), [Twitter Cards](https://dev.twitter.com/cards/overview), and [JSON Linked Data](https://json-ld.org) out of the box.
44

55
## Requirements
6-
- Kirby 2.3.2+
7-
- PHP 5.4+
8-
9-
## Installation
10-
11-
### Download
12-
[Download the files](https://github.com/pedroborges/kirby-meta-tags/archive/master.zip) and place them inside `site/plugins/meta-tags`.
13-
14-
### Kirby CLI
15-
Kirby's [command line interface](https://github.com/getkirby/cli) is the easiest way to install the Meta Tags plugin:
16-
17-
$ kirby plugin:install pedroborges/kirby-meta-tags
18-
19-
To update it simply run:
20-
21-
$ kirby plugin:update pedroborges/kirby-meta-tags
22-
23-
### Git Submodule
24-
You can add the Meta Tags as a Git submodule.
25-
26-
<details>
27-
<summary><strong>Show Git Submodule instructions</strong> 👁</summary><p>
28-
29-
$ cd your/project/root
30-
$ git submodule add https://github.com/pedroborges/kirby-meta-tags.git site/plugins/meta-tags
31-
$ git submodule update --init --recursive
32-
$ git commit -am "Add plugin Meta Tags"
33-
34-
Updating is as easy as running a few commands.
35-
36-
$ cd your/project/root
37-
$ git submodule foreach git checkout master
38-
$ git submodule foreach git pull
39-
$ git commit -am "Update submodules"
40-
$ git submodule update --init --recursive
41-
42-
</p></details>
6+
- Kirby 3
7+
- PHP 7.1+
438

449
## Basic Usage
4510
After installing the Meta Tags plugin, you need to add one line to the `head` element on your template, or `header.php` snippet:
@@ -65,53 +30,56 @@ Or specify which tags to render:
6530
```
6631

6732
### Default
68-
6933
The plugin ships with some default meta tags enabled for your convenience:
7034

7135
```php
72-
c::set('meta-tags.default', function(Page $page, Site $site) {
73-
return [
74-
'title' => $site->title(),
75-
'meta' => [
76-
'description' => $site->description()
77-
],
78-
'link' => [
79-
'canonical' => $page->url()
80-
],
81-
'og' => [
82-
'title' => $page->isHomePage()
83-
? $site->title()
84-
: $page->title(),
85-
'type' => 'website',
86-
'site_name' => $site->title(),
87-
'url' => $page->url()
88-
]
89-
];
90-
});
36+
return [
37+
// other options...
38+
'pedroborges.metatags.default' => function ($page, $site) {
39+
return [
40+
'title' => $site->title(),
41+
'meta' => [
42+
'description' => $site->description()
43+
],
44+
'link' => [
45+
'canonical' => $page->url()
46+
],
47+
'og' => [
48+
'title' => $page->isHomePage()
49+
? $site->title()
50+
: $page->title(),
51+
'type' => 'website',
52+
'site_name' => $site->title(),
53+
'url' => $page->url()
54+
]
55+
];
56+
}
57+
]
9158
```
9259

93-
**The `meta-tags.default` option is applied to all pages on your Kirby site.** Of course you can change the defaults. In order to do that, just copy this example to your `site/config/config.php` file and tweak it to fit your website needs.
94-
95-
> If your configuration file grows too much, you can extract it to a `site/config/meta-tags.php` file, for example, and require it from `site/config/config.php`.
60+
**The `pedroborges.metatags.default` option is applied to all pages on your Kirby site.** Of course you can change the defaults. In order to do that, just copy this example to your `site/config/config.php` file and tweak it to fit your website needs.
9661

9762
### Templates
9863
Following the flexible spirit of Kirby, you also have the option to add template specific meta tags:
9964

10065
```php
101-
c::set('meta-tags.templates', function(Page $page, Site $site) {
102-
return [
103-
'song' => [
104-
'og' => [
105-
'type' => 'music.song',
106-
'namespace:music' => [
107-
'duration' => $page->duration(),
108-
'album' => $page->parent()->url(),
109-
'musician' => $page->singer()->html()
66+
return [
67+
// other options...
68+
'pedroborges.metatags.templates' => function ($page, $site) {
69+
return [
70+
'song' => [
71+
'og' => [
72+
'type' => 'music.song',
73+
'namespace:music' => [
74+
'duration' => $page->duration(),
75+
'album' => $page->parent()->url(),
76+
'musician' => $page->singer()->html()
77+
]
11078
]
11179
]
112-
]
113-
];
114-
});
80+
];
81+
}
82+
]
11583
```
11684

11785
In the example above, those settings will only be applied to pages which template is `song`.
@@ -123,34 +91,38 @@ For more information on all the `meta`, `link`, Open Graph and Twitter Card tags
12391
- [Twitter Cards](https://dev.twitter.com/cards/overview)
12492

12593
## Options
126-
Both the `meta-tags.default` and `meta-tags.templates` accept similar values:
94+
Both the `pedroborges.metatags.default` and `pedroborges.metatags.templates` accept similar values:
12795

128-
### `meta-tags.default`
96+
### `pedroborges.metatags.default`
12997
It accepts an array containing any or all of the following keys: `title`, `meta`, `link`, `og`, and `twitter`. With the exception of `title`, all other groups must return an array of key-value pairs. Check out the [tag groups](#tag-groups) section to learn which value types are accepted by each key.
13098

13199
```php
132-
c::set('meta-tags.default', [
133-
'title' => 'Site Name',
134-
'meta' => [ /* meta tags */ ],
135-
'link' => [ /* link tags */ ],
136-
'og' => [ /* Open Graph tags */ ],
137-
'twitter' => [ /* Twitter Card tags */ ],
138-
'json-ld' => [ /* JSON-LD schema */ ],
139-
]);
100+
'pedroborges.metatags.default' => function ($page, $site) {
101+
return [
102+
'title' => 'Site Name',
103+
'meta' => [ /* meta tags */ ],
104+
'link' => [ /* link tags */ ],
105+
'og' => [ /* Open Graph tags */ ],
106+
'twitter' => [ /* Twitter Card tags */ ],
107+
'json-ld' => [ /* JSON-LD schema */ ],
108+
];
109+
}
140110
```
141111

142-
### `meta-tags.templates`
112+
### `pedroborges.metatags.templates`
143113
This option allows you to define a template specific set of meta tags. It must return an array where each key corresponds to the template name you are targeting.
144114

145115
```php
146-
c::set('meta-tags.templates', [
147-
'article' => [ /* tags groups */ ],
148-
'about' => [ /* tags groups */ ],
149-
'products' => [ /* tags groups */ ],
150-
]);
116+
'pedroborges.metatags.default' => function ($page, $site) {
117+
return [
118+
'article' => [ /* tags groups */ ],
119+
'about' => [ /* tags groups */ ],
120+
'products' => [ /* tags groups */ ],
121+
];
122+
}
151123
```
152124

153-
When a key matches the current page template name, it is merged and overrides any repeating properties defined on the `meta-tags.default` option so you don't have to repeat yourself.
125+
When a key matches the current page template name, it is merged and overrides any repeating properties defined on the `pedroborges.metatags.default` option so you don't have to repeat yourself.
154126

155127
## Tag Groups
156128
These groups accept string, closure, or array as their values. Being so flexible, the sky is the limit to what you can do with Meta Tags!
@@ -197,7 +169,7 @@ This tag group is used to render HTML `<link>` elements. It takes an `array` of
197169
['href' => url('assets/images/icons/favicon-192.png'), 'sizes' => '192x192', 'type' =>'image/png']
198170
],
199171
'canonical' => $page->url(),
200-
'alternate' => function(Page $page, Site $site) {
172+
'alternate' => function () {
201173
$locales = [];
202174

203175
foreach ($site->languages() as $language) {
@@ -255,15 +227,15 @@ Where you can define [Open Graph](http://ogp.me) `<meta>` elements.
255227
Of course you can use Open Graph [structured objects](http://ogp.me/#structured). Let's see a blog post example:
256228

257229
```php
258-
c::set('meta-tags.templates', function(Page $page, Site $site) {
230+
'pedroborges.metatags.templates' => function ($page, $site) {
259231
return [
260232
'article' => [ // template name
261233
'og' => [ // tags group name
262234
'type' => 'article', // overrides the default
263235
'namespace:article' => [
264236
'author' => $page->author(),
265-
'published_time' => $page->date('%F'),
266-
'modified_time' => $page->modified('%F'),
237+
'published_time' => $page->date('Y-m-d'),
238+
'modified_time' => $page->modified('Y-m-d'),
267239
'tag' => ['tech', 'web']
268240
],
269241
'namespace:image' => function(Page $page) {
@@ -279,7 +251,7 @@ c::set('meta-tags.templates', function(Page $page, Site $site) {
279251
]
280252
]
281253
];
282-
});
254+
}
283255
```
284256

285257
<details>
@@ -323,7 +295,7 @@ This tag group works just like the previous one, but it generates `<meta>` tags
323295
'card' => 'summary',
324296
'site' => $site->twitter(),
325297
'title' => $page->title(),
326-
'namespace:image' => function(Page $page) {
298+
'namespace:image' => function ($page) {
327299
$image = $page->cover()->toFile();
328300

329301
return [

src/MetaTags.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
<?php
22

3-
use PedroBorges\MetaTags\MetaTags as Head;
3+
namespace PedroBorges\KirbyMetaTags;
4+
5+
use Exception;
6+
use Kirby\Toolkit\A;
7+
use Kirby\Cms\Field;
8+
use Kirby\Cms\Page;
9+
use PedroBorges\MetaTags\MetaTags as Tags;
410

511
class MetaTags
612
{
@@ -14,12 +20,12 @@ class MetaTags
1420

1521
public function __construct(Page $page)
1622
{
17-
$this->indentation = c::get('meta-tags.indentation', null);
18-
$this->order = c::get('meta-tags.order', null);
19-
$this->tags = new Head($this->indentation, $this->order);
23+
$this->indentation = option('pedroborges.metatags.indentation', null);
24+
$this->order = option('pedroborges.metatags.order', null);
25+
$this->tags = new Tags($this->indentation, $this->order);
2026

21-
$templates = c::get('meta-tags.templates', []);
22-
$default = c::get('meta-tags.default', [
27+
$templates = option('pedroborges.metatags.templates', []);
28+
$default = option('pedroborges.metatags.default', [
2329
'title' => $page->isHomePage() ? site()->title() : $page->title(),
2430
'meta' => [
2531
'description' => site()->description()
@@ -40,15 +46,15 @@ public function __construct(Page $page)
4046
$templates = is_callable($templates) ? $templates($page, site()) : $templates;
4147

4248
if (! is_array($this->data)) {
43-
throw new Exception('Option "meta-tags.default" must return an array');
49+
throw new Exception('Option "pedroborges.metatags.default" must return an array');
4450
}
4551

4652
if (! is_array($templates)) {
47-
throw new Exception('Option "meta-tags.templates" must return an array');
53+
throw new Exception('Option "pedroborges.metatags.templates" must return an array');
4854
}
4955

50-
if (isset($templates[$page->intendedTemplate()])) {
51-
$this->data = a::merge($this->data, $templates[$page->intendedTemplate()]);
56+
if (isset($templates[$page->template()])) {
57+
$this->data = A::update($this->data, $templates[$page->template()]);
5258
}
5359

5460
static::$instance = $this;

0 commit comments

Comments
 (0)