Last active
August 16, 2019 20:41
-
-
Save jdembowski/645eb2d5b0f9b326a2d98ef5d43c73ac to your computer and use it in GitHub Desktop.
This file contains 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 | |
/* | |
Plugin name: Import IFTTT created Instagram posts | |
Description: This plugin will identify IFTTT created Instagram posts when published and will download the image into the media library. It will also modify the post to use the media library copy. | |
Version: 0.4 | |
Author: Jan Dembowski | |
Author URI: https://blog.dembowski.net/ | |
*/ | |
add_action( 'publish_post', 'mh_ifttt_instagram', 5, 2 ); | |
function mh_ifttt_instagram( $ID , $post ) { | |
// Get some stuff I need. | |
$content = $post->post_content; | |
$urls = wp_extract_urls( $content ); | |
$mh_instagram_cat_id = get_cat_ID( "Instagram" ); | |
$mh_instagram_img = false; | |
foreach ($urls as $value) { | |
// Get the img src url that start with https://scontent.cdninstagram.com | |
if ( preg_match('/^https\:\/\/scontent\.cdninstagram\.com\//' , $value, $matches)) | |
$mh_instagram_img = $value; | |
// Get the IFTTT short link for the Instagram page | |
if ( preg_match('/^http\:\/\/ift\.tt\//', $value, $matches)) | |
$mh_instagram_link = $value; | |
} | |
// Only mess around if the two links are found | |
if ( $mh_instagram_img and $mh_instagram_link ) { | |
// Import that https://scontent.cdninstagram.com hosted image into the media library | |
$mh_local_inst_image = mh_insta_post_thumb( $ID , $mh_instagram_img ); | |
// Get rid of the http://ift.tt/ shortlink and just go with the Instagram photo URL | |
$mh_expanded_instagram_link = mh_no_shortlink( $mh_instagram_link ); | |
// Substitute the IFTTT content with what I want | |
$content = '<p><a href="' . $mh_expanded_instagram_link . '" target="_blank"><img width="1080" height="1080"'; | |
$content .= ' src="' . $mh_local_inst_image[ 'url' ] . '" class="insta-image" alt="Pool all the things" /></a></p>'; | |
$content .= '<br />'; | |
$content .= '<p><a href="' . $mh_expanded_instagram_link . '" target="_blank">View in Instagram ⇒</a></p>'; | |
// Set the featured imaged to the new image in the media library | |
set_post_thumbnail( $ID , $mh_local_inst_image[ 'id' ] ); | |
// Prep array to update the post | |
$mh_post = array( | |
'ID' => $ID, | |
'post_content' => $content, | |
'post_category' => array( $mh_instagram_cat_id ) | |
); | |
// Very weird things happened when I did not remove the action before updating | |
remove_action( 'publish_post', 'mh_ifttt_instagram' ); | |
wp_update_post( $mh_post ); | |
// Put the action back | |
add_action( 'publish_post', 'mh_ifttt_instagram', 5, 2 ); | |
} | |
} | |
function mh_insta_post_thumb( $mh_id, $mh_url) { | |
// Unashamedly copied from https://codex.wordpress.org/Function_Reference/media_handle_sideload | |
// Need to require these files | |
if ( !function_exists('media_handle_upload') ) { | |
require_once(ABSPATH . "wp-admin" . '/includes/image.php'); | |
require_once(ABSPATH . "wp-admin" . '/includes/file.php'); | |
require_once(ABSPATH . "wp-admin" . '/includes/media.php'); | |
} | |
$url = $mh_url; | |
$tmp = download_url( $url ); | |
if( is_wp_error( $tmp ) ){ | |
// download failed, handle error | |
} | |
$post_id = $mh_id; | |
$desc = get_the_title( $mh_id ); | |
$file_array = array(); | |
// Set variables for storage | |
// fix file filename for query strings | |
preg_match('/[^\?]+\.(jpg|jpe|jpeg|gif|png)/i', $url, $matches); | |
$file_array['name'] = basename($matches[0]); | |
$file_array['tmp_name'] = $tmp; | |
// If error storing temporarily, unlink | |
if ( is_wp_error( $tmp ) ) { | |
@unlink($file_array['tmp_name']); | |
$file_array['tmp_name'] = ''; | |
} | |
// do the validation and storage stuff | |
$id = media_handle_sideload( $file_array, $post_id, $desc ); | |
// If error storing permanently, unlink | |
if ( is_wp_error($id) ) { | |
@unlink($file_array['tmp_name']); | |
return $id; | |
} | |
$src = wp_get_attachment_url( $id ); | |
// I need the media ID and the URL returned | |
$mh_media_attachment = array( | |
'id' => $id, | |
'url' => $src, | |
); | |
return $mh_media_attachment; | |
} | |
function mh_no_shortlink ( $url , $count = 0 ) { | |
// After 5 recursive tries we're done. Screw that link, return nada. | |
// In this case nada means a hashmark. | |
if ( $count >= 4 ) { | |
return '#'; | |
} | |
// I do not want WordPress to follow http redirects, | |
// I want the result from the first try. | |
$mh_get_args = array( | |
'redirection' => 0, | |
); | |
// Just get the HTTP head, don't actually get the whole web page. | |
$mh_response = wp_remote_head( esc_url_raw( $url ) , $mh_get_args ); | |
// $mh_response_code = wp_remote_retrieve_response_code( $mh_response ); | |
// Look for the http header for 'location' as that indicates a redirect. | |
$mh_unshortened = wp_remote_retrieve_header( $mh_response, 'location' ); | |
if ( $mh_unshortened ) { | |
// There was a redirect? Then let's increment count and check that URL too. | |
return mh_no_shortlink( $mh_unshortened , $count + 1 ); | |
} else { | |
// If no redirect so just return the URL we were given. | |
// Store the transient for 12 hours, return the real URL and we're finished. | |
return esc_url_raw( $url ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment