forked from scottsweb/wp-instagram-widget
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp-instagram-widget.php
228 lines (176 loc) · 10.3 KB
/
wp-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
<?php
/*
Plugin Name: WP Instagram Widget
Plugin URI: https://github.com/scottsweb/wp-instagram-widget
Description: A WordPress widget for showing your latest Instagram photos.
Version: 1.4.1
Author: Scott Evans
Author URI: http://scott.ee
Text Domain: wpiw
Domain Path: /assets/languages/
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Copyright © 2013 Scott Evans
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
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.
*/
function wpiw_init() {
// define some constants
define( 'WP_INSTAGRAM_WIDGET_JS_URL', plugins_url( '/assets/js', __FILE__ ) );
define( 'WP_INSTAGRAM_WIDGET_CSS_URL', plugins_url( '/assets/css', __FILE__ ) );
define( 'WP_INSTAGRAM_WIDGET_IMAGES_URL', plugins_url( '/assets/images', __FILE__ ) );
define( 'WP_INSTAGRAM_WIDGET_PATH', dirname( __FILE__ ) );
define( 'WP_INSTAGRAM_WIDGET_BASE', plugin_basename( __FILE__ ) );
define( 'WP_INSTAGRAM_WIDGET_FILE', __FILE__ );
// load language files
load_plugin_textdomain( 'wpiw', false, dirname( WP_INSTAGRAM_WIDGET_BASE ) . '/assets/languages/' );
}
add_action( 'init', 'wpiw_init' );
function wpiw_widget() {
register_widget( 'null_instagram_widget' );
}
add_action( 'widgets_init', 'wpiw_widget' );
class null_instagram_widget extends WP_Widget {
function null_instagram_widget() {
$widget_ops = array( 'classname' => 'null-instagram-feed', 'description' => __( 'Displays your latest Instagram photos', 'wpiw' ) );
$this->WP_Widget( 'null-instagram-feed', __( 'Instagram', 'wpiw' ), $widget_ops );
}
function widget( $args, $instance ) {
extract( $args, EXTR_SKIP );
$title = empty( $instance['title'] ) ? '' : apply_filters( 'widget_title', $instance['title'] );
$username = empty( $instance['username'] ) ? '' : $instance['username'];
$limit = empty( $instance['number'] ) ? 9 : $instance['number'];
$size = empty( $instance['size'] ) ? 'thumbnail' : $instance['size'];
$target = empty( $instance['target'] ) ? '_self' : $instance['target'];
$link = empty( $instance['link'] ) ? '' : $instance['link'];
echo $before_widget;
if ( !empty( $title ) ) { echo $before_title . $title . $after_title; };
do_action( 'wpiw_before_widget', $instance );
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' ) );
// filters for custom classes
$liclass = esc_attr( apply_filters( 'wpiw_item_class', '' ) );
$aclass = esc_attr( apply_filters( 'wpiw_a_class', '' ) );
$imgclass = esc_attr( apply_filters( 'wpiw_img_class', '' ) );
?><ul class="instagram-pics instagram-size-<?php echo esc_attr( $instance['size'] ); ?>"><?php
foreach ( $media_array as $item ) {
// copy the else line into a new file (parts/wp-instagram-widget.php) within your theme and customise accordingly
if ( locate_template( 'parts/wp-instagram-widget.php' ) != '' ) {
include locate_template( 'parts/wp-instagram-widget.php' );
} else {
echo '<li class="'. $liclass .'"><a href="'. esc_url( $item['link'] ) .'" target="'. esc_attr( $target ) .'" class="'. $aclass .'"><img src="'. esc_url( $item[$size]['url'] ) .'" alt="'. esc_attr( $item['description'] ) .'" title="'. esc_attr( $item['description'] ).'" class="'. $imgclass .'"/></a></li>';
}
}
?></ul><?php
}
}
if ( $link != '' ) {
?><p class="clear"><a href="//instagram.com/<?php echo esc_attr( trim( $username ) ); ?>" rel="me" target="<?php echo esc_attr( $target ); ?>"><?php echo $link; ?></a></p><?php
}
do_action( 'wpiw_after_widget', $instance );
echo $after_widget;
}
function form( $instance ) {
$instance = wp_parse_args( (array) $instance, array( 'title' => __( 'Instagram', 'wpiw' ), 'username' => '', 'link' => __( 'Follow Us', 'wpiw' ), 'number' => 9, 'size' => 'thumbnail', 'target' => '_self' ) );
$title = esc_attr( $instance['title'] );
$username = esc_attr( $instance['username'] );
$number = absint( $instance['number'] );
$size = esc_attr( $instance['size'] );
$target = esc_attr( $instance['target'] );
$link = esc_attr( $instance['link'] );
?>
<p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title', 'wpiw' ); ?>: <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; ?>" /></label></p>
<p><label for="<?php echo $this->get_field_id( 'username' ); ?>"><?php _e( 'Username', 'wpiw' ); ?>: <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; ?>" /></label></p>
<p><label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number of photos', 'wpiw' ); ?>: <input class="widefat" id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" type="text" value="<?php echo $number; ?>" /></label></p>
<p><label for="<?php echo $this->get_field_id( 'size' ); ?>"><?php _e( 'Photo size', 'wpiw' ); ?>:</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', $size ) ?>><?php _e( 'Thumbnail', 'wpiw' ); ?></option>
<option value="small" <?php selected( 'small', $size ) ?>><?php _e( 'Small', 'wpiw' ); ?></option>
<option value="large" <?php selected( 'large', $size ) ?>><?php _e( 'Large', 'wpiw' ); ?></option>
</select>
</p>
<p><label for="<?php echo $this->get_field_id( 'target' ); ?>"><?php _e( 'Open links in', 'wpiw' ); ?>:</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', $target ) ?>><?php _e( 'Current window (_self)', 'wpiw' ); ?></option>
<option value="_blank" <?php selected( '_blank', $target ) ?>><?php _e( 'New window (_blank)', 'wpiw' ); ?></option>
</select>
</p>
<p><label for="<?php echo $this->get_field_id( 'link' ); ?>"><?php _e( 'Link text', 'wpiw' ); ?>: <input class="widefat" id="<?php echo $this->get_field_id( 'link' ); ?>" name="<?php echo $this->get_field_name( 'link' ); ?>" type="text" value="<?php echo $link; ?>" /></label></p>
<?php
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['username'] = trim( strip_tags( $new_instance['username'] ) );
$instance['number'] = !absint( $new_instance['number'] ) ? 9 : $new_instance['number'];
$instance['size'] = ( ( $new_instance['size'] == 'thumbnail' || $new_instance['size'] == 'large' || $new_instance['size'] == 'small' ) ? $new_instance['size'] : 'thumbnail' );
$instance['target'] = ( ( $new_instance['target'] == '_self' || $new_instance['target'] == '_blank' ) ? $new_instance['target'] : '_self' );
$instance['link'] = strip_tags( $new_instance['link'] );
return $instance;
}
// based on https://gist.github.com/cosmocatalano/4544576
function scrape_instagram( $username, $slice = 9 ) {
$username = strtolower( $username );
if ( false === ( $instagram = get_transient( 'instagram-media-'.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.', 'wpiw' ) );
if ( 200 != wp_remote_retrieve_response_code( $remote ) )
return new WP_Error( 'invalid_response', __( 'Instagram did not return a 200.', 'wpiw' ) );
$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.', 'wpiw' ) );
$images = $insta_array['entry_data']['UserProfile'][0]['userMedia'];
$instagram = array();
foreach ( $images as $image ) {
if ( $image['user']['username'] == $username ) {
$image['link'] = preg_replace( "/^http:/i", "", $image['link'] );
$image['images']['thumbnail'] = preg_replace( "/^http:/i", "", $image['images']['thumbnail'] );
$image['images']['standard_resolution'] = preg_replace( "/^http:/i", "", $image['images']['standard_resolution'] );
$image['images']['low_resolution'] = preg_replace( "/^http:/i", "", $image['images']['low_resolution'] );
$instagram[] = array(
'description' => $image['caption']['text'],
'link' => $image['link'],
'time' => $image['created_time'],
'comments' => $image['comments']['count'],
'likes' => $image['likes']['count'],
'thumbnail' => $image['images']['thumbnail'],
'large' => $image['images']['standard_resolution'],
'small' => $image['images']['low_resolution'],
'type' => $image['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-'.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.', 'wpiw' ) );
}
}
function images_only( $media_item ) {
if ( $media_item['type'] == 'image' )
return true;
return false;
}
}