Last active
January 30, 2018 09:16
-
-
Save Tusko/226d1008659a1001b4bdb6ced2102d69 to your computer and use it in GitHub Desktop.
Instagram API + Wordpress Cache
This file contains hidden or 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 | |
/* | |
You need to authorize your app with scope=public_content | |
Example: | |
https://api.instagram.com/oauth/authorize/?client_id=YOUR_CLIENT_ID&redirect_uri=REDIRECT_URL&response_type=token&scope=public_content | |
*/ | |
$get_my_insta = instagram_feed('USERNAME', 'APP_IP', 'ACCESS_TOKEN', 10); | |
if ( $get_my_insta ) { | |
foreach($get_my_insta as $ins) { | |
echo '<a class="instalink" href="<?php echo $ins->link; ?>" rel="nofollow" target="_blank">'; | |
$insta_file = $upload_dir['path'] . '/insta/insta-' . $ins->created_time . '.jpg'; | |
if (!file_exists($insta_file)) { | |
$insta_file = compress_image($ins->images->standard_resolution->url, $insta_file, '70'); | |
} | |
$insta_url = substr($insta_file,strlen($_SERVER['DOCUMENT_ROOT'])); | |
?> | |
<span><i class="icon_heart_alt"></i><span><?php echo $ins->likes->count; ?></span></span> | |
<?php if($ins->type == 'video') { | |
echo '<video playsinline preload="auto" autoplay="autoplay" loop="loop" muted="" tabindex="0" src="'.$ins->videos->standard_resolution->url.'"></video>'; | |
} else { | |
echo '<img src="'.placeImg($ins->images->standard_resolution->width, $ins->images->standard_resolution->height).'" data-defer="'.$insta_url.'" alt="'.strip_tags($ins->caption->text).'" width="'.$ins->images->standard_resolution->width.'" height="'.$ins->images->standard_resolution->height.'" itemprop="thumbnail" class="ac" />'; | |
} | |
echo '</a>'; | |
} |
This file contains hidden or 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 instagram_user_ID($username, $client_id, $token){ | |
$ins_id = get_transient( 'instagram_userid' ); | |
if ( false === $ins_id || empty($ins_id) ) { | |
$url = 'https://api.instagram.com/v1/users/search?q=' . $username . '&client_id=' . $client_id .'&access_token=' . $token; | |
$api = wp_remote_get( $url ); | |
$data = json_decode($api['body']); | |
$ins_id = $data->data[0]->id; | |
set_transient( 'instagram_userid', $ins_id, 12 * '3600' ); | |
} | |
return $ins_id; | |
} | |
function instagram_feed( $username = '', $client_id = '', $access_token = '', $count = 2 ) { | |
$instagram_id = instagram_user_ID($username, $client_id, $access_token); | |
$insta_feed = get_transient( 'instagram_feed' ); | |
if ( false === $insta_feed || empty($insta_feed) ) { | |
$request = 'https://api.instagram.com/v1/users/' . $instagram_id . '/media/recent/?count=' . $count . '&access_token=' . $access_token; | |
$response = wp_remote_get( $request ); | |
$data = json_decode( $response['body'] ); | |
$insta_feed = $data->data; | |
set_transient( 'instagram_feed', $insta_feed, 12 * '3600' ); | |
} | |
return $insta_feed; | |
} | |
function curl_download_image($image_url, $image_file){ | |
$fp = fopen ($image_file, 'w+'); // open file handle | |
$ch = curl_init($image_url); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
curl_setopt($ch, CURLOPT_FILE, $fp); | |
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); | |
curl_setopt($ch, CURLOPT_TIMEOUT, 1000); | |
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0'); | |
// curl_setopt($ch, CURLOPT_VERBOSE, true); // Enable this line to see debug prints | |
curl_exec($ch); | |
curl_close($ch); | |
fclose($fp); | |
} | |
function compress_image($source_url, $destination_url, $quality) { | |
curl_download_image($source_url, $destination_url); | |
$compressFile = substr($destination_url, 0, (strlen($destination_url))-(strlen(strrchr($destination_url, '.')))) . '-min.jpg'; | |
$info = getimagesize($destination_url); | |
if ($info['mime'] == 'image/jpeg') $image = imagecreatefromjpeg($destination_url); | |
elseif ($info['mime'] == 'image/gif') $image = imagecreatefromgif($destination_url); | |
elseif ($info['mime'] == 'image/png') $image = imagecreatefrompng($destination_url); | |
imagejpeg($image, $compressFile, $quality); | |
return $compressFile; | |
} | |
function remove_insta_images(){ | |
$upload_dir = wp_upload_dir(); | |
$interval = strtotime('-7 days'); | |
$dir = $upload_dir['path'] . '/insta/'; | |
$files = array_diff(scandir($dir), array('.', '..')); | |
foreach($files as $img){ | |
if (filemtime($dir . $img) < $interval ) unlink($dir . $img); | |
} | |
} | |
function insta_wp_cron_071216(){ | |
if ( !wp_next_scheduled( 'insta_wp_cron_071216' ) ) { | |
wp_schedule_event( time(), 'weekly', 'remove_insta_images'); | |
} | |
} | |
add_action('init', 'insta_wp_cron_071216'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment