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
|
|
|
|
*/
|
2022-07-01 16:10:30 +03:00
|
|
|
class JsonFormat extends FormatAbstract
|
|
|
|
{
|
|
|
|
const MIME_TYPE = 'application/json';
|
|
|
|
|
|
|
|
const VENDOR_EXCLUDES = [
|
|
|
|
'author',
|
|
|
|
'title',
|
|
|
|
'uri',
|
|
|
|
'timestamp',
|
|
|
|
'content',
|
|
|
|
'enclosures',
|
|
|
|
'categories',
|
|
|
|
'uid',
|
|
|
|
];
|
|
|
|
|
|
|
|
public function stringify()
|
|
|
|
{
|
2024-01-09 22:18:33 +03:00
|
|
|
$feedArray = $this->getFeed();
|
|
|
|
|
2022-07-01 16:10:30 +03:00
|
|
|
$data = [
|
2024-01-09 22:18:33 +03:00
|
|
|
'version' => 'https://jsonfeed.org/version/1',
|
|
|
|
'title' => $feedArray['name'],
|
|
|
|
'home_page_url' => $feedArray['uri'],
|
|
|
|
'feed_url' => get_current_url(),
|
2022-07-01 16:10:30 +03:00
|
|
|
];
|
|
|
|
|
2024-01-09 22:18:33 +03:00
|
|
|
if ($feedArray['icon']) {
|
|
|
|
$data['icon'] = $feedArray['icon'];
|
|
|
|
$data['favicon'] = $feedArray['icon'];
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$items = [];
|
|
|
|
foreach ($this->getItems() as $item) {
|
|
|
|
$entry = [];
|
|
|
|
|
|
|
|
$entryAuthor = $item->getAuthor();
|
|
|
|
$entryTitle = $item->getTitle();
|
|
|
|
$entryUri = $item->getURI();
|
|
|
|
$entryTimestamp = $item->getTimestamp();
|
2024-01-09 23:36:42 +03:00
|
|
|
$entryContent = $item->getContent() ?? '';
|
2022-07-01 16:10:30 +03:00
|
|
|
$entryEnclosures = $item->getEnclosures();
|
|
|
|
$entryCategories = $item->getCategories();
|
|
|
|
|
|
|
|
$vendorFields = $item->toArray();
|
|
|
|
foreach (self::VENDOR_EXCLUDES as $key) {
|
|
|
|
unset($vendorFields[$key]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$entry['id'] = $item->getUid();
|
|
|
|
|
|
|
|
if (empty($entry['id'])) {
|
|
|
|
$entry['id'] = $entryUri;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
if (!empty($entryUri)) {
|
|
|
|
$entry['url'] = $entryUri;
|
|
|
|
}
|
|
|
|
if (!empty($entryContent)) {
|
2022-08-06 23:46:28 +03:00
|
|
|
if (is_html($entryContent)) {
|
2022-07-01 16:10:30 +03:00
|
|
|
$entry['content_html'] = $entryContent;
|
|
|
|
} else {
|
|
|
|
$entry['content_text'] = $entryContent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!empty($entryEnclosures)) {
|
|
|
|
$entry['attachments'] = [];
|
|
|
|
foreach ($entryEnclosures as $enclosure) {
|
|
|
|
$entry['attachments'][] = [
|
|
|
|
'url' => $enclosure,
|
2022-08-06 23:46:28 +03:00
|
|
|
'mime_type' => parse_mime_type($enclosure)
|
2022-07-01 16:10:30 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!empty($entryCategories)) {
|
|
|
|
$entry['tags'] = [];
|
|
|
|
foreach ($entryCategories as $category) {
|
|
|
|
$entry['tags'][] = $category;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!empty($vendorFields)) {
|
|
|
|
$entry['_rssbridge'] = $vendorFields;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($entry['id'])) {
|
|
|
|
$entry['id'] = hash('sha1', $entryTitle . $entryContent);
|
|
|
|
}
|
|
|
|
|
|
|
|
$items[] = $entry;
|
|
|
|
}
|
|
|
|
$data['items'] = $items;
|
|
|
|
|
2023-09-25 23:32:15 +03:00
|
|
|
// Ignoring invalid json
|
|
|
|
$json = json_encode($data, \JSON_PRETTY_PRINT | \JSON_INVALID_UTF8_IGNORE);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
|
|
|
return $json;
|
|
|
|
}
|
2016-02-19 19:15:06 +03:00
|
|
|
}
|