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

Commit

Permalink
Merge pull request #80 from scottsweb/79-tag-json
Browse files Browse the repository at this point in the history
Fix hashtag support after JSON changes
  • Loading branch information
scottsweb authored Jan 10, 2018
2 parents 994cf3b + e3950eb commit 253a3fd
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 77 deletions.
49 changes: 26 additions & 23 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,97 +84,100 @@ In version 1.9.6 you can now use a filter to change the location of your custom

## Changelog

####2.0.0
#### 2.0.1
* Fix hashtag support

#### 2.0.0
* Ability to track tags / hashtags instead of just usernames (e.g. #cheese)
* New filter for custom link class
* Bundle German language (props jensteichert)
* Other minor fixes and formatting

####1.9.8
#### 1.9.8
* Return more items when using filter for images only

####1.9.7
#### 1.9.7
* Fix double trailing slash on custom text link

####1.9.6
#### 1.9.6
* Trailing slash all URLs to support Instagram app on iOS
* Support selective refresh in 4.5
* New filter for `wpiw_template_part`

####1.9.5
#### 1.9.5
* Another fix for image URL handling, I think we have all the edge cases now!

####1.9.4
#### 1.9.4
* 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

####1.9.3
#### 1.9.3
* Fix issues introduced in the last version (apologies). Certain CDN URLs are still in use

####1.9.2
#### 1.9.2
* Better handling of all image and video image URLs props @thegallagher

####1.9.1
#### 1.9.1
* Fix bug with some image URLs

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

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

####1.8
#### 1.8
* Bring back image captions
* Fix small PHP error in 1.7

####1.7
#### 1.7
* Bring back image sizes
* Use thumbnails that are square (originals are in the data returned and can be used)
* Remove @ from usernames

####1.6
#### 1.6
* Compatibility with 4.3

####1.5.1
#### 1.5.1
* Invalidate old transients

####1.5
#### 1.5
* Remove null framework support
* Fix breaking change by Instagram whilst maintaining old style support
* Remove thumbnail size option

####1.4
#### 1.4
* Introduce class filters
* Only set a transient if images are returned
* Optional template part for complete output control

####1.3.1
#### 1.3.1
* Force lowercase usernames
* Correct hook name

####1.3
#### 1.3
* Option to open links in new window
* Support for video items (with filter to disable this)
* New actions for adding custom output to the widget
* Support for https://
* Correctly escape attributes

####1.2.1
#### 1.2.1
* Change transient name due to data change

####1.2
#### 1.2
* Better error handling
* Encode emoji as they cause transient issues

####1.1
#### 1.1
* Fix issue with Instagram feed
* Add composer.json

####1.0
#### 1.0
* Initial release

5 changes: 4 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Contributors: scottsweb, codeforthepeople
Tags: instagram, widget, photos, photography, hipster, sidebar, widgets, simple
Requires at least: 4.4
Tested up to: 4.8.2
Stable tag: 2.0.0
Stable tag: 2.0.1
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html

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

== Changelog ==

= 2.0.1 =
* Fix hashtag support

= 2.0.0 =
* Ability to track tags / hashtags instead of just usernames (e.g. #cheese)
* New filter for custom link class
Expand Down
143 changes: 90 additions & 53 deletions wp-instagram-widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
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: 2.0.0
Version: 2.0.1
Author: Scott Evans
Author URI: https://scott.ee
Text Domain: wp-instagram-widget
Expand Down Expand Up @@ -135,7 +135,14 @@ function widget( $args, $instance ) {
}

function form( $instance ) {
$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' ) );
$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',
) );
$title = $instance['title'];
$username = $instance['username'];
$number = absint( $instance['number'] );
Expand All @@ -148,16 +155,16 @@ function form( $instance ) {
<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>
<p><label for="<?php echo esc_attr( $this->get_field_id( 'size' ) ); ?>"><?php esc_html_e( 'Photo size', 'wp-instagram-widget' ); ?>:</label>
<select id="<?php echo esc_attr( $this->get_field_id( 'size' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'size' ) ); ?>" class="widefat">
<option value="thumbnail" <?php selected( 'thumbnail', $size ) ?>><?php esc_html_e( 'Thumbnail', 'wp-instagram-widget' ); ?></option>
<option value="small" <?php selected( 'small', $size ) ?>><?php esc_html_e( 'Small', 'wp-instagram-widget' ); ?></option>
<option value="large" <?php selected( 'large', $size ) ?>><?php esc_html_e( 'Large', 'wp-instagram-widget' ); ?></option>
<option value="original" <?php selected( 'original', $size ) ?>><?php esc_html_e( 'Original', 'wp-instagram-widget' ); ?></option>
<option value="thumbnail" <?php selected( 'thumbnail', $size ); ?>><?php esc_html_e( 'Thumbnail', 'wp-instagram-widget' ); ?></option>
<option value="small" <?php selected( 'small', $size ); ?>><?php esc_html_e( 'Small', 'wp-instagram-widget' ); ?></option>
<option value="large" <?php selected( 'large', $size ); ?>><?php esc_html_e( 'Large', 'wp-instagram-widget' ); ?></option>
<option value="original" <?php selected( 'original', $size ); ?>><?php esc_html_e( 'Original', 'wp-instagram-widget' ); ?></option>
</select>
</p>
<p><label for="<?php echo esc_attr( $this->get_field_id( 'target' ) ); ?>"><?php esc_html_e( 'Open links in', 'wp-instagram-widget' ); ?>:</label>
<select id="<?php echo esc_attr( $this->get_field_id( 'target' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'target' ) ); ?>" class="widefat">
<option value="_self" <?php selected( '_self', $target ) ?>><?php esc_html_e( 'Current window (_self)', 'wp-instagram-widget' ); ?></option>
<option value="_blank" <?php selected( '_blank', $target ) ?>><?php esc_html_e( 'New window (_blank)', 'wp-instagram-widget' ); ?></option>
<option value="_self" <?php selected( '_self', $target ); ?>><?php esc_html_e( 'Current window (_self)', 'wp-instagram-widget' ); ?></option>
<option value="_blank" <?php selected( '_blank', $target ); ?>><?php esc_html_e( 'New window (_blank)', 'wp-instagram-widget' ); ?></option>
</select>
</p>
<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>
Expand All @@ -181,7 +188,7 @@ function scrape_instagram( $username ) {

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

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

switch ( substr( $username, 0, 1 ) ) {
case '#':
Expand Down Expand Up @@ -213,8 +220,8 @@ function scrape_instagram( $username ) {

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

foreach ( $images as $image ) {

$image['thumbnail_src'] = preg_replace( '/^https?\:/i', '', $image['thumbnail_src'] );
$image['display_src'] = preg_replace( '/^https?\:/i', '', $image['display_src'] );

// handle both types of CDN url.
if ( ( strpos( $image['thumbnail_src'], 's640x640' ) !== false ) ) {
$image['thumbnail'] = str_replace( 's640x640', 's160x160', $image['thumbnail_src'] );
$image['small'] = str_replace( 's640x640', 's320x320', $image['thumbnail_src'] );
} else {
$urlparts = wp_parse_url( $image['thumbnail_src'] );
$pathparts = explode( '/', $urlparts['path'] );
array_splice( $pathparts, 3, 0, array( 's160x160' ) );
$image['thumbnail'] = '//' . $urlparts['host'] . implode( '/', $pathparts );
$pathparts[3] = 's320x320';
$image['small'] = '//' . $urlparts['host'] . implode( '/', $pathparts );
}

$image['large'] = $image['thumbnail_src'];

if ( true === $image['is_video'] ) {
$type = 'video';
} else {
$type = 'image';
// the hashtag json is now completely different
switch ( substr( $username, 0, 1 ) ) {
case '#':
if ( true === $image['node']['is_video'] ) {
$type = 'video';
} else {
$type = 'image';
}

$caption = __( 'Instagram Image', 'wp-instagram-widget' );
if ( ! empty( $image['node']['edge_media_to_caption']['edges'][0]['node']['text'] ) ) {
$caption = $image['node']['edge_media_to_caption']['edges'][0]['node']['text'];
}

$instagram[] = array(
'description' => $caption,
'link' => trailingslashit( '//instagram.com/p/' . $image['node']['shortcode'] ),
'time' => $image['node']['taken_at_timestamp'],
'comments' => $image['node']['edge_media_to_comment']['count'],
'likes' => $image['node']['edge_liked_by']['count'],
'thumbnail' => str_replace( 's150x150', 's160x160', preg_replace( '/^https?\:/i', '', $image['node']['thumbnail_resources'][0]['src'] ) ),
'small' => preg_replace( '/^https?\:/i', '', $image['node']['thumbnail_resources'][2]['src'] ),
'large' => preg_replace( '/^https?\:/i', '', $image['node']['thumbnail_resources'][4]['src'] ),
'original' => preg_replace( '/^https?\:/i', '', $image['node']['display_url'] ),
'type' => $type,
);
break;
default:
$image['thumbnail_src'] = preg_replace( '/^https?\:/i', '', $image['thumbnail_src'] );
$image['display_src'] = preg_replace( '/^https?\:/i', '', $image['display_src'] );

// handle both types of CDN url.
if ( ( strpos( $image['thumbnail_src'], 's640x640' ) !== false ) ) {
$image['thumbnail'] = str_replace( 's640x640', 's160x160', $image['thumbnail_src'] );
$image['small'] = str_replace( 's640x640', 's320x320', $image['thumbnail_src'] );
} else {
$urlparts = wp_parse_url( $image['thumbnail_src'] );
$pathparts = explode( '/', $urlparts['path'] );
array_splice( $pathparts, 3, 0, array( 's160x160' ) );
$image['thumbnail'] = '//' . $urlparts['host'] . implode( '/', $pathparts );
$pathparts[3] = 's320x320';
$image['small'] = '//' . $urlparts['host'] . implode( '/', $pathparts );
}

$image['large'] = $image['thumbnail_src'];

if ( true === $image['is_video'] ) {
$type = 'video';
} else {
$type = 'image';
}

$caption = __( 'Instagram Image', 'wp-instagram-widget' );
if ( ! empty( $image['caption'] ) ) {
$caption = $image['caption'];
}

$instagram[] = array(
'description' => $caption,
'link' => trailingslashit( '//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,
);

break;
}

$caption = __( 'Instagram Image', 'wp-instagram-widget' );
if ( ! empty( $image['caption'] ) ) {
$caption = $image['caption'];
}

$instagram[] = array(
'description' => $caption,
'link' => trailingslashit( '//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,
);
} // End foreach().

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

Expand Down

0 comments on commit 253a3fd

Please sign in to comment.