Created
March 19, 2018 15:57
-
-
Save congthien/847b4cc8b42d228d4c65d2b4df8d0d0b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function boston_pro_insgaram_widget() { | |
register_widget( 'Boston_Pro_Instagram_Widget' ); | |
} | |
add_action( 'widgets_init', 'boston_pro_insgaram_widget' ); | |
class Boston_Pro_Instagram_Widget extends WP_Widget { | |
function __construct() { | |
parent::__construct( | |
'instagram-feed', | |
esc_html__( 'Instagram', 'boston-pro' ), | |
array( | |
'classname' => 'instagram-feed', | |
'description' => esc_html__( 'Displays your latest Instagram photos', 'boston-pro' ), | |
'customize_selective_refresh' => true | |
) | |
); | |
} | |
function widget( $args, $instance ) { | |
$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'] ) ? 'large' : $instance['size']; | |
$target = empty( $instance['target'] ) ? '_self' : $instance['target']; | |
$show_button = isset( $instance['show_button'] ) ? absint( $instance['show_button'] ) : 1; | |
$button_text = ( isset( $instance['button_text'] ) && $instance['button_text'] ) ? $instance['button_text'] : esc_html__( 'Follow me @instagram!', 'boston-pro' ); | |
echo $args['before_widget']; | |
if ( ! empty( $title ) ) { echo $args['before_title'] . wp_kses_post( $title ) . $args['after_title']; }; | |
do_action( 'wpiw_before_widget', $instance ); | |
if ( $username != '' ) { | |
$media_array = $this->scrape_instagram( $username ); | |
if ( is_wp_error( $media_array ) ) { | |
echo wp_kses_post( $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' ) ); | |
} | |
// slice list down to required limit | |
$media_array = array_slice( $media_array, 0, $limit ); | |
// filters for custom classes | |
$ulclass = apply_filters( 'wpiw_list_class', 'instagram-pics instagram-size-' . $size ); | |
$liclass = apply_filters( 'wpiw_item_class', '' ); | |
$aclass = apply_filters( 'wpiw_a_class', '' ); | |
$imgclass = apply_filters( 'wpiw_img_class', '' ); | |
// copy the else line into a new file (parts/wp-instagram-widget.php) within your theme and customise according | |
$template_part = apply_filters( 'wpiw_template_part', 'parts/wp-instagram-widget.php' ); | |
?><ul data-number="<?php echo count( $media_array ); ?>" class="<?php echo esc_attr( $ulclass ); ?>"><?php | |
foreach ( $media_array as $item ) { | |
if ( locate_template( $template_part ) != '' ) { | |
include locate_template( $template_part ); | |
} else { | |
echo '<li class="'. esc_attr( $liclass ) .'"><a href="'. esc_url( $item['link'] ) .'" target="'. esc_attr( $target ) .'" class="'. esc_attr( $aclass ) .'"><img src="'. esc_url( $item[$size] ) .'" alt="'. esc_attr( $item['title'] ) .'" title="'. esc_attr( $item['title'] ).'" class="'. esc_attr( $imgclass ) .'"/></a></li>'; | |
} | |
} | |
?></ul><?php | |
if ( $show_button == 1 ) { | |
?> | |
<a class="instagram-button" href="<?php echo esc_url( trailingslashit( 'https://www.instagram.com/'.$username ) ); ?>" rel="me" target="<?php echo esc_attr( $target ); ?>"><span class="genericon genericon-instagram"></span><?php echo esc_html( $button_text ); ?></a> | |
<?php | |
} | |
} | |
} | |
do_action( 'wpiw_after_widget', $instance ); | |
echo $args['after_widget']; | |
} | |
function form( $instance ) { | |
$instance = wp_parse_args( (array) $instance, array( 'title' => esc_html__( 'Instagram', 'boston-pro' ), 'username' => '', 'size' => 'large', 'link' => esc_html__( 'Follow Me!', 'boston-pro' ), 'number' => 9, 'target' => '_self' ) ); | |
$title = $instance['title']; | |
$username = $instance['username']; | |
$number = absint( $instance['number'] ); | |
$size = $instance['size']; | |
$target = $instance['target']; | |
$show_button = isset( $instance['show_button'] ) ? absint( $instance['show_button'] ) : 1; | |
$button_text = ( isset( $instance['button_text'] ) && $instance['button_text'] ) ? $instance['button_text'] : esc_html__( 'Follow me @instagram!', 'boston-pro' ); | |
?> | |
<p><label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title', 'boston-pro' ); ?>: <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( $title ); ?>" /></label></p> | |
<p><label for="<?php echo esc_attr( $this->get_field_id( 'username' ) ); ?>"><?php esc_html_e( 'Username', 'boston-pro' ); ?>: <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( $username ); ?>" /></label></p> | |
<p><label for="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>"><?php esc_html_e( 'Number of photos', 'boston-pro' ); ?>: <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', 'boston-pro' ); ?>:</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', 'boston-pro' ); ?></option> | |
<option value="small" <?php selected( 'small', $size ) ?>><?php esc_html_e( 'Small', 'boston-pro' ); ?></option> | |
<option value="large" <?php selected( 'large', $size ) ?>><?php esc_html_e( 'Large', 'boston-pro' ); ?></option> | |
<option value="original" <?php selected( 'original', $size ) ?>><?php esc_html_e( 'Original', 'boston-pro' ); ?></option> | |
</select> | |
</p> | |
<p><label for="<?php echo esc_attr( $this->get_field_id( 'target' ) ); ?>"><?php esc_html_e( 'Open links in', 'boston-pro' ); ?>:</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)', 'boston-pro' ); ?></option> | |
<option value="_blank" <?php selected( '_blank', $target ) ?>><?php esc_html_e( 'New window (_blank)', 'boston-pro' ); ?></option> | |
</select> | |
</p> | |
<p><label for="<?php echo esc_attr( $this->get_field_id( 'show_button' ) ); ?>"> | |
<input <?php echo checked( $show_button , 1 ); ?> class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'show_button' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'show_button' ) ); ?>" type="checkbox" value="1" /> | |
<?php esc_html_e( 'Show button', 'boston-pro' ); ?> | |
</label> | |
</p> | |
<p><label for="<?php echo esc_attr( $this->get_field_id( 'button_text' ) ); ?>"><?php esc_html_e( 'Button text', 'boston-pro' ); ?>: <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'button_text' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'button_text' ) ); ?>" type="text" value="<?php echo esc_attr( $button_text ); ?>" /></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'] == 'original' ) ? $new_instance['size'] : 'large' ); | |
$instance['target'] = ( ( $new_instance['target'] == '_self' || $new_instance['target'] == '_blank' ) ? $new_instance['target'] : '_self' ); | |
$instance['show_button'] = ( $new_instance['show_button'] == 1 ) ? 1 : 0 ; | |
$instance['button_text'] = trim( $new_instance['button_text'] ); | |
return $instance; | |
} | |
// based on https://gist.github.com/cosmocatalano/4544576 | |
function scrape_instagram( $username ) { | |
$username = strtolower($username); | |
$username = str_replace('@', '', $username); | |
$remote = wp_remote_get('https://instagram.com/' . trim($username)); | |
if ( is_wp_error( $remote ) ) { | |
return false; | |
} | |
if ( 200 != wp_remote_retrieve_response_code($remote) ) { | |
return false; | |
} | |
$shards = explode('window._sharedData = ', $remote['body']); | |
$insta_json = explode(';</script>', $shards[1]); | |
$insta_array = json_decode($insta_json[0], TRUE); | |
if ( ! $insta_array ) | |
return false; | |
if (isset( $insta_array['entry_data']['ProfilePage'][0]['graphql']['user']['edge_owner_to_timeline_media']['edges'] )) { | |
$images = $insta_array['entry_data']['ProfilePage'][0]['graphql']['user']['edge_owner_to_timeline_media']['edges']; | |
} else { | |
return false; | |
} | |
if ( ! is_array( $images ) ) { | |
return false; | |
} | |
$instagram = array(); | |
foreach ($images as $image) { | |
if ( true === $image['node']['is_video'] ) { | |
$type = 'video'; | |
} else { | |
$type = 'image'; | |
} | |
$caption = __( 'Instagram Image', 'onepress' ); | |
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( | |
'title' => $caption, | |
'link' => trailingslashit( '//instagram.com/p/' . $image['node']['shortcode'] ), | |
'thumbnail' => preg_replace( '/^https?\:/i', '', $image['node']['thumbnail_resources'][4]['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'] ) | |
); | |
} | |
return $instagram; | |
} | |
function images_only( $media_item ) { | |
if ( $media_item['type'] == 'image' ) | |
return true; | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment