-
Notifications
You must be signed in to change notification settings - Fork 0
/
instagram-widget.php
295 lines (214 loc) · 10.3 KB
/
instagram-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
<?php
/*-----------------------------------------------------------------------------------
Plugin Name: Core Instagram
Plugin URI: http://themeshash.com/wordpress-plugins/
Description: A widget for showing Instagram photos via widget.
Version: 1.0
Author: Muhammad Faisal
Author URI: http://themeshash.com/
-----------------------------------------------------------------------------------*/
// Add function to widgets_init that'll load our widget
add_action( 'widgets_init', 'th_instagram_widget_init' );
// Register widget
function th_instagram_widget_init() {
register_widget( 'th_instagram_widget' );
}
// Widget class
class th_instagram_widget extends WP_Widget {
#-------------------------------------------------------------------------------#
# Widget Setup
#-------------------------------------------------------------------------------#
function __construct() {
// Widget settings
$widget_ops = array(
'classname' => 'widget-instagram',
'description' => esc_html__('A widget for showing your latest Instagram photos.', 'themeshash')
);
// Widget control settings
$control_ops = array(
'width' => 300,
'height' => 350,
'id_base' => 'th_instagram_widget'
);
// Create the widget
parent::__construct( 'th_instagram_widget', esc_html__('Instagram Feed', 'themeshash'), $widget_ops, $control_ops );
}
#-------------------------------------------------------------------------------#
# Widget Display
#-------------------------------------------------------------------------------#
public function widget( $args, $instance ) {
extract( $args );
// Our variables from the widget settings
$title = apply_filters('widget_title', $instance['title'] );
$username = $instance['username'];
$limit = $instance['number'];
$size = $instance['size'];
$target = $instance['target'];
// Before widget (defined by theme functions file)
echo wp_kses_post( $before_widget );
// Display the widget title if one was input
if ( $title )
echo wp_kses_post( $before_title . $title . $after_title );
?>
<div class="widget-content">
<?php do_action( 'th_before_instagram_widget' ); ?>
<?php
if ( $username != '' ) {
$media_array = $this->scrape_instagram( $username, $limit );
if ( is_wp_error( $media_array ) ) {
echo $media_array->get_error_message();
} else {
// filter for images only?
if ( $images_only = apply_filters( 'wpiw_images_only', FALSE ) )
$media_array = array_filter( $media_array, array( $this, 'images_only' ) );
?>
<ul class="instagram-pics instagram-size-<?php echo $size; ?>">
<?php foreach ( $media_array as $item ) {
echo '<li><a href="'. esc_url( $item['link'] ) .'" target="'. esc_attr( $target ) .'"><img src="'. esc_url( $item[$size] ) .'" alt="'. esc_attr( $item['description'] ) .'" title="'. esc_attr( $item['description'] ).'" /></a></li>';
} ?>
</ul>
<?php
}
}
?>
<?php do_action( 'th_after_instagram_widget' ); ?>
</div>
<?php
// After widget (defined by theme functions file)
echo wp_kses_post( $after_widget );
}
#-------------------------------------------------------------------------------#
# Widget Update
#-------------------------------------------------------------------------------#
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
// Strip tags to remove HTML (important for text inputs)
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['username'] = strip_tags( $new_instance['username'] );
$instance['number'] = strip_tags( $new_instance['number'] );
$instance['size'] = strip_tags( $new_instance['size'] );
$instance['target'] = strip_tags( $new_instance['target'] );
// No need to strip tags
return $instance;
}
#-------------------------------------------------------------------------------#
# Widget Form
#-------------------------------------------------------------------------------#
public function form( $instance ) {
// Set up some default widget settings
$defaults = array(
'title' => __('Instagram', 'themeshash'),
'username' => '',
'number' => '9',
'size' => 'small',
'target' => '_blank',
);
$instance = wp_parse_args( (array) $instance, $defaults ); ?>
<!-- Title: Text Input -->
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e('Title', 'themeshash') ?>:</label>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>" />
</p>
<!-- User Name: Text Input -->
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'username' ) ); ?>"><?php esc_html_e('Username', 'themeshash') ?>:</label>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'username' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'username' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['username'] ); ?>" />
</p>
<!-- Number: Text Input -->
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>"><?php esc_html_e('Number of Photos', 'themeshash') ?>:</label>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'number' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['number'] ); ?>" />
</p>
<!-- Size: Text Input -->
<p>
<label for="<?php echo $this->get_field_id( 'size' ); ?>"><?php _e( 'Photo size', 'themeshash' ); ?>:</label>
<select id="<?php echo $this->get_field_id( 'size' ); ?>" name="<?php echo $this->get_field_name( 'size' ); ?>" class="widefat">
<option value="thumbnail" <?php selected( 'thumbnail', $instance['size'] ) ?>><?php _e( 'Thumbnail', 'themeshash' ); ?></option>
<option value="small" <?php selected( 'small', $instance['size'] ) ?>><?php _e( 'Small', 'themeshash' ); ?></option>
<option value="large" <?php selected( 'large', $instance['size'] ) ?>><?php _e( 'Large', 'themeshash' ); ?></option>
<option value="original" <?php selected( 'original', $instance['size'] ) ?>><?php _e( 'Original', 'themeshash' ); ?></option>
</select>
</p>
<!-- Target: Text Input -->
<p>
<label for="<?php echo $this->get_field_id( 'target' ); ?>"><?php _e( 'Open links in', 'themeshash' ); ?>:</label>
<select id="<?php echo $this->get_field_id( 'target' ); ?>" name="<?php echo $this->get_field_name( 'target' ); ?>" class="widefat">
<option value="_self" <?php selected( '_self', $instance['target'] ) ?>><?php _e( 'Current window (_self)', 'themeshash' ); ?></option>
<option value="_blank" <?php selected( '_blank', $instance['target'] ) ?>><?php _e( 'New window (_blank)', 'themeshash' ); ?></option>
</select>
</p>
<?php
}
#-------------------------------------------------------------------------------#
# Instagram Scarpper
#-------------------------------------------------------------------------------#
// based on https://gist.github.com/cosmocatalano/4544576
public function scrape_instagram( $username, $slice = 9 ) {
$username = strtolower( $username );
$username = str_replace( '@', '', $username );
if ( false === ( $instagram = get_transient( 'instagram-media-5-'.sanitize_title_with_dashes( $username ) ) ) ) {
$remote = wp_remote_get( 'http://instagram.com/'.trim( $username ) );
if ( is_wp_error( $remote ) )
return new WP_Error( 'site_down', __( 'Unable to communicate with Instagram.', 'wp-instagram-widget' ) );
if ( 200 != wp_remote_retrieve_response_code( $remote ) )
return new WP_Error( 'invalid_response', __( 'Instagram did not return a 200.', 'wp-instagram-widget' ) );
$shards = explode( 'window._sharedData = ', $remote['body'] );
$insta_json = explode( ';</script>', $shards[1] );
$insta_array = json_decode( $insta_json[0], TRUE );
if ( ! $insta_array )
return new WP_Error( 'bad_json', __( 'Instagram has returned invalid data.', 'wp-instagram-widget' ) );
if ( isset( $insta_array['entry_data']['ProfilePage'][0]['user']['media']['nodes'] ) ) {
$images = $insta_array['entry_data']['ProfilePage'][0]['user']['media']['nodes'];
} else {
return new WP_Error( 'bad_json_2', __( 'Instagram has returned invalid data.', 'wp-instagram-widget' ) );
}
if ( ! is_array( $images ) )
return new WP_Error( 'bad_array', __( 'Instagram has returned invalid data.', 'wp-instagram-widget' ) );
$instagram = array();
foreach ( $images as $image ) {
$image['thumbnail_src'] = preg_replace( "/^https:/i", "", $image['thumbnail_src'] );
$image['thumbnail'] = str_replace( 's640x640', 's160x160', $image['thumbnail_src'] );
$image['small'] = str_replace( 's640x640', 's320x320', $image['thumbnail_src'] );
$image['large'] = $image['thumbnail_src'];
$image['display_src'] = preg_replace( "/^https:/i", "", $image['display_src'] );
if ( $image['is_video'] == true ) {
$type = 'video';
} else {
$type = 'image';
}
$caption = __( 'Instagram Image', 'wp-instagram-widget' );
if ( ! empty( $image['caption'] ) ) {
$caption = $image['caption'];
}
$instagram[] = array(
'description' => $caption,
'link' => '//instagram.com/p/' . $image['code'],
'time' => $image['date'],
'comments' => $image['comments']['count'],
'likes' => $image['likes']['count'],
'thumbnail' => $image['thumbnail'],
'small' => $image['small'],
'large' => $image['large'],
'original' => $image['display_src'],
'type' => $type
);
}
// do not set an empty transient - should help catch private or empty accounts
if ( ! empty( $instagram ) ) {
$instagram = base64_encode( serialize( $instagram ) );
set_transient( 'instagram-media-5-'.sanitize_title_with_dashes( $username ), $instagram, apply_filters( 'null_instagram_cache_time', HOUR_IN_SECONDS*2 ) );
}
}
if ( ! empty( $instagram ) ) {
$instagram = unserialize( base64_decode( $instagram ) );
return array_slice( $instagram, 0, $slice );
} else {
return new WP_Error( 'no_images', __( 'Instagram did not return any images.', 'wp-instagram-widget' ) );
}
}
public function images_only( $media_item ) {
if ( $media_item['type'] == 'image' )
return true;
return false;
}
}