Skip to content
This repository was archived by the owner on Sep 24, 2019. It is now read-only.

Commit 253a3fd

Browse files
authored
Merge pull request #80 from scottsweb/79-tag-json
Fix hashtag support after JSON changes
2 parents 994cf3b + e3950eb commit 253a3fd

File tree

3 files changed

+120
-77
lines changed

3 files changed

+120
-77
lines changed

readme.md

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -84,97 +84,100 @@ In version 1.9.6 you can now use a filter to change the location of your custom
8484

8585
## Changelog
8686

87-
####2.0.0
87+
#### 2.0.1
88+
* Fix hashtag support
89+
90+
#### 2.0.0
8891
* Ability to track tags / hashtags instead of just usernames (e.g. #cheese)
8992
* New filter for custom link class
9093
* Bundle German language (props jensteichert)
9194
* Other minor fixes and formatting
9295

93-
####1.9.8
96+
#### 1.9.8
9497
* Return more items when using filter for images only
9598

96-
####1.9.7
99+
#### 1.9.7
97100
* Fix double trailing slash on custom text link
98101

99-
####1.9.6
102+
#### 1.9.6
100103
* Trailing slash all URLs to support Instagram app on iOS
101104
* Support selective refresh in 4.5
102105
* New filter for `wpiw_template_part`
103106

104-
####1.9.5
107+
#### 1.9.5
105108
* Another fix for image URL handling, I think we have all the edge cases now!
106109

107-
####1.9.4
110+
#### 1.9.4
108111
* IMPORTANT: You must be on WordPress 4.4 or later to upgrade to this version of the plugin. This fixes a bug with PHP versions < 5.4.7. If you are on a version of WordPress older than 4.4 and you notice images are not loading then I recommend rolling your version of the widget back to this version: https://github.com/scottsweb/wp-instagram-widget/blob/c66550eded59bd51f508f304a85a3e031ac4044c/wp-instagram-widget.php
109112

110-
####1.9.3
113+
#### 1.9.3
111114
* Fix issues introduced in the last version (apologies). Certain CDN URLs are still in use
112115

113-
####1.9.2
116+
#### 1.9.2
114117
* Better handling of all image and video image URLs props @thegallagher
115118

116-
####1.9.1
119+
#### 1.9.1
117120
* Fix bug with some image URLs
118121

119-
####1.9
122+
#### 1.9
120123
* WordPress.com VIP checked
121124
* Updated CDN matching to ensure correct image sizes are served at all times
122125
* Shortened the transient key - should mean we bump into the character limit less often
123126
* Remove use of `extract`
124127

125-
####1.8.1
128+
#### 1.8.1
126129
* Apologies for all the recent updates, all good things though!
127130
* Update plugin text domain ready for translate.wordpress.org
128131
* Introduce an 'original' image size if you want to use non-square originals
129132
* Introduce new filter for `<ul>` class `wpiw_list_class`
130133

131-
####1.8
134+
#### 1.8
132135
* Bring back image captions
133136
* Fix small PHP error in 1.7
134137

135-
####1.7
138+
#### 1.7
136139
* Bring back image sizes
137140
* Use thumbnails that are square (originals are in the data returned and can be used)
138141
* Remove @ from usernames
139142

140-
####1.6
143+
#### 1.6
141144
* Compatibility with 4.3
142145

143-
####1.5.1
146+
#### 1.5.1
144147
* Invalidate old transients
145148

146-
####1.5
149+
#### 1.5
147150
* Remove null framework support
148151
* Fix breaking change by Instagram whilst maintaining old style support
149152
* Remove thumbnail size option
150153

151-
####1.4
154+
#### 1.4
152155
* Introduce class filters
153156
* Only set a transient if images are returned
154157
* Optional template part for complete output control
155158

156-
####1.3.1
159+
#### 1.3.1
157160
* Force lowercase usernames
158161
* Correct hook name
159162

160-
####1.3
163+
#### 1.3
161164
* Option to open links in new window
162165
* Support for video items (with filter to disable this)
163166
* New actions for adding custom output to the widget
164167
* Support for https://
165168
* Correctly escape attributes
166169

167-
####1.2.1
170+
#### 1.2.1
168171
* Change transient name due to data change
169172

170-
####1.2
173+
#### 1.2
171174
* Better error handling
172175
* Encode emoji as they cause transient issues
173176

174-
####1.1
177+
#### 1.1
175178
* Fix issue with Instagram feed
176179
* Add composer.json
177180

178-
####1.0
181+
#### 1.0
179182
* Initial release
180183

readme.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Contributors: scottsweb, codeforthepeople
33
Tags: instagram, widget, photos, photography, hipster, sidebar, widgets, simple
44
Requires at least: 4.4
55
Tested up to: 4.8.2
6-
Stable tag: 2.0.0
6+
Stable tag: 2.0.1
77
License: GPLv2 or later
88
License URI: http://www.gnu.org/licenses/gpl-2.0.html
99

@@ -80,6 +80,9 @@ In version 1.9.6 you can now use a filter to change the location of your custom
8080

8181
== Changelog ==
8282

83+
= 2.0.1 =
84+
* Fix hashtag support
85+
8386
= 2.0.0 =
8487
* Ability to track tags / hashtags instead of just usernames (e.g. #cheese)
8588
* New filter for custom link class

wp-instagram-widget.php

Lines changed: 90 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Plugin Name: WP Instagram Widget
44
Plugin URI: https://github.com/scottsweb/wp-instagram-widget
55
Description: A WordPress widget for showing your latest Instagram photos.
6-
Version: 2.0.0
6+
Version: 2.0.1
77
Author: Scott Evans
88
Author URI: https://scott.ee
99
Text Domain: wp-instagram-widget
@@ -135,7 +135,14 @@ function widget( $args, $instance ) {
135135
}
136136

137137
function form( $instance ) {
138-
$instance = wp_parse_args( (array) $instance, array( 'title' => __( 'Instagram', 'wp-instagram-widget' ), 'username' => '', 'size' => 'large', 'link' => __( 'Follow Me!', 'wp-instagram-widget' ), 'number' => 9, 'target' => '_self' ) );
138+
$instance = wp_parse_args( (array) $instance, array(
139+
'title' => __( 'Instagram', 'wp-instagram-widget' ),
140+
'username' => '',
141+
'size' => 'large',
142+
'link' => __( 'Follow Me!', 'wp-instagram-widget' ),
143+
'number' => 9,
144+
'target' => '_self',
145+
) );
139146
$title = $instance['title'];
140147
$username = $instance['username'];
141148
$number = absint( $instance['number'] );
@@ -148,16 +155,16 @@ function form( $instance ) {
148155
<p><label for="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>"><?php esc_html_e( 'Number of photos', 'wp-instagram-widget' ); ?>: <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( $number ); ?>" /></label></p>
149156
<p><label for="<?php echo esc_attr( $this->get_field_id( 'size' ) ); ?>"><?php esc_html_e( 'Photo size', 'wp-instagram-widget' ); ?>:</label>
150157
<select id="<?php echo esc_attr( $this->get_field_id( 'size' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'size' ) ); ?>" class="widefat">
151-
<option value="thumbnail" <?php selected( 'thumbnail', $size ) ?>><?php esc_html_e( 'Thumbnail', 'wp-instagram-widget' ); ?></option>
152-
<option value="small" <?php selected( 'small', $size ) ?>><?php esc_html_e( 'Small', 'wp-instagram-widget' ); ?></option>
153-
<option value="large" <?php selected( 'large', $size ) ?>><?php esc_html_e( 'Large', 'wp-instagram-widget' ); ?></option>
154-
<option value="original" <?php selected( 'original', $size ) ?>><?php esc_html_e( 'Original', 'wp-instagram-widget' ); ?></option>
158+
<option value="thumbnail" <?php selected( 'thumbnail', $size ); ?>><?php esc_html_e( 'Thumbnail', 'wp-instagram-widget' ); ?></option>
159+
<option value="small" <?php selected( 'small', $size ); ?>><?php esc_html_e( 'Small', 'wp-instagram-widget' ); ?></option>
160+
<option value="large" <?php selected( 'large', $size ); ?>><?php esc_html_e( 'Large', 'wp-instagram-widget' ); ?></option>
161+
<option value="original" <?php selected( 'original', $size ); ?>><?php esc_html_e( 'Original', 'wp-instagram-widget' ); ?></option>
155162
</select>
156163
</p>
157164
<p><label for="<?php echo esc_attr( $this->get_field_id( 'target' ) ); ?>"><?php esc_html_e( 'Open links in', 'wp-instagram-widget' ); ?>:</label>
158165
<select id="<?php echo esc_attr( $this->get_field_id( 'target' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'target' ) ); ?>" class="widefat">
159-
<option value="_self" <?php selected( '_self', $target ) ?>><?php esc_html_e( 'Current window (_self)', 'wp-instagram-widget' ); ?></option>
160-
<option value="_blank" <?php selected( '_blank', $target ) ?>><?php esc_html_e( 'New window (_blank)', 'wp-instagram-widget' ); ?></option>
166+
<option value="_self" <?php selected( '_self', $target ); ?>><?php esc_html_e( 'Current window (_self)', 'wp-instagram-widget' ); ?></option>
167+
<option value="_blank" <?php selected( '_blank', $target ); ?>><?php esc_html_e( 'New window (_blank)', 'wp-instagram-widget' ); ?></option>
161168
</select>
162169
</p>
163170
<p><label for="<?php echo esc_attr( $this->get_field_id( 'link' ) ); ?>"><?php esc_html_e( 'Link text', 'wp-instagram-widget' ); ?>: <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'link' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'link' ) ); ?>" type="text" value="<?php echo esc_attr( $link ); ?>" /></label></p>
@@ -181,7 +188,7 @@ function scrape_instagram( $username ) {
181188

182189
$username = trim( strtolower( $username ) );
183190

184-
if ( false === ( $instagram = get_transient( 'instagram-a7-' . sanitize_title_with_dashes( $username ) ) ) ) {
191+
if ( false === ( $instagram = get_transient( 'instagram-a8-' . sanitize_title_with_dashes( $username ) ) ) ) {
185192

186193
switch ( substr( $username, 0, 1 ) ) {
187194
case '#':
@@ -213,8 +220,8 @@ function scrape_instagram( $username ) {
213220

214221
if ( isset( $insta_array['entry_data']['ProfilePage'][0]['user']['media']['nodes'] ) ) {
215222
$images = $insta_array['entry_data']['ProfilePage'][0]['user']['media']['nodes'];
216-
} else if ( isset( $insta_array['entry_data']['TagPage'][0]['tag']['media']['nodes'] ) ) {
217-
$images = $insta_array['entry_data']['TagPage'][0]['tag']['media']['nodes'];
223+
} elseif ( isset( $insta_array['entry_data']['TagPage'][0]['graphql']['hashtag']['edge_hashtag_to_media']['edges'] ) ) {
224+
$images = $insta_array['entry_data']['TagPage'][0]['graphql']['hashtag']['edge_hashtag_to_media']['edges'];
218225
} else {
219226
return new WP_Error( 'bad_json_2', esc_html__( 'Instagram has returned invalid data.', 'wp-instagram-widget' ) );
220227
}
@@ -226,54 +233,84 @@ function scrape_instagram( $username ) {
226233
$instagram = array();
227234

228235
foreach ( $images as $image ) {
229-
230-
$image['thumbnail_src'] = preg_replace( '/^https?\:/i', '', $image['thumbnail_src'] );
231-
$image['display_src'] = preg_replace( '/^https?\:/i', '', $image['display_src'] );
232-
233-
// handle both types of CDN url.
234-
if ( ( strpos( $image['thumbnail_src'], 's640x640' ) !== false ) ) {
235-
$image['thumbnail'] = str_replace( 's640x640', 's160x160', $image['thumbnail_src'] );
236-
$image['small'] = str_replace( 's640x640', 's320x320', $image['thumbnail_src'] );
237-
} else {
238-
$urlparts = wp_parse_url( $image['thumbnail_src'] );
239-
$pathparts = explode( '/', $urlparts['path'] );
240-
array_splice( $pathparts, 3, 0, array( 's160x160' ) );
241-
$image['thumbnail'] = '//' . $urlparts['host'] . implode( '/', $pathparts );
242-
$pathparts[3] = 's320x320';
243-
$image['small'] = '//' . $urlparts['host'] . implode( '/', $pathparts );
244-
}
245-
246-
$image['large'] = $image['thumbnail_src'];
247-
248-
if ( true === $image['is_video'] ) {
249-
$type = 'video';
250-
} else {
251-
$type = 'image';
236+
// the hashtag json is now completely different
237+
switch ( substr( $username, 0, 1 ) ) {
238+
case '#':
239+
if ( true === $image['node']['is_video'] ) {
240+
$type = 'video';
241+
} else {
242+
$type = 'image';
243+
}
244+
245+
$caption = __( 'Instagram Image', 'wp-instagram-widget' );
246+
if ( ! empty( $image['node']['edge_media_to_caption']['edges'][0]['node']['text'] ) ) {
247+
$caption = $image['node']['edge_media_to_caption']['edges'][0]['node']['text'];
248+
}
249+
250+
$instagram[] = array(
251+
'description' => $caption,
252+
'link' => trailingslashit( '//instagram.com/p/' . $image['node']['shortcode'] ),
253+
'time' => $image['node']['taken_at_timestamp'],
254+
'comments' => $image['node']['edge_media_to_comment']['count'],
255+
'likes' => $image['node']['edge_liked_by']['count'],
256+
'thumbnail' => str_replace( 's150x150', 's160x160', preg_replace( '/^https?\:/i', '', $image['node']['thumbnail_resources'][0]['src'] ) ),
257+
'small' => preg_replace( '/^https?\:/i', '', $image['node']['thumbnail_resources'][2]['src'] ),
258+
'large' => preg_replace( '/^https?\:/i', '', $image['node']['thumbnail_resources'][4]['src'] ),
259+
'original' => preg_replace( '/^https?\:/i', '', $image['node']['display_url'] ),
260+
'type' => $type,
261+
);
262+
break;
263+
default:
264+
$image['thumbnail_src'] = preg_replace( '/^https?\:/i', '', $image['thumbnail_src'] );
265+
$image['display_src'] = preg_replace( '/^https?\:/i', '', $image['display_src'] );
266+
267+
// handle both types of CDN url.
268+
if ( ( strpos( $image['thumbnail_src'], 's640x640' ) !== false ) ) {
269+
$image['thumbnail'] = str_replace( 's640x640', 's160x160', $image['thumbnail_src'] );
270+
$image['small'] = str_replace( 's640x640', 's320x320', $image['thumbnail_src'] );
271+
} else {
272+
$urlparts = wp_parse_url( $image['thumbnail_src'] );
273+
$pathparts = explode( '/', $urlparts['path'] );
274+
array_splice( $pathparts, 3, 0, array( 's160x160' ) );
275+
$image['thumbnail'] = '//' . $urlparts['host'] . implode( '/', $pathparts );
276+
$pathparts[3] = 's320x320';
277+
$image['small'] = '//' . $urlparts['host'] . implode( '/', $pathparts );
278+
}
279+
280+
$image['large'] = $image['thumbnail_src'];
281+
282+
if ( true === $image['is_video'] ) {
283+
$type = 'video';
284+
} else {
285+
$type = 'image';
286+
}
287+
288+
$caption = __( 'Instagram Image', 'wp-instagram-widget' );
289+
if ( ! empty( $image['caption'] ) ) {
290+
$caption = $image['caption'];
291+
}
292+
293+
$instagram[] = array(
294+
'description' => $caption,
295+
'link' => trailingslashit( '//instagram.com/p/' . $image['code'] ),
296+
'time' => $image['date'],
297+
'comments' => $image['comments']['count'],
298+
'likes' => $image['likes']['count'],
299+
'thumbnail' => $image['thumbnail'],
300+
'small' => $image['small'],
301+
'large' => $image['large'],
302+
'original' => $image['display_src'],
303+
'type' => $type,
304+
);
305+
306+
break;
252307
}
253-
254-
$caption = __( 'Instagram Image', 'wp-instagram-widget' );
255-
if ( ! empty( $image['caption'] ) ) {
256-
$caption = $image['caption'];
257-
}
258-
259-
$instagram[] = array(
260-
'description' => $caption,
261-
'link' => trailingslashit( '//instagram.com/p/' . $image['code'] ),
262-
'time' => $image['date'],
263-
'comments' => $image['comments']['count'],
264-
'likes' => $image['likes']['count'],
265-
'thumbnail' => $image['thumbnail'],
266-
'small' => $image['small'],
267-
'large' => $image['large'],
268-
'original' => $image['display_src'],
269-
'type' => $type,
270-
);
271308
} // End foreach().
272309

273310
// do not set an empty transient - should help catch private or empty accounts.
274311
if ( ! empty( $instagram ) ) {
275312
$instagram = base64_encode( serialize( $instagram ) );
276-
set_transient( 'instagram-a7-' . sanitize_title_with_dashes( $username ), $instagram, apply_filters( 'null_instagram_cache_time', HOUR_IN_SECONDS * 2 ) );
313+
set_transient( 'instagram-a8-' . sanitize_title_with_dashes( $username ), $instagram, apply_filters( 'null_instagram_cache_time', HOUR_IN_SECONDS * 2 ) );
277314
}
278315
}
279316

0 commit comments

Comments
 (0)