Skip to content

Commit 99c51ce

Browse files
committed
and psr-2
1 parent 83e2652 commit 99c51ce

File tree

6 files changed

+24
-23
lines changed

6 files changed

+24
-23
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ return [
5757

5858
## Settings
5959

60+
**mime**
61+
- default: `null` to autodetect json or rss-xml otherwise enforce output with a certain [mime type](https://github.com/k-next/kirby/blob/master/src/Toolkit/Mime.php)
62+
6063
**expires**
6164
- default: `60*24*7` in minutes
6265

classes/feed.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ private static function cache(): \Kirby\Cache\Cache
1818
return static::$cache;
1919
}
2020

21-
public static function flush() {
21+
public static function flush()
22+
{
2223
return static::cache()->flush();
2324
}
2425

25-
public static function isJson($string) {
26+
public static function isJson($string)
27+
{
2628
json_decode($string);
2729
return (json_last_error() == JSON_ERROR_NONE);
2830
}
@@ -43,17 +45,18 @@ public static function isXml($content)
4345
return empty($errors);
4446
}
4547

46-
public static function feed($pages, $options = [], $force = null) {
47-
if($force == null && option('debug') && option('bnomei.feed.debugforce')) {
48+
public static function feed($pages, $options = [], $force = null)
49+
{
50+
if ($force == null && option('debug') && option('bnomei.feed.debugforce')) {
4851
$force = true;
4952
}
5053
$key = [];
51-
foreach($pages as $p) {
54+
foreach ($pages as $p) {
5255
$key[] = $p->modified();
5356
}
5457
$key = md5(\implode(',', $key));
5558
$response = $force ? null : static::cache()->get($key);
56-
if(!$response) {
59+
if (!$response) {
5760
$snippet = \Kirby\Toolkit\A::get($options, 'snippet', 'feed/rss');
5861
$response = snippet($snippet, static::data($pages, $options), true);
5962
static::cache()->set(
@@ -65,8 +68,8 @@ public static function feed($pages, $options = [], $force = null) {
6568
return $response;
6669
}
6770

68-
public static function data($pages, $options = []) {
69-
71+
public static function data($pages, $options = [])
72+
{
7073
$defaults = array(
7174
'url' => site()->url(),
7275
'title' => 'Feed',

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "bnomei/kirby3-feed",
33
"type": "plugin",
4-
"version": "1.0.2",
4+
"version": "1.0.3",
55
"description": "Generate a RSS/JSON-Feed from a Pages-Collection",
66
"license": "MIT",
77
"autoload": {

config.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
'cache' => true,
66
'debugforce' => true,
77
'expires' => (60*24*7), // minutes
8+
'mime' => null,
89
],
910
'snippets' => [
1011
'feed/rss' => __DIR__ . '/snippets/feed/rss.php',
@@ -13,12 +14,15 @@
1314
'pagesMethods' => [ // PAGES not PAGE
1415
'feed' => function ($options = [], $force = null) {
1516
$string = \Bnomei\Feed::feed($this, $options, $force);
17+
$mime = option('bnomei.feed.mime');
18+
$snippet = \Kirby\Toolkit\A::get($options, 'snippet');
1619

17-
if(\Bnomei\Feed::isJson($string)) {
20+
if ($mime) {
21+
return new Response($string, $mime);
22+
} elseif ($snippet == 'feed/json' || \Bnomei\Feed::isJson($string)) {
1823
return new Response($string, 'application/json');
19-
20-
} else if (\Bnomei\Feed::isXml($string)) {
21-
return new Response($string, 'text/xml');
24+
} elseif ($snippet == 'feed/rss' || \Bnomei\Feed::isXml($string)) {
25+
return new Response($string, 'application/rss+xml');
2226
}
2327
return $return;
2428
},

snippets/feed/json.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<?php
22

3-
header('Content-Type:application/json; charset=utf-8');
4-
53
$pages = $items;
64
$items = [];
75
foreach ($pages as $item) {

snippets/feed/rss.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
<?php
2-
3-
use \Kirby\Toolkit\XML;
4-
5-
header('Content-Type:text/xml; charset=utf-8');
6-
7-
?>
8-
<?xml version="1.0" encoding="utf-8"?>
1+
<?php use \Kirby\Toolkit\XML; ?><?xml version="1.0" encoding="utf-8"?>
92
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom">
103

114
<channel>

0 commit comments

Comments
 (0)