Instagram Slider Widget - Version 1.0.3

Version Description

  • Added Option to link images to User Profile or Image Url
  • Code Cleanup
Download this release

Release Info

Developer jetonr
Plugin Icon 128x128 Instagram Slider Widget
Version 1.0.3
Comparing to
See all releases

Code changes from version 1.0.2 to 1.0.3

{css → assets/css}/instag-slider.css RENAMED
File without changes
{js → assets/js}/jquery.flexslider-min.js RENAMED
File without changes
inc/functions.php DELETED
@@ -1,184 +0,0 @@
1
- <?php
2
- if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
3
-
4
- if ( ! function_exists( 'instag_images_data' ) ) :
5
- /**
6
- * Stores the fetched data from instagram in WordPress DB using transients
7
- *
8
- * @return array of localy saved instagram data
9
- */
10
- function instag_images_data( $username, $cache_hours, $nr_images ) {
11
-
12
- $opt_name = 'jr_insta_'.md5( $username );
13
- $instaData = get_transient( $opt_name );
14
- $user_opt = get_option( $opt_name );
15
-
16
- if (
17
- false === $instaData
18
- || $user_opt['username'] != $username
19
- || $user_opt['cache_hours'] != $cache_hours
20
- || $user_opt['nr_images'] != $nr_images
21
- )
22
- {
23
- $instaData = array();
24
- $insta_url = 'http://instagram.com/';
25
- $user_profile = $insta_url.$username;
26
- $json = wp_remote_get( $user_profile, array( 'sslverify' => false, 'timeout'=> 60 ) );
27
- $user_options = compact('username', 'cache_hours', 'nr_images');
28
- update_option($opt_name, $user_options);
29
- if ( $json['response']['code'] == 200 ) {
30
-
31
- $json = $json['body'];
32
- $json = strstr( $json, '{"entry_data"' );
33
-
34
- // Compatibility for version of php where strstr() doesnt accept third parameter
35
- if ( version_compare( phpversion(), '5.3.10', '<' ) ) {
36
- $json = substr( $json, 0, strpos($json, '</script>' ) );
37
- } else {
38
- $json = strstr( $json, '</script>', true );
39
- }
40
-
41
- $json = rtrim( $json, ';' );
42
- ( $results = json_decode( $json, true ) ) && json_last_error() == JSON_ERROR_NONE;
43
-
44
- if ( ( $results ) && is_array( $results ) ) {
45
- foreach( $results['entry_data']['UserProfile'][0]['userMedia'] as $current => $result ) {
46
-
47
- if( $current >= $nr_images ) break;
48
- $caption = $result['caption'];
49
- $image = $result['images']['standard_resolution'];
50
- $id = $result['id'];
51
- $image = $image['url'];
52
- $link = $result['link'];
53
- $created_time = $caption['created_time'];
54
- $text = utf8_4byte_to_3byte($caption['text']);
55
-
56
- $filename_data= explode('.',$image);
57
-
58
- if ( is_array( $filename_data ) ) {
59
-
60
- $fileformat = end( $filename_data );
61
-
62
- if ( $fileformat !== false ){
63
-
64
- $image = download_insta_image( $image, md5( $id ) . '.' . $fileformat );
65
- array_push( $instaData, array(
66
- 'id' => $id,
67
- 'user_name' => $username,
68
- 'user_url' => $user_profile,
69
- 'created_time'=> $created_time,
70
- 'text' => $text,
71
- 'image' => $image,
72
- 'link' => $link
73
- ));
74
-
75
- } // end -> if $fileformat !== false
76
-
77
- } // end -> is_array( $filename_data )
78
-
79
- } // end -> foreach
80
-
81
- } // end -> ( $results ) && is_array( $results ) )
82
-
83
- } // end -> $json['response']['code'] === 200 )
84
-
85
- if ( $instaData ) {
86
- set_transient( $opt_name, $instaData, $cache_hours * 60 * 60 );
87
- } // end -> true $instaData
88
-
89
- } // end -> false === $instaData
90
-
91
- return $instaData;
92
- }
93
- endif; // insta_images
94
-
95
- if ( ! function_exists( 'download_insta_image' ) ) :
96
- /**
97
- * Save Instagram images to upload folder and ads to media.
98
- * If the upload fails it returns the remote image url.
99
- *
100
- * @return url to image
101
- */
102
- function download_insta_image( $url , $file ){
103
-
104
- $local_file = JR_INSTAGWP_UPLOAD_PATH . $file;
105
-
106
- if ( file_exists( $local_file ) ) {
107
- return JR_INSTAGWP_UPLODAD_URL . $file;
108
- }
109
-
110
- $get = wp_remote_get( $url, array( 'sslverify' => false ) );
111
- $body = wp_remote_retrieve_body( $get );
112
- $upload = wp_upload_bits( $file, '', $body );
113
-
114
- if ( $upload ) {
115
- return $upload['url'];
116
- }
117
-
118
- return $url;
119
- }
120
- endif; // download_insta_image
121
-
122
- if ( ! function_exists( 'utf8_4byte_to_3byte' ) ) :
123
- /**
124
- * Sanitize 4-byte UTF8 chars; no full utf8mb4 support in drupal7+mysql stack.
125
- * This solution runs in O(n) time BUT assumes that all incoming input is
126
- * strictly UTF8.
127
- *
128
- * @return the sanitized input
129
- */
130
- function utf8_4byte_to_3byte($input) {
131
-
132
- if (!empty($input)) {
133
- $utf8_2byte = 0xC0 /*1100 0000*/; $utf8_2byte_bmask = 0xE0 /*1110 0000*/;
134
- $utf8_3byte = 0xE0 /*1110 0000*/; $utf8_3byte_bmask = 0XF0 /*1111 0000*/;
135
- $utf8_4byte = 0xF0 /*1111 0000*/; $utf8_4byte_bmask = 0xF8 /*1111 1000*/;
136
-
137
- $sanitized = "";
138
- $len = strlen($input);
139
- for ($i = 0; $i < $len; ++$i) {
140
- $mb_char = $input[$i]; // Potentially a multibyte sequence
141
- $byte = ord($mb_char);
142
- if (($byte & $utf8_2byte_bmask) == $utf8_2byte) {
143
- $mb_char .= $input[++$i];
144
- }
145
- else if (($byte & $utf8_3byte_bmask) == $utf8_3byte) {
146
- $mb_char .= $input[++$i];
147
- $mb_char .= $input[++$i];
148
- }
149
- else if (($byte & $utf8_4byte_bmask) == $utf8_4byte) {
150
- // Replace with ? to avoid MySQL exception
151
- $mb_char = '?';
152
- $i += 3;
153
- }
154
-
155
- $sanitized .= $mb_char;
156
- }
157
-
158
- $input= $sanitized;
159
- }
160
-
161
- return $input;
162
- }
163
- endif; // utf8_4byte_to_3byte
164
-
165
- if ( ! function_exists( 'instag_templates' ) ) :
166
- /**
167
- * Helper Function to insert Templates for widget
168
- *
169
- * @include file templates
170
- */
171
- function instag_templates( $template, $data_arr ){
172
-
173
- $filename = JR_INSTAGWP_PATH_TEMPLATE . $template . '.php';
174
-
175
- if(file_exists( $filename )){
176
-
177
- include $filename;
178
-
179
- } else {
180
-
181
- echo __( sprintf('Template not found<br>%s' , $filename), 'example' );
182
- }
183
- }
184
- endif; // instag_templates
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
instaram_slider.php CHANGED
@@ -2,71 +2,121 @@
2
  /*
3
  Plugin Name: Instagram Slider Widget
4
  Plugin URI: http://jrwebstudio.com/instagram-slider/
5
- Version: 1.0.2
6
  Description: Instagram Slider Widget is a responsive slider widget that shows 20 latest images from a public instagram user.
7
  Author: jetonr
8
  Author URI: http://jrwebstudio.com/
9
  License: GPLv2 or later
10
  */
