-
Notifications
You must be signed in to change notification settings - Fork 1
/
pinterest-pinboard-widget.php
296 lines (254 loc) · 11.1 KB
/
pinterest-pinboard-widget.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<?php
/*
Plugin Name: Pinterest Pinboard Widget
Plugin URI: http://wordpress.org/extend/plugins/pinterest-pinboard-widget/
Description: Add a Pinterest Pinboard widget and shortcode to WordPress.
Author: CodeFish
Author URI: http://www.codefish.nl
Version: 1.0.7
*/
/* Copyright 2012-2014 CodeFish (email: info at codefish.nl)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
include_once(ABSPATH . WPINC . '/feed.php');
// Defaults
define('PINTEREST_PINBOARD_DEFAULT_USERNAME', 'pinterest');
define('PINTEREST_PINBOARD_DEFAULT_ROWS', 3);
define('PINTEREST_PINBOARD_DEFAULT_COLS', 3);
define('PINTEREST_PINBOARD_DEFAULT_NEW_WINDOW', 0);
// Shortcode definition
define('PINTEREST_PINBOARD_SHORTCODE', 'pinterest_pinboard');
/**
* Pinterest pinboard class to fetch the pinterest feed
* and render the HTML pinboard.
*/
class Pinterest_Pinboard {
// Pinterest url
var $pinterest_feed_url = 'https://pinterest.com/%s/feed.rss';
// RSS cache lifetime in seconds
var $cache_lifetime = 900;
var $start_time;
function Pinterest_Pinboard() {
$this->start_time = microtime(true);
}
// Render the pinboard and output
function render($username, $rows, $cols, $new_window) {
$nr_pins = $rows * $cols;
$pins = $this->get_pins($username, $nr_pins);
if (is_null($pins)) {
echo("Unable to load Pinterest pins for '$username'\n");
} else {
echo("<div class=\"pinboard\">\n");
$row = 0;
$col = 0;
foreach ($pins as $pin) {
if ($col == 0) {
echo("<div class=\"row\">\n");
}
$title = $pin['title'];
$url = $pin['url'];
$image = $pin['image'];
echo("<a href=\"$url\"");
if ($new_window) {
echo(" target=\"_blank\"");
}
echo("><img src=\"$image\" alt=\"$title\" title=\"$title\" /></a>");
$col++;
if ($col >= $cols) {
echo("</div>\n");
$col = 0;
$row++;
}
}
}
?>
</div>
<div class="pin_link">
<a class="pin_logo" href="http://pinterest.com/<?php echo($username) ?>/">
<img src="//passets-cdn.pinterest.com/images/small-p-button.png" width="16" height="16" alt="Follow Me on Pinterest" />
</a>
<span class="pin_text"><a href="http://pinterest.com/<?php echo($username) ?>/" <?php if ($new_window) { ?>target="_blank"<?php } ?>><?php _e("More Pins") ?></a></span>
</div>
<?php
echo($this->get_footer());
}
/**
* Retrieve RSS feed for username, and parse the data needed from it.
* Returns null on error, otherwise a hash of pins.
*/
function get_pins($username, $nrpins) {
// Set caching.
add_filter('wp_feed_cache_transient_lifetime', create_function('$a', 'return '. $this->cache_lifetime .';'));
// Get the RSS feed.
$url = sprintf($this->pinterest_feed_url, $username);
$rss = fetch_feed($url);
if (is_wp_error($rss)) {
return null;
}
$maxitems = $rss->get_item_quantity($nrpins);
$rss_items = $rss->get_items(0, $maxitems);
$pins;
if (is_null($rss_items)) {
$pins = null;
} else {
// Build patterns to search/replace in the image urls.
// Pattern to replace for the images.
$search = array('_b.jpg');
$replace = array('_t.jpg');
// Make urls protocol relative
array_push($search, 'https://');
array_push($replace, '//');
$pins = array();
foreach ($rss_items as $item) {
$title = $item->get_title();
$description = $item->get_description();
$url = $item->get_permalink();
if (preg_match_all('/<img src="([^"]*)".*>/i', $description, $matches)) {
$image = str_replace($search, $replace, $matches[1][0]);
}
array_push($pins, array(
'title' => $title,
'image' => $image,
'url' => $url
));
}
}
return $pins;
}
/**
* Determine the running plugin's version.
*/
function get_version() {
$headers = array(
'Version' => 'Version'
);
$plugin_data = get_file_data(__FILE__, $headers);
$plugin_version = $plugin_data['Version'];
return $plugin_version;
}
/**
* Render HTML comment footer for debugging purposes.
*/
function get_footer() {
$execution_time = (microtime(true) - $this->start_time) * 1e6;
return '<!-- '.
'Version: '. $this->get_version() .' // '.
'Execution Time: '. $execution_time .' (ms) '.
"-->\n";
}
}
class Pinterest_Pinboard_Widget extends WP_Widget {
/**
* Widget settings.
*/
protected $widget = array(
// Default title for the widget in the sidebar.
'title' => 'Recent pins',
// Default widget settings.
'username' => PINTEREST_PINBOARD_DEFAULT_USERNAME,
'rows' => PINTEREST_PINBOARD_DEFAULT_ROWS,
'cols' => PINTEREST_PINBOARD_DEFAULT_COLS,
'new_window' => PINTEREST_PINBOARD_DEFAULT_NEW_WINDOW,
// The widget description used in the admin area.
'description' => 'Adds a Pinterest Pinboard widget to your sidebar'
);
function Pinterest_Pinboard_Widget() {
$id = str_replace('_', '-', get_class($this));
parent::WP_Widget(
$id,
'Pinterest Pinboard',
$options = array(
'description' => $this->widget['description']
)
);
}
function form($instance) {
// load current values or set to default.
$title = array_key_exists('title', $instance) ? esc_attr($instance['title']) : $this->widget['title'];
$username = array_key_exists('username', $instance) ? esc_attr($instance['username']) : $this->widget['username'];
$cols = array_key_exists('cols', $instance) ? esc_attr($instance['cols']) : $this->widget['cols'];
$rows = array_key_exists('rows', $instance) ? esc_attr($instance['rows']) : $this->widget['rows'];
$new_window = array_key_exists('new_window', $instance) ? esc_attr($instance['new_window']) : $this->widget['new_window'];
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('username'); ?>"><?php _e('Username:'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('username'); ?>" name="<?php echo $this->get_field_name('username'); ?>" type="text" value="<?php echo $username; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('cols'); ?>"><?php _e('Nr. of pins wide:'); ?></label>
<input id="<?php echo $this->get_field_id('cols'); ?>" name="<?php echo $this->get_field_name('cols'); ?>" type="text" value="<?php echo $cols; ?>" size="3" />
</p>
<p>
<label for="<?php echo $this->get_field_id('rows'); ?>"><?php _e('Nr. of pins tall:'); ?></label>
<input id="<?php echo $this->get_field_id('rows'); ?>" name="<?php echo $this->get_field_name('rows'); ?>" type="text" value="<?php echo $rows; ?>" size="3" />
</p>
<p>
<input id="<?php echo $this->get_field_id('new_window'); ?>" name="<?php echo $this->get_field_name('new_window'); ?>" type="checkbox" <?php if ($new_window) { ?>checked="checked" <?php } ?> />
<label for="<?php echo $this->get_field_id('new_window'); ?>"><?php _e('Open links in a new window?'); ?></label>
</p>
<?php
}
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['username'] = strip_tags($new_instance['username']);
$instance['rows'] = strip_tags($new_instance['rows']);
$instance['cols'] = strip_tags($new_instance['cols']);
$instance['new_window'] = isset($new_instance['new_window']) ? 1 : 0;
return $instance;
}
function widget($args, $instance) {
extract($args);
echo($before_widget);
$title = apply_filters('widget_title', $instance['title']);
echo($before_title . __($title) . $after_title);
echo("<div id=\"pinterest-pinboard-widget-container\">\n");
// Render the pinboard from the widget settings.
$username = $instance['username'];
$rows = $instance['rows'];
$cols = $instance['cols'];
$new_window = $instance['new_window'];
$pinboard = new Pinterest_Pinboard();
$pinboard->render($username, $rows, $cols, $new_window);
echo("</div>");
echo($after_widget);
}
}
// Register the widget.
add_action('widgets_init', create_function('', 'return register_widget("Pinterest_Pinboard_Widget");'));
// Register plugin stylesheet.
function pinterest_pinboard_widget_css() {
wp_register_style('pinterest-pinboard-widget-style', plugins_url('style.css?v=1', __FILE__));
wp_enqueue_style('pinterest-pinboard-widget-style');
}
add_action('wp_enqueue_scripts', 'pinterest_pinboard_widget_css');
// Register shortcode
function pinterest_pinboard_widget_shortcode($atts) {
$a = shortcode_atts(array(
'username' => PINTEREST_PINBOARD_DEFAULT_USERNAME,
'rows' => PINTEREST_PINBOARD_DEFAULT_ROWS,
'cols' => PINTEREST_PINBOARD_DEFAULT_COLS,
'new_window' => PINTEREST_PINBOARD_DEFAULT_NEW_WINDOW
), $atts, PINTEREST_PINBOARD_SHORTCODE
);
ob_start();
$pinboard = new Pinterest_Pinboard();
$pinboard->render($a['username'], $a['rows'], $a['cols'], $a['new_window']);
$html = ob_get_clean();
return "<div id=\"pinterest-pinboard-container\">$html</div>\n";
}
add_shortcode(PINTEREST_PINBOARD_SHORTCODE, 'pinterest_pinboard_widget_shortcode');
?>