Skip to content

Commit

Permalink
feat(blog): simple JSONP blog feed available at /blog-feed.js
Browse files Browse the repository at this point in the history
  • Loading branch information
mrclay committed Oct 4, 2016
1 parent 5de95af commit 09d0179
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
5 changes: 5 additions & 0 deletions start.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ function community_theme_init() {
]));

elgg_register_plugin_hook_handler('action', 'login', 'community_theme_login_action');

elgg_register_page_handler('blog-feed.js', function () {
echo elgg_view_resource('blog-feed.js');
return true;
});
}

function community_theme_login_action() {
Expand Down
29 changes: 29 additions & 0 deletions views/default/resources/blog-feed.js.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

$max_age = 3600 * 2;
header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() + $max_age), true);
header("Pragma: public", true);
header("Cache-Control: max-age=$max_age", true);
header("Content-Type; application/javascript; charset=utf-8");

$posts = elgg_get_entities([
'type' => 'object',
'subtype' => 'blog',
'distinct' => false,
'limit' => min(20, (int)get_input('limit', 10)),
]);

$items = [];
foreach ($posts as $post) {
/* @var ElggBlog $post */
$link = elgg_view('output/url', [
'href' => $post->getURL(),
'text' => $post->getDisplayName(),
]);
$time = date("F j, Y", $post->time_created);
$items[] = "<li>$link<div class='elgg-text-help'>$time</div></li>";
}
$html = "<ul class='elgg-blog-posts'>" . implode('', $items) . "</ul>";

?>
elgg_blog_feed(<?= json_encode($html) ?>);

0 comments on commit 09d0179

Please sign in to comment.