11
 
12
- /* Define Constants for this widget */
13
- define('JR_INSTAGWP_PATH_BASE' , dirname(__FILE__) . DIRECTORY_SEPARATOR);
14
- define('JR_INSTAGWP_PATH_TEMPLATE' , JR_INSTAGWP_PATH_BASE . 'templates/');
15
- define('JR_INSTAGWP_PATH_INC' , JR_INSTAGWP_PATH_BASE . 'inc/');
16
- define('JR_INSTAGWP_URL' , plugins_url( '/' , __FILE__ ));
17
- define('JR_INSTAGWP_WP_VERSION' , get_bloginfo('version'));
18
- define('JR_INSTAGWP_WP_MIN_VERSION' , 3.5);
19
 
20
- $upload_dir = wp_upload_dir();
21
- define('JR_INSTAGWP_UPLOAD_PATH' , $upload_dir['path'] . '/');
22
- define('JR_INSTAGWP_UPLODAD_URL' , $upload_dir['baseurl'] . $upload_dir['subdir'] . '/');
 
23
 
24
- // Require functions need for this widget
25
- require_once ( JR_INSTAGWP_PATH_INC . 'functions.php' );
 
 
 
 
 
 
26
 
27
- /* Enqueue Frontend Plugin Styles & Scripts */
28
- function jr_insta_slider_enqueue() {
29
-
30
- // Register and enqueue Styles
31
- wp_enqueue_style( 'instag-slider', JR_INSTAGWP_URL . 'css/instag-slider.css' );
 
32
 
33
- // Register and enqueue Scripts
34
- wp_enqueue_script(
35
- 'jquery-flexi-slider',
36
- JR_INSTAGWP_URL . 'js/jquery.flexslider-min.js',
37
- array( 'jquery' ),
38
- false,
39
- true
40
- );
41
-
42
- }
43
- add_action( 'wp_enqueue_scripts', 'jr_insta_slider_enqueue' );
44
-
45
- /* Register widget on windgets init */
46
- add_action( 'widgets_init', 'jr_insta_slider_register' );
47
- function jr_insta_slider_register() {
48
- register_widget( 'JR_InstagramSlider' );
49
- }
50
 
