Skip to content

Commit 7e8dab2

Browse files
committed
updated data structure to match new rss data source
1 parent dd34fee commit 7e8dab2

File tree

1 file changed

+29
-71
lines changed

1 file changed

+29
-71
lines changed

display-medium-posts.php

Lines changed: 29 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -125,82 +125,40 @@ function posts_display($atts){
125125

126126
}
127127

128-
$data = str_replace("])}while(1);</x>", "", $content);
129-
130-
if($publication) {
131-
//If handle provided is specified as a publication
132-
$json = json_decode($data);
133-
$items = array();
134-
$count = 0;
135-
if(isset($json->payload->posts))
136-
{
137-
$posts = $json->payload->posts;
138-
foreach($posts as $post)
139-
{
140-
$items[$count]['title'] = $post->title;
141-
$items[$count]['url'] = 'https://medium.com/'.$handle.'/'.$post->uniqueSlug;
142-
$items[$count]['subtitle'] = isset($post->virtuals->subtitle) ? $post->virtuals->subtitle : "";
143-
if(!empty($post->virtuals->previewImage->imageId))
144-
{
145-
$image = '//cdn-images-1.medium.com/max/500/'.$post->virtuals->previewImage->imageId;
146-
}
147-
else {
148-
$image = $default_image;
149-
}
150-
$items[$count]['image'] = $image;
151-
$items[$count]['duration'] = round($post->virtuals->readingTime);
152-
$items[$count]['date'] = isset($post->firstPublishedAt) ? date($date_format, $post->firstPublishedAt/1000): "";
153-
154-
$count++;
155-
}
156-
if($offset)
157-
{
158-
$items = array_slice($items, $offset);
128+
$json = json_decode($content);
129+
$items = array();
130+
$count = 0;
131+
if (isset($json->items)) {
132+
$posts = $json->items;
133+
foreach ($posts as $post) {
134+
$items[$count]['title'] = $post->title;
135+
$items[$count]['url'] = $post->link;
136+
137+
$start = strpos($post->description, '<p>');
138+
$end = strpos($post->description, '</p>', $start);
139+
$paragraph = substr($post->description, $start, $end - $start + 4);
140+
$items[$count]['subtitle'] = mb_strimwidth(html_entity_decode(strip_tags($paragraph)), 0, 140, "...");
141+
142+
if (!empty($post->thumbnail)) {
143+
$image = $post->thumbnail;
144+
} else {
145+
$image = $default_image;
159146
}
147+
$items[$count]['image'] = $image;
160148

161-
if(count($items) > $total)
162-
{
163-
$items = array_slice($items, 0, $total);
164-
}
165-
}
166-
}
167-
else {
149+
$items[$count]['date'] = date("M j, Y", strtotime($post->pubDate));
168150

169-
$json = json_decode($data);
170-
$items = array();
171-
$count = 0;
172-
if(isset($json->payload->references->Post))
173-
{
174-
$posts = $json->payload->references->Post;
175-
foreach($posts as $post)
176-
{
177-
$items[$count]['title'] = $post->title;
178-
$items[$count]['url'] = 'https://medium.com/'.$handle.'/'.$post->uniqueSlug;
179-
$items[$count]['subtitle'] = isset($post->content->subtitle) ? $post->content->subtitle : "";
180-
if(!empty($post->virtuals->previewImage->imageId))
181-
{
182-
$image = '//cdn-images-1.medium.com/max/500/'.$post->virtuals->previewImage->imageId;
183-
}
184-
else {
185-
$image = $default_image;
186-
}
187-
$items[$count]['image'] = $image;
188-
$items[$count]['duration'] = round($post->virtuals->readingTime);
189-
$items[$count]['date'] = isset($post->firstPublishedAt) ? date($date_format, $post->firstPublishedAt/1000): "";
190-
191-
$count++;
192-
}
193-
if($offset)
194-
{
195-
$items = array_slice($items, $offset);
196-
}
151+
$count++;
152+
}
153+
if ($offset) {
154+
$items = array_slice($items, $offset);
155+
}
197156

198-
if(count($items) > $total)
199-
{
200-
$items = array_slice($items, 0, $total);
201-
}
157+
if (count($items) > $total) {
158+
$items = array_slice($items, 0, $total);
202159
}
203-
}
160+
}
161+
204162
?>
205163
<div id="display-medium-owl-demo" class="display-medium-owl-carousel">
206164
<?php foreach($items as $item) { ?>

0 commit comments

Comments
 (0)