forked from cpannwitz/grav-plugin-fetchwp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetchwp.php
146 lines (122 loc) · 5.29 KB
/
fetchwp.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
namespace Grav\Plugin;
use \Grav\Common\Plugin;
use Grav\Common\Data\Data;
use Grav\Common\Page\Page;
use Grav\Common\GPM\Response;;
class fetchWPPlugin extends Plugin
{
private $template_post_html = 'partials/wordpress.post.html.twig';
private $template_post_vars = [];
private $template_event_vars = [];
private $feeds = array();
private $blog_image = '';
public static function getSubscribedEvents()
{
return [
'onPluginsInitialized' => ['onPluginsInitialized', 0]
];
}
public function onPluginsInitialized() {
$this->enable([
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
'onTwigInitialized' => ['onTwigInitialized', 0]]
);
}
public function onTwigInitialized() {
$this->grav['twig']->twig->addFunction(new \Twig_SimpleFunction(
'wordpress_posts',
[$this, 'getWordpressPosts']
));
}
/**
* Add current directory to twig lookup paths.
**/
public function onTwigTemplatePaths() {
$this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
}
public function getWordpressPosts() {
/** @var Page $page */
$page = $this->grav['page'];
/** @var Twig $twig */
$twig = $this->grav['twig'];
/** @var Data $config */
$config = $this->mergeConfig($page, TRUE);
$blogurl = $config->get('wordpress_fetch_settings.blogurl');
$enableFMedia = $config->get('wordpress_fetch_settings.enableFMedia');
$enableTranslation = $config->get('wordpress_fetch_settings.enableTranslation');
$translation = $this->grav['language']->getActive();
$url = $blogurl . '/wp-json/wp/v2/posts?' . ($enableFMedia ? '_embed' : '') . ($enableTranslation ? '&lang='.$translation : '');
try {
$results = Response::get($url);
} catch (\Exception $e) {
return;
}
if ($results != null) {
$this->parsePostResponse($results, $config);
$this->template_post_vars = [
'feed' => $this->feeds,
'postcount' => empty($config->get('wordpress_fetch_settings.count')) ? 3 : $config->get('wordpress_fetch_settings.count'),
'sectiontitle' => $config->get('wordpress_fetch_settings.sectiontitle'),
'altimage' => $config->get('wordpress_fetch_settings.altImage'),
];
$isitimage = $config->get('wordpress_fetch_settings.altImage');
$output = $this->grav['twig']->twig()->render($this->template_post_html, $this->template_post_vars);
return $output;
} else {
return;
}
}
private function parsePostResponse($json, $config) {
$r = array();
$content = json_decode($json);
$count = empty($config->get('wordpress_fetch_settings.count')) ? 3 : $config->get('wordpress_fetch_settings.count');
foreach ($content as $blogitem) {
if (property_exists($blogitem, 'excerpt') && isset($blogitem->excerpt)) {
$blog_image = '';
$has_blogimage = '';
$blog_image_quality = '';
$blog_title = '';
$blog_link = '';
$blog_excerpt = '';
if($config->get('wordpress_fetch_settings.enableFMedia')) {
$has_blogimage = $blogitem->featured_media;
$blog_image_quality = $config->get('wordpress_fetch_settings.qualityFMedia');
if ($has_blogimage !== 0) {
$blog_image = $blogitem->_embedded->{'wp:featuredmedia'}[0]->media_details->sizes->$blog_image_quality->source_url;
$r[$count]['hasimg'] = $has_blogimage;
$r[$count]['imgsrc'] = $blog_image;
}
$blog_author_name = $blogitem->_embedded->author[0]->name;
$blog_author_link = $blogitem->_embedded->author[0]->link;
$blog_author_description = $blogitem->_embedded->author[0]->description;
$r[$count]['authorname'] = $blog_author_name;
$r[$count]['authorlink'] = $blog_author_link;
$r[$count]['authordescription'] = $blog_author_description;
}
$blog_title = $blogitem->title->rendered;
$blog_link = $blogitem->link;
$blog_date = $blogitem->date;
$blog_modified = $blogitem->modified;
$blog_excerpt = $blogitem->excerpt->rendered;
$blog_content = $blogitem->content->rendered;
$r[$count]['blogtitle'] = $blog_title;
$r[$count]['bloglink'] = $blog_link;
$r[$count]['blogdate'] = $blog_date;
$r[$count]['blogmodified'] = $blog_modified;
$r[$count]['blogexcerpt'] = $blog_excerpt;
$r[$count]['blogcontent'] = $blog_content;
$this->addFeed($r);
$count -= 1;
}
}
}
private function addFeed($result) {
foreach ($result as $key => $blogitem) {
if (!isset($this->feeds[$key])) {
$this->feeds[$key] = $blogitem;
}
}
krsort($this->feeds);
}
}