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()
|
|
|
|
{
|
2024-01-09 22:18:33 +03:00
|
|
|
$feedArray = $this->getFeed();
|
|
|
|
|
2019-01-05 15:20:11 +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(),
|
2019-01-05 15:20:11 +03:00
|
|
|
];
|
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'];
|
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();
|
2024-01-09 23:36:42 +03:00
|
|
|
$entryContent = $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
|
|
|
|
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
|
|
|
|
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
|
|
|
}
|