| 1 |
<?php |
| 2 |
|
| 3 |
if( !class_exists( 'HTinstagram_feed' )){ |
| 4 |
|
| 5 |
class HTinstagram_feed{ |
| 6 |
|
| 7 |
/** |
| 8 |
* [$_instance] |
| 9 |
* @var null |
| 10 |
*/ |
| 11 |
private static $_instance = null; |
| 12 |
|
| 13 |
/** |
| 14 |
* [instance] Initializes a singleton instance |
| 15 |
* @return [HTinstagram_feed] |
| 16 |
*/ |
| 17 |
public static function instance() { |
| 18 |
if ( is_null( self::$_instance ) ) { |
| 19 |
self::$_instance = new self(); |
| 20 |
} |
| 21 |
return self::$_instance; |
| 22 |
} |
| 23 |
|
| 24 |
// Instagram Feed |
| 25 |
public function htinstagram_items_feed( $settings = array(), $id = '' ){ |
| 26 |
|
| 27 |
$access_token = htinstagram_get_option( 'instagram_access_token','htinstagram_general_tabs' ); |
| 28 |
$access_token = !empty( $access_token ) ? $access_token : ''; |
| 29 |
$limit = !empty( $settings['limit'] ) ? $settings['limit'] : 8; |
| 30 |
|
| 31 |
$cache_duration = $this->get_cacheing_duration( $settings['cash_time_duration'] ); |
| 32 |
$transient_var = 'htfeed_instragram_data_' .$id.'_'.$limit; |
| 33 |
|
| 34 |
if( $settings['delete_cache'] === 'yes' ){ |
| 35 |
delete_transient( $transient_var ); |
| 36 |
$cache_duration = MINUTE_IN_SECONDS; |
| 37 |
} |
| 38 |
|
| 39 |
if( empty( $access_token ) ){ |
| 40 |
echo '<p>'.esc_html__('Please enter your access token.','htmega-addons').'</p>'; |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
if ( false === ( $items = get_transient( $transient_var ) ) ) { |
| 45 |
|
| 46 |
$url = 'https://graph.instagram.com/me/media?fields=id,caption,media_type,media_url,permalink,thumbnail_url,timestamp,username&limit=200&access_token='.esc_html($access_token); |
| 47 |
|
| 48 |
$response = wp_remote_get( $url ); |
| 49 |
$instagram_data = wp_remote_retrieve_body( $response ); |
| 50 |
|
| 51 |
$instagram_data = json_decode( $instagram_data, true ); |
| 52 |
|
| 53 |
if ( ! is_wp_error( $instagram_data ) && 200 == (int) wp_remote_retrieve_response_code( $response ) ) { |
| 54 |
|
| 55 |
if ( isset( $instagram_data['error']['message'] ) ) { |
| 56 |
echo '<p>'.esc_html__('Incorrect access token specified.','htmega-addons').'</p>'; |
| 57 |
} |
| 58 |
|
| 59 |
$items = array(); |
| 60 |
foreach ( $instagram_data['data'] as $data_item ) { |
| 61 |
$item['id'] = $data_item['id']; |
| 62 |
$item['media_type'] = $data_item['media_type']; |
| 63 |
$item['src'] = $data_item['media_url']; |
| 64 |
$item['username'] = $data_item['username']; |
| 65 |
$item['link'] = $data_item['permalink']; |
| 66 |
$item['timestamp'] = $data_item['timestamp']; |
| 67 |
$item['caption'] = !empty( $data_item['caption'] ) ? $data_item['caption'] : ''; |
| 68 |
$items[] = $item; |
| 69 |
} |
| 70 |
set_transient( $transient_var, $items, 1 * $cache_duration ); |
| 71 |
|
| 72 |
} |
| 73 |
|
| 74 |
} |
| 75 |
return $items; |
| 76 |
|
| 77 |
} |
| 78 |
|
| 79 |
protected function get_cacheing_duration( $duration ){ |
| 80 |
switch ( $duration ) { |
| 81 |
case 'minute': |
| 82 |
$cache_duration = MINUTE_IN_SECONDS; |
| 83 |
break; |
| 84 |
case 'hour': |
| 85 |
$cache_duration = HOUR_IN_SECONDS; |
| 86 |
break; |
| 87 |
case 'day': |
| 88 |
$cache_duration = DAY_IN_SECONDS; |
| 89 |
break; |
| 90 |
case 'week': |
| 91 |
$cache_duration = WEEK_IN_SECONDS; |
| 92 |
break; |
| 93 |
case 'month': |
| 94 |
$cache_duration = MONTH_IN_SECONDS; |
| 95 |
break; |
| 96 |
case 'year': |
| 97 |
$cache_duration = YEAR_IN_SECONDS; |
| 98 |
break; |
| 99 |
default: |
| 100 |
$cache_duration = DAY_IN_SECONDS; |
| 101 |
} |
| 102 |
return $cache_duration; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
} |
| 107 |
|
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
?> |