forked from acekyd/display-medium-posts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay-medium-posts.php
258 lines (229 loc) · 7.82 KB
/
display-medium-posts.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php
/**
* The plugin bootstrap file
*
* This file is read by WordPress to generate the plugin information in the plugin
* admin area. This file also includes all of the dependencies used by the plugin,
* registers the activation and deactivation functions, and defines a function
* that starts the plugin.
*
* @link http://www.acekyd.com
* @since 1.0.0
* @package Display_Medium_Posts
*
* @wordpress-plugin
* Plugin Name: Display Medium Posts
* Plugin URI: https://github.com/acekyd/display-medium-posts
* Description: Display Medium Posts is a wordpress plugin that allows users display posts from medium.com on any part of their website.
* Version: 4.0
* Author: AceKYD
* Author URI: http://www.acekyd.com
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: display-medium-posts
* Domain Path: /languages
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
/**
* The code that runs during plugin activation.
* This action is documented in includes/class-display-medium-posts-activator.php
*/
function activate_display_medium_posts() {
require_once plugin_dir_path( __FILE__ ) . 'includes/class-display-medium-posts-activator.php';
Display_Medium_Posts_Activator::activate();
}
/**
* The code that runs during plugin deactivation.
* This action is documented in includes/class-display-medium-posts-deactivator.php
*/
function deactivate_display_medium_posts() {
require_once plugin_dir_path( __FILE__ ) . 'includes/class-display-medium-posts-deactivator.php';
Display_Medium_Posts_Deactivator::deactivate();
}
register_activation_hook( __FILE__, 'activate_display_medium_posts' );
register_deactivation_hook( __FILE__, 'deactivate_display_medium_posts' );
/**
* The core plugin class that is used to define internationalization,
* admin-specific hooks, and public-facing site hooks.
*/
require plugin_dir_path( __FILE__ ) . 'includes/class-display-medium-posts.php';
/**
* Begins execution of the plugin.
*
* Since everything within the plugin is registered via hooks,
* then kicking off the plugin from this point in the file does
* not affect the page life cycle.
*
* @since 1.0.0
*/
function run_display_medium_posts() {
$plugin = new Display_Medium_Posts();
$plugin->run();
}
run_display_medium_posts();
// Example 1 : WP Shortcode to display form on any page or post.
function posts_display($atts){
ob_start();
$a = shortcode_atts(array('handle'=>'-1', 'default_image'=>'//i.imgur.com/p4juyuT.png', 'display' => 3, 'offset' => 0, 'total' => 10, 'list' => false, 'publication' => false, 'title_tag' => 'p', 'tag' => false, 'date_format' => 'M d, Y'), $atts);
// No ID value
if(strcmp($a['handle'], '-1') == 0){
return "";
}
$handle=$a['handle'];
$default_image = $a['default_image'];
$display = $a['display'];
$offset = $a['offset'];
$total = $a['total'];
$list = $a['list'] =='false' ? false: $a['list'];
$publication = $a['publication'] =='false' ? false: $a['publication'];
$title_tag = $a['title_tag'];
$tag = $a['tag'];
$date_format = $a['date_format'];
$content = null;
if($tag)
{
$medium_url = "https://medium.com/tag/" . $tag . "?format=json";
}
else $medium_url = "https://medium.com/" . $handle . "/latest?format=json";
try {
$ch = curl_init();
if (false === $ch)
throw new Exception('failed to initialize');
curl_setopt($ch, CURLOPT_URL, $medium_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
$content = curl_exec($ch);
if (false === $content)
throw new Exception(curl_error($ch), curl_errno($ch));
// ...process $content now
} catch (Exception $e) {
trigger_error(
sprintf(
'Curl failed with error #%d: %s',
$e->getCode(),
$e->getMessage()
),
E_USER_ERROR
);
}
$data = str_replace("])}while(1);</x>", "", $content);
if($publication) {
//If handle provided is specified as a publication
$json = json_decode($data);
$items = array();
$count = 0;
if(isset($json->payload->posts))
{
$posts = $json->payload->posts;
foreach($posts as $post)
{
$items[$count]['title'] = $post->title;
$items[$count]['url'] = 'https://medium.com/'.$handle.'/'.$post->uniqueSlug;
$items[$count]['subtitle'] = isset($post->virtuals->subtitle) ? $post->virtuals->subtitle : "";
if(!empty($post->virtuals->previewImage->imageId))
{
$image = '//cdn-images-1.medium.com/max/500/'.$post->virtuals->previewImage->imageId;
}
else {
$image = $default_image;
}
$items[$count]['image'] = $image;
$items[$count]['duration'] = round($post->virtuals->readingTime);
$items[$count]['date'] = isset($post->firstPublishedAt) ? date($date_format, $post->firstPublishedAt/1000): "";
$count++;
}
if($offset)
{
$items = array_slice($items, $offset);
}
if(count($items) > $total)
{
$items = array_slice($items, 0, $total);
}
}
}
else {
$json = json_decode($data);
$items = array();
$count = 0;
if(isset($json->payload->references->Post))
{
$posts = $json->payload->references->Post;
foreach($posts as $post)
{
$items[$count]['title'] = $post->title;
$items[$count]['url'] = 'https://medium.com/'.$handle.'/'.$post->uniqueSlug;
$items[$count]['subtitle'] = isset($post->content->subtitle) ? $post->content->subtitle : "";
if(!empty($post->virtuals->previewImage->imageId))
{
$image = '//cdn-images-1.medium.com/max/500/'.$post->virtuals->previewImage->imageId;
}
else {
$image = $default_image;
}
$items[$count]['image'] = $image;
$items[$count]['duration'] = round($post->virtuals->readingTime);
$items[$count]['date'] = isset($post->firstPublishedAt) ? date($date_format, $post->firstPublishedAt/1000): "";
$count++;
}
if($offset)
{
$items = array_slice($items, $offset);
}
if(count($items) > $total)
{
$items = array_slice($items, 0, $total);
}
}
}
?>
<div id="display-medium-owl-demo" class="display-medium-owl-carousel">
<?php foreach($items as $item) { ?>
<div class="display-medium-item">
<a href="<?php echo $item['url']; ?>" target="_blank">
<?php
if($list)
{
echo '<img src="'.$item['image'].'" class="display-medium-img">';
}
else
{
echo '<div data-src="'.$item['image'].'" class="lazyOwl medium-image"></div>';
}
?>
<<?php echo $title_tag; ?> class="display-medium-title details-title"><?php echo $item['title']; ?></<?php echo $title_tag; ?>>
</a>
<p class="display-medium-subtitle">
<?php echo $item['subtitle']; ?>
</p>
<p class="display-medium-date-read">
<?php echo "<span class='display-medium-date'>".$item['date']."</span>"; ?> / <?php echo "<span class='display-medium-readtime'>".$item['duration']."min read</span>"; ?>.
<a href="<?php echo $item['url']; ?>" target="_blank" class="text-right display-medium-readmore">Read More</a>
</p>
</div>
<?php } ?>
</div>
<?php
if(empty($items)) echo "<div class='display-medium-no-post'>No posts found!</div>";
?>
<script type="text/javascript">
function initializeOwl(count) {
jQuery(".display-medium-owl-carousel").owlCarousel({
items: count,
lazyLoad : true,
});
}
</script>
<?php
if(!$list)
{
echo '<script>initializeOwl('.$display.');</script>';
}
?>
<?php
return ob_get_clean();
}
add_shortcode('display_medium_posts', 'posts_display');