Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Modified version of instagram widget by
- Author: Scott Evans
- 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.
- */
- class avia_instagram_widget extends WP_Widget {
- function __construct() {
- parent::__construct(
- 'avia-instagram-feed',
- THEMENAME ." ". __( 'Instagram', 'avia_framework' ),
- array( 'classname' => 'avia-instagram-feed', 'description' => __( 'Displays your latest Instagram photos', 'avia_framework' ) )
- );
- }
- 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'];
- $columns = empty( $instance['columns'] ) ? 3 : $instance['columns'];
- $size = empty( $instance['size'] ) ? 'large' : $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( 'aviw_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( 'aviw_images_only', FALSE ) )
- $media_array = array_filter( $media_array, array( $this, 'images_only' ) );
- // filters for custom classes
- $ulclass = esc_attr( apply_filters( 'aviw_list_class', 'av-instagram-pics av-instagram-size-' . $size ) );
- $rowclass = esc_attr( apply_filters( 'aviw_row_class', 'av-instagram-row' ) );
- $liclass = esc_attr( apply_filters( 'aviw_item_class', 'av-instagram-item' ) );
- $aclass = esc_attr( apply_filters( 'aviw_a_class', '' ) );
- $imgclass = esc_attr( apply_filters( 'aviw_img_class', '' ) );
- ?><div class="<?php echo esc_attr( $ulclass ); ?>"><?php
- $last_id = end($media_array);
- $last_id = $last_id['id'];
- $rowcount = 0;
- foreach ( $media_array as $item )
- {
- if($rowcount == 0)
- {
- echo "<div class='{$rowclass}'>";
- }
- $rowcount ++ ;
- $targeting = $target;
- if($target == "lightbox")
- {
- $targeting = "";
- $item['link'] = $item['original'];
- }
- echo '<div class="'. $liclass .'">';
- echo '<a href="'. esc_url( $item['link'] ) .'" target="'. esc_attr( $targeting ) .'" class="'. $aclass .'">';
- echo '<img src="'. esc_url( $item[$size] ) .'" alt="'. esc_attr( $item['description'] ) .'" title="'. esc_attr( $item['description'] ).'" class="'. $imgclass .'"/>';
- echo '</a></div>';
- if($rowcount % $columns == 0 || $last_id == $item['id'])
- {
- echo '</div>';
- $rowcount = 0;
- }
- }
- echo '</div>';
- }
- }
- if ( $link != '' ) {
- ?>
- <a class="av-instagram-follow avia-button" href="//instagram.com/<?php echo esc_attr( trim( $username ) ); ?>" rel="me" target="<?php echo esc_attr( $target ); ?>"><?php echo $link; ?></a><?php
- }
- do_action( 'aviw_after_widget', $instance );
- echo $after_widget;
- }
- function form( $instance )
- {
- $instance = wp_parse_args( (array) $instance, array(
- 'title' => __( 'Instagram', 'avia_framework' ),
- 'username' => '',
- 'size' => 'large',
- 'link' => __( 'Follow Me!', 'avia_framework' ),
- 'number' => 9,
- 'target' => 'lightbox' ,
- 'columns' => 3 )
- );
- $title = esc_attr( $instance['title'] );
- $username = esc_attr( $instance['username'] );
- $number = absint( $instance['number'] );
- if($number > 12) $number = 12;
- $size = esc_attr( $instance['size'] );
- $target = esc_attr( $instance['target'] );
- $link = esc_attr( $instance['link'] );
- $columns = esc_attr( $instance['columns'] );
- ?>
- <p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title', 'avia_framework' ); ?>: <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', 'avia_framework' ); ?>: <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 (maximum 12)', 'avia_framework' ); ?>: <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( 'columns' ); ?>"><?php _e( 'Number of columns', 'avia_framework' ); ?>: <input class="widefat" id="<?php echo $this->get_field_id( 'columns' ); ?>" name="<?php echo $this->get_field_name( 'columns' ); ?>" type="text" value="<?php echo $columns; ?>" /></label></p>
- <p><label for="<?php echo $this->get_field_id( 'size' ); ?>"><?php _e( 'Photo size', 'avia_framework' ); ?>:</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', 'avia_framework' ); ?></option>
- <option value="small" <?php selected( 'small', $size ) ?>><?php _e( 'Small', 'avia_framework' ); ?></option>
- <option value="large" <?php selected( 'large', $size ) ?>><?php _e( 'Large', 'avia_framework' ); ?></option>
- <option value="original" <?php selected( 'original', $size ) ?>><?php _e( 'Original', 'avia_framework' ); ?></option>
- </select>
- </p>
- <p><label for="<?php echo $this->get_field_id( 'target' ); ?>"><?php _e( 'Open links in', 'avia_framework' ); ?>:</label>
- <select id="<?php echo $this->get_field_id( 'target' ); ?>" name="<?php echo $this->get_field_name( 'target' ); ?>" class="widefat">
- <option value="lightbox" <?php selected( 'lightbox', $target ) ?>><?php _e( 'Lightbox', 'avia_framework' ); ?></option>
- <option value="_self" <?php selected( '_self', $target ) ?>><?php _e( 'Current window (_self)', 'avia_framework' ); ?></option>
- <option value="_blank" <?php selected( '_blank', $target ) ?>><?php _e( 'New window (_blank)', 'avia_framework' ); ?></option>
- </select>
- </p>
- <p><label for="<?php echo $this->get_field_id( 'link' ); ?>"><?php _e( 'Link text', 'avia_framework' ); ?>: <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['columns'] = ! absint( $new_instance['columns'] ) ? 3 : $new_instance['columns'];
- if($instance['columns'] > 6) $instance['columns'] = 6;
- if($instance['columns'] < 1) $instance['columns'] = 1;
- $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'] == 'lightbox' ) ? $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 );
- $username = str_replace( '@', '', $username );
- if ( false === ( $instagram = get_transient( 'av_insta1-'.sanitize_title_with_dashes( $username ) ) ) ) {
- //$remote = wp_remote_get( 'http://instagram.com/'.trim( $username ) );
- $remote = wp_remote_get( 'https://www.instagram.com/'.trim( $username ), array( 'sslverify' => false, 'timeout' => 60 ) );
- if ( is_wp_error( $remote ) )
- return new WP_Error( 'site_down', __( 'Unable to communicate with Instagram.', 'avia_framework' ) );
- if ( 200 != wp_remote_retrieve_response_code( $remote ) )
- return new WP_Error( 'invalid_response', __( 'Instagram did not return a 200.', 'avia_framework' ) );
- $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.', 'avia_framework' ) );
- 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 new WP_Error( 'bad_json_2', __( 'Instagram has returned invalid data.', 'avia_framework' ) );
- }
- if ( ! is_array( $images ) )
- return new WP_Error( 'bad_array', __( 'Instagram has returned invalid data.', 'avia_framework' ) );
- $instagram = array();
- foreach ( $images as $image ) {
- // see https://github.com/stevenschobert/instafeed.js/issues/549
- $image['node']['thumbnail_src'] = preg_replace( "/^https:/i", "", $image['node']['thumbnail_src'] );
- $image['node']['thumbnail'] = $image['node']['thumbnail_src'];
- $image['node']['small'] = $image['node']['thumbnail_src'];
- $image['node']['large'] = $image['node']['thumbnail_src'];
- $image['node']['display_url'] = preg_replace( "/^https:/i", "", $image['node']['display_url'] );
- if ( $image['node']['is_video'] == true ) {
- $type = 'video';
- } else {
- $type = 'image';
- }
- $caption = __( 'Instagram Image', 'avia_framework' );
- if ( ! empty( $image['node']['caption'] ) ) {
- $caption = $image['node']['caption'];
- }
- $instagram[] = array(
- 'description' => $caption,
- 'link' => '//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' => $image['node']['thumbnail'],
- 'small' => $image['node']['small'],
- 'large' => $image['node']['large'],
- 'original' => $image['node']['display_url'],
- 'type' => $type,
- 'id' => $image['node']['id']
- );
- }
- // do not set an empty transient - should help catch private or empty accounts
- if ( ! empty( $instagram ) ) {
- $instagram = base64_encode( serialize( $instagram ) );
- set_transient( 'av_insta1-'.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.', 'avia_framework' ) );
- }
- }
- function images_only( $media_item ) {
- if ( $media_item['type'] == 'image' )
- return true;
- return false;
- }
- }
Add Comment
Please, Sign In to add comment