51
- class JR_InstagramSlider extends WP_Widget {
 
52
 
53
- public function __construct() {
54
- parent::__construct(
55
- 'jr_insta_slider', //-----> Widget ID
56
- __( 'Instagram Slider', 'jrinstaslider' ), //--> Widget Name
57
- array( //-----> Widget Options Array
58
- 'classname' => 'jr-insta-slider',
59
- 'description' => __( 'A widget that displays a slider with instagram images ', 'jrinstaslider' ),
60
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  );
62
  }
63
 
64
- function widget( $args, $instance ) {
 
 
 
 
 
65
  extract( $args );
66
 
67
  //Our variables from the widget settings.
68
  $title = apply_filters('widget_title', $instance['title'] );
69
  $username = $instance['username'];
 
70
  $randomise = isset( $instance['randomise'] ) ? 'on' : 'off';
71
  $images_nr = $instance['images_number'];
72
  $refresh_hour = $instance['refresh_hour'];
@@ -79,7 +129,7 @@ class JR_InstagramSlider extends WP_Widget {
79
  echo $before_title . $title . $after_title;
80
  }
81
  // Get instagram data
82
- $insta_data = instag_images_data($username, $refresh_hour, $images_nr );
83
 
84
  // Randomise Images
85
  if ( "on" == $randomise ) {
@@ -87,20 +137,27 @@ class JR_InstagramSlider extends WP_Widget {
87
  }
88
 
89
  //include the template based on user choice
90
- instag_templates( $template, $insta_data );
91
 
92
  echo $after_widget;
93
  }
94
 
95
- //Update the widget
96
-
97
- function update( $new_instance, $old_instance ) {
 
 
 
 
 
 
98
  $instance = $old_instance;
99
 
100
  //Strip tags from title and name to remove HTML
101
  $instance['title'] = strip_tags( $new_instance['title'] );
102
  $instance['username'] = $new_instance['username'];
103
  $instance['template'] = $new_instance['template'];
 
104
  $instance['randomise'] = $new_instance['randomise'];
105
  $instance['images_number'] = $new_instance['images_number'];
106
  $instance['refresh_hour'] = $new_instance['refresh_hour'];
@@ -108,11 +165,15 @@ class JR_InstagramSlider extends WP_Widget {
108
  return $instance;
109
  }
110
 
111
-
112
- function form( $instance ) {
 
 
 
 
113
 
114
  //Set up some default widget settings.
115
- $defaults = array( 'title' => __('Instagram Slider', 'jrinstaslider'), 'username' => __('', 'jrinstaslider'), 'images_number' => 5, 'refresh_hour' => 5, 'template' => 'slider' );
116
  $instance = wp_parse_args( (array) $instance, $defaults ); ?>
117
 
118
  <p>
@@ -131,6 +192,11 @@ class JR_InstagramSlider extends WP_Widget {
131
  </select>
132
  </label>
133
  </p>
 
 
 
 
 
134
  <p>
135
  <label for="<?php echo $this->get_field_id( 'randomise' ); ?>"><?php _e( 'Randomise Images:', 'jrinstaslider' ); ?></label>
136
  <input class="widefat" id="<?php echo $this->get_field_id( 'randomise' ); ?>" name="<?php echo $this->get_field_name( 'randomise' ); ?>" type="checkbox" value="1" <?php checked( '1', $instance['randomise'] ); ?> />
@@ -150,4 +216,196 @@ class JR_InstagramSlider extends WP_Widget {
150
 
151
  <?php
152
  }
153
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  /*
3
  Plugin Name: Instagram Slider Widget
4
  Plugin URI: http://jrwebstudio.com/instagram-slider/
5
+ Version: 1.0.3
6
  Description: Instagram Slider Widget is a responsive slider widget that shows 20 latest images from a public instagram user.
7
  Author: jetonr
8
  Author URI: http://jrwebstudio.com/
9
  License: GPLv2 or later
10
  */
11
 
12
+ /**
13
+ * After the plugins have loaded initalise a single instance of JR_InstagramSlider
14
+ */
15
+ add_action( 'plugins_loaded', array( 'JR_InstagramSlider', 'get_instance' ) );
 
 
 
16
 
17
+ /**
18
+ * JR_InstagramSlider Class
19
+ */
20
+ class JR_InstagramSlider extends WP_Widget {
21
 
22
+ /**
23
+ * Plugin version, used for cache-busting of style and script file references.
24
+ *
25
+ * @since 1.0.0
26
+ *
27
+ * @var string
28
+ */
29
+ const VERSION = '1.0.3';
30
 
31
+ /**
32
+ * Instance of this class.
33
+ *
34
+ * @var object
35
+ */
36
+ protected static $instance = null;
37
 
38
+ /**
39
+ * Initialize the plugin by registering widget and loading public scripts
40
+ *
41
+ */
42
+ public function __construct() {
 
 
 
 
 
 
 
 
 
 
 
 
43
 
44
+ // Register Widget On Widgets Init
45
+ add_action( 'widgets_init', array( $this, 'register_widget' ) );
46
 
47
+ // Enqueue Plugin Styles and scripts
48
+ add_action( 'wp_enqueue_scripts', array( $this, 'public_enqueue' ) );
49
+
50
+
51
+ $widget_options = array(
52
+ 'classname' => 'jr-insta-slider',
53
+ 'description' => __( 'A widget that displays a slider with instagram images ', 'jrinstaslider' )
54
+ );
55
+
56
+ parent::__construct( 'jr_insta_slider', __('Instagram Slider', 'jrinstaslider'), $widget_options );
57
+ }
58
+
59
+ /**
60
+ * Return an instance of this class.
61
+ *
62
+ * @return object A single instance of this class.
63
+ */
64
+ public static function get_instance() {
65
+
66
+ // If the single instance hasn't been set, set it now.
67
+ if ( null == self::$instance ) {
68
+ self::$instance = new self;
69
+ }
70
+
71
+ return self::$instance;
72
+ }
73
+
74
+ /**
75
+ * Register widget on windgets init
76
+ *
77
+ * @return void
78
+ */
79
+ public function register_widget() {
80
+ register_widget( __CLASS__ );
81
+ }
82
+
83
+ /**
84
+ * Enqueue public-facing Scripts and style sheet.
85
+ *
86
+ * @return void
87
+ */
88
+ public function public_enqueue() {
89
+
90
+ // Enqueue Styles
91
+ wp_enqueue_style(
92
+ 'instag-slider',
93
+ plugins_url( 'assets/css/instag-slider.css', __FILE__ ),
94
+ array(),
95
+ self::VERSION
96
+ );
97
+
98
+ // Enqueue Scripts
99
+ wp_enqueue_script(
100
+ 'jquery-flexi-slider',
101
+ plugins_url( 'assets/js/jquery.flexslider-min.js', __FILE__ ),
102
+ array( 'jquery' ),
103
+ '2.2',
104
+ true
105
  );
106
  }
107
 
108
+ /**
109
+ * The Public view of the Widget
110
+ *
111
+ * @return mixed
112
+ */
113
+ public function widget( $args, $instance ) {
114
  extract( $args );
115
 
116
  //Our variables from the widget settings.
117
  $title = apply_filters('widget_title', $instance['title'] );
118
  $username = $instance['username'];
119
+ $images_link = $instance['images_link'];
120
  $randomise = isset( $instance['randomise'] ) ? 'on' : 'off';
121
  $images_nr = $instance['images_number'];
122
  $refresh_hour = $instance['refresh_hour'];
129
  echo $before_title . $title . $after_title;
130
  }
131
  // Get instagram data
132
+ $insta_data = $this->instagram_data( $username, $refresh_hour, $images_nr );
133
 
134
  // Randomise Images
135
  if ( "on" == $randomise ) {
137
  }
138
 
139
  //include the template based on user choice
140
+ $this->template( $template, $insta_data, $images_link );
141
 
142
  echo $after_widget;
143
  }
144
 
145
+ /**
146
+ * Update the widget settings
147
+ *
148
+ * @param array $new_instance New instance values
149
+ * @param array $old_instance Old instance values
150
+ *
151
+ * @return array
152
+ */
153
+ public function update( $new_instance, $old_instance ) {
154
  $instance = $old_instance;
155
 
156
  //Strip tags from title and name to remove HTML
157
  $instance['title'] = strip_tags( $new_instance['title'] );
158
  $instance['username'] = $new_instance['username'];
159
  $instance['template'] = $new_instance['template'];
160
+ $instance['images_link'] = $new_instance['images_link'];
161
  $instance['randomise'] = $new_instance['randomise'];
162
  $instance['images_number'] = $new_instance['images_number'];
163
  $instance['refresh_hour'] = $new_instance['refresh_hour'];
165
  return $instance;
166
  }
167
 
168
+ /**
169
+ * Widget Settings Form
170
+ *
171
+ * @return mixed
172
+ */
173
+ public function form( $instance ) {
174
 
175
  //Set up some default widget settings.
176
+ $defaults = array( 'title' => __('Instagram Slider', 'jrinstaslider'), 'username' => __('', 'jrinstaslider'), 'template' => 'slider', 'images_link' => 'image_url', 'images_number' => 5, 'refresh_hour' => 5 );
177
  $instance = wp_parse_args( (array) $instance, $defaults ); ?>
178
 
179
  <p>
192
  </select>
193
  </label>
194
  </p>
195
+ <p>
196
+ <?php _e('Link Images To:', 'jrinstaslider'); ?><br>
197
+ <label><input type="radio" id="<?php echo $this->get_field_id( 'images_link' ); ?>" name="<?php echo $this->get_field_name( 'images_link' ); ?>" value="image_url" <?php checked( 'image_url', $instance['images_link'] ); ?> /> <?php _e('Instagram Image URL', 'jrinstaslider'); ?></label><br />
198
+ <label><input type="radio" id="<?php echo $this->get_field_id( 'images_link' ); ?>" name="<?php echo $this->get_field_name( 'images_link' ); ?>" value="user_url" <?php checked( 'user_url', $instance['images_link'] ); ?> /> <?php _e('Instagram Profile URL', 'jrinstaslider'); ?></label><br />
199
+ </p>
200
  <p>
201
  <label for="<?php echo $this->get_field_id( 'randomise' ); ?>"><?php _e( 'Randomise Images:', 'jrinstaslider' ); ?></label>
202
  <input class="widefat" id="<?php echo $this->get_field_id( 'randomise' ); ?>" name="<?php echo $this->get_field_name( 'randomise' ); ?>" type="checkbox" value="1" <?php checked( '1', $instance['randomise'] ); ?> />
216
 
217
  <?php
218
  }
219
+
220
+ /**
221
+ * Stores the fetched data from instagram in WordPress DB using transients
222
+ *
223
+ * @param string $username Instagram Username to fetch images from
224
+ * @param string $cache_hours Cache hours for transient
225
+ * @param string $nr_images Nr of images to fetch from instagram
226
+ *
227
+ * @return array of localy saved instagram data
228
+ */
229
+ private function instagram_data( $username, $cache_hours, $nr_images ) {
230
+
231
+ $opt_name = 'jr_insta_'.md5( $username );
232
+ $instaData = get_transient( $opt_name );
233
+ $user_opt = get_option( $opt_name );
234
+
235
+ if (
236
+ false === $instaData
237
+ || $user_opt['username'] != $username
238
+ || $user_opt['cache_hours'] != $cache_hours
239
+ || $user_opt['nr_images'] != $nr_images
240
+ )
241
+ {
242
+ $instaData = array();
243
+ $insta_url = 'http://instagram.com/';
244
+ $user_profile = $insta_url.$username;
245
+ $json = wp_remote_get( $user_profile, array( 'sslverify' => false, 'timeout'=> 60 ) );
246
+ $user_options = compact('username', 'cache_hours', 'nr_images');
247
+ update_option($opt_name, $user_options);
248
+ if ( $json['response']['code'] == 200 ) {
249
+
250
+ $json = $json['body'];
251
+ $json = strstr( $json, '{"entry_data"' );
252
+
253
+ // Compatibility for version of php where strstr() doesnt accept third parameter
254
+ if ( version_compare( phpversion(), '5.3.10', '<' ) ) {
255
+ $json = substr( $json, 0, strpos($json, '</script>' ) );
256
+ } else {
257
+ $json = strstr( $json, '</script>', true );
258
+ }
259
+
260
+ $json = rtrim( $json, ';' );
261
+ ( $results = json_decode( $json, true ) ) && json_last_error() == JSON_ERROR_NONE;
262
+
263
+ if ( ( $results ) && is_array( $results ) ) {
264
+ foreach( $results['entry_data']['UserProfile'][0]['userMedia'] as $current => $result ) {
265
+
266
+ if( $current >= $nr_images ) break;
267
+ $caption = $result['caption'];
268
+ $image = $result['images']['standard_resolution'];
269
+ $id = $result['id'];
270
+ $image = $image['url'];
271
+ $link = $result['link'];
272
+ $created_time = $caption['created_time'];
273
+ $text = $this->utf8_4byte_to_3byte($caption['text']);
274
+
275
+ $filename_data= explode('.',$image);
276
+
277
+ if ( is_array( $filename_data ) ) {
278
+
279
+ $fileformat = end( $filename_data );
280
+
281
+ if ( $fileformat !== false ){
282
+
283
+ $image = $this->download_insta_image( $image, md5( $id ) . '.' . $fileformat );
284
+ array_push( $instaData, array(
285
+ 'id' => $id,
286
+ 'user_name' => $username,
287
+ 'user_url' => $user_profile,
288
+ 'created_time'=> $created_time,
289
+ 'text' => $text,
290
+ 'image' => $image,
291
+ 'link' => $link
292
+ ));
293
+
294
+ } // end -> if $fileformat !== false
295
+
296
+ } // end -> is_array( $filename_data )
297
+
298
+ } // end -> foreach
299
+
300
+ } // end -> ( $results ) && is_array( $results ) )
301
+
302
+ } // end -> $json['response']['code'] === 200 )
303
+
304
+ if ( $instaData ) {
305
+ set_transient( $opt_name, $instaData, $cache_hours * 60 * 60 );
306
+ } // end -> true $instaData
307
+
308
+ } // end -> false === $instaData
309
+
310
+ return $instaData;
311
+ }
312
+
313
+
314
+ /**
315
+ * Save Instagram images to upload folder and ads to media.
316
+ * If the upload fails it returns the remote image url.
317
+ *
318
+ * @param string $url Url of image to download
319
+ * @param string $file File path for image
320
+ *
321
+ * @return string Url to image
322
+ */
323
+ private function download_insta_image( $url , $file ){
324
+
325
+ $upload_dir = wp_upload_dir();
326
+ $local_file = $upload_dir['path'] . '/' . $file;
327
+
328
+ if ( file_exists( $local_file ) ) {
329
+ return $upload_dir['baseurl'] . $upload_dir['subdir'] . '/' . $file;
330
+ }
331
+
332
+ $get = wp_remote_get( $url, array( 'sslverify' => false ) );
333
+ $body = wp_remote_retrieve_body( $get );
334
+ $upload = wp_upload_bits( $file, '', $body );
335
+
336
+ if ( $upload ) {
337
+ return $upload['url'];
338
+ }
339
+
340
+ return $url;
341
+ }
342
+
343
+ /**
344
+ * Sanitize 4-byte UTF8 chars; no full utf8mb4 support in drupal7+mysql stack.
345
+ * This solution runs in O(n) time BUT assumes that all incoming input is
346
+ * strictly UTF8.
347
+ *
348
+ * @param string $input The input to be sanitised
349
+ *
350
+ * @return the sanitized input
351
+ */
352
+ private function utf8_4byte_to_3byte( $input ) {
353
+
354
+ if (!empty($input)) {
355
+ $utf8_2byte = 0xC0 /*1100 0000*/; $utf8_2byte_bmask = 0xE0 /*1110 0000*/;
356
+ $utf8_3byte = 0xE0 /*1110 0000*/; $utf8_3byte_bmask = 0XF0 /*1111 0000*/;
357
+ $utf8_4byte = 0xF0 /*1111 0000*/; $utf8_4byte_bmask = 0xF8 /*1111 1000*/;
358
+
359
+ $sanitized = "";
360
+ $len = strlen($input);
361
+ for ($i = 0; $i < $len; ++$i) {
362
+ $mb_char = $input[$i]; // Potentially a multibyte sequence
363
+ $byte = ord($mb_char);
364
+ if (($byte & $utf8_2byte_bmask) == $utf8_2byte) {
365
+ $mb_char .= $input[++$i];
366
+ }
367
+ else if (($byte & $utf8_3byte_bmask) == $utf8_3byte) {
368
+ $mb_char .= $input[++$i];
369
+ $mb_char .= $input[++$i];
370
+ }
371
+ else if (($byte & $utf8_4byte_bmask) == $utf8_4byte) {
372
+ // Replace with ? to avoid MySQL exception
373
+ $mb_char = '?';
374
+ $i += 3;
375
+ }
376
+
377
+ $sanitized .= $mb_char;
378
+ }
379
+
380
+ $input= $sanitized;
381
+ }
382
+
383
+ return $input;
384
+ }
385
+
386
+ /**
387
+ * Function to display Templates for widget
388
+ *
389
+ * @param string $template The input to be sanitised
390
+ * @param array $data_arr The input to be sanitised
391
+ * @param string $link_to The input to be sanitised
392
+ *
393
+ * @include file templates
394
+ *
395
+ * return void
396
+ */
397
+ private function template( $template, $data_arr, $link_to ){
398
+
399
+ $filename = plugin_dir_path( __FILE__ ) . "views/" . $template . '.php';
400
+
401
+ if( file_exists( $filename ) ){
402
+
403
+ include $filename;
404
+
405
+ } else {
406
+
407
+ echo __( sprintf( 'Template not found<br>%s' , $filename), 'jrinstaslider' );
408
+ }
409
+ }
410
+
411
+ } // end of class JR_InstagramSlider
readme.txt CHANGED
@@ -15,6 +15,7 @@ Instagram Slider Widget is a responsive slider widget that shows 20 latest image
15
  * Images are stored in your wordpress upload folder.
16
  * Display Images in Slider or Thumbnails
17
  * No Api Key Needed
 
18
  * Option to Randomise order of instagram images
19
  * For more info visit http://jrwebstudio.com/
20
 
@@ -30,10 +31,14 @@ Instagram Slider Widget is a responsive slider widget that shows 20 latest image
30
  3. Backend Configuration
31
 
32
  == Changelog ==
 
 
 
 
33
  = 1.0.2 =
34
- * Compatibility for php version older than 5.3
35
- * css stlying fix for thumbnail layout
36
- * Randomise Images
37
 
38
  = 1.0.1 =
39
  * Removed preg_match
15
  * Images are stored in your wordpress upload folder.
16
  * Display Images in Slider or Thumbnails
17
  * No Api Key Needed
18
+ * Link images to user profile or image url
19
  * Option to Randomise order of instagram images
20
  * For more info visit http://jrwebstudio.com/
21
 
31
  3. Backend Configuration
32
 
33
  == Changelog ==
34
+ = 1.0.3 =
35
+ * Added Option to link images to User Profile or Image Url
36
+ * Code Cleanup
37
+
38
  = 1.0.2 =
39
+ * Compatibility for php older than 5.3
40
+ * Stlying fix for thumbnail layout
41
+ * Added Option to Randomise Images
42
 
43
  = 1.0.1 =
44
  * Removed preg_match
{templates → views}/slider.php RENAMED
@@ -14,6 +14,11 @@ jQuery(document).ready(function($) {
14
  foreach ( $data as $k => $v) {
15
  $$k = $v;
16
  }
 
 
 
 
 
17
  echo '<li>'. "\n";
18
  echo '<a target="_blank" href="'.$link.'"><img src="'.$image.'" alt="'.$text.'"></a>' . "\n";
19
  if ( $created_time ) {
14
  foreach ( $data as $k => $v) {
15
  $$k = $v;
16
  }
17
+
18
+ if ( $link_to && ( 'user_url' == $link_to ) ) {
19
+ $link = $user_url;
20
+ }
21
+
22
  echo '<li>'. "\n";
23
  echo '<a target="_blank" href="'.$link.'"><img src="'.$image.'" alt="'.$text.'"></a>' . "\n";
24
  if ( $created_time ) {
{templates → views}/thumbs.php RENAMED
@@ -2,11 +2,16 @@
2
  <div class="instag">
3
  <ul class="thumbnails no-bullet">
4
  <?php
5
- if ( isset($data_arr) && is_array($data_arr) ) {
6
- foreach ($data_arr as $data) {
7
- foreach ( $data as $k => $v) {
8
  $$k = $v;
9
  }
 
 
 
 
 
10
  echo '<li>'. "\n";
11
  echo '<a target="_blank" href="'.$link.'"><img src="'.$image.'" alt="'.$text.'"></a>' . "\n";
12
  echo '</li>' . "\n";
2
  <div class="instag">
3
  <ul class="thumbnails no-bullet">
4
  <?php
5
+ if ( isset( $data_arr ) && is_array( $data_arr ) ) {
6
+ foreach ( $data_arr as $data ) {
7
+ foreach ( $data as $k => $v ) {
8
  $$k = $v;
9
  }
10
+
11
+ if ( $link_to && 'user_url' == $link_to ) {
12
+ $link = $user_url;
13
+ }
14
+
15
  echo '<li>'. "\n";
16
  echo '<a target="_blank" href="'.$link.'"><img src="'.$image.'" alt="'.$text.'"></a>' . "\n";
17
  echo '</li>' . "\n";