You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
106 lines
2.7 KiB
106 lines
2.7 KiB
<?php
|
|
|
|
namespace Grav\Plugin;
|
|
|
|
use Grav\Common\Config\Config;
|
|
use Grav\Common\GPM\Response;
|
|
use Grav\Common\Grav;
|
|
use Grav\Common\Uri;
|
|
use Grav\Common\Page\Collection;
|
|
use Grav\Common\Plugin;
|
|
use RocketTheme\Toolbox\Event\Event;
|
|
|
|
class LLInstagramPlugin extends Plugin
|
|
{
|
|
const API_URL = '/instagram/api/media';
|
|
|
|
public static function getSubscribedEvents()
|
|
{
|
|
return [
|
|
'onPluginsInitialized' => ['onPluginsInitialized', 0]
|
|
];
|
|
}
|
|
|
|
public function onPluginsInitialized()
|
|
{
|
|
$this->enable([
|
|
'onTwigInitialized' => ['onTwigInitialized', 0],
|
|
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
|
|
]);
|
|
}
|
|
|
|
public function onTwigInitialized()
|
|
{
|
|
$this->grav['assets']->add('plugin://llinstagram/llinstagram.css');
|
|
|
|
$this->grav['twig']->twig->addFunction(new \Twig_SimpleFunction('instagram_feed', [$this, 'feed']));
|
|
}
|
|
|
|
|
|
public function onTwigTemplatePaths()
|
|
{
|
|
$this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
|
|
}
|
|
|
|
public function feed($templateFile = 'partials/llinstagram.html.twig')
|
|
{
|
|
$feed = $this->getImages();
|
|
|
|
return $this->grav['twig']->twig()->render($templateFile, compact('feed'));
|
|
}
|
|
|
|
private function getImages()
|
|
{
|
|
$config = $this->mergeConfig($this->grav['page'], TRUE);
|
|
|
|
$type = $config->get('llinstagram.type');
|
|
$filter = $config->get('llinstagram.filter');
|
|
$count = $config->get('llinstagram.count');
|
|
$sort_order = $config->get('llinstagram.sort_order');
|
|
|
|
$params = array(
|
|
'filter' => $filter,
|
|
'page_size' => $count,
|
|
'page_number' => 0,
|
|
);
|
|
|
|
switch ($type) {
|
|
case 'images':
|
|
$params['type'] = 'GraphImage';
|
|
break;
|
|
|
|
case 'videos':
|
|
$params['type'] = 'GraphVideo';
|
|
break;
|
|
}
|
|
|
|
switch ($sort_order) {
|
|
case 'likes':
|
|
$params['sort_by'] = 'likes';
|
|
$params['sort_order'] = 'desc';
|
|
break;
|
|
|
|
case 'random':
|
|
$params['sort_order'] = 'random';
|
|
break;
|
|
|
|
case 'newest':
|
|
$params['sort_by'] = 'taken_at';
|
|
$params['sort_order'] = 'desc';
|
|
break;
|
|
|
|
case 'oldest':
|
|
$params['sort_by'] = 'taken_at';
|
|
$params['sort_order'] = 'asc';
|
|
break;
|
|
}
|
|
|
|
$baseurl = Uri::getCurrentUri()->withScheme('https')->getBaseUrl();
|
|
|
|
$url = sprintf('%s%s?%s', $baseurl, $this::API_URL, http_build_query($params));
|
|
$data = Response::get($url);
|
|
|
|
return json_decode($data);
|
|
}
|
|
}
|