2013-08-11 15:30:41 +04:00
|
|
|
<?php
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2013-08-11 15:30:41 +04:00
|
|
|
/**
|
2019-01-05 15:20:11 +03:00
|
|
|
* JsonFormat - JSON Feed Version 1
|
|
|
|
* https://jsonfeed.org/version/1
|
|
|
|
*
|
|
|
|
* Validators:
|
|
|
|
* https://validator.jsonfeed.org
|
|
|
|
* https://github.com/vigetlabs/json-feed-validator
|
|
|
|
*/
|
2016-09-10 21:41:11 +03:00
|
|
|
class JsonFormat extends FormatAbstract
|
|
|
|
{
|
2019-10-31 21:00:12 +03:00
|
|
|
const MIME_TYPE = 'application/json';
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-01-05 15:20:11 +03:00
|
|
|
const VENDOR_EXCLUDES = [
|
|
|
|
'author',
|
|
|
|
'title',
|
|
|
|
'uri',
|
|
|
|
'timestamp',
|
|
|
|
'content',
|
|
|
|
'enclosures',
|
|
|
|
'categories',
|
2019-02-03 22:56:41 +03:00
|
|
|
'uid',
|
2019-01-05 15:20:11 +03:00
|
|
|
];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
public function stringify()
|
|
|
|
{
|
2022-09-08 20:07:57 +03:00
|
|
|
$host = $_SERVER['HTTP_HOST'] ?? '';
|
2019-01-05 15:20:11 +03:00
|
|
|
$extraInfos = $this->getExtraInfos();
|
|
|
|
$data = [
|
2019-02-03 22:56:41 +03:00
|
|
|
'version' => 'https://jsonfeed.org/version/1',
|
2022-09-08 20:07:57 +03:00
|
|
|
'title' => empty($extraInfos['name']) ? $host : $extraInfos['name'],
|
|
|
|
'home_page_url' => empty($extraInfos['uri']) ? REPOSITORY : $extraInfos['uri'],
|
|
|
|
'feed_url' => get_current_url(),
|
2019-01-05 15:20:11 +03:00
|
|
|
];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-01-05 15:20:11 +03:00
|
|
|
if (!empty($extraInfos['icon'])) {
|
|
|
|
$data['icon'] = $extraInfos['icon'];
|
|
|
|
$data['favicon'] = $extraInfos['icon'];
|
2018-12-27 00:41:32 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-01-05 15:20:11 +03:00
|
|
|
$items = [];
|
|
|
|
foreach ($this->getItems() as $item) {
|
|
|
|
$entry = [];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-02-03 22:56:41 +03:00
|
|
|
$entryAuthor = $item->getAuthor();
|
|
|
|
$entryTitle = $item->getTitle();
|
|
|
|
$entryUri = $item->getURI();
|
|
|
|
$entryTimestamp = $item->getTimestamp();
|
2022-08-06 23:46:28 +03:00
|
|
|
$entryContent = $item->getContent() ? sanitize_html($item->getContent()) : '';
|
2019-02-03 22:56:41 +03:00
|
|
|
$entryEnclosures = $item->getEnclosures();
|
|
|
|
$entryCategories = $item->getCategories();
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-01-05 15:20:11 +03:00
|
|
|
$vendorFields = $item->toArray();
|
|
|
|
foreach (self::VENDOR_EXCLUDES as $key) {
|
|
|
|
unset($vendorFields[$key]);
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-02-03 22:56:41 +03:00
|
|
|
$entry['id'] = $item->getUid();
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-02-03 22:56:41 +03:00
|
|
|
if (empty($entry['id'])) {
|
|
|
|
$entry['id'] = $entryUri;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-01-05 15:20:11 +03:00
|
|
|
if (!empty($entryTitle)) {
|
|
|
|
$entry['title'] = $entryTitle;
|
|
|
|
}
|
|
|
|
if (!empty($entryAuthor)) {
|
|
|
|
$entry['author'] = [
|
|
|
|
'name' => $entryAuthor
|
|
|
|
];
|
|
|
|
}
|
|
|
|
if (!empty($entryTimestamp)) {
|
2022-08-06 23:46:28 +03:00
|
|
|
$entry['date_modified'] = gmdate(\DATE_ATOM, $entryTimestamp);
|
2019-01-05 15:20:11 +03:00
|
|
|
}
|
|
|
|
if (!empty($entryUri)) {
|
|
|
|
$entry['url'] = $entryUri;
|
|
|
|
}
|
|
|
|
if (!empty($entryContent)) {
|
2022-08-06 23:46:28 +03:00
|
|
|
if (is_html($entryContent)) {
|
2019-01-05 15:20:11 +03:00
|
|
|
$entry['content_html'] = $entryContent;
|
|
|
|
} else {
|
|
|
|
$entry['content_text'] = $entryContent;
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
2019-01-05 15:20:11 +03:00
|
|
|
}
|
|
|
|
if (!empty($entryEnclosures)) {
|
|
|
|
$entry['attachments'] = [];
|
|
|
|
foreach ($entryEnclosures as $enclosure) {
|
|
|
|
$entry['attachments'][] = [
|
2019-02-03 22:56:41 +03:00
|
|
|
'url' => $enclosure,
|
2022-08-06 23:46:28 +03:00
|
|
|
'mime_type' => parse_mime_type($enclosure)
|
2019-01-05 15:20:11 +03:00
|
|
|
];
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
2019-01-05 15:20:11 +03:00
|
|
|
}
|
|
|
|
if (!empty($entryCategories)) {
|
|
|
|
$entry['tags'] = [];
|
|
|
|
foreach ($entryCategories as $category) {
|
|
|
|
$entry['tags'][] = $category;
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
2019-01-05 15:20:11 +03:00
|
|
|
}
|
|
|
|
if (!empty($vendorFields)) {
|
|
|
|
$entry['_rssbridge'] = $vendorFields;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-01-05 15:20:11 +03:00
|
|
|
if (empty($entry['id'])) {
|
|
|
|
$entry['id'] = hash('sha1', $entryTitle . $entryContent);
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2019-01-05 15:20:11 +03:00
|
|
|
$items[] = $entry;
|
|
|
|
}
|
|
|
|
$data['items'] = $items;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-03-29 23:45:00 +03:00
|
|
|
/**
|
|
|
|
* The intention here is to discard non-utf8 byte sequences.
|
|
|
|
* But the JSON_PARTIAL_OUTPUT_ON_ERROR also discards lots of other errors.
|
|
|
|
* So consider this a hack.
|
|
|
|
* Switch to JSON_INVALID_UTF8_IGNORE when PHP 7.2 is the latest platform requirement.
|
|
|
|
*/
|
2022-08-06 23:46:28 +03:00
|
|
|
$json = json_encode($data, \JSON_PRETTY_PRINT | \JSON_PARTIAL_OUTPUT_ON_ERROR);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-03-29 23:45:00 +03:00
|
|
|
return $json;
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
2016-02-19 19:15:06 +03:00
|
|
|
}
|