Guest User

insta

a guest
Apr 2nd, 2018
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.08 KB | None | 0 0
  1. /*
  2. Modified version of instagram widget by
  3. Author: Scott Evans
  4. Copyright © 2013 Scott Evans
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15.  
  16. */
  17.  
  18. class avia_instagram_widget extends WP_Widget {
  19.  
  20. function __construct() {
  21. parent::__construct(
  22. 'avia-instagram-feed',
  23. THEMENAME ." ". __( 'Instagram', 'avia_framework' ),
  24. array( 'classname' => 'avia-instagram-feed', 'description' => __( 'Displays your latest Instagram photos', 'avia_framework' ) )
  25. );
  26. }
  27.  
  28. function widget( $args, $instance ) {
  29.  
  30. extract( $args, EXTR_SKIP );
  31.  
  32. $title = empty( $instance['title'] ) ? '' : apply_filters( 'widget_title', $instance['title'] );
  33. $username = empty( $instance['username'] ) ? '' : $instance['username'];
  34. $limit = empty( $instance['number'] ) ? 9 : $instance['number'];
  35. $columns = empty( $instance['columns'] ) ? 3 : $instance['columns'];
  36. $size = empty( $instance['size'] ) ? 'large' : $instance['size'];
  37. $target = empty( $instance['target'] ) ? '_self' : $instance['target'];
  38. $link = empty( $instance['link'] ) ? '' : $instance['link'];
  39.  
  40. echo $before_widget;
  41. if ( ! empty( $title ) ) { echo $before_title . $title . $after_title; };
  42.  
  43. do_action( 'aviw_before_widget', $instance );
  44.  
  45. if ( $username != '' ) {
  46.  
  47. $media_array = $this->scrape_instagram( $username, $limit );
  48.  
  49. if ( is_wp_error( $media_array ) ) {
  50.  
  51. echo $media_array->get_error_message();
  52.  
  53. } else {
  54.  
  55. // filter for images only?
  56. if ( $images_only = apply_filters( 'aviw_images_only', FALSE ) )
  57. $media_array = array_filter( $media_array, array( $this, 'images_only' ) );
  58.  
  59. // filters for custom classes
  60. $ulclass = esc_attr( apply_filters( 'aviw_list_class', 'av-instagram-pics av-instagram-size-' . $size ) );
  61. $rowclass = esc_attr( apply_filters( 'aviw_row_class', 'av-instagram-row' ) );
  62. $liclass = esc_attr( apply_filters( 'aviw_item_class', 'av-instagram-item' ) );
  63. $aclass = esc_attr( apply_filters( 'aviw_a_class', '' ) );
  64. $imgclass = esc_attr( apply_filters( 'aviw_img_class', '' ) );
  65.  
  66. ?><div class="<?php echo esc_attr( $ulclass ); ?>"><?php
  67.  
  68. $last_id = end($media_array);
  69. $last_id = $last_id['id'];
  70.  
  71. $rowcount = 0;
  72.  
  73. foreach ( $media_array as $item )
  74. {
  75. if($rowcount == 0)
  76. {
  77. echo "<div class='{$rowclass}'>";
  78. }
  79.  
  80. $rowcount ++ ;
  81. $targeting = $target;
  82. if($target == "lightbox")
  83. {
  84. $targeting = "";
  85. $item['link'] = $item['original'];
  86. }
  87.  
  88. echo '<div class="'. $liclass .'">';
  89. echo '<a href="'. esc_url( $item['link'] ) .'" target="'. esc_attr( $targeting ) .'" class="'. $aclass .'">';
  90. echo '<img src="'. esc_url( $item[$size] ) .'" alt="'. esc_attr( $item['description'] ) .'" title="'. esc_attr( $item['description'] ).'" class="'. $imgclass .'"/>';
  91. echo '</a></div>';
  92.  
  93. if($rowcount % $columns == 0 || $last_id == $item['id'])
  94. {
  95. echo '</div>';
  96. $rowcount = 0;
  97. }
  98.  
  99. }
  100. echo '</div>';
  101. }
  102. }
  103.  
  104. if ( $link != '' ) {
  105. ?>
  106. <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
  107. }
  108.  
  109. do_action( 'aviw_after_widget', $instance );
  110.  
  111. echo $after_widget;
  112. }
  113.  
  114.  
  115.  
  116. function form( $instance )
  117. {
  118.  
  119. $instance = wp_parse_args( (array) $instance, array(
  120. 'title' => __( 'Instagram', 'avia_framework' ),
  121. 'username' => '',
  122. 'size' => 'large',
  123. 'link' => __( 'Follow Me!', 'avia_framework' ),
  124. 'number' => 9,
  125. 'target' => 'lightbox' ,
  126. 'columns' => 3 )
  127. );
  128.  
  129. $title = esc_attr( $instance['title'] );
  130. $username = esc_attr( $instance['username'] );
  131. $number = absint( $instance['number'] );
  132. if($number > 12) $number = 12;
  133. $size = esc_attr( $instance['size'] );
  134. $target = esc_attr( $instance['target'] );
  135. $link = esc_attr( $instance['link'] );
  136. $columns = esc_attr( $instance['columns'] );
  137. ?>
  138. <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>
  139. <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>
  140. <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>
  141. <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>
  142. <p><label for="<?php echo $this->get_field_id( 'size' ); ?>"><?php _e( 'Photo size', 'avia_framework' ); ?>:</label>
  143. <select id="<?php echo $this->get_field_id( 'size' ); ?>" name="<?php echo $this->get_field_name( 'size' ); ?>" class="widefat">
  144. <option value="thumbnail" <?php selected( 'thumbnail', $size ) ?>><?php _e( 'Thumbnail', 'avia_framework' ); ?></option>
  145. <option value="small" <?php selected( 'small', $size ) ?>><?php _e( 'Small', 'avia_framework' ); ?></option>
  146. <option value="large" <?php selected( 'large', $size ) ?>><?php _e( 'Large', 'avia_framework' ); ?></option>
  147. <option value="original" <?php selected( 'original', $size ) ?>><?php _e( 'Original', 'avia_framework' ); ?></option>
  148. </select>
  149. </p>
  150. <p><label for="<?php echo $this->get_field_id( 'target' ); ?>"><?php _e( 'Open links in', 'avia_framework' ); ?>:</label>
  151. <select id="<?php echo $this->get_field_id( 'target' ); ?>" name="<?php echo $this->get_field_name( 'target' ); ?>" class="widefat">
  152. <option value="lightbox" <?php selected( 'lightbox', $target ) ?>><?php _e( 'Lightbox', 'avia_framework' ); ?></option>
  153. <option value="_self" <?php selected( '_self', $target ) ?>><?php _e( 'Current window (_self)', 'avia_framework' ); ?></option>
  154. <option value="_blank" <?php selected( '_blank', $target ) ?>><?php _e( 'New window (_blank)', 'avia_framework' ); ?></option>
  155. </select>
  156. </p>
  157. <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>
  158. <?php
  159. }
  160.  
  161. function update( $new_instance, $old_instance ) {
  162. $instance = $old_instance;
  163. $instance['title'] = strip_tags( $new_instance['title'] );
  164. $instance['username'] = trim( strip_tags( $new_instance['username'] ) );
  165. $instance['number'] = ! absint( $new_instance['number'] ) ? 9 : $new_instance['number'];
  166. $instance['columns'] = ! absint( $new_instance['columns'] ) ? 3 : $new_instance['columns'];
  167.  
  168. if($instance['columns'] > 6) $instance['columns'] = 6;
  169. if($instance['columns'] < 1) $instance['columns'] = 1;
  170.  
  171.  
  172. $instance['size'] = ( ( $new_instance['size'] == 'thumbnail' || $new_instance['size'] == 'large' || $new_instance['size'] == 'small' || $new_instance['size'] == 'original' ) ? $new_instance['size'] : 'large' );
  173. $instance['target'] = ( ( $new_instance['target'] == '_self' || $new_instance['target'] == '_blank'|| $new_instance['target'] == 'lightbox' ) ? $new_instance['target'] : '_self' );
  174. $instance['link'] = strip_tags( $new_instance['link'] );
  175. return $instance;
  176. }
  177.  
  178. // based on https://gist.github.com/cosmocatalano/4544576
  179. function scrape_instagram( $username, $slice = 9 ) {
  180.  
  181. $username = strtolower( $username );
  182. $username = str_replace( '@', '', $username );
  183.  
  184. if ( false === ( $instagram = get_transient( 'av_insta1-'.sanitize_title_with_dashes( $username ) ) ) ) {
  185.  
  186. //$remote = wp_remote_get( 'http://instagram.com/'.trim( $username ) );
  187. $remote = wp_remote_get( 'https://www.instagram.com/'.trim( $username ), array( 'sslverify' => false, 'timeout' => 60 ) );
  188.  
  189. if ( is_wp_error( $remote ) )
  190. return new WP_Error( 'site_down', __( 'Unable to communicate with Instagram.', 'avia_framework' ) );
  191.  
  192. if ( 200 != wp_remote_retrieve_response_code( $remote ) )
  193. return new WP_Error( 'invalid_response', __( 'Instagram did not return a 200.', 'avia_framework' ) );
  194.  
  195. $shards = explode( 'window._sharedData = ', $remote['body'] );
  196. $insta_json = explode( ';</script>', $shards[1] );
  197. $insta_array = json_decode( $insta_json[0], TRUE );
  198.  
  199. if ( ! $insta_array )
  200. return new WP_Error( 'bad_json', __( 'Instagram has returned invalid data.', 'avia_framework' ) );
  201.  
  202. if ( isset( $insta_array['entry_data']['ProfilePage'][0]['graphql']['user']['edge_owner_to_timeline_media']['edges'] ) ) {
  203. $images = $insta_array['entry_data']['ProfilePage'][0]['graphql']['user']['edge_owner_to_timeline_media']['edges'];
  204. } else {
  205. return new WP_Error( 'bad_json_2', __( 'Instagram has returned invalid data.', 'avia_framework' ) );
  206. }
  207.  
  208. if ( ! is_array( $images ) )
  209. return new WP_Error( 'bad_array', __( 'Instagram has returned invalid data.', 'avia_framework' ) );
  210.  
  211. $instagram = array();
  212.  
  213. foreach ( $images as $image ) {
  214. // see https://github.com/stevenschobert/instafeed.js/issues/549
  215. $image['node']['thumbnail_src'] = preg_replace( "/^https:/i", "", $image['node']['thumbnail_src'] );
  216. $image['node']['thumbnail'] = $image['node']['thumbnail_src'];
  217. $image['node']['small'] = $image['node']['thumbnail_src'];
  218. $image['node']['large'] = $image['node']['thumbnail_src'];
  219. $image['node']['display_url'] = preg_replace( "/^https:/i", "", $image['node']['display_url'] );
  220.  
  221. if ( $image['node']['is_video'] == true ) {
  222. $type = 'video';
  223. } else {
  224. $type = 'image';
  225. }
  226.  
  227. $caption = __( 'Instagram Image', 'avia_framework' );
  228. if ( ! empty( $image['node']['caption'] ) ) {
  229. $caption = $image['node']['caption'];
  230. }
  231.  
  232. $instagram[] = array(
  233. 'description' => $caption,
  234. 'link' => '//instagram.com/p/' . $image['node']['shortcode'],
  235. 'time' => $image['node']['taken_at_timestamp'],
  236. 'comments' => $image['node']['edge_media_to_comment']['count'],
  237. 'likes' => $image['node']['edge_liked_by']['count'],
  238. 'thumbnail' => $image['node']['thumbnail'],
  239. 'small' => $image['node']['small'],
  240. 'large' => $image['node']['large'],
  241. 'original' => $image['node']['display_url'],
  242. 'type' => $type,
  243. 'id' => $image['node']['id']
  244. );
  245. }
  246.  
  247. // do not set an empty transient - should help catch private or empty accounts
  248. if ( ! empty( $instagram ) ) {
  249. $instagram = base64_encode( serialize( $instagram ) );
  250. set_transient( 'av_insta1-'.sanitize_title_with_dashes( $username ), $instagram, apply_filters( 'null_instagram_cache_time', HOUR_IN_SECONDS*2 ) );
  251. }
  252. }
  253.  
  254. if ( ! empty( $instagram ) ) {
  255.  
  256. $instagram = unserialize( base64_decode( $instagram ) );
  257. return array_slice( $instagram, 0, $slice );
  258.  
  259. } else {
  260.  
  261. return new WP_Error( 'no_images', __( 'Instagram did not return any images.', 'avia_framework' ) );
  262.  
  263. }
  264. }
  265.  
  266. function images_only( $media_item ) {
  267.  
  268. if ( $media_item['type'] == 'image' )
  269. return true;
  270.  
  271. return false;
  272. }
  273. }
Add Comment
Please, Sign In to add comment