Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restructure main seo function into a class #33

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions core/seo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

class Seo {
public $type;
public $content;

public function __construct($type) {
$this->type = $type;

$controller = SeoCore::panel(page());
$this->content = $controller[$type]['full-replaced'];
}

public function __toString() {
$html = '';

if ($this->type == 'title') {
$html = '<title>' . $this->content . '</title>' . PHP_EOL;
} elseif (($this->type == 'description') && (!empty($this->content))) {
$html = '<meta name="description" content="' . $this->content . '">' . PHP_EOL;
}

return (string)$html;
}

public function value() {
return (string)$this->content;
}
}
File renamed without changes.
16 changes: 7 additions & 9 deletions docs/FRONTEND.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ You need to call a function in your template / snippet / pattern to make it work

**In my `header.php` I do like this:**

When using this example it will output the HTML as well.
The function `seo()` will return the values with added HTML tags:

```html
<?php seo('title'); ?>
<?php seo('description'); ?>
<?php echo seo('title'); ?>
<?php echo seo('description'); ?>
```

Output:
Expand All @@ -18,17 +18,15 @@ Output:
<meta name="description" content="Some description">
```

When using this example it will return the value.

Because you return it and therefor want to control it, it does not wrap it in HTML. If you want HTML, warp it yourself or create a snippet / pattern for it.
It's also possible to just return the values, without the HTML tags:

```html
<?php echo seo('title', array(), true); ?>
<?php echo seo('description', array(), true); ?>
<?php echo seo('title')->value(); ?>
<?php echo seo('description')->value(); ?>
```

Output:

```html
Some title Some description
```
```
18 changes: 4 additions & 14 deletions seo.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
<?php
require_once __DIR__ . DS . 'core' . DS . 'core.php';
require_once __DIR__ . DS . 'core' . DS . 'seocore.php';
require_once __DIR__ . DS . 'core' . DS . 'seo.php';

$kirby->set('field', 'seo', __DIR__ . DS . 'fields' . DS . 'seo');
$kirby->set('field', 'seoarchive', __DIR__ . DS . 'fields' . DS . 'seoarchive');

// Main seo function
function seo($type, $data = array(), $return = false) {
$page = page();
$controller = SeoCore::panel( $page );
$content = $controller[$type]['full-replaced'];

if( $return !== false ) {
echo $content;
} else {
$html = array();
$html['title'] = '<title>' . $content . '</title>' . "\n";
$html['description'] = ( ! empty( $content ) ) ? '<meta name="description" content="' . $content . '">' . "\n" : '';
return $html[$type];
}
function seo($type) {
return new Seo($type);
}